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 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * These routines are based on the standard UNIX stdio popen/pclose
2N/A * routines. This version takes an argv[][] argument instead of a string
2N/A * to be passed to the shell. The routine execvp() is used to call the
2N/A * program, hence the name popenvp() and the argument types.
2N/A *
2N/A * This routine avoids an extra shell completely, along with not having
2N/A * to worry about quoting conventions in strings that have spaces,
2N/A * quotes, etc.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <assert.h>
2N/A#include <string.h>
2N/A#include "libmail.h"
2N/A#include <sys/wait.h>
2N/A#include <stdio.h>
2N/A#include <fcntl.h>
2N/A#include <signal.h>
2N/A#include <errno.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/A#include <unistd.h>
2N/Astatic pid_t popen_pid[20];
2N/A
2N/A/* Functions calling popenvp() should ensure 'file' is non-NULL */
2N/A
2N/AFILE *
2N/Apopenvp(char *file, char **argv, char *mode, int resetid)
2N/A{
2N/A int p[2];
2N/A int myside, yourside;
2N/A pid_t pid;
2N/A
2N/A assert(file != NULL);
2N/A if (pipe(p) < 0)
2N/A return (NULL);
2N/A myside = tst(p[WTR], p[RDR]);
2N/A yourside = tst(p[RDR], p[WTR]);
2N/A if ((pid = fork()) == 0) {
2N/A /* myside and yourside reverse roles in child */
2N/A int stdio;
2N/A
2N/A if (resetid) {
2N/A (void) setgid(getgid());
2N/A (void) setuid(getuid());
2N/A }
2N/A stdio = tst(0, 1);
2N/A (void) close(myside);
2N/A (void) close(stdio);
2N/A (void) fcntl(yourside, F_DUPFD, stdio);
2N/A (void) close(yourside);
2N/A (void) execvp(file, argv);
2N/A (void) fprintf(stderr, "exec of \"%s\" failed: %s\n",
2N/A file, strerror(errno));
2N/A (void) fflush(stderr);
2N/A _exit(1);
2N/A }
2N/A if (pid == (pid_t)-1)
2N/A return (NULL);
2N/A popen_pid[myside] = pid;
2N/A (void) close(yourside);
2N/A return (fdopen(myside, mode));
2N/A}
2N/A
2N/Aint
2N/Apclosevp(FILE *ptr)
2N/A{
2N/A int f;
2N/A pid_t r;
2N/A int status;
2N/A void (*hstat)(int), (*istat)(int), (*qstat)(int);
2N/A
2N/A f = fileno(ptr);
2N/A (void) fclose(ptr);
2N/A istat = signal(SIGINT, SIG_IGN);
2N/A qstat = signal(SIGQUIT, SIG_IGN);
2N/A hstat = signal(SIGHUP, SIG_IGN);
2N/A do {
2N/A r = wait(&status);
2N/A } while (r != popen_pid[f] && r != (pid_t)-1);
2N/A
2N/A if (r == (pid_t)-1)
2N/A status = -1;
2N/A (void) signal(SIGINT, istat);
2N/A (void) signal(SIGQUIT, qstat);
2N/A (void) signal(SIGHUP, hstat);
2N/A return (status);
2N/A}