1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum BsdSyscall {
6 Exit,
8 Read,
10 Write,
12 Open,
14 Close,
16 Getpid,
18 Getppid,
20 Access,
22 Issetugid,
24 Munmap,
26 Mprotect,
28 Mmap,
30 Msync,
32 Dup,
34 Fcntl,
36 Lseek,
38 Stat,
40 Fstat,
42 Lstat,
44 Unlink,
46 Link,
48 Symlink,
50 Readlink,
52 Mkdir,
54 Rmdir,
56 Rename,
58 Ftruncate,
60 Fsync,
62 Openat,
64 Fstatat,
66 Gettimeofday,
68 ClockGettime,
70 Sysctl,
72 Sysctlbyname,
74 Sigprocmask,
76 Sigaction,
78 BsdthreadCreate,
80 BsdthreadTerminate,
82 BsdthreadRegister,
84 ThreadSelfid,
86 Pipe,
88 Socket,
90 Connect,
92 Bind,
94 Listen,
96 Accept,
98 Setsockopt,
100 Getsockopt,
102 Shutdown,
104 Sendto,
106 Recvfrom,
108 Send,
110 Recv,
112 Poll,
114 Select,
116 Getsockname,
118 Getpeername,
120}
121
122impl BsdSyscall {
123 #[must_use]
125 pub const fn name(self) -> &'static str {
126 match self {
127 Self::Exit => "exit",
128 Self::Read => "read",
129 Self::Write => "write",
130 Self::Open => "open",
131 Self::Close => "close",
132 Self::Getpid => "getpid",
133 Self::Getppid => "getppid",
134 Self::Access => "access",
135 Self::Issetugid => "issetugid",
136 Self::Munmap => "munmap",
137 Self::Mprotect => "mprotect",
138 Self::Mmap => "mmap",
139 Self::Msync => "msync",
140 Self::Dup => "dup",
141 Self::Fcntl => "fcntl",
142 Self::Lseek => "lseek",
143 Self::Stat => "stat",
144 Self::Fstat => "fstat",
145 Self::Lstat => "lstat",
146 Self::Unlink => "unlink",
147 Self::Link => "link",
148 Self::Symlink => "symlink",
149 Self::Readlink => "readlink",
150 Self::Mkdir => "mkdir",
151 Self::Rmdir => "rmdir",
152 Self::Rename => "rename",
153 Self::Ftruncate => "ftruncate",
154 Self::Fsync => "fsync",
155 Self::Openat => "openat",
156 Self::Fstatat => "fstatat",
157 Self::Gettimeofday => "gettimeofday",
158 Self::ClockGettime => "clock_gettime",
159 Self::Sysctl => "sysctl",
160 Self::Sysctlbyname => "sysctlbyname",
161 Self::Sigprocmask => "sigprocmask",
162 Self::Sigaction => "sigaction",
163 Self::BsdthreadCreate => "bsdthread_create",
164 Self::BsdthreadTerminate => "bsdthread_terminate",
165 Self::BsdthreadRegister => "bsdthread_register",
166 Self::ThreadSelfid => "thread_selfid",
167 Self::Pipe => "pipe",
168 Self::Socket => "socket",
169 Self::Connect => "connect",
170 Self::Bind => "bind",
171 Self::Listen => "listen",
172 Self::Accept => "accept",
173 Self::Setsockopt => "setsockopt",
174 Self::Getsockopt => "getsockopt",
175 Self::Shutdown => "shutdown",
176 Self::Sendto => "sendto",
177 Self::Recvfrom => "recvfrom",
178 Self::Send => "send",
179 Self::Recv => "recv",
180 Self::Poll => "poll",
181 Self::Select => "select",
182 Self::Getsockname => "getsockname",
183 Self::Getpeername => "getpeername",
184 }
185 }
186
187 #[must_use]
189 pub const fn number(self) -> u32 {
190 match self {
191 Self::Exit => 1,
192 Self::Read => 3,
193 Self::Write => 4,
194 Self::Open => 5,
195 Self::Close => 6,
196 Self::Unlink => 10,
197 Self::Link => 9,
198 Self::Getpid => 20,
199 Self::Getppid => 39,
200 Self::Access => 33,
201 Self::Dup => 41,
202 Self::Symlink => 57,
203 Self::Readlink => 58,
204 Self::Sigaction => 46,
205 Self::Sigprocmask => 48,
206 Self::Msync => 65,
207 Self::Munmap => 73,
208 Self::Mprotect => 74,
209 Self::Fcntl => 92,
210 Self::Fsync => 95,
211 Self::Gettimeofday => 116,
212 Self::Rename => 128,
213 Self::Mkdir => 136,
214 Self::Rmdir => 137,
215 Self::Mmap => 197,
216 Self::Lseek => 199,
217 Self::Ftruncate => 201,
218 Self::Sysctl => 202,
219 Self::ClockGettime => 266,
220 Self::Sysctlbyname => 274,
221 Self::Issetugid => 327,
222 Self::Stat => 338,
224 Self::Fstat => 339,
225 Self::Lstat => 340,
226 Self::BsdthreadCreate => 360,
227 Self::BsdthreadTerminate => 361,
228 Self::BsdthreadRegister => 366,
229 Self::ThreadSelfid => 372,
230 Self::Openat => 463,
231 Self::Fstatat => 470,
232 Self::Pipe => 42,
233 Self::Accept => 30,
234 Self::Recvfrom | Self::Recv => 29,
235 Self::Socket => 97,
236 Self::Connect => 98,
237 Self::Bind => 104,
238 Self::Setsockopt => 105,
239 Self::Listen => 106,
240 Self::Getsockopt => 118,
241 Self::Sendto | Self::Send => 133,
242 Self::Shutdown => 134,
243 Self::Select => 93,
244 Self::Poll => 230,
245 Self::Getpeername => 31,
246 Self::Getsockname => 32,
247 }
248 }
249}
250
251#[must_use]
253pub const fn lookup(number: u32) -> Option<BsdSyscall> {
254 match number {
255 1 => Some(BsdSyscall::Exit),
256 3 | 396 => Some(BsdSyscall::Read),
257 4 | 397 => Some(BsdSyscall::Write),
258 5 | 398 => Some(BsdSyscall::Open),
259 6 | 399 => Some(BsdSyscall::Close),
260 9 => Some(BsdSyscall::Link),
261 10 | 401 => Some(BsdSyscall::Unlink),
262 20 => Some(BsdSyscall::Getpid),
263 33 => Some(BsdSyscall::Access),
264 39 => Some(BsdSyscall::Getppid),
265 29 | 403 => Some(BsdSyscall::Recvfrom), 30 | 404 => Some(BsdSyscall::Accept), 31 => Some(BsdSyscall::Getpeername),
268 32 => Some(BsdSyscall::Getsockname),
269 41 => Some(BsdSyscall::Dup),
270 42 | 405 => Some(BsdSyscall::Pipe), 46 => Some(BsdSyscall::Sigaction),
272 48 => Some(BsdSyscall::Sigprocmask),
273 57 => Some(BsdSyscall::Symlink),
274 58 => Some(BsdSyscall::Readlink),
275 65 => Some(BsdSyscall::Msync),
276 73 => Some(BsdSyscall::Munmap),
277 74 => Some(BsdSyscall::Mprotect),
278 92 => Some(BsdSyscall::Fcntl),
279 93 | 394 => Some(BsdSyscall::Select), 95 | 406 => Some(BsdSyscall::Fsync),
281 97 => Some(BsdSyscall::Socket),
282 98 => Some(BsdSyscall::Connect),
283 104 => Some(BsdSyscall::Bind),
284 105 => Some(BsdSyscall::Setsockopt),
285 106 => Some(BsdSyscall::Listen),
286 116 => Some(BsdSyscall::Gettimeofday),
287 118 => Some(BsdSyscall::Getsockopt),
288 128 => Some(BsdSyscall::Rename),
289 133 | 407 => Some(BsdSyscall::Sendto), 134 => Some(BsdSyscall::Shutdown),
291 136 => Some(BsdSyscall::Mkdir),
292 137 => Some(BsdSyscall::Rmdir),
293 188 | 338 => Some(BsdSyscall::Stat), 189 | 339 => Some(BsdSyscall::Fstat), 190 | 340 => Some(BsdSyscall::Lstat), 197 => Some(BsdSyscall::Mmap),
297 199 => Some(BsdSyscall::Lseek),
298 201 => Some(BsdSyscall::Ftruncate),
299 202 => Some(BsdSyscall::Sysctl),
300 230 => Some(BsdSyscall::Poll),
301 266 => Some(BsdSyscall::ClockGettime),
302 274 => Some(BsdSyscall::Sysctlbyname),
303 327 => Some(BsdSyscall::Issetugid),
304 360 => Some(BsdSyscall::BsdthreadCreate),
305 361 => Some(BsdSyscall::BsdthreadTerminate),
306 366 => Some(BsdSyscall::BsdthreadRegister),
307 372 => Some(BsdSyscall::ThreadSelfid),
308 463 => Some(BsdSyscall::Openat),
309 470 | 471 => Some(BsdSyscall::Fstatat), _ => None,
311 }
312}
313
314#[must_use]
316pub const fn name_of(number: u32) -> Option<&'static str> {
317 match lookup(number) {
318 Some(s) => Some(s.name()),
319 None => None,
320 }
321}
322
323#[must_use]
325pub fn known_syscalls() -> &'static [(u32, &'static str)] {
326 &[
327 (1, "exit"),
328 (3, "read"),
329 (4, "write"),
330 (5, "open"),
331 (6, "close"),
332 (20, "getpid"),
333 (33, "access"),
334 (39, "getppid"),
335 (41, "dup"),
336 (46, "sigaction"),
337 (48, "sigprocmask"),
338 (57, "symlink"),
339 (58, "readlink"),
340 (65, "msync"),
341 (73, "munmap"),
342 (74, "mprotect"),
343 (92, "fcntl"),
344 (116, "gettimeofday"),
345 (197, "mmap"),
346 (199, "lseek"),
347 (202, "sysctl"),
348 (266, "clock_gettime"),
349 (274, "sysctlbyname"),
350 (327, "issetugid"),
351 (9, "link"),
352 (10, "unlink"),
353 (95, "fsync"),
354 (128, "rename"),
355 (136, "mkdir"),
356 (137, "rmdir"),
357 (201, "ftruncate"),
358 (338, "stat"),
359 (339, "fstat"),
360 (340, "lstat"),
361 (463, "openat"),
362 (470, "fstatat"),
363 (360, "bsdthread_create"),
364 (361, "bsdthread_terminate"),
365 (366, "bsdthread_register"),
366 (372, "thread_selfid"),
367 (463, "openat"),
368 (42, "pipe"),
369 (97, "socket"),
370 (98, "connect"),
371 (104, "bind"),
372 (105, "setsockopt"),
373 (106, "listen"),
374 (118, "getsockopt"),
375 (133, "sendto"),
376 (134, "shutdown"),
377 (29, "recvfrom"),
378 (30, "accept"),
379 (93, "select"),
380 (230, "poll"),
381 (31, "getpeername"),
382 (32, "getsockname"),
383 ]
384}
385
386#[cfg(test)]
387mod tests {
388 use super::*;
389
390 #[test]
391 fn snapshot_table_consistent() {
392 for &(num, name) in known_syscalls() {
393 assert_eq!(name_of(num), Some(name));
394 assert_eq!(lookup(num).map(BsdSyscall::name), Some(name));
395 }
396 }
397
398 #[test]
399 fn nocancel_and_stat_aliases() {
400 assert_eq!(lookup(396), Some(BsdSyscall::Read));
401 assert_eq!(lookup(397), Some(BsdSyscall::Write));
402 assert_eq!(lookup(398), Some(BsdSyscall::Open));
403 assert_eq!(lookup(399), Some(BsdSyscall::Close));
404 assert_eq!(lookup(188), Some(BsdSyscall::Stat));
405 assert_eq!(lookup(189), Some(BsdSyscall::Fstat));
406 }
407}