How do `std::process::Command` permissions work?

Hiya---I hope everyone is having a good one. When I try to use certain shell built-in commands (that I've confirmed exist in my shell) with std::process::Command, I get various errors:

Running
std::process::Command::new(".").arg("some/source/file/path").output()
seems to return a permission IO error even if I run my rust program (via cargo) with sudo

Running
std::process::Command::new("command").args(&["-v", "ls"]).output()
seems to return a file not found IO error when I run my rust without sudo

So I guess this means the child process doesn't have sufficient permissions? Is this something I can work around? More importantly, how can I know what I'm working with or where to read more? I looked at the source for std::process, but I remain, as always, blind to insight.

The . (dot, an alias for source) is a functionality of your shell, not an executable file. You can verify this with type or which:

$ type .
. is a shell builtin

The same is true for command. Regarding ., it is meaningless to execute in in a subprocess, as you'll not be able to observe the effect it has. Instead of command, you can call "sh" with ["-c", "command -v ls"] or call which or similar.

Edit: The permission error probably comes from the kernel saying you can't execute a directory, where . means current directory.

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.