2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
29 #include "sighandlers.h"
33 * Initialize process structure.
36 Proc_InitStruct (PROC_STAT *proc)
44 * Fork a child process.
47 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short), int timeout)
52 assert(pipefds != NULL);
53 assert(cbfunc != NULL);
55 if (pipe(pipefds) != 0) {
56 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
65 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
70 /* New child process: */
72 signal(SIGTERM, Proc_GenericSignalHandler);
73 signal(SIGALRM, Proc_GenericSignalHandler);
76 Conn_CloseAllSockets();
80 /* Old parent process: */
83 if (!io_setnonblock(pipefds[0])
84 || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
85 Log(LOG_CRIT, "Can't register callback for child process: %s!",
92 proc->pipe_fd = pipefds[0];
97 * Generic signal handler for forked child processes.
100 Proc_GenericSignalHandler(int Signal)
105 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
110 Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
117 * Read bytes from a pipe of a forked child process.
118 * In addition, this function makes sure that the child process is ignored
119 * after all data has been read or a fatal error occurred.
122 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
124 ssize_t bytes_read = 0;
126 assert(buffer != NULL);
129 bytes_read = read(proc->pipe_fd, buffer, buflen);
130 if (bytes_read < 0) {
133 Log(LOG_CRIT, "Can't read from child process %ld: %s",
134 proc->pid, strerror(errno));
138 else if (bytes_read == 0)
139 LogDebug("Can't read from child process %ld: EOF", proc->pid);
141 Proc_InitStruct(proc);
142 return (size_t)bytes_read;