signal.odin 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #+build linux, darwin, netbsd, openbsd, freebsd
  2. package posix
  3. import "base:intrinsics"
  4. import "core:c"
  5. when ODIN_OS == .Darwin {
  6. foreign import lib "system:System.framework"
  7. } else {
  8. foreign import lib "system:c"
  9. }
  10. // signal.h - signals
  11. foreign lib {
  12. /*
  13. Raise a signal to the process/group specified by pid.
  14. If sig is 0, this function can be used to check if the pid is just checked for validity.
  15. If pid is -1, the signal is sent to all processes that the current process has permission to send.
  16. If pid is negative (not -1), the signal is sent to all processes in the group identifier by the
  17. absolute value.
  18. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/kill.html ]]
  19. */
  20. kill :: proc(pid: pid_t, sig: Signal) -> result ---
  21. /*
  22. Shorthand for `kill(-pgrp, sig)` which will kill all processes in the given process group.
  23. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/killpg.html ]]
  24. */
  25. killpg :: proc(pgrp: pid_t, sig: Signal) -> result ---
  26. /*
  27. Writes a language-dependent message to stderror.
  28. Example:
  29. posix.psignal(.SIGSEGV, "that didn't go well")
  30. Possible Output:
  31. that didn't go well: Segmentation fault
  32. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/psignal.html ]]
  33. */
  34. psignal :: proc(signum: Signal, message: cstring) ---
  35. /*
  36. Send a signal to a thread.
  37. As with kill, if sig is 0, only validation (of the pthread_t given) is done and no signal is sent.
  38. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html ]]
  39. */
  40. pthread_kill :: proc(thread: pthread_t, sig: Signal) -> Errno ---
  41. /*
  42. Examine and change blocked signals.
  43. Equivalent to sigprocmask(), without the restriction that the call be made in a single-threaded process.
  44. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_sigmask.html ]]
  45. */
  46. pthread_sigmask :: proc(how: Sig, set: ^sigset_t, oset: ^sigset_t) -> Errno ---
  47. /*
  48. Examine and change blocked signals in a single-threaded process.
  49. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_sigmask.html ]]
  50. */
  51. @(link_name=LSIGPROCMASK)
  52. sigprocmask :: proc(how: Sig, set: ^sigset_t, oldset: ^sigset_t) -> result ---
  53. /*
  54. Examine and change a signal action.
  55. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html ]]
  56. */
  57. @(link_name=LSIGACTION)
  58. sigaction :: proc(sig: Signal, act: ^sigaction_t, oact: ^sigaction_t) -> result ---
  59. @(link_name=LSIGADDSET)
  60. sigaddset :: proc(set: ^sigset_t, signo: Signal) -> result ---
  61. @(link_name=LSIGDELSET)
  62. sigdelset :: proc(^sigset_t, Signal) -> c.int ---
  63. @(link_name=LSIGEMPTYSET)
  64. sigemptyset :: proc(^sigset_t) -> c.int ---
  65. @(link_name=LSIGFILLSET)
  66. sigfillset :: proc(^sigset_t) -> c.int ---
  67. /*
  68. Set and get the signal alternate stack context.
  69. Example:
  70. sigstk := posix.stack_t {
  71. ss_sp = make([^]byte, posix.SIGSTKSZ) or_else panic("allocation failure"),
  72. ss_size = posix.SIGSTKSZ,
  73. ss_flags = {},
  74. }
  75. if posix.sigaltstack(&sigstk, nil) != .OK {
  76. fmt.panicf("sigaltstack failure: %v", posix.strerror(posix.errno()))
  77. }
  78. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaltstack.html ]]
  79. */
  80. @(link_name=LSIGALTSTACK)
  81. sigaltstack :: proc(ss: ^stack_t, oss: ^stack_t) -> result ---
  82. /*
  83. Adds sig to the signal mask of the calling process.
  84. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html ]]
  85. */
  86. sighold :: proc(sig: Signal) -> result ---
  87. /*
  88. Sets the disposition of sig to SIG_IGN.
  89. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html ]]
  90. */
  91. sigignore :: proc(sig: Signal) -> result ---
  92. /*
  93. Removes sig from the signal mask of the calling process and suspend the calling process until
  94. a signal is received.
  95. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html ]]
  96. */
  97. sigpause :: proc(sig: Signal) -> result ---
  98. /*
  99. Removes sig from the signal mask of the calling process.
  100. Returns: always -1.
  101. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html ]]
  102. */
  103. sigrelse :: proc(sig: Signal) -> result ---
  104. /*
  105. Changes the restart behavior when a function is interrupted by the specified signal.
  106. If flag is true, SA_RESTART is removed, added otherwise.
  107. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/siginterrupt.html ]]
  108. */
  109. siginterrupt :: proc(sig: Signal, flag: b32) -> result ---
  110. /*
  111. Test for a signal in a signal set.
  112. Returns: 1 if it is a member, 0 if not, -1 (setting errno) on failure
  113. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigismember.html ]]
  114. */
  115. @(link_name=LSIGISMEMBER)
  116. sigismember :: proc(set: ^sigset_t, signo: Signal) -> c.int ---
  117. /*
  118. Stores the set of signals that are blocked from delivery to the calling thread and that are pending
  119. on the process or the calling thread.
  120. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigpending.html ]]
  121. */
  122. @(link_name=LSIGPENDING)
  123. sigpending :: proc(set: ^sigset_t) -> result ---
  124. /*
  125. Wait for one of the given signals.
  126. Returns: always -1
  127. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigsuspend.html ]]
  128. */
  129. @(link_name=LSIGSUSPEND)
  130. sigsuspend :: proc(sigmask: ^sigset_t) -> result ---
  131. /*
  132. Wait for queued signals.
  133. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigwait.html ]]
  134. */
  135. sigwait :: proc(set: ^sigset_t, sig: ^Signal) -> Errno ---
  136. /* NOTE: unimplemented on darwin.
  137. void psiginfo(const siginfo_t *, const char *);
  138. int sigqueue(pid_t, int, union sigval);
  139. void (*sigset(int, void (*)(int)))(int);
  140. int sigsuspend(const sigset_t *);
  141. int sigtimedwait(const sigset_t *restrict, siginfo_t *restrict,
  142. const struct timespec *restrict);
  143. int sigwaitinfo(const sigset_t *restrict, siginfo_t *restrict);
  144. */
  145. }
  146. sigval :: struct #raw_union {
  147. sigval_int: c.int, /* [PSX] integer signal value */
  148. sigval_ptr: rawptr, /* [PSX] pointer signal value */
  149. }
  150. ILL_Code :: enum c.int {
  151. // Illegal opcode.
  152. ILLOPC = ILL_ILLOPC,
  153. // Illegal operand.
  154. ILLOPN = ILL_ILLOPN,
  155. // Illegal addressing mode.
  156. ILLADR = ILL_ILLADR,
  157. // Illegal trap.
  158. ILLTRP = ILL_ILLTRP,
  159. // Priviledged opcode.
  160. PRVOPC = ILL_PRVOPC,
  161. // Priviledged register.
  162. PRVREG = ILL_PRVREG,
  163. // Coprocessor error.
  164. COPROC = ILL_COPROC,
  165. // Internal stack error.
  166. BADSTK = ILL_BADSTK,
  167. }
  168. FPE_Code :: enum c.int {
  169. // Integer divide by zero.
  170. INTDIV = FPE_INTDIV,
  171. // Integer overflow.
  172. INTOVF = FPE_INTOVF,
  173. // Floating-point divide by zero.
  174. FLTDIV = FPE_FLTDIV,
  175. // Floating-point overflow.
  176. FLTOVF = FPE_FLTOVF,
  177. // Floating-point underflow.
  178. FLTUND = FPE_FLTUND,
  179. // Floating-point inexact result.
  180. FLTRES = FPE_FLTRES,
  181. // Invalid floating-point operation.
  182. FLTINV = FPE_FLTINV,
  183. // Subscript out of range.
  184. FLTSUB = FPE_FLTSUB,
  185. }
  186. SEGV_Code :: enum c.int {
  187. // Address not mapped to object.
  188. MAPERR = SEGV_MAPERR,
  189. // Invalid permissions for mapped object.
  190. ACCERR = SEGV_ACCERR,
  191. }
  192. BUS_Code :: enum c.int {
  193. // Invalid address alignment.
  194. ADRALN = BUS_ADRALN,
  195. // Nonexistent physical address.
  196. ADRERR = BUS_ADRERR,
  197. // Object-specific hardware error.
  198. OBJERR = BUS_OBJERR,
  199. }
  200. TRAP_Code :: enum c.int {
  201. // Process breakpoint.
  202. BRKPT = TRAP_BRKPT,
  203. // Process trace trap.
  204. TRACE = TRAP_TRACE,
  205. }
  206. CLD_Code :: enum c.int {
  207. // Child has exited..
  208. EXITED = CLD_EXITED,
  209. // Child has terminated abnormally and did not create a core file.
  210. KILLED = CLD_KILLED,
  211. // Child has terminated abnormally and created a core file.
  212. DUMPED = CLD_DUMPED,
  213. // Traced child trapped.
  214. TRAPPED = CLD_TRAPPED,
  215. // Child has stopped.
  216. STOPPED = CLD_STOPPED,
  217. // Stopped child has continued.
  218. CONTINUED = CLD_CONTINUED,
  219. }
  220. POLL_Code :: enum c.int {
  221. // Data input is available.
  222. IN = POLL_IN,
  223. // Output buffers available.
  224. OUT = POLL_OUT,
  225. // Input message available.
  226. MSG = POLL_MSG,
  227. // I/O error.
  228. ERR = POLL_ERR,
  229. // High priority input available.
  230. PRI = POLL_PRI,
  231. // Device disconnected.
  232. HUP = POLL_HUP,
  233. }
  234. Any_Code :: enum c.int {
  235. // Signal sent by kill().
  236. USER = SI_USER,
  237. // Signal sent by sigqueue().
  238. QUEUE = SI_QUEUE,
  239. // Signal generated by expiration of a timer set by timer_settime().
  240. TIMER = SI_TIMER,
  241. // Signal generated by completion of an asynchronous I/O request.
  242. ASYNCIO = SI_ASYNCIO,
  243. // Signal generated by arrival of a message on an empty message queue.
  244. MESGQ = SI_MESGQ,
  245. }
  246. SA_Flags_Bits :: enum c.int {
  247. // Do not generate SIGCHLD when children stop or stopped children continue.
  248. NOCLDSTOP = log2(SA_NOCLDSTOP),
  249. // Cause signal delivery to occur on an alternate stack.
  250. ONSTACK = log2(SA_ONSTACK),
  251. // Cause signal disposition to be set to SIG_DFL on entry to signal handlers.
  252. RESETHAND = log2(SA_RESETHAND),
  253. // Cause certain functions to become restartable.
  254. RESTART = log2(SA_RESTART),
  255. // Cause extra information to be passed to signal handlers at the time of receipt of a signal.
  256. SIGINFO = log2(SA_SIGINFO),
  257. // Cause implementation not to create zombie processes or status information on child termination.
  258. NOCLDWAIT = log2(SA_NOCLDWAIT),
  259. // Cause signal not to be automatically blocked on entry to signal handler.
  260. SA_NODEFER = log2(SA_NODEFER),
  261. }
  262. SA_Flags :: bit_set[SA_Flags_Bits; c.int]
  263. SS_Flag_Bits :: enum c.int {
  264. // Process is executing on an alternate signal stack.
  265. ONSTACK = log2(SS_ONSTACK),
  266. // Alternate signal stack is disabled.
  267. DISABLE = log2(SS_DISABLE),
  268. }
  269. SS_Flags :: bit_set[SS_Flag_Bits; c.int]
  270. Sig :: enum c.int {
  271. // Resulting set is the union of the current set and the signal set and the complement of
  272. // the signal set pointed to by the argument.
  273. BLOCK = SIG_BLOCK,
  274. // Resulting set is the intersection of the current set and the complement of the signal set
  275. // pointed to by the argument.
  276. UNBLOCK = SIG_UNBLOCK,
  277. // Resulting set is the signal set pointed to by the argument.
  278. SETMASK = SIG_SETMASK,
  279. }
  280. when ODIN_OS == .NetBSD {
  281. @(private) LSIGPROCMASK :: "__sigprocmask14"
  282. @(private) LSIGACTION :: "__sigaction_siginfo"
  283. @(private) LSIGADDSET :: "__sigaddset14"
  284. @(private) LSIGDELSET :: "__sigdelset14"
  285. @(private) LSIGEMPTYSET :: "__sigemptyset14"
  286. @(private) LSIGFILLSET :: "__sigfillset14"
  287. @(private) LSIGALTSTACK :: "__sigaltstack14"
  288. @(private) LSIGISMEMBER :: "__sigismember14"
  289. @(private) LSIGPENDING :: "__sigpending14"
  290. @(private) LSIGSUSPEND :: "__sigsuspend14"
  291. } else {
  292. @(private) LSIGPROCMASK :: "sigprocmask"
  293. @(private) LSIGACTION :: "sigaction"
  294. @(private) LSIGADDSET :: "sigaddset"
  295. @(private) LSIGDELSET :: "sigdelset"
  296. @(private) LSIGEMPTYSET :: "sigemptyset"
  297. @(private) LSIGFILLSET :: "sigfillset"
  298. @(private) LSIGALTSTACK :: "sigaltstack"
  299. @(private) LSIGISMEMBER :: "sigismember"
  300. @(private) LSIGPENDING :: "sigpending"
  301. @(private) LSIGSUSPEND :: "sigsuspend"
  302. }
  303. when ODIN_OS == .Darwin {
  304. // Request that signal be held
  305. SIG_HOLD :: rawptr(uintptr(5))
  306. uid_t :: distinct c.uint32_t
  307. sigset_t :: distinct c.uint32_t
  308. SIGHUP :: 1
  309. SIGQUIT :: 3
  310. SIGTRAP :: 5
  311. SIGPOLL :: 7
  312. SIGKILL :: 9
  313. SIGBUS :: 10
  314. SIGSYS :: 12
  315. SIGPIPE :: 13
  316. SIGALRM :: 14
  317. SIGURG :: 16
  318. SIGCONT :: 19
  319. SIGSTOP :: 17
  320. SIGTSTP :: 18
  321. SIGCHLD :: 20
  322. SIGTTIN :: 21
  323. SIGTTOU :: 22
  324. SIGXCPU :: 24
  325. SIGXFSZ :: 25
  326. SIGVTALRM :: 26
  327. SIGPROF :: 27
  328. SIGUSR1 :: 30
  329. SIGUSR2 :: 31
  330. // NOTE: this is actually defined as `sigaction`, but due to the function with the same name
  331. // `_t` has been added.
  332. sigaction_t :: struct {
  333. using _: struct #raw_union {
  334. sa_handler: proc "c" (Signal), /* [PSX] signal-catching function or one of the SIG_IGN or SIG_DFL */
  335. sa_sigaction: proc "c" (Signal, ^siginfo_t, rawptr), /* [PSX] signal-catching function */
  336. },
  337. sa_mask: sigset_t, /* [PSX] set of signals to be blocked during execution of the signal handling function */
  338. sa_flags: SA_Flags, /* [PSX] special flags */
  339. }
  340. SIG_BLOCK :: 1
  341. SIG_UNBLOCK :: 2
  342. SIG_SETMASK :: 3
  343. SA_NOCLDSTOP :: 0x0008
  344. SA_ONSTACK :: 0x0001
  345. SA_RESETHAND :: 0x0004
  346. SA_RESTART :: 0x0002
  347. SA_SIGINFO :: 0x0040
  348. SA_NOCLDWAIT :: 0x0020
  349. SA_NODEFER :: 0x0010
  350. SS_ONSTACK :: 0x0001
  351. SS_DISABLE :: 0x0004
  352. MINSIGSTKSZ :: 32768
  353. SIGSTKSZ :: 131072
  354. stack_t :: struct {
  355. ss_sp: rawptr, /* [PSX] stack base or pointer */
  356. ss_size: c.size_t, /* [PSX] stack size */
  357. ss_flags: SS_Flags, /* [PSX] flags */
  358. }
  359. siginfo_t :: struct {
  360. si_signo: Signal, /* [PSX] signal number */
  361. si_errno: Errno, /* [PSX] errno value associated with this signal */
  362. si_code: struct #raw_union { /* [PSX] specific more detailed codes per signal */
  363. ill: ILL_Code,
  364. fpe: FPE_Code,
  365. segv: SEGV_Code,
  366. bus: BUS_Code,
  367. trap: TRAP_Code,
  368. chld: CLD_Code,
  369. poll: POLL_Code,
  370. any: Any_Code,
  371. },
  372. si_pid: pid_t, /* [PSX] sending process ID */
  373. si_uid: uid_t, /* [PSX] real user ID of sending process */
  374. si_status: c.int, /* [PSX] exit value or signal */
  375. si_addr: rawptr, /* [PSX] address of faulting instruction */
  376. si_value: sigval, /* [PSX] signal value */
  377. si_band: c.long, /* [PSX] band event for SIGPOLL */
  378. __pad: [7]c.ulong,
  379. }
  380. ILL_ILLOPC :: 1
  381. ILL_ILLOPN :: 4
  382. ILL_ILLADR :: 5
  383. ILL_ILLTRP :: 2
  384. ILL_PRVOPC :: 3
  385. ILL_PRVREG :: 6
  386. ILL_COPROC :: 7
  387. ILL_BADSTK :: 8
  388. FPE_INTDIV :: 7
  389. FPE_INTOVF :: 8
  390. FPE_FLTDIV :: 1
  391. FPE_FLTOVF :: 2
  392. FPE_FLTUND :: 3
  393. FPE_FLTRES :: 4
  394. FPE_FLTINV :: 5
  395. FPE_FLTSUB :: 6
  396. SEGV_MAPERR :: 1
  397. SEGV_ACCERR :: 2
  398. BUS_ADRALN :: 1
  399. BUS_ADRERR :: 2
  400. BUS_OBJERR :: 3
  401. TRAP_BRKPT :: 1
  402. TRAP_TRACE :: 2
  403. CLD_EXITED :: 1
  404. CLD_KILLED :: 2
  405. CLD_DUMPED :: 3
  406. CLD_TRAPPED :: 4
  407. CLD_STOPPED :: 5
  408. CLD_CONTINUED :: 6
  409. POLL_IN :: 1
  410. POLL_OUT :: 2
  411. POLL_MSG :: 3
  412. POLL_ERR :: 4
  413. POLL_PRI :: 5
  414. POLL_HUP :: 6
  415. SI_USER :: 0x10001
  416. SI_QUEUE :: 0x10002
  417. SI_TIMER :: 0x10003
  418. SI_ASYNCIO :: 0x10004
  419. SI_MESGQ :: 0x10005
  420. } else when ODIN_OS == .FreeBSD {
  421. // Request that signal be held
  422. SIG_HOLD :: rawptr(uintptr(3))
  423. uid_t :: distinct c.uint32_t
  424. sigset_t :: struct {
  425. __bits: [4]c.uint32_t,
  426. }
  427. SIGHUP :: 1
  428. SIGQUIT :: 3
  429. SIGTRAP :: 5
  430. SIGPOLL :: 7
  431. SIGKILL :: 9
  432. SIGBUS :: 10
  433. SIGSYS :: 12
  434. SIGPIPE :: 13
  435. SIGALRM :: 14
  436. SIGURG :: 16
  437. SIGCONT :: 19
  438. SIGSTOP :: 17
  439. SIGTSTP :: 18
  440. SIGCHLD :: 20
  441. SIGTTIN :: 21
  442. SIGTTOU :: 22
  443. SIGXCPU :: 24
  444. SIGXFSZ :: 25
  445. SIGVTALRM :: 26
  446. SIGPROF :: 27
  447. SIGUSR1 :: 30
  448. SIGUSR2 :: 31
  449. // NOTE: this is actually defined as `sigaction`, but due to the function with the same name
  450. // `_t` has been added.
  451. sigaction_t :: struct {
  452. using _: struct #raw_union {
  453. sa_handler: proc "c" (Signal), /* [PSX] signal-catching function or one of the SIG_IGN or SIG_DFL */
  454. sa_sigaction: proc "c" (Signal, ^siginfo_t, rawptr), /* [PSX] signal-catching function */
  455. },
  456. sa_flags: SA_Flags, /* [PSX] special flags */
  457. sa_mask: sigset_t, /* [PSX] set of signals to be blocked during execution of the signal handling function */
  458. }
  459. SIG_BLOCK :: 1
  460. SIG_UNBLOCK :: 2
  461. SIG_SETMASK :: 3
  462. SA_NOCLDSTOP :: 0x0008
  463. SA_ONSTACK :: 0x0001
  464. SA_RESETHAND :: 0x0004
  465. SA_RESTART :: 0x0002
  466. SA_SIGINFO :: 0x0040
  467. SA_NOCLDWAIT :: 0x0020
  468. SA_NODEFER :: 0x0010
  469. SS_ONSTACK :: 0x0001
  470. SS_DISABLE :: 0x0004
  471. when ODIN_ARCH == .amd64 || ODIN_ARCH == .arm32 {
  472. MINSIGSTKSZ :: 1024 * 4
  473. } else when ODIN_ARCH == .amd64 || ODIN_ARCH == .i386 {
  474. MINSIGSTKSZ :: 512 * 4
  475. }
  476. SIGSTKSZ :: MINSIGSTKSZ + 32768
  477. stack_t :: struct {
  478. ss_sp: rawptr, /* [PSX] stack base or pointer */
  479. ss_size: c.size_t, /* [PSX] stack size */
  480. ss_flags: SS_Flags, /* [PSX] flags */
  481. }
  482. siginfo_t :: struct {
  483. si_signo: Signal, /* [PSX] signal number */
  484. si_errno: Errno, /* [PSX] errno value associated with this signal */
  485. si_code: struct #raw_union { /* [PSX] specific more detailed codes per signal */
  486. ill: ILL_Code,
  487. fpe: FPE_Code,
  488. segv: SEGV_Code,
  489. bus: BUS_Code,
  490. trap: TRAP_Code,
  491. chld: CLD_Code,
  492. poll: POLL_Code,
  493. any: Any_Code,
  494. },
  495. si_pid: pid_t, /* [PSX] sending process ID */
  496. si_uid: uid_t, /* [PSX] real user ID of sending process */
  497. si_status: c.int, /* [PSX] exit value or signal */
  498. si_addr: rawptr, /* [PSX] address of faulting instruction */
  499. si_value: sigval, /* [PSX] signal value */
  500. using _reason: struct #raw_union {
  501. _fault: struct {
  502. _trapno: c.int, /* machine specific trap code */
  503. },
  504. _timer: struct {
  505. _timerid: c.int,
  506. _overrun: c.int,
  507. },
  508. _mesgq: struct {
  509. _mqd: c.int,
  510. },
  511. using _poll: struct {
  512. si_band: c.long, /* [PSX] band event for SIGPOLL */
  513. },
  514. _capsicum: struct {
  515. _syscall: c.int, /* syscall number for signals delivered as a result of system calls denied by capsicum */
  516. },
  517. __spare__: struct {
  518. __spare1__: c.long,
  519. __spare2__: [7]c.int,
  520. },
  521. },
  522. }
  523. ILL_ILLOPC :: 1
  524. ILL_ILLOPN :: 2
  525. ILL_ILLADR :: 3
  526. ILL_ILLTRP :: 4
  527. ILL_PRVOPC :: 5
  528. ILL_PRVREG :: 6
  529. ILL_COPROC :: 7
  530. ILL_BADSTK :: 8
  531. FPE_INTDIV :: 2
  532. FPE_INTOVF :: 1
  533. FPE_FLTDIV :: 3
  534. FPE_FLTOVF :: 4
  535. FPE_FLTUND :: 5
  536. FPE_FLTRES :: 6
  537. FPE_FLTINV :: 7
  538. FPE_FLTSUB :: 8
  539. SEGV_MAPERR :: 1
  540. SEGV_ACCERR :: 2
  541. BUS_ADRALN :: 1
  542. BUS_ADRERR :: 2
  543. BUS_OBJERR :: 3
  544. TRAP_BRKPT :: 1
  545. TRAP_TRACE :: 2
  546. CLD_EXITED :: 1
  547. CLD_KILLED :: 2
  548. CLD_DUMPED :: 3
  549. CLD_TRAPPED :: 4
  550. CLD_STOPPED :: 5
  551. CLD_CONTINUED :: 6
  552. POLL_IN :: 1
  553. POLL_OUT :: 2
  554. POLL_MSG :: 3
  555. POLL_ERR :: 4
  556. POLL_PRI :: 5
  557. POLL_HUP :: 6
  558. SI_USER :: 0x10001
  559. SI_QUEUE :: 0x10002
  560. SI_TIMER :: 0x10003
  561. SI_ASYNCIO :: 0x10004
  562. SI_MESGQ :: 0x10005
  563. } else when ODIN_OS == .NetBSD {
  564. // Request that signal be held
  565. SIG_HOLD :: rawptr(uintptr(3))
  566. uid_t :: distinct c.uint32_t
  567. sigset_t :: struct {
  568. __bits: [4]c.uint32_t,
  569. }
  570. SIGHUP :: 1
  571. SIGQUIT :: 3
  572. SIGTRAP :: 5
  573. SIGPOLL :: 7
  574. SIGKILL :: 9
  575. SIGBUS :: 10
  576. SIGSYS :: 12
  577. SIGPIPE :: 13
  578. SIGALRM :: 14
  579. SIGURG :: 16
  580. SIGCONT :: 19
  581. SIGSTOP :: 17
  582. SIGTSTP :: 18
  583. SIGCHLD :: 20
  584. SIGTTIN :: 21
  585. SIGTTOU :: 22
  586. SIGXCPU :: 24
  587. SIGXFSZ :: 25
  588. SIGVTALRM :: 26
  589. SIGPROF :: 27
  590. SIGUSR1 :: 30
  591. SIGUSR2 :: 31
  592. // NOTE: this is actually defined as `sigaction`, but due to the function with the same name
  593. // `_t` has been added.
  594. sigaction_t :: struct {
  595. using _: struct #raw_union {
  596. sa_handler: proc "c" (Signal), /* [PSX] signal-catching function or one of the SIG_IGN or SIG_DFL */
  597. sa_sigaction: proc "c" (Signal, ^siginfo_t, rawptr), /* [PSX] signal-catching function */
  598. },
  599. sa_mask: sigset_t, /* [PSX] set of signals to be blocked during execution of the signal handling function */
  600. sa_flags: SA_Flags, /* [PSX] special flags */
  601. }
  602. SIG_BLOCK :: 1
  603. SIG_UNBLOCK :: 2
  604. SIG_SETMASK :: 3
  605. SA_NOCLDSTOP :: 0x0008
  606. SA_ONSTACK :: 0x0001
  607. SA_RESETHAND :: 0x0004
  608. SA_RESTART :: 0x0002
  609. SA_SIGINFO :: 0x0040
  610. SA_NOCLDWAIT :: 0x0020
  611. SA_NODEFER :: 0x0010
  612. SS_ONSTACK :: 0x0001
  613. SS_DISABLE :: 0x0004
  614. MINSIGSTKSZ :: 8192
  615. SIGSTKSZ :: MINSIGSTKSZ + 32768
  616. stack_t :: struct {
  617. ss_sp: rawptr, /* [PSX] stack base or pointer */
  618. ss_size: c.size_t, /* [PSX] stack size */
  619. ss_flags: SS_Flags, /* [PSX] flags */
  620. }
  621. @(private)
  622. lwpid_t :: c.int32_t
  623. siginfo_t :: struct #raw_union {
  624. si_pad: [128]byte,
  625. using _info: struct {
  626. si_signo: Signal, /* [PSX] signal number */
  627. si_code: struct #raw_union { /* [PSX] specific more detailed codes per signal */
  628. ill: ILL_Code,
  629. fpe: FPE_Code,
  630. segv: SEGV_Code,
  631. bus: BUS_Code,
  632. trap: TRAP_Code,
  633. chld: CLD_Code,
  634. poll: POLL_Code,
  635. any: Any_Code,
  636. },
  637. si_errno: Errno, /* [PSX] errno value associated with this signal */
  638. // #ifdef _LP64
  639. /* In _LP64 the union starts on an 8-byte boundary. */
  640. _pad: c.int,
  641. // #endif
  642. using _reason: struct #raw_union {
  643. using _rt: struct {
  644. _pid: pid_t,
  645. _uid: uid_t,
  646. si_value: sigval, /* [PSX] signal value */
  647. },
  648. using _child: struct {
  649. si_pid: pid_t, /* [PSX] sending process ID */
  650. si_uid: uid_t, /* [PSX] real user ID of sending process */
  651. si_status: c.int, /* [PSX] exit value or signal */
  652. _utime: clock_t,
  653. _stime: clock_t,
  654. },
  655. using _fault: struct {
  656. si_addr: rawptr, /* [PSX] address of faulting instruction */
  657. _trap: c.int,
  658. _trap2: c.int,
  659. _trap3: c.int,
  660. },
  661. using _poll: struct {
  662. si_band: c.long, /* [PSX] band event for SIGPOLL */
  663. _fd: FD,
  664. },
  665. _syscall: struct {
  666. _sysnum: c.int,
  667. _retval: [2]c.int,
  668. _error: c.int,
  669. _args: [8]c.uint64_t,
  670. },
  671. _ptrace_state: struct {
  672. _pe_report_event: c.int,
  673. _option: struct #raw_union {
  674. _pe_other_pid: pid_t,
  675. _pe_lwp: lwpid_t,
  676. },
  677. },
  678. },
  679. },
  680. }
  681. ILL_ILLOPC :: 1
  682. ILL_ILLOPN :: 2
  683. ILL_ILLADR :: 3
  684. ILL_ILLTRP :: 4
  685. ILL_PRVOPC :: 5
  686. ILL_PRVREG :: 6
  687. ILL_COPROC :: 7
  688. ILL_BADSTK :: 8
  689. FPE_INTDIV :: 1
  690. FPE_INTOVF :: 2
  691. FPE_FLTDIV :: 3
  692. FPE_FLTOVF :: 4
  693. FPE_FLTUND :: 5
  694. FPE_FLTRES :: 6
  695. FPE_FLTINV :: 7
  696. FPE_FLTSUB :: 8
  697. SEGV_MAPERR :: 1
  698. SEGV_ACCERR :: 2
  699. BUS_ADRALN :: 1
  700. BUS_ADRERR :: 2
  701. BUS_OBJERR :: 3
  702. TRAP_BRKPT :: 1
  703. TRAP_TRACE :: 2
  704. CLD_EXITED :: 1
  705. CLD_KILLED :: 2
  706. CLD_DUMPED :: 3
  707. CLD_TRAPPED :: 4
  708. CLD_STOPPED :: 5
  709. CLD_CONTINUED :: 6
  710. POLL_IN :: 1
  711. POLL_OUT :: 2
  712. POLL_MSG :: 3
  713. POLL_ERR :: 4
  714. POLL_PRI :: 5
  715. POLL_HUP :: 6
  716. SI_USER :: 0
  717. SI_QUEUE :: -1
  718. SI_TIMER :: -2
  719. SI_ASYNCIO :: -3
  720. SI_MESGQ :: -4
  721. } else when ODIN_OS == .OpenBSD {
  722. // Request that signal be held
  723. SIG_HOLD :: rawptr(uintptr(3))
  724. uid_t :: distinct c.uint32_t
  725. sigset_t :: distinct c.uint32_t
  726. SIGHUP :: 1
  727. SIGQUIT :: 3
  728. SIGTRAP :: 5
  729. SIGPOLL :: 7
  730. SIGKILL :: 9
  731. SIGBUS :: 10
  732. SIGSYS :: 12
  733. SIGPIPE :: 13
  734. SIGALRM :: 14
  735. SIGURG :: 16
  736. SIGCONT :: 19
  737. SIGSTOP :: 17
  738. SIGTSTP :: 18
  739. SIGCHLD :: 20
  740. SIGTTIN :: 21
  741. SIGTTOU :: 22
  742. SIGXCPU :: 24
  743. SIGXFSZ :: 25
  744. SIGVTALRM :: 26
  745. SIGPROF :: 27
  746. SIGUSR1 :: 30
  747. SIGUSR2 :: 31
  748. // NOTE: this is actually defined as `sigaction`, but due to the function with the same name
  749. // `_t` has been added.
  750. sigaction_t :: struct {
  751. using _: struct #raw_union {
  752. sa_handler: proc "c" (Signal), /* [PSX] signal-catching function or one of the SIG_IGN or SIG_DFL */
  753. sa_sigaction: proc "c" (Signal, ^siginfo_t, rawptr), /* [PSX] signal-catching function */
  754. },
  755. sa_mask: sigset_t, /* [PSX] set of signals to be blocked during execution of the signal handling function */
  756. sa_flags: SA_Flags, /* [PSX] special flags */
  757. }
  758. SIG_BLOCK :: 1
  759. SIG_UNBLOCK :: 2
  760. SIG_SETMASK :: 3
  761. SA_NOCLDSTOP :: 0x0008
  762. SA_ONSTACK :: 0x0001
  763. SA_RESETHAND :: 0x0004
  764. SA_RESTART :: 0x0002
  765. SA_SIGINFO :: 0x0040
  766. SA_NOCLDWAIT :: 0x0020
  767. SA_NODEFER :: 0x0010
  768. SS_ONSTACK :: 0x0001
  769. SS_DISABLE :: 0x0004
  770. MINSIGSTKSZ :: 3 << 12
  771. SIGSTKSZ :: MINSIGSTKSZ + (1 << 12) * 4
  772. stack_t :: struct {
  773. ss_sp: rawptr, /* [PSX] stack base or pointer */
  774. ss_size: c.size_t, /* [PSX] stack size */
  775. ss_flags: SS_Flags, /* [PSX] flags */
  776. }
  777. SI_MAXSZ :: 128
  778. SI_PAD :: (SI_MAXSZ / size_of(c.int)) - 3
  779. siginfo_t :: struct {
  780. si_signo: Signal, /* [PSX] signal number */
  781. si_code: struct #raw_union { /* [PSX] specific more detailed codes per signal */
  782. ill: ILL_Code,
  783. fpe: FPE_Code,
  784. segv: SEGV_Code,
  785. bus: BUS_Code,
  786. trap: TRAP_Code,
  787. chld: CLD_Code,
  788. poll: POLL_Code,
  789. any: Any_Code,
  790. },
  791. si_errno: Errno, /* [PSX] errno value associated with this signal */
  792. using _data: struct #raw_union {
  793. _pad: [SI_PAD]c.int,
  794. using _proc: struct {
  795. si_pid: pid_t, /* [PSX] sending process ID */
  796. si_uid: uid_t, /* [PSX] real user ID of sending process */
  797. using _pdata: struct #raw_union {
  798. using _kill: struct {
  799. si_value: sigval,
  800. },
  801. using _cld: struct {
  802. _utime: clock_t,
  803. _stime: clock_t,
  804. si_status: c.int,
  805. },
  806. },
  807. },
  808. using _fault: struct {
  809. si_addr: rawptr,
  810. _trapno: c.int,
  811. },
  812. using _file: struct {
  813. _fd: FD,
  814. si_band: c.long, /* [PSX] band event for SIGPOLL */
  815. },
  816. },
  817. }
  818. ILL_ILLOPC :: 1
  819. ILL_ILLOPN :: 2
  820. ILL_ILLADR :: 3
  821. ILL_ILLTRP :: 4
  822. ILL_PRVOPC :: 5
  823. ILL_PRVREG :: 6
  824. ILL_COPROC :: 7
  825. ILL_BADSTK :: 8
  826. FPE_INTDIV :: 1
  827. FPE_INTOVF :: 2
  828. FPE_FLTDIV :: 3
  829. FPE_FLTOVF :: 4
  830. FPE_FLTUND :: 5
  831. FPE_FLTRES :: 6
  832. FPE_FLTINV :: 7
  833. FPE_FLTSUB :: 8
  834. SEGV_MAPERR :: 1
  835. SEGV_ACCERR :: 2
  836. BUS_ADRALN :: 1
  837. BUS_ADRERR :: 2
  838. BUS_OBJERR :: 3
  839. TRAP_BRKPT :: 1
  840. TRAP_TRACE :: 2
  841. CLD_EXITED :: 1
  842. CLD_KILLED :: 2
  843. CLD_DUMPED :: 3
  844. CLD_TRAPPED :: 4
  845. CLD_STOPPED :: 5
  846. CLD_CONTINUED :: 6
  847. POLL_IN :: 1
  848. POLL_OUT :: 2
  849. POLL_MSG :: 3
  850. POLL_ERR :: 4
  851. POLL_PRI :: 5
  852. POLL_HUP :: 6
  853. SI_USER :: 0
  854. SI_QUEUE :: -2
  855. SI_TIMER :: -3
  856. SI_ASYNCIO :: -4 // NOTE: not implemented
  857. SI_MESGQ :: -5 // NOTE: not implemented
  858. } else when ODIN_OS == .Linux {
  859. // Request that signal be held
  860. SIG_HOLD :: rawptr(uintptr(2))
  861. uid_t :: distinct c.uint32_t
  862. sigset_t :: struct {
  863. __val: [1024/(8 * size_of(c.ulong))]c.ulong,
  864. }
  865. SIGHUP :: 1
  866. SIGQUIT :: 3
  867. SIGTRAP :: 5
  868. SIGBUS :: 7
  869. SIGKILL :: 9
  870. SIGUSR1 :: 10
  871. SIGUSR2 :: 12
  872. SIGPIPE :: 13
  873. SIGALRM :: 14
  874. SIGCHLD :: 17
  875. SIGCONT :: 18
  876. SIGSTOP :: 19
  877. SIGTSTP :: 20
  878. SIGTTIN :: 21
  879. SIGTTOU :: 22
  880. SIGURG :: 23
  881. SIGXCPU :: 24
  882. SIGXFSZ :: 25
  883. SIGVTALRM :: 26
  884. SIGPROF :: 27
  885. SIGPOLL :: 29
  886. SIGSYS :: 31
  887. // NOTE: this is actually defined as `sigaction`, but due to the function with the same name
  888. // `_t` has been added.
  889. sigaction_t :: struct {
  890. using _: struct #raw_union {
  891. sa_handler: proc "c" (Signal), /* [PSX] signal-catching function or one of the SIG_IGN or SIG_DFL */
  892. sa_sigaction: proc "c" (Signal, ^siginfo_t, rawptr), /* [PSX] signal-catching function */
  893. },
  894. sa_mask: sigset_t, /* [PSX] set of signals to be blocked during execution of the signal handling function */
  895. sa_flags: SA_Flags, /* [PSX] special flags */
  896. sa_restorer: proc "c" (),
  897. }
  898. SIG_BLOCK :: 0
  899. SIG_UNBLOCK :: 1
  900. SIG_SETMASK :: 2
  901. SA_NOCLDSTOP :: 1
  902. SA_NOCLDWAIT :: 2
  903. SA_SIGINFO :: 4
  904. SA_ONSTACK :: 0x08000000
  905. SA_RESTART :: 0x10000000
  906. SA_NODEFER :: 0x40000000
  907. SA_RESETHAND :: 0x80000000
  908. SS_ONSTACK :: 1
  909. SS_DISABLE :: 2
  910. when ODIN_ARCH == .arm64 {
  911. MINSIGSTKSZ :: 6144
  912. SIGSTKSZ :: 12288
  913. } else {
  914. MINSIGSTKSZ :: 2048
  915. SIGSTKSZ :: 8192
  916. }
  917. stack_t :: struct {
  918. ss_sp: rawptr, /* [PSX] stack base or pointer */
  919. ss_flags: SS_Flags, /* [PSX] flags */
  920. ss_size: c.size_t, /* [PSX] stack size */
  921. }
  922. @(private)
  923. __SI_MAX_SIZE :: 128
  924. when size_of(int) == 8 {
  925. @(private)
  926. _pad0 :: struct {
  927. _pad0: c.int,
  928. }
  929. @(private)
  930. __SI_PAD_SIZE :: (__SI_MAX_SIZE / size_of(c.int)) - 4
  931. } else {
  932. @(private)
  933. _pad0 :: struct {}
  934. @(private)
  935. __SI_PAD_SIZE :: (__SI_MAX_SIZE / size_of(c.int)) - 3
  936. }
  937. siginfo_t :: struct #align(8) {
  938. si_signo: Signal, /* [PSX] signal number */
  939. si_errno: Errno, /* [PSX] errno value associated with this signal */
  940. si_code: struct #raw_union { /* [PSX] specific more detailed codes per signal */
  941. ill: ILL_Code,
  942. fpe: FPE_Code,
  943. segv: SEGV_Code,
  944. bus: BUS_Code,
  945. trap: TRAP_Code,
  946. chld: CLD_Code,
  947. poll: POLL_Code,
  948. any: Any_Code,
  949. },
  950. __pad0: _pad0,
  951. using _sifields: struct #raw_union {
  952. _pad: [__SI_PAD_SIZE]c.int,
  953. using _: struct {
  954. si_pid: pid_t, /* [PSX] sending process ID */
  955. si_uid: uid_t, /* [PSX] real user ID of sending process */
  956. using _: struct #raw_union {
  957. si_status: c.int, /* [PSX] exit value or signal */
  958. si_value: sigval, /* [PSX] signal value */
  959. },
  960. },
  961. using _: struct {
  962. si_addr: rawptr, /* [PSX] address of faulting instruction */
  963. },
  964. using _: struct {
  965. si_band: c.long, /* [PSX] band event for SIGPOLL */
  966. },
  967. },
  968. }
  969. ILL_ILLOPC :: 1
  970. ILL_ILLOPN :: 2
  971. ILL_ILLADR :: 3
  972. ILL_ILLTRP :: 4
  973. ILL_PRVOPC :: 5
  974. ILL_PRVREG :: 6
  975. ILL_COPROC :: 7
  976. ILL_BADSTK :: 8
  977. FPE_INTDIV :: 1
  978. FPE_INTOVF :: 2
  979. FPE_FLTDIV :: 3
  980. FPE_FLTOVF :: 4
  981. FPE_FLTUND :: 5
  982. FPE_FLTRES :: 6
  983. FPE_FLTINV :: 7
  984. FPE_FLTSUB :: 8
  985. SEGV_MAPERR :: 1
  986. SEGV_ACCERR :: 2
  987. BUS_ADRALN :: 1
  988. BUS_ADRERR :: 2
  989. BUS_OBJERR :: 3
  990. TRAP_BRKPT :: 1
  991. TRAP_TRACE :: 2
  992. CLD_EXITED :: 1
  993. CLD_KILLED :: 2
  994. CLD_DUMPED :: 3
  995. CLD_TRAPPED :: 4
  996. CLD_STOPPED :: 5
  997. CLD_CONTINUED :: 6
  998. POLL_IN :: 1
  999. POLL_OUT :: 2
  1000. POLL_MSG :: 3
  1001. POLL_ERR :: 4
  1002. POLL_PRI :: 5
  1003. POLL_HUP :: 6
  1004. SI_USER :: 0
  1005. SI_QUEUE :: -1
  1006. SI_TIMER :: -2
  1007. SI_MESGQ :: -3
  1008. SI_ASYNCIO :: -4
  1009. }