xref: /onnv-gate/usr/src/lib/libc/port/stdio/popen.c (revision 6812)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51846Scraigm  * Common Development and Distribution License (the "License").
61846Scraigm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211846Scraigm 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
301846Scraigm #pragma ident	"%Z%%M%	%I%	%E% SMI"
311846Scraigm 
32*6812Sraf #pragma weak _pclose = pclose
33*6812Sraf #pragma weak _popen = popen
340Sstevel@tonic-gate 
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include "mtlib.h"
370Sstevel@tonic-gate #include "file64.h"
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <wait.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <fcntl.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <thread.h>
473235Sraf #include <pthread.h>
480Sstevel@tonic-gate #include <synch.h>
490Sstevel@tonic-gate #include <spawn.h>
500Sstevel@tonic-gate #include "stdiom.h"
510Sstevel@tonic-gate #include "mse.h"
520Sstevel@tonic-gate #include "libc.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	tst(a, b) (*mode == 'r'? (b) : (a))
550Sstevel@tonic-gate #define	RDR	0
560Sstevel@tonic-gate #define	WTR	1
570Sstevel@tonic-gate 
580Sstevel@tonic-gate extern	int __xpg4;	/* defined in _xpg4.c; 0 if not xpg4-compiled program */
590Sstevel@tonic-gate extern const char **environ;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static mutex_t popen_lock = DEFAULTMUTEX;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate typedef struct node {
640Sstevel@tonic-gate 	pid_t	pid;
650Sstevel@tonic-gate 	int	fd;
660Sstevel@tonic-gate 	struct	node	*next;
670Sstevel@tonic-gate } node_t;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static	node_t  *head = NULL;
703235Sraf static	void	_insert_nolock(pid_t, int, node_t *);
710Sstevel@tonic-gate 
723235Sraf /*
733235Sraf  * Cancellation cleanup handler.
743235Sraf  * If we were cancelled in waitpid(), create a daemon thread to
753235Sraf  * reap our abandoned child.  No other thread can do this for us.
763235Sraf  */
773235Sraf static void
783235Sraf cleanup(void *arg)
793235Sraf {
803235Sraf 	extern const sigset_t maskset;
813235Sraf 	extern void *reapchild(void *);		/* see port/stdio/system.c */
823235Sraf 
835891Sraf 	/*
845891Sraf 	 * We have been cancelled.  There is no need to restore
855891Sraf 	 * the original sigmask after blocking all signals because
865891Sraf 	 * pthread_exit() will block all signals while we exit.
875891Sraf 	 */
883235Sraf 	(void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
893235Sraf 	(void) thr_create(NULL, 0, reapchild, arg, THR_DAEMON, NULL);
903235Sraf }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate FILE *
930Sstevel@tonic-gate popen(const char *cmd, const char *mode)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	int	p[2];
960Sstevel@tonic-gate 	pid_t	pid;
973235Sraf 	int	myside;
983235Sraf 	int	yourside;
993235Sraf 	int	fd;
1000Sstevel@tonic-gate 	const char *shpath;
1010Sstevel@tonic-gate 	FILE	*iop;
1020Sstevel@tonic-gate 	int	stdio;
1030Sstevel@tonic-gate 	node_t	*curr;
1040Sstevel@tonic-gate 	char	*argvec[4];
1053235Sraf 	node_t	*node;
1063235Sraf 	posix_spawnattr_t attr;
1070Sstevel@tonic-gate 	posix_spawn_file_actions_t fact;
1080Sstevel@tonic-gate 	int	error;
1090Sstevel@tonic-gate 	static const char *sun_path = "/bin/sh";
1100Sstevel@tonic-gate 	static const char *xpg4_path = "/usr/xpg4/bin/sh";
1110Sstevel@tonic-gate 	static const char *shell = "sh";
1120Sstevel@tonic-gate 	static const char *sh_flg = "-c";
1130Sstevel@tonic-gate 
1143235Sraf 	if ((node = lmalloc(sizeof (node_t))) == NULL)
1153235Sraf 		return (NULL);
1163235Sraf 	if ((error = posix_spawnattr_init(&attr)) != 0) {
1173235Sraf 		lfree(node, sizeof (node_t));
1183235Sraf 		errno = error;
1190Sstevel@tonic-gate 		return (NULL);
1203235Sraf 	}
1213235Sraf 	if ((error = posix_spawn_file_actions_init(&fact)) != 0) {
1223235Sraf 		lfree(node, sizeof (node_t));
1233235Sraf 		(void) posix_spawnattr_destroy(&attr);
1243235Sraf 		errno = error;
1253235Sraf 		return (NULL);
1263235Sraf 	}
1273235Sraf 	if (pipe(p) < 0) {
1283235Sraf 		error = errno;
1293235Sraf 		lfree(node, sizeof (node_t));
1303235Sraf 		(void) posix_spawnattr_destroy(&attr);
1313235Sraf 		(void) posix_spawn_file_actions_destroy(&fact);
1323235Sraf 		errno = error;
1333235Sraf 		return (NULL);
1343235Sraf 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	shpath = __xpg4? xpg4_path : sun_path;
1370Sstevel@tonic-gate 	if (access(shpath, X_OK))	/* XPG4 Requirement: */
1380Sstevel@tonic-gate 		shpath = "";		/* force child to fail immediately */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	myside = tst(p[WTR], p[RDR]);
1410Sstevel@tonic-gate 	yourside = tst(p[RDR], p[WTR]);
1420Sstevel@tonic-gate 	/* myside and yourside reverse roles in child */
1430Sstevel@tonic-gate 	stdio = tst(0, 1);
1440Sstevel@tonic-gate 
1451846Scraigm 	/* This will fail more quickly if we run out of fds */
1461846Scraigm 	if ((iop = fdopen(myside, mode)) == NULL) {
1473235Sraf 		error = errno;
1483235Sraf 		lfree(node, sizeof (node_t));
1493235Sraf 		(void) posix_spawnattr_destroy(&attr);
1503235Sraf 		(void) posix_spawn_file_actions_destroy(&fact);
1511846Scraigm 		(void) close(yourside);
1521846Scraigm 		(void) close(myside);
1533235Sraf 		errno = error;
1541846Scraigm 		return (NULL);
1551846Scraigm 	}
1561846Scraigm 
1570Sstevel@tonic-gate 	lmutex_lock(&popen_lock);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* in the child, close all pipes from other popen's */
1603235Sraf 	for (curr = head; curr != NULL && error == 0; curr = curr->next) {
1613235Sraf 		/*
1623235Sraf 		 * These conditions may apply if a previous iob returned
1633235Sraf 		 * by popen() was closed with fclose() rather than pclose(),
1645891Sraf 		 * or if close(fileno(iob)) was called.  Don't let these
1655891Sraf 		 * programming errors cause us to malfunction here.
1663235Sraf 		 */
1673235Sraf 		if ((fd = curr->fd) != myside && fd != yourside &&
1683235Sraf 		    fcntl(fd, F_GETFD) >= 0)
1693235Sraf 			error = posix_spawn_file_actions_addclose(&fact, fd);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	if (error == 0)
1720Sstevel@tonic-gate 		error =  posix_spawn_file_actions_addclose(&fact, myside);
1730Sstevel@tonic-gate 	if (yourside != stdio) {
1740Sstevel@tonic-gate 		if (error == 0)
1750Sstevel@tonic-gate 			error = posix_spawn_file_actions_adddup2(&fact,
1765891Sraf 			    yourside, stdio);
1770Sstevel@tonic-gate 		if (error == 0)
1780Sstevel@tonic-gate 			error = posix_spawn_file_actions_addclose(&fact,
1795891Sraf 			    yourside);
1800Sstevel@tonic-gate 	}
1813235Sraf 	if (error == 0)
1823235Sraf 		error = posix_spawnattr_setflags(&attr,
1833235Sraf 		    POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP);
1840Sstevel@tonic-gate 	if (error) {
1850Sstevel@tonic-gate 		lmutex_unlock(&popen_lock);
1863235Sraf 		lfree(node, sizeof (node_t));
1873235Sraf 		(void) posix_spawnattr_destroy(&attr);
1880Sstevel@tonic-gate 		(void) posix_spawn_file_actions_destroy(&fact);
1891846Scraigm 		(void) fclose(iop);
1900Sstevel@tonic-gate 		(void) close(yourside);
1910Sstevel@tonic-gate 		errno = error;
1920Sstevel@tonic-gate 		return (NULL);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	argvec[0] = (char *)shell;
1950Sstevel@tonic-gate 	argvec[1] = (char *)sh_flg;
1960Sstevel@tonic-gate 	argvec[2] = (char *)cmd;
1970Sstevel@tonic-gate 	argvec[3] = NULL;
1983235Sraf 	error = posix_spawn(&pid, shpath, &fact, &attr,
1995891Sraf 	    (char *const *)argvec, (char *const *)environ);
2003235Sraf 	(void) posix_spawnattr_destroy(&attr);
2010Sstevel@tonic-gate 	(void) posix_spawn_file_actions_destroy(&fact);
2020Sstevel@tonic-gate 	(void) close(yourside);
2033235Sraf 	if (error) {
2040Sstevel@tonic-gate 		lmutex_unlock(&popen_lock);
2053235Sraf 		lfree(node, sizeof (node_t));
2061846Scraigm 		(void) fclose(iop);
2073235Sraf 		errno = error;
2080Sstevel@tonic-gate 		return (NULL);
2090Sstevel@tonic-gate 	}
2103235Sraf 	_insert_nolock(pid, myside, node);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	lmutex_unlock(&popen_lock);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	_SET_ORIENTATION_BYTE(iop);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	return (iop);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2195891Sraf /*
2205891Sraf  * pclose() is a cancellation point.
2215891Sraf  */
2220Sstevel@tonic-gate int
2230Sstevel@tonic-gate pclose(FILE *ptr)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	pid_t	pid;
2260Sstevel@tonic-gate 	int status;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	pid = _delete(fileno(ptr));
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/* mark this pipe closed */
2310Sstevel@tonic-gate 	(void) fclose(ptr);
2320Sstevel@tonic-gate 
2333235Sraf 	if (pid <= 0) {
2343235Sraf 		errno = ECHILD;
2350Sstevel@tonic-gate 		return (-1);
2363235Sraf 	}
2370Sstevel@tonic-gate 
2383235Sraf 	/*
2395891Sraf 	 * waitpid() is a cancellation point.
2405891Sraf 	 * This causes pclose() to be a cancellation point.
2413235Sraf 	 *
2423235Sraf 	 * If we have already been cancelled (pclose() was called from
2433235Sraf 	 * a cancellation cleanup handler), attempt to reap the process
2443235Sraf 	 * w/o waiting, and if that fails just call cleanup(pid).
2453235Sraf 	 */
2463235Sraf 
2473235Sraf 	if (_thrp_cancelled()) {
2485891Sraf 		/* waitpid(..., WNOHANG) is not a cancellation point */
2493235Sraf 		if (waitpid(pid, &status, WNOHANG) == pid)
2503235Sraf 			return (status);
2513235Sraf 		cleanup((void *)(uintptr_t)pid);
2523235Sraf 		errno = ECHILD;
2533235Sraf 		return (-1);
2543235Sraf 	}
2553235Sraf 
2563235Sraf 	pthread_cleanup_push(cleanup, (void *)(uintptr_t)pid);
2575891Sraf 	while (waitpid(pid, &status, 0) < 0) {
2580Sstevel@tonic-gate 		if (errno != EINTR) {
2590Sstevel@tonic-gate 			status = -1;
2600Sstevel@tonic-gate 			break;
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	}
2633235Sraf 	pthread_cleanup_pop(0);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	return (status);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 
2693235Sraf static void
2703235Sraf _insert_nolock(pid_t pid, int fd, node_t *new)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	node_t	*prev;
2730Sstevel@tonic-gate 	node_t	*curr;
2740Sstevel@tonic-gate 
2753235Sraf 	for (prev = curr = head; curr != NULL; curr = curr->next) {
2763235Sraf 		/*
2773235Sraf 		 * curr->fd can equal fd if a previous iob returned by
2783235Sraf 		 * popen() was closed with fclose() rather than pclose(),
2795891Sraf 		 * or if close(fileno(iob)) was called.  Don't let these
2805891Sraf 		 * programming errors cause us to malfunction here.
2813235Sraf 		 */
2823235Sraf 		if (curr->fd == fd) {
2835891Sraf 			/* make a lame attempt to reap the forgotten child */
2843235Sraf 			(void) waitpid(curr->pid, NULL, WNOHANG);
2853235Sraf 			curr->pid = pid;
2863235Sraf 			lfree(new, sizeof (node_t));
2873235Sraf 			return;
2883235Sraf 		}
2890Sstevel@tonic-gate 		prev = curr;
2903235Sraf 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	new->pid = pid;
2930Sstevel@tonic-gate 	new->fd = fd;
2940Sstevel@tonic-gate 	new->next = NULL;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (head == NULL)
2970Sstevel@tonic-gate 		head = new;
2980Sstevel@tonic-gate 	else
2990Sstevel@tonic-gate 		prev->next = new;
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * _insert() and _delete() are used by p2open() in libgen.
3040Sstevel@tonic-gate  */
3050Sstevel@tonic-gate int
3060Sstevel@tonic-gate _insert(pid_t pid, int fd)
3070Sstevel@tonic-gate {
3083235Sraf 	node_t *node;
3093235Sraf 
3103235Sraf 	if ((node = lmalloc(sizeof (node_t))) == NULL)
3113235Sraf 		return (-1);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	lmutex_lock(&popen_lock);
3143235Sraf 	_insert_nolock(pid, fd, node);
3150Sstevel@tonic-gate 	lmutex_unlock(&popen_lock);
3160Sstevel@tonic-gate 
3173235Sraf 	return (0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate pid_t
3220Sstevel@tonic-gate _delete(int fd)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	node_t	*prev;
3250Sstevel@tonic-gate 	node_t	*curr;
3260Sstevel@tonic-gate 	pid_t	pid;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	lmutex_lock(&popen_lock);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	for (prev = curr = head; curr != NULL; curr = curr->next) {
3310Sstevel@tonic-gate 		if (curr->fd == fd) {
3320Sstevel@tonic-gate 			if (curr == head)
3330Sstevel@tonic-gate 				head = curr->next;
3340Sstevel@tonic-gate 			else
3350Sstevel@tonic-gate 				prev->next = curr->next;
3363235Sraf 			lmutex_unlock(&popen_lock);
3370Sstevel@tonic-gate 			pid = curr->pid;
3380Sstevel@tonic-gate 			lfree(curr, sizeof (node_t));
3390Sstevel@tonic-gate 			return (pid);
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 		prev = curr;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	lmutex_unlock(&popen_lock);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	return (-1);
3470Sstevel@tonic-gate }
348