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