Skip to content

Commit f660675

Browse files
committed
std: Add support for SerenityOS in various places
Not nearly the entire downstream patchset but these are completely uncontroversial and known to work.
1 parent 79a0de2 commit f660675

File tree

7 files changed

+56
-16
lines changed

7 files changed

+56
-16
lines changed

lib/std/Thread.zig

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ pub const max_name_len = switch (native_os) {
122122
.openbsd => 23,
123123
.dragonfly => 1023,
124124
.solaris, .illumos => 31,
125+
// https://github.com/SerenityOS/serenity/blob/6b4c300353da49d3508b5442cf61da70bd04d757/Kernel/Tasks/Thread.h#L102
126+
.serenity => 63,
125127
else => 0,
126128
};
127129

@@ -201,6 +203,15 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
201203
else => |e| return posix.unexpectedErrno(e),
202204
}
203205
},
206+
.serenity => if (use_pthreads) {
207+
const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr);
208+
switch (@as(posix.E, @enumFromInt(err))) {
209+
.SUCCESS => return,
210+
.NAMETOOLONG => unreachable,
211+
.SRCH => unreachable,
212+
else => |e| return posix.unexpectedErrno(e),
213+
}
214+
},
204215
.netbsd, .solaris, .illumos => if (use_pthreads) {
205216
const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr, null);
206217
switch (@as(posix.E, @enumFromInt(err))) {
@@ -302,6 +313,16 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
302313
else => |e| return posix.unexpectedErrno(e),
303314
}
304315
},
316+
.serenity => if (use_pthreads) {
317+
const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
318+
switch (@as(posix.E, @enumFromInt(err))) {
319+
.SUCCESS => return,
320+
.NAMETOOLONG => unreachable,
321+
.SRCH => unreachable,
322+
.FAULT => unreachable,
323+
else => |e| return posix.unexpectedErrno(e),
324+
}
325+
},
305326
.netbsd, .solaris, .illumos => if (use_pthreads) {
306327
const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
307328
switch (@as(posix.E, @enumFromInt(err))) {
@@ -342,6 +363,7 @@ pub const Id = switch (native_os) {
342363
.openbsd,
343364
.haiku,
344365
.wasi,
366+
.serenity,
345367
=> u32,
346368
.macos, .ios, .watchos, .tvos, .visionos => u64,
347369
.windows => windows.DWORD,
@@ -692,6 +714,9 @@ const PosixThreadImpl = struct {
692714
.haiku => {
693715
return @as(u32, @bitCast(c.find_thread(null)));
694716
},
717+
.serenity => {
718+
return @as(u32, @bitCast(c.pthread_self()));
719+
},
695720
else => {
696721
return @intFromPtr(c.pthread_self());
697722
},
@@ -713,11 +738,11 @@ const PosixThreadImpl = struct {
713738
};
714739
return @as(usize, @intCast(count));
715740
},
716-
.solaris, .illumos => {
741+
.solaris, .illumos, .serenity => {
717742
// The "proper" way to get the cpu count would be to query
718743
// /dev/kstat via ioctls, and traverse a linked list for each
719-
// cpu.
720-
const rc = c.sysconf(std.c._SC.NPROCESSORS_ONLN);
744+
// cpu. (solaris, illumos)
745+
const rc = c.sysconf(@intFromEnum(std.c._SC.NPROCESSORS_ONLN));
721746
return switch (posix.errno(rc)) {
722747
.SUCCESS => @as(usize, @intCast(rc)),
723748
else => |err| posix.unexpectedErrno(err),

lib/std/crypto/Certificate/Bundle.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn deinit(cb: *Bundle, gpa: Allocator) void {
5050
cb.* = undefined;
5151
}
5252

53-
pub const RescanError = RescanLinuxError || RescanMacError || RescanBSDError || RescanWindowsError;
53+
pub const RescanError = RescanLinuxError || RescanMacError || RescanWithPathError || RescanWindowsError;
5454

5555
/// Clears the set of certificates and then scans the host operating system
5656
/// file system standard locations for certificates.
@@ -60,10 +60,12 @@ pub fn rescan(cb: *Bundle, gpa: Allocator) RescanError!void {
6060
switch (builtin.os.tag) {
6161
.linux => return rescanLinux(cb, gpa),
6262
.macos => return rescanMac(cb, gpa),
63-
.freebsd, .openbsd => return rescanBSD(cb, gpa, "/etc/ssl/cert.pem"),
64-
.netbsd => return rescanBSD(cb, gpa, "/etc/openssl/certs/ca-certificates.crt"),
65-
.dragonfly => return rescanBSD(cb, gpa, "/usr/local/etc/ssl/cert.pem"),
66-
.solaris, .illumos => return rescanBSD(cb, gpa, "/etc/ssl/cacert.pem"),
63+
.freebsd, .openbsd => return rescanWithPath(cb, gpa, "/etc/ssl/cert.pem"),
64+
.netbsd => return rescanWithPath(cb, gpa, "/etc/openssl/certs/ca-certificates.crt"),
65+
.dragonfly => return rescanWithPath(cb, gpa, "/usr/local/etc/ssl/cert.pem"),
66+
.solaris, .illumos => return rescanWithPath(cb, gpa, "/etc/ssl/cacert.pem"),
67+
// https://github.com/SerenityOS/serenity/blob/222acc9d389bc6b490d4c39539761b043a4bfcb0/Ports/ca-certificates/package.sh#L19
68+
.serenity => return rescanWithPath(cb, gpa, "/etc/ssl/certs/ca-certificates.crt"),
6769
.windows => return rescanWindows(cb, gpa),
6870
else => {},
6971
}
@@ -116,9 +118,9 @@ fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void {
116118
cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);
117119
}
118120

119-
const RescanBSDError = AddCertsFromFilePathError;
121+
const RescanWithPathError = AddCertsFromFilePathError;
120122

121-
fn rescanBSD(cb: *Bundle, gpa: Allocator, cert_file_path: []const u8) RescanBSDError!void {
123+
fn rescanWithPath(cb: *Bundle, gpa: Allocator, cert_file_path: []const u8) RescanWithPathError!void {
122124
cb.bytes.clearRetainingCapacity();
123125
cb.map.clearRetainingCapacity();
124126
try addCertsFromFilePathAbsolute(cb, gpa, cert_file_path);

lib/std/fs.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub const MAX_PATH_BYTES = @compileError("deprecated; renamed to max_path_bytes"
5252
/// * On other platforms, `[]u8` file paths are opaque sequences of bytes with
5353
/// no particular encoding.
5454
pub const max_path_bytes = switch (native_os) {
55-
.linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .haiku, .solaris, .illumos, .plan9, .emscripten, .wasi => posix.PATH_MAX,
55+
.linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .haiku, .solaris, .illumos, .plan9, .emscripten, .wasi, .serenity => posix.PATH_MAX,
5656
// Each WTF-16LE code unit may be expanded to 3 WTF-8 bytes.
5757
// If it would require 4 WTF-8 bytes, then there would be a surrogate
5858
// pair in the WTF-16LE, and we (over)account 3 bytes for it that way.
@@ -73,7 +73,7 @@ pub const max_path_bytes = switch (native_os) {
7373
/// On WASI, file name components are encoded as valid UTF-8.
7474
/// On other platforms, `[]u8` components are an opaque sequence of bytes with no particular encoding.
7575
pub const max_name_bytes = switch (native_os) {
76-
.linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .illumos => posix.NAME_MAX,
76+
.linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .illumos, .serenity => posix.NAME_MAX,
7777
// Haiku's NAME_MAX includes the null terminator, so subtract one.
7878
.haiku => posix.NAME_MAX - 1,
7979
// Each WTF-16LE character may be expanded to 3 WTF-8 bytes.
@@ -466,7 +466,7 @@ pub fn symLinkAbsoluteZ(
466466
pub const OpenSelfExeError = posix.OpenError || SelfExePathError || posix.FlockError;
467467

468468
pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
469-
if (native_os == .linux) {
469+
if (native_os == .linux or native_os == .serenity) {
470470
return openFileAbsoluteZ("/proc/self/exe", flags);
471471
}
472472
if (native_os == .windows) {
@@ -572,7 +572,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
572572
return result;
573573
}
574574
switch (native_os) {
575-
.linux => return posix.readlinkZ("/proc/self/exe", out_buffer) catch |err| switch (err) {
575+
.linux, .serenity => return posix.readlinkZ("/proc/self/exe", out_buffer) catch |err| switch (err) {
576576
error.InvalidUtf8 => unreachable, // WASI-only
577577
error.InvalidWtf8 => unreachable, // Windows-only
578578
error.UnsupportedReparsePointType => unreachable, // Windows-only

lib/std/fs/get_app_data_dir.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
3030
};
3131
return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname });
3232
},
33-
.linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => {
33+
.linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos, .serenity => {
3434
if (posix.getenv("XDG_DATA_HOME")) |xdg| {
3535
if (xdg.len > 0) {
3636
return fs.path.join(allocator, &[_][]const u8{ xdg, appname });

lib/std/os.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn isGetFdPathSupportedOnTarget(os: std.Target.Os) bool {
8282
.solaris,
8383
.illumos,
8484
.freebsd,
85+
.serenity,
8586
=> true,
8687

8788
.dragonfly => os.version_range.semver.max.order(.{ .major = 6, .minor = 0, .patch = 0 }) != .lt,
@@ -127,7 +128,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
127128
const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse max_path_bytes;
128129
return out_buffer[0..len];
129130
},
130-
.linux => {
131+
.linux, .serenity => {
131132
var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
132133
const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}) catch unreachable;
133134

lib/std/process.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ pub fn getUserInfo(name: []const u8) !UserInfo {
15391539
.haiku,
15401540
.solaris,
15411541
.illumos,
1542+
.serenity,
15421543
=> posixGetUserInfo(name),
15431544
else => @compileError("Unsupported OS"),
15441545
};

lib/std/zig/target.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ pub fn isLibCLibName(target: std.Target, name: []const u8) bool {
318318
return true;
319319
}
320320

321+
if (target.os.tag == .serenity) {
322+
if (eqlIgnoreCase(ignore_case, name, "dl"))
323+
return true;
324+
if (eqlIgnoreCase(ignore_case, name, "m"))
325+
return true;
326+
if (eqlIgnoreCase(ignore_case, name, "pthread"))
327+
return true;
328+
if (eqlIgnoreCase(ignore_case, name, "ssp"))
329+
return true;
330+
}
331+
321332
return false;
322333
}
323334

0 commit comments

Comments
 (0)