No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / cron / popen.c
blob039d494c77324b1e092a8c06850c00b8f0a8d0e9
1 /* $NetBSD: popen.c,v 1.10 2005/07/31 17:52:01 christos Exp $ */
3 /*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software written by Ken Arnold and
8 * published in UNIX Review, Vol. 6, No. 8.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #if 0
36 static char rcsid[] = "Id: popen.c,v 1.5 1994/01/15 20:43:43 vixie Exp";
37 static char sccsid[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89";
38 #else
39 __RCSID("$NetBSD: popen.c,v 1.10 2005/07/31 17:52:01 christos Exp $");
40 #endif
41 #endif /* not lint */
43 #include "cron.h"
44 #include <signal.h>
47 * Special version of popen which avoids call to shell. This insures noone
48 * may create a pipe to a hidden program as a side effect of a list or dir
49 * command.
51 static PID_T *pids;
52 static int fds;
54 #define MAX_ARGS 1024
56 FILE *
57 cron_popen(char *program, const char *type)
59 char *cp;
60 FILE *iop;
61 int argc, pdes[2];
62 PID_T pid;
63 char *argv[MAX_ARGS];
65 if ((*type != 'r' && *type != 'w') || type[1])
66 return(NULL);
68 if (!pids) {
69 if ((fds = getdtablesize()) <= 0)
70 return(NULL);
71 if (!(pids = (PID_T *)malloc((u_int)(fds * sizeof(PID_T)))))
72 return(NULL);
73 bzero((char *)pids, fds * sizeof(PID_T));
75 if (pipe(pdes) < 0)
76 return(NULL);
78 /* break up string into pieces */
79 for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL)
80 if (!(argv[argc++] = strtok(cp, " \t\n")))
81 break;
83 switch(pid = vfork()) {
84 case -1: /* error */
85 (void)close(pdes[0]);
86 (void)close(pdes[1]);
87 return NULL;
88 case 0: /* child */
89 if (*type == 'r') {
90 if (pdes[1] != 1) {
91 (void)dup2(pdes[1], 1);
92 (void)dup2(pdes[1], 2); /* stderr, too! */
93 (void)close(pdes[1]);
95 (void)close(pdes[0]);
96 } else {
97 if (pdes[0] != 0) {
98 (void)dup2(pdes[0], 0);
99 (void)close(pdes[0]);
101 (void)close(pdes[1]);
103 (void)execvp(argv[0], argv);
104 _exit(1);
106 /* parent; assume fdopen can't fail... */
107 if (*type == 'r') {
108 iop = fdopen(pdes[0], type);
109 (void)close(pdes[1]);
110 } else {
111 iop = fdopen(pdes[1], type);
112 (void)close(pdes[0]);
114 pids[fileno(iop)] = pid;
115 return(iop);
119 cron_pclose(FILE *iop)
121 int fdes;
122 sigset_t oset, nset;
123 WAIT_T status;
124 PID_T pid;
127 * pclose returns -1 if stream is not associated with a
128 * `popened' command, or, if already `pclosed'.
130 if (pids == 0 || pids[fdes = fileno(iop)] == 0)
131 return -1;
132 (void)fclose(iop);
134 sigemptyset(&nset);
135 sigaddset(&nset, SIGINT);
136 sigaddset(&nset, SIGQUIT);
137 sigaddset(&nset, SIGHUP);
138 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
139 pid = waitpid(pids[fdes], &status, 0);
140 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
141 pids[fdes] = 0;
142 if (pid == -1)
143 return -1;
144 return WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status);