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.
31 * Initialize process structure.
34 Proc_InitStruct (PROC_STAT *proc)
42 * Fork a child process.
45 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short))
50 assert(pipefds != NULL);
51 assert(cbfunc != NULL);
53 if (pipe(pipefds) != 0) {
54 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
63 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
68 /* New child process: */
69 signal(SIGTERM, Proc_GenericSignalHandler);
74 /* Old parent process: */
77 if (!io_setnonblock(pipefds[0])
78 || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
79 Log(LOG_CRIT, "Can't register callback for child process: %s!",
86 proc->pipe_fd = pipefds[0];
91 * Kill forked child process.
94 Proc_Kill(PROC_STAT *proc)
98 if (proc->pipe_fd > 0)
99 io_close(proc->pipe_fd);
101 kill(proc->pid, SIGTERM);
102 Proc_InitStruct(proc);
106 * Generic signal handler for forked child processes.
109 Proc_GenericSignalHandler(int Signal)
114 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
121 * Read bytes from a pipe of a forked child process.
122 * In addition, this function makes sure that the child process is dead
123 * after all data has been read or a fatal error occurred.
126 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
128 ssize_t bytes_read = 0;
130 assert(buffer != NULL);
133 bytes_read = read(proc->pipe_fd, buffer, buflen);
134 if (bytes_read < 0) {
137 Log(LOG_CRIT, "Can't read from child process %ld: %s",
138 proc->pid, strerror(errno));
142 else if (bytes_read == 0)
143 LogDebug("Can't read from child process %ld: EOF", proc->pid);
146 return (size_t)bytes_read;