sys.odin 79 KB

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