xref: /titanic_50/usr/src/lib/libmail/common/popenvp.c (revision eab227978ccdaa5a7cc9fd92ace768915dae3a2b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *  These routines are based on the standard UNIX stdio popen/pclose
34  *  routines. This version takes an argv[][] argument instead of a string
35  *  to be passed to the shell. The routine execvp() is used to call the
36  *  program, hence the name popenvp() and the argument types.
37  *
38  *  This routine avoids an extra shell completely, along with not having
39  *  to worry about quoting conventions in strings that have spaces,
40  *  quotes, etc.
41  */
42 
43 #include <sys/types.h>
44 #include <assert.h>
45 #include <string.h>
46 #include "libmail.h"
47 #include <sys/wait.h>
48 #include <stdio.h>
49 #include <fcntl.h>
50 #include <signal.h>
51 #include <errno.h>
52 
53 #define	tst(a, b) (*mode == 'r'? (b) : (a))
54 #define	RDR	0
55 #define	WTR	1
56 
57 #include <unistd.h>
58 static pid_t popen_pid[20];
59 
60 /* Functions calling popenvp() should ensure 'file' is non-NULL */
61 
62 FILE *
63 popenvp(char *file, char **argv, char *mode, int resetid)
64 {
65 	int	p[2];
66 	int myside, yourside;
67 	pid_t pid;
68 
69 	assert(file != NULL);
70 	if (pipe(p) < 0)
71 		return (NULL);
72 	myside = tst(p[WTR], p[RDR]);
73 	yourside = tst(p[RDR], p[WTR]);
74 	if ((pid = fork()) == 0) {
75 		/* myside and yourside reverse roles in child */
76 		int	stdio;
77 
78 		if (resetid) {
79 			(void) setgid(getgid());
80 			(void) setuid(getuid());
81 		}
82 		stdio = tst(0, 1);
83 		(void) close(myside);
84 		(void) close(stdio);
85 		(void) fcntl(yourside, F_DUPFD, stdio);
86 		(void) close(yourside);
87 		(void) execvp(file, argv);
88 		(void) fprintf(stderr, "exec of \"%s\" failed: %s\n",
89 		    file, strerror(errno));
90 		(void) fflush(stderr);
91 		_exit(1);
92 	}
93 	if (pid == (pid_t)-1)
94 		return (NULL);
95 	popen_pid[myside] = pid;
96 	(void) close(yourside);
97 	return (fdopen(myside, mode));
98 }
99 
100 int
101 pclosevp(FILE *ptr)
102 {
103 	int f;
104 	pid_t r;
105 	int status;
106 	void (*hstat)(int), (*istat)(int), (*qstat)(int);
107 
108 	f = fileno(ptr);
109 	(void) fclose(ptr);
110 	istat = signal(SIGINT, SIG_IGN);
111 	qstat = signal(SIGQUIT, SIG_IGN);
112 	hstat = signal(SIGHUP, SIG_IGN);
113 	do {
114 		r = wait(&status);
115 	} while (r != popen_pid[f] && r != (pid_t)-1);
116 
117 	if (r == (pid_t)-1)
118 		status = -1;
119 	(void) signal(SIGINT, istat);
120 	(void) signal(SIGQUIT, qstat);
121 	(void) signal(SIGHUP, hstat);
122 	return (status);
123 }
124