Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 7 Jul 2005 11:14:25 +0530
From:      Dipjyoti Saikia <dipjyoti.saikia@gmail.com>
To:        Dan Nelson <dnelson@allantgroup.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: thread-safe popen
Message-ID:  <dcbc809d050706224410fff9c4@mail.gmail.com>
In-Reply-To: <20050705143814.GA38925@dan.emsphone.com>
References:  <dcbc809d05070503454a259f88@mail.gmail.com> <20050705143814.GA38925@dan.emsphone.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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 =3D 1;
                type =3D "r+";
        } else  {
                twoway =3D 0;
                if ((*type !=3D 'r' && *type !=3D 'w') || type[1])
                        return (NULL);
        }
        if (pipe(pdes) < 0)
                return (NULL);

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

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


                switch (pid =3D vfork()) {
        case -1:                        /* Error. */
                (void)_close(pdes[0]);
                (void)_close(pdes[1]);
                free(cur);
                return (NULL);
                /* NOTREACHED */
        case 0:                         /* Child. */
                if (*type =3D=3D '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] !=3D STDOUT_FILENO) {
                                (void)dup2(pdes[1], STDOUT_FILENO);
                                (void)_close(pdes[1]);
                                if (twoway)
                                        (void)dup2(STDOUT_FILENO, STDIN_FIL=
ENO);
                        } else if (twoway && (pdes[1] !=3D STDIN_FILENO))
                                (void)dup2(pdes[1], STDIN_FILENO);
                } else {
                        if (pdes[0] !=3D STDIN_FILENO) {
                                (void)dup2(pdes[0], STDIN_FILENO);
                                (void)_close(pdes[0]);
                        }
                        (void)_close(pdes[1]);
                }
                for (p =3D pidlist; p; p =3D 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=20
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@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 .
>=20
> 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?
>=20
> --
>        Dan Nelson
>        dnelson@allantgroup.com
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?dcbc809d050706224410fff9c4>