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 * Initialize process structure.
35 Proc_InitStruct (PROC_STAT *proc)
43 * Fork a child process.
46 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short), int timeout)
51 assert(pipefds != NULL);
52 assert(cbfunc != NULL);
54 if (pipe(pipefds) != 0) {
55 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
64 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
69 /* New child process: */
70 signal(SIGTERM, Proc_GenericSignalHandler);
71 signal(SIGALRM, Proc_GenericSignalHandler);
74 Conn_CloseAllSockets();
78 /* Old parent process: */
81 if (!io_setnonblock(pipefds[0])
82 || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
83 Log(LOG_CRIT, "Can't register callback for child process: %s!",
90 proc->pipe_fd = pipefds[0];
95 * Generic signal handler for forked child processes.
98 Proc_GenericSignalHandler(int Signal)
103 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
108 Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
115 * Read bytes from a pipe of a forked child process.
116 * In addition, this function makes sure that the child process is ignored
117 * after all data has been read or a fatal error occurred.
120 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
122 ssize_t bytes_read = 0;
124 assert(buffer != NULL);
127 bytes_read = read(proc->pipe_fd, buffer, buflen);
128 if (bytes_read < 0) {
131 Log(LOG_CRIT, "Can't read from child process %ld: %s",
132 proc->pid, strerror(errno));
136 else if (bytes_read == 0)
137 LogDebug("Can't read from child process %ld: EOF", proc->pid);
139 Proc_InitStruct(proc);
140 return (size_t)bytes_read;