3 # debianqueued -- daemon for managing Debian upload queues
5 # Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
6 # Copyright (C) 2001-2007 Ryan Murray <rmurray@debian.org>
7 # Copyright (C) 2008 Thomas Viehmann <tv@beamnet.de>
9 # This program is free software. You can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation: either version 2 or
12 # (at your option) any later version.
13 # This program comes with ABSOLUTELY NO WARRANTY!
19 use POSIX qw( strftime sys_stat_h sys_wait_h signal_h );
22 use Socket qw( PF_INET AF_INET SOCK_STREAM );
28 setlocale(&POSIX::LC_ALL, "C");
30 # ---------------------------------------------------------------------------
32 # ---------------------------------------------------------------------------
35 ( $conf::queued_dir = ( ( $0 !~ m,^/, ) ? POSIX::getcwd() . "/" : "" ) . $0 )
37 require "$conf::queued_dir/config";
38 my $junk = $conf::debug; # avoid spurious warnings about unused vars
39 $junk = $conf::ssh_key_file;
40 $junk = $conf::stray_remove_timeout;
41 $junk = $conf::problem_report_timeout;
42 $junk = $conf::queue_delay;
43 $junk = $conf::keep_files;
44 $junk = $conf::valid_files;
45 $junk = $conf::max_upload_retries;
46 $junk = $conf::upload_delay_1;
47 $junk = $conf::upload_delay_2;
51 $junk = $conf::check_md5sum;
55 $junk = $conf::ftpdebug;
56 $junk = $conf::ftptimeout;
57 $junk = $conf::no_changes_timeout;
58 $junk = @conf::nonus_packages;
59 $junk = @conf::test_binaries;
60 $junk = @conf::maintainer_mail;
61 $junk = @conf::targetdir_delayed;
62 $junk = $conf::mail ||= '/usr/sbin/sendmail';
63 $junk = $conf::overridemail;
64 $conf::target = "localhost" if $conf::upload_method eq "copy";
68 ( $main::progname = $0 ) =~ s,.*/,,;
70 ($main::hostname, undef, undef, undef, undef) = gethostbyname(hostname());
73 my $re_file_safe_prefix = qr/\A([a-zA-Z0-9][a-zA-Z0-9_.:~+-]*)/s;
74 my $re_file_safe = qr/$re_file_safe_prefix\z/s;
76 # extract -r and -k args
78 if ( @ARGV == 1 && $ARGV[0] =~ /^-[rk]$/ ) {
79 $main::arg = ( $ARGV[0] eq '-k' ) ? "kill" : "restart";
83 # test for another instance of the queued already running
84 my ( $pid, $delayed_dirs, $adelayedcore );
85 if ( open( PIDFILE, "<$conf::pidfile" ) ) {
86 chomp( $pid = <PIDFILE> );
90 # remove stale pid file
91 unlink($conf::pidfile);
92 } elsif ($main::arg) {
94 print "Killing running daemon (pid $pid) ...";
97 while ( kill( 0, $pid ) && $cnt-- > 0 ) {
101 if ( kill( 0, $pid ) ) {
102 print " failed!\nProcess $pid still running.\n";
106 if ( -e "$conf::incoming/core" ) {
107 unlink("$conf::incoming/core");
108 print "(Removed core file)\n";
110 for ( $delayed_dirs = 0 ;
111 $delayed_dirs <= $conf::max_delayed ;
115 sprintf( "$conf::incoming_delayed/core", $delayed_dirs );
116 if ( -e $adelayedcore ) {
117 unlink($adelayedcore);
118 print "(Removed core file)\n";
120 } ## end for ( $delayed_dirs = 0...
121 exit 0 if $main::arg eq "kill";
123 die "Another $main::progname is already running (pid $pid)\n"
124 if $pid && kill( 0, $pid );
126 } elsif ( $main::arg eq "kill" ) {
127 die "No daemon running\n";
128 } elsif ( $main::arg eq "restart" ) {
129 print "(No daemon running; starting anyway)\n";
132 # if started without arguments (initial invocation), then fork
135 # now go to background
136 die "$main::progname: fork failed: $!\n"
137 unless defined( $pid = fork );
140 # parent: wait for signal from child (SIGCHLD or SIGUSR1) and exit
141 my $sigset = POSIX::SigSet->new();
143 $SIG{"CHLD"} = sub { };
144 $SIG{"USR1"} = sub { };
145 POSIX::sigsuspend($sigset);
146 waitpid( $pid, WNOHANG );
147 if ( kill( 0, $pid ) ) {
148 print "Daemon (on $main::hostname) started in background (pid $pid)\n";
157 if ( $conf::upload_method eq "ssh" ) {
159 # exec an ssh-agent that starts us again
160 # force shell to be /bin/sh, ssh-agent may base its decision
161 # whether to use a fd or a Unix socket on the shell...
162 $ENV{"SHELL"} = "/bin/sh";
163 exec $conf::ssh_agent, $0, "startup", getppid();
164 die "$main::progname: Could not exec $conf::ssh_agent: $!\n";
167 # no need to exec, just set up @ARGV as expected below
168 @ARGV = ( "startup", getppid() );
170 } ## end else [ if ($pid)
171 } ## end if ( !@ARGV )
172 die "Please start without any arguments.\n"
173 if @ARGV != 2 || $ARGV[0] ne "startup";
174 my $parent_pid = $ARGV[1];
178 ( $version = 'Release: 0.95' ) =~ s/\$ ?//g;
179 print "debianqueued $version\n";
182 # check if all programs exist
184 foreach $prg ( $conf::gpg, $conf::ssh, $conf::scp, $conf::ssh_agent,
185 $conf::ssh_add, $conf::md5sum, $conf::mail, $conf::mkfifo )
187 die "Required program $prg doesn't exist or isn't executable\n"
190 # check for correct upload method
191 die "Bad upload method '$conf::upload_method'.\n"
192 if $conf::upload_method ne "ssh"
193 && $conf::upload_method ne "ftp"
194 && $conf::upload_method ne "copy";
195 die "No keyrings\n" if !@conf::keyrings;
197 } ## end foreach $prg ( $conf::gpg, ...
198 die "statusfile path must be absolute."
199 if $conf::statusfile !~ m,^/,;
200 die "upload and target queue paths must be absolute."
201 if $conf::incoming !~ m,^/,
202 || $conf::incoming_delayed !~ m,^/,
203 || $conf::targetdir !~ m,^/,
204 || $conf::targetdir_delayed !~ m,^/,;
206 # ---------------------------------------------------------------------------
208 # ---------------------------------------------------------------------------
213 sub get_filelist_from_known_good_changes($);
214 sub age_delayed_queues();
215 sub process_changes($\@);
216 sub process_commands($);
217 sub age_delayed_queues();
218 sub is_on_target($\@);
219 sub copy_to_target(@);
222 sub check_incoming_writable();
224 sub write_status_file();
225 sub print_status($$$$$$);
226 sub format_status_num(\$$);
227 sub format_status_str(\$$);
239 sub check_incoming_writable();
242 sub is_debian_file($);
243 sub get_maintainer($);
244 sub debian_file_stem($);
250 sub try_to_get_mail_addr($$);
254 sub unblock_signals();
257 sub restart_statusd();
260 $ENV{"PATH"} = "/bin:/usr/bin";
261 $ENV{"IFS"} = "" if defined( $ENV{"IFS"} && $ENV{"IFS"} ne "" );
274 sub ST_CTIME() { 10 }
276 # fixed lengths of data items passed over status pipe
277 sub STATNUM_LEN() { 30 }
278 sub STATSTR_LEN() { 128 }
280 # init list of signals
281 defined $Config{sig_name}
282 or die "$main::progname: No signal list defined!\n";
285 foreach $name ( split( ' ', $Config{sig_name} ) ) {
286 $main::signo{$name} = $i++;
289 @main::fatal_signals = qw( INT QUIT ILL TRAP ABRT BUS FPE USR2 SEGV PIPE
290 TERM XCPU XFSZ PWR );
292 $main::block_sigset = POSIX::SigSet->new;
293 $main::block_sigset->addset( $main::signo{"INT"} );
294 $main::block_sigset->addset( $main::signo{"TERM"} );
296 # some constant net stuff
297 $main::tcp_proto = ( getprotobyname('tcp') )[2]
298 or die "Cannot get protocol number for 'tcp'\n";
299 my $used_service = ( $conf::upload_method eq "ssh" ) ? "ssh" : "ftp";
300 $main::echo_port = ( getservbyname( $used_service, 'tcp' ) )[2]
301 or die "Cannot get port number for service '$used_service'\n";
303 # clear queue of stored mails
304 @main::stored_mails = ();
306 # run ssh-add to bring the key into the agent (will use stdin/stdout)
307 if ( $conf::upload_method eq "ssh" ) {
308 system "$conf::ssh_add $conf::ssh_key_file"
309 and die "$main::progname: Running $conf::ssh_add failed "
310 . "(exit status ", $? >> 8, ")\n";
313 # change to queue dir
314 chdir($conf::incoming)
315 or die "$main::progname: cannot cd to $conf::incoming: $!\n";
317 # needed before /dev/null redirects, some system send a SIGHUP when loosing
318 # the controlling tty
319 $SIG{"HUP"} = "IGNORE";
321 # open logfile, make it unbuffered
322 open( LOG, ">>$conf::logfile" )
323 or die "Cannot open my logfile $conf::logfile: $!\n";
324 chmod( 0644, $conf::logfile )
325 or die "Cannot set modes of $conf::logfile: $!\n";
326 select( ( select(LOG), $| = 1 )[0] );
329 $SIG{"HUP"} = \&close_log;
331 # redirect stdin, ... to /dev/null
332 open( STDIN, "</dev/null" )
333 or die "$main::progname: Can't redirect stdin to /dev/null: $!\n";
334 open( STDOUT, ">&LOG" )
335 or die "$main::progname: Can't redirect stdout to $conf::logfile: $!\n";
336 open( STDERR, ">&LOG" )
337 or die "$main::progname: Can't redirect stderr to $conf::logfile: $!\n";
339 # ok, from this point usually no "die" anymore, stderr is gone!
340 msg( "log", "daemon (pid $$) (on $main::hostname) started\n" );
342 # initialize variables used by send_status before launching the status daemon
344 format_status_num( $main::next_run, time + 10 );
345 format_status_str( $main::current_changes, "" );
347 $main::incoming_writable = 1; # assume this for now
349 # start the daemon watching the 'status' FIFO
350 if ( $conf::statusfile && $conf::statusdelay == 0 ) {
351 $main::statusd_pid = fork_statusd();
352 $SIG{"CHLD"} = \&kid_died; # watch out for dead status daemon
353 # SIGUSR1 triggers status info
354 $SIG{"USR1"} = \&send_status;
355 } ## end if ( $conf::statusfile...
356 $main::maind_pid = $$;
359 kill( $main::signo{"ABRT"}, $$ )
360 if defined $main::signo{"ABRT"};
364 open( PIDFILE, ">$conf::pidfile" )
365 or msg( "log", "Can't open $conf::pidfile: $!\n" );
366 printf PIDFILE "%5d\n", $$;
368 chmod( 0644, $conf::pidfile )
369 or die "Cannot set modes of $conf::pidfile: $!\n";
371 # other signals will just log an error and exit
372 foreach (@main::fatal_signals) {
373 $SIG{$_} = \&fatal_signal;
376 # send signal to user-started process that we're ready and it can exit
377 kill( $main::signo{"USR1"}, $parent_pid );
379 # ---------------------------------------------------------------------------
381 # ---------------------------------------------------------------------------
383 # default to classical incoming/target
384 $main::current_incoming = $conf::incoming;
385 $main::current_targetdir = $conf::targetdir;
388 write_status_file() if $conf::statusdelay;
391 # ping target only if there is the possibility that we'll contact it (but
392 # also don't wait too long).
393 my @have_changes = <*.changes *.commands *.dak-commands>;
394 for ( my $delayed_dirs = 0 ;
395 $delayed_dirs <= $conf::max_delayed ;
398 my $adelayeddir = sprintf( "$conf::incoming_delayed", $delayed_dirs );
399 push( @have_changes, <$adelayeddir/*.changes> );
400 } ## end for ( my $delayed_dirs ...
402 if @have_changes || ( time - $main::last_ping_time ) > 8 * 60 * 60;
404 if ( @have_changes && $main::target_up ) {
405 check_incoming_writable if !$main::incoming_writable;
406 check_dir() if $main::incoming_writable;
409 write_status_file() if $conf::statusdelay;
411 if ( $conf::upload_method eq "copy" ) {
412 age_delayed_queues();
415 # sleep() returns if we received a signal (SIGUSR1 for status FIFO), so
416 # calculate the end time once and wait for it being reached.
417 format_status_num( $main::next_run, time + $conf::queue_delay );
419 while ( ( $delta = calc_delta() ) > 0 ) {
420 debug("mainloop sleeping $delta secs");
423 # check if statusd died, if using status FIFO, or update status file
424 if ($conf::statusdelay) {
429 } ## end while ( ( $delta = calc_delta...
435 $delta = $main::next_run - time;
436 $delta = $conf::statusdelay
437 if $conf::statusdelay && $conf::statusdelay < $delta;
439 } ## end sub calc_delta()
441 # ---------------------------------------------------------------------------
442 # main working functions
443 # ---------------------------------------------------------------------------
446 # main function for checking the incoming dir
449 my ( @files, @changes, @keep_files, @this_keep_files, @stats, $file,
452 debug("starting checkdir");
454 write_status_file() if $conf::statusdelay;
456 # test if needed binaries are available; this is if they're on maybe
457 # slow-mounted NFS filesystems
458 foreach (@conf::test_binaries) {
461 # maybe the mount succeeds now
464 msg( "log", "binary test failed for $_; delaying queue run\n" );
466 } ## end foreach (@conf::test_binaries)
468 for ( $adelay = -1 ; $adelay <= $conf::max_delayed ; $adelay++ ) {
469 if ( $adelay == -1 ) {
470 $main::current_incoming = $conf::incoming;
471 $main::current_incoming_short = "";
472 $main::current_targetdir = $conf::targetdir;
474 $main::current_incoming = sprintf( $conf::incoming_delayed, $adelay );
475 $main::current_incoming_short = sprintf( "DELAYED/%d-day", $adelay );
476 $main::current_targetdir = sprintf( $conf::targetdir_delayed, $adelay );
479 # need to clear directory specific variables
481 undef(@this_keep_files);
483 chdir($main::current_incoming)
487 "Cannot change to dir "
488 . "${main::current_incoming_short}: $!\n"
493 # look for *.commands and *.dak-commands files but not in delayed queues
494 if ( $adelay == -1 ) {
495 foreach $file (<*.commands>) {
498 process_commands($file);
501 write_status_file() if $conf::statusdelay;
503 } ## end foreach $file (<*.commands>)
504 foreach $file (<*.dak-commands>) {
507 process_dak_commands($file);
510 write_status_file() if $conf::statusdelay;
513 } ## end if ( $adelay == -1 )
517 "log", "Cannot open dir ${main::current_incoming_short}: $!\n"
521 @files = readdir(INC);
524 # process all .changes files found
525 @changes = grep /\.changes$/, @files;
526 push( @keep_files, @changes ); # .changes files aren't stray
527 foreach $file (@changes) {
530 # wrap in an eval to allow jumpbacks to here with die in case
533 eval { process_changes( $file, @this_keep_files ); };
535 msg( "log,mail", $@ ) if $@;
537 write_status_file() if $conf::statusdelay;
539 # files which are ok in conjunction with this .changes
540 debug("$file tells to keep @this_keep_files");
541 push( @keep_files, @this_keep_files );
544 # break out of this loop if the incoming dir has become unwritable
545 goto end_run if !$main::incoming_writable;
546 } ## end foreach $file (@changes)
547 ftp_close() if $conf::upload_method eq "ftp";
549 # find files which aren't related to any .changes
550 foreach $file (@files) {
552 # filter out files we never want to delete
553 next if !-f $file || # may have disappeared in the meantime
556 || ( grep { $_ eq $file } @keep_files )
557 || $file =~ /$conf::keep_files/;
559 # Delete such files if they're older than
560 # $stray_remove_timeout; they could be part of an
561 # yet-incomplete upload, with the .changes still missing.
562 # Cannot send any notification, since owner unknown.
563 next if !( @stats = stat($file) );
564 my $age = time - $stats[ST_MTIME];
565 my ( $maint, $pattern, @job_files );
566 if ( $file =~ /^junk-for-writable-test/
567 || $file !~ m,$conf::valid_files,
568 || $age >= $conf::stray_remove_timeout )
571 "Deleted stray file ${main::current_incoming_short}/$file\n" )
574 $age > $conf::no_changes_timeout
575 && is_debian_file($file)
578 # not already reported
579 !( $stats[ST_MODE] & S_ISGID )
580 && ( $pattern = debian_file_stem($file) )
581 && ( @job_files = glob($pattern) )
584 # If a .changes is in the list, it has the same stem as the
585 # found file (probably a .orig.tar.gz). Don't report in this
587 !( grep( /\.changes$/, @job_files ) )
590 $maint = get_maintainer($file);
592 # Don't send a mail if this looks like the recompilation of a
593 # package for a non-i386 arch. For those, the maintainer field is
595 if ( !grep( /(\.dsc|_(i386|all)\.deb)$/, @job_files ) ) {
596 msg( "log", "Found an upload without .changes and with no ",
599 "Not sending a report, because probably ",
600 "recompilation job\n" );
603 $main::mail_addr = $maint;
604 $main::mail_addr = $1 if $main::mail_addr =~ /<([^>]*)>/;
605 $main::mail_subject =
606 "Incomplete upload found in " . "Debian upload queue";
609 "Probably you are the uploader of the following "
612 msg( "mail", "the Debian upload queue directory:\n " );
613 msg( "mail", join( "\n ", @job_files ), "\n" );
616 "This looks like an upload, but a .changes file "
617 . "is missing, so the job\n"
619 msg( "mail", "cannot be processed.\n\n" );
622 "If no .changes file arrives within ",
623 print_time( $conf::stray_remove_timeout - $age ),
624 ", the files will be deleted.\n\n"
628 "If you didn't upload those files, please just "
629 . "ignore this message.\n"
634 "Sending problem report for an upload without a "
637 msg( "log", "Maintainer: $maint\n" );
641 "Found an upload without .changes, but can't "
642 . "find a maintainer address\n"
644 } ## end else [ if ( !grep( /(\.dsc|_(i386|all)\.deb)$/...
645 msg( "log", "Files: @job_files\n" );
647 # remember we already have sent a mail regarding this file
648 foreach (@job_files) {
650 next if !@st; # file may have disappeared in the meantime
651 chmod +( $st[ST_MODE] |= S_ISGID ), $_;
655 "found stray file ${main::current_incoming_short}/$file, deleting in ",
656 print_time( $conf::stray_remove_timeout - $age )
658 } ## end else [ if ( $file =~ /^junk-for-writable-test/...
659 } ## end foreach $file (@files)
660 } ## end for ( $adelay = -1 ; $adelay...
661 chdir($conf::incoming);
665 write_status_file() if $conf::statusdelay;
666 } ## end sub check_dir()
668 sub get_filelist_from_known_good_changes($) {
674 # parse the .changes file
675 open( CHANGES, "<$changes" )
676 or die "$changes: $!\n";
677 outer_loop: while (<CHANGES>) {
680 redo outer_loop if !/^\s/;
681 my @field = split(/\s+/);
684 # forbid shell meta chars in the name, we pass it to a
685 # subshell several times...
686 $field[5] =~ /$re_file_safe/;
687 if ( $1 ne $field[5] ) {
688 msg( "log", "found suspicious filename $field[5]\n" );
691 push( @filenames, $field[5] );
692 } ## end while (<CHANGES>)
693 } ## end if (/^Files:/i)
694 } ## end while (<CHANGES>)
697 } ## end sub get_filelist_from_known_good_changes($)
700 # process one .changes file
702 sub process_changes($\@) {
704 my $keep_list = shift;
706 $pgplines, @files, @filenames, @changes_stats,
707 $failure_file, $retries, $last_retry, $upload_time,
708 $file, $do_report, $ls_l, $problems_reported,
709 $errs, $pkgname, $signator, $extralines
714 format_status_str( $main::current_changes,
715 "$main::current_incoming_short/$changes" );
717 write_status_file() if $conf::statusdelay;
720 msg( "log", "processing ${main::current_incoming_short}/$changes\n" );
722 # parse the .changes file
723 open( CHANGES, "<$changes" )
724 or die "Cannot open ${main::current_incoming_short}/$changes: $!\n";
727 $main::mail_addr = "";
729 outer_loop: while (<CHANGES>) {
730 if (/^---+(BEGIN|END) PGP .*---+$/) {
734 if ( $pgplines < 1 or $pgplines >= 3 ) {
735 $extralines++ if length $_ > 1;
738 if (/^Maintainer:\s*/i) {
739 chomp( $main::mail_addr = $' );
740 $main::mail_addr = $1 if $main::mail_addr =~ /<([^>]*)>/;
741 } elsif (/^Source:\s*/i) {
742 chomp( $pkgname = $' );
743 $pkgname =~ s/\s+$//;
744 $main::packages{$pkgname}++;
745 } elsif (/^Files:/i) {
747 redo outer_loop if !/^\s/;
748 my @field = split(/\s+/);
751 # forbid shell meta chars in the name, we pass it to a
752 # subshell several times...
753 $field[5] =~ /$re_file_safe/;
754 if ( $1 ne $field[5] ) {
755 msg( "log", "found suspicious filename $field[5]\n" );
758 "File '$field[5]' mentioned in $main::current_incoming_short/$changes\n",
759 "has bad characters in its name. Removed.\n"
763 } ## end if ( $1 ne $field[5] )
772 push( @filenames, $field[5] );
773 debug( "includes file $field[5], size $field[2], ", "md5 $field[1]" );
774 } ## end while (<CHANGES>)
775 } ## end elsif (/^Files:/i)
776 } ## end while (<CHANGES>)
779 # tell check_dir that the files mentioned in this .changes aren't stray,
780 # we know about them somehow
781 @$keep_list = @filenames;
783 # some consistency checks
786 "$main::current_incoming_short/$changes contained lines outside the pgp signed "
787 ."part, cannot process\n" );
788 goto remove_only_changes;
789 } ## end if ( $extralines )
790 if ( !$main::mail_addr ) {
792 "$main::current_incoming_short/$changes doesn't contain a Maintainer: field; "
793 . "cannot process\n" );
794 goto remove_only_changes;
795 } ## end if ( !$main::mail_addr)
796 if ( $main::mail_addr !~ /^(buildd_\S+-\S+|\S+\@\S+\.\S+)/ ) {
798 # doesn't look like a mail address, maybe only the name
799 my ( $new_addr, @addr_list );
800 if ( $new_addr = try_to_get_mail_addr( $main::mail_addr, \@addr_list ) ) {
802 # substitute (unique) found addr, but give a warning
805 "(The Maintainer: field didn't contain a proper "
810 "Looking for `$main::mail_addr' in the Debian "
811 . "keyring gave your address\n"
813 msg( "mail", "as unique result, so I used this.)\n" );
815 "Substituted $new_addr for malformed " . "$main::mail_addr\n" );
816 $main::mail_addr = $new_addr;
819 # not found or not unique: hold the job and inform queue maintainer
820 my $old_addr = $main::mail_addr;
821 $main::mail_addr = $conf::maintainer_mail;
824 "The job ${main::current_incoming_short}/$changes doesn't have a correct email\n"
826 msg( "mail", "address in the Maintainer: field:\n" );
827 msg( "mail", " $old_addr\n" );
828 msg( "mail", "A check for this in the Debian keyring gave:\n" );
831 ? " " . join( ", ", @addr_list ) . "\n"
833 msg( "mail", "Please fix this manually\n" );
836 "Bad Maintainer: field in ${main::current_incoming_short}/$changes: $old_addr\n"
838 goto remove_only_changes;
839 } ## end else [ if ( $new_addr = try_to_get_mail_addr...
840 } ## end if ( $main::mail_addr ...
841 if ( $pgplines < 3 ) {
844 "$main::current_incoming_short/$changes isn't signed with PGP/GnuPG\n"
846 msg( "log", "(uploader $main::mail_addr)\n" );
847 goto remove_only_changes;
848 } ## end if ( $pgplines < 3 )
851 "$main::current_incoming_short/$changes doesn't mention any files\n" );
852 msg( "log", "(uploader $main::mail_addr)\n" );
853 goto remove_only_changes;
854 } ## end if ( !@files )
856 # check for packages that shouldn't be processed
857 if ( grep( $_ eq $pkgname, @conf::nonus_packages ) ) {
860 "$pkgname is a package that must be uploaded "
861 . "to nonus.debian.org\n"
863 msg( "log,mail", "instead of target.\n" );
865 "Job rejected and removed all files belonging " . "to it:\n" );
866 msg( "log,mail", " ", join( ", ", @filenames ), "\n" );
867 rm( $changes, @filenames );
869 } ## end if ( grep( $_ eq $pkgname...
871 $failure_file = $changes . ".failures";
872 $retries = $last_retry = 0;
873 if ( -f $failure_file ) {
874 open( FAILS, "<$failure_file" )
875 or die "Cannot open $main::current_incoming_short/$failure_file: $!\n";
878 ( $retries, $last_retry ) = ( $1, $2 )
879 if $line =~ /^(\d+)\s+(\d+)$/;
880 push( @$keep_list, $failure_file );
881 } ## end if ( -f $failure_file )
883 # run PGP on the file to check the signature
884 if ( !( $signator = pgp_check($changes) ) ) {
887 "$main::current_incoming_short/$changes has bad PGP/GnuPG signature!\n"
889 msg( "log", "(uploader $main::mail_addr)\n" );
893 "Removing $main::current_incoming_short/$changes, but keeping its associated ",
898 # Set SGID bit on associated files, so that the test for Debian files
899 # without a .changes doesn't consider them.
900 foreach (@filenames) {
902 next if !@st; # file may have disappeared in the meantime
903 chmod +( $st[ST_MODE] |= S_ISGID ), $_;
906 } elsif ( $signator eq "LOCAL ERROR" ) {
908 # An error has appened when starting pgp... Don't process the file,
909 # but also don't delete it
911 "Can't PGP/GnuPG check $main::current_incoming_short/$changes -- don't process it for now"
914 } ## end elsif ( $signator eq "LOCAL ERROR")
916 die "Cannot stat ${main::current_incoming_short}/$changes (??): $!\n"
917 if !( @changes_stats = stat($changes) );
919 # Make $upload_time the maximum of all modification times of files
920 # related to this .changes (and the .changes it self). This is the
921 # last time something changes to these files.
922 $upload_time = $changes_stats[ST_MTIME];
925 next if !( @stats = stat( $file->{"name"} ) );
926 $file->{"stats"} = \@stats;
927 $upload_time = $stats[ST_MTIME] if $stats[ST_MTIME] > $upload_time;
928 } ## end for $file (@files)
930 $do_report = ( time - $upload_time ) > $conf::problem_report_timeout;
931 $problems_reported = $changes_stats[ST_MODE] & S_ISGID;
933 # if any of the files is newer than the .changes' ctime (the time
934 # we sent a report and set the sticky bit), send new problem reports
935 if ( $problems_reported && $changes_stats[ST_CTIME] < $upload_time ) {
936 $problems_reported = 0;
937 chmod +( $changes_stats[ST_MODE] &= ~S_ISGID ), $changes;
938 debug("upload_time>changes-ctime => resetting problems reported");
940 debug("do_report=$do_report problems_reported=$problems_reported");
942 # now check all files for correct size and md5 sum
944 my $filename = $file->{"name"};
945 if ( !defined( $file->{"stats"} ) ) {
947 # could be an upload that isn't complete yet, be quiet,
948 # but don't process the file;
949 msg( "log,mail", "$filename doesn't exist\n" )
950 if $do_report && !$problems_reported;
951 msg( "log", "$filename doesn't exist (ignored for now)\n" )
953 msg( "log", "$filename doesn't exist (already reported)\n" )
954 if $problems_reported;
956 } elsif ( $file->{"stats"}->[ST_SIZE] < $file->{"size"}
960 # could be an upload that isn't complete yet, be quiet,
961 # but don't process the file
962 msg( "log", "$filename is too small (ignored for now)\n" );
964 } elsif ( $file->{"stats"}->[ST_SIZE] != $file->{"size"} ) {
965 msg( "log,mail", "$filename has incorrect size; deleting it\n" );
968 } elsif ( md5sum($filename) ne $file->{"md5"} ) {
970 "$filename has incorrect md5 checksum; ",
974 } ## end elsif ( md5sum($filename)...
975 } ## end for $file (@files)
978 if ( ( time - $upload_time ) > $conf::bad_changes_timeout ) {
980 # if a .changes fails for a really long time (several days
981 # or so), remove it and all associated files
984 "$main::current_incoming_short/$changes couldn't be processed for ",
985 int( $conf::bad_changes_timeout / ( 60 * 60 ) ),
986 " hours and is now deleted\n"
988 msg( "log,mail", "All files it mentions are also removed:\n" );
989 msg( "log,mail", " ", join( ", ", @filenames ), "\n" );
990 rm( $changes, @filenames, $failure_file );
991 } elsif ( $do_report && !$problems_reported ) {
993 # otherwise, send a problem report, if not done already
996 "Due to the errors above, the .changes file couldn't ",
998 "Please fix the problems for the upload to happen.\n"
1001 # remember we already have sent a mail regarding this file
1002 debug("Sending problem report mail and setting SGID bit");
1003 my $mode = $changes_stats[ST_MODE] |= S_ISGID;
1004 msg( "log", "chmod failed: $!" )
1005 if ( chmod( $mode, $changes ) != 1 );
1006 } ## end elsif ( $do_report && !$problems_reported)
1013 # if this upload already failed earlier, wait until the delay requirement
1016 && ( time - $last_retry ) <
1017 ( $retries == 1 ? $conf::upload_delay_1 : $conf::upload_delay_2 ) )
1019 msg( "log", "delaying retry of upload\n" );
1021 } ## end if ( $retries > 0 && (...
1023 if ( $conf::upload_method eq "ftp" ) {
1024 return if !ftp_open();
1027 # check if the job is already present on target
1028 # (moved to here, to avoid bothering target as long as there are errors in
1030 if ( $ls_l = is_on_target( $changes, @filenames ) ) {
1033 "$main::current_incoming_short/$changes is already present on target host:\n"
1035 msg( "log,mail", "$ls_l\n" );
1037 "Either you already uploaded it, or someone else ",
1039 msg( "log,mail", "Job $changes removed.\n" );
1040 rm( $changes, @filenames, $failure_file );
1042 } ## end if ( $ls_l = is_on_target...
1044 # clear sgid bit before upload, scp would copy it to target. We don't need
1045 # it anymore, we know there are no problems if we come here. Also change
1046 # mode of files to 644 if this should be done locally.
1047 $changes_stats[ST_MODE] &= ~S_ISGID;
1048 if ( !$conf::chmod_on_target ) {
1049 $changes_stats[ST_MODE] &= ~0777;
1050 $changes_stats[ST_MODE] |= 0644;
1052 chmod +( $changes_stats[ST_MODE] ), $changes;
1054 # try uploading to target
1055 if ( !copy_to_target( $changes, @filenames ) ) {
1057 # if the upload failed, increment the retry counter and remember the
1058 # current time; both things are written to the .failures file. Don't
1059 # increment the fail counter if the error was due to incoming
1061 return if !$main::incoming_writable;
1062 if ( ++$retries >= $conf::max_upload_retries ) {
1064 "$changes couldn't be uploaded for $retries times now.\n" );
1066 "Giving up and removing it and its associated files:\n" );
1067 msg( "log,mail", " ", join( ", ", @filenames ), "\n" );
1068 rm( $changes, @filenames, $failure_file );
1071 if ( open( FAILS, ">$failure_file" ) ) {
1072 print FAILS "$retries $last_retry\n";
1074 chmod( 0600, $failure_file )
1075 or die "Cannot set modes of $failure_file: $!\n";
1076 } ## end if ( open( FAILS, ">$failure_file"...
1077 push( @$keep_list, $failure_file );
1078 debug("now $retries failed uploads");
1081 "The upload will be retried in ",
1084 ? $conf::upload_delay_1
1085 : $conf::upload_delay_2
1089 } ## end else [ if ( ++$retries >= $conf::max_upload_retries)
1091 } ## end if ( !copy_to_target( ...
1093 # If the files were uploaded ok, remove them
1094 rm( $changes, @filenames, $failure_file );
1096 msg( "mail", "$changes uploaded successfully to $conf::target\n" );
1097 msg( "mail", "along with the files:\n ", join( "\n ", @filenames ),
1100 "$changes processed successfully (uploader $main::mail_addr)\n" );
1102 # Check for files that have the same stem as the .changes (and weren't
1103 # mentioned there) and delete them. It happens often enough that people
1104 # upload a .orig.tar.gz where it isn't needed and also not in the
1105 # .changes. Explicitly deleting it (and not waiting for the
1106 # $stray_remove_timeout) reduces clutter in the queue dir and maybe also
1107 # educates uploaders :-)
1109 # my $pattern = debian_file_stem( $changes );
1110 # my $spattern = substr( $pattern, 0, -1 ); # strip off '*' at end
1111 # my @other_files = glob($pattern);
1112 # filter out files that have a Debian revision at all and a different
1113 # revision. Those belong to a different upload.
1114 # if ($changes =~ /^\Q$spattern\E-([\d.+-]+)/) {
1115 # my $this_rev = $1;
1116 # @other_files = grep( !/^\Q$spattern\E-([\d.+-]+)/ || $1 eq $this_rev,
1119 # Also do not remove those files if a .changes is among them. Then there
1120 # is probably a second upload for another version or another architecture.
1121 # if (@other_files && !grep( /\.changes$/, @other_files )) {
1122 # rm( @other_files );
1123 # msg( "mail", "\nThe following file(s) seemed to belong to the same ".
1124 # "upload, but weren't listed\n" );
1125 # msg( "mail", "in the .changes file:\n " );
1126 # msg( "mail", join( "\n ", @other_files ), "\n" );
1127 # msg( "mail", "They have been deleted.\n" );
1128 # msg( "log", "Deleted files in upload not in $changes: @other_files\n" );
1130 } ## end sub process_changes($\@)
1133 # process one .dak-commands file
1135 sub process_dak_commands {
1136 my $commands = shift;
1138 msg("log", "processing ${main::current_incoming_short}/$commands\n");
1140 # TODO: get mail address from signed contents
1141 # and NOT implement a third parser for armored PGP...
1142 $main::mail_addr = undef;
1145 my $signator = pgp_check($commands);
1148 "$main::current_incoming_short/$commands has bad PGP/GnuPG signature!\n");
1150 "Removing $main::current_incoming_short/$commands\n");
1154 elsif ($signator eq 'LOCAL ERROR') {
1155 debug("Can't check signature for $main::current_incoming_short/$commands -- don't process it for now");
1158 msg("log,mail", "(PGP/GnuPG signature by $signator)\n");
1161 my @filenames = ($commands);
1162 if (my $ls_l = is_on_target($commands, @filenames)) {
1163 msg("log,mail", "$main::current_incoming_short/$commands is already present on target host:\n");
1164 msg("log,mail", "$ls_l\n");
1165 msg("log,mail", "Job $commands removed.\n");
1170 if (!copy_to_target($commands)) {
1171 msg("log,mail", "$commands couldn't be uploaded to target.\n");
1172 msg("log,mail", "Giving up and removing it.\n");
1178 msg("mail", "$commands uploaded successfully to $conf::target\n");
1182 # process one .commands file
1184 sub process_commands($) {
1185 my $commands = shift;
1186 my ( @cmds, $cmd, $pgplines, $signator );
1188 my ( @files, $file, @removed, $target_delay );
1190 format_status_str( $main::current_changes, $commands );
1192 write_status_file() if $conf::statusdelay;
1194 msg( "log", "processing $main::current_incoming_short/$commands\n" );
1196 # parse the .commands file
1197 if ( !open( COMMANDS, "<$commands" ) ) {
1198 msg( "log", "Cannot open $main::current_incoming_short/$commands: $!\n" );
1202 $main::mail_addr = "";
1204 outer_loop: while (<COMMANDS>) {
1205 if (/^---+(BEGIN|END) PGP .*---+$/) {
1207 } elsif (/^Uploader:\s*/i) {
1208 chomp( $main::mail_addr = $' );
1209 $main::mail_addr = $1 if $main::mail_addr =~ /<([^>]*)>/;
1210 } elsif (/^Commands:/i) {
1213 s/^\s*(.*)\s*$/$1/; # delete whitespace at both ends
1216 debug("includes cmd $_");
1218 last outer_loop if !defined( $_ = scalar(<COMMANDS>) );
1220 redo outer_loop if !/^\s/ || /^$/;
1221 } ## end for ( ; ; )
1222 } ## end elsif (/^Commands:/i)
1223 } ## end while (<COMMANDS>)
1226 # some consistency checks
1227 if ( !$main::mail_addr || $main::mail_addr !~ /^\S+\@\S+\.\S+/ ) {
1229 "$main::current_incoming_short/$commands contains no or bad Uploader: field: "
1230 . "$main::mail_addr\n" );
1232 "cannot process $main::current_incoming_short/$commands\n" );
1233 $main::mail_addr = "";
1235 } ## end if ( !$main::mail_addr...
1236 msg( "log", "(command uploader $main::mail_addr)\n" );
1238 if ( $pgplines < 3 ) {
1241 "$main::current_incoming_short/$commands isn't signed with PGP/GnuPG\n"
1245 "or the uploaded file is broken. Make sure to transfer in binary mode\n"
1247 msg( "mail", "or better yet - use dcut for commands files\n" );
1249 } ## end if ( $pgplines < 3 )
1251 # run PGP on the file to check the signature
1252 if ( !( $signator = pgp_check($commands) ) ) {
1255 "$main::current_incoming_short/$commands has bad PGP/GnuPG signature!\n"
1258 msg( "log,mail", "Removing $main::current_incoming_short/$commands\n" );
1261 } elsif ( $signator eq "LOCAL ERROR" ) {
1263 # An error has appened when starting pgp... Don't process the file,
1264 # but also don't delete it
1266 "Can't PGP/GnuPG check $main::current_incoming_short/$commands -- don't process it for now"
1269 } ## end elsif ( $signator eq "LOCAL ERROR")
1270 msg( "log", "(PGP/GnuPG signature by $signator)\n" );
1272 # now process commands
1275 "Log of processing your commands file $main::current_incoming_short/$commands:\n\n"
1277 foreach $cmd (@cmds) {
1278 my @word = split( /\s+/, $cmd );
1279 msg( "mail,log", "> @word\n" );
1280 my $selecteddelayed = -1;
1283 if ( $word[0] eq "rm" ) {
1284 foreach ( @word[ 1 .. $#word ] ) {
1286 if (m,^DELAYED/([0-9]+)-day/,) {
1287 $selecteddelayed = $1;
1288 s,^DELAYED/[0-9]+-day/,,;
1291 msg("mail,log", "$_: filename component cannot start with a wildcard\n");
1292 } elsif ( $origword eq "--searchdirs" ) {
1293 $selecteddelayed = -2;
1297 "$_: filename may not contain slashes except for DELAYED/#-day/ prefixes\n"
1301 # process wildcards but also plain names
1303 my $pat = quotemeta($_);
1304 $pat =~ s/\\\*/.*/g;
1305 $pat =~ s/\\\?/.?/g;
1306 $pat =~ s/\\([][])/$1/g;
1308 if ( $selecteddelayed < 0 ) { # scanning or explicitly incoming
1309 opendir( DIR, "." );
1310 push( @thesefiles, grep /^$pat$/, readdir(DIR) );
1313 if ( $selecteddelayed >= 0 ) {
1314 my $dir = sprintf( $conf::incoming_delayed, $selecteddelayed );
1315 opendir( DIR, $dir );
1317 map ( "$dir/$_", grep /^$pat$/, readdir(DIR) ) );
1319 } elsif ( $selecteddelayed == -2 ) {
1320 for ( my ($adelay) = 0 ;
1321 ( !@thesefiles ) && $adelay <= $conf::max_delayed ;
1324 my $dir = sprintf( $conf::incoming_delayed, $adelay );
1325 opendir( DIR, $dir );
1327 map ( "$dir/$_", grep /^$pat$/, readdir(DIR) ) );
1329 } ## end for ( my ($adelay) = 0 ...
1330 } ## end elsif ( $selecteddelayed ...
1331 push( @files, @thesefiles );
1332 if ( !@thesefiles ) {
1333 msg( "mail,log", "$origword did not match anything\n" );
1335 } ## end else [ if ( $origword eq "--searchdirs")
1336 } ## end foreach ( @word[ 1 .. $#word...
1338 msg( "mail,log", "No files to delete\n" );
1341 foreach $file (@files) {
1343 msg( "mail,log", "$file: no such file\n" );
1344 } elsif ( $file =~ /$conf::keep_files/ ) {
1345 msg( "mail,log", "$file is protected, cannot " . "remove\n" );
1346 } elsif ( !unlink($file) ) {
1347 msg( "mail,log", "$file: rm: $!\n" );
1349 $file =~ s,$conf::incoming/?,,;
1350 push( @removed, $file );
1352 } ## end foreach $file (@files)
1353 msg( "mail,log", "Files removed: @removed\n" ) if @removed;
1354 } ## end else [ if ( !@files )
1355 } elsif ( $word[0] eq "reschedule" ) {
1357 msg( "mail,log", "Wrong number of arguments\n" );
1358 } elsif ( $conf::upload_method ne "copy" ) {
1359 msg( "mail,log", "reschedule not available\n" );
1360 } elsif ( $word[1] =~ m,/, || $word[1] !~ m/\.changes/ ) {
1363 "$word[1]: filename may not contain slashes and must be .changes\n"
1365 } elsif ( !( ($target_delay) = $word[2] =~ m,^([0-9]+)-day$, )
1366 || $target_delay > $conf::max_delayed )
1370 "$word[2]: rescheduling target must be #-day with # between 0 and $conf::max_delayed (in particular, no '/' allowed)\n"
1372 } elsif ( $word[1] =~ /$conf::keep_files/ ) {
1373 msg( "mail,log", "$word[1] is protected, cannot do stuff with it\n" );
1377 $adelay <= $conf::max_delayed
1379 sprintf( "$conf::targetdir_delayed", $adelay ) . "/$word[1]" ) ;
1382 } ## end for ( $adelay = 0 ; $adelay...
1383 if ( $adelay > $conf::max_delayed ) {
1384 msg( "mail,log", "$word[1] not found\n" );
1385 } elsif ( $adelay == $target_delay ) {
1386 msg( "mail,log", "$word[1] already is in $word[2]\n" );
1389 my ($dir) = sprintf( "$conf::targetdir_delayed", $adelay );
1391 sprintf( "$conf::targetdir_delayed", $target_delay );
1392 push( @thesefiles, $word[1] );
1394 get_filelist_from_known_good_changes("$dir/$word[1]") );
1395 for my $afile (@thesefiles) {
1396 if ( $afile =~ m/\.changes$/ ) {
1397 utime undef, undef, ("$dir/$afile");
1399 if ( !move("$dir/$afile", "$target_dir/$afile") ) {
1400 msg( "mail,log", "move: $!\n" );
1402 msg( "mail,log", "$afile moved to $target_delay-day\n" );
1404 } ## end for my $afile (@thesefiles)
1405 } ## end else [ if ( $adelay > $conf::max_delayed)
1406 } ## end else [ if ( @word != 3 )
1407 } elsif ( $word[0] eq "cancel" ) {
1409 msg( "mail,log", "Wrong number of arguments\n" );
1410 } elsif ( $conf::upload_method ne "copy" ) {
1411 msg( "mail,log", "cancel not available\n" );
1413 $word[1] !~ m,$re_file_safe_prefix.changes\z, )
1416 "argument to cancel must be one .changes filename without path\n" );
1417 } ## end elsif ( $word[1] !~ ...
1419 for ( my ($adelay) = 0 ; $adelay <= $conf::max_delayed ; $adelay++ ) {
1420 my ($dir) = sprintf( "$conf::targetdir_delayed", $adelay );
1421 if ( -f "$dir/$word[1]" ) {
1423 push( @files, "$word[1]" );
1425 get_filelist_from_known_good_changes("$dir/$word[1]") );
1426 foreach $file (@files) {
1427 if ( !-f "$dir/$file" ) {
1428 msg( "mail,log", "$dir/$file: no such file\n" );
1429 } elsif ( "$dir/$file" =~ /$conf::keep_files/ ) {
1431 "$dir/$file is protected, cannot " . "remove\n" );
1432 } elsif ( !unlink("$dir/$file") ) {
1433 msg( "mail,log", "$dir/$file: rm: $!\n" );
1435 push( @removed, $file );
1437 } ## end foreach $file (@files)
1438 msg( "mail,log", "Files removed from $adelay-day: @removed\n" )
1440 } ## end if ( -f "$dir/$word[1]")
1441 } ## end for ( my ($adelay) = 0 ...
1443 msg( "mail,log", "No upload found: $word[1]\n" );
1446 msg( "mail,log", "unknown command $word[0]\n" );
1448 } ## end foreach $cmd (@cmds)
1451 "-- End of $main::current_incoming_short/$commands processing\n" );
1452 } ## end sub process_commands($)
1454 sub age_delayed_queues() {
1455 for ( my ($adelay) = 0 ; $adelay <= $conf::max_delayed ; $adelay++ ) {
1456 my ($dir) = sprintf( "$conf::targetdir_delayed", $adelay );
1458 if ( $adelay == 0 ) {
1459 $target_dir = $conf::targetdir;
1461 $target_dir = sprintf( "$conf::targetdir_delayed", $adelay - 1 );
1463 for my $achanges (<$dir/*.changes>) {
1464 my $mtime = ( stat($achanges) )[9];
1465 if ( $mtime + 24 * 60 * 60 <= time || $adelay == 0 ) {
1466 utime undef, undef, ($achanges);
1467 my @thesefiles = ( $achanges =~ m,.*/([^/]*), );
1468 push( @thesefiles, get_filelist_from_known_good_changes($achanges) );
1469 for my $afile (@thesefiles) {
1470 if ( !move("$dir/$afile", "$target_dir/$afile") ) {
1471 msg( "log", "move: $!\n" );
1473 msg( "log", "$afile moved to $target_dir\n" );
1475 } ## end for my $afile (@thesefiles)
1476 } ## end if ( $mtime + 24 * 60 ...
1477 } ## end for my $achanges (<$dir/*.changes>)
1478 } ## end for ( my ($adelay) = 0 ...
1479 } ## end sub age_delayed_queues()
1482 # check if a file is already on target
1484 sub is_on_target($\@) {
1486 my $filelist = shift;
1490 if ( $conf::upload_method eq "ssh" ) {
1491 ( $msg, $stat ) = ssh_cmd("ls -l $file");
1492 } elsif ( $conf::upload_method eq "ftp" ) {
1494 ( $msg, $err ) = ftp_cmd( "dir", $file );
1500 $msg = "ls: no such file\n";
1503 $msg = join( "\n", @$msg );
1506 my @allfiles = ($file);
1507 push( @allfiles, @$filelist );
1509 $msg = "no such file";
1510 for my $afile (@allfiles) {
1511 if ( -f "$conf::targetdir/$afile" ) {
1515 } ## end for my $afile (@allfiles)
1516 for ( my ($adelay) = 0 ;
1517 $adelay <= $conf::max_delayed && $stat ;
1520 for my $afile (@allfiles) {
1522 -f ( sprintf( "$conf::targetdir_delayed", $adelay ) . "/$afile" ) )
1525 $msg = sprintf( "%d-day", $adelay ) . "/$afile";
1526 } ## end if ( -f ( sprintf( "$conf::targetdir_delayed"...
1527 } ## end for my $afile (@allfiles)
1528 } ## end for ( my ($adelay) = 0 ...
1529 } ## end else [ if ( $conf::upload_method...
1531 debug("exit status: $stat, output was: $msg");
1533 return "" if $stat && $msg =~ /no such file/i; # file not present
1534 msg( "log", "strange ls -l output on target:\n", $msg ), return ""
1535 if $stat || $@; # some other error, but still try to upload
1537 # ls -l returned 0 -> file already there
1538 $msg =~ s/\s\s+/ /g; # make multiple spaces into one, to save space
1540 } ## end sub is_on_target($\@)
1543 # copy a list of files to target
1545 sub copy_to_target(@) {
1547 my ( @md5sum, @expected_files, $sum, $name, $msgs, $stat );
1550 write_status_file() if $conf::statusdelay;
1553 if ( $conf::upload_method eq "ssh" ) {
1554 ( $msgs, $stat ) = scp_cmd(@files);
1556 } elsif ( $conf::upload_method eq "ftp" ) {
1558 if ( !$main::FTP_chan->cwd($main::current_targetdir) ) {
1560 "Can't cd to $main::current_targetdir on $conf::target\n" );
1563 foreach $file (@files) {
1564 ( $rv, $msgs ) = ftp_cmd( "put", $file );
1569 local_cmd( "$conf::cp @files $main::current_targetdir", 'NOCD' );
1573 # check md5sums or sizes on target against our own
1574 my $have_md5sums = 1;
1575 if ($conf::check_md5sum) {
1576 if ( $conf::upload_method eq "ssh" ) {
1577 ( $msgs, $stat ) = ssh_cmd("md5sum @files");
1579 @md5sum = split( "\n", $msgs );
1580 } elsif ( $conf::upload_method eq "ftp" ) {
1581 my ( $rv, $err, $file );
1582 foreach $file (@files) {
1583 ( $rv, $err ) = ftp_cmd( "quot", "site", "md5sum", $file );
1585 next if ftp_code() == 550; # file not found
1586 if ( ftp_code() == 500 ) { # unimplemented
1588 goto get_sizes_instead;
1593 chomp( my $t = ftp_response() );
1594 push( @md5sum, $t );
1595 } ## end foreach $file (@files)
1596 if ( !$have_md5sums ) {
1598 foreach $file (@files) {
1599 ( $rv, $err ) = ftp_cmd( "size", $file );
1601 next if ftp_code() == 550; # file not found
1605 push( @md5sum, "$rv $file" );
1606 } ## end foreach $file (@files)
1607 } ## end if ( !$have_md5sums )
1609 ( $msgs, $stat ) = local_cmd("$conf::md5sum @files");
1611 @md5sum = split( "\n", $msgs );
1614 @expected_files = @files;
1617 ( $sum, $name ) = split;
1618 next if !grep { $_ eq $name } @files; # a file we didn't upload??
1619 next if $sum eq "md5sum:"; # looks like an error message
1620 if ( ( $have_md5sums && $sum ne md5sum($name) )
1621 || ( !$have_md5sums && $sum != ( -s $name ) ) )
1625 "Upload of $name to $conf::target failed ",
1626 "(" . ( $have_md5sums ? "md5sum" : "size" ) . " mismatch)\n"
1629 } ## end if ( ( $have_md5sums &&...
1631 # seen that file, remove it from expect list
1632 @expected_files = map { $_ eq $name ? () : $_ } @expected_files;
1633 } ## end foreach (@md5sum)
1634 if (@expected_files) {
1635 msg( "log,mail", "Failed to upload the files\n" );
1636 msg( "log,mail", " ", join( ", ", @expected_files ), "\n" );
1637 msg( "log,mail", "(Not present on target after upload)\n" );
1639 } ## end if (@expected_files)
1640 } ## end if ($conf::check_md5sum)
1642 if ($conf::chmod_on_target) {
1644 # change file's mode explicitly to 644 on target
1645 if ( $conf::upload_method eq "ssh" ) {
1646 ( $msgs, $stat ) = ssh_cmd("chmod 644 @files");
1648 } elsif ( $conf::upload_method eq "ftp" ) {
1650 foreach $file (@files) {
1651 ( $rv, $msgs ) = ftp_cmd( "quot", "site", "chmod", "644", $file );
1652 msg( "log", "Can't chmod $file on target:\n$msgs" )
1655 } ## end foreach $file (@files)
1657 ( $msgs, $stat ) = local_cmd("$conf::chmod 644 @files");
1660 } ## end if ($conf::chmod_on_target)
1663 write_status_file() if $conf::statusdelay;
1668 "Upload to $conf::target failed",
1669 $? ? ", last exit status " . sprintf( "%s", $? >> 8 ) : "", "\n" );
1670 msg( "log,mail", "Error messages:\n", $msgs )
1673 # If "permission denied" was among the errors, test if the incoming is
1675 if ( $msgs =~ /(permission denied|read-?only file)/i ) {
1676 if ( !check_incoming_writable() ) {
1677 msg( "log,mail", "(The incoming directory seems to be ",
1680 } ## end if ( $msgs =~ /(permission denied|read-?only file)/i)
1682 # remove bad files or an incomplete upload on target
1683 if ( $conf::upload_method eq "ssh" ) {
1684 ssh_cmd("rm -f @files");
1685 } elsif ( $conf::upload_method eq "ftp" ) {
1687 foreach $file (@files) {
1689 ( $rv, $err ) = ftp_cmd( "delete", $file );
1690 msg( "log", "Can't delete $file on target:\n$err" )
1692 } ## end foreach $file (@files)
1694 my @tfiles = map { "$main::current_targetdir/$_" } @files;
1695 debug("executing unlink(@tfiles)");
1699 write_status_file() if $conf::statusdelay;
1701 } ## end sub copy_to_target(@)
1704 # check if a file is correctly signed with PGP
1714 if ($file =~ /$re_file_safe/) {
1717 msg( "log", "Tainted filename, skipping: $file\n" );
1718 return "LOCAL ERROR";
1722 if ( -x $conf::gpg ) {
1723 debug( "executing $conf::gpg --no-options --batch "
1724 . "--no-default-keyring --always-trust "
1726 . join( " --keyring ", @conf::keyrings )
1727 . " --verify '$file'" );
1730 "$conf::gpg --no-options --batch "
1731 . "--no-default-keyring --always-trust "
1733 . join( " --keyring ", @conf::keyrings )
1734 . " --verify '$file'"
1739 msg( "log", "Can't open pipe to $conf::gpg: $!\n" );
1740 return "LOCAL ERROR";
1741 } ## end if ( !open( PIPE, "$conf::gpg --no-options --batch "...
1742 $output .= $_ while (<PIPE>);
1745 } ## end if ( -x $conf::gpg )
1748 msg( "log,mail", "GnuPG signature check failed on $file\n" );
1749 msg( "mail", $output );
1750 msg( "log,mail", "(Exit status ", $stat >> 8, ")\n" );
1754 $output =~ /^(gpg: )?good signature from (user )?"(.*)"\.?$/im;
1755 ( $signator = $3 ) ||= "unknown signator";
1757 debug("GnuPG signature ok (by $signator)");
1760 } ## end sub pgp_check($)
1762 # ---------------------------------------------------------------------------
1764 # ---------------------------------------------------------------------------
1767 # fork a subprocess that watches the 'status' FIFO
1769 # that process blocks until someone opens the FIFO, then sends a
1770 # signal (SIGUSR1) to the main process, expects
1772 sub fork_statusd() {
1778 $statusd_pid = open( STATUSD, "|-" );
1779 die "cannot fork: $!\n" if !defined($statusd_pid);
1781 # parent just returns
1783 msg( "log", "forked status daemon (pid $statusd_pid)\n" );
1784 return $statusd_pid;
1787 # child: the status FIFO daemon
1789 # ignore SIGPIPE here, in case some closes the FIFO without completely
1791 $SIG{"PIPE"} = "IGNORE";
1793 # also ignore SIGCLD, we don't want to inherit the restart-statusd handler
1795 $SIG{"CHLD"} = "DEFAULT";
1797 rm($conf::statusfile);
1798 $errs = `$conf::mkfifo $conf::statusfile`;
1799 die "$main::progname: cannot create named pipe $conf::statusfile: $errs"
1801 chmod( 0644, $conf::statusfile )
1802 or die "Cannot set modes of $conf::statusfile: $!\n";
1804 # close log file, so that log rotating works
1810 my ( $status, $mup, $incw, $ds, $next_run, $last_ping, $currch, $l );
1812 # open the FIFO for writing; this blocks until someone (probably ftpd)
1813 # opens it for reading
1814 open( STATFIFO, ">$conf::statusfile" )
1815 or die "Cannot open $conf::statusfile\n";
1818 # tell main daemon to send us status infos
1819 kill( $main::signo{"USR1"}, $main_pid );
1821 # get the infos from stdin; must loop until enough bytes received!
1822 my $expect_len = 3 + 2 * STATNUM_LEN + STATSTR_LEN;
1823 for ( $status = "" ; ( $l = length($status) ) < $expect_len ; ) {
1824 sysread( STDIN, $status, $expect_len - $l, $l );
1827 # disassemble the status byte stream
1833 [ next_run => STATNUM_LEN ],
1834 [ last_ping => STATNUM_LEN ],
1835 [ currch => STATSTR_LEN ]
1838 eval "\$$_->[0] = substr( \$status, $pos, $_->[1] );";
1840 } ## end foreach ( [ mup => 1 ], [ incw...
1841 $currch =~ s/\n+//g;
1843 print_status( $mup, $incw, $ds, $next_run, $last_ping, $currch );
1846 # This sleep is necessary so that we can't reopen the FIFO
1847 # immediately, in case the reader hasn't closed it yet if we get to
1848 # the open again. Is there a better solution for this??
1851 } ## end sub fork_statusd()
1854 # update the status file, in case we use a plain file and not a FIFO
1856 sub write_status_file() {
1858 return if !$conf::statusfile;
1860 open( STATFILE, ">$conf::statusfile" )
1861 or ( msg( "log", "Could not open $conf::statusfile: $!\n" ), return );
1862 my $oldsel = select(STATFILE);
1865 $main::target_up, $main::incoming_writable,
1866 $main::dstat, $main::next_run,
1867 $main::last_ping_time, $main::current_changes
1872 } ## end sub write_status_file()
1874 sub print_status($$$$$$) {
1878 my $next_run = shift;
1879 my $last_ping = shift;
1884 ( $version = 'Release: 0.9 $Revision: 1.51 $' ) =~ s/\$ ?//g;
1885 print "debianqueued $version\n";
1887 $approx = $conf::statusdelay ? "approx. " : "";
1889 if ( $mup eq "0" ) {
1890 print "$conf::target is down, queue pausing\n";
1892 } elsif ( $conf::upload_method ne "copy" ) {
1893 print "$conf::target seems to be up, last ping $approx",
1894 print_time( time - $last_ping ), " ago\n";
1897 if ( $incw eq "0" ) {
1898 print "The incoming directory is not writable, queue pausing\n";
1903 print "Next queue check in $approx", print_time( $next_run - time ), "\n";
1905 } elsif ( $ds eq "c" ) {
1906 print "Checking queue directory\n";
1907 } elsif ( $ds eq "u" ) {
1908 print "Uploading to $conf::target\n";
1910 print "Bad status data from daemon: \"$mup$incw$ds\"\n";
1914 print "Current job is $currch\n" if $currch;
1915 } ## end sub print_status($$$$$$)
1918 # format a number for sending to statusd (fixed length STATNUM_LEN)
1920 sub format_status_num(\$$) {
1924 $$varref = sprintf "%" . STATNUM_LEN . "d", $num;
1925 } ## end sub format_status_num(\$$)
1928 # format a string for sending to statusd (fixed length STATSTR_LEN)
1930 sub format_status_str(\$$) {
1934 $$varref = substr( $str, 0, STATSTR_LEN );
1935 $$varref .= "\n" x ( STATSTR_LEN - length($$varref) );
1936 } ## end sub format_status_str(\$$)
1939 # send a status string to the status daemon
1941 # Avoid all operations that could call malloc() here! Most libc
1942 # implementations aren't reentrant, so we may not call it from a
1943 # signal handler. So use only already-defined variables.
1946 local $! = 0; # preserve errno
1948 # re-setup handler, in case we have broken SysV signals
1949 $SIG{"USR1"} = \&send_status;
1951 syswrite( STATUSD, $main::target_up, 1 );
1952 syswrite( STATUSD, $main::incoming_writable, 1 );
1953 syswrite( STATUSD, $main::dstat, 1 );
1954 syswrite( STATUSD, $main::next_run, STATNUM_LEN );
1955 syswrite( STATUSD, $main::last_ping_time, STATNUM_LEN );
1956 syswrite( STATUSD, $main::current_changes, STATSTR_LEN );
1957 } ## end sub send_status()
1959 # ---------------------------------------------------------------------------
1961 # ---------------------------------------------------------------------------
1964 # open FTP connection to target host if not already open
1968 if ($main::FTP_chan) {
1970 # is already open, but might have timed out; test with a cwd
1971 return $main::FTP_chan
1972 if $main::FTP_chan->cwd($main::current_targetdir);
1974 # cwd didn't work, channel is closed, try to reopen it
1975 $main::FTP_chan = undef;
1976 } ## end if ($main::FTP_chan)
1983 Debug => $conf::ftpdebug,
1984 Timeout => $conf::ftptimeout,
1990 msg( "log,mail", "Cannot open FTP server $conf::target\n" );
1992 } ## end if ( !( $main::FTP_chan...
1993 if ( !$main::FTP_chan->login() ) {
1994 msg( "log,mail", "Anonymous login on FTP server $conf::target failed\n" );
1997 if ( !$main::FTP_chan->binary() ) {
1998 msg( "log,mail", "Can't set binary FTP mode on $conf::target\n" );
2001 if ( !$main::FTP_chan->cwd($main::current_targetdir) ) {
2003 "Can't cd to $main::current_targetdir on $conf::target\n" );
2006 debug("opened FTP channel to $conf::target");
2010 $main::FTP_chan = undef;
2012 } ## end sub ftp_open()
2017 my $direct_resp_cmd = ( $cmd eq "quot" );
2019 debug( "executing FTP::$cmd(" . join( ", ", @_ ) . ")" );
2020 $SIG{"ALRM"} = sub { die "timeout in FTP::$cmd\n" };
2021 alarm($conf::remote_timeout);
2022 eval { $rv = $main::FTP_chan->$cmd(@_); };
2025 $rv = ( ftp_code() =~ /^2/ ) ? 1 : 0 if $direct_resp_cmd;
2030 $err = ftp_response();
2032 return ( $rv, $err );
2033 } ## end sub ftp_cmd($@)
2036 if ($main::FTP_chan) {
2037 $main::FTP_chan->quit();
2038 $main::FTP_chan = undef;
2041 } ## end sub ftp_close()
2043 sub ftp_response() {
2044 return join( '', @{ ${*$main::FTP_chan}{'net_cmd_resp'} } );
2048 return ${*$main::FTP_chan}{'net_cmd_code'};
2052 my $code = ftp_code();
2053 return ( $code =~ /^[45]/ ) ? 1 : 0;
2056 # ---------------------------------------------------------------------------
2058 # ---------------------------------------------------------------------------
2064 my $ecmd = "$conf::ssh $conf::ssh_options $conf::target "
2065 . "-l $conf::targetlogin \'cd $main::current_targetdir; $cmd\'";
2066 debug("executing $ecmd");
2067 $SIG{"ALRM"} = sub { die "timeout in ssh command\n" };
2068 alarm($conf::remote_timeout);
2069 eval { $msg = `$ecmd 2>&1`; };
2077 return ( $msg, $stat );
2078 } ## end sub ssh_cmd($)
2083 my $ecmd = "$conf::scp $conf::ssh_options @_ "
2084 . "$conf::targetlogin\@$conf::target:$main::current_targetdir";
2085 debug("executing $ecmd");
2086 $SIG{"ALRM"} = sub { die "timeout in scp\n" };
2087 alarm($conf::remote_timeout);
2088 eval { $msg = `$ecmd 2>&1`; };
2096 return ( $msg, $stat );
2097 } ## end sub scp_cmd(@)
2099 sub local_cmd($;$) {
2104 my $ecmd = ( $nocd ? "" : "cd $main::current_targetdir; " ) . $cmd;
2105 debug("executing $ecmd");
2106 $msg = `($ecmd) 2>&1`;
2108 return ( $msg, $stat );
2110 } ## end sub local_cmd($;$)
2113 # check if target is alive (code stolen from Net::Ping.pm)
2115 sub check_alive(;$) {
2116 my $timeout = shift;
2117 my ( $saddr, $ret, $target_ip );
2120 if ( $conf::upload_method eq "copy" ) {
2121 format_status_num( $main::last_ping_time, time );
2122 $main::target_up = 1;
2128 if ( !( $target_ip = ( gethostbyname($conf::target) )[4] ) ) {
2129 msg( "log", "Cannot get IP address of $conf::target\n" );
2133 $saddr = pack( 'S n a4 x8', AF_INET, $main::echo_port, $target_ip );
2134 $SIG{'ALRM'} = sub { die };
2137 $ret = $main::tcp_proto; # avoid warnings about unused variable
2140 return unless socket( PINGSOCK, PF_INET, SOCK_STREAM, $main::tcp_proto );
2141 return unless connect( PINGSOCK, $saddr );
2146 msg( "log", "pinging $conf::target: " . ( $ret ? "ok" : "down" ) . "\n" );
2148 $main::target_up = $ret ? "1" : "0";
2149 format_status_num( $main::last_ping_time, time );
2150 write_status_file() if $conf::statusdelay;
2151 } ## end sub check_alive(;$)
2154 # check if incoming dir on target is writable
2156 sub check_incoming_writable() {
2157 my $testfile = ".debianqueued-testfile";
2160 if ( $conf::upload_method eq "ssh" ) {
2162 ssh_cmd( "rm -f $testfile; touch $testfile; " . "rm -f $testfile" );
2163 } elsif ( $conf::upload_method eq "ftp" ) {
2164 my $file = "junk-for-writable-test-" . format_time();
2165 $file =~ s/[ :.]/-/g;
2167 open( F, ">$file" );
2170 ( $rv, $msg ) = ftp_cmd( "put", $file );
2172 $msg = "" if !defined $msg;
2174 ftp_cmd( "delete", $file );
2175 } elsif ( $conf::upload_method eq "copy" ) {
2177 local_cmd( "rm -f $testfile; touch $testfile; " . "rm -f $testfile" );
2180 debug("exit status: $stat, output was: $msg");
2184 # change incoming_writable only if ssh didn't return an error
2185 $main::incoming_writable =
2186 ( $msg =~ /(permission denied|read-?only file|cannot create)/i )
2190 debug("local error, keeping old status");
2192 debug("incoming_writable = $main::incoming_writable");
2193 write_status_file() if $conf::statusdelay;
2194 return $main::incoming_writable;
2195 } ## end sub check_incoming_writable()
2198 # remove a list of files, log failing ones
2204 ( unlink $_ and ++$done )
2206 or msg( "log", "Could not delete $_: $!\n" );
2212 # get md5 checksum of a file
2217 open my $fh, "<", $file or return "";
2218 my $md5 = $md5->addfile($fh);
2221 return $md5->hexdigest;
2222 } ## end sub md5sum($)
2225 # check if a file probably belongs to a Debian upload
2227 sub is_debian_file($) {
2229 return $file =~ /\.(deb|dsc|(diff|tar)\.gz)$/
2230 && $file !~ /\.orig\.tar\.gz/;
2234 # try to extract maintainer email address from some a non-.changes file
2235 # return "" if not possible
2237 sub get_maintainer($) {
2239 my $maintainer = "";
2242 if ( $file =~ /\.diff\.gz$/ ) {
2245 open( F, "$conf::gzip -dc '$file' 2>/dev/null |" ) or return "";
2248 # look for header line of a file */debian/control
2249 last if m,^\+\+\+\s+[^/]+/debian/control(\s+|$),;
2252 last if /^---/; # end of control file patch, no Maintainer: found
2253 # inside control file patch look for Maintainer: field
2254 $maintainer = $1, last if /^\+Maintainer:\s*(.*)$/i;
2256 while (<F>) { } # read to end of file to avoid broken pipe
2257 close(F) or return "";
2258 } elsif ( $file =~ /\.(deb|dsc|tar\.gz)$/ ) {
2259 if ( $file =~ /\.deb$/ && $conf::ar ) {
2261 # extract control.tar.gz from .deb with ar, then let tar extract
2262 # the control file itself
2264 "($conf::ar p '$file' control.tar.gz | "
2265 . "$conf::tar -xOf - "
2266 . "--use-compress-program $conf::gzip "
2267 . "control) 2>/dev/null |"
2269 } elsif ( $file =~ /\.dsc$/ ) {
2271 # just do a plain grep
2272 debug("get_maint: .dsc, no cmd");
2273 open( F, "<$file" ) or return "";
2274 } elsif ( $file =~ /\.tar\.gz$/ ) {
2276 # let tar extract a file */debian/control
2278 "$conf::tar -xOf '$file' "
2279 . "--use-compress-program $conf::gzip "
2280 . "\\*/debian/control 2>&1 |"
2286 $maintainer = $1, last if /^Maintainer:\s*(.*)$/i;
2288 close(F) or return "";
2289 } ## end elsif ( $file =~ /\.(deb|dsc|tar\.gz)$/)
2292 } ## end sub get_maintainer($)
2295 # return a pattern that matches all files that probably belong to one job
2297 sub debian_file_stem($) {
2299 my ( $pkg, $version );
2302 $file =~ s,\.(deb|dsc|changes|(orig\.)?tar\.gz|diff\.gz)$,,;
2304 # if not is *_* (name_version), can't derive a stem and return just
2306 return $file if !( $file =~ /^([^_]+)_([^_]+)/ );
2307 ( $pkg, $version ) = ( $1, $2 );
2309 # strip Debian revision from version
2310 $version =~ s/^(.*)-[\d.+-]+$/$1/;
2312 return "${pkg}_${version}*";
2313 } ## end sub debian_file_stem($)
2316 # output a messages to several destinations
2318 # first arg is a comma-separated list of destinations; valid are "log"
2319 # and "mail"; rest is stuff to be printed, just as with print
2322 my @dest = split( ',', shift );
2324 if ( grep /log/, @dest ) {
2325 my $now = format_time();
2326 print LOG "$now ", @_;
2329 if ( grep /mail/, @dest ) {
2330 $main::mail_text .= join( '', @_ );
2332 } ## end sub msg($@)
2335 # print a debug messages, if $debug is true
2338 return if !$conf::debug;
2339 my $now = format_time();
2340 print LOG "$now DEBUG ", @_, "\n";
2344 # intialize the "mail" destination of msg() (this clears text,
2345 # address, subject, ...)
2350 $main::mail_addr = "";
2351 $main::mail_text = "";
2352 %main::packages = ();
2353 $main::mail_subject = $file ? "Processing of $file" : "";
2354 } ## end sub init_mail(;$)
2357 # finalize mail to be sent from msg(): check if something present, and
2362 debug("No mail for $main::mail_addr")
2363 if $main::mail_addr && !$main::mail_text;
2364 return unless $main::mail_addr && $main::mail_text;
2366 if ( !send_mail( $main::mail_addr, $main::mail_subject, $main::mail_text ) )
2369 # store this mail in memory so it isn't lost if executing sendmail
2372 @main::stored_mails,
2374 addr => $main::mail_addr,
2375 subject => $main::mail_subject,
2376 text => $main::mail_text
2379 } ## end if ( !send_mail( $main::mail_addr...
2382 # try to send out stored mails
2384 while ( $mailref = shift(@main::stored_mails) ) {
2386 !send_mail( $mailref->{'addr'}, $mailref->{'subject'},
2387 $mailref->{'text'} )
2390 unshift( @main::stored_mails, $mailref );
2392 } ## end if ( !send_mail( $mailref...
2393 } ## end while ( $mailref = shift(...
2394 } ## end sub finish_mail()
2399 sub send_mail($$$) {
2401 my $subject = shift;
2405 keys %main::packages ? join( ' ', keys %main::packages ) : "";
2409 unless ( defined($Email::Send::Sendmail::SENDMAIL) ) {
2410 $Email::Send::Sendmail::SENDMAIL = $conf::mail;
2413 if ($conf::overridemail) {
2414 $addr = $conf::overridemail;
2417 my $date = sprintf "%s",
2418 strftime( "%a, %d %b %Y %T %z", ( localtime(time) ) );
2419 my $message = <<__MESSAGE__;
2421 From: Debian FTP Masters <ftpmaster\@ftp-master.debian.org>
2428 if ( length $package ) {
2429 $message .= "X-Debian-Package: $package\n";
2432 $message .= "\n$text";
2433 $message .= "\nGreetings,\n\n\tYour Debian queue daemon (running on host $main::hostname)\n";
2435 my $mail = Email::Send->new;
2436 for (qw[Sendmail SMTP]) {
2437 $mail->mailer($_) and last if $mail->mailer_available($_);
2440 my $ret = $mail->send($message);
2441 if ( $ret && $ret !~ /Message sent|success/ ) {
2446 } ## end sub send_mail($$$)
2449 # try to find a mail address for a name in the keyrings
2451 sub try_to_get_mail_addr($$) {
2453 my $listref = shift;
2457 "$conf::gpg --no-options --batch --no-default-keyring "
2458 . "--always-trust --keyring "
2459 . join( " --keyring ", @conf::keyrings )
2463 if ( /^pub / && / $name / ) {
2465 push( @$listref, $1 );
2467 } ## end while (<F>)
2470 return ( @$listref >= 1 ) ? $listref->[0] : "";
2471 } ## end sub try_to_get_mail_addr($$)
2474 # return current time as string
2479 # omit weekday and year for brevity
2480 ( $t = localtime ) =~ /^\w+\s(.*)\s\d+$/;
2482 } ## end sub format_time()
2486 my $hours = int( $secs / ( 60 * 60 ) );
2488 $secs -= $hours * 60 * 60;
2489 return sprintf "%d:%02d:%02d", $hours, int( $secs / 60 ), $secs % 60;
2490 } ## end sub print_time($)
2493 # block some signals during queue processing
2495 # This is just to avoid data inconsistency or uploads being aborted in the
2496 # middle. Only "soft" signals are blocked, i.e. SIGINT and SIGTERM, try harder
2497 # ones if you really want to kill the daemon at once.
2499 sub block_signals() {
2500 POSIX::sigprocmask( SIG_BLOCK, $main::block_sigset );
2503 sub unblock_signals() {
2504 POSIX::sigprocmask( SIG_UNBLOCK, $main::block_sigset );
2508 # process SIGHUP: close log file and reopen it (for logfile cycling)
2515 open( LOG, ">>$conf::logfile" )
2516 or die "Cannot open my logfile $conf::logfile: $!\n";
2517 chmod( 0644, $conf::logfile )
2518 or msg( "log", "Cannot set modes of $conf::logfile: $!\n" );
2519 select( ( select(LOG), $| = 1 )[0] );
2521 open( STDOUT, ">&LOG" )
2523 "$main::progname: Can't redirect stdout to " . "$conf::logfile: $!\n" );
2524 open( STDERR, ">&LOG" )
2526 "$main::progname: Can't redirect stderr to " . "$conf::logfile: $!\n" );
2527 msg( "log", "Restart after SIGHUP\n" );
2528 } ## end sub close_log($)
2531 # process SIGCHLD: check if it was our statusd process
2536 # reap statusd, so that it's no zombie when we try to kill(0) it
2537 waitpid( $main::statusd_pid, WNOHANG );
2539 # Uncomment the following line if your Perl uses unreliable System V signal
2540 # (i.e. if handlers reset to default if the signal is delivered).
2541 # (Unfortunately, the re-setup can't be done in any case, since on some
2542 # systems this will cause the SIGCHLD to be delivered again if there are
2543 # still unreaped children :-(( )
2545 # $SIG{"CHLD"} = \&kid_died; # resetup handler for SysV
2546 } ## end sub kid_died($)
2548 sub restart_statusd() {
2550 # restart statusd if it died
2551 if ( !kill( 0, $main::statusd_pid ) ) {
2552 close(STATUSD); # close out pipe end
2553 $main::statusd_pid = fork_statusd();
2555 } ## end sub restart_statusd()
2558 # process a fatal signal: cleanup and exit
2560 sub fatal_signal($) {
2561 my $signame = shift;
2564 # avoid recursions of fatal_signal in case of BSD signals
2565 foreach $sig (qw( ILL ABRT BUS FPE SEGV PIPE )) {
2566 $SIG{$sig} = "DEFAULT";
2569 if ( $$ == $main::maind_pid ) {
2571 # only the main daemon should do this
2572 kill( $main::signo{"TERM"}, $main::statusd_pid )
2573 if defined $main::statusd_pid;
2574 unlink( $conf::statusfile, $conf::pidfile );
2575 } ## end if ( $$ == $main::maind_pid)
2576 msg( "log", "Caught SIG$signame -- exiting (pid $$)\n" );
2578 } ## end sub fatal_signal($)