sys.odin 78 KB

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