os_linux.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "core:runtime"
  5. import "core:strings"
  6. OS :: "linux";
  7. Handle :: distinct i32;
  8. File_Time :: distinct u64;
  9. Errno :: distinct i32;
  10. Syscall :: distinct int;
  11. INVALID_HANDLE :: ~Handle(0);
  12. ERROR_NONE: Errno : 0;
  13. EPERM: Errno : 1;
  14. ENOENT: Errno : 2;
  15. EINTR: Errno : 4;
  16. EIO: Errno : 5;
  17. ENXIO: Errno : 6;
  18. EBADF: Errno : 9;
  19. EAGAIN: Errno : 11;
  20. EWOULDBLOCK: Errno : EAGAIN;
  21. ENOMEM: Errno : 12;
  22. EACCES: Errno : 13;
  23. EFAULT: Errno : 14;
  24. EEXIST: Errno : 17;
  25. ENODEV: Errno : 19;
  26. ENOTDIR: Errno : 20;
  27. EISDIR: Errno : 21;
  28. EINVAL: Errno : 22;
  29. ENFILE: Errno : 23;
  30. EMFILE: Errno : 24;
  31. ETXTBSY: Errno : 26;
  32. EFBIG: Errno : 27;
  33. ENOSPC: Errno : 28;
  34. ESPIPE: Errno : 29;
  35. EROFS: Errno : 30;
  36. EPIPE: Errno : 32;
  37. ENAMETOOLONG: Errno : 36;
  38. ELOOP: Errno : 40;
  39. EOVERFLOW: Errno : 75;
  40. EDESTADDRREQ: Errno : 89;
  41. EOPNOTSUPP: Errno : 95;
  42. EDQUOT: Errno : 122;
  43. O_RDONLY :: 0x00000;
  44. O_WRONLY :: 0x00001;
  45. O_RDWR :: 0x00002;
  46. O_CREATE :: 0x00040;
  47. O_EXCL :: 0x00080;
  48. O_NOCTTY :: 0x00100;
  49. O_TRUNC :: 0x00200;
  50. O_NONBLOCK :: 0x00800;
  51. O_APPEND :: 0x00400;
  52. O_SYNC :: 0x01000;
  53. O_ASYNC :: 0x02000;
  54. O_CLOEXEC :: 0x80000;
  55. SEEK_SET :: 0;
  56. SEEK_CUR :: 1;
  57. SEEK_END :: 2;
  58. SEEK_DATA :: 3;
  59. SEEK_HOLE :: 4;
  60. SEEK_MAX :: SEEK_HOLE;
  61. // NOTE(zangent): These are OS specific!
  62. // Do not mix these up!
  63. RTLD_LAZY :: 0x001;
  64. RTLD_NOW :: 0x002;
  65. RTLD_BINDING_MASK :: 0x3;
  66. RTLD_GLOBAL :: 0x100;
  67. // "Argv" arguments converted to Odin strings
  68. args := _alloc_command_line_arguments();
  69. _File_Time :: struct {
  70. seconds: i64,
  71. nanoseconds: i64,
  72. }
  73. Stat :: struct {
  74. device_id: u64, // ID of device containing file
  75. serial: u64, // File serial number
  76. nlink: u64, // Number of hard links
  77. mode: u32, // Mode of the file
  78. uid: u32, // User ID of the file's owner
  79. gid: u32, // Group ID of the file's group
  80. _padding: i32, // 32 bits of padding
  81. rdev: u64, // Device ID, if device
  82. size: i64, // Size of the file, in bytes
  83. block_size: i64, // Optimal bllocksize for I/O
  84. blocks: i64, // Number of 512-byte blocks allocated
  85. last_access: _File_Time, // Time of last access
  86. modified: _File_Time, // Time of last modification
  87. status_change: _File_Time, // Time of last status change
  88. _reserve1,
  89. _reserve2,
  90. _reserve3: i64,
  91. };
  92. // File type
  93. S_IFMT :: 0o170000; // Type of file mask
  94. S_IFIFO :: 0o010000; // Named pipe (fifo)
  95. S_IFCHR :: 0o020000; // Character special
  96. S_IFDIR :: 0o040000; // Directory
  97. S_IFBLK :: 0o060000; // Block special
  98. S_IFREG :: 0o100000; // Regular
  99. S_IFLNK :: 0o120000; // Symbolic link
  100. S_IFSOCK :: 0o140000; // Socket
  101. // File mode
  102. // Read, write, execute/search by owner
  103. S_IRWXU :: 0o0700; // RWX mask for owner
  104. S_IRUSR :: 0o0400; // R for owner
  105. S_IWUSR :: 0o0200; // W for owner
  106. S_IXUSR :: 0o0100; // X for owner
  107. // Read, write, execute/search by group
  108. S_IRWXG :: 0o0070; // RWX mask for group
  109. S_IRGRP :: 0o0040; // R for group
  110. S_IWGRP :: 0o0020; // W for group
  111. S_IXGRP :: 0o0010; // X for group
  112. // Read, write, execute/search by others
  113. S_IRWXO :: 0o0007; // RWX mask for other
  114. S_IROTH :: 0o0004; // R for other
  115. S_IWOTH :: 0o0002; // W for other
  116. S_IXOTH :: 0o0001; // X for other
  117. S_ISUID :: 0o4000; // Set user id on execution
  118. S_ISGID :: 0o2000; // Set group id on execution
  119. S_ISVTX :: 0o1000; // Directory restrcted delete
  120. S_ISLNK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFLNK;
  121. S_ISREG :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFREG;
  122. S_ISDIR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFDIR;
  123. S_ISCHR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFCHR;
  124. S_ISBLK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFBLK;
  125. S_ISFIFO :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFIFO;
  126. S_ISSOCK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFSOCK;
  127. F_OK :: 0; // Test for file existance
  128. X_OK :: 1; // Test for execute permission
  129. W_OK :: 2; // Test for write permission
  130. R_OK :: 4; // Test for read permission
  131. TimeSpec :: struct {
  132. tv_sec : i64, /* seconds */
  133. tv_nsec : i64, /* nanoseconds */
  134. };
  135. CLOCK_REALTIME :: 0;
  136. CLOCK_MONOTONIC :: 1;
  137. CLOCK_PROCESS_CPUTIME_ID :: 2;
  138. CLOCK_THREAD_CPUTIME_ID :: 3;
  139. CLOCK_MONOTONIC_RAW :: 4;
  140. CLOCK_REALTIME_COARSE :: 5;
  141. CLOCK_MONOTONIC_COARSE :: 6;
  142. CLOCK_BOOTTIME :: 7;
  143. CLOCK_REALTIME_ALARM :: 8;
  144. CLOCK_BOOTTIME_ALARM :: 9;
  145. SYS_GETTID: Syscall : 186;
  146. foreign libc {
  147. @(link_name="__errno_location") __errno_location :: proc() -> ^int ---;
  148. @(link_name="syscall") syscall :: proc(number: Syscall, #c_vararg args: ..any) -> int ---;
  149. @(link_name="open") _unix_open :: proc(path: cstring, flags: int, #c_vararg mode: ..any) -> Handle ---;
  150. @(link_name="close") _unix_close :: proc(fd: Handle) -> int ---;
  151. @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: int) -> int ---;
  152. @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: int) -> int ---;
  153. @(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: i32) -> i64 ---;
  154. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
  155. @(link_name="stat") _unix_stat :: proc(path: cstring, stat: ^Stat) -> int ---;
  156. @(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^Stat) -> int ---;
  157. @(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
  158. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
  159. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
  160. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
  161. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
  162. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
  163. @(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^TimeSpec) ---;
  164. @(link_name="nanosleep") _unix_nanosleep :: proc(requested: ^TimeSpec, remaining: ^TimeSpec) -> int ---;
  165. @(link_name="sleep") _unix_sleep :: proc(seconds: u64) -> int ---;
  166. @(link_name="exit") _unix_exit :: proc(status: int) -> ! ---;
  167. }
  168. foreign dl {
  169. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---;
  170. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
  171. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---;
  172. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
  173. }
  174. is_path_separator :: proc(r: rune) -> bool {
  175. return r == '/';
  176. }
  177. get_last_error :: proc() -> int {
  178. return __errno_location()^;
  179. }
  180. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
  181. cstr := strings.clone_to_cstring(path);
  182. handle := _unix_open(cstr, flags, mode);
  183. delete(cstr);
  184. if handle == -1 {
  185. return INVALID_HANDLE, Errno(get_last_error());
  186. }
  187. return handle, ERROR_NONE;
  188. }
  189. close :: proc(fd: Handle) -> Errno {
  190. result := _unix_close(fd);
  191. if result == -1 {
  192. return Errno(get_last_error());
  193. }
  194. return ERROR_NONE;
  195. }
  196. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  197. bytes_read := _unix_read(fd, &data[0], len(data));
  198. if bytes_read == -1 {
  199. return -1, Errno(get_last_error());
  200. }
  201. return bytes_read, ERROR_NONE;
  202. }
  203. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  204. bytes_written := _unix_write(fd, &data[0], len(data));
  205. if bytes_written == -1 {
  206. return -1, Errno(get_last_error());
  207. }
  208. return bytes_written, ERROR_NONE;
  209. }
  210. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  211. res := _unix_seek(fd, offset, i32(whence));
  212. if res == -1 {
  213. return -1, Errno(get_last_error());
  214. }
  215. return res, ERROR_NONE;
  216. }
  217. file_size :: proc(fd: Handle) -> (i64, Errno) {
  218. s, err := fstat(fd);
  219. if err != ERROR_NONE {
  220. return -1, err;
  221. }
  222. return s.size, ERROR_NONE;
  223. }
  224. // NOTE(bill): Uses startup to initialize it
  225. stdin: Handle = 0;
  226. stdout: Handle = 1;
  227. stderr: Handle = 2;
  228. /* TODO(zangent): Implement these!
  229. last_write_time :: proc(fd: Handle) -> File_Time {}
  230. last_write_time_by_name :: proc(name: string) -> File_Time {}
  231. */
  232. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  233. s, err := fstat(fd);
  234. if err != ERROR_NONE {
  235. return 0, err;
  236. }
  237. return File_Time(s.modified.nanoseconds), ERROR_NONE;
  238. }
  239. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  240. s, err := stat(name);
  241. if err != ERROR_NONE {
  242. return 0, err;
  243. }
  244. return File_Time(s.modified.nanoseconds), ERROR_NONE;
  245. }
  246. stat :: inline proc(path: string) -> (Stat, Errno) {
  247. cstr := strings.clone_to_cstring(path);
  248. defer delete(cstr);
  249. s: Stat;
  250. result := _unix_stat(cstr, &s);
  251. if result == -1 {
  252. return s, Errno(get_last_error());
  253. }
  254. return s, ERROR_NONE;
  255. }
  256. fstat :: inline proc(fd: Handle) -> (Stat, Errno) {
  257. s: Stat;
  258. result := _unix_fstat(fd, &s);
  259. if result == -1 {
  260. return s, Errno(get_last_error());
  261. }
  262. return s, ERROR_NONE;
  263. }
  264. access :: inline proc(path: string, mask: int) -> (bool, Errno) {
  265. cstr := strings.clone_to_cstring(path);
  266. defer delete(cstr);
  267. result := _unix_access(cstr, mask);
  268. if result == -1 {
  269. return false, Errno(get_last_error());
  270. }
  271. return true, ERROR_NONE;
  272. }
  273. heap_alloc :: proc(size: int) -> rawptr {
  274. assert(size >= 0);
  275. return _unix_calloc(1, size);
  276. }
  277. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  278. return _unix_realloc(ptr, new_size);
  279. }
  280. heap_free :: proc(ptr: rawptr) {
  281. _unix_free(ptr);
  282. }
  283. getenv :: proc(name: string) -> (string, bool) {
  284. path_str := strings.clone_to_cstring(name);
  285. defer delete(path_str);
  286. cstr := _unix_getenv(path_str);
  287. if cstr == nil {
  288. return "", false;
  289. }
  290. return string(cstr), true;
  291. }
  292. exit :: proc(code: int) -> ! {
  293. _unix_exit(code);
  294. }
  295. clock_gettime :: proc(clock_id: u64) -> TimeSpec {
  296. ts : TimeSpec;
  297. _unix_clock_gettime(clock_id, &ts);
  298. return ts;
  299. }
  300. sleep :: proc(seconds: u64) -> int {
  301. return _unix_sleep(seconds);
  302. }
  303. nanosleep :: proc(nanoseconds: i64) -> int {
  304. assert(nanoseconds <= 999999999);
  305. requested, remaining : TimeSpec;
  306. requested = TimeSpec{tv_nsec = nanoseconds};
  307. return _unix_nanosleep(&requested, &remaining);
  308. }
  309. current_thread_id :: proc "contextless" () -> int {
  310. return syscall(SYS_GETTID);
  311. }
  312. dlopen :: inline proc(filename: string, flags: int) -> rawptr {
  313. cstr := strings.clone_to_cstring(filename);
  314. defer delete(cstr);
  315. handle := _unix_dlopen(cstr, flags);
  316. return handle;
  317. }
  318. dlsym :: inline proc(handle: rawptr, symbol: string) -> rawptr {
  319. assert(handle != nil);
  320. cstr := strings.clone_to_cstring(symbol);
  321. defer delete(cstr);
  322. proc_handle := _unix_dlsym(handle, cstr);
  323. return proc_handle;
  324. }
  325. dlclose :: inline proc(handle: rawptr) -> bool {
  326. assert(handle != nil);
  327. return _unix_dlclose(handle) == 0;
  328. }
  329. dlerror :: proc() -> string {
  330. return string(_unix_dlerror());
  331. }
  332. _alloc_command_line_arguments :: proc() -> []string {
  333. res := make([]string, len(runtime.args__));
  334. for arg, i in runtime.args__ {
  335. res[i] = string(arg);
  336. }
  337. return res;
  338. }