sys.odin 82 KB

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