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