]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/proc.c
dbcff6f1aa5cfe0edf16ac4b90aace58a681698f
[ngircd-alex.git] / src / ngircd / proc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
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  * Process management
12  */
13
14 #include "portab.h"
15
16 #include "imp.h"
17 #include <assert.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include "log.h"
25 #include "io.h"
26 #include "conn.h"
27
28 #include "exp.h"
29 #include "proc.h"
30
31 /**
32  * Initialize process structure.
33  */
34 GLOBAL void
35 Proc_InitStruct (PROC_STAT *proc)
36 {
37         assert(proc != NULL);
38         proc->pid = 0;
39         proc->pipe_fd = -1;
40 }
41
42 /**
43  * Fork a child process.
44  */
45 GLOBAL pid_t
46 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short), int timeout)
47 {
48         pid_t pid;
49
50         assert(proc != NULL);
51         assert(pipefds != NULL);
52         assert(cbfunc != NULL);
53
54         if (pipe(pipefds) != 0) {
55                 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
56                     strerror(errno));
57                 return -1;
58         }
59
60         pid = fork();
61         switch (pid) {
62         case -1:
63                 /* Error on fork: */
64                 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
65                 close(pipefds[0]);
66                 close(pipefds[1]);
67                 return -1;
68         case 0:
69                 /* New child process: */
70                 signal(SIGTERM, Proc_GenericSignalHandler);
71                 signal(SIGALRM, Proc_GenericSignalHandler);
72                 close(pipefds[0]);
73                 alarm(timeout);
74                 Conn_CloseAllSockets();
75                 return 0;
76         }
77
78         /* Old parent process: */
79         close(pipefds[1]);
80
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!",
84                     strerror(errno));
85                 close(pipefds[0]);
86                 return -1;
87         }
88
89         proc->pid = pid;
90         proc->pipe_fd = pipefds[0];
91         return pid;
92 }
93
94 /**
95  * Generic signal handler for forked child processes.
96  */
97 GLOBAL void
98 Proc_GenericSignalHandler(int Signal)
99 {
100         switch(Signal) {
101         case SIGTERM:
102 #ifdef DEBUG
103                 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
104 #endif
105                 exit(1);
106         case SIGALRM:
107 #ifdef DEBUG
108                 Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
109 #endif
110                 exit(1);
111         }
112 }
113
114 /**
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.
118  */
119 GLOBAL size_t
120 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
121 {
122         ssize_t bytes_read = 0;
123
124         assert(buffer != NULL);
125         assert(buflen > 0);
126
127         bytes_read = read(proc->pipe_fd, buffer, buflen);
128         if (bytes_read < 0) {
129                 if (errno == EAGAIN)
130                         return 0;
131                 Log(LOG_CRIT, "Can't read from child process %ld: %s",
132                     proc->pid, strerror(errno));
133                 bytes_read = 0;
134         }
135 #if DEBUG
136         else if (bytes_read == 0)
137                 LogDebug("Can't read from child process %ld: EOF", proc->pid);
138 #endif
139         Proc_InitStruct(proc);
140         return (size_t)bytes_read;
141 }
142
143 /* -eof- */