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
|
/*
* This source code is released into the public domain.
*/
#include <sys/stat.h>
#include <err.h>
#include <fcntl.h>
#include <paths.h>
#include <unistd.h>
#include "exec.hh"
extern char **environ;
using namespace std::literals;
namespace lfjail::exec {
/*
* fexecv
*/
fexecv::fexecv(fd &&execfd, argv &&args) noexcept
: _execfd(std::move(execfd))
, _args(std::move(args))
{
}
auto fexecv::exec(this fexecv &self) noexcept -> void {
::fexecve(self._execfd.get(), self._args.data(), environ);
::err(1, "fexecve");
}
/*
* execv()
*/
auto execv(std::string const &path, argv &&argv) -> fexecv {
auto const ret = ::open(path.c_str(), O_EXEC);
if (ret == -1)
throw executable_not_found(path);
return {fd(ret), std::move(argv)};
}
/*
* execvp()
*/
auto execvp(std::string const &file, argv &&argv) -> fexecv {
auto execfd = find_in_path(file);
if (!execfd)
throw executable_not_found(file);
return {std::move(*execfd), std::move(argv)};
}
/*
* shell
*/
auto shell(std::string const &command) -> fexecv {
return execl("/bin/sh", "sh", "-c", command);
}
} // namespace lfjail
|