xref: /onnv-gate/usr/src/lib/libmail/common/popenvp.c (revision 1219:f89f56c2d9ac)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1219Sraf 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  *  These routines are based on the standard UNIX stdio popen/pclose
360Sstevel@tonic-gate  *  routines. This version takes an argv[][] argument instead of a string
370Sstevel@tonic-gate  *  to be passed to the shell. The routine execvp() is used to call the
380Sstevel@tonic-gate  *  program, hence the name popenvp() and the argument types.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *  This routine avoids an extra shell completely, along with not having
410Sstevel@tonic-gate  *  to worry about quoting conventions in strings that have spaces,
420Sstevel@tonic-gate  *  quotes, etc.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
45*1219Sraf #include "c_synonyms.h"
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <assert.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include "libmail.h"
500Sstevel@tonic-gate #include <sys/wait.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <fcntl.h>
530Sstevel@tonic-gate #include <signal.h>
540Sstevel@tonic-gate #include <errno.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #define	tst(a, b) (*mode == 'r'? (b) : (a))
570Sstevel@tonic-gate #define	RDR	0
580Sstevel@tonic-gate #define	WTR	1
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <unistd.h>
610Sstevel@tonic-gate static pid_t popen_pid[20];
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Functions calling popenvp() should ensure 'file' is non-NULL */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate FILE *
660Sstevel@tonic-gate popenvp(char *file, char **argv, char *mode, int resetid)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	int	p[2];
690Sstevel@tonic-gate 	int myside, yourside;
700Sstevel@tonic-gate 	pid_t pid;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	assert(file != NULL);
730Sstevel@tonic-gate 	if (pipe(p) < 0)
740Sstevel@tonic-gate 		return (NULL);
750Sstevel@tonic-gate 	myside = tst(p[WTR], p[RDR]);
760Sstevel@tonic-gate 	yourside = tst(p[RDR], p[WTR]);
770Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
780Sstevel@tonic-gate 		/* myside and yourside reverse roles in child */
790Sstevel@tonic-gate 		int	stdio;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		if (resetid) {
820Sstevel@tonic-gate 			(void) setgid(getgid());
830Sstevel@tonic-gate 			(void) setuid(getuid());
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 		stdio = tst(0, 1);
860Sstevel@tonic-gate 		(void) close(myside);
870Sstevel@tonic-gate 		(void) close(stdio);
880Sstevel@tonic-gate 		(void) fcntl(yourside, F_DUPFD, stdio);
890Sstevel@tonic-gate 		(void) close(yourside);
900Sstevel@tonic-gate 		(void) execvp(file, argv);
910Sstevel@tonic-gate 		(void) fprintf(stderr, "exec of \"%s\" failed: %s\n",
920Sstevel@tonic-gate 		    file, strerror(errno));
930Sstevel@tonic-gate 		(void) fflush(stderr);
940Sstevel@tonic-gate 		_exit(1);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	if (pid == (pid_t)-1)
970Sstevel@tonic-gate 		return (NULL);
980Sstevel@tonic-gate 	popen_pid[myside] = pid;
990Sstevel@tonic-gate 	(void) close(yourside);
1000Sstevel@tonic-gate 	return (fdopen(myside, mode));
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int
1040Sstevel@tonic-gate pclosevp(FILE *ptr)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	int f;
1070Sstevel@tonic-gate 	pid_t r;
1080Sstevel@tonic-gate 	int status;
1090Sstevel@tonic-gate 	void (*hstat)(int), (*istat)(int), (*qstat)(int);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	f = fileno(ptr);
1120Sstevel@tonic-gate 	(void) fclose(ptr);
1130Sstevel@tonic-gate 	istat = signal(SIGINT, SIG_IGN);
1140Sstevel@tonic-gate 	qstat = signal(SIGQUIT, SIG_IGN);
1150Sstevel@tonic-gate 	hstat = signal(SIGHUP, SIG_IGN);
1160Sstevel@tonic-gate 	do
1170Sstevel@tonic-gate 		r = wait(&status);
1180Sstevel@tonic-gate 	while (r != popen_pid[f] && r != (pid_t)-1);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (r == (pid_t)-1)
1210Sstevel@tonic-gate 		status = -1;
1220Sstevel@tonic-gate 	(void) signal(SIGINT, istat);
1230Sstevel@tonic-gate 	(void) signal(SIGQUIT, qstat);
1240Sstevel@tonic-gate 	(void) signal(SIGHUP, hstat);
1250Sstevel@tonic-gate 	return (status);
1260Sstevel@tonic-gate }
127