]> git.ozlabs.org Git - petitboot/blob - lib/process/process.h
discover/grub2: Allow to separate the --id argument using a space char
[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_stdout {
31         size_t len;
32         char *buf;
33 };
34
35 struct process {
36         /* caller-provided configuration */
37         const char              *path;
38         const char              **argv;
39         bool                    keep_stdout;
40         bool                    add_stderr;
41         bool                    raw_stdout;
42         process_exit_cb         exit_cb;
43         void                    *data;
44         waiter_cb               stdout_cb;
45         void                    *stdout_data;
46         char                    *pipe_stdin;
47
48         /* runtime data */
49         pid_t                   pid;
50         int                     stdout_len;
51         char                    *stdout_buf;
52
53         /* post-execution information */
54         int                     exit_status;
55         bool                    cancelled;
56 };
57
58 /* Process management system init. process_init must be called before
59  * process_create. The pointer returned can be talloc_free()-ed, or can be
60  * automatically freed through destruction of the ctx talloc tree.
61  */
62 struct procset *process_init(void *ctx, struct waitset *set, bool dry_run);
63
64 struct process *process_create(void *ctx);
65
66 /* process_release: release our reference to the process, but potentially
67  * leave it running. When the process exits, associated resources will
68  * be deallocated.
69  */
70 void process_release(struct process *process);
71
72 /* Synchronous interface. The process_run_sync, process_run_simple and
73  * process_get_stdout functions will all block while waiting for the child
74  * process to exit.  Calls to the variadic versions must have a NULL terminating
75  * argument.  For the process_get_stdout calls stderr will go to the log.
76  */
77 int process_run_sync(struct process *process);
78 int process_get_stdout_argv(void *ctx, struct process_stdout **stdout,
79         const char *argv[]);
80 int process_get_stdout(void *ctx, struct process_stdout **stdout,
81         const char *path, ...) __attribute__((sentinel(0)));
82
83 static inline int process_run_simple_argv(void *ctx, const char *argv[])
84 {
85         return process_get_stdout_argv(ctx, NULL, argv);
86 }
87 #define process_run_simple(_ctx, _path, args...) \
88         process_get_stdout(_ctx, NULL, _path, args)
89
90
91 /* Asynchronous interface. When a process is run with process_run_async, the
92  * function returns without wait()ing for the child process to exit. If the
93  * process' exit_cb member is set, that callback will be invoked when the
94  * process exits.
95  */
96 int process_run_async(struct process *process);
97
98 void process_stop_async(struct process *process);
99 void process_stop_async_all(void);
100
101 /* helper function to determine if a process exited cleanly, with a non-zero
102  * exit status */
103 bool process_exit_ok(struct process *process);
104
105 /* Functions to assist callers using a custom stdout callback */
106 struct process *procinfo_get_process(struct process_info *procinfo);
107 int process_process_stdout(struct process_info *procinfo, char **line);
108
109 #endif /* PROCESS_H */