1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum BsdSyscall {
6 Exit,
8 Fork,
10 Vfork,
12 Wait4,
14 Execve,
16 Dup2,
18 Setsid,
20 Setpgid,
22 Getpgrp,
24 Kill,
26 Read,
28 Write,
30 Open,
32 Close,
34 Getpid,
36 Getppid,
38 Getuid,
40 Geteuid,
42 Getgid,
44 Getegid,
46 Access,
48 Issetugid,
50 Munmap,
52 Mprotect,
54 Mmap,
56 Msync,
58 Dup,
60 Fcntl,
62 Lseek,
64 Pread,
66 Pwrite,
68 Stat,
70 Fstat,
72 Lstat,
74 Unlink,
76 Link,
78 Symlink,
80 Readlink,
82 Mkdir,
84 Rmdir,
86 Chdir,
88 Getcwd,
90 Rename,
92 Ftruncate,
94 Fsync,
96 Openat,
98 Fstatat,
100 Gettimeofday,
102 ClockGettime,
104 Sysctl,
106 Sysctlbyname,
108 Sigprocmask,
110 Sigaction,
112 BsdthreadCreate,
114 BsdthreadTerminate,
116 BsdthreadRegister,
118 ThreadSelfid,
120 Pipe,
122 Socket,
124 Socketpair,
126 Connect,
128 Bind,
130 Listen,
132 Accept,
134 Setsockopt,
136 Getsockopt,
138 Shutdown,
140 Sendto,
142 Recvfrom,
144 Sendmsg,
146 Recvmsg,
148 Send,
150 Recv,
152 Poll,
154 Select,
156 Getsockname,
158 Getpeername,
160}
161
162impl BsdSyscall {
163 #[must_use]
165 pub const fn name(self) -> &'static str {
166 match self {
167 Self::Exit => "exit",
168 Self::Fork => "fork",
169 Self::Vfork => "vfork",
170 Self::Wait4 => "wait4",
171 Self::Execve => "execve",
172 Self::Dup2 => "dup2",
173 Self::Setsid => "setsid",
174 Self::Setpgid => "setpgid",
175 Self::Getpgrp => "getpgrp",
176 Self::Kill => "kill",
177 Self::Read => "read",
178 Self::Write => "write",
179 Self::Open => "open",
180 Self::Close => "close",
181 Self::Getpid => "getpid",
182 Self::Getppid => "getppid",
183 Self::Getuid => "getuid",
184 Self::Geteuid => "geteuid",
185 Self::Getgid => "getgid",
186 Self::Getegid => "getegid",
187 Self::Access => "access",
188 Self::Issetugid => "issetugid",
189 Self::Munmap => "munmap",
190 Self::Mprotect => "mprotect",
191 Self::Mmap => "mmap",
192 Self::Msync => "msync",
193 Self::Dup => "dup",
194 Self::Fcntl => "fcntl",
195 Self::Lseek => "lseek",
196 Self::Pread => "pread",
197 Self::Pwrite => "pwrite",
198 Self::Stat => "stat",
199 Self::Fstat => "fstat",
200 Self::Lstat => "lstat",
201 Self::Unlink => "unlink",
202 Self::Link => "link",
203 Self::Symlink => "symlink",
204 Self::Readlink => "readlink",
205 Self::Mkdir => "mkdir",
206 Self::Rmdir => "rmdir",
207 Self::Chdir => "chdir",
208 Self::Getcwd => "__getcwd",
209 Self::Rename => "rename",
210 Self::Ftruncate => "ftruncate",
211 Self::Fsync => "fsync",
212 Self::Openat => "openat",
213 Self::Fstatat => "fstatat",
214 Self::Gettimeofday => "gettimeofday",
215 Self::ClockGettime => "clock_gettime",
216 Self::Sysctl => "sysctl",
217 Self::Sysctlbyname => "sysctlbyname",
218 Self::Sigprocmask => "sigprocmask",
219 Self::Sigaction => "sigaction",
220 Self::BsdthreadCreate => "bsdthread_create",
221 Self::BsdthreadTerminate => "bsdthread_terminate",
222 Self::BsdthreadRegister => "bsdthread_register",
223 Self::ThreadSelfid => "thread_selfid",
224 Self::Pipe => "pipe",
225 Self::Socket => "socket",
226 Self::Socketpair => "socketpair",
227 Self::Connect => "connect",
228 Self::Bind => "bind",
229 Self::Listen => "listen",
230 Self::Accept => "accept",
231 Self::Setsockopt => "setsockopt",
232 Self::Getsockopt => "getsockopt",
233 Self::Shutdown => "shutdown",
234 Self::Sendto => "sendto",
235 Self::Recvfrom => "recvfrom",
236 Self::Sendmsg => "sendmsg",
237 Self::Recvmsg => "recvmsg",
238 Self::Send => "send",
239 Self::Recv => "recv",
240 Self::Poll => "poll",
241 Self::Select => "select",
242 Self::Getsockname => "getsockname",
243 Self::Getpeername => "getpeername",
244 }
245 }
246
247 #[must_use]
249 pub const fn number(self) -> u32 {
250 match self {
251 Self::Exit => 1,
252 Self::Fork => 2,
253 Self::Read => 3,
254 Self::Write => 4,
255 Self::Open => 5,
256 Self::Close => 6,
257 Self::Wait4 => 7,
258 Self::Unlink => 10,
259 Self::Execve => 59,
260 Self::Vfork => 66,
261 Self::Dup2 => 90,
262 Self::Setsid => 147,
263 Self::Setpgid => 82,
264 Self::Getpgrp => 81,
265 Self::Kill => 37,
266 Self::Link => 9,
267 Self::Getpid => 20,
268 Self::Getuid => 24,
269 Self::Geteuid => 25,
270 Self::Getgid => 47,
271 Self::Getegid => 43,
272 Self::Getppid => 39,
273 Self::Access => 33,
274 Self::Dup => 41,
275 Self::Symlink => 57,
276 Self::Readlink => 58,
277 Self::Sigaction => 46,
278 Self::Sigprocmask => 48,
279 Self::Msync => 65,
280 Self::Munmap => 73,
281 Self::Mprotect => 74,
282 Self::Fcntl => 92,
283 Self::Fsync => 95,
284 Self::Gettimeofday => 116,
285 Self::Rename => 128,
286 Self::Mkdir => 136,
287 Self::Rmdir => 137,
288 Self::Chdir => 12,
289 Self::Getcwd => 344,
290 Self::Mmap => 197,
291 Self::Lseek => 199,
292 Self::Pread => 153,
293 Self::Pwrite => 154,
294 Self::Ftruncate => 201,
295 Self::Sysctl => 202,
296 Self::ClockGettime => 266,
297 Self::Sysctlbyname => 274,
298 Self::Issetugid => 327,
299 Self::Stat => 338,
301 Self::Fstat => 339,
302 Self::Lstat => 340,
303 Self::BsdthreadCreate => 360,
304 Self::BsdthreadTerminate => 361,
305 Self::BsdthreadRegister => 366,
306 Self::ThreadSelfid => 372,
307 Self::Openat => 463,
308 Self::Fstatat => 470,
309 Self::Pipe => 42,
310 Self::Accept => 30,
311 Self::Recvmsg => 27,
312 Self::Sendmsg => 28,
313 Self::Recvfrom | Self::Recv => 29,
314 Self::Socket => 97,
315 Self::Socketpair => 135,
316 Self::Connect => 98,
317 Self::Bind => 104,
318 Self::Setsockopt => 105,
319 Self::Listen => 106,
320 Self::Getsockopt => 118,
321 Self::Sendto | Self::Send => 133,
322 Self::Shutdown => 134,
323 Self::Select => 93,
324 Self::Poll => 230,
325 Self::Getpeername => 31,
326 Self::Getsockname => 32,
327 }
328 }
329}
330
331#[must_use]
333pub const fn lookup(number: u32) -> Option<BsdSyscall> {
334 match number {
335 1 => Some(BsdSyscall::Exit),
336 2 => Some(BsdSyscall::Fork),
337 3 | 396 => Some(BsdSyscall::Read),
338 4 | 397 => Some(BsdSyscall::Write),
339 5 | 398 => Some(BsdSyscall::Open),
340 6 | 399 => Some(BsdSyscall::Close),
341 7 | 400 => Some(BsdSyscall::Wait4), 9 => Some(BsdSyscall::Link),
343 10 | 401 => Some(BsdSyscall::Unlink),
344 12 => Some(BsdSyscall::Chdir),
345 20 => Some(BsdSyscall::Getpid),
346 24 => Some(BsdSyscall::Getuid),
347 25 => Some(BsdSyscall::Geteuid),
348 33 => Some(BsdSyscall::Access),
349 37 => Some(BsdSyscall::Kill),
350 39 => Some(BsdSyscall::Getppid),
351 43 => Some(BsdSyscall::Getegid),
352 47 => Some(BsdSyscall::Getgid),
353 81 => Some(BsdSyscall::Getpgrp),
354 82 => Some(BsdSyscall::Setpgid),
355 147 => Some(BsdSyscall::Setsid),
356 27 | 402 => Some(BsdSyscall::Recvmsg), 28 | 408 => Some(BsdSyscall::Sendmsg), 29 | 403 => Some(BsdSyscall::Recvfrom), 30 | 404 => Some(BsdSyscall::Accept), 31 => Some(BsdSyscall::Getpeername),
361 32 => Some(BsdSyscall::Getsockname),
362 41 => Some(BsdSyscall::Dup),
363 42 | 405 => Some(BsdSyscall::Pipe), 46 => Some(BsdSyscall::Sigaction),
365 48 => Some(BsdSyscall::Sigprocmask),
366 57 => Some(BsdSyscall::Symlink),
367 58 => Some(BsdSyscall::Readlink),
368 59 => Some(BsdSyscall::Execve),
369 65 => Some(BsdSyscall::Msync),
370 66 => Some(BsdSyscall::Vfork),
371 73 => Some(BsdSyscall::Munmap),
372 74 => Some(BsdSyscall::Mprotect),
373 90 => Some(BsdSyscall::Dup2),
374 92 => Some(BsdSyscall::Fcntl),
375 93 | 394 => Some(BsdSyscall::Select), 95 | 406 => Some(BsdSyscall::Fsync),
377 97 => Some(BsdSyscall::Socket),
378 135 => Some(BsdSyscall::Socketpair),
379 98 => Some(BsdSyscall::Connect),
380 104 => Some(BsdSyscall::Bind),
381 105 => Some(BsdSyscall::Setsockopt),
382 106 => Some(BsdSyscall::Listen),
383 116 => Some(BsdSyscall::Gettimeofday),
384 118 => Some(BsdSyscall::Getsockopt),
385 128 => Some(BsdSyscall::Rename),
386 153 | 474 => Some(BsdSyscall::Pread), 154 | 475 => Some(BsdSyscall::Pwrite), 133 | 407 => Some(BsdSyscall::Sendto), 134 => Some(BsdSyscall::Shutdown),
390 136 => Some(BsdSyscall::Mkdir),
391 137 => Some(BsdSyscall::Rmdir),
392 188 | 338 => Some(BsdSyscall::Stat), 189 | 339 => Some(BsdSyscall::Fstat), 190 | 340 => Some(BsdSyscall::Lstat), 197 => Some(BsdSyscall::Mmap),
396 199 => Some(BsdSyscall::Lseek),
397 201 => Some(BsdSyscall::Ftruncate),
398 202 => Some(BsdSyscall::Sysctl),
399 230 => Some(BsdSyscall::Poll),
400 266 => Some(BsdSyscall::ClockGettime),
401 274 => Some(BsdSyscall::Sysctlbyname),
402 327 => Some(BsdSyscall::Issetugid),
403 344 => Some(BsdSyscall::Getcwd),
404 360 => Some(BsdSyscall::BsdthreadCreate),
405 361 => Some(BsdSyscall::BsdthreadTerminate),
406 366 => Some(BsdSyscall::BsdthreadRegister),
407 372 => Some(BsdSyscall::ThreadSelfid),
408 463 => Some(BsdSyscall::Openat),
409 470 | 471 => Some(BsdSyscall::Fstatat), _ => None,
411 }
412}
413
414#[must_use]
416pub const fn name_of(number: u32) -> Option<&'static str> {
417 match lookup(number) {
418 Some(s) => Some(s.name()),
419 None => None,
420 }
421}
422
423#[must_use]
425pub fn known_syscalls() -> &'static [(u32, &'static str)] {
426 &[
427 (1, "exit"),
428 (2, "fork"),
429 (3, "read"),
430 (4, "write"),
431 (5, "open"),
432 (6, "close"),
433 (7, "wait4"),
434 (20, "getpid"),
435 (59, "execve"),
436 (66, "vfork"),
437 (90, "dup2"),
438 (24, "getuid"),
439 (25, "geteuid"),
440 (33, "access"),
441 (39, "getppid"),
442 (43, "getegid"),
443 (47, "getgid"),
444 (41, "dup"),
445 (46, "sigaction"),
446 (48, "sigprocmask"),
447 (57, "symlink"),
448 (58, "readlink"),
449 (65, "msync"),
450 (73, "munmap"),
451 (74, "mprotect"),
452 (92, "fcntl"),
453 (116, "gettimeofday"),
454 (197, "mmap"),
455 (153, "pread"),
456 (154, "pwrite"),
457 (199, "lseek"),
458 (202, "sysctl"),
459 (266, "clock_gettime"),
460 (274, "sysctlbyname"),
461 (327, "issetugid"),
462 (9, "link"),
463 (10, "unlink"),
464 (95, "fsync"),
465 (128, "rename"),
466 (12, "chdir"),
467 (136, "mkdir"),
468 (137, "rmdir"),
469 (344, "__getcwd"),
470 (201, "ftruncate"),
471 (338, "stat"),
472 (339, "fstat"),
473 (340, "lstat"),
474 (463, "openat"),
475 (470, "fstatat"),
476 (360, "bsdthread_create"),
477 (361, "bsdthread_terminate"),
478 (366, "bsdthread_register"),
479 (372, "thread_selfid"),
480 (463, "openat"),
481 (42, "pipe"),
482 (97, "socket"),
483 (98, "connect"),
484 (104, "bind"),
485 (105, "setsockopt"),
486 (106, "listen"),
487 (118, "getsockopt"),
488 (133, "sendto"),
489 (134, "shutdown"),
490 (29, "recvfrom"),
491 (30, "accept"),
492 (93, "select"),
493 (230, "poll"),
494 (31, "getpeername"),
495 (32, "getsockname"),
496 ]
497}
498
499#[cfg(test)]
500mod tests {
501 use super::*;
502
503 #[test]
504 fn snapshot_table_consistent() {
505 for &(num, name) in known_syscalls() {
506 assert_eq!(name_of(num), Some(name));
507 assert_eq!(lookup(num).map(BsdSyscall::name), Some(name));
508 }
509 }
510
511 #[test]
512 fn nocancel_and_stat_aliases() {
513 assert_eq!(lookup(396), Some(BsdSyscall::Read));
514 assert_eq!(lookup(397), Some(BsdSyscall::Write));
515 assert_eq!(lookup(398), Some(BsdSyscall::Open));
516 assert_eq!(lookup(399), Some(BsdSyscall::Close));
517 assert_eq!(lookup(188), Some(BsdSyscall::Stat));
518 assert_eq!(lookup(189), Some(BsdSyscall::Fstat));
519 }
520}