os_linux.odin 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "core:runtime"
  5. import "core:strings"
  6. import "core:c"
  7. import "core:strconv"
  8. import "core:intrinsics"
  9. // NOTE(flysand): For compatibility we'll make core:os package
  10. // depend on the old (scheduled for removal) linux package.
  11. // Seeing that there are plans for os2, I'm imagining that *that*
  12. // package should inherit the new sys functionality.
  13. // The reasons for these are as follows:
  14. // 1. It's very hard to update this package without breaking *a lot* of code.
  15. // 2. os2 is not stable anyways, so we can break compatibility all we want
  16. // It might be weird to bring up compatibility when Odin in it's nature isn't
  17. // all that about compatibility. But we don't want to push experimental changes
  18. // and have people's code break while it's still work in progress.
  19. import unix "core:sys/unix"
  20. Handle :: distinct i32
  21. Pid :: distinct i32
  22. File_Time :: distinct u64
  23. Errno :: distinct i32
  24. Socket :: distinct int
  25. INVALID_HANDLE :: ~Handle(0)
  26. ERROR_NONE: Errno : 0
  27. EPERM: Errno : 1
  28. ENOENT: Errno : 2
  29. ESRCH: Errno : 3
  30. EINTR: Errno : 4
  31. EIO: Errno : 5
  32. ENXIO: Errno : 6
  33. EBADF: Errno : 9
  34. EAGAIN: Errno : 11
  35. ENOMEM: Errno : 12
  36. EACCES: Errno : 13
  37. EFAULT: Errno : 14
  38. EEXIST: Errno : 17
  39. ENODEV: Errno : 19
  40. ENOTDIR: Errno : 20
  41. EISDIR: Errno : 21
  42. EINVAL: Errno : 22
  43. ENFILE: Errno : 23
  44. EMFILE: Errno : 24
  45. ETXTBSY: Errno : 26
  46. EFBIG: Errno : 27
  47. ENOSPC: Errno : 28
  48. ESPIPE: Errno : 29
  49. EROFS: Errno : 30
  50. EPIPE: Errno : 32
  51. ERANGE: Errno : 34 /* Result too large */
  52. EDEADLK: Errno : 35 /* Resource deadlock would occur */
  53. ENAMETOOLONG: Errno : 36 /* File name too long */
  54. ENOLCK: Errno : 37 /* No record locks available */
  55. ENOSYS: Errno : 38 /* Invalid system call number */
  56. ENOTEMPTY: Errno : 39 /* Directory not empty */
  57. ELOOP: Errno : 40 /* Too many symbolic links encountered */
  58. EWOULDBLOCK: Errno : EAGAIN /* Operation would block */
  59. ENOMSG: Errno : 42 /* No message of desired type */
  60. EIDRM: Errno : 43 /* Identifier removed */
  61. ECHRNG: Errno : 44 /* Channel number out of range */
  62. EL2NSYNC: Errno : 45 /* Level 2 not synchronized */
  63. EL3HLT: Errno : 46 /* Level 3 halted */
  64. EL3RST: Errno : 47 /* Level 3 reset */
  65. ELNRNG: Errno : 48 /* Link number out of range */
  66. EUNATCH: Errno : 49 /* Protocol driver not attached */
  67. ENOCSI: Errno : 50 /* No CSI structure available */
  68. EL2HLT: Errno : 51 /* Level 2 halted */
  69. EBADE: Errno : 52 /* Invalid exchange */
  70. EBADR: Errno : 53 /* Invalid request descriptor */
  71. EXFULL: Errno : 54 /* Exchange full */
  72. ENOANO: Errno : 55 /* No anode */
  73. EBADRQC: Errno : 56 /* Invalid request code */
  74. EBADSLT: Errno : 57 /* Invalid slot */
  75. EDEADLOCK: Errno : EDEADLK
  76. EBFONT: Errno : 59 /* Bad font file format */
  77. ENOSTR: Errno : 60 /* Device not a stream */
  78. ENODATA: Errno : 61 /* No data available */
  79. ETIME: Errno : 62 /* Timer expired */
  80. ENOSR: Errno : 63 /* Out of streams resources */
  81. ENONET: Errno : 64 /* Machine is not on the network */
  82. ENOPKG: Errno : 65 /* Package not installed */
  83. EREMOTE: Errno : 66 /* Object is remote */
  84. ENOLINK: Errno : 67 /* Link has been severed */
  85. EADV: Errno : 68 /* Advertise error */
  86. ESRMNT: Errno : 69 /* Srmount error */
  87. ECOMM: Errno : 70 /* Communication error on send */
  88. EPROTO: Errno : 71 /* Protocol error */
  89. EMULTIHOP: Errno : 72 /* Multihop attempted */
  90. EDOTDOT: Errno : 73 /* RFS specific error */
  91. EBADMSG: Errno : 74 /* Not a data message */
  92. EOVERFLOW: Errno : 75 /* Value too large for defined data type */
  93. ENOTUNIQ: Errno : 76 /* Name not unique on network */
  94. EBADFD: Errno : 77 /* File descriptor in bad state */
  95. EREMCHG: Errno : 78 /* Remote address changed */
  96. ELIBACC: Errno : 79 /* Can not access a needed shared library */
  97. ELIBBAD: Errno : 80 /* Accessing a corrupted shared library */
  98. ELIBSCN: Errno : 81 /* .lib section in a.out corrupted */
  99. ELIBMAX: Errno : 82 /* Attempting to link in too many shared libraries */
  100. ELIBEXEC: Errno : 83 /* Cannot exec a shared library directly */
  101. EILSEQ: Errno : 84 /* Illegal byte sequence */
  102. ERESTART: Errno : 85 /* Interrupted system call should be restarted */
  103. ESTRPIPE: Errno : 86 /* Streams pipe error */
  104. EUSERS: Errno : 87 /* Too many users */
  105. ENOTSOCK: Errno : 88 /* Socket operation on non-socket */
  106. EDESTADDRREQ: Errno : 89 /* Destination address required */
  107. EMSGSIZE: Errno : 90 /* Message too long */
  108. EPROTOTYPE: Errno : 91 /* Protocol wrong type for socket */
  109. ENOPROTOOPT: Errno : 92 /* Protocol not available */
  110. EPROTONOSUPPORT:Errno : 93 /* Protocol not supported */
  111. ESOCKTNOSUPPORT:Errno : 94 /* Socket type not supported */
  112. EOPNOTSUPP: Errno : 95 /* Operation not supported on transport endpoint */
  113. EPFNOSUPPORT: Errno : 96 /* Protocol family not supported */
  114. EAFNOSUPPORT: Errno : 97 /* Address family not supported by protocol */
  115. EADDRINUSE: Errno : 98 /* Address already in use */
  116. EADDRNOTAVAIL: Errno : 99 /* Cannot assign requested address */
  117. ENETDOWN: Errno : 100 /* Network is down */
  118. ENETUNREACH: Errno : 101 /* Network is unreachable */
  119. ENETRESET: Errno : 102 /* Network dropped connection because of reset */
  120. ECONNABORTED: Errno : 103 /* Software caused connection abort */
  121. ECONNRESET: Errno : 104 /* Connection reset by peer */
  122. ENOBUFS: Errno : 105 /* No buffer space available */
  123. EISCONN: Errno : 106 /* Transport endpoint is already connected */
  124. ENOTCONN: Errno : 107 /* Transport endpoint is not connected */
  125. ESHUTDOWN: Errno : 108 /* Cannot send after transport endpoint shutdown */
  126. ETOOMANYREFS: Errno : 109 /* Too many references: cannot splice */
  127. ETIMEDOUT: Errno : 110 /* Connection timed out */
  128. ECONNREFUSED: Errno : 111 /* Connection refused */
  129. EHOSTDOWN: Errno : 112 /* Host is down */
  130. EHOSTUNREACH: Errno : 113 /* No route to host */
  131. EALREADY: Errno : 114 /* Operation already in progress */
  132. EINPROGRESS: Errno : 115 /* Operation now in progress */
  133. ESTALE: Errno : 116 /* Stale file handle */
  134. EUCLEAN: Errno : 117 /* Structure needs cleaning */
  135. ENOTNAM: Errno : 118 /* Not a XENIX named type file */
  136. ENAVAIL: Errno : 119 /* No XENIX semaphores available */
  137. EISNAM: Errno : 120 /* Is a named type file */
  138. EREMOTEIO: Errno : 121 /* Remote I/O error */
  139. EDQUOT: Errno : 122 /* Quota exceeded */
  140. ENOMEDIUM: Errno : 123 /* No medium found */
  141. EMEDIUMTYPE: Errno : 124 /* Wrong medium type */
  142. ECANCELED: Errno : 125 /* Operation Canceled */
  143. ENOKEY: Errno : 126 /* Required key not available */
  144. EKEYEXPIRED: Errno : 127 /* Key has expired */
  145. EKEYREVOKED: Errno : 128 /* Key has been revoked */
  146. EKEYREJECTED: Errno : 129 /* Key was rejected by service */
  147. /* for robust mutexes */
  148. EOWNERDEAD: Errno : 130 /* Owner died */
  149. ENOTRECOVERABLE: Errno : 131 /* State not recoverable */
  150. ERFKILL: Errno : 132 /* Operation not possible due to RF-kill */
  151. EHWPOISON: Errno : 133 /* Memory page has hardware error */
  152. ADDR_NO_RANDOMIZE :: 0x40000
  153. O_RDONLY :: 0x00000
  154. O_WRONLY :: 0x00001
  155. O_RDWR :: 0x00002
  156. O_CREATE :: 0x00040
  157. O_EXCL :: 0x00080
  158. O_NOCTTY :: 0x00100
  159. O_TRUNC :: 0x00200
  160. O_NONBLOCK :: 0x00800
  161. O_APPEND :: 0x00400
  162. O_SYNC :: 0x01000
  163. O_ASYNC :: 0x02000
  164. O_CLOEXEC :: 0x80000
  165. SEEK_DATA :: 3
  166. SEEK_HOLE :: 4
  167. SEEK_MAX :: SEEK_HOLE
  168. AF_UNSPEC: int : 0
  169. AF_UNIX: int : 1
  170. AF_LOCAL: int : AF_UNIX
  171. AF_INET: int : 2
  172. AF_INET6: int : 10
  173. AF_PACKET: int : 17
  174. AF_BLUETOOTH: int : 31
  175. SOCK_STREAM: int : 1
  176. SOCK_DGRAM: int : 2
  177. SOCK_RAW: int : 3
  178. SOCK_RDM: int : 4
  179. SOCK_SEQPACKET: int : 5
  180. SOCK_PACKET: int : 10
  181. INADDR_ANY: c.ulong : 0
  182. INADDR_BROADCAST: c.ulong : 0xffffffff
  183. INADDR_NONE: c.ulong : 0xffffffff
  184. INADDR_DUMMY: c.ulong : 0xc0000008
  185. IPPROTO_IP: int : 0
  186. IPPROTO_ICMP: int : 1
  187. IPPROTO_TCP: int : 6
  188. IPPROTO_UDP: int : 17
  189. IPPROTO_IPV6: int : 41
  190. IPPROTO_ETHERNET: int : 143
  191. IPPROTO_RAW: int : 255
  192. SHUT_RD: int : 0
  193. SHUT_WR: int : 1
  194. SHUT_RDWR: int : 2
  195. SOL_SOCKET: int : 1
  196. SO_DEBUG: int : 1
  197. SO_REUSEADDR: int : 2
  198. SO_DONTROUTE: int : 5
  199. SO_BROADCAST: int : 6
  200. SO_SNDBUF: int : 7
  201. SO_RCVBUF: int : 8
  202. SO_KEEPALIVE: int : 9
  203. SO_OOBINLINE: int : 10
  204. SO_LINGER: int : 13
  205. SO_REUSEPORT: int : 15
  206. SO_RCVTIMEO_NEW: int : 66
  207. SO_SNDTIMEO_NEW: int : 67
  208. TCP_NODELAY: int : 1
  209. TCP_CORK: int : 3
  210. MSG_TRUNC : int : 0x20
  211. // TODO: add remaining fcntl commands
  212. // reference: https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/fcntl.h
  213. F_GETFL: int : 3 /* Get file flags */
  214. F_SETFL: int : 4 /* Set file flags */
  215. // NOTE(zangent): These are OS specific!
  216. // Do not mix these up!
  217. RTLD_LAZY :: 0x001
  218. RTLD_NOW :: 0x002
  219. RTLD_BINDING_MASK :: 0x3
  220. RTLD_GLOBAL :: 0x100
  221. socklen_t :: c.int
  222. Timeval :: struct {
  223. seconds: i64,
  224. microseconds: int,
  225. }
  226. // "Argv" arguments converted to Odin strings
  227. args := _alloc_command_line_arguments()
  228. Unix_File_Time :: struct {
  229. seconds: i64,
  230. nanoseconds: i64,
  231. }
  232. OS_Stat :: struct {
  233. device_id: u64, // ID of device containing file
  234. serial: u64, // File serial number
  235. nlink: u64, // Number of hard links
  236. mode: u32, // Mode of the file
  237. uid: u32, // User ID of the file's owner
  238. gid: u32, // Group ID of the file's group
  239. _padding: i32, // 32 bits of padding
  240. rdev: u64, // Device ID, if device
  241. size: i64, // Size of the file, in bytes
  242. block_size: i64, // Optimal bllocksize for I/O
  243. blocks: i64, // Number of 512-byte blocks allocated
  244. last_access: Unix_File_Time, // Time of last access
  245. modified: Unix_File_Time, // Time of last modification
  246. status_change: Unix_File_Time, // Time of last status change
  247. _reserve1,
  248. _reserve2,
  249. _reserve3: i64,
  250. }
  251. // NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
  252. Dirent :: struct {
  253. ino: u64,
  254. off: u64,
  255. reclen: u16,
  256. type: u8,
  257. name: [256]byte,
  258. }
  259. ADDRESS_FAMILY :: u16
  260. SOCKADDR :: struct #packed {
  261. sa_family: ADDRESS_FAMILY,
  262. sa_data: [14]c.char,
  263. }
  264. SOCKADDR_STORAGE_LH :: struct #packed {
  265. ss_family: ADDRESS_FAMILY,
  266. __ss_pad1: [6]c.char,
  267. __ss_align: i64,
  268. __ss_pad2: [112]c.char,
  269. }
  270. sockaddr_in :: struct #packed {
  271. sin_family: ADDRESS_FAMILY,
  272. sin_port: u16be,
  273. sin_addr: in_addr,
  274. sin_zero: [8]c.char,
  275. }
  276. sockaddr_in6 :: struct #packed {
  277. sin6_family: ADDRESS_FAMILY,
  278. sin6_port: u16be,
  279. sin6_flowinfo: c.ulong,
  280. sin6_addr: in6_addr,
  281. sin6_scope_id: c.ulong,
  282. }
  283. in_addr :: struct #packed {
  284. s_addr: u32,
  285. }
  286. in6_addr :: struct #packed {
  287. s6_addr: [16]u8,
  288. }
  289. rtnl_link_stats :: struct #packed {
  290. rx_packets: u32,
  291. tx_packets: u32,
  292. rx_bytes: u32,
  293. tx_bytes: u32,
  294. rx_errors: u32,
  295. tx_errors: u32,
  296. rx_dropped: u32,
  297. tx_dropped: u32,
  298. multicast: u32,
  299. collisions: u32,
  300. rx_length_errors: u32,
  301. rx_over_errors: u32,
  302. rx_crc_errors: u32,
  303. rx_frame_errors: u32,
  304. rx_fifo_errors: u32,
  305. rx_missed_errors: u32,
  306. tx_aborted_errors: u32,
  307. tx_carrier_errors: u32,
  308. tx_fifo_errors: u32,
  309. tx_heartbeat_errors: u32,
  310. tx_window_errors: u32,
  311. rx_compressed: u32,
  312. tx_compressed: u32,
  313. rx_nohandler: u32,
  314. }
  315. SIOCGIFFLAG :: enum c.int {
  316. UP = 0, /* Interface is up. */
  317. BROADCAST = 1, /* Broadcast address valid. */
  318. DEBUG = 2, /* Turn on debugging. */
  319. LOOPBACK = 3, /* Is a loopback net. */
  320. POINT_TO_POINT = 4, /* Interface is point-to-point link. */
  321. NO_TRAILERS = 5, /* Avoid use of trailers. */
  322. RUNNING = 6, /* Resources allocated. */
  323. NOARP = 7, /* No address resolution protocol. */
  324. PROMISC = 8, /* Receive all packets. */
  325. ALL_MULTI = 9, /* Receive all multicast packets. Unimplemented. */
  326. MASTER = 10, /* Master of a load balancer. */
  327. SLAVE = 11, /* Slave of a load balancer. */
  328. MULTICAST = 12, /* Supports multicast. */
  329. PORTSEL = 13, /* Can set media type. */
  330. AUTOMEDIA = 14, /* Auto media select active. */
  331. DYNAMIC = 15, /* Dialup device with changing addresses. */
  332. LOWER_UP = 16,
  333. DORMANT = 17,
  334. ECHO = 18,
  335. }
  336. SIOCGIFFLAGS :: bit_set[SIOCGIFFLAG; c.int]
  337. ifaddrs :: struct {
  338. next: ^ifaddrs,
  339. name: cstring,
  340. flags: SIOCGIFFLAGS,
  341. address: ^SOCKADDR,
  342. netmask: ^SOCKADDR,
  343. broadcast_or_dest: ^SOCKADDR, // Broadcast or Point-to-Point address
  344. data: rawptr, // Address-specific data.
  345. }
  346. Dir :: distinct rawptr // DIR*
  347. // File type
  348. S_IFMT :: 0o170000 // Type of file mask
  349. S_IFIFO :: 0o010000 // Named pipe (fifo)
  350. S_IFCHR :: 0o020000 // Character special
  351. S_IFDIR :: 0o040000 // Directory
  352. S_IFBLK :: 0o060000 // Block special
  353. S_IFREG :: 0o100000 // Regular
  354. S_IFLNK :: 0o120000 // Symbolic link
  355. S_IFSOCK :: 0o140000 // Socket
  356. // File mode
  357. // Read, write, execute/search by owner
  358. S_IRWXU :: 0o0700 // RWX mask for owner
  359. S_IRUSR :: 0o0400 // R for owner
  360. S_IWUSR :: 0o0200 // W for owner
  361. S_IXUSR :: 0o0100 // X for owner
  362. // Read, write, execute/search by group
  363. S_IRWXG :: 0o0070 // RWX mask for group
  364. S_IRGRP :: 0o0040 // R for group
  365. S_IWGRP :: 0o0020 // W for group
  366. S_IXGRP :: 0o0010 // X for group
  367. // Read, write, execute/search by others
  368. S_IRWXO :: 0o0007 // RWX mask for other
  369. S_IROTH :: 0o0004 // R for other
  370. S_IWOTH :: 0o0002 // W for other
  371. S_IXOTH :: 0o0001 // X for other
  372. S_ISUID :: 0o4000 // Set user id on execution
  373. S_ISGID :: 0o2000 // Set group id on execution
  374. S_ISVTX :: 0o1000 // Directory restrcted delete
  375. S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK }
  376. S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG }
  377. S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR }
  378. S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR }
  379. S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK }
  380. S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO }
  381. S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK }
  382. F_OK :: 0 // Test for file existance
  383. X_OK :: 1 // Test for execute permission
  384. W_OK :: 2 // Test for write permission
  385. R_OK :: 4 // Test for read permission
  386. AT_FDCWD :: ~uintptr(99) /* -100 */
  387. AT_REMOVEDIR :: uintptr(0x200)
  388. AT_SYMLINK_NOFOLLOW :: uintptr(0x100)
  389. pollfd :: struct {
  390. fd: c.int,
  391. events: c.short,
  392. revents: c.short,
  393. }
  394. sigset_t :: distinct u64
  395. foreign libc {
  396. @(link_name="__errno_location") __errno_location :: proc() -> ^c.int ---
  397. @(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
  398. @(link_name="get_nprocs") _unix_get_nprocs :: proc() -> c.int ---
  399. @(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
  400. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
  401. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
  402. @(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  403. @(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---
  404. @(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---
  405. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  406. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---
  407. @(link_name="execvp") _unix_execvp :: proc(path: cstring, argv: [^]cstring) -> int ---
  408. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
  409. @(link_name="putenv") _unix_putenv :: proc(cstring) -> c.int ---
  410. @(link_name="setenv") _unix_setenv :: proc(key: cstring, value: cstring, overwrite: c.int) -> c.int ---
  411. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  412. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
  413. }
  414. foreign dl {
  415. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---
  416. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
  417. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---
  418. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
  419. @(link_name="getifaddrs") _getifaddrs :: proc(ifap: ^^ifaddrs) -> (c.int) ---
  420. @(link_name="freeifaddrs") _freeifaddrs :: proc(ifa: ^ifaddrs) ---
  421. }
  422. is_path_separator :: proc(r: rune) -> bool {
  423. return r == '/'
  424. }
  425. // determine errno from syscall return value
  426. @private
  427. _get_errno :: proc(res: int) -> Errno {
  428. if res < 0 && res > -4096 {
  429. return Errno(-res)
  430. }
  431. return 0
  432. }
  433. // get errno from libc
  434. get_last_error :: proc "contextless" () -> int {
  435. return int(__errno_location()^)
  436. }
  437. personality :: proc(persona: u64) -> (Errno) {
  438. res := unix.sys_personality(persona)
  439. if res == -1 {
  440. return _get_errno(res)
  441. }
  442. return ERROR_NONE
  443. }
  444. fork :: proc() -> (Pid, Errno) {
  445. pid := unix.sys_fork()
  446. if pid == -1 {
  447. return -1, _get_errno(pid)
  448. }
  449. return Pid(pid), ERROR_NONE
  450. }
  451. execvp :: proc(path: string, args: []string) -> Errno {
  452. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  453. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  454. args_cstrs := make([]cstring, len(args) + 2, context.temp_allocator)
  455. args_cstrs[0] = strings.clone_to_cstring(path, context.temp_allocator)
  456. for i := 0; i < len(args); i += 1 {
  457. args_cstrs[i+1] = strings.clone_to_cstring(args[i], context.temp_allocator)
  458. }
  459. _unix_execvp(path_cstr, raw_data(args_cstrs))
  460. return Errno(get_last_error())
  461. }
  462. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0o000) -> (Handle, Errno) {
  463. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  464. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  465. handle := unix.sys_open(cstr, flags, uint(mode))
  466. if handle < 0 {
  467. return INVALID_HANDLE, _get_errno(handle)
  468. }
  469. return Handle(handle), ERROR_NONE
  470. }
  471. close :: proc(fd: Handle) -> Errno {
  472. return _get_errno(unix.sys_close(int(fd)))
  473. }
  474. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  475. if len(data) == 0 {
  476. return 0, ERROR_NONE
  477. }
  478. bytes_read := unix.sys_read(int(fd), raw_data(data), len(data))
  479. if bytes_read < 0 {
  480. return -1, _get_errno(bytes_read)
  481. }
  482. return bytes_read, ERROR_NONE
  483. }
  484. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  485. if len(data) == 0 {
  486. return 0, ERROR_NONE
  487. }
  488. bytes_written := unix.sys_write(int(fd), raw_data(data), len(data))
  489. if bytes_written < 0 {
  490. return -1, _get_errno(bytes_written)
  491. }
  492. return bytes_written, ERROR_NONE
  493. }
  494. read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  495. if len(data) == 0 {
  496. return 0, ERROR_NONE
  497. }
  498. bytes_read := unix.sys_pread(int(fd), raw_data(data), len(data), offset)
  499. if bytes_read < 0 {
  500. return -1, _get_errno(bytes_read)
  501. }
  502. return bytes_read, ERROR_NONE
  503. }
  504. write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  505. if len(data) == 0 {
  506. return 0, ERROR_NONE
  507. }
  508. bytes_written := unix.sys_pwrite(int(fd), raw_data(data), uint(len(data)), offset)
  509. if bytes_written < 0 {
  510. return -1, _get_errno(bytes_written)
  511. }
  512. return bytes_written, ERROR_NONE
  513. }
  514. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  515. res := unix.sys_lseek(int(fd), offset, whence)
  516. if res < 0 {
  517. return -1, _get_errno(int(res))
  518. }
  519. return i64(res), ERROR_NONE
  520. }
  521. file_size :: proc(fd: Handle) -> (i64, Errno) {
  522. // deliberately uninitialized; the syscall fills this buffer for us
  523. s: OS_Stat = ---
  524. result := unix.sys_fstat(int(fd), rawptr(&s))
  525. if result < 0 {
  526. return 0, _get_errno(result)
  527. }
  528. return max(s.size, 0), ERROR_NONE
  529. }
  530. rename :: proc(old_path, new_path: string) -> Errno {
  531. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  532. old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
  533. new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
  534. return _get_errno(unix.sys_rename(old_path_cstr, new_path_cstr))
  535. }
  536. remove :: proc(path: string) -> Errno {
  537. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  538. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  539. return _get_errno(unix.sys_unlink(path_cstr))
  540. }
  541. make_directory :: proc(path: string, mode: u32 = 0o775) -> Errno {
  542. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  543. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  544. return _get_errno(unix.sys_mkdir(path_cstr, uint(mode)))
  545. }
  546. remove_directory :: proc(path: string) -> Errno {
  547. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  548. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  549. return _get_errno(unix.sys_rmdir(path_cstr))
  550. }
  551. is_file_handle :: proc(fd: Handle) -> bool {
  552. s, err := _fstat(fd)
  553. if err != ERROR_NONE {
  554. return false
  555. }
  556. return S_ISREG(s.mode)
  557. }
  558. is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
  559. s: OS_Stat
  560. err: Errno
  561. if follow_links {
  562. s, err = _stat(path)
  563. } else {
  564. s, err = _lstat(path)
  565. }
  566. if err != ERROR_NONE {
  567. return false
  568. }
  569. return S_ISREG(s.mode)
  570. }
  571. is_dir_handle :: proc(fd: Handle) -> bool {
  572. s, err := _fstat(fd)
  573. if err != ERROR_NONE {
  574. return false
  575. }
  576. return S_ISDIR(s.mode)
  577. }
  578. is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
  579. s: OS_Stat
  580. err: Errno
  581. if follow_links {
  582. s, err = _stat(path)
  583. } else {
  584. s, err = _lstat(path)
  585. }
  586. if err != ERROR_NONE {
  587. return false
  588. }
  589. return S_ISDIR(s.mode)
  590. }
  591. is_file :: proc {is_file_path, is_file_handle}
  592. is_dir :: proc {is_dir_path, is_dir_handle}
  593. exists :: proc(path: string) -> bool {
  594. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  595. cpath := strings.clone_to_cstring(path, context.temp_allocator)
  596. res := unix.sys_access(cpath, O_RDONLY)
  597. return res == 0
  598. }
  599. // NOTE(bill): Uses startup to initialize it
  600. stdin: Handle = 0
  601. stdout: Handle = 1
  602. stderr: Handle = 2
  603. /* TODO(zangent): Implement these!
  604. last_write_time :: proc(fd: Handle) -> File_Time {}
  605. last_write_time_by_name :: proc(name: string) -> File_Time {}
  606. */
  607. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  608. s, err := _fstat(fd)
  609. if err != ERROR_NONE {
  610. return 0, err
  611. }
  612. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  613. return File_Time(modified), ERROR_NONE
  614. }
  615. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  616. s, err := _stat(name)
  617. if err != ERROR_NONE {
  618. return 0, err
  619. }
  620. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  621. return File_Time(modified), ERROR_NONE
  622. }
  623. @private
  624. _stat :: proc(path: string) -> (OS_Stat, Errno) {
  625. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  626. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  627. // deliberately uninitialized; the syscall fills this buffer for us
  628. s: OS_Stat = ---
  629. result := unix.sys_stat(cstr, &s)
  630. if result < 0 {
  631. return s, _get_errno(result)
  632. }
  633. return s, ERROR_NONE
  634. }
  635. @private
  636. _lstat :: proc(path: string) -> (OS_Stat, Errno) {
  637. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  638. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  639. // deliberately uninitialized; the syscall fills this buffer for us
  640. s: OS_Stat = ---
  641. result := unix.sys_lstat(cstr, &s)
  642. if result < 0 {
  643. return s, _get_errno(result)
  644. }
  645. return s, ERROR_NONE
  646. }
  647. @private
  648. _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  649. // deliberately uninitialized; the syscall fills this buffer for us
  650. s: OS_Stat = ---
  651. result := unix.sys_fstat(int(fd), rawptr(&s))
  652. if result < 0 {
  653. return s, _get_errno(result)
  654. }
  655. return s, ERROR_NONE
  656. }
  657. @private
  658. _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
  659. dirp := _unix_fdopendir(fd)
  660. if dirp == cast(Dir)nil {
  661. return nil, Errno(get_last_error())
  662. }
  663. return dirp, ERROR_NONE
  664. }
  665. @private
  666. _closedir :: proc(dirp: Dir) -> Errno {
  667. rc := _unix_closedir(dirp)
  668. if rc != 0 {
  669. return Errno(get_last_error())
  670. }
  671. return ERROR_NONE
  672. }
  673. @private
  674. _rewinddir :: proc(dirp: Dir) {
  675. _unix_rewinddir(dirp)
  676. }
  677. @private
  678. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
  679. result: ^Dirent
  680. rc := _unix_readdir_r(dirp, &entry, &result)
  681. if rc != 0 {
  682. err = Errno(get_last_error())
  683. return
  684. }
  685. err = ERROR_NONE
  686. if result == nil {
  687. end_of_stream = true
  688. return
  689. }
  690. end_of_stream = false
  691. return
  692. }
  693. @private
  694. _readlink :: proc(path: string) -> (string, Errno) {
  695. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  696. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  697. bufsz : uint = 256
  698. buf := make([]byte, bufsz)
  699. for {
  700. rc := unix.sys_readlink(path_cstr, &(buf[0]), bufsz)
  701. if rc < 0 {
  702. delete(buf)
  703. return "", _get_errno(rc)
  704. } else if rc == int(bufsz) {
  705. // NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
  706. bufsz *= 2
  707. delete(buf)
  708. buf = make([]byte, bufsz)
  709. } else {
  710. return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
  711. }
  712. }
  713. }
  714. absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
  715. buf : [256]byte
  716. fd_str := strconv.itoa( buf[:], cast(int)fd )
  717. procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } )
  718. defer delete(procfs_path)
  719. return _readlink(procfs_path)
  720. }
  721. absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
  722. rel := rel
  723. if rel == "" {
  724. rel = "."
  725. }
  726. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  727. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  728. path_ptr := _unix_realpath(rel_cstr, nil)
  729. if path_ptr == nil {
  730. return "", Errno(get_last_error())
  731. }
  732. defer _unix_free(path_ptr)
  733. path_cstr := transmute(cstring)path_ptr
  734. path = strings.clone( string(path_cstr) )
  735. return path, ERROR_NONE
  736. }
  737. access :: proc(path: string, mask: int) -> (bool, Errno) {
  738. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  739. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  740. result := unix.sys_access(cstr, mask)
  741. if result < 0 {
  742. return false, _get_errno(result)
  743. }
  744. return true, ERROR_NONE
  745. }
  746. heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
  747. if size <= 0 {
  748. return nil
  749. }
  750. if zero_memory {
  751. return _unix_calloc(1, c.size_t(size))
  752. } else {
  753. return _unix_malloc(c.size_t(size))
  754. }
  755. }
  756. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  757. // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
  758. // POSIX platforms. Ensure your caller takes this into account.
  759. return _unix_realloc(ptr, c.size_t(new_size))
  760. }
  761. heap_free :: proc(ptr: rawptr) {
  762. _unix_free(ptr)
  763. }
  764. lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
  765. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  766. path_str := strings.clone_to_cstring(key, context.temp_allocator)
  767. // NOTE(tetra): Lifetime of 'cstr' is unclear, but _unix_free(cstr) segfaults.
  768. cstr := _unix_getenv(path_str)
  769. if cstr == nil {
  770. return "", false
  771. }
  772. return strings.clone(string(cstr), allocator), true
  773. }
  774. get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
  775. value, _ = lookup_env(key, allocator)
  776. return
  777. }
  778. set_env :: proc(key, value: string) -> Errno {
  779. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  780. key_cstring := strings.clone_to_cstring(key, context.temp_allocator)
  781. value_cstring := strings.clone_to_cstring(value, context.temp_allocator)
  782. // NOTE(GoNZooo): `setenv` instead of `putenv` because it copies both key and value more commonly
  783. res := _unix_setenv(key_cstring, value_cstring, 1)
  784. if res < 0 {
  785. return Errno(get_last_error())
  786. }
  787. return ERROR_NONE
  788. }
  789. unset_env :: proc(key: string) -> Errno {
  790. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  791. s := strings.clone_to_cstring(key, context.temp_allocator)
  792. res := _unix_putenv(s)
  793. if res < 0 {
  794. return Errno(get_last_error())
  795. }
  796. return ERROR_NONE
  797. }
  798. get_current_directory :: proc() -> string {
  799. // NOTE(tetra): I would use PATH_MAX here, but I was not able to find
  800. // an authoritative value for it across all systems.
  801. // The largest value I could find was 4096, so might as well use the page size.
  802. page_size := get_page_size()
  803. buf := make([dynamic]u8, page_size)
  804. for {
  805. #no_bounds_check res := unix.sys_getcwd(&buf[0], uint(len(buf)))
  806. if res >= 0 {
  807. return strings.string_from_null_terminated_ptr(&buf[0], len(buf))
  808. }
  809. if _get_errno(res) != ERANGE {
  810. delete(buf)
  811. return ""
  812. }
  813. resize(&buf, len(buf)+page_size)
  814. }
  815. unreachable()
  816. }
  817. set_current_directory :: proc(path: string) -> (err: Errno) {
  818. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  819. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  820. res := unix.sys_chdir(cstr)
  821. if res < 0 {
  822. return _get_errno(res)
  823. }
  824. return ERROR_NONE
  825. }
  826. exit :: proc "contextless" (code: int) -> ! {
  827. runtime._cleanup_runtime_contextless()
  828. _unix_exit(c.int(code))
  829. }
  830. current_thread_id :: proc "contextless" () -> int {
  831. return unix.sys_gettid()
  832. }
  833. dlopen :: proc(filename: string, flags: int) -> rawptr {
  834. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  835. cstr := strings.clone_to_cstring(filename, context.temp_allocator)
  836. handle := _unix_dlopen(cstr, c.int(flags))
  837. return handle
  838. }
  839. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  840. assert(handle != nil)
  841. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  842. cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
  843. proc_handle := _unix_dlsym(handle, cstr)
  844. return proc_handle
  845. }
  846. dlclose :: proc(handle: rawptr) -> bool {
  847. assert(handle != nil)
  848. return _unix_dlclose(handle) == 0
  849. }
  850. dlerror :: proc() -> string {
  851. return string(_unix_dlerror())
  852. }
  853. get_page_size :: proc() -> int {
  854. // NOTE(tetra): The page size never changes, so why do anything complicated
  855. // if we don't have to.
  856. @static page_size := -1
  857. if page_size != -1 {
  858. return page_size
  859. }
  860. page_size = int(_unix_getpagesize())
  861. return page_size
  862. }
  863. @(private)
  864. _processor_core_count :: proc() -> int {
  865. return int(_unix_get_nprocs())
  866. }
  867. _alloc_command_line_arguments :: proc() -> []string {
  868. res := make([]string, len(runtime.args__))
  869. for arg, i in runtime.args__ {
  870. res[i] = string(arg)
  871. }
  872. return res
  873. }
  874. socket :: proc(domain: int, type: int, protocol: int) -> (Socket, Errno) {
  875. result := unix.sys_socket(domain, type, protocol)
  876. if result < 0 {
  877. return 0, _get_errno(result)
  878. }
  879. return Socket(result), ERROR_NONE
  880. }
  881. bind :: proc(sd: Socket, addr: ^SOCKADDR, len: socklen_t) -> (Errno) {
  882. result := unix.sys_bind(int(sd), addr, len)
  883. if result < 0 {
  884. return _get_errno(result)
  885. }
  886. return ERROR_NONE
  887. }
  888. connect :: proc(sd: Socket, addr: ^SOCKADDR, len: socklen_t) -> (Errno) {
  889. result := unix.sys_connect(int(sd), addr, len)
  890. if result < 0 {
  891. return _get_errno(result)
  892. }
  893. return ERROR_NONE
  894. }
  895. accept :: proc(sd: Socket, addr: ^SOCKADDR, len: rawptr) -> (Socket, Errno) {
  896. result := unix.sys_accept(int(sd), rawptr(addr), len)
  897. if result < 0 {
  898. return 0, _get_errno(result)
  899. }
  900. return Socket(result), ERROR_NONE
  901. }
  902. listen :: proc(sd: Socket, backlog: int) -> (Errno) {
  903. result := unix.sys_listen(int(sd), backlog)
  904. if result < 0 {
  905. return _get_errno(result)
  906. }
  907. return ERROR_NONE
  908. }
  909. setsockopt :: proc(sd: Socket, level: int, optname: int, optval: rawptr, optlen: socklen_t) -> (Errno) {
  910. result := unix.sys_setsockopt(int(sd), level, optname, optval, optlen)
  911. if result < 0 {
  912. return _get_errno(result)
  913. }
  914. return ERROR_NONE
  915. }
  916. recvfrom :: proc(sd: Socket, data: []byte, flags: int, addr: ^SOCKADDR, addr_size: ^socklen_t) -> (u32, Errno) {
  917. result := unix.sys_recvfrom(int(sd), raw_data(data), len(data), flags, addr, uintptr(addr_size))
  918. if result < 0 {
  919. return 0, _get_errno(int(result))
  920. }
  921. return u32(result), ERROR_NONE
  922. }
  923. recv :: proc(sd: Socket, data: []byte, flags: int) -> (u32, Errno) {
  924. result := unix.sys_recvfrom(int(sd), raw_data(data), len(data), flags, nil, 0)
  925. if result < 0 {
  926. return 0, _get_errno(int(result))
  927. }
  928. return u32(result), ERROR_NONE
  929. }
  930. sendto :: proc(sd: Socket, data: []u8, flags: int, addr: ^SOCKADDR, addrlen: socklen_t) -> (u32, Errno) {
  931. result := unix.sys_sendto(int(sd), raw_data(data), len(data), flags, addr, addrlen)
  932. if result < 0 {
  933. return 0, _get_errno(int(result))
  934. }
  935. return u32(result), ERROR_NONE
  936. }
  937. send :: proc(sd: Socket, data: []byte, flags: int) -> (u32, Errno) {
  938. result := unix.sys_sendto(int(sd), raw_data(data), len(data), 0, nil, 0)
  939. if result < 0 {
  940. return 0, _get_errno(int(result))
  941. }
  942. return u32(result), ERROR_NONE
  943. }
  944. shutdown :: proc(sd: Socket, how: int) -> (Errno) {
  945. result := unix.sys_shutdown(int(sd), how)
  946. if result < 0 {
  947. return _get_errno(result)
  948. }
  949. return ERROR_NONE
  950. }
  951. fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
  952. result := unix.sys_fcntl(fd, cmd, arg)
  953. if result < 0 {
  954. return 0, _get_errno(result)
  955. }
  956. return result, ERROR_NONE
  957. }
  958. poll :: proc(fds: []pollfd, timeout: int) -> (int, Errno) {
  959. result := unix.sys_poll(raw_data(fds), uint(len(fds)), timeout)
  960. if result < 0 {
  961. return 0, _get_errno(result)
  962. }
  963. return result, ERROR_NONE
  964. }
  965. ppoll :: proc(fds: []pollfd, timeout: ^unix.timespec, sigmask: ^sigset_t) -> (int, Errno) {
  966. result := unix.sys_ppoll(raw_data(fds), uint(len(fds)), timeout, sigmask, size_of(sigset_t))
  967. if result < 0 {
  968. return 0, _get_errno(result)
  969. }
  970. return result, ERROR_NONE
  971. }