Commit f660675467

Linus Groh <mail@linusgroh.de>
2025-03-10 23:48:21
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
Changed files (7)
lib/std/crypto/Certificate/Bundle.zig
@@ -50,7 +50,7 @@ pub fn deinit(cb: *Bundle, gpa: Allocator) void {
     cb.* = undefined;
 }
 
-pub const RescanError = RescanLinuxError || RescanMacError || RescanBSDError || RescanWindowsError;
+pub const RescanError = RescanLinuxError || RescanMacError || RescanWithPathError || RescanWindowsError;
 
 /// Clears the set of certificates and then scans the host operating system
 /// file system standard locations for certificates.
@@ -60,10 +60,12 @@ pub fn rescan(cb: *Bundle, gpa: Allocator) RescanError!void {
     switch (builtin.os.tag) {
         .linux => return rescanLinux(cb, gpa),
         .macos => return rescanMac(cb, gpa),
-        .freebsd, .openbsd => return rescanBSD(cb, gpa, "/etc/ssl/cert.pem"),
-        .netbsd => return rescanBSD(cb, gpa, "/etc/openssl/certs/ca-certificates.crt"),
-        .dragonfly => return rescanBSD(cb, gpa, "/usr/local/etc/ssl/cert.pem"),
-        .solaris, .illumos => return rescanBSD(cb, gpa, "/etc/ssl/cacert.pem"),
+        .freebsd, .openbsd => return rescanWithPath(cb, gpa, "/etc/ssl/cert.pem"),
+        .netbsd => return rescanWithPath(cb, gpa, "/etc/openssl/certs/ca-certificates.crt"),
+        .dragonfly => return rescanWithPath(cb, gpa, "/usr/local/etc/ssl/cert.pem"),
+        .solaris, .illumos => return rescanWithPath(cb, gpa, "/etc/ssl/cacert.pem"),
+        // https://github.com/SerenityOS/serenity/blob/222acc9d389bc6b490d4c39539761b043a4bfcb0/Ports/ca-certificates/package.sh#L19
+        .serenity => return rescanWithPath(cb, gpa, "/etc/ssl/certs/ca-certificates.crt"),
         .windows => return rescanWindows(cb, gpa),
         else => {},
     }
@@ -116,9 +118,9 @@ fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void {
     cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);
 }
 
-const RescanBSDError = AddCertsFromFilePathError;
+const RescanWithPathError = AddCertsFromFilePathError;
 
-fn rescanBSD(cb: *Bundle, gpa: Allocator, cert_file_path: []const u8) RescanBSDError!void {
+fn rescanWithPath(cb: *Bundle, gpa: Allocator, cert_file_path: []const u8) RescanWithPathError!void {
     cb.bytes.clearRetainingCapacity();
     cb.map.clearRetainingCapacity();
     try addCertsFromFilePathAbsolute(cb, gpa, cert_file_path);
lib/std/fs/get_app_data_dir.zig
@@ -30,7 +30,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
             };
             return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname });
         },
