2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1988, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma weak _pclose = pclose
2N/A#pragma weak _popen = popen
2N/A
2N/A#include "lint.h"
2N/A#include "mtlib.h"
2N/A#include "file64.h"
2N/A#include <sys/types.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <wait.h>
2N/A#include <signal.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <errno.h>
2N/A#include <thread.h>
2N/A#include <pthread.h>
2N/A#include <synch.h>
2N/A#include <spawn.h>
2N/A#include "stdiom.h"
2N/A#include "mse.h"
2N/A#include "libc.h"
2N/A
2N/A#define tst(a, b) (*mode == 'r'? (b) : (a))
2N/A#define RDR 0
2N/A#define WTR 1
2N/A
2N/Aextern int __xpg4; /* defined in _xpg4.c; 0 if not xpg4-compiled program */
2N/Aextern const char **_environ;
2N/A
2N/Astatic mutex_t popen_lock = DEFAULTMUTEX;
2N/A
2N/Atypedef struct node {
2N/A pid_t pid;
2N/A int fd;
2N/A struct node *next;
2N/A} node_t;
2N/A
2N/Astatic node_t *head = NULL;
2N/Astatic void _insert_nolock(pid_t, int, node_t *);
2N/Aextern pid_t _delete(int);
2N/Aextern int __close(int);
2N/A
2N/A/*
2N/A * Cancellation cleanup handler.
2N/A * If we were cancelled in waitpid(), create a daemon thread to
2N/A * reap our abandoned child. No other thread can do this for us.
2N/A */
2N/Astatic void
2N/Acleanup(void *arg)
2N/A{
2N/A extern const sigset_t maskset;
2N/A extern void *reapchild(void *); /* see port/stdio/system.c */
2N/A
2N/A /*
2N/A * We have been cancelled. There is no need to restore
2N/A * the original sigmask after blocking all signals because
2N/A * pthread_exit() will block all signals while we exit.
2N/A */
2N/A (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
2N/A (void) thr_create(NULL, 0, reapchild, arg, THR_DAEMON, NULL);
2N/A}
2N/A
2N/AFILE *
2N/Apopen(const char *cmd, const char *mode)
2N/A{
2N/A int p[2];
2N/A pid_t pid;
2N/A int myside;
2N/A int yourside;
2N/A int fd;
2N/A const char *shpath;
2N/A FILE *iop;
2N/A int stdio;
2N/A node_t *curr;
2N/A char *argvec[4];
2N/A node_t *node;
2N/A posix_spawnattr_t attr;
2N/A posix_spawn_file_actions_t fact;
2N/A int error;
2N/A static const char *sun_path = "/bin/sh";
2N/A static const char *xpg4_path = "/usr/xpg4/bin/sh";
2N/A static const char *shell = "sh";
2N/A static const char *sh_flg = "-c";
2N/A
2N/A /* quick check to see if fdopen() would fail */
2N/A switch (*mode) {
2N/A case 'r':
2N/A case 'w':
2N/A break;
2N/A default:
2N/A errno = EINVAL;
2N/A return (NULL);
2N/A }
2N/A if ((node = lmalloc(sizeof (node_t))) == NULL)
2N/A return (NULL);
2N/A if ((error = posix_spawnattr_init(&attr)) != 0) {
2N/A lfree(node, sizeof (node_t));
2N/A errno = error;
2N/A return (NULL);
2N/A }
2N/A if ((error = posix_spawn_file_actions_init(&fact)) != 0) {
2N/A lfree(node, sizeof (node_t));
2N/A (void) posix_spawnattr_destroy(&attr);
2N/A errno = error;
2N/A return (NULL);
2N/A }
2N/A
2N/A shpath = __xpg4? xpg4_path : sun_path;
2N/A if (access(shpath, X_OK)) /* XPG4 Requirement: */
2N/A shpath = ""; /* force child to fail immediately */
2N/A
2N/A /*
2N/A * Protect the pipe() file descriptors from other calls to popen().
2N/A */
2N/A lmutex_lock(&popen_lock);
2N/A
2N/A if (pipe(p) < 0) {
2N/A error = errno;
2N/A lmutex_unlock(&popen_lock);
2N/A lfree(node, sizeof (node_t));
2N/A (void) posix_spawnattr_destroy(&attr);
2N/A (void) posix_spawn_file_actions_destroy(&fact);
2N/A errno = error;
2N/A return (NULL);
2N/A }
2N/A
2N/A myside = tst(p[WTR], p[RDR]);
2N/A yourside = tst(p[RDR], p[WTR]);
2N/A /* myside and yourside reverse roles in child */
2N/A stdio = tst(0, 1);
2N/A
2N/A /* in the child, close all pipes from other popen's */
2N/A for (curr = head; curr != NULL && error == 0; curr = curr->next) {
2N/A /*
2N/A * These conditions may apply if a previous iob returned
2N/A * by popen() was closed with fclose() rather than pclose(),
2N/A * or if close(fileno(iob)) was called. Don't let these
2N/A * programming errors cause us to malfunction here.
2N/A */
2N/A if ((fd = curr->fd) != myside && fd != yourside &&
2N/A fcntl(fd, F_GETFD) >= 0)
2N/A error = posix_spawn_file_actions_addclose(&fact, fd);
2N/A }
2N/A if (error == 0)
2N/A error = posix_spawn_file_actions_addclose(&fact, myside);
2N/A if (yourside != stdio) {
2N/A if (error == 0)
2N/A error = posix_spawn_file_actions_adddup2(&fact,
2N/A yourside, stdio);
2N/A if (error == 0)
2N/A error = posix_spawn_file_actions_addclose(&fact,
2N/A yourside);
2N/A }
2N/A /*
2N/A * See the comments in port/stdio/system.c for why these
2N/A * non-portable posix_spawn() attributes are being used.
2N/A */
2N/A if (error == 0)
2N/A error = posix_spawnattr_setflags(&attr,
2N/A POSIX_SPAWN_RESETIDS |
2N/A POSIX_SPAWN_NOSIGCHLD_NP |
2N/A POSIX_SPAWN_WAITPID_NP |
2N/A POSIX_SPAWN_NOEXECERR_NP);
2N/A if (error) {
2N/A (void) __close(myside);
2N/A (void) __close(yourside);
2N/A lmutex_unlock(&popen_lock);
2N/A lfree(node, sizeof (node_t));
2N/A (void) posix_spawnattr_destroy(&attr);
2N/A (void) posix_spawn_file_actions_destroy(&fact);
2N/A errno = error;
2N/A return (NULL);
2N/A }
2N/A argvec[0] = (char *)shell;
2N/A argvec[1] = (char *)sh_flg;
2N/A argvec[2] = (char *)cmd;
2N/A argvec[3] = NULL;
2N/A error = posix_spawn(&pid, shpath, &fact, &attr,
2N/A (char *const *)argvec, (char *const *)_environ);
2N/A (void) posix_spawnattr_destroy(&attr);
2N/A (void) posix_spawn_file_actions_destroy(&fact);
2N/A (void) __close(yourside);
2N/A if (error) {
2N/A (void) __close(myside);
2N/A lmutex_unlock(&popen_lock);
2N/A lfree(node, sizeof (node_t));
2N/A errno = error;
2N/A return (NULL);
2N/A }
2N/A _insert_nolock(pid, myside, node);
2N/A
2N/A lmutex_unlock(&popen_lock);
2N/A
2N/A /*
2N/A * This goes here because we cannot call fdopen()
2N/A * while holding a lock acquired with lmutex_lock().
2N/A * If this fails, we are in trouble because the child
2N/A * process has already been spawned. Hopefully
2N/A * the child will die due to receiving SIGPIPE.
2N/A */
2N/A if ((iop = fdopen(myside, mode)) == NULL) {
2N/A error = errno;
2N/A (void) __close(myside);
2N/A (void) _delete(myside);
2N/A errno = error;
2N/A return (NULL);
2N/A }
2N/A _SET_ORIENTATION_BYTE(iop);
2N/A
2N/A return (iop);
2N/A}
2N/A
2N/A/*
2N/A * pclose() is a cancellation point.
2N/A */
2N/Aint
2N/Apclose(FILE *ptr)
2N/A{
2N/A pid_t pid;
2N/A int status;
2N/A
2N/A pid = _delete(fileno(ptr));
2N/A
2N/A /* mark this pipe closed */
2N/A (void) fclose(ptr);
2N/A
2N/A if (pid <= 0) {
2N/A errno = ECHILD;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * waitpid() is a cancellation point.
2N/A * This causes pclose() to be a cancellation point.
2N/A *
2N/A * If we have already been cancelled (pclose() was called from
2N/A * a cancellation cleanup handler), attempt to reap the process
2N/A * w/o waiting, and if that fails just call cleanup(pid).
2N/A */
2N/A
2N/A if (_thrp_cancelled()) {
2N/A /* waitpid(..., WNOHANG) is not a cancellation point */
2N/A if (waitpid(pid, &status, WNOHANG) == pid)
2N/A return (status);
2N/A cleanup((void *)(uintptr_t)pid);
2N/A errno = ECHILD;
2N/A return (-1);
2N/A }
2N/A
2N/A pthread_cleanup_push(cleanup, (void *)(uintptr_t)pid);
2N/A while (waitpid(pid, &status, 0) < 0) {
2N/A if (errno != EINTR) {
2N/A status = -1;
2N/A break;
2N/A }
2N/A }
2N/A pthread_cleanup_pop(0);
2N/A
2N/A return (status);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/A_insert_nolock(pid_t pid, int fd, node_t *new)
2N/A{
2N/A node_t *prev;
2N/A node_t *curr;
2N/A
2N/A for (prev = curr = head; curr != NULL; curr = curr->next) {
2N/A /*
2N/A * curr->fd can equal fd if a previous iob returned by
2N/A * popen() was closed with fclose() rather than pclose(),
2N/A * or if close(fileno(iob)) was called. Don't let these
2N/A * programming errors cause us to malfunction here.
2N/A */
2N/A if (curr->fd == fd) {
2N/A /* make a lame attempt to reap the forgotten child */
2N/A (void) waitpid(curr->pid, NULL, WNOHANG);
2N/A curr->pid = pid;
2N/A lfree(new, sizeof (node_t));
2N/A return;
2N/A }
2N/A prev = curr;
2N/A }
2N/A
2N/A new->pid = pid;
2N/A new->fd = fd;
2N/A new->next = NULL;
2N/A
2N/A if (head == NULL)
2N/A head = new;
2N/A else
2N/A prev->next = new;
2N/A}
2N/A
2N/A/*
2N/A * _insert() and _delete() are used by p2open() in libgen.
2N/A */
2N/Aint
2N/A_insert(pid_t pid, int fd)
2N/A{
2N/A node_t *node;
2N/A
2N/A if ((node = lmalloc(sizeof (node_t))) == NULL)
2N/A return (-1);
2N/A
2N/A lmutex_lock(&popen_lock);
2N/A _insert_nolock(pid, fd, node);
2N/A lmutex_unlock(&popen_lock);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/Apid_t
2N/A_delete(int fd)
2N/A{
2N/A node_t *prev;
2N/A node_t *curr;
2N/A pid_t pid;
2N/A
2N/A lmutex_lock(&popen_lock);
2N/A
2N/A for (prev = curr = head; curr != NULL; curr = curr->next) {
2N/A if (curr->fd == fd) {
2N/A if (curr == head)
2N/A head = curr->next;
2N/A else
2N/A prev->next = curr->next;
2N/A lmutex_unlock(&popen_lock);
2N/A pid = curr->pid;
2N/A lfree(curr, sizeof (node_t));
2N/A return (pid);
2N/A }
2N/A prev = curr;
2N/A }
2N/A
2N/A lmutex_unlock(&popen_lock);
2N/A
2N/A return (-1);
2N/A}