Debian Codegraph @

os/exec/+linux/exec.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use errors;
5 use fs;
6 use io;
7 use os;
8 use rt;
9 use types::c;
10 use unix;
11
12 // Forks the current process, returning the [[process]] of the child (to the
13 // parent) and void (to the child), or an error.
14 export fn fork() (process | void | error) = {
15 match (rt::fork()) {
16 case let err: rt::errno =>
17 return errors::errno(err);
18 case let i: rt::pid_t =>
19 return i: process;
20 case void =>
21 return void;
22 };
23 };
24
25 // Creates an anonymous pipe for use with [[addfile]]. Any data written to the
26 // second file may be read from the first file. The caller should close one or
27 // both of the file descriptors after they have transferred them to another
28 // process, and after they have finished using them themselves, if applicable.
29 //
30 // This function will abort the process if the system is unable to allocate the
31 // resources for a pipe. If you need to handle this error gracefully, you may
32 // call [[unix::pipe]] yourself, but this may reduce the portability of your
33 // software.
34 //
35 // To capture the standard output of a process:
36 //
37 // const (read, write) = exec::pipe();
38 // exec::addfile(&cmd, os::stdout_file, write);
39 // let proc = exec::start(&cmd)!;
40 // io::close(write)!;
41 //
42 // let data = io::drain(read)!;
43 // io::close(read)!;
44 // exec::wait(&proc)!;
45 //
46 // To write to the standard input of a process:
47 //
48 // const (read, write) = exec::pipe();
49 // exec::addfile(&cmd, os::stdin_file, read);
50 // let proc = exec::start(&cmd)!;
51 // io::close(read)!;
52 //
53 // io::writeall(write, data)!;
54 // io::close(write)!;
55 // exec::wait(&proc)!;
56 export fn pipe() (io::file, io::file) = {
57 return unix::pipe()!;
58 };
59
60 fn open(path: str) (platform_cmd | error) = {
61 // O_PATH is used because it allows us to use an executable for which we
62 // have execute permissions, but not read permissions.
63 let fd = match (rt::open(path, rt::O_PATH, 0u)) {
64 case let fd: int =>
65 yield fd;
66 case let err: rt::errno =>
67 return errors::errno(err);
68 };
69 let success = false;
70 defer if (!success) rt::close(fd)!;
71 match (rt::faccessat(fd, "", rt::X_OK, rt::AT_EMPTY_PATH)) {
72 case let err: rt::errno =>
73 // not ideal, but better to do Something on old kernels rather
74 // than just breaking entirely
75 if (err != rt::ENOSYS) {
76 return errors::errno(err);
77 };
78 case let b: bool =>
79 if (!b) {
80 return errors::noaccess;
81 };
82 };
83 // Make sure we are not trying to execute anything weird. fstat()
84 // already dereferences symlinks, so if this is anything other than a
85 // regular file it cannot be executed.
86 let s = rt::st { ... };
87 match (rt::fstat(fd, &s)) {
88 case let err: rt::errno =>
89 return errors::errno(err);
90 case void =>
91 if (s.mode & rt::S_IFREG == 0) {
92 return errors::noaccess;
93 };
94 };
95 success = true;
96 return fd;
97 };
98
99 fn platform_exec(cmd: *command) error = {
100 let need_devnull = false;
101 for (let file &.. cmd.files) {
102 const from = match (file.0) {
103 case let file: io::file =>
104 yield file;
105 case nullfd =>
106 need_devnull = true;
107 continue;
108 case closefd =>
109 continue;
110 };
111
112 file.0 = match (rt::fcntl(from, rt::F_DUPFD_CLOEXEC, 0)) {
113 case let fd: int =>
114 yield fd;
115 case let err: rt::errno =>
116 return errors::errno(err);
117 };
118 };
119
120 const devnull: io::file = if (need_devnull) {
121 yield os::open("/dev/null", fs::flag::RDWR)!;
122 } else -1;
123
124 for (let file .. cmd.files) {
125 const from = match (file.0) {
126 case let file: io::file =>
127 yield file;
128 case nullfd =>
129 yield devnull;
130 case closefd =>
131 io::close(file.1)?;
132 continue;
133 };
134
135 if (file.1 == from) {
136 let flags = match (rt::fcntl(from, rt::F_GETFD, 0)) {
137 case let flags: int =>
138 yield flags;
139 case let e: rt::errno =>
140 return errors::errno(e);
141 };
142 rt::fcntl(from, rt::F_SETFD, flags & ~rt::FD_CLOEXEC)!;
143 } else {
144 match (rt::dup2(from, file.1)) {
145 case int => void;
146 case let e: rt::errno =>
147 return errors::errno(e);
148 };
149 };
150 };
151
152 if (cmd.dir != "") {
153 os::chdir(cmd.dir)?;
154 };
155
156 let envp: nullable *[*]nullable *const c::char = null;
157 if (len(cmd.env) > 1) {
158 envp = cmd.env: *[*]nullable *const c::char;
159 };
160
161 return errors::errno(rt::execveat(cmd.platform,
162 "\0", cmd.argv: *[*]nullable *const u8,
163 envp: *[*]nullable *const u8, rt::AT_EMPTY_PATH));
164 };
165
166 fn platform_start(cmd: *command) (process | errors::error) = {
167 // TODO: Let the user configure clone more to their taste (e.g. SIGCHLD)
168 let pipe: [2]int = [0...];
169 match (rt::pipe2(&pipe, rt::O_CLOEXEC)) {
170 case let err: rt::errno =>
171 return errors::errno(err);
172 case void => void;
173 };
174
175 match (rt::clone(null, rt::SIGCHLD, null, null, 0)) {
176 case let err: rt::errno =>
177 return errors::errno(err);
178 case let pid: int =>
179 rt::close(pipe[1])!;
180 defer rt::close(pipe[0])!;
181 let errno: int = 0;
182 match (rt::read(pipe[0], &errno, size(int))) {
183 case let err: rt::errno =>
184 return errors::errno(err);
185 case let n: size =>
186 switch (n) {
187 case size(int) =>
188 return errors::errno(errno);
189 case 0 =>
190 return pid;
191 case =>
192 abort("Unexpected rt::read result");
193 };
194 };
195 case void =>
196 rt::close(pipe[0])!;
197 let err = platform_exec(cmd);
198 if (!(err is errors::opaque_)) {
199 rt::exit(1);
200 };
201 let err = err as errors::opaque_;
202 let err = &err.data: *rt::errno;
203 rt::write(pipe[1], err, size(int))!;
204 rt::exit(1);
205 };
206 };