From 05d7820d63dd5abe4f49675ca93cbbf059ff278c Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:26:12 +0200 Subject: [PATCH 1/7] Added error message 'ProcessNotFound' for reading and writing in a Linux process. This error occurs if the process to be read from or written to no longer exists. Fixes #19875 --- lib/std/posix.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 8eb6ed256be5..396b2a0f1e5f 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -798,6 +798,11 @@ pub const ReadError = error{ /// In WASI, this error occurs when the file descriptor does /// not hold the required rights to read from it. AccessDenied, + + // This error occurs in Linux if the process to be read from + // no longer exists. + ProcessNotFound, + } || UnexpectedError; /// Returns the number of bytes that were read, which can be less than @@ -854,6 +859,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .CANCELED => return error.Canceled, .BADF => return error.NotOpenForReading, // Can be a race condition. @@ -917,6 +923,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // can be a race condition .IO => return error.InputOutput, @@ -996,6 +1003,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize { .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // Can be a race condition. .IO => return error.InputOutput, @@ -1133,6 +1141,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // can be a race condition .IO => return error.InputOutput, @@ -1176,6 +1185,11 @@ pub const WriteError = error{ /// Connection reset by peer. ConnectionResetByPeer, + + // This error occurs in Linux if the process being written to + // no longer exists. + ProcessNotFound, + } || UnexpectedError; /// Write to a file descriptor. @@ -1243,6 +1257,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { .INTR => continue, .INVAL => return error.InvalidArgument, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. @@ -1315,6 +1330,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { .INTR => continue, .INVAL => return error.InvalidArgument, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. @@ -1404,6 +1420,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { .INTR => continue, .INVAL => return error.InvalidArgument, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. @@ -1488,6 +1505,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz .INTR => continue, .INVAL => return error.InvalidArgument, .FAULT => unreachable, + .NOENT => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. From 372f87727c2a205a6f1546d7f34c73507002e41b Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:40:57 +0200 Subject: [PATCH 2/7] Added error message "ProcessNotFound" for error forwarding. --- lib/compiler/fmt.zig | 1 + lib/std/zig/system.zig | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/compiler/fmt.zig b/lib/compiler/fmt.zig index c747addd72aa..5a4f112c34b0 100644 --- a/lib/compiler/fmt.zig +++ b/lib/compiler/fmt.zig @@ -207,6 +207,7 @@ const FmtError = error{ LockViolation, NetNameDeleted, InvalidArgument, + ProcessNotFound, } || fs.File.OpenError; fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index 046bd3854e45..77d9efb2a11b 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -443,6 +443,7 @@ pub const AbiAndDynamicLinkerFromFileError = error{ Unexpected, UnexpectedEndOfFile, NameTooLong, + ProcessNotFound, }; pub fn abiAndDynamicLinkerFromFile( @@ -1176,6 +1177,7 @@ fn preadAtLeast(file: fs.File, buf: []u8, offset: u64, min_read_len: usize) !usi error.Unexpected => return error.Unexpected, error.InputOutput => return error.FileSystem, error.AccessDenied => return error.Unexpected, + error.ProcessNotFound => return error.ProcessNotFound, }; if (len == 0) return error.UnexpectedEndOfFile; i += len; From c57c56e0cef71360b7265b88f31cb8b578e1049a Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:55:19 +0200 Subject: [PATCH 3/7] Add error messgae for forwarding. --- lib/std/zig/system.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index 77d9efb2a11b..26aed9566c7b 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -1078,6 +1078,7 @@ fn detectAbiAndDynamicLinker( const len = preadAtLeast(file, &buffer, 0, min_len) catch |err| switch (err) { error.UnexpectedEndOfFile, error.UnableToReadElfFile, + error.ProcessNotFound, => return defaultAbiAndDynamicLinker(cpu, os, query), else => |e| return e, From 834e351e42b3160b42f2b7ad6a7354b6a1c81252 Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:59:43 +0200 Subject: [PATCH 4/7] Added message for forwarding. --- lib/std/zig/system.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index 26aed9566c7b..4f2a01cb4fb7 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -832,6 +832,7 @@ fn glibcVerFromRPath(rpath: []const u8) !std.SemanticVersion { error.UnableToReadElfFile, error.Unexpected, error.FileSystem, + error.ProcessNotFound, => |e| return e, }; } @@ -1122,6 +1123,7 @@ fn detectAbiAndDynamicLinker( error.SymLinkLoop, error.ProcessFdQuotaExceeded, error.SystemFdQuotaExceeded, + error.ProcessNotFound, => |e| return e, error.UnableToReadElfFile, From 03d6c084f7ae07bdbb1d8d6a44adc2dcb8f52884 Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:27:14 +0200 Subject: [PATCH 5/7] Error set completed. --- lib/std/zig/system.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index 4f2a01cb4fb7..51da2cc6f93a 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -172,6 +172,7 @@ pub const DetectError = error{ DeviceBusy, OSVersionDetectionFail, Unexpected, + ProcessNotFound, }; /// Given a `Target.Query`, which specifies in detail which parts of the From 30d18048457a7c32a8ad39d4ae41e195f87cb3a6 Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:10:16 +0200 Subject: [PATCH 6/7] Fixed format error. --- lib/std/posix.zig | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 396b2a0f1e5f..05b598b51878 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -802,7 +802,6 @@ pub const ReadError = error{ // This error occurs in Linux if the process to be read from // no longer exists. ProcessNotFound, - } || UnexpectedError; /// Returns the number of bytes that were read, which can be less than @@ -1189,7 +1188,6 @@ pub const WriteError = error{ // This error occurs in Linux if the process being written to // no longer exists. ProcessNotFound, - } || UnexpectedError; /// Write to a file descriptor. From 0bdd25212c8ff909a39579fc312c30047b7aae88 Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:17:15 +0200 Subject: [PATCH 7/7] Changed comments to doc comments. --- lib/std/posix.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 05b598b51878..000f0898d18f 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -799,8 +799,8 @@ pub const ReadError = error{ /// not hold the required rights to read from it. AccessDenied, - // This error occurs in Linux if the process to be read from - // no longer exists. + /// This error occurs in Linux if the process to be read from + /// no longer exists. ProcessNotFound, } || UnexpectedError; @@ -1185,8 +1185,8 @@ pub const WriteError = error{ /// Connection reset by peer. ConnectionResetByPeer, - // This error occurs in Linux if the process being written to - // no longer exists. + /// This error occurs in Linux if the process being written to + /// no longer exists. ProcessNotFound, } || UnexpectedError;