#ifdef VARIANT_DARWINEXTSN
#define _DARWIN_UNLIMITED_STREAMS
#endif
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95";
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/gen/popen.c,v 1.18 2003/01/04 00:15:15 tjr Exp $");
#include "namespace.h"
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <wchar.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <paths.h>
#include <pthread.h>
#include <spawn.h>
#include "un-namespace.h"
#include "libc_private.h"
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
struct pid {
struct pid *next;
FILE *fp;
int fd;
pid_t pid;
};
#define pidlist __popen_pidlist
#define pidlist_mutex __popen_pidlist_mutex
#ifndef BUILDING_VARIANT
__private_extern__ struct pid *pidlist = NULL;
__private_extern__ pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
extern struct pid *pidlist;
extern pthread_mutex_t pidlist_mutex;
#endif
#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
#define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
FILE *
popen(command, type)
const char *command, *type;
{
struct pid *cur;
FILE *iop;
int pdes[2], pid, twoway, other;
char *argv[4];
struct pid *p;
posix_spawn_file_actions_t file_actions;
int err;
if (type == NULL) {
errno = EINVAL;
return (NULL);
}
if (strcmp(type, "r+") == 0) {
twoway = 1;
type = "r+";
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
return (NULL);
} else {
twoway = 0;
if ((*type != 'r' && *type != 'w') || type[1]) {
errno = EINVAL;
return (NULL);
}
if (pipe(pdes) < 0)
return (NULL);
}
if (*type == 'r') {
iop = fdopen(pdes[0], type);
other = pdes[1];
} else {
iop = fdopen(pdes[1], type);
other = pdes[0];
}
if (iop == NULL) {
(void)_close(pdes[0]);
(void)_close(pdes[1]);
return (NULL);
}
if ((cur = malloc(sizeof(struct pid))) == NULL) {
(void)fclose(iop);
(void)_close(other);
return (NULL);
}
if ((err = posix_spawn_file_actions_init(&file_actions)) != 0) {
(void)fclose(iop);
(void)_close(other);
free(cur);
errno = err;
return (NULL);
}
if (*type == 'r') {
(void)posix_spawn_file_actions_addclose(&file_actions, pdes[0]);
if (pdes[1] != STDOUT_FILENO) {
(void)posix_spawn_file_actions_adddup2(&file_actions, pdes[1], STDOUT_FILENO);
(void)posix_spawn_file_actions_addclose(&file_actions, pdes[1]);
if (twoway)
(void)posix_spawn_file_actions_adddup2(&file_actions, STDOUT_FILENO, STDIN_FILENO);
} else if (twoway && (pdes[1] != STDIN_FILENO))
(void)posix_spawn_file_actions_adddup2(&file_actions, pdes[1], STDIN_FILENO);
} else {
if (pdes[0] != STDIN_FILENO) {
(void)posix_spawn_file_actions_adddup2(&file_actions, pdes[0], STDIN_FILENO);
(void)posix_spawn_file_actions_addclose(&file_actions, pdes[0]);
}
(void)posix_spawn_file_actions_addclose(&file_actions, pdes[1]);
}
for (p = pidlist; p; p = p->next) {
(void)posix_spawn_file_actions_addclose(&file_actions, p->fd);
}
argv[0] = "sh";
argv[1] = "-c";
argv[2] = (char *)command;
argv[3] = NULL;
err = posix_spawn(&pid, _PATH_BSHELL, &file_actions, NULL, argv, environ);
posix_spawn_file_actions_destroy(&file_actions);
if (err == ENOMEM || err == EAGAIN) {
(void)fclose(iop);
(void)_close(other);
free(cur);
errno = err;
return (NULL);
} else if (err != 0) {
pid = -1;
}
if (*type == 'r') {
cur->fd = pdes[0];
(void)_close(pdes[1]);
} else {
cur->fd = pdes[1];
(void)_close(pdes[0]);
}
cur->fp = iop;
cur->pid = pid;
THREAD_LOCK();
cur->next = pidlist;
pidlist = cur;
THREAD_UNLOCK();
fwide(iop, -1);
return (iop);
}
#ifndef BUILDING_VARIANT
int
pclose(iop)
FILE *iop;
{
struct pid *cur, *last;
int pstat;
pid_t pid;
THREAD_LOCK();
for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
if (cur->fp == iop)
break;
if (cur == NULL) {
THREAD_UNLOCK();
return (-1);
}
if (last == NULL)
pidlist = cur->next;
else
last->next = cur->next;
THREAD_UNLOCK();
(void)fclose(iop);
if (cur->pid < 0) {
free(cur);
return W_EXITCODE(127, 0);
}
do {
pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
} while (pid == -1 && errno == EINTR);
free(cur);
return (pid == -1 ? -1 : pstat);
}
#endif