]> arthur.barton.de Git - ngircd.git/blob - src/ngircd/proc.c
New function Proc_GenericSignalHandler()
[ngircd.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
27 #include "exp.h"
28 #include "proc.h"
29
30 /**
31  * Initialize process structure.
32  */
33 GLOBAL void
34 Proc_InitStruct (PROC_STAT *proc)
35 {
36         assert(proc != NULL);
37         proc->pid = 0;
38         proc->pipe_fd = -1;
39 }
40
41 /**
42  * Fork a child process.
43  */
44 GLOBAL pid_t
45 Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short))
46 {
47         pid_t pid;
48
49         assert(proc != NULL);
50         assert(pipefds != NULL);
51         assert(cbfunc != NULL);
52
53         if (pipe(pipefds) != 0) {
54                 Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
55                     strerror(errno));
56                 return -1;
57         }
58
59         pid = fork();
60         switch (pid) {
61         case -1:
62                 /* Error on fork: */
63                 Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
64                 close(pipefds[0]);
65                 close(pipefds[1]);
66                 return -1;
67         case 0:
68                 /* New child process: */
69                 close(pipefds[0]);
70                 return 0;
71         }
72
73         /* Old parent process: */
74         close(pipefds[1]);
75
76         if (!io_setnonblock(pipefds[0])
77          || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
78                 Log(LOG_CRIT, "Can't register callback for child process: %s!",
79                     strerror(errno));
80                 close(pipefds[0]);
81                 return -1;
82         }
83
84         proc->pid = pid;
85         proc->pipe_fd = pipefds[0];
86         return pid;
87 }
88
89 /**
90  * Kill forked child process.
91  */
92 GLOBAL void
93 Proc_Kill(PROC_STAT *proc)
94 {
95         assert(proc != NULL);
96         assert(proc->pipe_fd >= 0);
97
98         io_close(proc->pipe_fd);
99         kill(proc->pid, SIGTERM);
100         Proc_InitStruct(proc);
101 }
102
103 /**
104  * Generic signal handler for forked child processes.
105  */
106 GLOBAL void
107 Proc_GenericSignalHandler(int Signal)
108 {
109         switch(Signal) {
110         case SIGTERM:
111 #ifdef DEBUG
112                 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
113 #endif
114                 exit(1);
115         }
116 }
117
118 /* -eof- */