Commit 4442288656

Andrew Kelley <andrew@ziglang.org>
2024-09-25 18:54:41
std: fix inappropriate use of unreachable in fanotify_init
1 parent 4ceefca
Changed files (2)
lib
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) {
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,