1const std = @import("../std.zig");
2const builtin = @import("builtin");
3const assert = std.debug.assert;
4const fd_t = std.c.fd_t;
5const off_t = std.c.off_t;
6const dev_t = std.c.dev_t;
7const ino_t = std.c.ino_t;
8
9comptime {
10 assert(builtin.os.tag == .haiku); // Prevent access of std.c symbols on wrong OS.
11}
12
13pub const B_OS_NAME_LENGTH = 32;
14pub const B_ABSOLUTE_TIMEOUT = 0x10;
15pub const B_TIMEOUT_REAL_TIME_BASE = 0x40;
16
17pub extern "root" fn _kern_create_sem(count: c_int, name: ?[*:0]const u8) sem_id;
18pub extern "root" fn _kern_delete_sem(id: sem_id) status_t;
19pub extern "root" fn _kern_acquire_sem_etc(id: sem_id, count: u32, flags: u32, timeout: i64) status_t;
20pub extern "root" fn _kern_release_sem_etc(id: sem_id, count: u32, flags: u32) status_t;
21pub extern "root" fn _kern_open_dir(fd: fd_t, path: [*:0]const u8) fd_t;
22pub extern "root" fn _kern_read_dir(fd: fd_t, buffer: [*]u8, bufferSize: usize, maxCount: u32) isize;
23pub extern "root" fn _kern_rewind_dir(fd: fd_t) status_t;
24pub extern "root" fn _kern_read_stat(fd: fd_t, path: [*:0]const u8, traverseLink: bool, stat: *std.c.Stat, statSize: usize) status_t;
25
26pub extern "root" fn on_exit_thread(callback: *const fn (?*anyopaque) callconv(.c) void, data: ?*anyopaque) status_t;
27pub extern "root" fn find_thread(name: ?[*:0]const u8) thread_id;
28pub extern "root" fn get_system_info(info: *system_info) status_t;
29
30pub extern "root" fn _errnop() *i32;
31
32pub extern "root" fn readv_pos(fd: fd_t, pos: off_t, vec: [*]const std.c.iovec, count: i32) isize;
33pub extern "root" fn writev_pos(fd: fd_t, pos: off_t, vec: [*]const std.c.iovec_const, count: i32) isize;
34
35pub const system_info = extern struct {
36 boot_time: i64,
37 cpu_count: u32,
38 max_pages: u64,
39 used_pages: u64,
40 cached_pages: u64,
41 block_cache_pages: u64,
42 ignored_pages: u64,
43 needed_memory: u64,
44 free_memory: u64,
45 max_swap_pages: u64,
46 free_swap_pages: u64,
47 page_faults: u32,
48 max_sems: u32,
49 used_sems: u32,
50 max_ports: u32,
51 used_ports: u32,
52 max_threads: u32,
53 used_threads: u32,
54 max_teams: u32,
55 used_teams: u32,
56 kernel_name: [256]u8,
57 kernel_build_date: [B_OS_NAME_LENGTH]u8,
58 kernel_build_time: [B_OS_NAME_LENGTH]u8,
59 kernel_version: i64,
60 abi: u32,
61};
62
63pub const area_id = i32;
64pub const port_id = i32;
65pub const sem_id = i32;
66pub const team_id = i32;
67pub const thread_id = i32;
68
69pub const E = enum(i32) {
70 pub const B_GENERAL_ERROR_BASE: i32 = std.math.minInt(i32);
71 pub const B_OS_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x1000;
72 pub const B_APP_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x2000;
73 pub const B_INTERFACE_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x3000;
74 pub const B_MEDIA_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x4000;
75 pub const B_TRANSLATION_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x4800;
76 pub const B_MIDI_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x5000;
77 pub const B_STORAGE_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x6000;
78 pub const B_POSIX_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x7000;
79 pub const B_MAIL_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x8000;
80 pub const B_PRINT_ERROR_BASE = B_GENERAL_ERROR_BASE + 0x9000;
81 pub const B_DEVICE_ERROR_BASE = B_GENERAL_ERROR_BASE + 0xa000;
82
83 pub const B_ERRORS_END = B_GENERAL_ERROR_BASE + 0xffff;
84
85 pub const B_NO_MEMORY = B_GENERAL_ERROR_BASE + 0;
86 pub const B_IO_ERROR = B_GENERAL_ERROR_BASE + 1;
87 pub const B_PERMISSION_DENIED = B_GENERAL_ERROR_BASE + 2;
88 pub const B_BAD_INDEX = B_GENERAL_ERROR_BASE + 3;
89 pub const B_BAD_TYPE = B_GENERAL_ERROR_BASE + 4;
90 pub const B_BAD_VALUE = B_GENERAL_ERROR_BASE + 5;
91 pub const B_MISMATCHED_VALUES = B_GENERAL_ERROR_BASE + 6;
92 pub const B_NAME_NOT_FOUND = B_GENERAL_ERROR_BASE + 7;
93 pub const B_NAME_IN_USE = B_GENERAL_ERROR_BASE + 8;
94 pub const B_TIMED_OUT = B_GENERAL_ERROR_BASE + 9;
95 pub const B_INTERRUPTED = B_GENERAL_ERROR_BASE + 10;
96 pub const B_WOULD_BLOCK = B_GENERAL_ERROR_BASE + 11;
97 pub const B_CANCELED = B_GENERAL_ERROR_BASE + 12;
98 pub const B_NO_INIT = B_GENERAL_ERROR_BASE + 13;
99 pub const B_NOT_INITIALIZED = B_GENERAL_ERROR_BASE + 13;
100 pub const B_BUSY = B_GENERAL_ERROR_BASE + 14;
101 pub const B_NOT_ALLOWED = B_GENERAL_ERROR_BASE + 15;
102 pub const B_BAD_DATA = B_GENERAL_ERROR_BASE + 16;
103 pub const B_DONT_DO_THAT = B_GENERAL_ERROR_BASE + 17;
104
105 pub const B_BAD_IMAGE_ID = B_OS_ERROR_BASE + 0x300;
106 pub const B_BAD_ADDRESS = B_OS_ERROR_BASE + 0x301;
107 pub const B_NOT_AN_EXECUTABLE = B_OS_ERROR_BASE + 0x302;
108 pub const B_MISSING_LIBRARY = B_OS_ERROR_BASE + 0x303;
109 pub const B_MISSING_SYMBOL = B_OS_ERROR_BASE + 0x304;
110 pub const B_UNKNOWN_EXECUTABLE = B_OS_ERROR_BASE + 0x305;
111 pub const B_LEGACY_EXECUTABLE = B_OS_ERROR_BASE + 0x306;
112
113 pub const B_FILE_ERROR = B_STORAGE_ERROR_BASE + 0;
114 pub const B_FILE_EXISTS = B_STORAGE_ERROR_BASE + 2;
115 pub const B_ENTRY_NOT_FOUND = B_STORAGE_ERROR_BASE + 3;
116 pub const B_NAME_TOO_LONG = B_STORAGE_ERROR_BASE + 4;
117 pub const B_NOT_A_DIRECTORY = B_STORAGE_ERROR_BASE + 5;
118 pub const B_DIRECTORY_NOT_EMPTY = B_STORAGE_ERROR_BASE + 6;
119 pub const B_DEVICE_FULL = B_STORAGE_ERROR_BASE + 7;
120 pub const B_READ_ONLY_DEVICE = B_STORAGE_ERROR_BASE + 8;
121 pub const B_IS_A_DIRECTORY = B_STORAGE_ERROR_BASE + 9;
122 pub const B_NO_MORE_FDS = B_STORAGE_ERROR_BASE + 10;
123 pub const B_CROSS_DEVICE_LINK = B_STORAGE_ERROR_BASE + 11;
124 pub const B_LINK_LIMIT = B_STORAGE_ERROR_BASE + 12;
125 pub const B_BUSTED_PIPE = B_STORAGE_ERROR_BASE + 13;
126 pub const B_UNSUPPORTED = B_STORAGE_ERROR_BASE + 14;
127 pub const B_PARTITION_TOO_SMALL = B_STORAGE_ERROR_BASE + 15;
128 pub const B_PARTIAL_READ = B_STORAGE_ERROR_BASE + 16;
129 pub const B_PARTIAL_WRITE = B_STORAGE_ERROR_BASE + 17;
130
131 SUCCESS = 0,
132
133 @"2BIG" = B_POSIX_ERROR_BASE + 1,
134 CHILD = B_POSIX_ERROR_BASE + 2,
135 DEADLK = B_POSIX_ERROR_BASE + 3,
136 FBIG = B_POSIX_ERROR_BASE + 4,
137 MLINK = B_POSIX_ERROR_BASE + 5,
138 NFILE = B_POSIX_ERROR_BASE + 6,
139 NODEV = B_POSIX_ERROR_BASE + 7,
140 NOLCK = B_POSIX_ERROR_BASE + 8,
141 NOSYS = B_POSIX_ERROR_BASE + 9,
142 NOTTY = B_POSIX_ERROR_BASE + 10,
143 NXIO = B_POSIX_ERROR_BASE + 11,
144 SPIPE = B_POSIX_ERROR_BASE + 12,
145 SRCH = B_POSIX_ERROR_BASE + 13,
146 FPOS = B_POSIX_ERROR_BASE + 14,
147 SIGPARM = B_POSIX_ERROR_BASE + 15,
148 DOM = B_POSIX_ERROR_BASE + 16,
149 RANGE = B_POSIX_ERROR_BASE + 17,
150 PROTOTYPE = B_POSIX_ERROR_BASE + 18,
151 PROTONOSUPPORT = B_POSIX_ERROR_BASE + 19,
152 PFNOSUPPORT = B_POSIX_ERROR_BASE + 20,
153 AFNOSUPPORT = B_POSIX_ERROR_BASE + 21,
154 ADDRINUSE = B_POSIX_ERROR_BASE + 22,
155 ADDRNOTAVAIL = B_POSIX_ERROR_BASE + 23,
156 NETDOWN = B_POSIX_ERROR_BASE + 24,
157 NETUNREACH = B_POSIX_ERROR_BASE + 25,
158 NETRESET = B_POSIX_ERROR_BASE + 26,
159 CONNABORTED = B_POSIX_ERROR_BASE + 27,
160 CONNRESET = B_POSIX_ERROR_BASE + 28,
161 ISCONN = B_POSIX_ERROR_BASE + 29,
162 NOTCONN = B_POSIX_ERROR_BASE + 30,
163 SHUTDOWN = B_POSIX_ERROR_BASE + 31,
164 CONNREFUSED = B_POSIX_ERROR_BASE + 32,
165 HOSTUNREACH = B_POSIX_ERROR_BASE + 33,
166 NOPROTOOPT = B_POSIX_ERROR_BASE + 34,
167 NOBUFS = B_POSIX_ERROR_BASE + 35,
168 INPROGRESS = B_POSIX_ERROR_BASE + 36,
169 ALREADY = B_POSIX_ERROR_BASE + 37,
170 ILSEQ = B_POSIX_ERROR_BASE + 38,
171 NOMSG = B_POSIX_ERROR_BASE + 39,
172 STALE = B_POSIX_ERROR_BASE + 40,
173 OVERFLOW = B_POSIX_ERROR_BASE + 41,
174 MSGSIZE = B_POSIX_ERROR_BASE + 42,
175 OPNOTSUPP = B_POSIX_ERROR_BASE + 43,
176 NOTSOCK = B_POSIX_ERROR_BASE + 44,
177 HOSTDOWN = B_POSIX_ERROR_BASE + 45,
178 BADMSG = B_POSIX_ERROR_BASE + 46,
179 CANCELED = B_POSIX_ERROR_BASE + 47,
180 DESTADDRREQ = B_POSIX_ERROR_BASE + 48,
181 DQUOT = B_POSIX_ERROR_BASE + 49,
182 IDRM = B_POSIX_ERROR_BASE + 50,
183 MULTIHOP = B_POSIX_ERROR_BASE + 51,
184 NODATA = B_POSIX_ERROR_BASE + 52,
185 NOLINK = B_POSIX_ERROR_BASE + 53,
186 NOSR = B_POSIX_ERROR_BASE + 54,
187 NOSTR = B_POSIX_ERROR_BASE + 55,
188 NOTSUP = B_POSIX_ERROR_BASE + 56,
189 PROTO = B_POSIX_ERROR_BASE + 57,
190 TIME = B_POSIX_ERROR_BASE + 58,
191 TXTBSY = B_POSIX_ERROR_BASE + 59,
192 NOATTR = B_POSIX_ERROR_BASE + 60,
193 NOTRECOVERABLE = B_POSIX_ERROR_BASE + 61,
194 OWNERDEAD = B_POSIX_ERROR_BASE + 62,
195
196 NOMEM = B_NO_MEMORY,
197
198 ACCES = B_PERMISSION_DENIED,
199 INTR = B_INTERRUPTED,
200 IO = B_IO_ERROR,
201 BUSY = B_BUSY,
202 FAULT = B_BAD_ADDRESS,
203 TIMEDOUT = B_TIMED_OUT,
204 /// Also used for WOULDBLOCK
205 AGAIN = B_WOULD_BLOCK,
206 BADF = B_FILE_ERROR,
207 EXIST = B_FILE_EXISTS,
208 INVAL = B_BAD_VALUE,
209 NAMETOOLONG = B_NAME_TOO_LONG,
210 NOENT = B_ENTRY_NOT_FOUND,
211 PERM = B_NOT_ALLOWED,
212 NOTDIR = B_NOT_A_DIRECTORY,
213 ISDIR = B_IS_A_DIRECTORY,
214 NOTEMPTY = B_DIRECTORY_NOT_EMPTY,
215 NOSPC = B_DEVICE_FULL,
216 ROFS = B_READ_ONLY_DEVICE,
217 MFILE = B_NO_MORE_FDS,
218 XDEV = B_CROSS_DEVICE_LINK,
219 LOOP = B_LINK_LIMIT,
220 NOEXEC = B_NOT_AN_EXECUTABLE,
221 PIPE = B_BUSTED_PIPE,
222
223 _,
224};
225
226pub const status_t = i32;
227
228pub const DirEnt = extern struct {
229 /// device
230 dev: dev_t,
231 /// parent device (only for queries)
232 pdev: dev_t,
233 /// inode number
234 ino: ino_t,
235 /// parent inode (only for queries)
236 pino: ino_t,
237 /// length of this record, not the name
238 reclen: u16,
239 /// name of the entry (null byte terminated)
240 name: [0]u8,
241 pub fn getName(dirent: *const DirEnt) [*:0]const u8 {
242 return @ptrCast(&dirent.name);
243 }
244};
245
246// https://github.com/haiku/haiku/blob/2aab5f5f14aeb3f34c3a3d9a9064cc3c0d914bea/headers/posix/netinet/in.h#L122
247pub const IP = struct {
248 pub const OPTIONS = 1;
249 pub const HDRINCL = 2;
250 pub const TOS = 3;
251 pub const TTL = 4;
252 pub const RECVOPTS = 5;
253 pub const RECVRETOPTS = 6;
254 pub const RECVDSTADDR = 7;
255 pub const RETOPTS = 8;
256 pub const MULTICAST_IF = 9;
257 pub const MULTICAST_TTL = 10;
258 pub const MULTICAST_LOOP = 11;
259 pub const ADD_MEMBERSHIP = 12;
260 pub const DROP_MEMBERSHIP = 13;
261 pub const BLOCK_SOURCE = 14;
262 pub const UNBLOCK_SOURCE = 15;
263 pub const ADD_SOURCE_MEMBERSHIP = 16;
264 pub const DROP_SOURCE_MEMBERSHIP = 17;
265 pub const DONTFRAG = 38;
266};
267
268// https://github.com/haiku/haiku/blob/2aab5f5f14aeb3f34c3a3d9a9064cc3c0d914bea/headers/posix/netinet/in.h#L150
269pub const IPV6 = struct {
270 pub const MULTICAST_IF = 24;
271 pub const MULTICAST_HOPS = 25;
272 pub const MULTICAST_LOOP = 26;
273 pub const UNICAST_HOPS = 27;
274 pub const JOIN_GROUP = 28;
275 pub const LEAVE_GROUP = 29;
276 pub const V6ONLY = 30;
277 pub const PKTINFO = 31;
278 pub const RECVPKTINFO = 32;
279 pub const HOPLIMIT = 33;
280 pub const RECVHOPLIMIT = 34;
281 pub const HOPOPTS = 35;
282 pub const DSTOPTS = 36;
283 pub const RTHDR = 37;
284};
285
286// https://github.com/haiku/haiku/blob/2aab5f5f14aeb3f34c3a3d9a9064cc3c0d914bea/headers/posix/netinet/ip.h#L36
287pub const IPTOS = struct {
288 pub const RELIABILITY = 0x04;
289 pub const THROUGHPUT = 0x08;
290 pub const LOWDELAY = 0x10;
291};
292
293// https://github.com/haiku/haiku/blob/2ffb9aef511dde272ab57d545a5d8631705a5ec8/headers/posix/netinet/tcp.h#L74
294pub const TCP = struct {
295 pub const NODELAY = 0x01;
296 pub const MAXSEG = 0x02;
297 pub const NOPUSH = 0x04;
298 pub const NOOPT = 0x08;
299};