Linux systems programming, suggestions wanted

OK, I know people will find this amusing, but I am doing system level Linux programming for the first time in a very long time (15+ years?) and I'm not actually sure what the best recommendation is for doing something as simple as starting a process.

Now, since I'm trying to get some low level infrastructure to work correctly, I want to do things right.

So, the question is: How do I properly detect that execv has failed? Sure, the function will return, but when what do I do?

I then remembered seeing that the JVM implementation of Runtime.exec is implemented by sending messages on a pipe, so I decided to try that.

I tried using pipe to create a pipe, I set the CLOEXEC flag on it, and I then send a message on the pipe after execv returns. The idea was that if execv suceeded, it would close the pipe, and I can monitor this using epoll on the calling side. This doesn't work, for some reason that I have yet to figure out (I don't get a notification when the pipe is closed, perhaps it isn't closed at all?)

Last time I did this, I was using Solaris, so I fixed all the race conditions and other issues that can occur with fork and exec by using posix_spawn. But it appears that Linux doesn't actually implement this on the kernel level.

Follow

@loke Maybe you can use waitpid() instead of sending a message through the pipe?

When using fork() it can be non-intuitive how execution is handled going forward (at least it was for me): "On success, the PID of the child process is returned in the parent, and 0 is returned in the child." If you want to use waitpid() (and its macros) then do that in the parent.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.