]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/proc.c
Add support for arc4random
[ngircd-alex.git] / src / ngircd / proc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
4  *
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.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Process management
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "log.h"
28 #include "io.h"
29 #include "conn.h"
30
31 #include "exp.h"
32 #include "sighandlers.h"
33 #include "proc.h"
34
35 /**
36  * Initialize process structure.
37  */
38 GLOBAL void
39 Proc_InitStruct (PROC_STAT *proc)
40 {
41         assert(proc != NULL);
42         proc->pid = 0;
43         proc->pipe_fd = -1;
44 }
45
46 /**
47  * Fork a child process.
48  */
49 GLOBAL pid_t
50 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short), int timeout)
51 {
52         pid_t pid;
53 #ifndef HAVE_ARC4RANDOM
54         unsigned int seed;
55 #endif
56
57         assert(proc != NULL);
58         assert(pipefds != NULL);
59         assert(cbfunc != NULL);
60
61         if (pipe(pipefds) != 0) {
62                 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
63                     strerror(errno));
64                 return -1;
65         }
66
67 #ifndef HAVE_ARC4RANDOM
68         seed = (unsigned int)rand();
69 #endif
70         pid = fork();
71         switch (pid) {
72         case -1:
73                 /* Error on fork: */
74                 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
75                 close(pipefds[0]);
76                 close(pipefds[1]);
77                 return -1;
78         case 0:
79                 /* New child process: */
80 #ifndef HAVE_ARC4RANDOM
81                 srand(seed ^ (unsigned int)time(NULL) ^ getpid());
82 #endif
83                 Signals_Exit();
84                 signal(SIGTERM, Proc_GenericSignalHandler);
85                 signal(SIGALRM, Proc_GenericSignalHandler);
86                 close(pipefds[0]);
87                 alarm(timeout);
88                 return 0;
89         }
90
91         /* Old parent process: */
92         close(pipefds[1]);
93
94         if (!io_setnonblock(pipefds[0])
95          || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
96                 Log(LOG_CRIT, "Can't register callback for child process: %s!",
97                     strerror(errno));
98                 close(pipefds[0]);
99                 return -1;
100         }
101
102         proc->pid = pid;
103         proc->pipe_fd = pipefds[0];
104         return pid;
105 }
106
107 /**
108  * Generic signal handler for forked child processes.
109  */
110 GLOBAL void
111 Proc_GenericSignalHandler(int Signal)
112 {
113         switch(Signal) {
114         case SIGTERM:
115 #ifdef DEBUG
116                 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
117 #endif
118                 exit(1);
119         case SIGALRM:
120 #ifdef DEBUG
121                 Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
122 #endif
123                 exit(1);
124         }
125 }
126
127 /**
128  * Read bytes from a pipe of a forked child process.
129  * In addition, this function makes sure that the child process is ignored
130  * after all data has been read or a fatal error occurred.
131  */
132 GLOBAL size_t
133 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
134 {
135         ssize_t bytes_read = 0;
136
137         assert(buffer != NULL);
138         assert(buflen > 0);
139
140         bytes_read = read(proc->pipe_fd, buffer, buflen);
141         if (bytes_read < 0) {
142                 if (errno == EAGAIN)
143                         return 0;
144                 Log(LOG_CRIT, "Can't read from child process %ld: %s",
145                     proc->pid, strerror(errno));
146                 Proc_Close(proc);
147                 bytes_read = 0;
148         } else if (bytes_read == 0) {
149                 /* EOF: clean up */
150                 LogDebug("Child process %ld: EOF reached, closing pipe.",
151                          proc->pid);
152                 Proc_Close(proc);
153         }
154         return (size_t)bytes_read;
155 }
156
157 /**
158  * Close pipe to a forked child process.
159  */
160 GLOBAL void
161 Proc_Close(PROC_STAT *proc)
162 {
163         /* Close socket, if it exists */
164         if (proc->pipe_fd >= 0)
165                 io_close(proc->pipe_fd);
166
167         Proc_InitStruct(proc);
168 }
169
170 /* -eof- */