2 * Copyright (c) 1988 The Regents of the University of California.
5 * This code is derived from software written by Ken Arnold and
6 * published in UNIX Review, Vol. 6, No. 8.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 /* this came out of the ftpd sources; it's been modified to avoid the
23 * globbing stuff since we don't need it. also execvp instead of execv.
28 static char sccsid[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89";
30 static const char rcsid[] =
35 #include <sys/signal.h>
41 #if defined(LOGIN_CAP)
42 # include <login_cap.h>
47 #define WANT_GLOBBING 0
50 * Special version of popen which avoids call to shell. This insures no one
51 * may create a pipe to a hidden program as a side effect of a list or dir
58 cron_popen(program, type, e)
67 char *argv[MAX_ARGS + 1];
68 # if defined(LOGIN_CAP)
76 extern char **glob(), **copyblk();
79 if ((*type != 'r' && *type != 'w') || type[1])
83 if ((fds = getdtablesize()) <= 0)
85 if (!(pids = calloc(fds, sizeof(PID_T))))
91 /* break up string into pieces */
92 for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL)
93 if (!(argv[argc++] = strtok(cp, " \t\n")))
95 argv[MAX_ARGS] = NULL;
100 for (gargc = argc = 1; argv[argc]; argc++) {
101 if (!(pop = glob(argv[argc]))) { /* globbing failed */
106 argv[argc] = (char *)pop; /* save to free later */
107 while (*pop && gargc < 1000)
108 gargv[gargc++] = *pop++;
114 switch(pid = vfork()) {
116 (void)close(pdes[0]);
117 (void)close(pdes[1]);
126 /* get new pgrp, void tty, etc.
131 /* Do not share our parent's stdin */
133 (void)open(_PATH_DEVNULL, O_RDWR);
136 dup2(pdes[1], 2); /* stderr, too! */
137 (void)close(pdes[1]);
139 (void)close(pdes[0]);
143 (void)close(pdes[0]);
145 /* Hack: stdout gets revoked */
147 (void)open(_PATH_DEVNULL, O_RDWR);
149 (void)open(_PATH_DEVNULL, O_RDWR);
150 (void)close(pdes[1]);
153 /* Set user's entire context, but skip the environment
154 * as cron provides a separate interface for this
156 usernm = env_get("LOGNAME", e->envp);
157 # if defined(LOGIN_CAP)
158 if ((pwd = getpwnam(usernm)) == NULL)
159 pwd = getpwuid(e->uid);
162 pwd->pw_gid = e->gid;
163 if (e->class != NULL)
164 lc = login_getclass(e->class);
167 setusercontext(lc, pwd, e->uid,
168 LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
171 /* fall back to the old method */
175 * Set our directory, uid and gid. Set gid
176 * first since once we set uid, we've lost
179 if (setgid(e->gid) != 0)
182 if (initgroups(usernm, e->gid) != 0)
185 if (setlogin(usernm) != 0)
187 if (setuid(e->uid) != 0)
189 /* we aren't root after this..*/
190 #if defined(LOGIN_CAP)
195 chdir(env_get("HOME", e->envp));
198 execvp(gargv[0], gargv);
200 execvp(argv[0], argv);
204 /* parent; assume fdopen can't fail... */
206 iop = fdopen(pdes[0], type);
207 (void)close(pdes[1]);
209 iop = fdopen(pdes[1], type);
210 (void)close(pdes[0]);
212 pids[fileno(iop)] = pid;
216 for (argc = 1; argv[argc] != NULL; argc++) {
217 /* blkfree((char **)argv[argc]); */
218 free((char *)argv[argc]);
234 * pclose returns -1 if stream is not associated with a
235 * `popened' command, or, if already `pclosed'.
237 if (pids == 0 || pids[fdes = fileno(iop)] == 0)
240 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
241 while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1)
243 (void)sigsetmask(omask);
245 return (pid == -1 ? -1 : WEXITSTATUS(stat_loc));