Skip to content

Commit 03f7cff

Browse files
committed
Merge branch 'openbsd-minimal' of https://github.com/semarie/zig into semarie-openbsd-minimal
2 parents 9052e0b + 35a7247 commit 03f7cff

22 files changed

+1195
-45
lines changed

lib/std/c.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
249249
pub extern "c" fn malloc(usize) ?*c_void;
250250

251251
pub usingnamespace switch (builtin.os.tag) {
252-
.linux, .freebsd, .kfreebsd, .netbsd, .openbsd => struct {
252+
.linux, .freebsd, .kfreebsd, .netbsd => struct {
253253
pub extern "c" fn malloc_usable_size(?*const c_void) usize;
254254
},
255255
.macos, .ios, .watchos, .tvos => struct {

lib/std/c/openbsd.zig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@
33
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
44
// The MIT license requires this copyright notice to be included in all copies
55
// and substantial portions of the software.
6+
const std = @import("../std.zig");
7+
const builtin = std.builtin;
8+
9+
usingnamespace std.c;
10+
11+
extern "c" fn __errno() *c_int;
12+
pub const _errno = __errno;
13+
14+
pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int;
15+
pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
16+
17+
pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
18+
19+
pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
20+
pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
21+
622
pub const pthread_mutex_t = extern struct {
723
inner: ?*c_void = null,
824
};
925
pub const pthread_cond_t = extern struct {
1026
inner: ?*c_void = null,
1127
};
28+
pub const pthread_spinlock_t = extern struct {
29+
inner: ?*c_void = null,
30+
};
31+
32+
pub const pthread_attr_t = extern struct {
33+
inner: ?*c_void = null,
34+
};

lib/std/debug.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
654654
.freebsd,
655655
.netbsd,
656656
.dragonfly,
657+
.openbsd,
657658
.macos,
658659
.windows,
659660
=> return DebugInfo.init(allocator),
@@ -1640,7 +1641,7 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
16401641
};
16411642
}
16421643
},
1643-
.linux, .netbsd, .freebsd, .dragonfly => struct {
1644+
.linux, .netbsd, .freebsd, .dragonfly, .openbsd => struct {
16441645
base_address: usize,
16451646
dwarf: DW.DwarfInfo,
16461647
mapped_memory: []const u8,

lib/std/dynamic_library.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const max = std.math.max;
1919
pub const DynLib = switch (builtin.os.tag) {
2020
.linux => if (builtin.link_libc) DlDynlib else ElfDynLib,
2121
.windows => WindowsDynLib,
22-
.macos, .tvos, .watchos, .ios, .freebsd => DlDynlib,
22+
.macos, .tvos, .watchos, .ios, .freebsd, .openbsd => DlDynlib,
2323
else => void,
2424
};
2525

@@ -402,7 +402,7 @@ pub const DlDynlib = struct {
402402

403403
test "dynamic_library" {
404404
const libname = switch (builtin.os.tag) {
405-
.linux, .freebsd => "invalid_so.so",
405+
.linux, .freebsd, .openbsd => "invalid_so.so",
406406
.windows => "invalid_dll.dll",
407407
.macos, .tvos, .watchos, .ios => "invalid_dylib.dylib",
408408
else => return error.SkipZigTest,

lib/std/event/loop.zig

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub const Loop = struct {
6969
};
7070

7171
pub const EventFd = switch (builtin.os.tag) {
72-
.macos, .freebsd, .netbsd, .dragonfly => KEventFd,
72+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => KEventFd,
7373
.linux => struct {
7474
base: ResumeNode,
7575
epoll_op: u32,
@@ -88,7 +88,7 @@ pub const Loop = struct {
8888
};
8989

9090
pub const Basic = switch (builtin.os.tag) {
91-
.macos, .freebsd, .netbsd, .dragonfly => KEventBasic,
91+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => KEventBasic,
9292
.linux => struct {
9393
base: ResumeNode,
9494
},
@@ -266,7 +266,7 @@ pub const Loop = struct {
266266
self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun);
267267
}
268268
},
269-
.macos, .freebsd, .netbsd, .dragonfly => {
269+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
270270
self.os_data.kqfd = try os.kqueue();
271271
errdefer os.close(self.os_data.kqfd);
272272

@@ -391,7 +391,7 @@ pub const Loop = struct {
391391
while (self.available_eventfd_resume_nodes.pop()) |node| os.close(node.data.eventfd);
392392
os.close(self.os_data.epollfd);
393393
},
394-
.macos, .freebsd, .netbsd, .dragonfly => {
394+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
395395
os.close(self.os_data.kqfd);
396396
},
397397
.windows => {
@@ -485,7 +485,7 @@ pub const Loop = struct {
485485
.linux => {
486486
self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN);
487487
},
488-
.macos, .freebsd, .netbsd, .dragonfly => {
488+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
489489
self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT);
490490
},
491491
else => @compileError("Unsupported OS"),
@@ -497,7 +497,7 @@ pub const Loop = struct {
497497
.linux => {
498498
self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT);
499499
},
500-
.macos, .freebsd, .netbsd, .dragonfly => {
500+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
501501
self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT);
502502
},
503503
else => @compileError("Unsupported OS"),
@@ -509,7 +509,7 @@ pub const Loop = struct {
509509
.linux => {
510510
self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN);
511511
},
512-
.macos, .freebsd, .netbsd, .dragonfly => {
512+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
513513
self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT);
514514
self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT);
515515
},
@@ -578,7 +578,7 @@ pub const Loop = struct {
578578
const eventfd_node = &resume_stack_node.data;
579579
eventfd_node.base.handle = next_tick_node.data;
580580
switch (builtin.os.tag) {
581-
.macos, .freebsd, .netbsd, .dragonfly => {
581+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
582582
const kevent_array = @as(*const [1]os.Kevent, &eventfd_node.kevent);
583583
const empty_kevs = &[0]os.Kevent{};
584584
_ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch {
@@ -644,6 +644,7 @@ pub const Loop = struct {
644644
.freebsd,
645645
.netbsd,
646646
.dragonfly,
647+
.openbsd,
647648
=> self.fs_thread.wait(),
648649
else => {},
649650
}
@@ -736,7 +737,7 @@ pub const Loop = struct {
736737
}
737738
return;
738739
},
739-
.macos, .freebsd, .netbsd, .dragonfly => {
740+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
740741
const final_kevent = @as(*const [1]os.Kevent, &self.os_data.final_kevent);
741742
const empty_kevs = &[0]os.Kevent{};
742743
// cannot fail because we already added it and this just enables it
@@ -1351,7 +1352,7 @@ pub const Loop = struct {
13511352
}
13521353
}
13531354
},
1354-
.macos, .freebsd, .netbsd, .dragonfly => {
1355+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
13551356
var eventlist: [1]os.Kevent = undefined;
13561357
const empty_kevs = &[0]os.Kevent{};
13571358
const count = os.kevent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable;
@@ -1477,7 +1478,7 @@ pub const Loop = struct {
14771478

14781479
const OsData = switch (builtin.os.tag) {
14791480
.linux => LinuxOsData,
1480-
.macos, .freebsd, .netbsd, .dragonfly => KEventData,
1481+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => KEventData,
14811482
.windows => struct {
14821483
io_port: windows.HANDLE,
14831484
extra_thread_count: usize,

lib/std/fs.zig

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub const Watch = @import("fs/watch.zig").Watch;
3939
/// fit into a UTF-8 encoded array of this length.
4040
/// The byte count includes room for a null sentinel byte.
4141
pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
42-
.linux, .macos, .ios, .freebsd, .netbsd, .dragonfly => os.PATH_MAX,
42+
.linux, .macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd => os.PATH_MAX,
4343
// Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
4444
// If it would require 4 UTF-8 bytes, then there would be a surrogate
4545
// pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
@@ -303,7 +303,7 @@ pub const Dir = struct {
303303
const IteratorError = error{AccessDenied} || os.UnexpectedError;
304304

305305
pub const Iterator = switch (builtin.os.tag) {
306-
.macos, .ios, .freebsd, .netbsd, .dragonfly => struct {
306+
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd => struct {
307307
dir: Dir,
308308
seek: i64,
309309
buf: [8192]u8, // TODO align(@alignOf(os.dirent)),
@@ -319,7 +319,7 @@ pub const Dir = struct {
319319
pub fn next(self: *Self) Error!?Entry {
320320
switch (builtin.os.tag) {
321321
.macos, .ios => return self.nextDarwin(),
322-
.freebsd, .netbsd, .dragonfly => return self.nextBsd(),
322+
.freebsd, .netbsd, .dragonfly, .openbsd => return self.nextBsd(),
323323
else => @compileError("unimplemented"),
324324
}
325325
}
@@ -615,7 +615,7 @@ pub const Dir = struct {
615615

616616
pub fn iterate(self: Dir) Iterator {
617617
switch (builtin.os.tag) {
618-
.macos, .ios, .freebsd, .netbsd, .dragonfly => return Iterator{
618+
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd => return Iterator{
619619
.dir = self,
620620
.seek = 0,
621621
.index = 0,
@@ -1302,7 +1302,7 @@ pub const Dir = struct {
13021302
error.AccessDenied => |e| switch (builtin.os.tag) {
13031303
// non-Linux POSIX systems return EPERM when trying to delete a directory, so
13041304
// we need to handle that case specifically and translate the error
1305-
.macos, .ios, .freebsd, .netbsd, .dragonfly => {
1305+
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd => {
13061306
// Don't follow symlinks to match unlinkat (which acts on symlinks rather than follows them)
13071307
const fstat = os.fstatatZ(self.fd, sub_path_c, os.AT_SYMLINK_NOFOLLOW) catch return e;
13081308
const is_dir = fstat.mode & os.S_IFMT == os.S_IFDIR;
@@ -2232,6 +2232,44 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
22322232
// TODO could this slice from 0 to out_len instead?
22332233
return mem.spanZ(@ptrCast([*:0]u8, out_buffer));
22342234
},
2235+
.openbsd => {
2236+
// OpenBSD doesn't support getting the path of a running process, so try to guess it
2237+
if (os.argv.len >= 1) {
2238+
const argv0 = mem.span(os.argv[0]);
2239+
if (mem.indexOf(u8, argv0, "/") != null) {
2240+
// argv[0] is a path (relative or absolute): use realpath(3) directly
2241+
var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2242+
const real_path = try os.realpathZ(os.argv[0], &real_path_buf);
2243+
if (real_path.len > out_buffer.len)
2244+
return error.NameTooLong;
2245+
mem.copy(u8, out_buffer, real_path);
2246+
return out_buffer[0..real_path.len];
2247+
} else if (argv0.len != 0) {
2248+
// argv[0] is not empty (and not a path): search it inside PATH
2249+
const PATH = std.os.getenv("PATH") orelse "";
2250+
var path_it = mem.tokenize(PATH, &[_]u8{path.delimiter});
2251+
while (path_it.next()) |a_path| {
2252+
var resolved_path_buf: [MAX_PATH_BYTES-1:0]u8 = undefined;
2253+
const resolved_path = std.fmt.bufPrint(&resolved_path_buf, "{}/{}\x00", .{
2254+
a_path,
2255+
os.argv[0],
2256+
}) catch "";
2257+
2258+
var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2259+
if (os.realpathZ(&resolved_path_buf, &real_path_buf) catch null) |real_path| {
2260+
// found a file, and hope it is the right file
2261+
if (real_path.len > out_buffer.len)
2262+
return error.NameTooLong;
2263+
mem.copy(u8, out_buffer, real_path);
2264+
return out_buffer[0..real_path.len];
2265+
}
2266+
}
2267+
}
2268+
}
2269+
2270+
// sorry, we don't find it
2271+
return error.FileNotFound;
2272+
},
22352273
.windows => {
22362274
const utf16le_slice = selfExePathW();
22372275
// Trust that Windows gives us valid UTF-16LE.

lib/std/fs/get_app_data_dir.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
4949
};
5050
return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname });
5151
},
52-
.linux, .freebsd, .netbsd, .dragonfly => {
52+
.linux, .freebsd, .netbsd, .dragonfly, .openbsd => {
5353
const home_dir = os.getenv("HOME") orelse {
5454
// TODO look in /etc/passwd
5555
return error.AppDataDirUnavailable;

lib/std/fs/watch.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn Watch(comptime V: type) type {
4949

5050
const OsData = switch (builtin.os.tag) {
5151
// TODO https://github.com/ziglang/zig/issues/3778
52-
.macos, .freebsd, .netbsd, .dragonfly => KqOsData,
52+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => KqOsData,
5353
.linux => LinuxOsData,
5454
.windows => WindowsOsData,
5555

@@ -160,7 +160,7 @@ pub fn Watch(comptime V: type) type {
160160
return self;
161161
},
162162

163-
.macos, .freebsd, .netbsd, .dragonfly => {
163+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
164164
self.* = Self{
165165
.allocator = allocator,
166166
.channel = channel,
@@ -178,7 +178,7 @@ pub fn Watch(comptime V: type) type {
178178
/// All addFile calls and removeFile calls must have completed.
179179
pub fn deinit(self: *Self) void {
180180
switch (builtin.os.tag) {
181-
.macos, .freebsd, .netbsd, .dragonfly => {
181+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
182182
// TODO we need to cancel the frames before destroying the lock
183183
self.os_data.table_lock.deinit();
184184
var it = self.os_data.file_table.iterator();
@@ -229,7 +229,7 @@ pub fn Watch(comptime V: type) type {
229229

230230
pub fn addFile(self: *Self, file_path: []const u8, value: V) !?V {
231231
switch (builtin.os.tag) {
232-
.macos, .freebsd, .netbsd, .dragonfly => return addFileKEvent(self, file_path, value),
232+
.macos, .freebsd, .netbsd, .dragonfly, .openbsd => return addFileKEvent(self, file_path, value),
233233
.linux => return addFileLinux(self, file_path, value),
234234
.windows => return addFileWindows(self, file_path, value),
235235
else => @compileError("Unsupported OS"),

lib/std/os.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub const darwin = @import("os/darwin.zig");
3333
pub const dragonfly = @import("os/dragonfly.zig");
3434
pub const freebsd = @import("os/freebsd.zig");
3535
pub const netbsd = @import("os/netbsd.zig");
36+
pub const openbsd = @import("os/openbsd.zig");
3637
pub const linux = @import("os/linux.zig");
3738
pub const uefi = @import("os/uefi.zig");
3839
pub const wasi = @import("os/wasi.zig");
@@ -47,6 +48,7 @@ test "" {
4748
_ = freebsd;
4849
_ = linux;
4950
_ = netbsd;
51+
_ = openbsd;
5052
_ = uefi;
5153
_ = wasi;
5254
_ = windows;
@@ -66,6 +68,7 @@ else switch (builtin.os.tag) {
6668
.freebsd => freebsd,
6769
.linux => linux,
6870
.netbsd => netbsd,
71+
.openbsd => openbsd,
6972
.dragonfly => dragonfly,
7073
.wasi => wasi,
7174
.windows => windows,
@@ -161,8 +164,8 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
161164
}
162165
return;
163166
}
164-
if (builtin.os.tag == .netbsd) {
165-
netbsd.arc4random_buf(buffer.ptr, buffer.len);
167+
if (builtin.os.tag == .netbsd or builtin.os.tag == .openbsd) {
168+
system.arc4random_buf(buffer.ptr, buffer.len);
166169
return;
167170
}
168171
if (builtin.os.tag == .wasi) {

lib/std/os/bits.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub usingnamespace switch (std.Target.current.os.tag) {
1717
.freebsd => @import("bits/freebsd.zig"),
1818
.linux => @import("bits/linux.zig"),
1919
.netbsd => @import("bits/netbsd.zig"),
20+
.openbsd => @import("bits/openbsd.zig"),
2021
.wasi => @import("bits/wasi.zig"),
2122
.windows => @import("bits/windows.zig"),
2223
else => struct {},

0 commit comments

Comments
 (0)