Skip to content

Commit

Permalink
std: fix inappropriate use of unreachable in fanotify_init
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Sep 25, 2024
1 parent 4ceefca commit 4442288
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/std/Build/Watch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,18 @@ 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,
.REPORT_NAME = true,
.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) {
Expand Down
5 changes: 4 additions & 1 deletion lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4442288

Please sign in to comment.