]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/proc.c
Always cloak client hostname, if needed
[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         unsigned int seed;
54
55         assert(proc != NULL);
56         assert(pipefds != NULL);
57         assert(cbfunc != NULL);
58
59         if (pipe(pipefds) != 0) {
60                 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
61                     strerror(errno));
62                 return -1;
63         }
64
65         seed = (unsigned int)rand();
66         pid = fork();
67         switch (pid) {
68         case -1:
69                 /* Error on fork: */
70                 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
71                 close(pipefds[0]);
72                 close(pipefds[1]);
73                 return -1;
74         case 0:
75                 /* New child process: */
76                 srand(seed ^ (unsigned int)time(NULL) ^ getpid());
77                 Signals_Exit();
78                 signal(SIGTERM, Proc_GenericSignalHandler);
79                 signal(SIGALRM, Proc_GenericSignalHandler);
80                 close(pipefds[0]);
81                 alarm(timeout);
82                 return 0;
83         }
84
85         /* Old parent process: */
86         close(pipefds[1]);
87
88         if (!io_setnonblock(pipefds[0])
89          || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
90                 Log(LOG_CRIT, "Can't register callback for child process: %s!",
91                     strerror(errno));
92                 close(pipefds[0]);
93                 return -1;
94         }
95
96         proc->pid = pid;
97         proc->pipe_fd = pipefds[0];
98         return pid;
99 }
100
101 /**
102  * Generic signal handler for forked child processes.
103  */
104 GLOBAL void
105 Proc_GenericSignalHandler(int Signal)
106 {
107         switch(Signal) {
108         case SIGTERM:
109 #ifdef DEBUG
110                 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
111 #endif
112                 exit(1);
113         case SIGALRM:
114 #ifdef DEBUG
115                 Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
116 #endif
117                 exit(1);
118         }
119 }
120
121 /**
122  * Read bytes from a pipe of a forked child process.
123  * In addition, this function makes sure that the child process is ignored
124  * after all data has been read or a fatal error occurred.
125  */
126 GLOBAL size_t
127 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
128 {
129         ssize_t bytes_read = 0;
130
131         assert(buffer != NULL);
132         assert(buflen > 0);
133
134         bytes_read = read(proc->pipe_fd, buffer, buflen);
135         if (bytes_read < 0) {
136                 if (errno == EAGAIN)
137                         return 0;
138                 Log(LOG_CRIT, "Can't read from child process %ld: %s",
139                     proc->pid, strerror(errno));
140                 Proc_Close(proc);
141                 bytes_read = 0;
142         } else if (bytes_read == 0) {
143                 /* EOF: clean up */
144                 LogDebug("Child process %ld: EOF reached, closing pipe.",
145                          proc->pid);
146                 Proc_Close(proc);
147         }
148         return (size_t)bytes_read;
149 }
150
151 /**
152  * Close pipe to a forked child process.
153  */
154 GLOBAL void
155 Proc_Close(PROC_STAT *proc)
156 {
157         /* Close socket, if it exists */
158         if (proc->pipe_fd >= 0)
159                 io_close(proc->pipe_fd);
160
161         Proc_InitStruct(proc);
162 }
163
164 /* -eof- */