]> git.ozlabs.org Git - petitboot/blob - lib/process/process.h
f75f1976c718df3970930c88be9bf1de2bde299f
[petitboot] / lib / process / process.h
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 #ifndef PROCESS_H
16 #define PROCESS_H
17
18 #include <stdbool.h>
19 #include <sys/types.h>
20
21 #include <list/list.h>
22 #include <waiter/waiter.h>
23
24 struct process;
25 struct procset;
26
27 typedef void    (*process_exit_cb)(struct process *);
28
29 struct process {
30         /* caller-provided configuration */
31         const char              *path;
32         const char              **argv;
33         bool                    keep_stdout;
34         bool                    add_stderr;
35         process_exit_cb         exit_cb;
36         void                    *data;
37
38         /* runtime data */
39         pid_t                   pid;
40         int                     stdout_len;
41         char                    *stdout_buf;
42
43         /* post-execution information */
44         int                     exit_status;
45 };
46
47 /* Process management system init. process_init must be called before
48  * process_create. The pointer returned can be talloc_free()-ed, or can be
49  * automatically freed through destruction of the ctx talloc tree.
50  */
51 struct procset *process_init(void *ctx, struct waitset *set, bool dry_run);
52
53 struct process *process_create(void *ctx);
54
55 /* process_release: release our reference to the process, but potentially
56  * leave it running. When the process exits, associated resources will
57  * be deallocated.
58  */
59 void process_release(struct process *process);
60
61 /* Synchronous interface. These functions will all block while waiting for
62  * the process to exit.
63  */
64 int process_run_sync(struct process *process);
65 int process_run_simple_argv(void *ctx, const char *argv[]);
66 int process_run_simple(void *ctx, const char *name, ...)
67         __attribute__((sentinel(0)));
68
69 /* Asynchronous interface. When a process is run with process_run_async, the
70  * function returns without wait()ing for the child process to exit. If the
71  * process' exit_cb member is set, that callback will be invoked when the
72  * process exits.
73  */
74 int process_run_async(struct process *process);
75
76 void process_stop_async(struct process *process);
77
78 /* helper function to determine if a process exited cleanly, with a non-zero
79  * exit status */
80 bool process_exit_ok(struct process *process);
81
82 #endif /* PROCESS_H */