Reputation: 198
I am trying to use posix_spawn() to create a new child process. After the child process starts, the caller process should continue running.
TLDR: Why is posix_spawn() returning 0(success) even when path to child executable is invalid(does not exist)? How to correctly detect error in this case and any other cases where posix_spawn has actually failed but returns success?
I tried the following code.
/* The CALLER process*/
int main(int argc, char *argv) {
int status, pid;
printf("CALLER - Start\n");
char *args[] = {"/home/<user>/child_exec", NULL};
status = posix_spawn(&pid, args[0], NULL, NULL, args, environ);
printf("Status: %d; PID: %d\n", status, pid);
printf("CALLER - End\n");
return 0;
}
/* The CHILD process */
int main() {
printf("From CHILD\n");
return 0;
}
When I run the caller program with path to correct child executable, it runs as expected. The status of posix_spawn is 0 and the string from child process gets printed.
CALLER - Start
Status: 0; PID: 5110
CALLER - End
From CHILD
Now when I run the same program with an invalid child executable path(For example /home/user/child_exec123), it still returns status 0 even though the child process has not executed.
CALLER - Start
Status: 0; PID: 5251
CALLER - End
For this case where child path does not exist, I could just check existence of file before calling posix_spawn(). But what if there are other errors like these where posix_spawn() actually failed but returns 0? How do I find if there were some errors?
Upvotes: 2
Views: 4705
Reputation: 5407
posix_spawn
returns before child process start where incorrect path will be detected. So, any errors during start of child process you can check only by its exit value.
As described in documentation:
RETURN VALUE
Upon successful completion, posix_spawn() and posix_spawnp() place the PID of the child process in pid, and return 0. If there is an error during the fork() step, then no child is created, the contents of *pid are unspecified, and these functions return an error number as described below. Even when these functions return a success status, the child process may still fail for a plethora of reasons related to its pre-exec() initialization. In addition, the exec(3) may fail. In all of these cases, the child process will exit with the exit value of 127.
ERRORS
The posix_spawn() and posix_spawnp() functions fail only in the case where the underlying fork(2), vfork(2) or clone(2) call fails; in these cases, these functions return an error number, which will be one of the errors described for fork(2), vfork(2) or clone(2). In addition, these functions fail if: ENOSYS Function not supported on this system.
Upvotes: 3
Reputation: 7863
From the man page (in particular the second paragraph):
RETURN VALUE
Upon successful completion, posix_spawn() and posix_spawnp() place the
PID of the child process in pid, and return 0. If there is an error
before or during the fork(2), then no child is created, the contents of
*pid are unspecified, and these functions return an error number as de‐
scribed below.
Even when these functions return a success status, the child process
may still fail for a plethora of reasons related to its pre-exec() ini‐
tialization. In addition, the exec(3) may fail. In all of these
cases, the child process will exit with the exit value of 127.
You'll need to use one of the wait*
functions to check the result of your child process. This would be a good idea anyway, since otherwise you'd have a zombie.
Upvotes: 3