thread-safe popen

Dipjyoti Saikia dipjyoti.saikia at gmail.com
Thu Jul 7 05:44:26 GMT 2005


Hi ,

Please find the code snippet of popen() from the source code that I
have (We are working on an OS that is derived from FreeBSD 4.1) . I
don't think I have the thread safe version .

-------------------------

FILE *
popen(command, type)
        const char *command, *type;
{
        struct pid *cur;
        FILE *iop;
        int pdes[2], pid, twoway;
        char *argv[4];
        struct pid *p;

        /*
         * Lite2 introduced two-way popen() pipes using socketpair().
         * FreeBSD's pipe() is bidirectional, so we use that.
         */
        if (strchr(type, '+')) {
                twoway = 1;
                type = "r+";
        } else  {
                twoway = 0;
                if ((*type != 'r' && *type != 'w') || type[1])
                        return (NULL);
        }
        if (pipe(pdes) < 0)
                return (NULL);

        if ((cur = malloc(sizeof(struct pid))) == NULL) {
                (void)_close(pdes[0]);
                (void)_close(pdes[1]);
                return (NULL);
        }

        argv[0] = "sh";
        argv[1] = "-c";
        argv[2] = (char *)command;
        argv[3] = NULL;


                switch (pid = vfork()) {
        case -1:                        /* Error. */
                (void)_close(pdes[0]);
                (void)_close(pdes[1]);
                free(cur);
                return (NULL);
                /* NOTREACHED */
        case 0:                         /* Child. */
                if (*type == 'r') {
                        /*
                         * The dup2() to STDIN_FILENO is repeated to avoid
                         * writing to pdes[1], which might corrupt the
                         * parent's copy.  This isn't good enough in
                         * general, since the _exit() is no return, so
                         * the compiler is free to corrupt all the local
                         * variables.
                         */
                        (void)_close(pdes[0]);
                        if (pdes[1] != STDOUT_FILENO) {
                                (void)dup2(pdes[1], STDOUT_FILENO);
                                (void)_close(pdes[1]);
                                if (twoway)
                                        (void)dup2(STDOUT_FILENO, STDIN_FILENO);
                        } else if (twoway && (pdes[1] != STDIN_FILENO))
                                (void)dup2(pdes[1], STDIN_FILENO);
                } else {
                        if (pdes[0] != STDIN_FILENO) {
                                (void)dup2(pdes[0], STDIN_FILENO);
                                (void)_close(pdes[0]);
                        }
                        (void)_close(pdes[1]);
                }
                for (p = pidlist; p; p = p->next) {
                        (void)_close(fileno(p->fp));
                }
                execve(_PATH_BSHELL, argv, environ);
                _exit(127);
                /* NOTREACHED */
        }

      ---
      ----

}
--------------------------------------------------

We  had cases where our RAID applications (multi-threaded )  failed 
with invocations of popen() .  Initially we  handpicked the popen()
calls and replaced it with actual code of the functions but it was
simply too much work and little gain .

So we decided to backport a thread-safe version . (If the above code
is not thread-safe then we will have a bigger problem at hand .)

--Dip



On 7/5/05, Dan Nelson <dnelson at allantgroup.com> wrote:
> In the last episode (Jul 05), Dipjyoti Saikia said:
> > I am working on an OS derived for BSD 4.1 .  I am trying to backport
> > a thread-safe version of popen() from BSD 4.10 .
> 
> popen should be threadsafe as of rev 1.17 (2003-01-03) of
> /usr/src/lib/libc/gen/popen.c .  It was merged into the 4.* branch in
> rev 1.14.2.1 (2004/12/15).  The PR is bin/50770 .  Do you have a
> testcase that causes it to fail?
> 
> --
>        Dan Nelson
>        dnelson at allantgroup.com
>


More information about the freebsd-hackers mailing list