aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/execv.ccm
blob: 5c5a891b743ca379e1f5ade51c03d114f507fca7 (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
// This source code is released into the public domain.
module;

#include <unistd.h> // execv()

#include "nihil.hh"

extern char **environ; // NOLINT

export module nihil.posix:execv;

import nihil.std;
import nihil.core;
import :argv;
import :executor;
import :fd;

namespace nihil {

// execv: use a filename and an argument vector to call ::execv().
export struct execv final
{
	using tag = exec_tag;

	// Construct an execv from a filename.
	execv(std::filesystem::path path, argv &&args) noexcept
		: m_executable(std::move(path))
		, m_args(std::move(args))
	{
	}

	// Construct an execv from a file descriptor
	execv(fd &&executable, argv &&argv) noexcept
		: m_executable(std::move(executable))
		, m_args(std::move(argv))
	{
	}

	~execv() = default;

	// Movable
	execv(execv &&) noexcept = default;
	auto operator=(execv &&) noexcept -> execv & = default;

	// Not copyable (because m_args isn't copyable).
	execv(execv const &) = delete;
	auto operator=(this execv &, execv const &) -> execv & = delete;

	// Perform the execv().  This only returns on failure.
	[[nodiscard]] auto exec(this execv &self) -> std::expected<void, error>
	{
		auto guard = save_errno();

		// clang-format off
		return self.m_executable | match {
			[&](std::filesystem::path const &path) {
				::execv(path.string().c_str(), self.m_args.data());
				return std::unexpected(error("execve failed", error(sys_error())));
			},

			[&](fd const &file) {
#if NIHIL_HAVE_FEXECVE == 1
				::fexecve(file.get(), self.m_args.data(), environ);
				return std::unexpected(error("execve failed", error(sys_error())));
#else
				std::ignore = file;
				return std::unexpected(error(std::errc::function_not_supported));
#endif
			}
		};
		// clang-format on
	}

private:
	// The thing we will execute.
	std::variant<std::filesystem::path, fd> m_executable;
	// Arguments to pass to the thing.
	argv m_args;
};

} // namespace nihil