Thursday, May 21, 2015

How to properly spawn an external command in C (or not use posix_spawn)

When spawning an external command, as a programmer, you would definitely want to determine if you have succeeded in doing so.

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!

13 comments:

  1. Your blog was too good. i really appreciate with your blog.Thanks for sharing.
    IPL Auction 2016
    IPL Auction 2016 Players
    IPL Auction 2016 Teams

    ReplyDelete
  2. This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
    animal jam 2 | five nights at freddys 2 | hotmail login

    ReplyDelete
  3. 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!
    happy wheels

    ReplyDelete
  4. /* 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);
    }
    rolling sky

    ReplyDelete
  5. 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
    some demo versions include Happy wheels demo & happy wheels game
    Play Free Games Play fireboy and watergirl Fireboy & Watergirl unblocked

    ReplyDelete
  6. 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

    ReplyDelete
  7. Webroot.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

Note: Only a member of this blog may post a comment.