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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
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.
38 * This routine avoids an extra shell completely, along with not having
39 * to worry about quoting conventions in strings that have spaces,
43 #include <sys/types.h>
53 #define tst(a, b) (*mode == 'r'? (b) : (a))
58 static pid_t popen_pid
[20];
60 /* Functions calling popenvp() should ensure 'file' is non-NULL */
63 popenvp(char *file
, char **argv
, char *mode
, int resetid
)
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 */
79 (void) setgid(getgid());
80 (void) setuid(getuid());
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
);
95 popen_pid
[myside
] = pid
;
96 (void) close(yourside
);
97 return (fdopen(myside
, mode
));
106 void (*hstat
)(int), (*istat
)(int), (*qstat
)(int);
110 istat
= signal(SIGINT
, SIG_IGN
);
111 qstat
= signal(SIGQUIT
, SIG_IGN
);
112 hstat
= signal(SIGHUP
, SIG_IGN
);
115 } while (r
!= popen_pid
[f
] && r
!= (pid_t
)-1);
119 (void) signal(SIGINT
, istat
);
120 (void) signal(SIGQUIT
, qstat
);
121 (void) signal(SIGHUP
, hstat
);