]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Daemon.pm
daemon: do not chdir unless daemonizing
[public-inbox.git] / lib / PublicInbox / Daemon.pm
index 3d895b0912cc2e7c574b89d2f3a33cb5fedb7663..795ab8222950e57d9df5727720f304a7977ae4e3 100644 (file)
@@ -102,17 +102,18 @@ sub check_absolute ($$) {
 }
 
 sub daemonize () {
-       foreach my $i (0..$#ARGV) {
-               my $arg = $ARGV[$i];
-               next unless -e $arg;
-               $ARGV[$i] = abs_path($arg);
-       }
-       check_absolute('stdout', $stdout);
-       check_absolute('stderr', $stderr);
-       check_absolute('pid-file', $pid_file);
+       if ($daemonize) {
+               foreach my $i (0..$#ARGV) {
+                       my $arg = $ARGV[$i];
+                       next unless -e $arg;
+                       $ARGV[$i] = abs_path($arg);
+               }
+               check_absolute('stdout', $stdout);
+               check_absolute('stderr', $stderr);
+               check_absolute('pid-file', $pid_file);
 
-       chdir '/' or die "chdir failed: $!";
-       open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!";
+               chdir '/' or die "chdir failed: $!";
+       }
 
        return unless (defined $pid_file || defined $group || defined $user
                        || $daemonize);
@@ -145,6 +146,8 @@ sub daemonize () {
                die "could not fork: $!\n" unless defined $pid;
                exit if $pid;
 
+               open(STDIN, '+<', '/dev/null') or
+                                       die "redirect stdin failed: $!\n";
                open STDOUT, '>&STDIN' or die "redirect stdout failed: $!\n";
                open STDERR, '>&STDIN' or die "redirect stderr failed: $!\n";
                POSIX::setsid();
@@ -180,7 +183,8 @@ sub worker_quit {
                my $n = 0;
 
                foreach my $s (values %$dmap) {
-                       if ($s->can('busy') && $s->busy) {
+                       $s->can('busy') or next;
+                       if ($s->busy) {
                                ++$n;
                        } else {
                                # close as much as possible, early as possible
@@ -375,7 +379,13 @@ sub master_loop {
                                exit if $quit++;
                                kill_workers($s);
                        } elsif ($s eq 'WINCH') {
-                               $worker_processes = 0;
+                               if (-t STDIN || -t STDOUT || -t STDERR) {
+                                       warn
+"ignoring SIGWINCH since we are not daemonized\n";
+                                       $SIG{WINCH} = 'IGNORE';
+                               } else {
+                                       $worker_processes = 0;
+                               }
                        } elsif ($s eq 'HUP') {
                                $worker_processes = $set_workers;
                                kill_workers($s);
@@ -442,6 +452,8 @@ sub daemon_loop ($$) {
        $SIG{QUIT} = $SIG{INT} = $SIG{TERM} = *worker_quit;
        $SIG{USR1} = *reopen_logs;
        $SIG{HUP} = $refresh;
+       $SIG{CHLD} = 'DEFAULT';
+       $SIG{$_} = 'IGNORE' for qw(USR2 TTIN TTOU WINCH);
        # this calls epoll_create:
        @listeners = map {
                PublicInbox::Listener->new($_, $post_accept)