os_linux.odin 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. when ODIN_ARCH == .arm64 {
  233. OS_Stat :: struct {
  234. device_id: u64, // ID of device containing file
  235. serial: u64, // File serial number
  236. mode: u32, // Mode of the file
  237. nlink: u32, // Number of hard links
  238. uid: u32, // User ID of the file's owner
  239. gid: u32, // Group ID of the file's group
  240. rdev: u64, // Device ID, if device
  241. _: u64, // Padding
  242. size: i64, // Size of the file, in bytes
  243. block_size: i32, // Optimal blocksize for I/O
  244. _: i32, // Padding
  245. blocks: i64, // Number of 512-byte blocks allocated
  246. last_access: Unix_File_Time, // Time of last access
  247. modified: Unix_File_Time, // Time of last modification
  248. status_change: Unix_File_Time, // Time of last status change
  249. _reserved: [2]i32,
  250. }
  251. #assert(size_of(OS_Stat) == 128)
  252. } else {
  253. OS_Stat :: struct {
  254. device_id: u64, // ID of device containing file
  255. serial: u64, // File serial number
  256. nlink: u64, // Number of hard links
  257. mode: u32, // Mode of the file
  258. uid: u32, // User ID of the file's owner
  259. gid: u32, // Group ID of the file's group
  260. _: i32, // 32 bits of padding
  261. rdev: u64, // Device ID, if device
  262. size: i64, // Size of the file, in bytes
  263. block_size: i64, // Optimal bllocksize for I/O
  264. blocks: i64, // Number of 512-byte blocks allocated
  265. last_access: Unix_File_Time, // Time of last access
  266. modified: Unix_File_Time, // Time of last modification
  267. status_change: Unix_File_Time, // Time of last status change
  268. _reserved: [3]i64,
  269. }
  270. }
  271. // NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
  272. Dirent :: struct {
  273. ino: u64,
  274. off: u64,
  275. reclen: u16,
  276. type: u8,
  277. name: [256]byte,
  278. }
  279. ADDRESS_FAMILY :: u16
  280. SOCKADDR :: struct #packed {
  281. sa_family: ADDRESS_FAMILY,
  282. sa_data: [14]c.char,
  283. }
  284. SOCKADDR_STORAGE_LH :: struct #packed {
  285. ss_family: ADDRESS_FAMILY,
  286. __ss_pad1: [6]c.char,
  287. __ss_align: i64,
  288. __ss_pad2: [112]c.char,
  289. }
  290. sockaddr_in :: struct #packed {
  291. sin_family: ADDRESS_FAMILY,
  292. sin_port: u16be,
  293. sin_addr: in_addr,
  294. sin_zero: [8]c.char,
  295. }
  296. sockaddr_in6 :: struct #packed {
  297. sin6_family: ADDRESS_FAMILY,
  298. sin6_port: u16be,
  299. sin6_flowinfo: c.ulong,
  300. sin6_addr: in6_addr,
  301. sin6_scope_id: c.ulong,
  302. }
  303. in_addr :: struct #packed {
  304. s_addr: u32,
  305. }
  306. in6_addr :: struct #packed {
  307. s6_addr: [16]u8,
  308. }
  309. rtnl_link_stats :: struct #packed {
  310. rx_packets: u32,
  311. tx_packets: u32,
  312. rx_bytes: u32,
  313. tx_bytes: u32,
  314. rx_errors: u32,
  315. tx_errors: u32,
  316. rx_dropped: u32,
  317. tx_dropped: u32,
  318. multicast: u32,
  319. collisions: u32,
  320. rx_length_errors: u32,
  321. rx_over_errors: u32,
  322. rx_crc_errors: u32,
  323. rx_frame_errors: u32,
  324. rx_fifo_errors: u32,
  325. rx_missed_errors: u32,
  326. tx_aborted_errors: u32,
  327. tx_carrier_errors: u32,
  328. tx_fifo_errors: u32,
  329. tx_heartbeat_errors: u32,
  330. tx_window_errors: u32,
  331. rx_compressed: u32,
  332. tx_compressed: u32,
  333. rx_nohandler: u32,
  334. }
  335. SIOCGIFFLAG :: enum c.int {
  336. UP = 0, /* Interface is up. */
  337. BROADCAST = 1, /* Broadcast address valid. */
  338. DEBUG = 2, /* Turn on debugging. */
  339. LOOPBACK = 3, /* Is a loopback net. */
  340. POINT_TO_POINT = 4, /* Interface is point-to-point link. */
  341. NO_TRAILERS = 5, /* Avoid use of trailers. */
  342. RUNNING = 6, /* Resources allocated. */
  343. NOARP = 7, /* No address resolution protocol. */
  344. PROMISC = 8, /* Receive all packets. */
  345. ALL_MULTI = 9, /* Receive all multicast packets. Unimplemented. */
  346. MASTER = 10, /* Master of a load balancer. */
  347. SLAVE = 11, /* Slave of a load balancer. */
  348. MULTICAST = 12, /* Supports multicast. */
  349. PORTSEL = 13, /* Can set media type. */
  350. AUTOMEDIA = 14, /* Auto media select active. */
  351. DYNAMIC = 15, /* Dialup device with changing addresses. */
  352. LOWER_UP = 16,
  353. DORMANT = 17,
  354. ECHO = 18,
  355. }
  356. SIOCGIFFLAGS :: bit_set[SIOCGIFFLAG; c.int]
  357. ifaddrs :: struct {
  358. next: ^ifaddrs,
  359. name: cstring,
  360. flags: SIOCGIFFLAGS,
  361. address: ^SOCKADDR,
  362. netmask: ^SOCKADDR,
  363. broadcast_or_dest: ^SOCKADDR, // Broadcast or Point-to-Point address
  364. data: rawptr, // Address-specific data.
  365. }
  366. Dir :: distinct rawptr // DIR*
  367. // File type
  368. S_IFMT :: 0o170000 // Type of file mask
  369. S_IFIFO :: 0o010000 // Named pipe (fifo)
  370. S_IFCHR :: 0o020000 // Character special
  371. S_IFDIR :: 0o040000 // Directory
  372. S_IFBLK :: 0o060000 // Block special
  373. S_IFREG :: 0o100000 // Regular
  374. S_IFLNK :: 0o120000 // Symbolic link
  375. S_IFSOCK :: 0o140000 // Socket
  376. // File mode
  377. // Read, write, execute/search by owner
  378. S_IRWXU :: 0o0700 // RWX mask for owner
  379. S_IRUSR :: 0o0400 // R for owner
  380. S_IWUSR :: 0o0200 // W for owner
  381. S_IXUSR :: 0o0100 // X for owner
  382. // Read, write, execute/search by group
  383. S_IRWXG :: 0o0070 // RWX mask for group
  384. S_IRGRP :: 0o0040 // R for group
  385. S_IWGRP :: 0o0020 // W for group
  386. S_IXGRP :: 0o0010 // X for group
  387. // Read, write, execute/search by others
  388. S_IRWXO :: 0o0007 // RWX mask for other
  389. S_IROTH :: 0o0004 // R for other
  390. S_IWOTH :: 0o0002 // W for other
  391. S_IXOTH :: 0o0001 // X for other
  392. S_ISUID :: 0o4000 // Set user id on execution
  393. S_ISGID :: 0o2000 // Set group id on execution
  394. S_ISVTX :: 0o1000 // Directory restrcted delete
  395. S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK }
  396. S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG }
  397. S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR }
  398. S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR }
  399. S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK }
  400. S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO }
  401. S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK }
  402. F_OK :: 0 // Test for file existance
  403. X_OK :: 1 // Test for execute permission
  404. W_OK :: 2 // Test for write permission
  405. R_OK :: 4 // Test for read permission
  406. AT_FDCWD :: ~uintptr(99) /* -100 */
  407. AT_REMOVEDIR :: uintptr(0x200)
  408. AT_SYMLINK_NOFOLLOW :: uintptr(0x100)
  409. pollfd :: struct {
  410. fd: c.int,
  411. events: c.short,
  412. revents: c.short,
  413. }
  414. sigset_t :: distinct u64
  415. foreign libc {
  416. @(link_name="__errno_location") __errno_location :: proc() -> ^c.int ---
  417. @(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
  418. @(link_name="get_nprocs") _unix_get_nprocs :: proc() -> c.int ---
  419. @(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
  420. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
  421. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
  422. @(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  423. @(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---
  424. @(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---
  425. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  426. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---
  427. @(link_name="execvp") _unix_execvp :: proc(path: cstring, argv: [^]cstring) -> int ---
  428. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
  429. @(link_name="putenv") _unix_putenv :: proc(cstring) -> c.int ---
  430. @(link_name="setenv") _unix_setenv :: proc(key: cstring, value: cstring, overwrite: c.int) -> c.int ---
  431. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  432. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
  433. }
  434. foreign dl {
  435. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---
  436. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
  437. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---
  438. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
  439. @(link_name="getifaddrs") _getifaddrs :: proc(ifap: ^^ifaddrs) -> (c.int) ---
  440. @(link_name="freeifaddrs") _freeifaddrs :: proc(ifa: ^ifaddrs) ---
  441. }
  442. is_path_separator :: proc(r: rune) -> bool {
  443. return r == '/'
  444. }
  445. // determine errno from syscall return value
  446. @private
  447. _get_errno :: proc(res: int) -> Errno {
  448. if res < 0 && res > -4096 {
  449. return Errno(-res)
  450. }
  451. return 0
  452. }
  453. // get errno from libc
  454. get_last_error :: proc "contextless" () -> int {
  455. return int(__errno_location()^)
  456. }
  457. personality :: proc(persona: u64) -> (Errno) {
  458. res := unix.sys_personality(persona)
  459. if res == -1 {
  460. return _get_errno(res)
  461. }
  462. return ERROR_NONE
  463. }
  464. fork :: proc() -> (Pid, Errno) {
  465. pid := unix.sys_fork()
  466. if pid == -1 {
  467. return -1, _get_errno(pid)
  468. }
  469. return Pid(pid), ERROR_NONE
  470. }
  471. execvp :: proc(path: string, args: []string) -> Errno {
  472. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  473. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  474. args_cstrs := make([]cstring, len(args) + 2, context.temp_allocator)
  475. args_cstrs[0] = strings.clone_to_cstring(path, context.temp_allocator)
  476. for i := 0; i < len(args); i += 1 {
  477. args_cstrs[i+1] = strings.clone_to_cstring(args[i], context.temp_allocator)
  478. }
  479. _unix_execvp(path_cstr, raw_data(args_cstrs))
  480. return Errno(get_last_error())
  481. }
  482. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0o000) -> (Handle, Errno) {
  483. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  484. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  485. handle := unix.sys_open(cstr, flags, uint(mode))
  486. if handle < 0 {
  487. return INVALID_HANDLE, _get_errno(handle)
  488. }
  489. return Handle(handle), ERROR_NONE
  490. }
  491. close :: proc(fd: Handle) -> Errno {
  492. return _get_errno(unix.sys_close(int(fd)))
  493. }
  494. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  495. if len(data) == 0 {
  496. return 0, ERROR_NONE
  497. }
  498. bytes_read := unix.sys_read(int(fd), raw_data(data), len(data))
  499. if bytes_read < 0 {
  500. return -1, _get_errno(bytes_read)
  501. }
  502. return bytes_read, ERROR_NONE
  503. }
  504. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  505. if len(data) == 0 {
  506. return 0, ERROR_NONE
  507. }
  508. bytes_written := unix.sys_write(int(fd), raw_data(data), len(data))
  509. if bytes_written < 0 {
  510. return -1, _get_errno(bytes_written)
  511. }
  512. return bytes_written, ERROR_NONE
  513. }
  514. read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  515. if len(data) == 0 {
  516. return 0, ERROR_NONE
  517. }
  518. bytes_read := unix.sys_pread(int(fd), raw_data(data), len(data), offset)
  519. if bytes_read < 0 {
  520. return -1, _get_errno(bytes_read)
  521. }
  522. return bytes_read, ERROR_NONE
  523. }
  524. write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  525. if len(data) == 0 {
  526. return 0, ERROR_NONE
  527. }
  528. bytes_written := unix.sys_pwrite(int(fd), raw_data(data), uint(len(data)), offset)
  529. if bytes_written < 0 {
  530. return -1, _get_errno(bytes_written)
  531. }
  532. return bytes_written, ERROR_NONE
  533. }
  534. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  535. res := unix.sys_lseek(int(fd), offset, whence)
  536. if res < 0 {
  537. return -1, _get_errno(int(res))
  538. }
  539. return i64(res), ERROR_NONE
  540. }
  541. file_size :: proc(fd: Handle) -> (i64, Errno) {
  542. // deliberately uninitialized; the syscall fills this buffer for us
  543. s: OS_Stat = ---
  544. result := unix.sys_fstat(int(fd), rawptr(&s))
  545. if result < 0 {
  546. return 0, _get_errno(result)
  547. }
  548. return max(s.size, 0), ERROR_NONE
  549. }
  550. rename :: proc(old_path, new_path: string) -> Errno {
  551. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  552. old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
  553. new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
  554. return _get_errno(unix.sys_rename(old_path_cstr, new_path_cstr))
  555. }
  556. remove :: proc(path: string) -> Errno {
  557. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  558. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  559. return _get_errno(unix.sys_unlink(path_cstr))
  560. }
  561. make_directory :: proc(path: string, mode: u32 = 0o775) -> Errno {
  562. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  563. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  564. return _get_errno(unix.sys_mkdir(path_cstr, uint(mode)))
  565. }
  566. remove_directory :: proc(path: string) -> Errno {
  567. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  568. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  569. return _get_errno(unix.sys_rmdir(path_cstr))
  570. }
  571. is_file_handle :: proc(fd: Handle) -> bool {
  572. s, err := _fstat(fd)
  573. if err != ERROR_NONE {
  574. return false
  575. }
  576. return S_ISREG(s.mode)
  577. }
  578. is_file_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_ISREG(s.mode)
  590. }
  591. is_dir_handle :: proc(fd: Handle) -> bool {
  592. s, err := _fstat(fd)
  593. if err != ERROR_NONE {
  594. return false
  595. }
  596. return S_ISDIR(s.mode)
  597. }
  598. is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
  599. s: OS_Stat
  600. err: Errno
  601. if follow_links {
  602. s, err = _stat(path)
  603. } else {
  604. s, err = _lstat(path)
  605. }
  606. if err != ERROR_NONE {
  607. return false
  608. }
  609. return S_ISDIR(s.mode)
  610. }
  611. is_file :: proc {is_file_path, is_file_handle}
  612. is_dir :: proc {is_dir_path, is_dir_handle}
  613. exists :: proc(path: string) -> bool {
  614. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  615. cpath := strings.clone_to_cstring(path, context.temp_allocator)
  616. res := unix.sys_access(cpath, O_RDONLY)
  617. return res == 0
  618. }
  619. // NOTE(bill): Uses startup to initialize it
  620. stdin: Handle = 0
  621. stdout: Handle = 1
  622. stderr: Handle = 2
  623. /* TODO(zangent): Implement these!
  624. last_write_time :: proc(fd: Handle) -> File_Time {}
  625. last_write_time_by_name :: proc(name: string) -> File_Time {}
  626. */
  627. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  628. s, err := _fstat(fd)
  629. if err != ERROR_NONE {
  630. return 0, err
  631. }
  632. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  633. return File_Time(modified), ERROR_NONE
  634. }
  635. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  636. s, err := _stat(name)
  637. if err != ERROR_NONE {
  638. return 0, err
  639. }
  640. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  641. return File_Time(modified), ERROR_NONE
  642. }
  643. @private
  644. _stat :: proc(path: string) -> (OS_Stat, Errno) {
  645. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  646. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  647. // deliberately uninitialized; the syscall fills this buffer for us
  648. s: OS_Stat = ---
  649. result := unix.sys_stat(cstr, &s)
  650. if result < 0 {
  651. return s, _get_errno(result)
  652. }
  653. return s, ERROR_NONE
  654. }
  655. @private
  656. _lstat :: proc(path: string) -> (OS_Stat, Errno) {
  657. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  658. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  659. // deliberately uninitialized; the syscall fills this buffer for us
  660. s: OS_Stat = ---
  661. result := unix.sys_lstat(cstr, &s)
  662. if result < 0 {
  663. return s, _get_errno(result)
  664. }
  665. return s, ERROR_NONE
  666. }
  667. @private
  668. _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  669. // deliberately uninitialized; the syscall fills this buffer for us
  670. s: OS_Stat = ---
  671. result := unix.sys_fstat(int(fd), rawptr(&s))
  672. if result < 0 {
  673. return s, _get_errno(result)
  674. }
  675. return s, ERROR_NONE
  676. }
  677. @private
  678. _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
  679. dirp := _unix_fdopendir(fd)
  680. if dirp == cast(Dir)nil {
  681. return nil, Errno(get_last_error())
  682. }
  683. return dirp, ERROR_NONE
  684. }
  685. @private
  686. _closedir :: proc(dirp: Dir) -> Errno {
  687. rc := _unix_closedir(dirp)
  688. if rc != 0 {
  689. return Errno(get_last_error())
  690. }
  691. return ERROR_NONE
  692. }
  693. @private
  694. _rewinddir :: proc(dirp: Dir) {
  695. _unix_rewinddir(dirp)
  696. }
  697. @private
  698. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
  699. result: ^Dirent
  700. rc := _unix_readdir_r(dirp, &entry, &result)
  701. if rc != 0 {
  702. err = Errno(get_last_error())
  703. return
  704. }
  705. err = ERROR_NONE
  706. if result == nil {
  707. end_of_stream = true
  708. return
  709. }
  710. end_of_stream = false
  711. return
  712. }
  713. @private
  714. _readlink :: proc(path: string) -> (string, Errno) {
  715. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  716. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  717. bufsz : uint = 256
  718. buf := make([]byte, bufsz)
  719. for {
  720. rc := unix.sys_readlink(path_cstr, &(buf[0]), bufsz)
  721. if rc < 0 {
  722. delete(buf)
  723. return "", _get_errno(rc)
  724. } else if rc == int(bufsz) {
  725. // NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
  726. bufsz *= 2
  727. delete(buf)
  728. buf = make([]byte, bufsz)
  729. } else {
  730. return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
  731. }
  732. }
  733. }
  734. absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
  735. buf : [256]byte
  736. fd_str := strconv.itoa( buf[:], cast(int)fd )
  737. procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } )
  738. defer delete(procfs_path)
  739. return _readlink(procfs_path)
  740. }
  741. absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
  742. rel := rel
  743. if rel == "" {
  744. rel = "."
  745. }
  746. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  747. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  748. path_ptr := _unix_realpath(rel_cstr, nil)
  749. if path_ptr == nil {
  750. return "", Errno(get_last_error())
  751. }
  752. defer _unix_free(path_ptr)
  753. path_cstr := transmute(cstring)path_ptr
  754. path = strings.clone( string(path_cstr) )
  755. return path, ERROR_NONE
  756. }
  757. access :: proc(path: string, mask: int) -> (bool, Errno) {
  758. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  759. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  760. result := unix.sys_access(cstr, mask)
  761. if result < 0 {
  762. return false, _get_errno(result)
  763. }
  764. return true, ERROR_NONE
  765. }
  766. heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
  767. if size <= 0 {
  768. return nil
  769. }
  770. if zero_memory {
  771. return _unix_calloc(1, c.size_t(size))
  772. } else {
  773. return _unix_malloc(c.size_t(size))
  774. }
  775. }
  776. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  777. // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
  778. // POSIX platforms. Ensure your caller takes this into account.
  779. return _unix_realloc(ptr, c.size_t(new_size))
  780. }
  781. heap_free :: proc(ptr: rawptr) {
  782. _unix_free(ptr)
  783. }
  784. lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
  785. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  786. path_str := strings.clone_to_cstring(key, context.temp_allocator)
  787. // NOTE(tetra): Lifetime of 'cstr' is unclear, but _unix_free(cstr) segfaults.
  788. cstr := _unix_getenv(path_str)
  789. if cstr == nil {
  790. return "", false
  791. }
  792. return strings.clone(string(cstr), allocator), true
  793. }
  794. get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
  795. value, _ = lookup_env(key, allocator)
  796. return
  797. }
  798. set_env :: proc(key, value: string) -> Errno {
  799. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  800. key_cstring := strings.clone_to_cstring(key, context.temp_allocator)
  801. value_cstring := strings.clone_to_cstring(value, context.temp_allocator)
  802. // NOTE(GoNZooo): `setenv` instead of `putenv` because it copies both key and value more commonly
  803. res := _unix_setenv(key_cstring, value_cstring, 1)
  804. if res < 0 {
  805. return Errno(get_last_error())
  806. }
  807. return ERROR_NONE
  808. }
  809. unset_env :: proc(key: string) -> Errno {
  810. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  811. s := strings.clone_to_cstring(key, context.temp_allocator)
  812. res := _unix_putenv(s)
  813. if res < 0 {
  814. return Errno(get_last_error())
  815. }
  816. return ERROR_NONE
  817. }
  818. get_current_directory :: proc() -> string {
  819. // NOTE(tetra): I would use PATH_MAX here, but I was not able to find
  820. // an authoritative value for it across all systems.
  821. // The largest value I could find was 4096, so might as well use the page size.
  822. page_size := get_page_size()
  823. buf := make([dynamic]u8, page_size)
  824. for {
  825. #no_bounds_check res := unix.sys_getcwd(&buf[0], uint(len(buf)))
  826. if res >= 0 {
  827. return strings.string_from_null_terminated_ptr(&buf[0], len(buf))
  828. }
  829. if _get_errno(res) != ERANGE {
  830. delete(buf)
  831. return ""
  832. }
  833. resize(&buf, len(buf)+page_size)
  834. }
  835. unreachable()
  836. }
  837. set_current_directory :: proc(path: string) -> (err: Errno) {
  838. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  839. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  840. res := unix.sys_chdir(cstr)
  841. if res < 0 {
  842. return _get_errno(res)
  843. }
  844. return ERROR_NONE
  845. }
  846. exit :: proc "contextless" (code: int) -> ! {
  847. runtime._cleanup_runtime_contextless()
  848. _unix_exit(c.int(code))
  849. }
  850. current_thread_id :: proc "contextless" () -> int {
  851. return unix.sys_gettid()
  852. }
  853. dlopen :: proc(filename: string, flags: int) -> rawptr {
  854. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  855. cstr := strings.clone_to_cstring(filename, context.temp_allocator)
  856. handle := _unix_dlopen(cstr, c.int(flags))
  857. return handle
  858. }
  859. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  860. assert(handle != nil)
  861. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  862. cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
  863. proc_handle := _unix_dlsym(handle, cstr)
  864. return proc_handle
  865. }
  866. dlclose :: proc(handle: rawptr) -> bool {
  867. assert(handle != nil)
  868. return _unix_dlclose(handle) == 0
  869. }
  870. dlerror :: proc() -> string {
  871. return string(_unix_dlerror())
  872. }
  873. get_page_size :: proc() -> int {
  874. // NOTE(tetra): The page size never changes, so why do anything complicated
  875. // if we don't have to.
  876. @static page_size := -1
  877. if page_size != -1 {
  878. return page_size
  879. }
  880. page_size = int(_unix_getpagesize())
  881. return page_size
  882. }
  883. @(private)
  884. _processor_core_count :: proc() -> int {
  885. return int(_unix_get_nprocs())
  886. }
  887. _alloc_command_line_arguments :: proc() -> []string {
  888. res := make([]string, len(runtime.args__))
  889. for arg, i in runtime.args__ {
  890. res[i] = string(arg)
  891. }
  892. return res
  893. }
  894. socket :: proc(domain: int, type: int, protocol: int) -> (Socket, Errno) {
  895. result := unix.sys_socket(domain, type, protocol)
  896. if result < 0 {
  897. return 0, _get_errno(result)
  898. }
  899. return Socket(result), ERROR_NONE
  900. }
  901. bind :: proc(sd: Socket, addr: ^SOCKADDR, len: socklen_t) -> (Errno) {
  902. result := unix.sys_bind(int(sd), addr, len)
  903. if result < 0 {
  904. return _get_errno(result)
  905. }
  906. return ERROR_NONE
  907. }
  908. connect :: proc(sd: Socket, addr: ^SOCKADDR, len: socklen_t) -> (Errno) {
  909. result := unix.sys_connect(int(sd), addr, len)
  910. if result < 0 {
  911. return _get_errno(result)
  912. }
  913. return ERROR_NONE
  914. }
  915. accept :: proc(sd: Socket, addr: ^SOCKADDR, len: rawptr) -> (Socket, Errno) {
  916. result := unix.sys_accept(int(sd), rawptr(addr), len)
  917. if result < 0 {
  918. return 0, _get_errno(result)
  919. }
  920. return Socket(result), ERROR_NONE
  921. }
  922. listen :: proc(sd: Socket, backlog: int) -> (Errno) {
  923. result := unix.sys_listen(int(sd), backlog)
  924. if result < 0 {
  925. return _get_errno(result)
  926. }
  927. return ERROR_NONE
  928. }
  929. setsockopt :: proc(sd: Socket, level: int, optname: int, optval: rawptr, optlen: socklen_t) -> (Errno) {
  930. result := unix.sys_setsockopt(int(sd), level, optname, optval, optlen)
  931. if result < 0 {
  932. return _get_errno(result)
  933. }
  934. return ERROR_NONE
  935. }
  936. recvfrom :: proc(sd: Socket, data: []byte, flags: int, addr: ^SOCKADDR, addr_size: ^socklen_t) -> (u32, Errno) {
  937. result := unix.sys_recvfrom(int(sd), raw_data(data), len(data), flags, addr, uintptr(addr_size))
  938. if result < 0 {
  939. return 0, _get_errno(int(result))
  940. }
  941. return u32(result), ERROR_NONE
  942. }
  943. recv :: proc(sd: Socket, data: []byte, flags: int) -> (u32, Errno) {
  944. result := unix.sys_recvfrom(int(sd), raw_data(data), len(data), flags, nil, 0)
  945. if result < 0 {
  946. return 0, _get_errno(int(result))
  947. }
  948. return u32(result), ERROR_NONE
  949. }
  950. sendto :: proc(sd: Socket, data: []u8, flags: int, addr: ^SOCKADDR, addrlen: socklen_t) -> (u32, Errno) {
  951. result := unix.sys_sendto(int(sd), raw_data(data), len(data), flags, addr, addrlen)
  952. if result < 0 {
  953. return 0, _get_errno(int(result))
  954. }
  955. return u32(result), ERROR_NONE
  956. }
  957. send :: proc(sd: Socket, data: []byte, flags: int) -> (u32, Errno) {
  958. result := unix.sys_sendto(int(sd), raw_data(data), len(data), 0, nil, 0)
  959. if result < 0 {
  960. return 0, _get_errno(int(result))
  961. }
  962. return u32(result), ERROR_NONE
  963. }
  964. shutdown :: proc(sd: Socket, how: int) -> (Errno) {
  965. result := unix.sys_shutdown(int(sd), how)
  966. if result < 0 {
  967. return _get_errno(result)
  968. }
  969. return ERROR_NONE
  970. }
  971. fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
  972. result := unix.sys_fcntl(fd, cmd, arg)
  973. if result < 0 {
  974. return 0, _get_errno(result)
  975. }
  976. return result, ERROR_NONE
  977. }
  978. poll :: proc(fds: []pollfd, timeout: int) -> (int, Errno) {
  979. result := unix.sys_poll(raw_data(fds), uint(len(fds)), timeout)
  980. if result < 0 {
  981. return 0, _get_errno(result)
  982. }
  983. return result, ERROR_NONE
  984. }
  985. ppoll :: proc(fds: []pollfd, timeout: ^unix.timespec, sigmask: ^sigset_t) -> (int, Errno) {
  986. result := unix.sys_ppoll(raw_data(fds), uint(len(fds)), timeout, sigmask, size_of(sigset_t))
  987. if result < 0 {
  988. return 0, _get_errno(result)
  989. }
  990. return result, ERROR_NONE
  991. }