sys.odin 79 KB

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