2

I've seen a few implementations of popen()/pclose(). They all used a static list of pids, and no locking:

static int *pids;
static int fds;

if (!pids) {
        if ((fds = getdtablesize()) <= 0)
            return (NULL);
        if ((pids = malloc(fds * sizeof(int))) == NULL)
            return (NULL);
        memset(pids, 0, fds * sizeof(int));
    }

Or this, supposedly NetBSD:

static struct pid {
    struct pid *next;
    FILE *fp;
    pid_t pid;
} *pidlist; 

    /* Link into list of file descriptors. */
    cur->fp = iop;
    cur->pid =  pid;
    cur->next = pidlist;
    pidlist = cur;

Is it what it looks like - a not thread safe implementation? Or am I missing something obvious?

n-alexander
  • 14,663
  • 12
  • 42
  • 43

2 Answers2

3

I don't think you're missing something obvious. I don't see any reference to thread-safety with popen() either requiring it to be thread-safe or not. As a result you should probably treat it as non-thread-safe.

Aaron
  • 9,123
  • 5
  • 40
  • 38
  • I remember when threads were getting supported, man pages used to tell you explicitly if a function was safe or unsafe. Now I expect a function to be safe it not told otherwise.. – n-alexander Nov 09 '09 at 17:41
  • 2
    That would be nice. However - I would reverse your expectation -- functions are generally unsafe unless the author actually thought about the thread issues. And if it's not mentioned in the man page then they probably didn't think about it. – Aaron Nov 09 '09 at 17:44
  • At least on Linux, the manpages aren't necessarily written by the glibc folks who write the implementations. – bdonlan Nov 09 '09 at 18:04
  • Posix requires `popen` to be thread safe. The Posix standard states that all functions are thread safe except, followed by a (very short) list of those that aren't. `popen` isn't in the list. – James Kanze Feb 28 '13 at 12:52
3

The GNU libc implementation is threadsafe if libc is configured to be reentrant (which it is likely to be). However, this may not be the case for other implementations of libc.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • yes, I see it locks the list of children. This is much better. Thanks for the link. I'd rather popen returned a pid and I passed that to pclose, though, but that's not an option for libc in this case – n-alexander Nov 11 '09 at 11:39