master
  1const std = @import("../std.zig");
  2const assert = std.debug.assert;
  3const maxInt = std.math.maxInt;
  4const builtin = @import("builtin");
  5const caddr_t = std.c.caddr_t;
  6const iovec = std.posix.iovec;
  7const iovec_const = std.posix.iovec_const;
  8const passwd = std.c.passwd;
  9const timespec = std.c.timespec;
 10const uid_t = std.c.uid_t;
 11const pid_t = std.c.pid_t;
 12
 13comptime {
 14    assert(builtin.os.tag == .openbsd); // Prevent access of std.c symbols on wrong OS.
 15}
 16
 17pub extern "c" fn ptrace(request: c_int, pid: pid_t, addr: caddr_t, data: c_int) c_int;
 18
 19pub const pthread_spinlock_t = extern struct {
 20    inner: ?*anyopaque = null,
 21};
 22
 23pub extern "c" fn pledge(promises: ?[*:0]const u8, execpromises: ?[*:0]const u8) c_int;
 24pub extern "c" fn unveil(path: ?[*:0]const u8, permissions: ?[*:0]const u8) c_int;
 25pub extern "c" fn getthrid() pid_t;
 26
 27pub const FUTEX = struct {
 28    pub const WAIT = 1;
 29    pub const WAKE = 2;
 30    pub const REQUEUE = 3;
 31    pub const PRIVATE_FLAG = 128;
 32};
 33pub extern "c" fn futex(uaddr: ?*const volatile u32, op: c_int, val: c_int, timeout: ?*const timespec, uaddr2: ?*const volatile u32) c_int;
 34
 35pub const login_cap_t = extern struct {
 36    class: ?[*:0]const u8,
 37    cap: ?[*:0]const u8,
 38    style: ?[*:0]const u8,
 39};
 40
 41pub extern "c" fn login_getclass(class: ?[*:0]const u8) ?*login_cap_t;
 42pub extern "c" fn login_getstyle(lc: *login_cap_t, style: ?[*:0]const u8, atype: ?[*:0]const u8) ?[*:0]const u8;
 43pub extern "c" fn login_getcapbool(lc: *login_cap_t, cap: [*:0]const u8, def: c_int) c_int;
 44pub extern "c" fn login_getcapnum(lc: *login_cap_t, cap: [*:0]const u8, def: i64, err: i64) i64;
 45pub extern "c" fn login_getcapsize(lc: *login_cap_t, cap: [*:0]const u8, def: i64, err: i64) i64;
 46pub extern "c" fn login_getcapstr(lc: *login_cap_t, cap: [*:0]const u8, def: [*:0]const u8, err: [*:0]const u8) [*:0]const u8;
 47pub extern "c" fn login_getcaptime(lc: *login_cap_t, cap: [*:0]const u8, def: i64, err: i64) i64;
 48pub extern "c" fn login_close(lc: *login_cap_t) void;
 49pub extern "c" fn setclasscontext(class: [*:0]const u8, flags: c_uint) c_int;
 50pub extern "c" fn setusercontext(lc: *login_cap_t, pwd: *passwd, uid: uid_t, flags: c_uint) c_int;
 51
 52pub const auth_session_t = opaque {};
 53
 54pub extern "c" fn auth_userokay(name: [*:0]const u8, style: ?[*:0]const u8, arg_type: ?[*:0]const u8, password: ?[*:0]const u8) c_int;
 55pub extern "c" fn auth_approval(as: ?*auth_session_t, ?*login_cap_t, name: ?[*:0]const u8, type: ?[*:0]const u8) c_int;
 56pub extern "c" fn auth_userchallenge(name: [*:0]const u8, style: ?[*:0]const u8, arg_type: ?[*:0]const u8, chappengep: *?[*:0]const u8) ?*auth_session_t;
 57pub extern "c" fn auth_userresponse(as: *auth_session_t, response: [*:0]const u8, more: c_int) c_int;
 58pub extern "c" fn auth_usercheck(name: [*:0]const u8, style: ?[*:0]const u8, arg_type: ?[*:0]const u8, password: ?[*:0]const u8) ?*auth_session_t;
 59pub extern "c" fn auth_open() ?*auth_session_t;
 60pub extern "c" fn auth_close(as: *auth_session_t) c_int;
 61pub extern "c" fn auth_setdata(as: *auth_session_t, ptr: *anyopaque, len: usize) c_int;
 62pub extern "c" fn auth_setitem(as: *auth_session_t, item: auth_item_t, value: [*:0]const u8) c_int;
 63pub extern "c" fn auth_getitem(as: *auth_session_t, item: auth_item_t) ?[*:0]const u8;
 64pub extern "c" fn auth_setoption(as: *auth_session_t, n: [*:0]const u8, v: [*:0]const u8) c_int;
 65pub extern "c" fn auth_setstate(as: *auth_session_t, s: c_int) void;
 66pub extern "c" fn auth_getstate(as: *auth_session_t) c_int;
 67pub extern "c" fn auth_clean(as: *auth_session_t) void;
 68pub extern "c" fn auth_clrenv(as: *auth_session_t) void;
 69pub extern "c" fn auth_clroption(as: *auth_session_t, option: [*:0]const u8) void;
 70pub extern "c" fn auth_clroptions(as: *auth_session_t) void;
 71pub extern "c" fn auth_setenv(as: *auth_session_t) void;
 72pub extern "c" fn auth_getvalue(as: *auth_session_t, what: [*:0]const u8) ?[*:0]const u8;
 73pub extern "c" fn auth_verify(as: ?*auth_session_t, style: ?[*:0]const u8, name: ?[*:0]const u8, ...) ?*auth_session_t;
 74pub extern "c" fn auth_call(as: *auth_session_t, path: [*:0]const u8, ...) c_int;
 75pub extern "c" fn auth_challenge(as: *auth_session_t) [*:0]const u8;
 76pub extern "c" fn auth_check_expire(as: *auth_session_t) i64;
 77pub extern "c" fn auth_check_change(as: *auth_session_t) i64;
 78pub extern "c" fn auth_getpwd(as: *auth_session_t) ?*passwd;
 79pub extern "c" fn auth_setpwd(as: *auth_session_t, pwd: *passwd) c_int;
 80pub extern "c" fn auth_mkvalue(value: [*:0]const u8) ?[*:0]const u8;
 81pub extern "c" fn auth_cat(file: [*:0]const u8) c_int;
 82pub extern "c" fn auth_checknologin(lc: *login_cap_t) void;
 83// TODO: auth_set_va_list requires zig support for va_list type (#515)
 84
 85pub extern "c" fn getpwuid_shadow(uid: uid_t) ?*passwd;
 86pub extern "c" fn getpwnam_shadow(name: [*:0]const u8) ?*passwd;
 87pub extern "c" fn setpassent(stayopen: c_int) c_int;
 88pub extern "c" fn uid_from_user(name: [*:0]const u8, uid: *uid_t) c_int;
 89pub extern "c" fn user_from_uid(uid: uid_t, noname: c_int) ?[*:0]const u8;
 90pub extern "c" fn bcrypt_gensalt(log_rounds: u8) [*:0]const u8;
 91pub extern "c" fn bcrypt(pass: [*:0]const u8, salt: [*:0]const u8) ?[*:0]const u8;
 92pub extern "c" fn bcrypt_newhash(pass: [*:0]const u8, log_rounds: c_int, hash: [*]u8, hashlen: usize) c_int;
 93pub extern "c" fn bcrypt_checkpass(pass: [*:0]const u8, goodhash: [*:0]const u8) c_int;
 94pub extern "c" fn pw_dup(pw: *const passwd) ?*passwd;
 95
 96pub const auth_item_t = enum(c_int) {
 97    ALL = 0,
 98    CHALLENGE = 1,
 99    CLASS = 2,
100    NAME = 3,
101    SERVICE = 4,
102    STYLE = 5,
103    INTERACTIVE = 6,
104};
105
106pub const BI = struct {
107    pub const AUTH = "authorize"; // Accepted authentication
108    pub const REJECT = "reject"; // Rejected authentication
109    pub const CHALLENGE = "reject challenge"; // Reject with a challenge
110    pub const SILENT = "reject silent"; // Reject silently
111    pub const REMOVE = "remove"; // remove file on error
112    pub const ROOTOKAY = "authorize root"; // root authenticated
113    pub const SECURE = "authorize secure"; // okay on non-secure line
114    pub const SETENV = "setenv"; // set environment variable
115    pub const UNSETENV = "unsetenv"; // unset environment variable
116    pub const VALUE = "value"; // set local variable
117    pub const EXPIRED = "reject expired"; // account expired
118    pub const PWEXPIRED = "reject pwexpired"; // password expired
119    pub const FDPASS = "fd"; // child is passing an fd
120};
121
122pub const AUTH = struct {
123    pub const OKAY: c_int = 0x01; // user authenticated
124    pub const ROOTOKAY: c_int = 0x02; // authenticated as root
125    pub const SECURE: c_int = 0x04; // secure login
126    pub const SILENT: c_int = 0x08; // silent rejection
127    pub const CHALLENGE: c_int = 0x10; // a challenge was given
128    pub const EXPIRED: c_int = 0x20; // account expired
129    pub const PWEXPIRED: c_int = 0x40; // password expired
130    pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);
131};
132
133pub const TCFLUSH = enum(u32) {
134    none = 0,
135    I = 1,
136    O = 2,
137    IO = 3,
138};
139
140pub const TCIO = enum(u32) {
141    OOFF = 1,
142    OON = 2,
143    IOFF = 3,
144    ION = 4,
145};
146
147pub const E = enum(u16) {
148    /// No error occurred.
149    SUCCESS = 0,
150    PERM = 1, // Operation not permitted
151    NOENT = 2, // No such file or directory
152    SRCH = 3, // No such process
153    INTR = 4, // Interrupted system call
154    IO = 5, // Input/output error
155    NXIO = 6, // Device not configured
156    @"2BIG" = 7, // Argument list too long
157    NOEXEC = 8, // Exec format error
158    BADF = 9, // Bad file descriptor
159    CHILD = 10, // No child processes
160    DEADLK = 11, // Resource deadlock avoided
161    // 11 was AGAIN
162    NOMEM = 12, // Cannot allocate memory
163    ACCES = 13, // Permission denied
164    FAULT = 14, // Bad address
165    NOTBLK = 15, // Block device required
166    BUSY = 16, // Device busy
167    EXIST = 17, // File exists
168    XDEV = 18, // Cross-device link
169    NODEV = 19, // Operation not supported by device
170    NOTDIR = 20, // Not a directory
171    ISDIR = 21, // Is a directory
172    INVAL = 22, // Invalid argument
173    NFILE = 23, // Too many open files in system
174    MFILE = 24, // Too many open files
175    NOTTY = 25, // Inappropriate ioctl for device
176    TXTBSY = 26, // Text file busy
177    FBIG = 27, // File too large
178    NOSPC = 28, // No space left on device
179    SPIPE = 29, // Illegal seek
180    ROFS = 30, // Read-only file system
181    MLINK = 31, // Too many links
182    PIPE = 32, // Broken pipe
183
184    // math software
185    DOM = 33, // Numerical argument out of domain
186    RANGE = 34, // Result too large or too small
187
188    // non-blocking and interrupt i/o
189    // also: WOULDBLOCK: operation would block
190    AGAIN = 35, // Resource temporarily unavailable
191    INPROGRESS = 36, // Operation now in progress
192    ALREADY = 37, // Operation already in progress
193
194    // ipc/network software -- argument errors
195    NOTSOCK = 38, // Socket operation on non-socket
196    DESTADDRREQ = 39, // Destination address required
197    MSGSIZE = 40, // Message too long
198    PROTOTYPE = 41, // Protocol wrong type for socket
199    NOPROTOOPT = 42, // Protocol option not available
200    PROTONOSUPPORT = 43, // Protocol not supported
201    SOCKTNOSUPPORT = 44, // Socket type not supported
202    OPNOTSUPP = 45, // Operation not supported
203    PFNOSUPPORT = 46, // Protocol family not supported
204    AFNOSUPPORT = 47, // Address family not supported by protocol family
205    ADDRINUSE = 48, // Address already in use
206    ADDRNOTAVAIL = 49, // Can't assign requested address
207
208    // ipc/network software -- operational errors
209    NETDOWN = 50, // Network is down
210    NETUNREACH = 51, // Network is unreachable
211    NETRESET = 52, // Network dropped connection on reset
212    CONNABORTED = 53, // Software caused connection abort
213    CONNRESET = 54, // Connection reset by peer
214    NOBUFS = 55, // No buffer space available
215    ISCONN = 56, // Socket is already connected
216    NOTCONN = 57, // Socket is not connected
217    SHUTDOWN = 58, // Can't send after socket shutdown
218    TOOMANYREFS = 59, // Too many references: can't splice
219    TIMEDOUT = 60, // Operation timed out
220    CONNREFUSED = 61, // Connection refused
221
222    LOOP = 62, // Too many levels of symbolic links
223    NAMETOOLONG = 63, // File name too long
224
225    // should be rearranged
226    HOSTDOWN = 64, // Host is down
227    HOSTUNREACH = 65, // No route to host
228    NOTEMPTY = 66, // Directory not empty
229
230    // quotas & mush
231    PROCLIM = 67, // Too many processes
232    USERS = 68, // Too many users
233    DQUOT = 69, // Disc quota exceeded
234
235    // Network File System
236    STALE = 70, // Stale NFS file handle
237    REMOTE = 71, // Too many levels of remote in path
238    BADRPC = 72, // RPC struct is bad
239    RPCMISMATCH = 73, // RPC version wrong
240    PROGUNAVAIL = 74, // RPC prog. not avail
241    PROGMISMATCH = 75, // Program version wrong
242    PROCUNAVAIL = 76, // Bad procedure for program
243
244    NOLCK = 77, // No locks available
245    NOSYS = 78, // Function not implemented
246
247    FTYPE = 79, // Inappropriate file type or format
248    AUTH = 80, // Authentication error
249    NEEDAUTH = 81, // Need authenticator
250    IPSEC = 82, // IPsec processing failure
251    NOATTR = 83, // Attribute not found
252
253    // Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995
254    ILSEQ = 84, // Illegal byte sequence
255
256    NOMEDIUM = 85, // No medium found
257    MEDIUMTYPE = 86, // Wrong medium type
258    OVERFLOW = 87, // Value too large to be stored in data type
259    CANCELED = 88, // Operation canceled
260    IDRM = 89, // Identifier removed
261    NOMSG = 90, // No message of desired type
262    NOTSUP = 91, // Not supported
263    BADMSG = 92, // Bad or Corrupt message
264    NOTRECOVERABLE = 93, // State not recoverable
265    OWNERDEAD = 94, // Previous owner died
266    PROTO = 95, // Protocol error
267
268    _,
269};
270
271pub const MAX_PAGE_SHIFT = switch (builtin.cpu.arch) {
272    .x86 => 12,
273    .sparc64 => 13,
274};
275
276pub const HW = struct {
277    pub const MACHINE = 1;
278    pub const MODEL = 2;
279    pub const NCPU = 3;
280    pub const BYTEORDER = 4;
281    pub const PHYSMEM = 5;
282    pub const USERMEM = 6;
283    pub const PAGESIZE = 7;
284    pub const DISKNAMES = 8;
285    pub const DISKSTATS = 9;
286    pub const DISKCOUNT = 10;
287    pub const SENSORS = 11;
288    pub const CPUSPEED = 12;
289    pub const SETPERF = 13;
290    pub const VENDOR = 14;
291    pub const PRODUCT = 15;
292    pub const VERSION = 16;
293    pub const SERIALNO = 17;
294    pub const UUID = 18;
295    pub const PHYSMEM64 = 19;
296    pub const USERMEM64 = 20;
297    pub const NCPUFOUND = 21;
298    pub const ALLOWPOWERDOWN = 22;
299    pub const PERFPOLICY = 23;
300    pub const SMT = 24;
301    pub const NCPUONLINE = 25;
302    pub const POWER = 26;
303};
304
305pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {
306    .sparc64 => 1 << 13,
307    .mips64 => 1 << 14,
308    else => 1 << 12,
309};
310
311// https://github.com/openbsd/src/blob/718a31b40d39fc6064de6355eb144e74633133fc/sys/netinet/in.h#L283
312pub const IP = struct {
313    pub const OPTIONS = 1;
314    pub const HDRINCL = 2;
315    pub const TOS = 3;
316    pub const TTL = 4;
317    pub const RECVOPTS = 5;
318    pub const RECVRETOPTS = 6;
319    pub const RECVDSTADDR = 7;
320    pub const RETOPTS = 8;
321    pub const MULTICAST_IF = 9;
322    pub const MULTICAST_TTL = 10;
323    pub const MULTICAST_LOOP = 11;
324    pub const ADD_MEMBERSHIP = 12;
325    pub const DROP_MEMBERSHIP = 13;
326    pub const PORTRANGE = 19;
327    pub const AUTH_LEVEL = 20;
328    pub const ESP_TRANS_LEVEL = 21;
329    pub const ESP_NETWORK_LEVEL = 22;
330    pub const IPSEC_LOCAL_ID = 23;
331    pub const IPSEC_REMOTE_ID = 24;
332    pub const IPSEC_LOCAL_CRED = 25;
333    pub const IPSEC_REMOTE_CRED = 26;
334    pub const IPSEC_LOCAL_AUTH = 27;
335    pub const IPSEC_REMOTE_AUTH = 28;
336    pub const IPCOMP_LEVEL = 29;
337    pub const RECVIF = 30;
338    pub const RECVTTL = 31;
339    pub const MINTTL = 32;
340    pub const RECVDSTPORT = 33;
341    pub const PIPEX = 34;
342    pub const RECVRTABLE = 35;
343    pub const IPSECFLOWINFO = 36;
344    pub const IPDEFTTL = 37;
345    pub const SENDSRCADDR = RECVDSTADDR;
346    pub const RTABLE = 0x1021;
347    pub const DEFAULT_MULTICAST_TTL = 1;
348    pub const DEFAULT_MULTICAST_LOOP = 1;
349    pub const MIN_MEMBERSHIPS = 15;
350    pub const MAX_MEMBERSHIPS = 4095;
351    pub const PORTRANGE_DEFAULT = 0;
352    pub const PORTRANGE_HIGH = 1;
353    pub const PORTRANGE_LOW = 2;
354};
355
356// https://github.com/openbsd/src/blob/718a31b40d39fc6064de6355eb144e74633133fc/sys/netinet6/in6.h#L284
357pub const IPV6 = struct {
358    pub const UNICAST_HOPS = 4;
359    pub const MULTICAST_IF = 9;
360    pub const MULTICAST_HOPS = 10;
361    pub const MULTICAST_LOOP = 11;
362    pub const JOIN_GROUP = 12;
363    pub const LEAVE_GROUP = 13;
364    pub const PORTRANGE = 14;
365    pub const CHECKSUM = 26;
366    pub const V6ONLY = 27;
367    pub const RTHDRDSTOPTS = 35;
368    pub const RECVPKTINFO = 36;
369    pub const RECVHOPLIMIT = 37;
370    pub const RECVRTHDR = 38;
371    pub const RECVHOPOPTS = 39;
372    pub const RECVDSTOPTS = 40;
373    pub const USE_MIN_MTU = 42;
374    pub const RECVPATHMTU = 43;
375    pub const PATHMTU = 44;
376    pub const PKTINFO = 46;
377    pub const HOPLIMIT = 47;
378    pub const NEXTHOP = 48;
379    pub const HOPOPTS = 49;
380    pub const DSTOPTS = 50;
381    pub const RTHDR = 51;
382    pub const AUTH_LEVEL = 53;
383    pub const ESP_TRANS_LEVEL = 54;
384    pub const ESP_NETWORK_LEVEL = 55;
385    pub const RECVTCLASS = 57;
386    pub const AUTOFLOWLABEL = 59;
387    pub const IPCOMP_LEVEL = 60;
388    pub const TCLASS = 61;
389    pub const DONTFRAG = 62;
390    pub const PIPEX = 63;
391    pub const RECVDSTPORT = 64;
392    pub const MINHOPCOUNT = 65;
393    pub const RTABLE = 0x1021;
394    pub const RTHDR_LOOSE = 0;
395    pub const RTHDR_TYPE_0 = 0;
396    pub const DEFAULT_MULTICAST_HOPS = 1;
397    pub const DEFAULT_MULTICAST_LOOP = 1;
398    pub const PORTRANGE_DEFAULT = 0;
399    pub const PORTRANGE_HIGH = 1;
400    pub const PORTRANGE_LOW = 2;
401};
402
403// https://github.com/openbsd/src/blob/718a31b40d39fc6064de6355eb144e74633133fc/sys/netinet/ip.h#L73
404pub const IPTOS = struct {
405    pub const LOWDELAY = 0x10;
406    pub const THROUGHPUT = 0x08;
407    pub const RELIABILITY = 0x04;
408    pub const CE = 0x01;
409    pub const ECT = 0x02;
410    pub const PREC_NETCONTROL = 0xe0;
411    pub const PREC_INTERNETCONTROL = 0xc0;
412    pub const PREC_CRITIC_ECP = 0xa0;
413    pub const PREC_FLASHOVERRIDE = 0x80;
414    pub const PREC_FLASH = 0x60;
415    pub const PREC_IMMEDIATE = 0x40;
416    pub const PREC_PRIORITY = 0x20;
417    pub const PREC_ROUTINE = 0x00;
418    pub const DSCP_CS0 = 0x00;
419    pub const DSCP_LE = 0x04;
420    pub const DSCP_CS1 = 0x20;
421    pub const DSCP_AF11 = 0x28;
422    pub const DSCP_AF12 = 0x30;
423    pub const DSCP_AF13 = 0x38;
424    pub const DSCP_CS2 = 0x40;
425    pub const DSCP_AF21 = 0x48;
426    pub const DSCP_AF22 = 0x50;
427    pub const DSCP_AF23 = 0x58;
428    pub const DSCP_CS3 = 0x60;
429    pub const DSCP_AF31 = 0x68;
430    pub const DSCP_AF32 = 0x70;
431    pub const DSCP_AF33 = 0x78;
432    pub const DSCP_CS4 = 0x80;
433    pub const DSCP_AF41 = 0x88;
434    pub const DSCP_AF42 = 0x90;
435    pub const DSCP_AF43 = 0x98;
436    pub const DSCP_CS5 = 0xa0;
437    pub const DSCP_EF = 0xb8;
438    pub const DSCP_CS6 = 0xc0;
439    pub const DSCP_CS7 = 0xe0;
440    pub const ECN_NOTECT = 0x00;
441    pub const ECN_ECT1 = 0x01;
442    pub const ECN_ECT0 = 0x02;
443    pub const ECN_CE = 0x03;
444    pub const ECN_MASK = 0x03;
445};