Debian Codegraph @

os/exec/+freebsd/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 let fd = match (rt::open(path, rt::O_RDONLY, 0u)) {
62 case let fd: int =>
63 yield fd;
64 case let err: rt::errno =>
65 return errors::errno(err);
66 };
67 let success = false;
68 defer if (!success) rt::close(fd)!;
69 match (rt::faccessat(fd, "", rt::X_OK, rt::AT_EMPTY_PATH)) {
70 case let err: rt::errno =>
71 return errors::errno(err);
72 case let b: bool =>
73 if (!b) {
74 return errors::noaccess;
75 };
76 };
77 // Make sure we are not trying to execute anything weird. fstat()
78 // already dereferences symlinks, so if this is anything other than a
79 // regular file it cannot be executed.
80 let s = rt::st { ... };
81 match (rt::fstat(fd, &s)) {
82 case let err: rt::errno =>
83 return errors::errno(err);
84 case void =>
85 if (s.mode & rt::S_IFREG == 0) {
86 return errors::noaccess;
87 };
88 };
89 success = true;
90 return fd;
91
92 };
93
94 fn platform_exec(cmd: *command) error = {
95 let need_devnull = false;
96 for (let file &.. cmd.files) {
97 const from = match (file.0) {
98 case let file: io::file =>
99 yield file;
100 case nullfd =>
101 need_devnull = true;
102 continue;
103 case closefd =>
104 continue;
105 };
106
107 file.0 = match (rt::fcntl(from, rt::F_DUPFD_CLOEXEC, 0)) {
108 case let fd: int =>
109 yield fd;
110 case let err: rt::errno =>
111 return errors::errno(err);
112 };
113 };
114
115 const devnull: io::file = if (need_devnull) {
116 yield os::open("/dev/null", fs::flag::RDWR)!;
117 } else -1;
118
119 for (let file .. cmd.files) {
120 const from = match (file.0) {
121 case let file: io::file =>
122 yield file;
123 case nullfd =>
124 yield devnull;
125 case closefd =>
126 io::close(file.1)?;
127 continue;
128 };
129
130 if (file.1 == from) {
131 let flags = match (rt::fcntl(from, rt::F_GETFD, 0)) {
132 case let flags: int =>
133 yield flags;
134 case let e: rt::errno =>
135 return errors::errno(e);
136 };
137 rt::fcntl(from, rt::F_SETFD, flags & ~rt::FD_CLOEXEC)!;
138 } else {
139 match (rt::dup2(from, file.1)) {
140 case int => void;
141 case let e: rt::errno =>
142 return errors::errno(e);
143 };
144 };
145 };
146
147 if (cmd.dir != "") {
148 os::chdir(cmd.dir)?;
149 };
150
151 let envp: nullable *[*]nullable *const c::char = null;
152 if (len(cmd.env) > 1) {
153 envp = cmd.env: *[*]nullable *const c::char;
154 };
155
156 return errors::errno(rt::fexecve(cmd.platform,
157 cmd.argv: *[*]nullable *const u8,
158 envp: *[*]nullable *const u8));
159 };
160
161 fn platform_start(cmd: *command) (process | errors::error) = {
162 // TODO: Let the user configure clone more to their taste (e.g. SIGCHLD)
163 let pipe: [2]int = [0...];
164 match (rt::pipe2(&pipe, rt::O_CLOEXEC)) {
165 case let err: rt::errno =>
166 return errors::errno(err);
167 case void => void;
168 };
169
170 match (rt::fork()) {
171 case let err: rt::errno =>
172 return errors::errno(err);
173 case let pid: rt::pid_t =>
174 rt::close(pipe[1])!;
175 defer rt::close(pipe[0])!;
176 let errno: int = 0;
177 match (rt::read(pipe[0], &errno, size(int))) {
178 case let err: rt::errno =>
179 return errors::errno(err);
180 case let n: size =>
181 switch (n) {
182 case size(int) =>
183 return errors::errno(errno);
184 case 0 =>
185 return pid;
186 case =>
187 abort("Unexpected rt::read result");
188 };
189 };
190 case void =>
191 rt::close(pipe[0])!;
192 let err = platform_exec(cmd);
193 if (!(err is errors::opaque_)) {
194 rt::exit(1);
195 };
196 let err = err as errors::opaque_;
197 let err = &err.data: *rt::errno;
198 rt::write(pipe[1], err, size(int))!;
199 rt::exit(1);
200 };
201 };