Unfortunately,
posix_spawn
(and posix_spawnp
) does not provide such a feature. To be accurate, there is no guaranteed way to synchronously determine if the function has succeeded in spawning the command synchronously.In case of Linux, the function returns zero (i.e. success) even if the external command does not exist.
The document suggests that if the function succeeded in spawning the command should be determined asynchronously by checking the exit status of
waitpid
. But such approach (that waits for the termination of the sub-process) cannot be used if your intension is to spawn a external command that is going to run continuously.Recently I have faced the issue while working on H2O, and have come up with a solution; a function that spawns an external command that synchronously returns an error if it failed to do so.
What follows is the core logic I implemented. It is fairly simple; it uses the traditional approach of spawning an external command:
fork
and execvp
. And at the same time uses a pipe
with FD_CLOEXEC
flag set to detect the success of execvp
(the pipe gets closed), which is also used for returning errno
in case the syscall fails.pid_t safe_spawnp(const char *cmd, char **argv) { int pipefds[2] = {-1, -1}, errnum; pid_t pid; ssize_t rret; /* create pipe, used for sending error codes */ if (pipe2(pipefds, O_CLOEXEC) != 0) goto Error; /* fork */ if ((pid = fork()) == -1) goto Error; if (pid == 0) { /* in child process */ execvp(cmd, argv); errnum = errno; write(pipefds[1], &errnum, sizeof(errnum)); _exit(127); } /* parent process */ close(pipefds[1]); pipefds[1] = -1; errnum = 0; while ((rret = read(pipefds[0], &errnum, sizeof(errnum))) == -1 && errno == EINTR) ; if (rret != 0) { /* spawn failed */ while (waitpid(pid, NULL, 0) != pid) ; pid = -1; errno = errnum; goto Error; } /* spawn succeeded */ close(pipefds[0]); return pid; Error: errnum = errno; if (pipefds[0] != -1) close(pipefds[0]); if (pipefds[1] != -1) close(pipefds[1]); errno = errnum; return -1; }
The actual implementation used in H2O does more; it has a feature to remap the file descriptors so that the caller can communicate with the spawned command via pipes. You can find the implementation here.
I am not sure if this kind of workaround is also needed for other languages, but I am afraid it might be the case.
Anyways I wrote this blogpost as a memo for myself and hopefully others. Happy hacking!
Your blog was too good. i really appreciate with your blog.Thanks for sharing.
ReplyDeleteIPL Auction 2016
IPL Auction 2016 Players
IPL Auction 2016 Teams
Thanks you for this post… great information.
ReplyDeleteIPL 2016 Schedule time table
IPL 2016 Schedule release date
fan box office prediction shahrukh khan
ReplyDeletefan box office prediction shahrukh khan
fan box office prediction shahrukh khan
fan movie first look official teaser
fan movie first look official teaser
fan box office prediction
Your blog was too good. i really appreciate with your blog.Thanks for sharing.
ReplyDeleteGujarat Lions Team Squad Captain Name
DD New Players List
RPS 2016 Logo Jersey Images Wallpapers
hermes bags
ReplyDeletekd 8 shoes
rolex watches for sale
nfl jerseys wholesale
kate spade
christian louboutin outlet
ralph lauren outlet online
true religion jeans outlet
louis vuitton outlet store
coach outlet online
zhi20161216
This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
ReplyDeleteanimal jam 2 | five nights at freddys 2 | hotmail login
Pretty good post. I found your website perfect for my needs. Thanks for sharing the great ideas. I liked the article, Ill be back to read more of your blog later =) Thanks for posting it, again!
ReplyDeletehappy wheels
Klasifikasi lain antara lain :Toko Besi Pipa Baja Schedule 40
ReplyDeleteAgen Pipa Besi baja Schedule sch 80
Jual besi unp baja
Supplier besi beton master steel ms
Supplier plat kapal besi baja
Supplier plat kapal besi baja
Agen atap spandek
/* fork */
ReplyDeleteif ((pid = fork()) == -1)
goto Error;
if (pid == 0) {
/* in child process */
execvp(cmd, argv);
errnum = errno;
write(pipefds[1], &errnum, sizeof(errnum));
_exit(127);
}
rolling sky
نقل العفش بالرياض
ReplyDeleteنقل عفش بالدمام
شركات نقل اثاث بالدمام
شركة نقل اثاث بالخبر
شركة نقل عفش بجدة
happy wheels is a ragdoll physics-based platform browser game developed and published by Fancy Force. Created by video game designer Jim Bonacci in 2010, the game features several player characters, who use different, sometimes atypical, vehicles to traverse the game's many levels. The happy wheels game is best known for its graphic and the amount of user-generated content its players produce on a regular basis, with game maps shared on a public server.I used to play Happy Wheels 2 game online Happy Weels
ReplyDeletesome demo versions include Happy wheels demo & happy wheels game
Play Free Games Play fireboy and watergirl Fireboy & Watergirl unblocked
Tu Aashiqui is back again with new episode for the viewers so watch each episode with thrill again. The review of this drama serial is good. and the strory line is much intersting and unique and from the start. No Single scene of this drama is boring. Tu Aashiqui
ReplyDeleteWebroot.com/safe is a protection software solution that communicates with the cloud avoiding the hassle to manage the signature updates to deploy. for office setup visit office.com/setup
ReplyDelete