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