sys.odin 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040
  1. #+no-instrumentation
  2. package linux
  3. import "base:intrinsics"
  4. /*
  5. Read data from file into the buffer
  6. Returns the number of bytes successfully read, which may be less than the size
  7. of the buffer even if the termination is successfull.
  8. Available since Linux 1.0.
  9. Before Linux 3.14, this operation is not atomic (i.e. not thread safe).
  10. */
  11. read :: proc "contextless" (fd: Fd, buf: []u8) -> (int, Errno) {
  12. ret := syscall(SYS_read, fd, raw_data(buf), len(buf))
  13. return errno_unwrap(ret, int)
  14. }
  15. /*
  16. Write the data from a buffer into the file
  17. Returns the number of bytes successfully written, which may be less than the size
  18. of the buffer, even if the termination is successfull
  19. When using direct I/O, error doesn't mean the write has failed. Partial data may
  20. have been written.
  21. If .Eintr is returned, the write operation has failed due to interrupt. You'll probably
  22. need to restart this syscall
  23. Available since Linux 1.0
  24. Before Linux 3.14 this operation is not atomic (i.e. not thread safe)
  25. */
  26. write :: proc "contextless" (fd: Fd, buf: []u8) -> (int, Errno) {
  27. ret := syscall(SYS_write, fd, raw_data(buf), len(buf))
  28. return errno_unwrap(ret, int)
  29. }
  30. /*
  31. Open file, get the file descriptor
  32. Available since Linux 1.0.
  33. On ARM64 available since Linux 2.6.16.
  34. */
  35. open :: proc "contextless" (name: cstring, flags: Open_Flags, mode: Mode = {}) -> (Fd, Errno) {
  36. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  37. ret := syscall(SYS_openat, AT_FDCWD, transmute(uintptr) name, transmute(u32) flags, transmute(u32) mode)
  38. return errno_unwrap(ret, Fd)
  39. } else {
  40. ret := syscall(SYS_open, transmute(uintptr) name, transmute(u32) flags, transmute(u32) mode)
  41. return errno_unwrap(ret, Fd)
  42. }
  43. }
  44. /*
  45. Close the file.
  46. Available since Linux 1.0.
  47. */
  48. close :: proc "contextless" (fd: Fd) -> (Errno) {
  49. ret := syscall(SYS_close, fd)
  50. return Errno(-ret)
  51. }
  52. /*
  53. Get file status.
  54. Returns information about the file in struct pointed to by `stat` parameter.
  55. Available since Linux 1.0
  56. For 32-bit systems a different syscall is used that became available since 2.4.
  57. Not available on ARM64.
  58. */
  59. stat :: proc "contextless" (filename: cstring, stat: ^Stat) -> (Errno) {
  60. when size_of(int) == 8 {
  61. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  62. ret := syscall(SYS_fstatat, AT_FDCWD, cast(rawptr) filename, stat, 0)
  63. return Errno(-ret)
  64. } else {
  65. ret := syscall(SYS_stat, cast(rawptr) filename, stat)
  66. return Errno(-ret)
  67. }
  68. } else {
  69. ret := syscall(SYS_stat64, cast(rawptr) filename, stat)
  70. return Errno(-ret)
  71. }
  72. }
  73. /*
  74. Get file status from file descriptor.
  75. Returns information about the file in struct pointed to by `stat` parameter.
  76. Available since Linux 1.0.
  77. For 32-bit systems a different syscall is used that became available since 2.4.
  78. */
  79. fstat :: proc "contextless" (fd: Fd, stat: ^Stat) -> (Errno) {
  80. when size_of(int) == 8 {
  81. ret := syscall(SYS_fstat, cast(i32) fd, stat)
  82. return Errno(-ret)
  83. } else {
  84. ret := syscall(SYS_fstat64, cast(i32) fd, stat)
  85. return Errno(-ret)
  86. }
  87. }
  88. /*
  89. Get information about the file that's potentially a symbolic link
  90. The information is returned in a struct pointed to by `stat` parameter.
  91. The difference with stat, fstat is that if the file is a symbolic link,
  92. stat and fstat will dereference the link. lstat doesn't dereference symlinks.
  93. Available since Linux 1.0.
  94. For 32-bit systems a different syscall is used that became available since 2.4.
  95. Not available on arm64.
  96. */
  97. lstat :: proc "contextless" (filename: cstring, stat: ^Stat) -> (Errno) {
  98. when size_of(int) == 8 {
  99. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  100. return fstatat(AT_FDCWD, filename, stat, {.SYMLINK_NOFOLLOW})
  101. } else {
  102. ret := syscall(SYS_lstat, cast(rawptr) filename, stat)
  103. return Errno(-ret)
  104. }
  105. } else {
  106. ret := syscall(SYS_lstat64, cast(rawptr) filename, stat)
  107. return Errno(-ret)
  108. }
  109. }
  110. /*
  111. Wait on event on a file descriptor.
  112. Available since Linux 2.2.
  113. */
  114. poll :: proc "contextless" (fds: []Poll_Fd, timeout: i32) -> (i32, Errno) {
  115. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  116. seconds := cast(uint) timeout / 1000
  117. nanoseconds := cast(uint) (timeout % 1000) * 1_000_000
  118. timeout_spec := Time_Spec{seconds, nanoseconds}
  119. ret := syscall(SYS_ppoll, raw_data(fds), len(fds), &timeout_spec, 0, 0)
  120. return errno_unwrap(ret, i32)
  121. } else {
  122. ret := syscall(SYS_poll, raw_data(fds), len(fds), timeout)
  123. return errno_unwrap(ret, i32)
  124. }
  125. }
  126. /*
  127. Seek the file stream to specified offset.
  128. Available since Linux 1.0.
  129. On 32-bit platforms available since Linux 1.2.
  130. */
  131. lseek :: proc "contextless" (fd: Fd, off: i64, whence: Seek_Whence) -> (i64, Errno) {
  132. when size_of(int) == 8 {
  133. ret := syscall(SYS_lseek, fd, off, whence)
  134. return errno_unwrap(ret, i64)
  135. } else {
  136. result: i64 = ---
  137. lo, hi := compat64_arg_pair(off)
  138. ret := syscall(SYS__llseek, fd, hi, lo, &result, whence)
  139. return result, Errno(-ret)
  140. }
  141. }
  142. /*
  143. Map files into memory.
  144. Available since Linux 1.0.
  145. On 32-bit platforms since Linux 1.0.
  146. */
  147. mmap :: proc "contextless" (addr: uintptr, size: uint, prot: Mem_Protection, flags: Map_Flags, fd: Fd = Fd(-1), offset: i64 = 0) -> (rawptr, Errno) {
  148. when size_of(int) == 8 {
  149. ret := syscall(SYS_mmap, addr, size, transmute(i32) prot, transmute(i32) flags, fd, offset)
  150. return errno_unwrap(ret, rawptr, uintptr)
  151. } else {
  152. ret := syscall(SYS_mmap2, addr, size, transmute(i32) prot, transmute(i32) flags, fd, cast(uintptr)(offset/4096))
  153. return errno_unwrap(ret, rawptr, uintptr)
  154. }
  155. }
  156. /*
  157. Protect memory region.
  158. Available since Linux 1.0.
  159. */
  160. mprotect :: proc "contextless" (addr: rawptr, size: uint, prot: Mem_Protection) -> (Errno) {
  161. ret := syscall(SYS_mprotect, addr, size, transmute(i32) prot)
  162. return Errno(-ret)
  163. }
  164. /*
  165. Unmap memory.
  166. Available since Linux 1.0.
  167. */
  168. munmap :: proc "contextless" (addr: rawptr, size: uint) -> (Errno) {
  169. ret := syscall(SYS_munmap, addr, size)
  170. return Errno(-ret)
  171. }
  172. /*
  173. Extend the data segment size until the address `addr`. Note: you may be
  174. familiar with sbrk(), which is not actually a syscall, so is not
  175. implemented here.
  176. Available since Linux 1.0.
  177. */
  178. brk :: proc "contextless" (addr: uintptr) -> (Errno) {
  179. ret := syscall(SYS_brk, addr)
  180. return Errno(-ret)
  181. }
  182. /*
  183. Returns from signal handlers on some archs.
  184. */
  185. rt_sigreturn :: proc "c" () -> ! {
  186. intrinsics.syscall(uintptr(SYS_rt_sigreturn))
  187. unreachable()
  188. }
  189. /*
  190. Alter an action taken by a process.
  191. */
  192. rt_sigaction :: proc "contextless" (sig: Signal, sigaction: ^Sig_Action($T), old_sigaction: ^Sig_Action) -> Errno {
  193. // NOTE(jason): It appears that the restorer is required for i386 and amd64
  194. when ODIN_ARCH == .i386 || ODIN_ARCH == .amd64 {
  195. sigaction.flags += {.RESTORER}
  196. }
  197. if sigaction != nil && sigaction.restorer == nil && .RESTORER in sigaction.flags {
  198. sigaction.restorer = rt_sigreturn
  199. }
  200. ret := syscall(SYS_rt_sigaction, sig, sigaction, old_sigaction, size_of(Sig_Set))
  201. return Errno(-ret)
  202. }
  203. /*
  204. Examime and alter blocked signals.
  205. Available since Linux 2.2.
  206. */
  207. rt_sigprocmask :: proc "contextless" (mask_kind: Sig_Mask_Kind, new_set: ^Sig_Set, old_set: ^Sig_Set) -> Errno {
  208. ret := syscall(SYS_rt_sigprocmask, mask_kind, new_set, old_set, size_of(Sig_Set))
  209. return Errno(-ret)
  210. }
  211. /*
  212. Control devices. The ioctl syscall is a bit special because
  213. its argument is usually a pointer to some driver-specific structure.
  214. The request value is device-specific. Consult your LibC implementation's
  215. ioctls.h file to learn more. The returned value of ioctl *may* be an error
  216. code value instead of a memory address depending on the request type.
  217. Available since Linux 1.0.
  218. */
  219. ioctl :: proc "contextless" (fd: Fd, request: u32, arg: uintptr) -> (uintptr) {
  220. ret := syscall(SYS_ioctl, fd, request, arg)
  221. return uintptr(ret)
  222. }
  223. /*
  224. Read the file at a specified offset.
  225. Note, it is not an error to return less bytes than requested.
  226. Available since Linux 2.2.
  227. */
  228. pread :: proc "contextless" (fd: Fd, buf: []u8, offset: i64) -> (int, Errno) {
  229. when ODIN_ARCH == .arm32 {
  230. ret := syscall(SYS_pread64, fd, raw_data(buf), len(buf), 0, compat64_arg_pair(offset))
  231. } else {
  232. ret := syscall(SYS_pread64, fd, raw_data(buf), len(buf), compat64_arg_pair(offset))
  233. }
  234. return errno_unwrap(ret, int)
  235. }
  236. /*
  237. Read the file at a specified offset.
  238. Note, it is not an error to return less bytes than requested.
  239. Available since Linux 2.2.
  240. */
  241. pwrite :: proc "contextless" (fd: Fd, buf: []u8, offset: i64) -> (int, Errno) {
  242. when ODIN_ARCH == .arm32 {
  243. ret := syscall(SYS_pwrite64, fd, raw_data(buf), len(buf), 0, compat64_arg_pair(offset))
  244. } else {
  245. ret := syscall(SYS_pwrite64, fd, raw_data(buf), len(buf), compat64_arg_pair(offset))
  246. }
  247. return errno_unwrap(ret, int)
  248. }
  249. /*
  250. Read the data from file into multiple buffers.
  251. Available since Linux 2.0.
  252. */
  253. readv :: proc "contextless" (fd: Fd, iov: []IO_Vec) -> (int, Errno) {
  254. ret := syscall(SYS_readv, fd, raw_data(iov), len(iov))
  255. return errno_unwrap(ret, int)
  256. }
  257. /*
  258. Write the data from multiple buffers into a file.
  259. Available since Linux 2.0.
  260. */
  261. writev :: proc "contextless" (fd: Fd, iov: []IO_Vec) -> (int, Errno) {
  262. ret := syscall(SYS_writev, fd, raw_data(iov), len(iov))
  263. return errno_unwrap(ret, int)
  264. }
  265. /*
  266. Check user permissions for a file.
  267. If Mode is F_OK, checks whether the file exists.
  268. Similarly, X_OK, W_OK, R_OK check if the file is executable, writeable, readable respectively.
  269. Available since Linux 1.0.
  270. For ARM64 available since Linux 2.6.16.
  271. */
  272. access :: proc "contextless" (name: cstring, mode: Mode = F_OK) -> (Errno) {
  273. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  274. ret := syscall(SYS_faccessat, AT_FDCWD, cast(rawptr) name, transmute(u32) mode)
  275. return Errno(-ret)
  276. } else {
  277. ret := syscall(SYS_access, cast(rawptr) name, transmute(u32) mode)
  278. return Errno(-ret)
  279. }
  280. }
  281. /*
  282. Create a pipe
  283. Available since Linux 2.6.27
  284. */
  285. pipe2 :: proc "contextless" (pipes: ^[2]Fd, flags: Open_Flags) -> (Errno) {
  286. ret := syscall(SYS_pipe2, pipes, transmute(u32) flags)
  287. return Errno(-ret)
  288. }
  289. /*
  290. Yield the processor.
  291. Available since Linux 2.0.
  292. */
  293. sched_yield :: proc "contextless" () {
  294. syscall(SYS_sched_yield)
  295. }
  296. /*
  297. Remap a virtual memory address.
  298. Available since Linux 2.0.
  299. */
  300. mremap :: proc "contextless" (old_addr: rawptr, old_size: uint, new_size: uint, flags: MRemap_Flags, new_addr: uintptr = 0) -> (rawptr, Errno) {
  301. if .FIXED in flags {
  302. ret := syscall(SYS_mremap, old_addr, old_size, new_size, transmute(i32) flags, new_addr)
  303. return errno_unwrap(ret, rawptr, rawptr)
  304. } else {
  305. ret := syscall(SYS_mremap, old_addr, old_size, new_size, transmute(i32) flags)
  306. return errno_unwrap(ret, rawptr, rawptr)
  307. }
  308. }
  309. /*
  310. Sync file with memory map.
  311. Available since Linux 2.0.
  312. */
  313. msync :: proc "contextless" (addr: rawptr, size: uint, flags: MSync_Flags) -> (Errno) {
  314. ret := syscall(SYS_msync, addr, size, transmute(i32) flags)
  315. return Errno(-ret)
  316. }
  317. /*
  318. Determine if pages are resident in memory.
  319. Available since Linux 2.4.
  320. */
  321. mincore :: proc "contextless" (addr: rawptr, size: uint, vec: []b8) -> (Errno) {
  322. ret := syscall(SYS_mincore, addr, size, raw_data(vec))
  323. return Errno(-ret)
  324. }
  325. /*
  326. Give advice about use of memory.
  327. Available since Linux 2.4.
  328. */
  329. madvise :: proc "contextless" (addr: rawptr, size: uint, advice: MAdvice) -> (Errno) {
  330. ret := syscall(SYS_madvise, addr, size, advice)
  331. return Errno(-ret)
  332. }
  333. /*
  334. Allocate a SystemV shared memory segment.
  335. Available since Linux 2.0.
  336. */
  337. shmget :: proc "contextless" (key: Key, size: uint, flags: IPC_Flags) -> (Key, Errno) {
  338. ret := syscall(SYS_shmget, key, size, transmute(i16) flags)
  339. return errno_unwrap(ret, Key)
  340. }
  341. /*
  342. SystemV shared memory segment operations.
  343. Available since Linux 2.0.
  344. */
  345. shmat :: proc "contextless" (key: Key, addr: rawptr, flags: IPC_Flags) -> (rawptr, Errno) {
  346. ret := syscall(SYS_shmat, key, addr, transmute(i16) flags)
  347. return errno_unwrap(ret, rawptr, uintptr)
  348. }
  349. shmctl_ds :: proc "contextless" (key: Key, cmd: IPC_Cmd, buf: ^Shmid_DS) -> (Errno) {
  350. ret := syscall(SYS_shmctl, key, cmd, buf)
  351. return Errno(-ret)
  352. }
  353. shmctl_info :: proc "contextless" (key: Key, cmd: IPC_Cmd, buf: ^Shm_Info) -> (int, Errno) {
  354. ret := syscall(SYS_shmctl, key, cmd, buf)
  355. return errno_unwrap(ret, int)
  356. }
  357. /*
  358. SystemV shared memory control.
  359. Available since Linux 2.0.
  360. */
  361. shmctl :: proc {shmctl_ds, shmctl_info}
  362. /*
  363. Allocate a new file descriptor that refers to the same file as the one provided.
  364. Available since Linux 1.0.
  365. */
  366. dup :: proc "contextless" (fd: Fd) -> (Fd, Errno) {
  367. ret := syscall(SYS_dup, fd)
  368. return errno_unwrap(ret, Fd)
  369. }
  370. /*
  371. Adjust an existing file descriptor to point to the same file as `old`.
  372. Available since Linux 1.0.
  373. On ARM64 available since Linux 2.6.27.
  374. */
  375. dup2 :: proc "contextless" (old: Fd, new: Fd) -> (Fd, Errno) {
  376. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  377. ret := syscall(SYS_dup3, old, new, 0)
  378. return errno_unwrap(ret, Fd)
  379. } else {
  380. ret := syscall(SYS_dup2, old, new)
  381. return errno_unwrap(ret, Fd)
  382. }
  383. }
  384. /*
  385. Wait until the next signal is delivered.
  386. Available since Linux 1.0.
  387. On ARM64 available since Linux 2.6.16.
  388. */
  389. pause :: proc "contextless" () {
  390. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  391. syscall(SYS_ppoll, 0, 0, 0, 0)
  392. } else {
  393. syscall(SYS_pause)
  394. }
  395. }
  396. /*
  397. High-resolution sleep.
  398. Available since Linux 2.0.
  399. */
  400. nanosleep :: proc "contextless" (req: ^Time_Spec, rem: ^Time_Spec) -> (Errno) {
  401. ret := syscall(SYS_nanosleep, req, rem)
  402. return Errno(-ret)
  403. }
  404. /*
  405. Get the value of an internal timer.
  406. Available since Linux 1.0.
  407. */
  408. getitimer :: proc "contextless" (which: ITimer_Which, cur: ^ITimer_Val) -> (Errno) {
  409. ret := syscall(SYS_getitimer, cur)
  410. return Errno(-ret)
  411. }
  412. /*
  413. Set an alarm clock for delivery of a signal.
  414. Available since Linux 1.0.
  415. */
  416. alarm :: proc "contextless" (seconds: u32) -> u32 {
  417. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  418. new := ITimer_Val { value = { seconds = cast(int) seconds } }
  419. old := ITimer_Val {}
  420. syscall(SYS_setitimer, ITimer_Which.REAL, &new, &old)
  421. return u32(old.value.seconds) + u32(new.value.microseconds)
  422. } else {
  423. return cast(u32) syscall(SYS_alarm, seconds)
  424. }
  425. }
  426. /*
  427. Set the value of an internal timer.
  428. Available since Linux 1.0.
  429. */
  430. setitimer :: proc "contextless" (which: ITimer_Which, new: ^ITimer_Val, old: ^ITimer_Val) -> (Errno) {
  431. ret := syscall(SYS_getitimer, new, old)
  432. return Errno(-ret)
  433. }
  434. /*
  435. Returns the thread group ID of the current process.
  436. Note that it doesn't return the pid, despite it's name.
  437. Available since Linux 1.0.
  438. */
  439. getpid :: proc "contextless" () -> Pid {
  440. return cast(Pid) syscall(SYS_getpid)
  441. }
  442. /*
  443. Transfer the data between file descriptors.
  444. Available since Linux 2.2.
  445. On 32-bit platforms available since Linux 2.6.
  446. */
  447. sendfile :: proc "contextless" (out_fd: Fd, in_fd: Fd, offset: ^i64, count: uint) -> (int, Errno) {
  448. when size_of(int) == 8 {
  449. ret := syscall(SYS_sendfile, out_fd, in_fd, offset, count)
  450. return errno_unwrap(ret, int)
  451. } else {
  452. ret := syscall(SYS_sendfile64, out_fd, in_fd, offset, count)
  453. return errno_unwrap(ret, int)
  454. }
  455. }
  456. /*
  457. Create a socket file descriptor.
  458. Available since Linux 2.0.
  459. */
  460. socket :: proc "contextless" (domain: Address_Family, socktype: Socket_Type, sockflags: Socket_FD_Flags, protocol: Protocol) -> (Fd, Errno) {
  461. sock_type_flags: int = cast(int) socktype | transmute(int) sockflags
  462. ret := syscall(SYS_socket, domain, sock_type_flags, protocol)
  463. return errno_unwrap(ret, Fd)
  464. }
  465. /*
  466. Connect the socket to the address.
  467. Available since Linux 2.0.
  468. */
  469. connect :: proc "contextless" (sock: Fd, addr: ^$T) -> (Errno)
  470. where
  471. T == Sock_Addr_In ||
  472. T == Sock_Addr_In6 ||
  473. T == Sock_Addr_Un ||
  474. T == Sock_Addr_Any
  475. {
  476. ret := syscall(SYS_connect, sock, addr, size_of(T))
  477. return Errno(-ret)
  478. }
  479. /*
  480. Accept a pending connection or block until new connection appears.
  481. Depends on Sock_FD_Flags of the `sock` parameter.
  482. Available since Linux 2.0.
  483. */
  484. accept :: proc "contextless" (sock: Fd, addr: ^$T, sockflags: Socket_FD_Flags = {}) -> (Fd, Errno)
  485. where
  486. T == Sock_Addr_In ||
  487. T == Sock_Addr_In6 ||
  488. T == Sock_Addr_Un ||
  489. T == Sock_Addr_Any
  490. {
  491. addr_len: i32 = size_of(T)
  492. ret := syscall(SYS_accept4, sock, addr, &addr_len, transmute(int) sockflags)
  493. return errno_unwrap(ret, Fd)
  494. }
  495. // TODO(flysand): Rewrite recvfrom and sendto to use default parameters
  496. recvfrom :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg, addr: ^$T) -> (int, Errno)
  497. where
  498. T == Sock_Addr_In ||
  499. T == Sock_Addr_In6 ||
  500. T == Sock_Addr_Un ||
  501. T == Sock_Addr_Any
  502. {
  503. addr_len: i32 = size_of(T)
  504. ret := syscall(SYS_recvfrom, sock, raw_data(buf), len(buf), transmute(i32) flags, addr, &addr_len)
  505. return errno_unwrap(ret, int)
  506. }
  507. @private
  508. recv_noaddr :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg) -> (int, Errno) {
  509. ret := syscall(SYS_recvfrom, sock, raw_data(buf), len(buf), transmute(i32) flags, cast(rawptr) nil, cast(uintptr) 0)
  510. return errno_unwrap(ret, int)
  511. }
  512. sendto :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg, addr: ^$T) -> (int, Errno)
  513. where
  514. T == Sock_Addr_In ||
  515. T == Sock_Addr_In6 ||
  516. T == Sock_Addr_Un ||
  517. T == Sock_Addr_Any
  518. {
  519. ret := syscall(SYS_sendto, sock, raw_data(buf), len(buf), transmute(i32) flags, addr, size_of(T))
  520. return errno_unwrap(ret, int)
  521. }
  522. @private
  523. send_noaddr :: proc "contextless" (sock: Fd, buf: []u8, flags: Socket_Msg) -> (int, Errno) {
  524. ret := syscall(SYS_sendto, sock, raw_data(buf), len(buf), transmute(i32) flags, cast(rawptr) nil, cast(uintptr) 0)
  525. return errno_unwrap(ret, int)
  526. }
  527. /*
  528. Receive a message from a socket.
  529. Available since Linux 2.0.
  530. */
  531. recv :: proc {recvfrom, recv_noaddr}
  532. /*
  533. Send a message through a socket.
  534. Available since Linux 2.0.
  535. */
  536. send :: proc {sendto, send_noaddr}
  537. /*
  538. Send a message on a socket.
  539. Available since Linux 2.0.
  540. */
  541. sendmsg :: proc "contextless" (sock: Fd, msghdr: ^Msg_Hdr, flags: Socket_Msg) -> (int, Errno) {
  542. ret := syscall(SYS_sendmsg, sock, msghdr, transmute(i32) flags)
  543. return errno_unwrap(ret, int)
  544. }
  545. /*
  546. Receive a message on a socket.
  547. Available since Linux 2.0.
  548. */
  549. recvmsg :: proc "contextless" (sock: Fd, msghdr: ^Msg_Hdr, flags: Socket_Msg) -> (int, Errno) {
  550. ret := syscall(SYS_recvmsg, sock, msghdr, transmute(i32) flags)
  551. return errno_unwrap(ret, int)
  552. }
  553. /*
  554. Shutdown a socket.
  555. Available since Linux 2.0.
  556. */
  557. shutdown :: proc "contextless" (sock: Fd, how: Shutdown_How) -> (Errno) {
  558. ret := syscall(SYS_shutdown, sock, how)
  559. return Errno(-ret)
  560. }
  561. /*
  562. Bind a socket to the given local address.
  563. Available since Linux 2.0.
  564. */
  565. bind :: proc "contextless" (sock: Fd, addr: ^$T) -> (Errno)
  566. where
  567. T == Sock_Addr_In ||
  568. T == Sock_Addr_In6 ||
  569. T == Sock_Addr_Un ||
  570. T == Sock_Addr_Any
  571. {
  572. ret := syscall(SYS_bind, sock, addr, size_of(T))
  573. return Errno(-ret)
  574. }
  575. /*
  576. Marks the socket as a socket that listen to connections using accept(2).
  577. Available since Linux 2.0.
  578. */
  579. listen :: proc "contextless" (sock: Fd, queue_len: i32) -> (Errno) {
  580. ret := syscall(SYS_listen, sock, queue_len)
  581. return Errno(-ret)
  582. }
  583. /*
  584. Get socket name (aka it's bound address).
  585. Available since Linux 2.0.
  586. */
  587. getsockname :: proc "contextless" (sock: Fd, addr: ^Sock_Addr_Any) -> (Errno) {
  588. addr_len := size_of(Sock_Addr_Any)
  589. ret := syscall(SYS_getsockname, sock, addr, &addr_len)
  590. return Errno(-ret)
  591. }
  592. /*
  593. Get the name of the connected peer socket.
  594. Available since Linux 2.0.
  595. */
  596. getpeername :: proc "contextless" (sock: Fd, addr: ^Sock_Addr_Any) -> (Errno) {
  597. addr_len := size_of(Sock_Addr_Any)
  598. ret := syscall(SYS_getpeername, sock, addr, &addr_len)
  599. return Errno(-ret)
  600. }
  601. /*
  602. Create a pair of connected sockets.
  603. Available since Linux 2.0.
  604. */
  605. socketpair :: proc "contextless" (domain: Protocol_Family, type: Socket_Type, proto: Protocol, pair: ^[2]Fd) -> (Errno) {
  606. ret := syscall(SYS_socketpair, domain, type, proto, pair)
  607. return Errno(-ret)
  608. }
  609. // TODO(flysand): the parameters are the same, maybe there's a way to make it into a single proc, sacrificing type
  610. // safety slightly
  611. // TODO(flysand): add ability to specify slices
  612. setsockopt_base :: proc "contextless" (sock: Fd, level: int, opt: int, val: $T) -> (Errno)
  613. where
  614. intrinsics.type_is_pointer(T) ||
  615. intrinsics.type_is_multi_pointer(T)
  616. {
  617. val_data := val
  618. val_size := size_of(intrinsics.type_elem_type(T))
  619. ret := syscall(SYS_setsockopt, sock, level, opt, val_data, val_size)
  620. return Errno(-ret)
  621. }
  622. setsockopt_sock :: proc "contextless" (sock: Fd, level: Socket_API_Level_Sock, opt: Socket_Option, val: $T) -> (Errno)
  623. where
  624. intrinsics.type_is_pointer(T) ||
  625. intrinsics.type_is_multi_pointer(T)
  626. {
  627. return setsockopt_base(sock, cast(int) level, cast(int) opt, val)
  628. }
  629. setsockopt_tcp :: proc "contextless" (sock: Fd, level: Socket_API_Level_TCP, opt: Socket_TCP_Option, val: $T) -> (Errno)
  630. where
  631. intrinsics.type_is_pointer(T) ||
  632. intrinsics.type_is_multi_pointer(T)
  633. {
  634. return setsockopt_base(sock, cast(int) level, cast(int) opt, val)
  635. }
  636. setsockopt_udp :: proc "contextless" (sock: Fd, level: Socket_API_Level_UDP, opt: Socket_UDP_Option, val: $T) -> (Errno)
  637. where
  638. intrinsics.type_is_pointer(T) ||
  639. intrinsics.type_is_multi_pointer(T)
  640. {
  641. return setsockopt_base(sock, cast(int) level, cast(int) opt, val)
  642. }
  643. /*
  644. Set socket option for a given socket API level.
  645. Available since Linux 2.0.
  646. */
  647. setsockopt :: proc {
  648. setsockopt_sock,
  649. setsockopt_tcp,
  650. setsockopt_udp,
  651. setsockopt_base,
  652. }
  653. getsockopt_base :: proc "contextless" (sock: Fd, level: int, opt: Socket_Option, val: $T) -> (int, Errno)
  654. where
  655. intrinsics.type_is_pointer(T) ||
  656. intrinsics.type_is_multi_pointer(T)
  657. {
  658. val_data := val
  659. val_size := size_of(T)
  660. ret := syscall(SYS_getsockopt, sock, level, opt, val_data, cast(rawptr) &val_size)
  661. return val_size, Errno(-ret)
  662. }
  663. getsockopt_sock :: proc "contextless" (sock: Fd, level: Socket_API_Level_Sock, opt: Socket_Option, val: ^$T) -> (int, Errno)
  664. where
  665. intrinsics.type_is_pointer(T) ||
  666. intrinsics.type_is_multi_pointer(T)
  667. {
  668. return getsockopt_base(sock, cast(int) level, cast(int) opt, val)
  669. }
  670. getsockopt_tcp :: proc "contextless" (sock: Fd, level: Socket_API_Level_TCP, opt: Socket_TCP_Option, val: ^$T) -> (int, Errno)
  671. where
  672. intrinsics.type_is_pointer(T) ||
  673. intrinsics.type_is_multi_pointer(T)
  674. {
  675. return getsockopt_base(sock, cast(int) level, cast(int) opt, val)
  676. }
  677. getsockopt_udp :: proc "contextless" (sock: Fd, level: Socket_API_Level_UDP, opt: Socket_UDP_Option, val: ^$T) -> (int, Errno)
  678. where
  679. intrinsics.type_is_pointer(T) ||
  680. intrinsics.type_is_multi_pointer(T)
  681. {
  682. return getsockopt_base(sock, cast(int) level, cast(int) opt, val)
  683. }
  684. /*
  685. Get socket option for a given socket API level.
  686. Available since Linux 2.0.
  687. */
  688. getsockopt :: proc {
  689. getsockopt_sock,
  690. getsockopt_tcp,
  691. getsockopt_udp,
  692. getsockopt_base,
  693. }
  694. /*
  695. Creates a copy of the running process.
  696. Available since Linux 1.0.
  697. */
  698. fork :: proc "contextless" () -> (Pid, Errno) {
  699. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  700. ret := syscall(SYS_clone, u64(Signal.SIGCHLD), cast(rawptr) nil, cast(rawptr) nil, cast(rawptr) nil, u64(0))
  701. return errno_unwrap(ret, Pid)
  702. } else {
  703. ret := syscall(SYS_fork)
  704. return errno_unwrap(ret, Pid)
  705. }
  706. }
  707. /*
  708. Create a child process and block parent.
  709. Available since Linux 2.2.
  710. */
  711. vfork :: proc "contextless" () -> Pid {
  712. when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
  713. return Pid(syscall(SYS_vfork))
  714. } else {
  715. return Pid(syscall(SYS_clone, Signal.SIGCHLD))
  716. }
  717. }
  718. /*
  719. Replace the current process with another program.
  720. Available since Linux 1.0.
  721. On ARM64 available since Linux 3.19.
  722. */
  723. execve :: proc "contextless" (name: cstring, argv: [^]cstring, envp: [^]cstring) -> (Errno) {
  724. when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
  725. ret := syscall(SYS_execve, cast(rawptr) name, cast(rawptr) argv, cast(rawptr) envp)
  726. return Errno(-ret)
  727. } else {
  728. ret := syscall(SYS_execveat, AT_FDCWD, cast(rawptr) name, cast(rawptr) argv, cast(rawptr) envp, i32(0))
  729. return Errno(-ret)
  730. }
  731. }
  732. /*
  733. Exit the thread with a given exit code.
  734. Available since Linux 1.0.
  735. */
  736. exit :: proc "contextless" (code: i32) -> ! {
  737. syscall(SYS_exit, code)
  738. unreachable()
  739. }
  740. /*
  741. Wait for the process to change state.
  742. Available since Linux 1.0.
  743. */
  744. wait4 :: proc "contextless" (pid: Pid, status: ^u32, options: Wait_Options, rusage: ^RUsage) -> (Pid, Errno) {
  745. ret := syscall(SYS_wait4, pid, status, transmute(u32) options, rusage)
  746. return errno_unwrap(ret, Pid)
  747. }
  748. /*
  749. See `wait4` for documentation.
  750. */
  751. waitpid :: wait4
  752. /*
  753. Send signal to a process.
  754. Available since Linux 1.0.
  755. */
  756. kill :: proc "contextless" (pid: Pid, signal: Signal) -> (Errno) {
  757. ret := syscall(SYS_kill, pid, signal)
  758. return Errno(-ret)
  759. }
  760. /*
  761. Get system information.
  762. Available since Linux 1.0.
  763. */
  764. uname :: proc "contextless" (uts_name: ^UTS_Name) -> (Errno) {
  765. ret := syscall(SYS_uname, uts_name)
  766. return Errno(-ret)
  767. }
  768. /*
  769. Get a SystemV semaphore set identifier.
  770. Available since Linux 2.0.
  771. */
  772. semget :: proc "contextless" (key: Key, n: i32, flags: IPC_Flags) -> (Key, Errno) {
  773. ret := syscall(SYS_semget, key, n, transmute(i16) flags)
  774. return errno_unwrap(ret, Key)
  775. }
  776. /*
  777. SystemV semaphore operations.
  778. Available since Linux 2.0.
  779. */
  780. semop :: proc "contextless" (key: Key, ops: []Sem_Buf) -> (Errno) {
  781. when ODIN_ARCH != .i386 {
  782. ret := syscall(SYS_semop, key, raw_data(ops), len(ops))
  783. return Errno(-ret)
  784. } else {
  785. // Note(flysand): Max time limit, let's not worry about nanoseconds...
  786. max_timespec := Time_Spec {
  787. time_sec = max(uint),
  788. time_nsec = 0,
  789. }
  790. ret := syscall(SYS_semtimedop_time64, key, raw_data(ops), len(ops), &max_timespec)
  791. return Errno(-ret)
  792. }
  793. }
  794. semctl3 :: proc "contextless" (key: Key, semnum: i32, cmd: IPC_Cmd) -> (int, Errno) {
  795. ret := syscall(SYS_semctl, key, semnum, cmd)
  796. return errno_unwrap(ret, int)
  797. }
  798. semctl4 :: proc "contextless" (key: Key, semnum: i32, cmd: IPC_Cmd, semun: ^Sem_Un) -> (int, Errno) {
  799. ret := syscall(SYS_semctl, key, semnum, cmd, semun)
  800. return errno_unwrap(ret, int)
  801. }
  802. /*
  803. SystemV semaphore control operations.
  804. Available since Linux 2.0.
  805. */
  806. semctl :: proc {semctl3, semctl4}
  807. /*
  808. SystemV shared memory operations.
  809. Available since Linux 2.0.
  810. */
  811. shmdt :: proc "contextless" (shmaddr: rawptr) -> (Errno) {
  812. ret := syscall(SYS_shmdt, shmaddr)
  813. return Errno(-ret)
  814. }
  815. /*
  816. Get SystemV message queue identifier.
  817. Available since Linux 2.0.
  818. */
  819. msgget :: proc "contextless" (key: Key, flags: IPC_Flags) -> (Key, Errno) {
  820. ret := syscall(SYS_msgget, key, transmute(i16) flags)
  821. return errno_unwrap(ret, Key)
  822. }
  823. /*
  824. Send message to a SystemV message queue.
  825. Available since Linux 2.0.
  826. */
  827. msgsnd :: proc "contextless" (key: Key, buf: rawptr, size: int, flags: IPC_Flags) -> (Errno) {
  828. ret := syscall(SYS_msgsnd, key, buf, size, transmute(i16) flags)
  829. return Errno(-ret)
  830. }
  831. /*
  832. Receive a message from a SystemV message queue.
  833. Available since Linux 2.0.
  834. */
  835. msgrcv :: proc "contextless" (key: Key, buf: rawptr, size: int, type: uint, flags: IPC_Flags) -> (int, Errno) {
  836. ret := syscall(SYS_msgrcv, key, buf, size, type, transmute(i16) flags)
  837. return errno_unwrap(ret, int)
  838. }
  839. /*
  840. SystemV message control operations.
  841. Available since Linux 2.0.
  842. */
  843. msgctl :: proc "contextless" (key: Key, cmd: IPC_Cmd, buf: ^Msqid_DS) -> (int, Errno) {
  844. ret := syscall(SYS_msgctl, key, cmd, buf)
  845. return errno_unwrap(ret, int)
  846. }
  847. fcntl_dupfd :: proc "contextless" (fd: Fd, cmd: FCntl_Command_DUPFD, newfd: Fd) -> (Fd, Errno) {
  848. ret := syscall(SYS_fcntl, fd, cmd, newfd)
  849. return errno_unwrap(ret, Fd)
  850. }
  851. fcntl_dupfd_cloexec :: proc "contextless" (fd: Fd, cmd: FCntl_Command_DUPFD_CLOEXEC, newfd: Fd) -> (Fd, Errno) {
  852. ret := syscall(SYS_fcntl, fd, cmd, newfd)
  853. return errno_unwrap(ret, Fd)
  854. }
  855. fcntl_getfd :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETFD) -> (Fd, Errno) {
  856. ret := syscall(SYS_fcntl, fd, cmd)
  857. return errno_unwrap(ret, Fd)
  858. }
  859. fcntl_setfd :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETFD, newfd: Fd) -> (Errno) {
  860. ret := syscall(SYS_fcntl, fd, cmd, newfd)
  861. return Errno(-ret)
  862. }
  863. fcntl_getfl :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETFL) -> (Open_Flags, Errno) {
  864. ret := syscall(SYS_fcntl, fd, cmd)
  865. return errno_unwrap(u32(ret), Open_Flags, Open_Flags)
  866. }
  867. fcntl_setfl :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETFL, flags: Open_Flags) -> (Errno) {
  868. ret := syscall(SYS_fcntl, fd, cmd, transmute(u32) flags)
  869. return Errno(-ret)
  870. }
  871. fcntl_setlk :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETLK, lock: ^FLock) -> (Errno) {
  872. when size_of(int) == 4 {
  873. ret := syscall(SYS_fcntl64, fd, cmd, lock)
  874. return Errno(-ret)
  875. } else {
  876. ret := syscall(SYS_fcntl, fd, cmd, lock)
  877. return Errno(-ret)
  878. }
  879. }
  880. fcntl_setlkw :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETLKW, lock: ^FLock) -> (Errno) {
  881. when size_of(int) == 4 {
  882. ret := syscall(SYS_fcntl64, fd, cmd, lock)
  883. return Errno(-ret)
  884. } else {
  885. ret := syscall(SYS_fcntl, fd, cmd, lock)
  886. return Errno(-ret)
  887. }
  888. }
  889. fcntl_getlk :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETLK, lock: ^FLock) -> (Errno) {
  890. when size_of(int) == 4 {
  891. ret := syscall(SYS_fcntl64, fd, cmd, lock)
  892. return Errno(-ret)
  893. } else {
  894. ret := syscall(SYS_fcntl, fd, cmd, lock)
  895. return Errno(-ret)
  896. }
  897. }
  898. fcntl_getown_ex :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETOWN_EX, owner: ^F_Owner) -> (Errno) {
  899. ret := syscall(SYS_fcntl, fd, cmd, owner)
  900. return Errno(-ret)
  901. }
  902. fcntl_setown_ex :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETOWN_EX, owner: ^F_Owner) -> (Errno) {
  903. ret := syscall(SYS_fcntl, fd, cmd, owner)
  904. return Errno(-ret)
  905. }
  906. fcntl_getsig :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETSIG) -> (Signal, Errno) {
  907. ret := syscall(SYS_fcntl, fd, cmd)
  908. return errno_unwrap(ret, Signal)
  909. }
  910. fcntl_setsig :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETSIG, sig: Signal) -> (Errno) {
  911. ret := syscall(SYS_fcntl, fd, cmd, sig)
  912. return Errno(-ret)
  913. }
  914. fcntl_setlease :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETLEASE, lease: FD_Lease) -> (Errno) {
  915. ret := syscall(SYS_fcntl, fd, cmd, lease)
  916. return Errno(-ret)
  917. }
  918. fcntl_getlease :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETLEASE) -> (FD_Lease, Errno) {
  919. ret := syscall(SYS_fcntl, fd, cmd)
  920. return errno_unwrap(ret, FD_Lease)
  921. }
  922. fcntl_notify :: proc "contextless" (fd: Fd, cmd: FCntl_Command_NOTIFY, notifications: FD_Notifications) -> (Errno) {
  923. ret := syscall(SYS_fcntl, fd, cmd)
  924. return Errno(-ret)
  925. }
  926. fcntl_setpipe_sz :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SETPIPE_SZ, sz: i32) -> (i32, Errno) {
  927. ret := syscall(SYS_fcntl, fd, cmd, sz)
  928. return errno_unwrap(ret, i32)
  929. }
  930. fcntl_getpipe_sz :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GETPIPE_SZ) -> (i32, Errno) {
  931. ret := syscall(SYS_fcntl, fd, cmd)
  932. return errno_unwrap(ret, i32)
  933. }
  934. fcntl_add_seals :: proc "contextless" (fd: Fd, cmd: FCntl_Command_ADD_SEALS, seal: Seal) -> (Errno) {
  935. ret := syscall(SYS_fcntl, fd, cmd, transmute(i32) seal)
  936. return Errno(-ret)
  937. }
  938. fcntl_get_seals :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GET_SEALS) -> (Seal, Errno) {
  939. ret := syscall(SYS_fcntl, fd, cmd)
  940. return errno_unwrap(i32(ret), Seal, Seal)
  941. }
  942. fcntl_get_rw_hint :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GET_RW_HINT, hint: ^RW_Hint) -> (Errno) {
  943. ret := syscall(SYS_fcntl, fd, cmd, hint)
  944. return Errno(-ret)
  945. }
  946. fcntl_set_rw_hint :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SET_RW_HINT, hint: ^RW_Hint) -> (Errno) {
  947. ret := syscall(SYS_fcntl, fd, cmd, hint)
  948. return Errno(-ret)
  949. }
  950. fcntl_get_file_rw_hint :: proc "contextless" (fd: Fd, cmd: FCntl_Command_GET_FILE_RW_HINT, hint: ^RW_Hint) -> (Errno) {
  951. ret := syscall(SYS_fcntl, fd, cmd, hint)
  952. return Errno(-ret)
  953. }
  954. fcntl_set_file_rw_hint :: proc "contextless" (fd: Fd, cmd: FCntl_Command_SET_FILE_RW_HINT, hint: ^RW_Hint) -> (Errno) {
  955. ret := syscall(SYS_fcntl, fd, cmd, hint)
  956. return Errno(-ret)
  957. }
  958. fcntl :: proc {
  959. fcntl_dupfd,
  960. fcntl_dupfd_cloexec,
  961. fcntl_getfd,
  962. fcntl_setfd,
  963. fcntl_getfl,
  964. fcntl_setfl,
  965. fcntl_setlk,
  966. fcntl_setlkw,
  967. fcntl_getlk,
  968. fcntl_getown_ex,
  969. fcntl_setown_ex,
  970. fcntl_getsig,
  971. fcntl_setsig,
  972. fcntl_setlease,
  973. fcntl_getlease,
  974. fcntl_notify,
  975. fcntl_setpipe_sz,
  976. fcntl_getpipe_sz,
  977. fcntl_add_seals,
  978. fcntl_get_seals,
  979. fcntl_get_rw_hint,
  980. fcntl_set_rw_hint,
  981. fcntl_get_file_rw_hint,
  982. fcntl_set_file_rw_hint,
  983. }
  984. /*
  985. Apply or remove advisory lock on an open file.
  986. Available since Linux 2.0.
  987. */
  988. flock :: proc "contextless" (fd: Fd, operation: FLock_Op) -> (Errno) {
  989. ret := syscall(SYS_flock, fd, transmute(i32) operation)
  990. return Errno(-ret)
  991. }
  992. /*
  993. Sync state of the file with the storage device.
  994. Available since Linux 1.0.
  995. */
  996. fsync :: proc "contextless" (fd: Fd) -> (Errno) {
  997. ret := syscall(SYS_fsync, fd)
  998. return Errno(-ret)
  999. }
  1000. /*
  1001. Synchronize the state of the file with the storage device. Similar to `fsync`,
  1002. except does not flush the metadata.
  1003. Available since Linux 2.0.
  1004. */
  1005. fdatasync :: proc "contextless" (fd: Fd) -> (Errno) {
  1006. ret := syscall(SYS_fdatasync, fd)
  1007. return Errno(-ret)
  1008. }
  1009. /*
  1010. Truncate a file to specified length.
  1011. Available since Linux 1.0.
  1012. On 32-bit architectures available since Linux 2.4.
  1013. */
  1014. truncate :: proc "contextless" (name: cstring, length: i64) -> (Errno) {
  1015. when ODIN_ARCH == .arm32 {
  1016. ret := syscall(SYS_truncate64, cast(rawptr) name, 0, compat64_arg_pair(length))
  1017. return Errno(-ret)
  1018. } else when size_of(int) == 4 {
  1019. ret := syscall(SYS_truncate64, cast(rawptr) name, compat64_arg_pair(length))
  1020. return Errno(-ret)
  1021. } else {
  1022. ret := syscall(SYS_truncate, cast(rawptr) name, compat64_arg_pair(length))
  1023. return Errno(-ret)
  1024. }
  1025. }
  1026. /*
  1027. Truncate a file specified by file descriptor to specified length.
  1028. On 32-bit architectures available since 2.4.
  1029. */
  1030. ftruncate :: proc "contextless" (fd: Fd, length: i64) -> (Errno) {
  1031. when ODIN_ARCH == .arm32 {
  1032. ret := syscall(SYS_ftruncate64, fd, 0, compat64_arg_pair(length))
  1033. return Errno(-ret)
  1034. } else when size_of(int) == 4 {
  1035. ret := syscall(SYS_ftruncate64, fd, compat64_arg_pair(length))
  1036. return Errno(-ret)
  1037. } else {
  1038. ret := syscall(SYS_ftruncate, fd, compat64_arg_pair(length))
  1039. return Errno(-ret)
  1040. }
  1041. }
  1042. /*
  1043. Retrieve the contents of the directory specified by dirfd.
  1044. Returns the number of bytes written.
  1045. Available since Linux 2.4.
  1046. */
  1047. getdents :: proc "contextless" (dirfd: Fd, buf: []u8) -> (int, Errno) {
  1048. ret := syscall(SYS_getdents64, dirfd, raw_data(buf), len(buf))
  1049. return errno_unwrap(ret, int)
  1050. }
  1051. /*
  1052. Get current working directory.
  1053. Available since Linux 1.0.
  1054. */
  1055. getcwd :: proc "contextless" (buf: []u8) -> (int, Errno) {
  1056. ret := syscall(SYS_getcwd, raw_data(buf), len(buf))
  1057. return errno_unwrap(ret, int)
  1058. }
  1059. /*
  1060. Change working directory to the directory specified by path.
  1061. Available since Linux 1.0.
  1062. */
  1063. chdir :: proc "contextless" (path: cstring) -> (Errno) {
  1064. ret := syscall(SYS_chdir, cast(rawptr) path)
  1065. return Errno(-ret)
  1066. }
  1067. /*
  1068. Change working directory to the directory specified by dirfd.
  1069. Available since Linux 1.0.
  1070. */
  1071. fchdir :: proc "contextless" (fd: Fd) -> (Errno) {
  1072. ret := syscall(SYS_fchdir, fd)
  1073. return Errno(-ret)
  1074. }
  1075. /*
  1076. Rename (move) the file.
  1077. Available since Linux 1.0.
  1078. On ARM64 available since Linux 2.6.16.
  1079. */
  1080. rename :: proc "contextless" (old: cstring, new: cstring) -> (Errno) {
  1081. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1082. ret := syscall(SYS_renameat, AT_FDCWD, cast(rawptr) old, AT_FDCWD, cast(rawptr) new)
  1083. return Errno(-ret)
  1084. } else {
  1085. ret := syscall(SYS_rename, cast(rawptr) old, cast(rawptr) new)
  1086. return Errno(-ret)
  1087. }
  1088. }
  1089. /*
  1090. Creates a directory.
  1091. Available since Linux 1.0.
  1092. On ARM64 available since Linux 2.6.16.
  1093. */
  1094. mkdir :: proc "contextless" (name: cstring, mode: Mode) -> (Errno) {
  1095. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1096. ret := syscall(SYS_mkdirat, AT_FDCWD, cast(rawptr) name, transmute(u32) mode)
  1097. return Errno(-ret)
  1098. } else {
  1099. ret := syscall(SYS_mkdir, cast(rawptr) name, transmute(u32) mode)
  1100. return Errno(-ret)
  1101. }
  1102. }
  1103. /*
  1104. Remove a directory specified by name.
  1105. Available since Linux 1.0.
  1106. On ARM64 available since Linux 2.6.16.
  1107. */
  1108. rmdir :: proc "contextless" (name: cstring) -> (Errno) {
  1109. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1110. ret := syscall(SYS_unlinkat, AT_FDCWD, cast(rawptr) name, transmute(i32) FD_Flags{.REMOVEDIR})
  1111. return Errno(-ret)
  1112. } else {
  1113. ret := syscall(SYS_rmdir, cast(rawptr) name)
  1114. return Errno(-ret)
  1115. }
  1116. }
  1117. /*
  1118. Create a file.
  1119. Available since Linux 1.0.
  1120. On ARM64 available since Linux 2.6.16.
  1121. */
  1122. creat :: proc "contextless" (name: cstring, mode: Mode) -> (Fd, Errno) {
  1123. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1124. return openat(AT_FDCWD, name, {.CREAT, .WRONLY,.TRUNC}, mode)
  1125. } else {
  1126. ret := syscall(SYS_creat, cast(rawptr) name, transmute(u32) mode)
  1127. return errno_unwrap(ret, Fd)
  1128. }
  1129. }
  1130. /*
  1131. Create a hard link on a file.
  1132. Available since Linux 1.0.
  1133. On ARM64 available since Linux 2.6.16.
  1134. */
  1135. link :: proc "contextless" (target: cstring, linkpath: cstring) -> (Errno) {
  1136. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1137. ret := syscall(SYS_linkat, AT_FDCWD, cast(rawptr) target, AT_FDCWD, cast(rawptr) linkpath, 0)
  1138. return Errno(-ret)
  1139. } else {
  1140. ret := syscall(SYS_link, cast(rawptr) target, cast(rawptr) linkpath)
  1141. return Errno(-ret)
  1142. }
  1143. }
  1144. /*
  1145. Delete a name, and possible a file it refers to.
  1146. Available since Linux 1.0.
  1147. On ARM64 available since Linux 2.6.16.
  1148. */
  1149. unlink :: proc "contextless" (name: cstring) -> (Errno) {
  1150. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1151. ret := syscall(SYS_unlinkat, AT_FDCWD, cast(rawptr) name, 0)
  1152. return Errno(-ret)
  1153. } else {
  1154. ret := syscall(SYS_unlink, cast(rawptr) name)
  1155. return Errno(-ret)
  1156. }
  1157. }
  1158. /*
  1159. Create a symbolic link.
  1160. Available since Linux 1.0.
  1161. On arm64 available since Linux 2.6.16.
  1162. */
  1163. symlink :: proc "contextless" (target: cstring, linkpath: cstring) -> (Errno) {
  1164. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1165. ret := syscall(SYS_symlinkat, cast(rawptr) target, AT_FDCWD, cast(rawptr) linkpath)
  1166. return Errno(-ret)
  1167. } else {
  1168. ret := syscall(SYS_symlink, cast(rawptr) target, cast(rawptr) linkpath)
  1169. return Errno(-ret)
  1170. }
  1171. }
  1172. /*
  1173. Read the value of a symbolic link.
  1174. Available since Linux 1.0.
  1175. On arm64 available since Linux 2.6.16.
  1176. */
  1177. readlink :: proc "contextless" (name: cstring, buf: []u8) -> (int, Errno) {
  1178. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1179. ret := syscall(SYS_readlinkat, AT_FDCWD, cast(rawptr) name, raw_data(buf), len(buf))
  1180. return errno_unwrap(ret, int)
  1181. } else {
  1182. ret := syscall(SYS_readlink, cast(rawptr) name, raw_data(buf), len(buf))
  1183. return errno_unwrap(ret, int)
  1184. }
  1185. }
  1186. /*
  1187. Change file permissions.
  1188. Available since Linux 1.0.
  1189. On ARM64 available since Linux 2.6.16.
  1190. */
  1191. chmod :: proc "contextless" (name: cstring, mode: Mode) -> (Errno) {
  1192. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1193. ret := syscall(SYS_fchmodat, AT_FDCWD, cast(rawptr) name, transmute(u32) mode)
  1194. return Errno(-ret)
  1195. } else {
  1196. ret := syscall(SYS_chmod, cast(rawptr) name, transmute(u32) mode)
  1197. return Errno(-ret)
  1198. }
  1199. }
  1200. /*
  1201. Change file permissions through a file descriptor.
  1202. Available since Linux 1.0.
  1203. */
  1204. fchmod :: proc "contextless" (fd: Fd, mode: Mode) -> (Errno) {
  1205. ret := syscall(SYS_fchmod, fd, transmute(u32) mode)
  1206. return Errno(-ret)
  1207. }
  1208. /*
  1209. Change ownership of a file.
  1210. Available since Linux 2.2.
  1211. On 32-bit architectures available since Linux 2.4.
  1212. On ARM64 available since Linux 2.6.16.
  1213. */
  1214. chown :: proc "contextless" (name: cstring, uid: Uid, gid: Gid) -> (Errno) {
  1215. when size_of(int) == 4 {
  1216. ret := syscall(SYS_chown32, cast(rawptr) name, uid, gid)
  1217. return Errno(-ret)
  1218. } else when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1219. ret := syscall(SYS_fchownat, AT_FDCWD, cast(rawptr) name, uid, gid, 0)
  1220. return Errno(-ret)
  1221. } else {
  1222. ret := syscall(SYS_chown, cast(rawptr) name, uid, gid)
  1223. return Errno(-ret)
  1224. }
  1225. }
  1226. /*
  1227. Change ownership of a file by file descriptor.
  1228. Available since Linux 1.0.
  1229. On 32-bit architecvtures available since Linux 2.4.
  1230. */
  1231. fchown :: proc "contextless" (fd: Fd, uid: Uid, gid: Gid) -> (Errno) {
  1232. when size_of(int) == 4 {
  1233. ret := syscall(SYS_fchown32, fd, uid, gid)
  1234. return Errno(-ret)
  1235. } else {
  1236. ret := syscall(SYS_fchown, fd, uid, gid)
  1237. return Errno(-ret)
  1238. }
  1239. }
  1240. /*
  1241. Change ownership of a file. Unlike chown, if a file is a symlink dooesn't dereference it.
  1242. Available since Linux 1.0.
  1243. On 32-bit architectures available since Linux 2.4.
  1244. On ARM64 available since Linux 2.6.16.
  1245. */
  1246. lchown :: proc "contextless" (name: cstring, uid: Uid, gid: Gid) -> (Errno) {
  1247. when size_of(int) == 4 {
  1248. ret := syscall(SYS_lchown32, cast(rawptr) name, uid, gid)
  1249. return Errno(-ret)
  1250. } else when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1251. ret := syscall(SYS_fchownat, AT_FDCWD, cast(rawptr) name, uid, gid, transmute(i32) FD_Flags{.SYMLINK_NOFOLLOW})
  1252. return Errno(-ret)
  1253. } else {
  1254. ret := syscall(SYS_lchown, cast(rawptr) name, uid, gid)
  1255. return Errno(-ret)
  1256. }
  1257. }
  1258. /*
  1259. Set file mode creation mask.
  1260. Available since Linux 1.0.
  1261. */
  1262. umask :: proc "contextless" (mask: Mode) -> Mode {
  1263. ret := syscall(SYS_umask, transmute(u32) mask)
  1264. return transmute(Mode) cast(u32) ret
  1265. }
  1266. /*
  1267. Get current time.
  1268. Available since Linux 1.0.
  1269. */
  1270. gettimeofday :: proc "contextless" (tv: ^Time_Val) -> (Errno) {
  1271. ret := syscall(SYS_gettimeofday, tv)
  1272. return Errno(-ret)
  1273. }
  1274. /*
  1275. Get limits on resources.
  1276. Available since Linux 1.0.
  1277. */
  1278. getrlimit :: proc "contextless" (kind: RLimit_Kind, resource: ^RLimit) -> (Errno) {
  1279. ret := syscall(SYS_getrlimit, kind, resource)
  1280. return Errno(-ret)
  1281. }
  1282. /*
  1283. Get resource usage.
  1284. Available since Linux 1.0.
  1285. */
  1286. getrusage :: proc "contextless" (who: RUsage_Who, rusage: ^RUsage) -> (Errno) {
  1287. ret := syscall(SYS_getrusage, who, rusage)
  1288. return Errno(-ret)
  1289. }
  1290. /*
  1291. Get information about the system.
  1292. Available since Linux 1.0.
  1293. */
  1294. sysinfo :: proc "contextless" (sysinfo: ^Sys_Info) -> (Errno) {
  1295. ret := syscall(SYS_sysinfo, sysinfo)
  1296. return Errno(-ret)
  1297. }
  1298. /*
  1299. Get current process times.
  1300. Available since Linux 1.0.
  1301. */
  1302. times :: proc "contextless" (tms: ^Tms) -> (Errno) {
  1303. ret := syscall(SYS_times, cast(rawptr) tms)
  1304. return Errno(-ret)
  1305. }
  1306. ptrace_traceme :: proc "contextless" (rq: PTrace_Traceme_Type) -> (Errno) {
  1307. ret := syscall(SYS_ptrace, rq)
  1308. return Errno(-ret)
  1309. }
  1310. ptrace_peek :: proc "contextless" (rq: PTrace_Peek_Type, pid: Pid, addr: uintptr) -> (uint, Errno) {
  1311. res: uint = ---
  1312. ret := syscall(SYS_ptrace, rq, pid, addr, &res)
  1313. return res, Errno(-ret)
  1314. }
  1315. ptrace_poke :: proc "contextless" (rq: PTrace_Poke_Type, pid: Pid, addr: uintptr, data: uint) -> (Errno) {
  1316. ret := syscall(SYS_ptrace, rq, pid, addr, data)
  1317. return Errno(-ret)
  1318. }
  1319. ptrace_getregs :: proc "contextless" (rq: PTrace_Getregs_Type, pid: Pid, buf: ^User_Regs) -> (Errno) {
  1320. ret := syscall(SYS_ptrace, rq, pid, 0, buf)
  1321. return Errno(-ret)
  1322. }
  1323. ptrace_getfpregs :: proc "contextless" (rq: PTrace_Getfpregs_Type, pid: Pid, buf: ^User_FP_Regs) -> (Errno) {
  1324. ret := syscall(SYS_ptrace, rq, pid, 0, buf)
  1325. return Errno(-ret)
  1326. }
  1327. ptrace_getfpxregs :: proc "contextless" (rq: PTrace_Getfpxregs_Type, pid: Pid, buf: ^User_FPX_Regs) -> (Errno) {
  1328. ret := syscall(SYS_ptrace, rq, pid, 0, buf)
  1329. return Errno(-ret)
  1330. }
  1331. ptrace_setregs :: proc "contextless" (rq: PTrace_Setregs_Type, pid: Pid, buf: ^User_Regs) -> (Errno) {
  1332. ret := syscall(SYS_ptrace, rq, pid, 0, buf)
  1333. return Errno(-ret)
  1334. }
  1335. ptrace_setfpregs :: proc "contextless" (rq: PTrace_Setfpregs_Type, pid: Pid, buf: ^User_FP_Regs) -> (Errno) {
  1336. ret := syscall(SYS_ptrace, rq, pid, 0, buf)
  1337. return Errno(-ret)
  1338. }
  1339. ptrace_setfpxregs :: proc "contextless" (rq: PTrace_Setfpxregs_Type, pid: Pid, buf: ^User_FPX_Regs) -> (Errno) {
  1340. ret := syscall(SYS_ptrace, rq, pid, 0, buf)
  1341. return Errno(-ret)
  1342. }
  1343. ptrace_getregset :: proc "contextless" (rq: PTrace_Getregset_Type, pid: Pid, note: PTrace_Note_Type, buf: ^IO_Vec) -> (Errno) {
  1344. ret := syscall(SYS_ptrace, rq, pid, note, buf)
  1345. return Errno(-ret)
  1346. }
  1347. ptrace_setregset :: proc "contextless" (rq: PTrace_Setregset_Type, pid: Pid, note: PTrace_Note_Type, buf: ^IO_Vec) -> (Errno) {
  1348. ret := syscall(SYS_ptrace, rq, pid, note, buf)
  1349. return Errno(-ret)
  1350. }
  1351. ptrace_getsiginfo :: proc "contextless" (rq: PTrace_Getsiginfo_Type, pid: Pid, si: ^Sig_Info) -> (Errno) {
  1352. ret := syscall(SYS_ptrace, rq, pid, si, rawptr(nil))
  1353. return Errno(-ret)
  1354. }
  1355. ptrace_peeksiginfo :: proc "contextless" (rq: PTrace_Peeksiginfo_Type, pid: Pid, si: ^Sig_Info) -> (Errno) {
  1356. ret := syscall(SYS_ptrace, rq, pid, si, rawptr(nil))
  1357. return Errno(-ret)
  1358. }
  1359. ptrace_getsigmask :: proc "contextless" (rq: PTrace_Getsigmask_Type, pid: Pid, sigmask: ^Sig_Set) -> (Errno) {
  1360. ret := syscall(SYS_ptrace, rq, pid, size_of(Sig_Set), sigmask)
  1361. return Errno(-ret)
  1362. }
  1363. ptrace_setsigmask :: proc "contextless" (rq: PTrace_Setsigmask_Type, pid: Pid, sigmask: ^Sig_Set) -> (Errno) {
  1364. ret := syscall(SYS_ptrace, rq, pid, size_of(Sig_Set), sigmask)
  1365. return Errno(-ret)
  1366. }
  1367. ptrace_setoptions :: proc "contextless" (rq: PTrace_Setoptions_Type, pid: Pid, options: PTrace_Options) -> (Errno) {
  1368. ret := syscall(SYS_ptrace, rq, pid, 0, transmute(u32) options)
  1369. return Errno(-ret)
  1370. }
  1371. ptrace_geteventmsg :: proc "contextless" (rq: PTrace_Geteventmsg_Type, pid: Pid, msg: ^uint) -> (Errno) {
  1372. ret := syscall(SYS_ptrace, rq, pid, msg, rawptr(nil))
  1373. return Errno(-ret)
  1374. }
  1375. ptrace_cont :: proc "contextless" (rq: PTrace_Cont_Type, pid: Pid, sig: Signal) -> (Errno) {
  1376. ret := syscall(SYS_ptrace, rq, pid, rawptr(nil), sig)
  1377. return Errno(-ret)
  1378. }
  1379. ptrace_singlestep :: proc "contextless" (rq: PTrace_Singlestep_Type, pid: Pid, sig: Signal) -> (Errno) {
  1380. ret := syscall(SYS_ptrace, rq, pid, rawptr(nil), sig)
  1381. return Errno(-ret)
  1382. }
  1383. ptrace_syscall :: proc "contextless" (rq: PTrace_Syscall_Type, pid: Pid, sig: Signal) -> (Errno) {
  1384. ret := syscall(SYS_ptrace, rq, pid, rawptr(nil), sig)
  1385. return Errno(-ret)
  1386. }
  1387. ptrace_sysemu :: proc "contextless" (rq: PTrace_Sysemu_Type, pid: Pid, sig: Signal) -> (Errno) {
  1388. ret := syscall(SYS_ptrace, rq, pid, rawptr(nil), sig)
  1389. return Errno(-ret)
  1390. }
  1391. ptrace_sysemu_singlestep :: proc "contextless" (rq: PTrace_Sysemu_Singlestep_Type, pid: Pid, sig: Signal) -> (Errno) {
  1392. ret := syscall(SYS_ptrace, rq, pid, rawptr(nil), sig)
  1393. return Errno(-ret)
  1394. }
  1395. ptrace_listen :: proc "contextless" (rq: PTrace_Listen_Type, pid: Pid) -> (Errno) {
  1396. ret := syscall(SYS_ptrace, rq, pid, 0, rawptr(nil))
  1397. return Errno(-ret)
  1398. }
  1399. ptrace_interrupt :: proc "contextless" (rq: PTrace_Interrupt_Type, pid: Pid) -> (Errno) {
  1400. ret := syscall(SYS_ptrace, rq, pid, 0, rawptr(nil))
  1401. return Errno(-ret)
  1402. }
  1403. ptrace_attach :: proc "contextless" (rq: PTrace_Attach_Type, pid: Pid) -> (Errno) {
  1404. ret := syscall(SYS_ptrace, rq, pid, 0, rawptr(nil))
  1405. return Errno(-ret)
  1406. }
  1407. ptrace_seize :: proc "contextless" (rq: PTrace_Seize_Type, pid: Pid, opt: PTrace_Options) -> (Errno) {
  1408. ret := syscall(SYS_ptrace, rq, pid, 0, transmute(u32) opt, rawptr(nil))
  1409. return Errno(-ret)
  1410. }
  1411. // TODO(flysand): ptrace_seccomp_get_filter
  1412. ptrace_detach :: proc "contextless" (rq: PTrace_Detach_Type, pid: Pid, sig: Signal) -> (Errno) {
  1413. ret := syscall(SYS_ptrace, rq, pid, 0, sig)
  1414. return Errno(-ret)
  1415. }
  1416. // TODO(flysand): ptrace_get_thread_area
  1417. // TODO(flysand): ptrace_set_thread_area
  1418. // TODO(flysand): ptrace_get_syscall_info
  1419. /*
  1420. Trace process.
  1421. */
  1422. ptrace :: proc {
  1423. ptrace_traceme,
  1424. ptrace_peek,
  1425. ptrace_poke,
  1426. ptrace_getregs,
  1427. ptrace_getfpregs,
  1428. ptrace_getfpxregs,
  1429. ptrace_setregs,
  1430. ptrace_setfpregs,
  1431. ptrace_setfpxregs,
  1432. ptrace_getregset,
  1433. ptrace_setregset,
  1434. ptrace_getsiginfo,
  1435. ptrace_peeksiginfo,
  1436. ptrace_getsigmask,
  1437. ptrace_setsigmask,
  1438. ptrace_setoptions,
  1439. ptrace_geteventmsg,
  1440. ptrace_cont,
  1441. ptrace_singlestep,
  1442. ptrace_syscall,
  1443. ptrace_sysemu,
  1444. ptrace_sysemu_singlestep,
  1445. ptrace_listen,
  1446. ptrace_interrupt,
  1447. ptrace_attach,
  1448. ptrace_seize,
  1449. ptrace_detach,
  1450. }
  1451. /*
  1452. Get real user ID.
  1453. Available since Linux 1.0.
  1454. On 32-bit platforms available since Linux 2.4.
  1455. */
  1456. getuid :: proc "contextless" () -> Uid {
  1457. when size_of(int) == 8 {
  1458. return cast(Uid) syscall(SYS_getuid)
  1459. } else {
  1460. return cast(Uid) syscall(SYS_getuid32)
  1461. }
  1462. }
  1463. /*
  1464. Read or clear kernel message ring buffer.
  1465. Available since Linux 1.0.
  1466. */
  1467. syslog :: proc "contextless" (act: Syslog_Action, buf: []u8) -> (int, Errno) {
  1468. ret := syscall(SYS_syslog, act, raw_data(buf), len(buf))
  1469. return errno_unwrap(ret, int)
  1470. }
  1471. /*
  1472. Get real group ID.
  1473. Available since Linux 1.0.
  1474. On 32-bit platforms available since Linux 2.4.
  1475. */
  1476. getgid :: proc "contextless" () -> Gid {
  1477. when size_of(int) == 8 {
  1478. return cast(Gid) syscall(SYS_getgid)
  1479. } else {
  1480. return cast(Gid) syscall(SYS_getgid32)
  1481. }
  1482. }
  1483. /*
  1484. Set effective User ID.
  1485. Available since Linux 1.0.
  1486. On 32-bit platforms available since Linux 2.4.
  1487. */
  1488. @(require_results)
  1489. setuid :: proc "contextless" (uid: Uid) -> (Errno) {
  1490. when size_of(int) == 8 {
  1491. ret := syscall(SYS_setuid, uid)
  1492. return Errno(-ret)
  1493. } else {
  1494. ret := syscall(SYS_setuid32, uid)
  1495. return Errno(-ret)
  1496. }
  1497. }
  1498. /*
  1499. Set effective group id.
  1500. If the process is privileged also sets real group id.
  1501. Available since Linux 1.0.
  1502. On 32-bit platforms available since Linux 2.4.
  1503. */
  1504. @(require_results)
  1505. setgid :: proc "contextless" (gid: Gid) -> (Errno) {
  1506. when size_of(int) == 8 {
  1507. ret := syscall(SYS_setgid, gid)
  1508. return Errno(-ret)
  1509. } else {
  1510. ret := syscall(SYS_setgid32, gid)
  1511. return Errno(-ret)
  1512. }
  1513. }
  1514. /*
  1515. Get effective user ID.
  1516. Available since Linux 1.0.
  1517. On 32-bit platforms available since Linux 2.4.
  1518. */
  1519. geteuid :: proc "contextless" () -> Uid {
  1520. when size_of(int) == 8 {
  1521. return cast(Uid) syscall(SYS_geteuid)
  1522. } else {
  1523. return cast(Uid) syscall(SYS_geteuid32)
  1524. }
  1525. }
  1526. /*
  1527. Get effective group ID.
  1528. Available since Linux 1.0.
  1529. On 32-bit platforms available since Linux 2.4.
  1530. */
  1531. getegid :: proc "contextless" () -> Gid {
  1532. when size_of(int) == 8 {
  1533. return cast(Gid) syscall(SYS_getegid)
  1534. } else {
  1535. return cast(Gid) syscall(SYS_getegid32)
  1536. }
  1537. }
  1538. /*
  1539. Set process group.
  1540. Available since Linux 1.0.
  1541. */
  1542. setpgid :: proc "contextless" (pid: Pid, pgid: Pid) -> (Errno) {
  1543. ret := syscall(SYS_setpgid, pid, pgid)
  1544. return Errno(-ret)
  1545. }
  1546. /*
  1547. Get the parent process ID.
  1548. Available since Linux 1.0.
  1549. */
  1550. getppid :: proc "contextless" () -> Pid {
  1551. return cast(Pid) syscall(SYS_getppid)
  1552. }
  1553. /*
  1554. Get process group.
  1555. Available since Linux 1.0.
  1556. */
  1557. getpgrp :: proc "contextless" () -> (Pid, Errno) {
  1558. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1559. ret := syscall(SYS_getpgid, 0)
  1560. return errno_unwrap(ret, Pid)
  1561. } else {
  1562. ret := syscall(SYS_getpgrp)
  1563. return errno_unwrap(ret, Pid)
  1564. }
  1565. }
  1566. /*
  1567. Create a session and set the process group ID.
  1568. Available since Linux 2.0.
  1569. */
  1570. setsid :: proc "contextless" () -> (Pid, Errno) {
  1571. ret := syscall(SYS_setsid)
  1572. return errno_unwrap(ret, Pid)
  1573. }
  1574. /*
  1575. Set real and/or effective user id.
  1576. If any of the arguments is -1, the corresponding id is not changed.
  1577. Available since Linux 1.0.
  1578. On 32-bit platforms available since Linux 2.4.
  1579. */
  1580. @(require_results)
  1581. setreuid :: proc "contextless" (real: Uid, effective: Uid) -> (Errno) {
  1582. when size_of(int) == 8 {
  1583. ret := syscall(SYS_setreuid, real, effective)
  1584. return Errno(-ret)
  1585. } else {
  1586. ret := syscall(SYS_setreuid32, real, effective)
  1587. return Errno(-ret)
  1588. }
  1589. }
  1590. /*
  1591. Set real and/or effective group id.
  1592. If any of the arguments is -1, the corresponding id is not changed.
  1593. Available since Linux 1.0.
  1594. On 32-bit platforms available since Linux 2.4.
  1595. */
  1596. @(require_results)
  1597. setregid :: proc "contextless" (real: Gid, effective: Gid) -> (Errno) {
  1598. when size_of(int) == 8 {
  1599. ret := syscall(SYS_setregid, real, effective)
  1600. return Errno(-ret)
  1601. } else {
  1602. ret := syscall(SYS_setregid32, real, effective)
  1603. return Errno(-ret)
  1604. }
  1605. }
  1606. /*
  1607. Get supplementary group IDs.
  1608. Available since Linux 1.0.
  1609. On 32-bit platforms available since Linux 2.4.
  1610. */
  1611. getgroups :: proc "contextless" (gids: []Gid) -> (int, Errno) {
  1612. when size_of(int) == 8 {
  1613. ret := syscall(SYS_getgroups, len(gids), raw_data(gids))
  1614. return errno_unwrap(ret, int)
  1615. } else {
  1616. ret := syscall(SYS_getgroups32, len(gids), raw_data(gids))
  1617. return errno_unwrap(ret, int)
  1618. }
  1619. }
  1620. /*
  1621. Set supplementary group IDs.
  1622. Available since Linux 1.0.
  1623. On 32-bit platforms available since Linux 2.4.
  1624. */
  1625. setgroups :: proc "contextless" (gids: []Gid) -> (Errno) {
  1626. when size_of(int) == 8 {
  1627. ret := syscall(SYS_setgroups, len(gids), raw_data(gids))
  1628. return Errno(-ret)
  1629. } else {
  1630. ret := syscall(SYS_setgroups32, len(gids), raw_data(gids))
  1631. return Errno(-ret)
  1632. }
  1633. }
  1634. /*
  1635. Set real, effective and/or saved user id.
  1636. If any of the arguments is -1, the corresponding id is not changed.
  1637. Available since Linux 2.2.
  1638. On 32-bit platforms available since Linux 2.4.
  1639. */
  1640. @(require_results)
  1641. setresuid :: proc "contextless" (real: Uid, effective: Uid, saved: Uid) -> (Errno) {
  1642. when size_of(int) == 8 {
  1643. ret := syscall(SYS_setresuid, real, effective, saved)
  1644. return Errno(-ret)
  1645. } else {
  1646. ret := syscall(SYS_setresuid32, real, effective, saved)
  1647. return Errno(-ret)
  1648. }
  1649. }
  1650. /*
  1651. Get real, effective and saved user id.
  1652. Available since Linux 2.2.
  1653. On 32-bit platforms available since Linux 2.4.
  1654. */
  1655. getresuid :: proc "contextless" (real: ^Uid, effective: ^Uid, saved: ^Uid) -> (Errno) {
  1656. when size_of(int) == 8 {
  1657. ret := syscall(SYS_getresuid, cast(rawptr) real, cast(rawptr) effective, cast(rawptr) saved)
  1658. return Errno(-ret)
  1659. } else {
  1660. ret := syscall(SYS_getresuid32, cast(rawptr) real, cast(rawptr) effective, cast(rawptr) saved)
  1661. return Errno(-ret)
  1662. }
  1663. }
  1664. /*
  1665. Set real, effective and/or saved group id.
  1666. If any of the arguments is -1, the corresponding id is not changed.
  1667. Available since Linux 2.2.
  1668. On 32-bit platforms available since Linux 2.4.
  1669. */
  1670. @(require_results)
  1671. setresgid :: proc "contextless" (real: Gid, effective: Gid, saved: Uid) -> (Errno) {
  1672. when size_of(int) == 8 {
  1673. ret := syscall(SYS_setresgid, real, effective, saved)
  1674. return Errno(-ret)
  1675. } else {
  1676. ret := syscall(SYS_setresgid32, real, effective, saved)
  1677. return Errno(-ret)
  1678. }
  1679. }
  1680. /*
  1681. Get real, effective and saved group id.
  1682. Available since Linux 2.2.
  1683. On 32-bit platforms available since Linux 2.4.
  1684. */
  1685. getresgid :: proc "contextless" (real: ^Gid, effective: ^Gid, saved: ^Gid) -> (Errno) {
  1686. when size_of(int) == 8 {
  1687. ret := syscall(SYS_getresgid, cast(rawptr) real, cast(rawptr) effective, cast(rawptr) saved)
  1688. return Errno(-ret)
  1689. } else {
  1690. ret := syscall(SYS_getresgid32, cast(rawptr) real, cast(rawptr) effective, cast(rawptr) saved)
  1691. return Errno(-ret)
  1692. }
  1693. }
  1694. /*
  1695. Get process group.
  1696. Available since Linux 1.0.
  1697. */
  1698. getpgid :: proc "contextless" (pid: Pid) -> (Pid, Errno) {
  1699. ret := syscall(SYS_getpgid, pid)
  1700. return errno_unwrap(ret, Pid)
  1701. }
  1702. /*
  1703. Get session ID of the calling process.
  1704. Available since Linux 2.0.
  1705. */
  1706. getsid :: proc "contextless" (pid: Pid) -> (Pid, Errno) {
  1707. ret := syscall(SYS_getsid, pid)
  1708. return errno_unwrap(ret, Pid)
  1709. }
  1710. // TODO(flysand): capget
  1711. // TODO(flysand): capset
  1712. /*
  1713. Examine pending signals.
  1714. Available since Linux 2.2.
  1715. */
  1716. rt_sigpending :: proc "contextless" (sigs: ^Sig_Set) -> Errno {
  1717. ret := syscall(SYS_rt_sigpending, sigs, size_of(Sig_Set))
  1718. return Errno(-ret)
  1719. }
  1720. /*
  1721. Synchronously wait for queued signals.
  1722. Available since Linux 2.2.
  1723. */
  1724. rt_sigtimedwait :: proc "contextless" (sigs: ^Sig_Set, info: ^Sig_Info, time_sus: ^Time_Spec) -> (Signal, Errno) {
  1725. ret := syscall(SYS_rt_sigtimedwait, sigs, info, time_sus, size_of(Sig_Set))
  1726. return errno_unwrap(ret, Signal)
  1727. }
  1728. /*
  1729. Send signal information to a process.
  1730. Available since Linux 2.2.
  1731. */
  1732. rt_sigqueueinfo :: proc "contextless" (pid: Pid, sig: Signal, si: ^Sig_Info) -> (Errno) {
  1733. ret := syscall(SYS_rt_sigqueueinfo, pid, sig, si)
  1734. return Errno(-ret)
  1735. }
  1736. /*
  1737. Replace the signal mask for a value with the new mask until a signal is received.
  1738. Available since Linux 2.2.
  1739. */
  1740. rt_sigsuspend :: proc "contextless" (sigset: ^Sig_Set) -> Errno {
  1741. ret := syscall(SYS_rt_sigsuspend, sigset, size_of(Sig_Set))
  1742. return Errno(-ret)
  1743. }
  1744. /*
  1745. Set or get signal stack context.
  1746. Available since Linux 2.2.
  1747. */
  1748. sigaltstack :: proc "contextless" (stack: ^Sig_Stack, old_stack: ^Sig_Stack) -> (Errno) {
  1749. ret := syscall(SYS_sigaltstack, stack, old_stack)
  1750. return Errno(-ret)
  1751. }
  1752. /*
  1753. Create a special or ordinary file.
  1754. `mode` parameter contains both the the file mode and the type of the node to create.
  1755. -> Add one of S_IFSOCK, S_IFBLK, S_IFFIFO, S_IFCHR to mode.
  1756. Available since Linux 1.0.
  1757. On ARM64 available since Linux 2.6.16.
  1758. */
  1759. mknod :: proc "contextless" (name: cstring, mode: Mode, dev: Dev) -> (Errno) {
  1760. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  1761. ret := syscall(SYS_mknodat, AT_FDCWD, cast(rawptr) name, transmute(u32) mode, cast(uint) dev)
  1762. return Errno(-ret)
  1763. } else {
  1764. ret := syscall(SYS_mknod, cast(rawptr) name, transmute(u32) mode, cast(uint) dev)
  1765. return Errno(-ret)
  1766. }
  1767. }
  1768. /*
  1769. Set the process execution domain.
  1770. Available since Linux 1.2.
  1771. */
  1772. personality :: proc "contextless" (personality: uint) -> (uint, Errno) {
  1773. ret := syscall(SYS_personality, personality)
  1774. return errno_unwrap(ret, uint)
  1775. }
  1776. // TODO(flysand): ustat
  1777. /*
  1778. Query information about filesystem.
  1779. Available since Linux 1.0.
  1780. For 32-bit systems a different syscall is used that became available since 2.6.
  1781. */
  1782. statfs :: proc "contextless" (path: cstring, statfs: ^Stat_FS) -> (Errno) {
  1783. when size_of(int) == 8 {
  1784. ret := syscall(SYS_statfs, transmute(uintptr) path, statfs)
  1785. return Errno(-ret)
  1786. } else {
  1787. ret := syscall(SYS_statfs64, cast(rawptr) path, size_of(Stat_FS), statfs)
  1788. return Errno(-ret)
  1789. }
  1790. }
  1791. /*
  1792. Query information about filesystem by file descriptor.
  1793. Available since Linux 1.0.
  1794. For 32-bit systems a different syscall is used that became available since 2.6.
  1795. */
  1796. fstatfs :: proc "contextless" (fd: Fd, statfs: ^Stat_FS) -> (Errno) {
  1797. when size_of(int) == 8 {
  1798. ret := syscall(SYS_statfs, fd, statfs)
  1799. return Errno(-ret)
  1800. } else {
  1801. ret := syscall(SYS_statfs64, fd, size_of(Stat_FS), statfs)
  1802. return Errno(-ret)
  1803. }
  1804. }
  1805. // TODO(flysand): sysfs
  1806. /*
  1807. Get priority on user, process group or process.
  1808. Available since Linux 1.0.
  1809. */
  1810. getpriority :: proc "contextless" (which: Priority_Which, who: i32) -> (i32, Errno) {
  1811. ret := syscall(SYS_getpriority, which, who)
  1812. prio, err := errno_unwrap(ret, i32)
  1813. // NOTE(flysand): getpriority will return `20 - priority` to avoid returning
  1814. // negative priorities as errors
  1815. prio = 20 - prio
  1816. return prio, err
  1817. }
  1818. /*
  1819. Set priority on user, process group or process.
  1820. Available since Linux 1.0.
  1821. */
  1822. setpriority :: proc "contextless" (which: Priority_Which, who: i32, prio: i32) -> (Errno) {
  1823. ret := syscall(SYS_setpriority, which, who, prio)
  1824. return Errno(-ret)
  1825. }
  1826. // TODO(flysand): sched_setparam
  1827. // TODO(flysand): sched_getparam
  1828. // TODO(flysand): sched_setscheduler
  1829. // TODO(flysand): sched_getscheduler
  1830. // TODO(flysand): sched_get_priority_max
  1831. // TODO(flysand): sched_get_priority_min
  1832. // TODO(flysand): sched_rr_get_interval
  1833. /*
  1834. Lock and memory.
  1835. Available since Linux 2.0.
  1836. If flags specified, available since Linux 4.4.
  1837. */
  1838. mlock :: proc "contextless" (addr: rawptr, size: uint, flags: MLock_Flags = {}) -> (Errno) {
  1839. // Pretty darn recent syscall, better call simpler version if we can
  1840. if flags > {} {
  1841. ret := syscall(SYS_mlock2, addr, size, transmute(i32) flags)
  1842. return Errno(-ret)
  1843. } else {
  1844. ret := syscall(SYS_mlock, addr, size)
  1845. return Errno(-ret)
  1846. }
  1847. }
  1848. /*
  1849. Unlock memory.
  1850. Available since Linux 2.0.
  1851. */
  1852. munlock :: proc "contextless" (addr: rawptr, size: uint) -> (Errno) {
  1853. ret := syscall(SYS_munlock, addr, size)
  1854. return Errno(-ret)
  1855. }
  1856. /*
  1857. Lock all memory.
  1858. Available since Linux 2.0.
  1859. */
  1860. mlockall :: proc "contextless" (flags: MLock_Flags = {}) -> (Errno) {
  1861. ret := syscall(SYS_mlockall, transmute(i32) flags)
  1862. return Errno(-ret)
  1863. }
  1864. /*
  1865. Unlock all memory.
  1866. Available since Linux 2.0.
  1867. */
  1868. munlockall :: proc "contextless" () -> (Errno) {
  1869. ret := syscall(SYS_munlockall)
  1870. return Errno(-ret)
  1871. }
  1872. // TODO(flysand): vhangup
  1873. // TODO(flysand): modify_ldt
  1874. // TODO(flysand): pivot_root
  1875. // TODO(flysand): _sysctl
  1876. // TODO(flysand): prctl
  1877. // TODO(flysand): arch_prctl
  1878. // TODO(flysand): adj_timex
  1879. /*
  1880. Set limits on resources.
  1881. Available since Linux 1.0.
  1882. */
  1883. setrlimit :: proc "contextless" (kind: RLimit_Kind, resource: ^RLimit) -> (Errno) {
  1884. ret := syscall(SYS_setrlimit, kind, resource)
  1885. return Errno(-ret)
  1886. }
  1887. // TODO(flysand): sync
  1888. // TODO(flysand): acct
  1889. // TODO(flysand): settimeofday
  1890. // TODO(flysand): mount
  1891. // TODO(flysand): umount2
  1892. // TODO(flysand): swapon
  1893. // TODO(flysand): swapoff
  1894. // TODO(flysand): reboot
  1895. /*
  1896. Set hostname.
  1897. Note: to get the host name, use `uname` syscall.
  1898. Available since Linux 1.0.
  1899. */
  1900. sethostname :: proc "contextless" (hostname: string) -> (Errno) {
  1901. ret := syscall(SYS_sethostname, raw_data(hostname), len(hostname))
  1902. return Errno(-ret)
  1903. }
  1904. /*
  1905. Set domain name.
  1906. Note: to get the domain name, use `uname` syscall.
  1907. Available since Linux 2.2.
  1908. */
  1909. setdomainname :: proc "contextless" (name: string) -> (Errno) {
  1910. ret := syscall(SYS_setdomainname, raw_data(name), len(name))
  1911. return Errno(-ret)
  1912. }
  1913. // TODO(flysand): iopl
  1914. // TODO(flysand): ioperm
  1915. // TODO(flysand): init_module
  1916. // TODO(flysand): delete_module
  1917. // TODO(flysand): quotactl
  1918. // TODO(flysand): nfsservctl
  1919. // TODO(flysand): getpmsg
  1920. // TODO(flysand): putpmsg
  1921. // TODO(flysand): afs_syscall
  1922. // TODO(flysand): tuxcall
  1923. // TODO(flysand): security
  1924. /*
  1925. Returns the thread ID of the current process
  1926. This is what the kernel calls "pid".
  1927. Let me insert a tiny rant here, this terminology is confusing:
  1928. sometimes pid refers to a thread, and other times it refers
  1929. to a thread group (process group?)
  1930. Anyway, this syscall is available since Linux 1.0
  1931. */
  1932. gettid :: proc "contextless" () -> Pid {
  1933. return cast(Pid) syscall(SYS_gettid)
  1934. }
  1935. // TODO(flysand): readahead
  1936. // TODO(flysand): setxattr
  1937. // TODO(flysand): lsetxattr
  1938. // TODO(flysand): fsetxattr
  1939. // TODO(flysand): getxattr
  1940. // TODO(flysand): lgetxattr
  1941. // TODO(flysand): fgetxattr
  1942. // TODO(flysand): listxattr
  1943. // TODO(flysand): llistxattr
  1944. // TODO(flysand): flistxattr
  1945. // TODO(flysand): removexattr
  1946. // TODO(flysand): lremovexattr
  1947. // TODO(flysand): fremovexattr
  1948. /*
  1949. Get current time in seconds.
  1950. Available since Linux 1.0.
  1951. */
  1952. time :: proc "contextless" (tloc: ^uint) -> (Errno) {
  1953. when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
  1954. ret := syscall(SYS_time, tloc)
  1955. return Errno(-ret)
  1956. } else {
  1957. ts: Time_Spec
  1958. ret := syscall(SYS_clock_gettime, Clock_Id.REALTIME, &ts)
  1959. tloc^ = ts.time_sec
  1960. return Errno(-ret)
  1961. }
  1962. }
  1963. /*
  1964. Wait on a futex until it's signaled.
  1965. */
  1966. futex_wait :: proc "contextless" (futex: ^Futex, op: Futex_Wait_Type, flags: Futex_Flags, val: u32, timeout: ^Time_Spec = nil) -> (Errno) {
  1967. futex_flags := cast(u32) op + transmute(u32) flags
  1968. ret := syscall(SYS_futex, futex, futex_flags, val, timeout)
  1969. return Errno(-ret)
  1970. }
  1971. /*
  1972. Wake up other threads on a futex
  1973. n_wakeup specifies the number of processes to wakeup. Specify max(i32) to wake up all processes waiting
  1974. */
  1975. futex_wake :: proc "contextless" (futex: ^Futex, op: Futex_Wake_Type, flags: Futex_Flags, n_wakeup: i32) -> (int, Errno) {
  1976. futex_flags := cast(u32) op + transmute(u32) flags
  1977. ret := syscall(SYS_futex, futex, futex_flags, n_wakeup)
  1978. return errno_unwrap(ret, int)
  1979. }
  1980. // NOTE(flysand): futex_fd is racy, so not implemented
  1981. /*
  1982. Requeues processes waiting on futex `futex` to wait on futex `requeue_futex`
  1983. `requeue_threshold` specifies the maximum amount of waiters to wake up, the rest of the waiters will be requeued
  1984. `requeue_max` specifies the maximum amount of waiters that are required at `requeue_futex`
  1985. The operation blocks until the `requeue_max` requirement is satisfied
  1986. If the value of the mutex is not equal to `val`, fails with EAGAIN before any further checks
  1987. Returns the total number of waiters that have been woken up plus the number of waiters requeued.
  1988. */
  1989. futex_cmp_requeue :: proc "contextless" (futex: ^Futex, op: Futex_Cmp_Requeue_Type, flags: Futex_Flags, requeue_threshold: u32,
  1990. requeue_max: i32, requeue_futex: ^Futex, val: i32) -> (int, Errno) {
  1991. futex_flags := cast(u32) op + transmute(u32) flags
  1992. ret := syscall(SYS_futex, futex, futex_flags, requeue_threshold, requeue_max, requeue_futex, val)
  1993. return errno_unwrap(ret, int)
  1994. }
  1995. /*
  1996. See `futex_cmp_requeue`, this function does the same thing but doesn't check the value of the futex.
  1997. Returns the total number of waiters that have been woken up.
  1998. */
  1999. futex_requeue :: proc "contextless" (futex: ^Futex, op: Futex_Requeue_Type, flags: Futex_Flags, requeue_threshold: u32,
  2000. requeue_max: i32, requeue_futex: ^Futex) -> (int, Errno) {
  2001. futex_flags := cast(u32) op + transmute(u32) flags
  2002. ret := syscall(SYS_futex, futex, futex_flags, requeue_threshold, requeue_max, requeue_futex)
  2003. return errno_unwrap(ret, int)
  2004. }
  2005. /*
  2006. Okay, for this one, see the man pages, the description for it is pretty long and very specific. It's sole.
  2007. purpose is to allow implementing conditional values sync primitive, it seems like.
  2008. */
  2009. futex_wake_op :: proc "contextless" (futex: ^Futex, op: Futex_Wake_Op_Type, flags: Futex_Flags, wakeup: i32,
  2010. dst_wakeup, dst: ^Futex, futex_op: u32) -> (int, Errno) {
  2011. futex_flags := cast(u32) op + transmute(u32) flags
  2012. ret := syscall(SYS_futex, futex, futex_flags, wakeup, dst_wakeup, dst, futex_op)
  2013. return errno_unwrap(ret, int)
  2014. }
  2015. /*
  2016. Same as wait, but mask specifies bits that must be equal for the mutex to wake up.
  2017. */
  2018. futex_wait_bitset :: proc "contextless" (futex: ^Futex, op: Futex_Wait_Bitset_Type, flags: Futex_Flags, val: u32,
  2019. timeout: ^Time_Spec, mask: u32) -> (int, Errno) {
  2020. futex_flags := cast(u32) op + transmute(u32) flags
  2021. ret := syscall(SYS_futex, futex, futex_flags, val, timeout, 0, mask)
  2022. return errno_unwrap(ret, int)
  2023. }
  2024. /*
  2025. Wake up on bitset.
  2026. */
  2027. futex_wake_bitset :: proc "contextless" (futex: ^Futex, op: Futex_Wake_Bitset_Type, flags: Futex_Flags, n_wakeup: u32, mask: u32) -> (int, Errno) {
  2028. futex_flags := cast(u32) op + transmute(u32) flags
  2029. ret := syscall(SYS_futex, futex, futex_flags, n_wakeup, 0, 0, mask)
  2030. return errno_unwrap(ret, int)
  2031. }
  2032. // TODO(flysand): Priority inheritance (PI) futicees
  2033. futex :: proc{
  2034. futex_wait,
  2035. futex_wake,
  2036. futex_cmp_requeue,
  2037. futex_requeue,
  2038. futex_wake_op,
  2039. futex_wait_bitset,
  2040. futex_wake_bitset,
  2041. }
  2042. // TODO(flysand): sched_setaffinity
  2043. // TODO(flysand): sched_getaffinity
  2044. // TODO(flysand): set_thread_area
  2045. // TODO(flysand): io_setup
  2046. // TODO(flysand): io_destroy
  2047. // TODO(flysand): io_getevents
  2048. // TODO(flysand): io_submit
  2049. // TODO(flysand): io_cancel
  2050. // TODO(flysand): get_thread_area
  2051. // TODO(flysand): lookup_dcookie
  2052. /*
  2053. Open an epoll file descriptor.
  2054. The `size` argument is ignored but must be greater than zero.
  2055. Available since Linux 2.6.
  2056. */
  2057. epoll_create :: proc(size: i32 = 1) -> (Fd, Errno) {
  2058. when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
  2059. ret := syscall(SYS_epoll_create, i32(1))
  2060. return errno_unwrap(ret, Fd)
  2061. } else {
  2062. ret := syscall(SYS_epoll_create1, i32(0))
  2063. return errno_unwrap(ret, Fd)
  2064. }
  2065. }
  2066. // TODO(flysand): remap_file_pages
  2067. /*
  2068. Set the address of the futex that's gonna be waken when.
  2069. current thread terminates.
  2070. Available since Linux 2.6.
  2071. */
  2072. set_tid_address :: proc "contextless" (tidptr: ^u32) {
  2073. syscall(SYS_set_tid_address, tidptr)
  2074. }
  2075. // TODO(flysand): semtimedop
  2076. // TODO(flysand): fadvise64
  2077. /*
  2078. Create POSIX per-process timer.
  2079. Available since Linux 2.6.
  2080. */
  2081. timer_create :: proc "contextless" (clock_id: Clock_Id, sigevent: ^Sig_Event, timer: ^Timer) -> (Errno) {
  2082. ret := syscall(SYS_timer_create, clock_id, sigevent, timer)
  2083. return Errno(-ret)
  2084. }
  2085. /*
  2086. Get the state of the POSIX per-process timer.
  2087. Available since Linux 2.6.
  2088. */
  2089. timer_gettime :: proc "contextless" (timer: Timer, curr_value: ^ITimer_Spec) -> (Errno) {
  2090. ret := syscall(SYS_timer_gettime, timer, curr_value)
  2091. return Errno(-ret)
  2092. }
  2093. /*
  2094. Arm/disarm the state of the POSIX per-process timer.
  2095. Available since Linux 2.6.
  2096. */
  2097. timer_settime :: proc "contextless" (timer: Timer, flags: ITimer_Flags, #no_alias new_value, old_value: ^ITimer_Spec) -> (Errno) {
  2098. ret := syscall(SYS_timer_settime, timer, transmute(u32) flags, new_value, old_value)
  2099. return Errno(-ret)
  2100. }
  2101. /*
  2102. Get overrun count of the POSIX per-process timer.
  2103. Available since Linux 2.6.
  2104. */
  2105. timer_getoverrun :: proc "contextless" (timer: Timer) -> (int, Errno) {
  2106. ret := syscall(SYS_timer_getoverrun, timer)
  2107. return errno_unwrap(ret, int)
  2108. }
  2109. /*
  2110. Delete a POSIX per-process timer.
  2111. Available since Linux 2.6.
  2112. */
  2113. timer_delete :: proc "contextless" (timer: Timer) -> (Errno) {
  2114. ret := syscall(SYS_timer_delete, timer)
  2115. return Errno(-ret)
  2116. }
  2117. /*
  2118. Set the time of the specified clock.
  2119. Available since Linux 2.6.
  2120. */
  2121. clock_settime :: proc "contextless" (clock: Clock_Id, ts: ^Time_Spec) -> (Errno) {
  2122. ret := syscall(SYS_clock_settime, clock, ts)
  2123. return Errno(-ret)
  2124. }
  2125. /*
  2126. Retrieve the time of the specified clock.
  2127. Available since Linux 2.6.
  2128. */
  2129. clock_gettime :: proc "contextless" (clock: Clock_Id) -> (ts: Time_Spec, err: Errno) {
  2130. ret := syscall(SYS_clock_gettime, clock, &ts)
  2131. err = Errno(-ret)
  2132. return
  2133. }
  2134. /*
  2135. Finds the resolution of the specified clock.
  2136. Available since Linux 2.6.
  2137. */
  2138. clock_getres :: proc "contextless" (clock: Clock_Id) -> (res: Time_Spec, err: Errno) {
  2139. ret := syscall(SYS_clock_getres, clock, &res)
  2140. err = Errno(-ret)
  2141. return
  2142. }
  2143. /*
  2144. Sleep for an interval specified with nanosecond precision.
  2145. Available since Linux 2.6.
  2146. */
  2147. clock_nanosleep :: proc "contextless" (clock: Clock_Id, flags: ITimer_Flags, request: ^Time_Spec, remain: ^Time_Spec) -> (Errno) {
  2148. ret := syscall(SYS_clock_nanosleep, clock, transmute(u32) flags, request, remain)
  2149. return Errno(-ret)
  2150. }
  2151. /*
  2152. Exit the thread group.
  2153. Available since Linux 2.6.
  2154. */
  2155. exit_group :: proc "contextless" (code: i32) -> ! {
  2156. syscall(SYS_exit_group, code)
  2157. unreachable()
  2158. }
  2159. /*
  2160. Wait for an I/O event on an epoll file descriptor.
  2161. `timeout` is specified in milliseconds.
  2162. Available since Linux 2.6.
  2163. */
  2164. epoll_wait :: proc(epfd: Fd, events: [^]EPoll_Event, count: i32, timeout: i32) -> (i32, Errno) {
  2165. when ODIN_ARCH != .arm64 && ODIN_ARCH != .riscv64 {
  2166. ret := syscall(SYS_epoll_wait, epfd, events, count, timeout)
  2167. return errno_unwrap(ret, i32)
  2168. } else {
  2169. // Convert milliseconds to nanosecond timespec
  2170. timeout_ns := Time_Spec {
  2171. time_sec = uint(timeout * 1000),
  2172. time_nsec = 0,
  2173. }
  2174. ret := syscall(SYS_epoll_pwait, epfd, events, count, &timeout_ns, rawptr(nil))
  2175. return errno_unwrap(ret, i32)
  2176. }
  2177. }
  2178. /*
  2179. Control interface for an epoll file descriptor.
  2180. Available since Linux 2.6.
  2181. */
  2182. epoll_ctl :: proc(epfd: Fd, op: EPoll_Ctl_Opcode, fd: Fd, event: ^EPoll_Event) -> (Errno) {
  2183. ret := syscall(SYS_epoll_ctl, epfd, op, fd, event)
  2184. return Errno(-ret)
  2185. }
  2186. /*
  2187. Send a signal to a specific thread in a thread group.
  2188. Available since Linux 2.6.
  2189. */
  2190. tgkill :: proc "contextless" (tgid, tid: Pid, sig: Signal) -> (Errno) {
  2191. ret := syscall(SYS_tgkill, tgid, tid, sig)
  2192. return Errno(-ret)
  2193. }
  2194. // TODO(flysand): utimes
  2195. // TODO(flysand): vserver
  2196. // TODO(flysand): mbind
  2197. // TODO(flysand): set_mempolicy
  2198. // TODO(flysand): get_mempolicy
  2199. // TODO(flysand): mq_open
  2200. // TODO(flysand): mq_unlink
  2201. // TODO(flysand): mq_timedsend
  2202. // TODO(flysand): mq_timedreceive
  2203. // TODO(flysand): mq_notify
  2204. // TODO(flysand): mq_getsetattr
  2205. // TODO(flysand): kexec_load
  2206. /*
  2207. Wait on process, process group or pid file descriptor.
  2208. Available since Linux 2.6.10.
  2209. */
  2210. waitid :: proc "contextless" (id_type: Id_Type, id: Id, sig_info: ^Sig_Info, options: Wait_Options, rusage: ^RUsage) -> (Errno) {
  2211. ret := syscall(SYS_waitid, id_type, id, sig_info, transmute(i32) options, rusage)
  2212. return Errno(-ret)
  2213. }
  2214. // TODO(flysand): add_key
  2215. // TODO(flysand): request_key
  2216. // TODO(flysand): keyctl
  2217. // TODO(flysand): ioprio_set
  2218. // TODO(flysand): ioprio_get
  2219. inotify_init :: proc "contextless" () -> (Fd, Errno) {
  2220. when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 {
  2221. ret := syscall(SYS_inotify_init1, 0)
  2222. return errno_unwrap(ret, Fd)
  2223. } else {
  2224. ret := syscall(SYS_inotify_init)
  2225. return errno_unwrap(ret, Fd)
  2226. }
  2227. }
  2228. inotify_init1 :: proc "contextless" (flags: Inotify_Init_Flags) -> (Fd, Errno) {
  2229. ret := syscall(SYS_inotify_init1, transmute(i32)flags)
  2230. return errno_unwrap(ret, Fd)
  2231. }
  2232. inotify_add_watch :: proc "contextless" (fd: Fd, pathname: cstring, mask: Inotify_Event_Mask) -> (Wd, Errno) {
  2233. ret := syscall(SYS_inotify_add_watch, fd, transmute(uintptr) pathname, transmute(u32) mask)
  2234. return errno_unwrap(ret, Wd)
  2235. }
  2236. inotify_rm_watch :: proc "contextless" (fd: Fd, wd: Wd) -> (Errno) {
  2237. ret := syscall(SYS_inotify_rm_watch, fd, wd)
  2238. return Errno(-ret)
  2239. }
  2240. // TODO(flysand): migrate_pages
  2241. /*
  2242. Open file at the specified file descriptor.
  2243. Available since Linux 2.6.16.
  2244. */
  2245. openat :: proc "contextless" (fd: Fd, name: cstring, flags: Open_Flags, mode: Mode = {}) -> (Fd, Errno) {
  2246. ret := syscall(SYS_openat, fd, transmute(uintptr) name, transmute(u32) flags, transmute(u32) mode)
  2247. return errno_unwrap(ret, Fd)
  2248. }
  2249. /*
  2250. Create a directory relative to specified dirfd.
  2251. Available since Linux 2.6.16.
  2252. */
  2253. mkdirat :: proc "contextless" (dirfd: Fd, name: cstring, mode: Mode) -> (Errno) {
  2254. ret := syscall(SYS_mkdirat, dirfd, cast(rawptr) name, transmute(u32) mode)
  2255. return Errno(-ret)
  2256. }
  2257. /*
  2258. Create a special or ordinary file wrt given directory specified by dirfd.
  2259. Available since Linux 2.6.16.
  2260. */
  2261. mknodat :: proc "contextless" (dirfd: Fd, name: cstring, mode: Mode, dev: Dev) -> (Errno) {
  2262. ret := syscall(SYS_mknodat, dirfd, cast(rawptr) name, transmute(u32) mode, cast(uint) dev)
  2263. return Errno(-ret)
  2264. }
  2265. /*
  2266. Change the ownership of the file specified relative to directory.
  2267. Available since Linux 2.6.16.
  2268. */
  2269. fchownat :: proc "contextless" (dirfd: Fd, name: cstring, uid: Uid, gid: Gid) -> (Errno) {
  2270. ret := syscall(SYS_fchownat, dirfd, cast(rawptr) name, uid, gid)
  2271. return Errno(-ret)
  2272. }
  2273. /*
  2274. Get information about a file at a specific directory.
  2275. Available since Linux 2.6.16.
  2276. */
  2277. fstatat :: proc "contextless" (dirfd: Fd, name: cstring, stat: ^Stat, flags: FD_Flags) -> (Errno) {
  2278. when size_of(int) == 4 {
  2279. ret := syscall(SYS_fstatat64, dirfd, cast(rawptr) name, stat, transmute(i32) flags)
  2280. return Errno(-ret)
  2281. } else when ODIN_ARCH == .amd64 {
  2282. ret := syscall(SYS_newfstatat, dirfd, cast(rawptr) name, stat, transmute(i32) flags)
  2283. return Errno(-ret)
  2284. } else {
  2285. ret := syscall(SYS_fstatat, dirfd, cast(rawptr) name, stat, transmute(i32) flags)
  2286. return Errno(-ret)
  2287. }
  2288. }
  2289. /*
  2290. Remove a directory entry relative to a directory file descriptor.
  2291. Available since Linux 2.6.16.
  2292. */
  2293. unlinkat :: proc "contextless" (dirfd: Fd, name: cstring, flags: FD_Flags) -> (Errno) {
  2294. ret := syscall(SYS_unlinkat, dirfd, cast(rawptr) name, transmute(i32) flags)
  2295. return Errno(-ret)
  2296. }
  2297. /*
  2298. Rename the file with names relative to the specified dirfd's.
  2299. Available since Linux 2.6.16.
  2300. */
  2301. renameat :: proc "contextless" (oldfd: Fd, old: cstring, newfd: Fd, new: cstring) -> (Errno) {
  2302. ret := syscall(SYS_renameat, oldfd, cast(rawptr) old, newfd, cast(rawptr) new)
  2303. return Errno(-ret)
  2304. }
  2305. /*
  2306. Creates a hard link on a file relative to specified dirfd.
  2307. Available since Linux 2.6.16.
  2308. */
  2309. linkat :: proc "contextless" (target_dirfd: Fd, oldpath: cstring, link_dirfd: Fd, link: cstring, flags: FD_Flags) -> (Errno) {
  2310. ret := syscall(SYS_linkat, target_dirfd, cast(rawptr) oldpath, link_dirfd, cast(rawptr) link, transmute(i32) flags)
  2311. return Errno(-ret)
  2312. }
  2313. /*
  2314. Create a symbolic link at specified dirfd.
  2315. Available since Linux 2.6.16.
  2316. */
  2317. symlinkat :: proc "contextless" (target: cstring, dirfd: Fd, linkpath: cstring) -> (Errno) {
  2318. ret := syscall(SYS_symlinkat, cast(rawptr) target, dirfd, cast(rawptr) linkpath)
  2319. return Errno(-ret)
  2320. }
  2321. /*
  2322. Read the value of a symbolic link at given dirfd.
  2323. Available since Linux 2.6.16.
  2324. */
  2325. readlinkat :: proc "contextless" (dirfd: Fd, name: cstring, buf: []u8) -> (int, Errno) {
  2326. ret := syscall(SYS_readlinkat, dirfd, cast(rawptr) name, raw_data(buf), len(buf))
  2327. return errno_unwrap(ret, int)
  2328. }
  2329. /*
  2330. Change the file mode at a specified file descriptor.
  2331. Available since Linux 2.6.16.
  2332. */
  2333. fchmodat :: proc "contextless" (dirfd: Fd, name: cstring, mode: Mode, flags: FD_Flags) -> (Errno) {
  2334. ret := syscall(SYS_fchmodat, cast(rawptr) name, transmute(u32) mode, transmute(i32) flags)
  2335. return Errno(-ret)
  2336. }
  2337. /*
  2338. Checks the user permissions for a file at specified dirfd.
  2339. Available since Linux 2.6.16.
  2340. */
  2341. faccessat :: proc "contextless" (dirfd: Fd, name: cstring, mode: Mode = F_OK) -> (Errno) {
  2342. ret := syscall(SYS_faccessat, dirfd, cast(rawptr) name, transmute(u32) mode)
  2343. return Errno(-ret)
  2344. }
  2345. /*
  2346. Wait for events on a file descriptor.
  2347. Available since Linux 2.6.16.
  2348. */
  2349. ppoll :: proc "contextless" (fds: []Poll_Fd, timeout: ^Time_Spec, sigmask: ^Sig_Set) -> (i32, Errno) {
  2350. ret := syscall(SYS_ppoll, raw_data(fds), len(fds), timeout, sigmask, size_of(Sig_Set))
  2351. return errno_unwrap(ret, i32)
  2352. }
  2353. // TODO(flysand): unshare
  2354. // TODO(flysand): set_robust_list
  2355. // TODO(flysand): get_robust_list
  2356. /*
  2357. Transfer the data between file descriptors.
  2358. Available since Linux 2.6.17.
  2359. */
  2360. splice :: proc "contextless" (fd_in: Fd, off_in: ^i64, fd_out: Fd, off_out: ^i64, len: uint, flags: Splice_Flags) -> (int, Errno) {
  2361. ret := syscall(SYS_splice, fd_in, off_in, fd_out, off_out, len, transmute(u32) flags)
  2362. return errno_unwrap(ret, int)
  2363. }
  2364. /*
  2365. Transfer the data between file descriptors.
  2366. Available since Linux 2.6.16.
  2367. */
  2368. tee :: proc "contextless" (fd_in: Fd, fd_out: Fd, len: uint, flags: Splice_Flags) -> (int, Errno) {
  2369. ret := syscall(SYS_tee, fd_in, fd_out, len, transmute(u32) flags)
  2370. return errno_unwrap(ret, int)
  2371. }
  2372. // TODO(flysand): sync_file_range
  2373. // TODO(flysand): vmsplice
  2374. // TODO(flysand): move_pages
  2375. /*
  2376. Change file timestamps with nanosecond precision.
  2377. **utimes** must point to an array of two `Time_Spec`'s. The "utime" is the
  2378. last access time, the second is last modification time.
  2379. Available since Linux 2.6.22.
  2380. */
  2381. utimensat :: proc "contextless" (dirfd: Fd, name: cstring, utimes: [^]Time_Spec, flags: FD_Flags) -> (Errno) {
  2382. ret := syscall(SYS_utimensat, dirfd, cast(rawptr) name, utimes, transmute(i32) flags)
  2383. return Errno(-ret)
  2384. }
  2385. /*
  2386. Wait for an I/O event on an epoll file descriptor.
  2387. Available since Linux 2.6.
  2388. */
  2389. epoll_pwait :: proc(epfd: Fd, events: [^]EPoll_Event, count: i32, timeout: i32, sigmask: ^Sig_Set) -> (i32, Errno) {
  2390. ret := syscall(SYS_epoll_pwait, epfd, events, count, timeout, sigmask)
  2391. return errno_unwrap(ret, i32)
  2392. }
  2393. // TODO(flysand): signalfd
  2394. // TODO(flysand): timerfd_create
  2395. // TODO(flysand): eventfd
  2396. // TODO(flysand): fallocate
  2397. // TODO(flysand): timerfd_settime
  2398. // TODO(flysand): timerfd_gettime
  2399. // TODO(flysand): accept4
  2400. // TODO(flysand): signalfd4
  2401. // TODO(flysand): eventfd2
  2402. epoll_create1 :: proc(flags: EPoll_Flags) -> (Fd, Errno) {
  2403. ret := syscall(SYS_epoll_create1, transmute(i32) flags)
  2404. return errno_unwrap(ret, Fd)
  2405. }
  2406. /*
  2407. Adjust an existing file descriptor to point to the same file as `old`.
  2408. In addition to dup2 allows to pass O_CLOEXEC flag.
  2409. Available since Linux 2.6.27.
  2410. */
  2411. dup3 :: proc "contextless" (old: Fd, new: Fd, flags: Open_Flags) -> (Fd, Errno) {
  2412. ret := syscall(SYS_dup3, old, new, transmute(i32) flags)
  2413. return errno_unwrap(ret, Fd)
  2414. }
  2415. // TODO(flysand): inotify_init1
  2416. // TODO(flysand): preadv
  2417. // TODO(flysand): pwritev
  2418. /// Send signal information to a thread
  2419. /// Available since Linux 2.2
  2420. rt_tgsigqueueinfo :: proc "contextless" (tgid: Pid, pid: Pid, sig: Signal, si: ^Sig_Info) -> (Errno) {
  2421. ret := syscall(SYS_rt_tgsigqueueinfo, tgid, pid, sig, si)
  2422. return Errno(-ret)
  2423. }
  2424. /*
  2425. Set up performance monitoring.
  2426. Available since Linux 2.6.31.
  2427. */
  2428. perf_event_open :: proc "contextless" (attr: ^Perf_Event_Attr, pid: Pid, cpu: int, group_fd: Fd, flags: Perf_Flags = {}) -> (Fd, Errno) {
  2429. ret := syscall(SYS_perf_event_open, attr, pid, cpu, group_fd, transmute(uint) flags)
  2430. return errno_unwrap(ret, Fd)
  2431. }
  2432. /*
  2433. Receive multiple messages from a socket.
  2434. Available since Linux 2.6.33.
  2435. */
  2436. recvmmsg :: proc "contextless" (sock: Fd, msg_vec: []MMsg_Hdr, flags: Socket_Msg) -> (int, Errno) {
  2437. ret := syscall(SYS_recvmmsg, sock, raw_data(msg_vec), len(msg_vec), transmute(i32) flags)
  2438. return errno_unwrap(ret, int)
  2439. }
  2440. // TODO(flysand): fanotify_init
  2441. // TODO(flysand): fanotify_mark
  2442. // TODO(flysand): prlimit64
  2443. // TODO(flysand): name_to_handle_at
  2444. // TODO(flysand): open_by_handle_at
  2445. // TODO(flysand): clock_adjtime
  2446. // TODO(flysand): syncfs
  2447. /*
  2448. Send multiple messages on a socket.
  2449. Available since Linux 3.0.
  2450. */
  2451. sendmmsg :: proc "contextless" (sock: Fd, msg_vec: []MMsg_Hdr, flags: Socket_Msg) -> (int, Errno) {
  2452. ret := syscall(SYS_sendmmsg, sock, raw_data(msg_vec), len(msg_vec), transmute(i32) flags)
  2453. return errno_unwrap(ret, int)
  2454. }
  2455. // TODO(flysand): setns
  2456. // TODO(flysand): getcpu
  2457. // TODO(flysand): process_vm_readv
  2458. // TODO(flysand): process_vm_writev
  2459. // TODO(flysand): kcmp
  2460. // TODO(flysand): finit_module
  2461. // TODO(flysand): sched_setattr
  2462. // TODO(flysand): sched_getattr
  2463. /*
  2464. Rename the file with names relative to the specified dirfd's with other options.
  2465. Available since Linux 3.15.
  2466. */
  2467. renameat2 :: proc "contextless" (oldfd: Fd, old: cstring, newfd: Fd, new: cstring, flags: Rename_Flags) -> (Errno) {
  2468. ret := syscall(SYS_renameat2, oldfd, cast(rawptr) old, newfd, cast(rawptr) new, transmute(u32) flags)
  2469. return Errno(-ret)
  2470. }
  2471. // TODO(flysand): seccomp
  2472. getrandom :: proc "contextless" (buf: []u8, flags: Get_Random_Flags) -> (int, Errno) {
  2473. ret := syscall(SYS_getrandom, raw_data(buf), len(buf), transmute(i32) flags)
  2474. return errno_unwrap(ret, int)
  2475. }
  2476. // TODO(flysand): memfd_create
  2477. // TODO(flysand): kexec_file_load
  2478. // TODO(flysand): bpf
  2479. /*
  2480. Execute program relative to a directory file descriptor.
  2481. Available since Linux 3.19.
  2482. */
  2483. execveat :: proc "contextless" (dirfd: Fd, name: cstring, argv: [^]cstring, envp: [^]cstring, flags: Execveat_Flags = {}) -> (Errno) {
  2484. ret := syscall(SYS_execveat, dirfd, cast(rawptr) name, cast(rawptr) argv, cast(rawptr) envp, transmute(i32) flags)
  2485. return Errno(-ret)
  2486. }
  2487. // TODO(flysand): userfaultfd
  2488. // TODO(flysand): membarrier
  2489. // TODO(flysand): mlock2
  2490. // TODO(flysand): copy_file_range
  2491. // TODO(flysand): preadv2
  2492. // TODO(flysand): pwritev2
  2493. // TODO(flysand): pkey_mprotect
  2494. // TODO(flysand): pkey_alloc
  2495. // TODO(flysand): pkey_free
  2496. /*
  2497. Query extended information about the file
  2498. The file can be specified as:
  2499. - absolute pathname: `dir` parameter is ignored
  2500. - relatvie pathname: `dir` parameter specifies the base directory's fd
  2501. - file descriptor: `AT_EMPTY_PATH` is passed in flags, pathname is empty, `dir` specifies the file descriptor
  2502. Available since Linux 4.11
  2503. */
  2504. statx :: proc "contextless" (dir: Fd, pathname: cstring, flags: FD_Flags, mask: Statx_Mask, statx: ^Statx) -> (Errno) {
  2505. ret := syscall(SYS_statx, dir, transmute(uintptr) pathname, transmute(i32) flags, transmute(u32) mask, statx)
  2506. return Errno(-ret)
  2507. }
  2508. // TODO(flysand): io_pgetevents
  2509. // TODO(flysand): rseq
  2510. // TODO(flysand): pidfd_send_signal
  2511. // TODO(flysand): io_uring_setup
  2512. // TODO(flysand): io_uring_enter
  2513. // TODO(flysand): io_uring_register
  2514. // TODO(flysand): open_tree
  2515. // TODO(flysand): move_mount
  2516. // TODO(flysand): fsopen
  2517. // TODO(flysand): fsconfig
  2518. // TODO(flysand): fsmount
  2519. // TODO(flysand): fspick
  2520. /*
  2521. Creates a new PID file descriptor.
  2522. The process identified by `pid` must be a pid group leader.
  2523. The returned `pidfd` has `CLOEXEC` semantics.
  2524. Available since Linux 5.3.
  2525. */
  2526. pidfd_open :: proc "contextless" (pid: Pid, flags: Pid_FD_Flags) -> (Pid_FD, Errno) {
  2527. ret := syscall(SYS_pidfd_open, pid, transmute(i32) flags)
  2528. return errno_unwrap(ret, Pid_FD)
  2529. }
  2530. // TODO(flysand): clone3 (probably not this PR)
  2531. /*
  2532. Close the range of files as an atomic operation.
  2533. The range of file descriptors is inclusive, and may contain invalid file descriptors.
  2534. Available since Linux 5.9.
  2535. */
  2536. close_range :: proc "contextless" (lo: Fd, hi: Fd, flags: Close_Range_Flags) -> (Errno) {
  2537. ret := syscall(SYS_close_range, lo, hi, transmute(u32) flags)
  2538. return Errno(-ret)
  2539. }
  2540. // TODO(flysand): openat2
  2541. /*
  2542. Get a file descriptor from another process.
  2543. - `fd` refers to a file descriptor number to get.
  2544. - `flags` must be zero.
  2545. Available since Linux 5.3.
  2546. */
  2547. pidfd_getfd :: proc "contextless" (pidfd: Pid_FD, fd: Fd, flags: i32 = 0) -> (Fd, Errno) {
  2548. ret := syscall(SYS_pidfd_getfd, pidfd, fd, flags)
  2549. return errno_unwrap(ret, Fd)
  2550. }
  2551. /*
  2552. Checks the user permissions for a file at specified dirfd (with flags).
  2553. Available since Linux 5.8.
  2554. */
  2555. faccessat2 :: proc "contextless" (dirfd: Fd, name: cstring, mode: Mode = F_OK, flags: FD_Flags = FD_Flags{}) -> (Errno) {
  2556. ret := syscall(SYS_faccessat2, dirfd, cast(rawptr) name, transmute(u32) mode, transmute(i32) flags)
  2557. return Errno(-ret)
  2558. }
  2559. // TODO(flysand): process_madvise
  2560. /*
  2561. Wait for an I/O event on an epoll file descriptor.
  2562. Available since Linux 2.6.
  2563. */
  2564. epoll_pwait2 :: proc(epfd: Fd, events: [^]EPoll_Event, count: i32, timeout: ^Time_Spec, sigmask: ^Sig_Set) -> (i32, Errno) {
  2565. ret := syscall(SYS_epoll_pwait2, epfd, events, count, timeout, sigmask)
  2566. return errno_unwrap(ret, i32)
  2567. }
  2568. // TODO(flysand): mount_setattr
  2569. // TODO(flysand): quotactl_fd
  2570. // TODO(flysand): landlock_create_ruleset
  2571. // TODO(flysand): landlock_add_rule
  2572. // TODO(flysand): landlock_restrict_self
  2573. // TODO(flysand): memfd_secret
  2574. // TODO(flysand): process_mrelease
  2575. // TODO(flysand): futex_waitv
  2576. // TODO(flysand): set_mempolicy_home_node
  2577. // TODO(flysand): cachestat
  2578. // TODO(flysand): fchmodat2
  2579. // TODO(flysand): map_shadow_stack
  2580. when ODIN_ARCH == .riscv64 {
  2581. /*
  2582. Probe for RISC-V Hardware Support.
  2583. Available since Linux 6.4.
  2584. TODO: cpu_set_t
  2585. See: https://docs.kernel.org/arch/riscv/hwprobe.html
  2586. */
  2587. riscv_hwprobe :: proc "contextless" (pairs: [^]RISCV_HWProbe, pair_count: uint, cpu_count: uint, cpus: rawptr /* cpu_set_t */, flags: RISCV_HWProbe_Flags) -> Errno {
  2588. ret := syscall(SYS_riscv_hwprobe, pairs, pair_count, cpu_count, cpus, transmute(u32)flags)
  2589. return Errno(-ret)
  2590. }
  2591. }