summaryrefslogtreecommitdiff
path: root/src/libstrongswan/utils/process.h
blob: 81719201cae2b81b1e8b99b52ca465a058949d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2014 Martin Willi
 * Copyright (C) 2014 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/**
 * @defgroup process process
 * @{ @ingroup utils
 */

#ifndef PROCESS_H_
#define PROCESS_H_

#include <utils/utils.h>

typedef struct process_t process_t;

/**
 * Child process spawning abstraction
 */
struct process_t {

	/**
	 * Wait for a started process to terminate.
	 *
	 * The process object gets destroyed by this call, regardless of the
	 * return value.
	 *
	 * The returned code is the exit code, not the status returned by waitpid().
	 * If the program could not be executed or has terminated abnormally
	 * (by signals etc.), FALSE is returned.
	 *
	 * @param code	process exit code, set only if TRUE returned
	 * @return		TRUE if program exited normally through exit()
	 */
	bool (*wait)(process_t *this, int *code);
};

/**
 * Spawn a child process with redirected I/O.
 *
 * Forks the current process, optionally redirects stdin/out/err to the current
 * process, and executes the provided program with arguments.
 *
 * The process to execute is specified as argv[0], followed by the process
 * arguments, followed by NULL. envp[] has a NULL terminated list of arguments
 * to invoke the process with.
 *
 * If any of in/out/err is given, stdin/out/err from the child process get
 * connected over pipe()s to the caller. If close_all is TRUE, all other
 * open file descriptors get closed, regardless of any CLOEXEC setting.
 *
 * A caller must close all of the returned file descriptors to avoid file
 * descriptor leaks.
 *
 * A non-NULL return value does not guarantee that the process has been
 * invoked successfully.
 *
 * @param argv		NULL terminated process arguments, with argv[0] as program
 * @param envp		NULL terminated list of environment variables
 * @param in		pipe fd returned for redirecting data to child stdin
 * @param out		pipe fd returned to redirect child stdout data to
 * @param err		pipe fd returned to redirect child stderr data to
 * @param close_all	close all open file descriptors above 2 before execve()
 * @return			process, NULL on failure
 */
process_t* process_start(char *const argv[], char *const envp[],
						 int *in, int *out, int *err, bool close_all);

/**
 * Spawn a command in a shell child process.
 *
 * Same as process_start(), but passes a single command to a shell, such as
 * "sh -c". See process_start() for I/O redirection notes.
 *
 * @param envp		NULL terminated list of environment variables
 * @param in		pipe fd returned for redirecting data to child stdin
 * @param out		pipe fd returned to redirect child stdout data to
 * @param err		pipe fd returned to redirect child stderr data to
 * @param fmt		printf format string for command
 * @param ...		arguments for fmt
 * @return			process, NULL on failure
 */
process_t* process_start_shell(char *const envp[], int *in, int *out, int *err,
							   char *fmt, ...);

#endif /** PROCESS_H_ @}*/