Debian Codegraph @

os/exec/+openbsd/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 use path;
12
13 // Forks the current process, returning the [[process]] of the child (to the
14 // parent) and void (to the child), or an error.
15 export fn fork() (process | void | error) = {
16 match (rt::fork()) {
17 case let err: rt::errno =>
18 return errors::errno(err);
19 case let i: int =>
20 return i: process;
21 case void =>
22 return void;
23 };
24 };
25
26 // Creates an anonymous pipe for use with [[addfile]]. Any data written to the
27 // second file may be read from the first file. The caller should close one or
28 // both of the file descriptors after they have transferred them to another
29 // process, and after they have finished using them themselves, if applicable.
30 //
31 // This function will abort the process if the system is unable to allocate the
32 // resources for a pipe. If you need to handle this error gracefully, you may
33 // call [[unix::pipe]] yourself, but this may reduce the portability of your
34 // software.
35 //
36 // To capture the standard output of a process:
37 //
38 // const (read, write) = exec::pipe();
39 // exec::addfile(&cmd, os::stdout_file, write);
40 // let proc = exec::start(&cmd)!;
41 // io::close(write)!;
42 //
43 // let data = io::drain(read)!;
44 // io::close(read)!;
45 // exec::wait(&proc)!;
46 //
47 // To write to the standard input of a process:
48 //
49 // const (read, write) = exec::pipe();
50 // exec::addfile(&cmd, os::stdin_file, read);
51 // let proc = exec::start(&cmd)!;
52 // io::close(read)!;
53 //
54 // io::writeall(write, data)!;
55 // io::close(write)!;
56 // exec::wait(&proc)!;
57 export fn pipe() (io::file, io::file) = {
58 return unix::pipe()!;
59 };
60
61 fn open(path: str) (platform_cmd | error) = {
62 if (os::access(path, os::amode::X_OK)?) {
63 // Length was already checked by access()
64 return path::init(path)!;
65 };
66 return errors::noaccess;
67 };
68
69 fn platform_exec(cmd: *command) error = {
70 let need_devnull = false;
71 for (let file &.. cmd.files) {
72 const from = match (file.0) {
73 case let file: io::file =>
74 yield file;
75 case nullfd =>
76 need_devnull = true;
77 continue;
78 case closefd =>
79 continue;
80 };
81
82 file.0 = match (rt::fcntl(from, rt::F_DUPFD_CLOEXEC, 0)) {
83 case let fd: int =>
84 yield fd;
85 case let err: rt::errno =>
86 return errors::errno(err);
87 };
88 };
89
90 const devnull: io::file = if (need_devnull) {
91 yield os::open("/dev/null", fs::flag::RDWR)!;
92 } else -1;
93
94 for (let file .. cmd.files) {
95 const from = match (file.0) {
96 case let file: io::file =>
97 yield file;
98 case nullfd =>
99 yield devnull;
100 case closefd =>
101 io::close(file.1)?;
102 continue;
103 };
104
105 if (file.1 == from) {
106 let flags = match (rt::fcntl(from, rt::F_GETFD, 0)) {
107 case let flags: int =>
108 yield flags;
109 case let e: rt::errno =>
110 return errors::errno(e);
111 };
112 rt::fcntl(from, rt::F_SETFD, flags & ~rt::FD_CLOEXEC)!;
113 } else {
114 match (rt::dup2(from, file.1)) {
115 case int => void;
116 case let e: rt::errno =>
117 return errors::errno(e);
118 };
119 };
120 };
121
122 if (cmd.dir != "") {
123 os::chdir(cmd.dir)?;
124 };
125
126 let envp: nullable *[*]nullable *const c::char = null;
127 if (len(cmd.env) > 1) {
128 envp = cmd.env: *[*]nullable *const c::char;
129 };
130
131 return errors::errno(rt::execve(path::string(&cmd.platform),
132 cmd.argv: *[*]nullable *const u8,
133 envp: *[*]nullable *const u8));
134 };
135
136 fn platform_start(cmd: *command) (process | errors::error) = {
137 // TODO: Let the user configure clone more to their taste (e.g. SIGCHLD)
138 let pipe: [2]int = [0...];
139 match (rt::pipe2(&pipe, rt::O_CLOEXEC)) {
140 case let err: rt::errno =>
141 return errors::errno(err);
142 case void => void;
143 };
144
145 match (rt::fork()) {
146 case let err: rt::errno =>
147 return errors::errno(err);
148 case let pid: int =>
149 rt::close(pipe[1])!;
150 defer rt::close(pipe[0])!;
151 let errno: int = 0;
152 match (rt::read(pipe[0], &errno, size(int))) {
153 case let err: rt::errno =>
154 return errors::errno(err);
155 case let n: size =>
156 switch (n) {
157 case size(int) =>
158 return errors::errno(errno);
159 case 0 =>
160 return pid;
161 case =>
162 abort("Unexpected rt::read result");
163 };
164 };
165 case void =>
166 rt::close(pipe[0])!;
167 let err = platform_exec(cmd);
168 if (!(err is errors::opaque_)) {
169 rt::exit(1);
170 };
171 let err = err as errors::opaque_;
172 let err = &err.data: *rt::errno;
173 rt::write(pipe[1], err, size(int))!;
174 rt::exit(1);
175 };
176 };