]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/proc.c
Refactor Resolve_Read() into generic Proc_Read() function
[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
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
97         if (proc->pipe_fd > 0)
98                 io_close(proc->pipe_fd);
99         if (proc->pid > 0)
100                 kill(proc->pid, SIGTERM);
101         Proc_InitStruct(proc);
102 }
103
104 /**
105  * Generic signal handler for forked child processes.
106  */
107 GLOBAL void
108 Proc_GenericSignalHandler(int Signal)
109 {
110         switch(Signal) {
111         case SIGTERM:
112 #ifdef DEBUG
113                 Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
114 #endif
115                 exit(1);
116         }
117 }
118
119 /**
120  * Read bytes from a pipe of a forked child process.
121  */
122 GLOBAL size_t
123 Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
124 {
125         ssize_t bytes_read = 0;
126
127         assert(buffer != NULL);
128         assert(buflen > 0);
129
130         bytes_read = read(proc->pipe_fd, buffer, buflen);
131         if (bytes_read < 0) {
132                 if (errno == EAGAIN)
133                         return 0;
134                 Log(LOG_CRIT, "Can't read from child process %ld: %s",
135                     proc->pid, strerror(errno));
136                 bytes_read = 0;
137         }
138 #if DEBUG
139         else if (bytes_read == 0)
140                 LogDebug("Can't read from child process %ld: EOF", proc->pid);
141 #endif
142         Proc_Kill(proc);
143         return (size_t)bytes_read;
144 }
145
146 /* -eof- */