]> git.itanic.dy.fi Git - rrdd/blob - process.c
Rename get_process_count() to get_sibling_count()
[rrdd] / process.c
1 #include <unistd.h>
2 #include <sys/select.h>
3
4 #include "process.h"
5 #include "debug.h"
6
7 static int child_count = 0;
8 static int parent_count = 0;
9 static int process_count = 0;
10
11 int get_child_count(void)
12 {
13         return child_count;
14 }
15
16 int get_parent_count(void)
17 {
18         return parent_count;
19 }
20
21 int get_sibling_count(void)
22 {
23         return process_count;
24 }
25
26 int do_fork(void)
27 {
28         int child, error;
29         child = fork();
30         if (child < 0) {
31                 error = errno;
32                 pr_err("fork() failed: %s\n", strerror(error));
33                 return -1;
34         }
35
36         if (child) {
37                 child_count++;
38                 pr_info("Fork %d, child %d\n", child_count, child);
39                 return child;
40         }
41
42         process_count = child_count;
43         /* reset child's child count */
44         child_count = 0;
45         parent_count++;
46         return 0;
47 }
48
49 int harvest_zombies(int pid)
50 {
51         int status, error;
52
53         if (child_count == 0)
54                 return 0;
55
56         if (pid)
57                 pr_info("Waiting on pid %d, children left: %d\n", pid, 
58                         child_count);
59
60         pid = waitpid(pid, &status, 0);
61         if (pid < 0) {
62                 error = errno;
63                 pr_err("Error on wait(): %s\n", strerror(error));
64                 child_count--; /* Decrement child count anyway */
65         }
66         else {
67                 child_count--;
68                 pr_info("pid %d: exit code %d. Children left: %d\n", pid,
69                         status, child_count);
70         }
71
72         return 1;
73 }
74
75 /*
76  * Runs a command cmd with params argv, connects stdin and stdout to
77  * readfd and writefd
78  *
79  * Returns the pid of the executed process
80  */
81 int run_piped(const char *cmd, char *const argv[],
82               int *stdinfd, int *stdoutfd, int *stderrfd)
83 {
84         int ifd[2], ofd[2], efd[2], error, pid;
85
86         if (stdinfd && pipe(ifd)) {
87                 error = errno;
88                 pr_err("pipe() failed: %s\n", strerror(error));
89                 return -1;
90         }
91
92         if (stdoutfd && pipe(ofd)) {
93                 error = errno;
94                 pr_err("pipe() failed: %s\n", strerror(error));
95                 return -1;
96         }
97
98         if (stderrfd && pipe(efd)) {
99                 error = errno;
100                 pr_err("pipe() failed: %s\n", strerror(error));
101                 return -1;
102         }
103
104         pid = do_fork();
105         if (pid) { /* Parent side */
106                 if (stdinfd) {
107                         close(ifd[0]);
108                         *stdinfd = ifd[0];
109                 }
110
111                 if (stdoutfd) {
112                         close(ofd[1]);
113                         *stdoutfd = ofd[0];
114                 }
115
116                 if (stderrfd) {
117                         close(efd[1]);
118                         *stderrfd = efd[0];
119                 }
120
121                 return pid;
122         }
123
124         if (stdinfd) {
125                 close(ifd[1]);
126                 dup2(ifd[0], STDIN_FILENO);
127         }
128
129         if (stdoutfd) {
130                 close(ofd[0]);
131                 dup2(ofd[1], STDOUT_FILENO);
132         }
133
134         if (stderrfd) {
135                 close(efd[0]);
136                 dup2(efd[1], STDERR_FILENO);
137         }
138
139         /* Now we have redirected standard streams to parent process */
140         execvp(cmd, argv);
141         error = errno;
142         pr_err("Failed to execv command %s: %s\n", cmd, strerror(error));
143         exit(1);
144
145         return 0;
146 }
147
148 /*
149  * Runs a command cmd with params argv, connects stdin and stdout to
150  * readfd and writefd
151  *
152  * Returns the pid of the executed process
153  */
154 int run_piped_stream(const char *cmd, char *const argv[],
155                      FILE **stdinf, FILE **stdoutf, FILE **stderrf)
156 {
157         int ifd, ofd, efd, pid, error;
158         int *i, *o, *e;
159
160         if (stdinf)
161                 i = &ifd;
162         else 
163                 i = 0;
164         if (stdoutf)
165                 o = &ofd;
166         else 
167                 o = 0;
168         if (stderrf)
169                 e = &efd;
170         else 
171                 e = 0;
172
173         pid = run_piped(cmd, argv, i, o, e);
174
175         if (stdinf) {
176                 *stdinf = fdopen(ifd, "r");
177                 if (*stdinf == NULL) {
178                         error = errno;
179                         pr_err("Error opening file stream for fd %d: %s\n",
180                                ifd, strerror(error));
181                         return -1;
182                 }
183         }
184
185         if (stdoutf) {
186                 *stdoutf = fdopen(ofd, "r");
187                 if (*stdoutf == NULL) {
188                         error = errno;
189                         pr_err("Error opening file stream for fd %d: %s\n",
190                                ofd, strerror(error));
191                         return -1;
192                 }
193         }
194
195         if (stderrf) {
196                 *stderrf = fdopen(efd, "r");
197                 if (*stderrf == NULL) {
198                         error = errno;
199                         pr_err("Error opening file stream for fd %d: %s\n",
200                                efd, strerror(error));
201                         return -1;
202                 }
203         }
204
205         return pid;
206 }
207
208 /*
209  * Forks a child and executes a command to run on parallel
210  */
211
212 #define max(a,b) (a) < (b) ? (b) : (a)
213 #define BUF_SIZE (128*1024)
214 int run(const char *cmd, char *const argv[])
215 {
216         int child, error;
217         int ofd, efd;
218         fd_set rfds;
219         int maxfd;
220         char stdoutstr[32], stderrstr[32], indent[16] = { "                " };
221
222         indent[get_parent_count() + 1] = 0;
223                 
224         if ((child = do_fork()))
225             return child;
226
227         child = run_piped(cmd, argv, NULL, &ofd, &efd);
228         snprintf(stdoutstr, 32, "%sstdout", green_color);
229         snprintf(stderrstr, 32, "%sstderr", red_color);
230
231         FD_ZERO(&rfds);
232         FD_SET(ofd, &rfds);
233         FD_SET(efd, &rfds);
234
235         while (1) {
236                 char *sptr , *eptr;
237                 char rbuf[BUF_SIZE];
238                 int bytes;
239                 char *typestr;
240
241                 maxfd = max(ofd, efd);
242                 error = select(maxfd, &rfds, NULL, NULL, NULL);
243
244                 if (FD_ISSET(ofd, &rfds)) {
245                         typestr = stdoutstr;
246                         bytes = read(ofd, rbuf, BUF_SIZE);
247                         goto print;
248                 }
249
250                 if (FD_ISSET(efd, &rfds)) {
251                         typestr = stderrstr;
252                         bytes = read(efd, rbuf, BUF_SIZE);
253                         goto print;
254                 }
255
256                 pr_err("select() returned unknown fd\n");
257                 break;
258
259 print:
260                 if (bytes < 0) {
261                         error = errno;
262                         pr_err("read() failed: %s\n", strerror(error));
263                         break;
264                 }
265                 if (bytes == 0)
266                         break;
267
268                 sptr = eptr = rbuf;
269                 while(bytes--) {
270                         if (*eptr == '\n') {
271                                 *eptr = 0;
272                                 printf("%s[%5d %s] %s: %s%s\n", indent,
273                                        child, cmd, typestr, sptr, normal_color);
274                                 sptr = eptr;
275                         }
276                         eptr++;
277                 }
278         }
279
280         harvest_zombies(child); 
281
282         exit(1);
283         return 0;
284 }