From 44422886561f5c0029f3d61898609220bd35deea Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 25 Sep 2024 09:54:41 -0700 Subject: [PATCH] std: fix inappropriate use of unreachable in fanotify_init --- lib/std/Build/Watch.zig | 7 +++++-- lib/std/posix.zig | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/std/Build/Watch.zig b/lib/std/Build/Watch.zig index c1a9e80b8e..c8f6c0d64f 100644 --- a/lib/std/Build/Watch.zig +++ b/lib/std/Build/Watch.zig @@ -516,7 +516,7 @@ const Os = switch (builtin.os.tag) { pub fn init() !Watch { switch (builtin.os.tag) { .linux => { - const fan_fd = try std.posix.fanotify_init(.{ + const fan_fd = std.posix.fanotify_init(.{ .CLASS = .NOTIF, .CLOEXEC = true, .NONBLOCK = true, @@ -524,7 +524,10 @@ pub fn init() !Watch { .REPORT_DIR_FID = true, .REPORT_FID = true, .REPORT_TARGET_FID = true, - }, 0); + }, 0) catch |err| switch (err) { + error.UnsupportedFlags => fatal("fanotify_init failed due to old kernel; requires 5.17+", .{}), + else => |e| return e, + }; return .{ .dir_table = .{}, .os = switch (builtin.os.tag) { diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 94b37a0878..8eb6ed256b 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -4544,13 +4544,16 @@ pub const FanotifyInitError = error{ SystemFdQuotaExceeded, SystemResources, PermissionDenied, + /// The kernel does not recognize the flags passed, likely because it is an + /// older version. + UnsupportedFlags, } || UnexpectedError; pub fn fanotify_init(flags: std.os.linux.fanotify.InitFlags, event_f_flags: u32) FanotifyInitError!i32 { const rc = system.fanotify_init(flags, event_f_flags); switch (errno(rc)) { .SUCCESS => return @intCast(rc), - .INVAL => unreachable, + .INVAL => return error.UnsupportedFlags, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOMEM => return error.SystemResources,