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?