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
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.
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]
23 * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
45 #define sigmask(m) (1 << ((m)-1))
48 #define set2mask(setp) ((setp)->__sigbits[0])
49 #define mask2set(mask, setp) \
50 ((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
59 (void) sigprocmask(0, (sigset_t
*)0, &nset
);
60 mask2set(mask
, &nset
);
61 (void) sigprocmask(SIG_SETMASK
, &nset
, &oset
);
62 return (set2mask(&oset
));
71 (void) sigprocmask(0, (sigset_t
*)0, &nset
);
72 mask2set(mask
, &nset
);
73 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
74 return (set2mask(&oset
));
77 #define signal(s, f) sigset(s, f)
79 #define tst(a, b) (*mode == 'r'? (b) : (a))
82 #define NOFILES 20 /* just in case */
84 static pid_t
*popen_pid
;
85 static rlim_t nfiles
= 0;
88 mypopen(char *cmd
, char *mode
)
92 int myside
, remside
, i
;
96 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0)
101 if (popen_pid
== NULL
) {
102 popen_pid
= (pid_t
*)malloc((unsigned)nfiles
*
103 sizeof (*popen_pid
));
104 if (popen_pid
== NULL
)
106 for (i
= 0; i
< nfiles
; i
++)
107 popen_pid
[i
] = (pid_t
)-1;
111 myside
= tst(p
[WTR
], p
[RDR
]);
112 remside
= tst(p
[RDR
], p
[WTR
]);
113 if ((pid
= vfork()) == 0) {
114 /* myside and remside reverse roles in child */
115 (void) close(myside
);
116 if (remside
!= tst(0, 1)) {
117 (void) dup2(remside
, tst(0, 1));
118 (void) close(remside
);
120 execl("/bin/sh", "sh", "-c", cmd
, NULL
);
123 if (pid
== (pid_t
)-1) {
124 (void) close(myside
);
125 (void) close(remside
);
128 popen_pid
[myside
] = pid
;
129 (void) close(remside
);
130 return (fdopen(myside
, mode
));
150 child
= popen_pid
[fileno(ptr
)];
151 popen_pid
[fileno(ptr
)] = (pid_t
)-1;
153 if (child
== (pid_t
)-1)
155 istat
= signal(SIGINT
, pabort
);
156 omask
= sigblock(sigmask(SIGQUIT
)|sigmask(SIGHUP
));
157 while ((pid
= wait(&status
)) != child
&& pid
!= (pid_t
)-1)
159 (void) sigsetmask(omask
);
160 (void) signal(SIGINT
, istat
);
161 return (pid
== (pid_t
)-1 ? -1 : 0);