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

std.posix: Added error message 'ProcessNotFound' for reading and writing in a Linux process #21430

Merged
merged 7 commits into from
Oct 3, 2024

Conversation

chrboesch
Copy link
Contributor

This error occurs if the process to be read from or written to no longer exists.

Fixes #19875

@chrboesch
Copy link
Contributor Author

I can't complete this PR due to the complexity of the dependencies. Since I can't test on MacOS or Windows and the error message only says "test-fmt transitive error", I don't know how to fix this.

@alexrp
Copy link
Member

alexrp commented Sep 16, 2024

It means you have code formatting violations; run zig build fmt.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be doc comments (/// instead of //)

Copy link
Contributor

@linusg linusg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles ENOENT aka errno 2 aka error.FileNotFound, the linked issue is about ESRCH (errno 3).

@chrboesch
Copy link
Contributor Author

Because Linux returns the ENOENT error for a process (aka file) that no longer exists when trying to read or write to it.
The ESRCH error occurs when you try to kill a process or process group that no longer exists.

@linusg
Copy link
Contributor

linusg commented Sep 18, 2024

Then it would be good to note that in the original issue so the author knows they're mistaken or can clarify the situation in which ESRCH gets returned :)

@chrboesch
Copy link
Contributor Author

#19875 (comment)

@alexrp
Copy link
Member

alexrp commented Sep 24, 2024

Actually, I'm not so confident in this change on second thought, given this: giampaolo/psutil@22651a6#diff-d969f5377d3dc82ba7501f4c9f6d6510e1240df2debdca7c726a9ff37fde7af7R1340-R1349

I think this behavior needs to be verified manually.

@chrboesch
Copy link
Contributor Author

I think this behavior needs to be verified manually.

What do you mean exactly?

@alexrp
Copy link
Member

alexrp commented Sep 25, 2024

As in, write a test program that does:

  • open("/proc/pid/maps", ...) for a non-existent pid, and then checks what the returned open() error is.
  • open("/proc/pid/maps", ...) for a running pid, then kill(pid, SIGKILL) and read(fd, &buf, 1024), and then checks what the returned read() error is.

@chrboesch
Copy link
Contributor Author

I did this manually on two different Linux systems (Arch, Gentoo) to check which error I get. A test program is difficult because the PIDs change every time and in principle two programs have to start, which tell each other the PID they currently have. Would be feasible. Only why? I think the result will be the same as with my tests. And if that changes in Linux (in a future kernel), we have to make adjustments anyway.

@alexrp
Copy link
Member

alexrp commented Sep 25, 2024

The commit I linked above claims that the returned error for read() on a dead process is ESRCH rather than ENOENT. I don't know which is correct, which is why I suggested verifying it manually with a test program. But if you've already done that, then there's no issue.

@chrboesch
Copy link
Contributor Author

I also assumed that an ESRCH error would occur, but I received ENOENT. I then analyzed the kernel source code in 'fs/proc/fd.c', and in the function 'proc_readfd_common', only ENOENT is used. No idea what the Python folks are doing, but ESRCH is definitely not returned on an x64 system.
Here the source from 'linux_6.6.47:

static int proc_readfd_common(struct file *file, struct dir_context *ctx,
			      instantiate_t instantiate)
{
	struct task_struct *p = get_proc_task(file_inode(file));
	unsigned int fd;

	if (!p)
		return -ENOENT;

...

@mlugg
Copy link
Member

mlugg commented Sep 26, 2024

Please don't post LLM-generated content on the issue tracker. A glorified text prediction engine does not provide any useful perspective or information.

@chrboesch
Copy link
Contributor Author

No prob, I deleted it.

@alexrp alexrp enabled auto-merge (squash) October 2, 2024 21:26
@alexrp alexrp merged commit e22d79d into ziglang:master Oct 3, 2024
10 checks passed
@chrboesch chrboesch deleted the i19875 branch October 7, 2024 15:34
richerfu pushed a commit to richerfu/zig that referenced this pull request 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESRCH is not handled by file io operations
5 participants