Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESRCH is not handled by file io operations #19875

Closed
breavyn opened this issue May 6, 2024 · 5 comments · Fixed by #21430 · May be fixed by #23268
Closed

ESRCH is not handled by file io operations #19875

breavyn opened this issue May 6, 2024 · 5 comments · Fixed by #21430 · May be fixed by #23268
Labels
bug Observed behavior contradicts documented or intended behavior os-linux standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@breavyn
Copy link

breavyn commented May 6, 2024

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

Reading or writing files under /proc/<pid>/ on linux can result in an ESRCH error if the related process has terminated.

Functions read, pread, readv, etc. and their write counterparts, seek, and possibly others, return error.Unexpected in this case.

Expected Behavior

Functions should return error.ProcessNotFound on ESRCH.

@breavyn breavyn added the bug Observed behavior contradicts documented or intended behavior label May 6, 2024
@Vexu Vexu added standard library This issue involves writing Zig code for the standard library. os-linux labels May 31, 2024
@Vexu Vexu added this to the 0.15.0 milestone May 31, 2024
chrboesch added a commit to chrboesch/zig that referenced this issue Sep 15, 2024
process.
This error occurs if the process to be read from or written to no longer exists.
Fixes ziglang#19875
@chrboesch
Copy link
Contributor

Reading or writing files under /proc// on linux can result in an ESRCH error if the related process has terminated.

Linux returns the ENOENT error for a process (aka file) that no longer exists when trying to read or write to it. So I added 'ProcessNotFound' on this.

Functions should return error.ProcessNotFound on ESRCH.

This error occurs when you try to kill a process or process group that no longer exists.

alexrp pushed a commit to chrboesch/zig that referenced this issue Oct 2, 2024
process.
This error occurs if the process to be read from or written to no longer exists.
Fixes ziglang#19875
alexrp pushed a commit that referenced this issue Oct 3, 2024
…ing 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.
richerfu pushed a commit to richerfu/zig that referenced this issue Oct 28, 2024
…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.
@alexrp alexrp modified the milestones: 0.15.0, 0.14.0 Dec 8, 2024
@KilianHanich
Copy link

Reading or writing files under /proc// on linux can result in an ESRCH error if the related process has terminated.

Linux returns the ENOENT error for a process (aka file) that no longer exists when trying to read or write to it. So I added 'ProcessNotFound' on this.

Ehm, I get 3/ESRCH on an error, not 2/ENOENT, which also wouldn't really make a whole lot of sense since we already have an open file descriptor.

Here's how to check that too:

  1. Start some process you can kill later, e.g. sleep 60m and get its pid.
  2. Run this but insert the pid you got:
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

int main() {
        int proc = open("/proc/<PID>", O_DIRECTORY | O_RDONLY);
        if (proc < 0) return 1;
        int fd = openat(proc, "cpuset", O_RDONLY);
        if (fd < 0) return 2;
        getchar();
        char buf[5];
        if (0 > read(fd, buf, 5)) {
                perror("read");
                printf("%d\n", errno);
                return -1;
        }
        close(fd);
}
  1. Before hitting enter enter after starting, kill the process you created.
  2. Hit Enter.
  3. I got the following error message:
read: No such process
3```

@chrboesch
Copy link
Contributor

Thanks, I'll look into this again.

@chrboesch
Copy link
Contributor

As far as I can tell, the Linux kernel issues both 'ENOENT' and 'ESRCH', depending on whether the operation is at the file or process level. Therefore, it makes sense to catch both errors and return 'error.ProcessNotFound' accordingly, because that's what it boils down to.
I've added this and tested it with different variations; all the error messages now seem understandable to me.
PR follows shortly.

@chrboesch
Copy link
Contributor

Here is my Zig test program, which causes the correct error after merging the PR.

const std = @import("std");

pub fn main() !void {
    const proc = try std.posix.open("/proc/<PID>", .{}, 0);
    std.debug.print("proc={d}\n", .{proc});

    const fd = try std.posix.openat(proc, "cpuset", .{}, 0);
    defer _ = std.posix.close(fd);
    std.debug.print("fd={d}\n", .{fd});

    // wait for a key
    const reader = std.io.getStdIn().reader();
    _ = try reader.readByte();

    var buf: [5]u8 = undefined;
    const res = try std.posix.read(fd, buf[0..]);

    std.debug.print("res={d}\n", .{res});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-linux standard library This issue involves writing Zig code for the standard library.
Projects
None yet
5 participants