aboutsummaryrefslogtreecommitdiffstats
path: root/liblfjail/exec.hh
blob: 18a2dd7dc676fb93b237256d2eee83f0368af16a (plain) (blame)
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
98
99
100
101
102
/*
 * This source code is released into the public domain.
 */

/*
 * Exec providers for spawn().
 */

#ifndef	LFJAIL_EXEC_HH
#define	LFJAIL_EXEC_HH

#include <unistd.h>

#include "argv.hh"
#include "exec_error.hh"
#include "fd.hh"
#include "guard.hh"
#include "path.hh"

namespace lfjail::exec {

/*
 * A concept to mark spawn executors.
 */

struct exec_tag{};

template<typename T>
concept executor =
	requires (T e) {
		std::same_as<exec_tag, typename T::tag>;
		{ e.exec() };
	};

/*
 * fexecv: use a file descriptor and an argument vector to call ::fexecve().
 * This is the lowest-level executor which all others are implemented
 * in terms of.
 *
 * TODO: Should have a way to pass the environment (envp).
 */
struct fexecv final {
	using tag = exec_tag;

	fexecv(fd &&execfd, argv &&args) noexcept;
	[[noreturn]] auto exec(this fexecv &self) noexcept -> void;
	
	// Movable
	fexecv(fexecv &&) noexcept = default;
	auto operator=(this fexecv &, fexecv &&) noexcept -> fexecv& = default;
	
	// Not copyable (because we hold the open fd object)
	fexecv(fexecv const &) = delete;
	auto operator=(this fexecv &, fexecv const &) -> fexecv& = delete;

private:
	fd _execfd;
	argv _args;
};

static_assert(executor<fexecv>);

/*
 * execv: equivalent to fexecv(), except the command is passed as
 * a pathname instead of a file descriptor.  Does not search $PATH.
 */
auto execv(std::string path, argv &&argv) -> fexecv;

/*
 * execvp: equivalent to fexecv(), except the command is passed as
 * a filename instead of a file descriptor.  If the filename is not
 * an absolute path, it will be searched for in $PATH.
 */
auto execvp(std::string file, argv &&argv) -> fexecv;

/*
 * execl: equivalent to execv, except the arguments are passed as a
 * variadic pack of string-like objects.
 */
auto execl(std::string path, auto &&...args) {
	return execv(std::move(path),
		     argv::from_args({std::string_view(args)...}));
}

/*
 * execlp: equivalent to execvp, except the arguments are passed as a
 * variadic pack of string-like objects.
 */
auto execlp(std::string file, auto &&...args) {
	return execvp(std::move(file),
		      argv::from_args({std::string_view(args)...}));
}

/*
 * shell: run the process by invoking /bin/sh -c with the single argument,
 * equivalent to system(3).
 */
auto shell(std::string command) -> fexecv;

} // namespace lfjail

#endif	// LFJAIL_EXEC_HH