]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/proc.c
Use Proc_GenericSignalHandler() as handler for SIGTERM by default
[ngircd-alex.git] / src / ngircd / proc.c
index 75c1aaf1a0ba6dbfbb2f7abdfd8c91e01c76c8cf..11cb0396644548387926407913f09530e3373dcd 100644 (file)
@@ -66,6 +66,7 @@ Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short))
                return -1;
        case 0:
                /* New child process: */
+               signal(SIGTERM, Proc_GenericSignalHandler);
                close(pipefds[0]);
                return 0;
        }
@@ -93,10 +94,11 @@ GLOBAL void
 Proc_Kill(PROC_STAT *proc)
 {
        assert(proc != NULL);
-       assert(proc->pipe_fd >= 0);
 
-       io_close(proc->pipe_fd);
-       kill(proc->pid, SIGTERM);
+       if (proc->pipe_fd > 0)
+               io_close(proc->pipe_fd);
+       if (proc->pid > 0)
+               kill(proc->pid, SIGTERM);
        Proc_InitStruct(proc);
 }
 
@@ -115,4 +117,31 @@ Proc_GenericSignalHandler(int Signal)
        }
 }
 
+/**
+ * Read bytes from a pipe of a forked child process.
+ */
+GLOBAL size_t
+Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
+{
+       ssize_t bytes_read = 0;
+
+       assert(buffer != NULL);
+       assert(buflen > 0);
+
+       bytes_read = read(proc->pipe_fd, buffer, buflen);
+       if (bytes_read < 0) {
+               if (errno == EAGAIN)
+                       return 0;
+               Log(LOG_CRIT, "Can't read from child process %ld: %s",
+                   proc->pid, strerror(errno));
+               bytes_read = 0;
+       }
+#if DEBUG
+       else if (bytes_read == 0)
+               LogDebug("Can't read from child process %ld: EOF", proc->pid);
+#endif
+       Proc_Kill(proc);
+       return (size_t)bytes_read;
+}
+
 /* -eof- */