src/process.{c,h}: first implementation of create_child
[vlock.git] / src / process.h
blobf7415173a4be03fb82b848d37fd56298785c9745
1 /* process.h -- header for child process routines for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
10 * the author.
14 #include <stdbool.h>
15 #include <sys/types.h>
17 /* Wait for the given amount of time for the death of the given child process.
18 * If the child process dies in the given amount of time or already was dead
19 * true is returned and false otherwise. */
20 bool wait_for_death(pid_t pid, long sec, long usec);
22 /* Try hard to kill the given child process. */
23 void ensure_death(pid_t pid);
25 /* Close all possibly open file descriptors except STDIN_FILENO, STDOUT_FILENO
26 * and STDERR_FILENO. */
27 void close_all_fds(void);
29 #define NO_REDIRECT (-2)
30 #define REDIRECT_DEV_NULL (-3)
31 #define REDIRECT_PIPE (-4)
33 struct child_process {
34 /* Function that will be run in the child. */
35 int (*function)(void *argument);
36 /* Argument for the function. */
37 void *argument;
38 /* First argument to execv. */
39 const char *path;
40 /* Second argument to execv. */
41 const char *const *argv;
42 /* The child's stdin. */
43 int stdin_fd;
44 /* The child's stdout. */
45 int stdout_fd;
46 /* The child's stderr. */
47 int stderr_fd;
48 /* The child's PID. */
49 pid_t pid;
52 /* Create a new child process. All file descriptors except stdin, stdout and
53 * stderr are closed and privileges are dropped. All fields of the child
54 * struct except pid must be set. If a stdio file descriptor field has the
55 * special value of REDIRECT_DEV_NULL it is redirected from or to /dev/null.
56 * If it has the value REDIRECT_PIPE a pipe will be created and one end will be
57 * connected to the respective descriptor of the child. The file descriptor of
58 * the other end is stored in the field after the call. It is up to the caller
59 * to close the pipe descriptor(s). */
60 bool create_child(struct child_process *child);