authorgravatar for 48591413+chrboesch@users.noreply.github.comChris Boesch <48591413+chrboesch@users.noreply.github.com> 2024-10-03 03:54:30+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-10-03 01:54:30+00:00
loge22d79dacb52468e1602dfb6849c3ab96d846294
tree85634c9a0d1b276e9c567c9542cbdcf398e68bd3
parent8ee52f99ceab6a05b931eeb48825c00fd26ac486
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.posix: Added error message 'ProcessNotFound' for reading and writing in a Linux process (#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 #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.

3 files changed, 23 insertions(+), 0 deletions(-)

lib/compiler/fmt.zig+1
...@@ -207,6 +207,7 @@ const FmtError = error{...@@ -207,6 +207,7 @@ const FmtError = error{
207 LockViolation,207 LockViolation,
208 NetNameDeleted,208 NetNameDeleted,
209 InvalidArgument,209 InvalidArgument,
210 ProcessNotFound,
210} || fs.File.OpenError;211} || fs.File.OpenError;
211212
212fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {213fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {
lib/std/posix.zig+16
...@@ -798,6 +798,10 @@ pub const ReadError = error{...@@ -798,6 +798,10 @@ pub const ReadError = error{
798 /// In WASI, this error occurs when the file descriptor does798 /// In WASI, this error occurs when the file descriptor does
799 /// not hold the required rights to read from it.799 /// not hold the required rights to read from it.
800 AccessDenied,800 AccessDenied,
801
802 /// This error occurs in Linux if the process to be read from
803 /// no longer exists.
804 ProcessNotFound,
801} || UnexpectedError;805} || UnexpectedError;
802806
803/// Returns the number of bytes that were read, which can be less than807/// Returns the number of bytes that were read, which can be less than
...@@ -854,6 +858,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -854,6 +858,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
854 .INTR => continue,858 .INTR => continue,
855 .INVAL => unreachable,859 .INVAL => unreachable,
856 .FAULT => unreachable,860 .FAULT => unreachable,
861 .NOENT => return error.ProcessNotFound,
857 .AGAIN => return error.WouldBlock,862 .AGAIN => return error.WouldBlock,
858 .CANCELED => return error.Canceled,863 .CANCELED => return error.Canceled,
859 .BADF => return error.NotOpenForReading, // Can be a race condition.864 .BADF => return error.NotOpenForReading, // Can be a race condition.
...@@ -917,6 +922,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {...@@ -917,6 +922,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
917 .INTR => continue,922 .INTR => continue,
918 .INVAL => unreachable,923 .INVAL => unreachable,
919 .FAULT => unreachable,924 .FAULT => unreachable,
925 .NOENT => return error.ProcessNotFound,
920 .AGAIN => return error.WouldBlock,926 .AGAIN => return error.WouldBlock,
921 .BADF => return error.NotOpenForReading, // can be a race condition927 .BADF => return error.NotOpenForReading, // can be a race condition
922 .IO => return error.InputOutput,928 .IO => return error.InputOutput,
...@@ -996,6 +1002,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {...@@ -996,6 +1002,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
996 .INTR => continue,1002 .INTR => continue,
997 .INVAL => unreachable,1003 .INVAL => unreachable,
998 .FAULT => unreachable,1004 .FAULT => unreachable,
1005 .NOENT => return error.ProcessNotFound,
999 .AGAIN => return error.WouldBlock,1006 .AGAIN => return error.WouldBlock,
1000 .BADF => return error.NotOpenForReading, // Can be a race condition.1007 .BADF => return error.NotOpenForReading, // Can be a race condition.
1001 .IO => return error.InputOutput,1008 .IO => return error.InputOutput,
...@@ -1133,6 +1140,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {...@@ -1133,6 +1140,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
1133 .INTR => continue,1140 .INTR => continue,
1134 .INVAL => unreachable,1141 .INVAL => unreachable,
1135 .FAULT => unreachable,1142 .FAULT => unreachable,
1143 .NOENT => return error.ProcessNotFound,
1136 .AGAIN => return error.WouldBlock,1144 .AGAIN => return error.WouldBlock,
1137 .BADF => return error.NotOpenForReading, // can be a race condition1145 .BADF => return error.NotOpenForReading, // can be a race condition
1138 .IO => return error.InputOutput,1146 .IO => return error.InputOutput,
...@@ -1176,6 +1184,10 @@ pub const WriteError = error{...@@ -1176,6 +1184,10 @@ pub const WriteError = error{
11761184
1177 /// Connection reset by peer.1185 /// Connection reset by peer.
1178 ConnectionResetByPeer,1186 ConnectionResetByPeer,
1187
1188 /// This error occurs in Linux if the process being written to
1189 /// no longer exists.
1190 ProcessNotFound,
1179} || UnexpectedError;1191} || UnexpectedError;
11801192
1181/// Write to a file descriptor.1193/// Write to a file descriptor.
...@@ -1243,6 +1255,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {...@@ -1243,6 +1255,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
1243 .INTR => continue,1255 .INTR => continue,
1244 .INVAL => return error.InvalidArgument,1256 .INVAL => return error.InvalidArgument,
1245 .FAULT => unreachable,1257 .FAULT => unreachable,
1258 .NOENT => return error.ProcessNotFound,
1246 .AGAIN => return error.WouldBlock,1259 .AGAIN => return error.WouldBlock,
1247 .BADF => return error.NotOpenForWriting, // can be a race condition.1260 .BADF => return error.NotOpenForWriting, // can be a race condition.
1248 .DESTADDRREQ => unreachable, // `connect` was never called.1261 .DESTADDRREQ => unreachable, // `connect` was never called.
...@@ -1315,6 +1328,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {...@@ -1315,6 +1328,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
1315 .INTR => continue,1328 .INTR => continue,
1316 .INVAL => return error.InvalidArgument,1329 .INVAL => return error.InvalidArgument,
1317 .FAULT => unreachable,1330 .FAULT => unreachable,
1331 .NOENT => return error.ProcessNotFound,
1318 .AGAIN => return error.WouldBlock,1332 .AGAIN => return error.WouldBlock,
1319 .BADF => return error.NotOpenForWriting, // Can be a race condition.1333 .BADF => return error.NotOpenForWriting, // Can be a race condition.
1320 .DESTADDRREQ => unreachable, // `connect` was never called.1334 .DESTADDRREQ => unreachable, // `connect` was never called.
...@@ -1404,6 +1418,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {...@@ -1404,6 +1418,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
1404 .INTR => continue,1418 .INTR => continue,
1405 .INVAL => return error.InvalidArgument,1419 .INVAL => return error.InvalidArgument,
1406 .FAULT => unreachable,1420 .FAULT => unreachable,
1421 .NOENT => return error.ProcessNotFound,
1407 .AGAIN => return error.WouldBlock,1422 .AGAIN => return error.WouldBlock,
1408 .BADF => return error.NotOpenForWriting, // Can be a race condition.1423 .BADF => return error.NotOpenForWriting, // Can be a race condition.
1409 .DESTADDRREQ => unreachable, // `connect` was never called.1424 .DESTADDRREQ => unreachable, // `connect` was never called.
...@@ -1488,6 +1503,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz...@@ -1488,6 +1503,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
1488 .INTR => continue,1503 .INTR => continue,
1489 .INVAL => return error.InvalidArgument,1504 .INVAL => return error.InvalidArgument,
1490 .FAULT => unreachable,1505 .FAULT => unreachable,
1506 .NOENT => return error.ProcessNotFound,
1491 .AGAIN => return error.WouldBlock,1507 .AGAIN => return error.WouldBlock,
1492 .BADF => return error.NotOpenForWriting, // Can be a race condition.1508 .BADF => return error.NotOpenForWriting, // Can be a race condition.
1493 .DESTADDRREQ => unreachable, // `connect` was never called.1509 .DESTADDRREQ => unreachable, // `connect` was never called.
lib/std/zig/system.zig+6
...@@ -172,6 +172,7 @@ pub const DetectError = error{...@@ -172,6 +172,7 @@ pub const DetectError = error{
172 DeviceBusy,172 DeviceBusy,
173 OSVersionDetectionFail,173 OSVersionDetectionFail,
174 Unexpected,174 Unexpected,
175 ProcessNotFound,
175};176};
176177
177/// Given a `Target.Query`, which specifies in detail which parts of the178/// Given a `Target.Query`, which specifies in detail which parts of the
...@@ -443,6 +444,7 @@ pub const AbiAndDynamicLinkerFromFileError = error{...@@ -443,6 +444,7 @@ pub const AbiAndDynamicLinkerFromFileError = error{
443 Unexpected,444 Unexpected,
444 UnexpectedEndOfFile,445 UnexpectedEndOfFile,
445 NameTooLong,446 NameTooLong,
447 ProcessNotFound,
446};448};
447449
448pub fn abiAndDynamicLinkerFromFile(450pub fn abiAndDynamicLinkerFromFile(
...@@ -831,6 +833,7 @@ fn glibcVerFromRPath(rpath: []const u8) !std.SemanticVersion {...@@ -831,6 +833,7 @@ fn glibcVerFromRPath(rpath: []const u8) !std.SemanticVersion {
831 error.UnableToReadElfFile,833 error.UnableToReadElfFile,
832 error.Unexpected,834 error.Unexpected,
833 error.FileSystem,835 error.FileSystem,
836 error.ProcessNotFound,
834 => |e| return e,837 => |e| return e,
835 };838 };
836}839}
...@@ -1077,6 +1080,7 @@ fn detectAbiAndDynamicLinker(...@@ -1077,6 +1080,7 @@ fn detectAbiAndDynamicLinker(
1077 const len = preadAtLeast(file, &buffer, 0, min_len) catch |err| switch (err) {1080 const len = preadAtLeast(file, &buffer, 0, min_len) catch |err| switch (err) {
1078 error.UnexpectedEndOfFile,1081 error.UnexpectedEndOfFile,
1079 error.UnableToReadElfFile,1082 error.UnableToReadElfFile,
1083 error.ProcessNotFound,
1080 => return defaultAbiAndDynamicLinker(cpu, os, query),1084 => return defaultAbiAndDynamicLinker(cpu, os, query),
10811085
1082 else => |e| return e,1086 else => |e| return e,
...@@ -1120,6 +1124,7 @@ fn detectAbiAndDynamicLinker(...@@ -1120,6 +1124,7 @@ fn detectAbiAndDynamicLinker(
1120 error.SymLinkLoop,1124 error.SymLinkLoop,
1121 error.ProcessFdQuotaExceeded,1125 error.ProcessFdQuotaExceeded,
1122 error.SystemFdQuotaExceeded,1126 error.SystemFdQuotaExceeded,
1127 error.ProcessNotFound,
1123 => |e| return e,1128 => |e| return e,
11241129
1125 error.UnableToReadElfFile,1130 error.UnableToReadElfFile,
...@@ -1176,6 +1181,7 @@ fn preadAtLeast(file: fs.File, buf: []u8, offset: u64, min_read_len: usize) !usi...@@ -1176,6 +1181,7 @@ fn preadAtLeast(file: fs.File, buf: []u8, offset: u64, min_read_len: usize) !usi
1176 error.Unexpected => return error.Unexpected,1181 error.Unexpected => return error.Unexpected,
1177 error.InputOutput => return error.FileSystem,1182 error.InputOutput => return error.FileSystem,
1178 error.AccessDenied => return error.Unexpected,1183 error.AccessDenied => return error.Unexpected,
1184 error.ProcessNotFound => return error.ProcessNotFound,
1179 };1185 };
1180 if (len == 0) return error.UnexpectedEndOfFile;1186 if (len == 0) return error.UnexpectedEndOfFile;
1181 i += len;1187 i += len;