Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/cron/cron/popen.c
Show All 18 Lines | |||||
* | * | ||||
*/ | */ | ||||
/* this came out of the ftpd sources; it's been modified to avoid the | /* this came out of the ftpd sources; it's been modified to avoid the | ||||
* globbing stuff since we don't need it. also execvp instead of execv. | * globbing stuff since we don't need it. also execvp instead of execv. | ||||
*/ | */ | ||||
#ifndef lint | #ifndef lint | ||||
static const char rcsid[] = | |||||
"$Id: popen.c,v 1.3 1998/08/14 00:32:41 vixie Exp $"; | |||||
#if 0 | #if 0 | ||||
static char sccsid[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89"; | static const char sccsid[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89"; | ||||
#endif | #endif | ||||
static const char rcsid[] = | |||||
"$FreeBSD$"; | |||||
#endif /* not lint */ | #endif /* not lint */ | ||||
#include "cron.h" | #include "cron.h" | ||||
#include <sys/signal.h> | |||||
#include <fcntl.h> | |||||
#include <paths.h> | |||||
#if defined(SYSLOG) | |||||
# include <syslog.h> | |||||
#endif | |||||
#if defined(LOGIN_CAP) | #if defined(LOGIN_CAP) | ||||
# include <login_cap.h> | # include <login_cap.h> | ||||
#endif | #endif | ||||
#define MAX_ARGS 100 | #define MAX_ARGS 100 | ||||
#define WANT_GLOBBING 0 | #define WANT_GLOBBING 0 | ||||
/* | /* | ||||
* Special version of popen which avoids call to shell. This insures no one | * Special version of popen which avoids call to shell. This insures no one | ||||
* may create a pipe to a hidden program as a side effect of a list or dir | * may create a pipe to a hidden program as a side effect of a list or dir | ||||
* command. | * command. | ||||
*/ | */ | ||||
static PID_T *pids; | static PID_T *pids; | ||||
static int fds; | static int fds; | ||||
FILE * | FILE * | ||||
cron_popen(char *program, char *type, entry *e, PID_T *pidptr) | cron_popen(char *program, char *type, entry *e, PID_T *pidptr) | ||||
{ | { | ||||
register char *cp; | char *cp; | ||||
FILE *iop; | FILE *iop; | ||||
int argc, pdes[2]; | int argc, pdes[2]; | ||||
PID_T pid; | PID_T pid; | ||||
char *usernm; | char *usernm; | ||||
char *argv[MAX_ARGS + 1]; | char *argv[MAX_ARGS + 1]; | ||||
# if defined(LOGIN_CAP) | # if defined(LOGIN_CAP) | ||||
struct passwd *pwd; | struct passwd *pwd; | ||||
login_cap_t *lc; | login_cap_t *lc; | ||||
# endif | # endif | ||||
#if WANT_GLOBBING | #if WANT_GLOBBING | ||||
char **pop, *vv[2]; | char **pop, *vv[2]; | ||||
int gargc; | int gargc; | ||||
char *gargv[1000]; | char *gargv[1000]; | ||||
extern char **glob(), **copyblk(); | extern char **glob(), **copyblk(); | ||||
#endif | #endif | ||||
if ((*type != 'r' && *type != 'w') || type[1]) | if ((*type != 'r' && *type != 'w') || type[1] != '\0') | ||||
return(NULL); | return (NULL); | ||||
if (!pids) { | if (!pids) { | ||||
if ((fds = getdtablesize()) <= 0) | if ((fds = sysconf(_SC_OPEN_MAX)) <= 0) | ||||
return(NULL); | return (NULL); | ||||
if (!(pids = calloc(fds, sizeof(PID_T)))) | if (!(pids = calloc(fds, sizeof(PID_T)))) | ||||
return(NULL); | return (NULL); | ||||
} | } | ||||
if (pipe(pdes) < 0) | if (pipe(pdes) < 0) | ||||
return(NULL); | return (NULL); | ||||
/* break up string into pieces */ | /* break up string into pieces */ | ||||
for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL) | for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL) | ||||
if (!(argv[argc++] = strtok(cp, " \t\n"))) | if (!(argv[argc++] = strtok(cp, " \t\n"))) | ||||
break; | break; | ||||
argv[MAX_ARGS] = NULL; | argv[MAX_ARGS] = NULL; | ||||
#if WANT_GLOBBING | #if WANT_GLOBBING | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | #if WANT_GLOBBING | ||||
for (argc = 1; argv[argc] != NULL; argc++) { | for (argc = 1; argv[argc] != NULL; argc++) { | ||||
/* blkfree((char **)argv[argc]); */ | /* blkfree((char **)argv[argc]); */ | ||||
free((char *)argv[argc]); | free((char *)argv[argc]); | ||||
} | } | ||||
#endif | #endif | ||||
*pidptr = pid; | *pidptr = pid; | ||||
return(iop); | return (iop); | ||||
} | } | ||||
int | int | ||||
cron_pclose(FILE *iop) | cron_pclose(FILE *iop) | ||||
{ | { | ||||
register int fdes; | int fdes; | ||||
int omask; | int omask; | ||||
WAIT_T stat_loc; | WAIT_T stat_loc; | ||||
PID_T pid; | PID_T pid; | ||||
/* | /* | ||||
* pclose returns -1 if stream is not associated with a | * pclose returns -1 if stream is not associated with a | ||||
* `popened' command, or, if already `pclosed'. | * `popened' command, or, if already `pclosed'. | ||||
*/ | */ | ||||
if (pids == 0 || pids[fdes = fileno(iop)] == 0) | if (pids == 0 || pids[fdes = fileno(iop)] == 0) | ||||
return(-1); | return (-1); | ||||
(void)fclose(iop); | (void)fclose(iop); | ||||
omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); | omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); | ||||
while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1) | while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1) | ||||
; | ; | ||||
(void)sigsetmask(omask); | (void)sigsetmask(omask); | ||||
pids[fdes] = 0; | pids[fdes] = 0; | ||||
return (pid == -1 ? -1 : WEXITSTATUS(stat_loc)); | return (pid == -1 ? -1 : WEXITSTATUS(stat_loc)); | ||||
} | } |