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