Debian Codegraph @

third_party/rust/libc/src/unix/solarish/compat.rs

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