sys.odin 79 KB

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