]> git.ozlabs.org Git - petitboot/blob - lib/process/process.c
lib/process: handle EINTR from waitpid
[petitboot] / lib / process / process.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; version 2 of the License.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include <assert.h>
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/wait.h>
21
22 #include <process/process.h>
23 #include <talloc/talloc.h>
24 #include <waiter/waiter.h>
25 #include <log/log.h>
26
27 struct procset {
28         struct waitset          *waitset;
29         struct list             async_list;
30         int                     sigchld_pipe[2];
31         struct waiter           *sigchld_waiter;
32         bool                    dry_run;
33 };
34
35 /* Internal data type for process handling
36  *
37  * Allocation: these structures may have multiple references:
38  *  - from the original ctx pointer
39  *  - due to inclusion in async_list
40  *  - due to a currently-registered waiter
41  *
42  */
43 struct process_info {
44 #ifdef DEBUG
45         /* prevent talloc_free(process) from working */
46         int                     __pad;
47 #endif
48         struct process          process;
49         struct list_item        async_list;
50         int                     stdout_buf_len;
51         struct waiter           *stdout_waiter;
52         int                     stdout_pipe[2];
53         void                    *orig_ctx;
54 };
55
56 static struct procset *procset;
57
58 static struct process_info *get_info(struct process *process)
59 {
60         return container_of(process, struct process_info, process);
61 }
62
63 /* Read as much as possible into the currently-allocated stdout buffer, and
64  * possibly realloc it for the next read */
65 static int process_read_stdout_once(struct process_info *procinfo)
66 {
67         struct process *process = &procinfo->process;
68         int rc, fd, max_len;
69
70         assert(process->keep_stdout);
71
72         fd = procinfo->stdout_pipe[0];
73
74         max_len =  procinfo->stdout_buf_len - process->stdout_len - 1;
75
76         rc = read(fd, process->stdout_buf + process->stdout_len, max_len);
77         if (rc <= 0)
78                 return rc;
79
80         process->stdout_len += rc;
81         if (process->stdout_len == procinfo->stdout_buf_len - 1) {
82                 procinfo->stdout_buf_len *= 2;
83                 process->stdout_buf = talloc_realloc(procinfo,
84                                 process->stdout_buf, char,
85                                 procinfo->stdout_buf_len);
86         }
87
88         return rc;
89 }
90
91 static int process_setup_stdout_pipe(struct process_info *procinfo)
92 {
93         int rc;
94
95         if (!procinfo->process.keep_stdout)
96                 return 0;
97
98         procinfo->stdout_buf_len = 4096;
99         procinfo->process.stdout_len = 0;
100         procinfo->process.stdout_buf = talloc_array(procinfo, char,
101                         procinfo->stdout_buf_len);
102
103         rc = pipe(procinfo->stdout_pipe);
104         if (rc) {
105                 pb_log("pipe failed");
106                 return rc;
107         }
108         return 0;
109 }
110
111 static void process_setup_stdout_parent(struct process_info *procinfo)
112 {
113         if (!procinfo->process.keep_stdout)
114                 return;
115
116         close(procinfo->stdout_pipe[1]);
117 }
118
119 static void process_setup_stdout_child(struct process_info *procinfo)
120 {
121         int log = fileno(pb_log_get_stream());
122
123         if (procinfo->process.keep_stdout)
124                 dup2(procinfo->stdout_pipe[1], STDOUT_FILENO);
125         else
126                 dup2(log, STDOUT_FILENO);
127
128         dup2(log, STDERR_FILENO);
129 }
130
131 static void process_finish_stdout(struct process_info *procinfo)
132 {
133         close(procinfo->stdout_pipe[0]);
134         procinfo->process.stdout_buf[procinfo->process.stdout_len] = '\0';
135 }
136
137 static int process_read_stdout(struct process_info *procinfo)
138 {
139         int rc;
140
141         if (!procinfo->process.keep_stdout)
142                 return 0;
143
144         do {
145                 rc = process_read_stdout_once(procinfo);
146         } while (rc > 0);
147
148         process_finish_stdout(procinfo);
149
150         return rc < 0 ? rc : 0;
151 }
152
153 static int process_stdout_cb(void *arg)
154 {
155         struct process_info *procinfo = arg;
156         int rc;
157
158         rc = process_read_stdout_once(procinfo);
159
160         /* if we're going to signal to the waitset that we're done (ie, non-zero
161          * return value), then the waiters will remove us, so we drop the
162          * reference */
163         if (rc < 0) {
164                 talloc_unlink(procset, procinfo);
165                 procinfo->stdout_waiter = NULL;
166                 rc = -1;
167         } else {
168                 rc = 0;
169         }
170
171         return rc;
172 }
173
174 static void sigchld_sigaction(int signo, siginfo_t *info,
175                 void *arg __attribute__((unused)))
176 {
177         pid_t pid;
178         int rc;
179
180         if (signo != SIGCHLD)
181                 return;
182
183         pid = info->si_pid;
184
185         rc = write(procset->sigchld_pipe[1], &pid, sizeof(pid));
186         if (rc != sizeof(pid))
187                 pb_log("%s: write failed: %s\n", __func__, strerror(errno));
188 }
189
190 static int sigchld_pipe_event(void *arg)
191 {
192         struct process_info *procinfo;
193         struct procset *procset = arg;
194         struct process *process;
195         int pid, rc;
196
197         rc = read(procset->sigchld_pipe[0], &pid, sizeof(pid));
198         if (rc != sizeof(pid))
199                 return 0;
200
201         process = NULL;
202         list_for_each_entry(&procset->async_list, procinfo, async_list) {
203                 if (procinfo->process.pid == pid) {
204                         process = &procinfo->process;
205                         break;
206                 }
207         }
208
209         /* We'll receive SIGCHLD for synchronous processes too; just ignore */
210         if (!process)
211                 return 0;
212
213         rc = waitpid(process->pid, &process->exit_status, WNOHANG);
214
215         /* if the process is still running, ignore the event. We leave
216          * the process in async_list so we can manage the final signal */
217         if (rc == 0)
218                 return 0;
219
220         /* ensure we have all of the child's stdout */
221         process_read_stdout(procinfo);
222
223         if (process->exit_cb)
224                 process->exit_cb(process);
225
226         list_remove(&procinfo->async_list);
227         talloc_unlink(procset, procinfo);
228
229         return 0;
230 }
231
232 static int process_fini(void *p)
233 {
234         struct procset *procset = p;
235         struct sigaction sa;
236
237         memset(&sa, 0, sizeof(sa));
238         sa.sa_handler = SIG_DFL;
239
240         sigaction(SIGCHLD, &sa, NULL);
241
242         waiter_remove(procset->sigchld_waiter);
243
244         close(procset->sigchld_pipe[0]);
245         close(procset->sigchld_pipe[1]);
246         return 0;
247 }
248
249 struct procset *process_init(void *ctx, struct waitset *set, bool dry_run)
250 {
251         struct sigaction sa;
252         int rc;
253
254         procset = talloc(ctx, struct procset);
255         procset->waitset = set;
256         procset->dry_run = dry_run;
257         list_init(&procset->async_list);
258
259         rc = pipe(procset->sigchld_pipe);
260         if (rc) {
261                 pb_log("%s: pipe() failed: %s\n", __func__, strerror(errno));
262                 goto err_free;
263         }
264
265         procset->sigchld_waiter = waiter_register_io(set,
266                                         procset->sigchld_pipe[0], WAIT_IN,
267                                         sigchld_pipe_event, procset);
268         if (!procset->sigchld_waiter)
269                 goto err_close;
270
271         memset(&sa, 0, sizeof(sa));
272         sa.sa_sigaction = sigchld_sigaction;
273         sa.sa_flags = SA_SIGINFO | SA_NOCLDSTOP;
274
275         rc = sigaction(SIGCHLD, &sa, NULL);
276         if (rc) {
277                 pb_log("%s: sigaction() failed: %s\n", __func__,
278                                 strerror(errno));
279                 goto err_remove;
280         }
281
282         talloc_set_destructor(procset, process_fini);
283
284         return procset;
285
286 err_remove:
287         waiter_remove(procset->sigchld_waiter);
288 err_close:
289         close(procset->sigchld_pipe[0]);
290         close(procset->sigchld_pipe[1]);
291 err_free:
292         talloc_free(procset);
293         return NULL;
294 }
295
296 struct process *process_create(void *ctx)
297 {
298         struct process_info *info = talloc_zero(ctx, struct process_info);
299         info->orig_ctx = ctx;
300         return &info->process;
301 }
302
303 void process_release(struct process *process)
304 {
305         struct process_info *info = get_info(process);
306         talloc_unlink(info->orig_ctx, info);
307 }
308
309 static int process_run_common(struct process_info *procinfo)
310 {
311         struct process *process = &procinfo->process;
312         const char *arg;
313         char *logmsg;
314         pid_t pid;
315         int rc, i;
316
317         logmsg = talloc_asprintf(procinfo, " exe:  %s\n argv:", process->path);
318         for (i = 0, arg = process->argv[i]; arg; i++, arg = process->argv[i])
319                 logmsg = talloc_asprintf_append(logmsg, " '%s'", arg);
320
321         pb_log("Running command:\n%s\n", logmsg);
322
323         rc = process_setup_stdout_pipe(procinfo);
324         if (rc)
325                 return rc;
326
327         pid = fork();
328         if (pid < 0) {
329                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
330                 return pid;
331         }
332
333         if (pid == 0) {
334                 process_setup_stdout_child(procinfo);
335                 if (procset->dry_run)
336                         exit(EXIT_SUCCESS);
337                 execvp(process->path, (char * const *)process->argv);
338                 exit(EXIT_FAILURE);
339         }
340
341         process_setup_stdout_parent(procinfo);
342         process->pid = pid;
343
344         return 0;
345 }
346
347 int process_run_sync(struct process *process)
348 {
349         struct process_info *procinfo = get_info(process);
350         int rc;
351
352         rc = process_run_common(procinfo);
353         if (rc)
354                 return rc;
355
356         process_read_stdout(procinfo);
357
358         for (;;) {
359                 rc = waitpid(process->pid, &process->exit_status, 0);
360                 if (rc >= 0)
361                         break;
362                 if (errno == EINTR)
363                         continue;
364
365                 pb_log("%s: waitpid failed: %s\n", __func__, strerror(errno));
366                 return rc;
367         }
368
369         return 0;
370 }
371
372 int process_run_async(struct process *process)
373 {
374         struct process_info *procinfo = get_info(process);
375         int rc;
376
377         rc = process_run_common(procinfo);
378         if (rc)
379                 return rc;
380
381         if (process->keep_stdout) {
382                 procinfo->stdout_waiter = waiter_register_io(procset->waitset,
383                                                 procinfo->stdout_pipe[0],
384                                                 WAIT_IN, process_stdout_cb,
385                                                 procinfo);
386                 talloc_reference(procset, procinfo);
387         }
388
389         list_add(&procset->async_list, &procinfo->async_list);
390         talloc_reference(procset, procinfo);
391
392         return 0;
393 }
394
395 void process_stop_async(struct process *process)
396 {
397         kill(process->pid, SIGTERM);
398 }
399
400 int process_run_simple_argv(void *ctx, const char *argv[])
401 {
402         struct process *process;
403         int rc;
404
405         process = process_create(ctx);
406
407         process->path = argv[0];
408         process->argv = argv;
409
410         rc = process_run_sync(process);
411
412         if (!rc)
413                 rc = process->exit_status;
414
415         process_release(process);
416
417         return rc;
418 }
419
420 int process_run_simple(void *ctx, const char *name, ...)
421 {
422         int rc, i, n_argv = 1;
423         const char **argv;
424         va_list ap;
425
426         va_start(ap, name);
427         while (va_arg(ap, char *))
428                 n_argv++;
429         va_end(ap);
430
431         argv = talloc_array(ctx, const char *, n_argv + 1);
432         argv[0] = name;
433
434         va_start(ap, name);
435         for (i = 1; i < n_argv; i++)
436                 argv[i] = va_arg(ap, const char *);
437         va_end(ap);
438
439         argv[i] = NULL;
440
441         rc = process_run_simple_argv(ctx, argv);
442
443         talloc_free(argv);
444
445         return rc;
446 }