interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / libc / src / unix / solarish / compat.rs

222 lines · 6743 bytes Raw Blame History
1// Common functions that are unfortunately missing on illumos and
2// Solaris, but often needed by other crates.
3use core::cmp::min;
4
5use crate::unix::solarish::*;
6use crate::{
7 c_char,
8 c_int,
9 size_t,
10};
11
12pub unsafe fn cfmakeraw(termios: *mut crate::termios) {
13 (*termios).c_iflag &=
14 !(IMAXBEL | IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
15 (*termios).c_oflag &= !OPOST;
16 (*termios).c_lflag &= !(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
17 (*termios).c_cflag &= !(CSIZE | PARENB);
18 (*termios).c_cflag |= CS8;
19
20 // By default, most software expects a pending read to block until at
21 // least one byte becomes available. As per termio(7I), this requires
22 // setting the MIN and TIME parameters appropriately.
23 //
24 // As a somewhat unfortunate artefact of history, the MIN and TIME slots
25 // in the control character array overlap with the EOF and EOL slots used
26 // for canonical mode processing. Because the EOF character needs to be
27 // the ASCII EOT value (aka Control-D), it has the byte value 4. When
28 // switching to raw mode, this is interpreted as a MIN value of 4; i.e.,
29 // reads will block until at least four bytes have been input.
30 //
31 // Other platforms with a distinct MIN slot like Linux and FreeBSD appear
32 // to default to a MIN value of 1, so we'll force that value here:
33 (*termios).c_cc[VMIN] = 1;
34 (*termios).c_cc[VTIME] = 0;
35}
36
37pub unsafe fn cfsetspeed(termios: *mut crate::termios, speed: crate::speed_t) -> c_int {
38 // Neither of these functions on illumos or Solaris actually ever
39 // return an error
40 crate::cfsetispeed(termios, speed);
41 crate::cfsetospeed(termios, speed);
42 0
43}
44
45#[cfg(target_os = "illumos")]
46unsafe fn bail(fdm: c_int, fds: c_int) -> c_int {
47 let e = *___errno();
48 if fds >= 0 {
49 crate::close(fds);
50 }
51 if fdm >= 0 {
52 crate::close(fdm);
53 }
54 *___errno() = e;
55 -1
56}
57
58#[cfg(target_os = "illumos")]
59pub unsafe fn openpty(
60 amain: *mut c_int,
61 asubord: *mut c_int,
62 name: *mut c_char,
63 termp: *const termios,
64 winp: *const crate::winsize,
65) -> c_int {
66 const PTEM: &[u8] = b"ptem\0";
67 const LDTERM: &[u8] = b"ldterm\0";
68
69 // Open the main pseudo-terminal device, making sure not to set it as the
70 // controlling terminal for this process:
71 let fdm = crate::posix_openpt(O_RDWR | O_NOCTTY);
72 if fdm < 0 {
73 return -1;
74 }
75
76 // Set permissions and ownership on the subordinate device and unlock it:
77 if crate::grantpt(fdm) < 0 || crate::unlockpt(fdm) < 0 {
78 return bail(fdm, -1);
79 }
80
81 // Get the path name of the subordinate device:
82 let subordpath = crate::ptsname(fdm);
83 if subordpath.is_null() {
84 return bail(fdm, -1);
85 }
86
87 // Open the subordinate device without setting it as the controlling
88 // terminal for this process:
89 let fds = crate::open(subordpath, O_RDWR | O_NOCTTY);
90 if fds < 0 {
91 return bail(fdm, -1);
92 }
93
94 // Check if the STREAMS modules are already pushed:
95 let setup = crate::ioctl(fds, I_FIND, LDTERM.as_ptr());
96 if setup < 0 {
97 return bail(fdm, fds);
98 } else if setup == 0 {
99 // The line discipline is not present, so push the appropriate STREAMS
100 // modules for the subordinate device:
101 if crate::ioctl(fds, I_PUSH, PTEM.as_ptr()) < 0
102 || crate::ioctl(fds, I_PUSH, LDTERM.as_ptr()) < 0
103 {
104 return bail(fdm, fds);
105 }
106 }
107
108 // If provided, set the terminal parameters:
109 if !termp.is_null() && crate::tcsetattr(fds, TCSAFLUSH, termp) != 0 {
110 return bail(fdm, fds);
111 }
112
113 // If provided, set the window size:
114 if !winp.is_null() && crate::ioctl(fds, TIOCSWINSZ, winp) < 0 {
115 return bail(fdm, fds);
116 }
117
118 // If the caller wants the name of the subordinate device, copy it out.
119 //
120 // Note that this is a terrible interface: there appears to be no standard
121 // upper bound on the copy length for this pointer. Nobody should pass
122 // anything but NULL here, preferring instead to use ptsname(3C) directly.
123 if !name.is_null() {
124 crate::strcpy(name, subordpath);
125 }
126
127 *amain = fdm;
128 *asubord = fds;
129 0
130}
131
132#[cfg(target_os = "illumos")]
133pub unsafe fn forkpty(
134 amain: *mut c_int,
135 name: *mut c_char,
136 termp: *const termios,
137 winp: *const crate::winsize,
138) -> crate::pid_t {
139 let mut fds = -1;
140
141 if openpty(amain, &mut fds, name, termp, winp) != 0 {
142 return -1;
143 }
144
145 let pid = crate::fork();
146 if pid < 0 {
147 return bail(*amain, fds);
148 } else if pid > 0 {
149 // In the parent process, we close the subordinate device and return the
150 // process ID of the new child:
151 crate::close(fds);
152 return pid;
153 }
154
155 // The rest of this function executes in the child process.
156
157 // Close the main side of the pseudo-terminal pair:
158 crate::close(*amain);
159
160 // Use TIOCSCTTY to set the subordinate device as our controlling
161 // terminal. This will fail (with ENOTTY) if we are not the leader in
162 // our own session, so we call setsid() first. Finally, arrange for
163 // the pseudo-terminal to occupy the standard I/O descriptors.
164 if crate::setsid() < 0
165 || crate::ioctl(fds, TIOCSCTTY, 0) < 0
166 || crate::dup2(fds, 0) < 0
167 || crate::dup2(fds, 1) < 0
168 || crate::dup2(fds, 2) < 0
169 {
170 // At this stage there are no particularly good ways to handle failure.
171 // Exit as abruptly as possible, using _exit() to avoid messing with any
172 // state still shared with the parent process.
173 crate::_exit(EXIT_FAILURE);
174 }
175 // Close the inherited descriptor, taking care to avoid closing the standard
176 // descriptors by mistake:
177 if fds > 2 {
178 crate::close(fds);
179 }
180
181 0
182}
183
184pub unsafe fn getpwent_r(
185 pwd: *mut passwd,
186 buf: *mut c_char,
187 buflen: size_t,
188 result: *mut *mut passwd,
189) -> c_int {
190 let old_errno = *crate::___errno();
191 *crate::___errno() = 0;
192 *result = native_getpwent_r(pwd, buf, min(buflen, c_int::MAX as size_t) as c_int);
193
194 let ret = if (*result).is_null() {
195 *crate::___errno()
196 } else {
197 0
198 };
199 *crate::___errno() = old_errno;
200
201 ret
202}
203
204pub unsafe fn getgrent_r(
205 grp: *mut crate::group,
206 buf: *mut c_char,
207 buflen: size_t,
208 result: *mut *mut crate::group,
209) -> c_int {
210 let old_errno = *crate::___errno();
211 *crate::___errno() = 0;
212 *result = native_getgrent_r(grp, buf, min(buflen, c_int::MAX as size_t) as c_int);
213
214 let ret = if (*result).is_null() {
215 *crate::___errno()
216 } else {
217 0
218 };
219 *crate::___errno() = old_errno;
220
221 ret
222}