Skip to content

Commit

Permalink
std.posix: Added error message 'ProcessNotFound' for reading and writ…
Browse files Browse the repository at this point in the history
…ing in a Linux process (ziglang#21430)

* 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 ziglang#19875

* Added error message "ProcessNotFound" for error forwarding.

* Add error messgae for forwarding.

* Added message for forwarding.

* Error set completed.

* Fixed format error.

* Changed comments to doc comments.
  • Loading branch information
chrboesch authored and richerfu committed Oct 28, 2024
1 parent c58405a commit c7084cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/compiler/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,10 @@ 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
Expand Down Expand Up @@ -854,6 +858,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.
Expand Down Expand Up @@ -917,6 +922,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,
Expand Down Expand Up @@ -996,6 +1002,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,
Expand Down Expand Up @@ -1133,6 +1140,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,
Expand Down Expand Up @@ -1176,6 +1184,10 @@ 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.
Expand Down Expand Up @@ -1243,6 +1255,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.
Expand Down Expand Up @@ -1315,6 +1328,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.
Expand Down Expand Up @@ -1404,6 +1418,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.
Expand Down Expand Up @@ -1488,6 +1503,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.
Expand Down
6 changes: 6 additions & 0 deletions lib/std/zig/system.zig
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ pub const DetectError = error{
DeviceBusy,
OSVersionDetectionFail,
Unexpected,
ProcessNotFound,
};

/// Given a `Target.Query`, which specifies in detail which parts of the
Expand Down Expand Up @@ -443,6 +444,7 @@ pub const AbiAndDynamicLinkerFromFileError = error{
Unexpected,
UnexpectedEndOfFile,
NameTooLong,
ProcessNotFound,
};

pub fn abiAndDynamicLinkerFromFile(
Expand Down Expand Up @@ -831,6 +833,7 @@ fn glibcVerFromRPath(rpath: []const u8) !std.SemanticVersion {
error.UnableToReadElfFile,
error.Unexpected,
error.FileSystem,
error.ProcessNotFound,
=> |e| return e,
};
}
Expand Down Expand Up @@ -1077,6 +1080,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,
Expand Down Expand Up @@ -1120,6 +1124,7 @@ fn detectAbiAndDynamicLinker(
error.SymLinkLoop,
error.ProcessFdQuotaExceeded,
error.SystemFdQuotaExceeded,
error.ProcessNotFound,
=> |e| return e,

error.UnableToReadElfFile,
Expand Down Expand Up @@ -1176,6 +1181,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;
Expand Down

0 comments on commit c7084cb

Please sign in to comment.