sys.odin 79 KB

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