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.
32 #include "sighandlers.h"
36 * Initialize process structure.
39 Proc_InitStruct (PROC_STAT *proc)
47 * Fork a child process.
50 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short), int timeout)
55 assert(pipefds != NULL);
56 assert(cbfunc != NULL);
58 if (pipe(pipefds) != 0) {
59 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
68 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
73 /* New child process: */
75 signal(SIGTERM, Proc_GenericSignalHandler);
76 signal(SIGALRM, Proc_GenericSignalHandler);
79 Conn_CloseAllSockets();
83 /* Old parent process: */
86 if (!io_setnonblock(pipefds[0])
87 || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
88 Log(LOG_CRIT, "Can't register callback for child process: %s!",
95 proc->pipe_fd = pipefds[0];
100 * Generic signal handler for forked child processes.
103 Proc_GenericSignalHandler(int Signal)
108 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
113 Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
120 * Read bytes from a pipe of a forked child process.
121 * In addition, this function makes sure that the child process is ignored
122 * after all data has been read or a fatal error occurred.
125 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
127 ssize_t bytes_read = 0;
129 assert(buffer != NULL);
132 bytes_read = read(proc->pipe_fd, buffer, buflen);
133 if (bytes_read < 0) {
136 Log(LOG_CRIT, "Can't read from child process %ld: %s",
137 proc->pid, strerror(errno));
141 else if (bytes_read == 0)
142 LogDebug("Can't read from child process %ld: EOF", proc->pid);
144 Proc_InitStruct(proc);
145 return (size_t)bytes_read;