-        .linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => {
+        .linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos, .serenity => {
             if (posix.getenv("XDG_DATA_HOME")) |xdg| {
                 if (xdg.len > 0) {
                     return fs.path.join(allocator, &[_][]const u8{ xdg, appname });
lib/std/zig/target.zig
@@ -318,6 +318,17 @@ pub fn isLibCLibName(target: std.Target, name: []const u8) bool {
             return true;
     }
 
+    if (target.os.tag == .serenity) {
+        if (eqlIgnoreCase(ignore_case, name, "dl"))
+            return true;
+        if (eqlIgnoreCase(ignore_case, name, "m"))
+            return true;
+        if (eqlIgnoreCase(ignore_case, name, "pthread"))
+            return true;
+        if (eqlIgnoreCase(ignore_case, name, "ssp"))
+            return true;
+    }
+
     return false;
 }
 
lib/std/fs.zig
@@ -52,7 +52,7 @@ pub const MAX_PATH_BYTES = @compileError("deprecated; renamed to max_path_bytes"
 /// * On other platforms, `[]u8` file paths are opaque sequences of bytes with
 ///   no particular encoding.
 pub const max_path_bytes = switch (native_os) {
-    .linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .haiku, .solaris, .illumos, .plan9, .emscripten, .wasi => posix.PATH_MAX,
+    .linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .haiku, .solaris, .illumos, .plan9, .emscripten, .wasi, .serenity => posix.PATH_MAX,
     // Each WTF-16LE code unit may be expanded to 3 WTF-8 bytes.
     // If it would require 4 WTF-8 bytes, then there would be a surrogate
     // 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) {
 /// On WASI, file name components are encoded as valid UTF-8.
 /// On other platforms, `[]u8` components are an opaque sequence of bytes with no particular encoding.
 pub const max_name_bytes = switch (native_os) {
-    .linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .illumos => posix.NAME_MAX,
+    .linux, .macos, .ios, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .illumos, .serenity => posix.NAME_MAX,
     // Haiku's NAME_MAX includes the null terminator, so subtract one.
     .haiku => posix.NAME_MAX - 1,
     // Each WTF-16LE character may be expanded to 3 WTF-8 bytes.
@@ -466,7 +466,7 @@ pub fn symLinkAbsoluteZ(
 pub const OpenSelfExeError = posix.OpenError || SelfExePathError || posix.FlockError;
 
 pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
-    if (native_os == .linux) {
+    if (native_os == .linux or native_os == .serenity) {
         return openFileAbsoluteZ("/proc/self/exe", flags);
     }
     if (native_os == .windows) {
@@ -572,7 +572,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
         return result;
     }
     switch (native_os) {
-        .linux => return posix.readlinkZ("/proc/self/exe", out_buffer) catch |err| switch (err) {
+        .linux, .serenity => return posix.readlinkZ("/proc/self/exe", out_buffer) catch |err| switch (err) {
             error.InvalidUtf8 => unreachable, // WASI-only
             error.InvalidWtf8 => unreachable, // Windows-only
             error.UnsupportedReparsePointType => unreachable, // Windows-only
lib/std/os.zig
@@ -82,6 +82,7 @@ pub fn isGetFdPathSupportedOnTarget(os: std.Target.Os) bool {
         .solaris,
         .illumos,
         .freebsd,
+        .serenity,
         => true,
 
         .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.
             const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse max_path_bytes;
             return out_buffer[0..len];
         },
-        .linux => {
+        .linux, .serenity => {
             var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
             const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}) catch unreachable;
 
lib/std/process.zig
@@ -1539,6 +1539,7 @@ pub fn getUserInfo(name: []const u8) !UserInfo {
         .haiku,
         .solaris,
         .illumos,
+        .serenity,
         => posixGetUserInfo(name),
         else => @compileError("Unsupported OS"),
     };
lib/std/Thread.zig
@@ -122,6 +122,8 @@ pub const max_name_len = switch (native_os) {
     .openbsd => 23,
     .dragonfly => 1023,
     .solaris, .illumos => 31,
+    // https://github.com/SerenityOS/serenity/blob/6b4c300353da49d3508b5442cf61da70bd04d757/Kernel/Tasks/Thread.h#L102
+    .serenity => 63,
     else => 0,
 };
 
@@ -201,6 +203,15 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
                 else => |e| return posix.unexpectedErrno(e),
             }
         },
+        .serenity => if (use_pthreads) {
+            const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr);
+            switch (@as(posix.E, @enumFromInt(err))) {
+                .SUCCESS => return,
+                .NAMETOOLONG => unreachable,
+                .SRCH => unreachable,
+                else => |e| return posix.unexpectedErrno(e),
+            }
+        },
         .netbsd, .solaris, .illumos => if (use_pthreads) {
             const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr, null);
             switch (@as(posix.E, @enumFromInt(err))) {
@@ -302,6 +313,16 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
                 else => |e| return posix.unexpectedErrno(e),
             }
         },
+        .serenity => if (use_pthreads) {
+            const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
+            switch (@as(posix.E, @enumFromInt(err))) {
+                .SUCCESS => return,
+                .NAMETOOLONG => unreachable,
+                .SRCH => unreachable,
+                .FAULT => unreachable,
+                else => |e| return posix.unexpectedErrno(e),
+            }
+        },
         .netbsd, .solaris, .illumos => if (use_pthreads) {
             const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
             switch (@as(posix.E, @enumFromInt(err))) {
@@ -342,6 +363,7 @@ pub const Id = switch (native_os) {
     .openbsd,
     .haiku,
     .wasi,
+    .serenity,
     => u32,
     .macos, .ios, .watchos, .tvos, .visionos => u64,
     .windows => windows.DWORD,
@@ -692,6 +714,9 @@ const PosixThreadImpl = struct {
             .haiku => {
                 return @as(u32, @bitCast(c.find_thread(null)));
             },
+            .serenity => {
+                return @as(u32, @bitCast(c.pthread_self()));
+            },
             else => {
                 return @intFromPtr(c.pthread_self());
             },
@@ -713,11 +738,11 @@ const PosixThreadImpl = struct {
                 };
                 return @as(usize, @intCast(count));
             },
-            .solaris, .illumos => {
+            .solaris, .illumos, .serenity => {
                 // The "proper" way to get the cpu count would be to query
                 // /dev/kstat via ioctls, and traverse a linked list for each
-                // cpu.
-                const rc = c.sysconf(std.c._SC.NPROCESSORS_ONLN);
+                // cpu. (solaris, illumos)
+                const rc = c.sysconf(@intFromEnum(std.c._SC.NPROCESSORS_ONLN));
                 return switch (posix.errno(rc)) {
                     .SUCCESS => @as(usize, @intCast(rc)),
                     else => |err| posix.unexpectedErrno(err),