#include "globals.h"
#include "pathnames.h"
-#define TMAX(a,b) ((a)>(b)?(a):(b))
-#define TMIN(a,b) ((a)<(b)?(a):(b))
-
/* size of the event structure, not counting name */
#define EVENT_SIZE (sizeof (struct inotify_event))
*/
{
char *shell = env_get("SHELL", jobenv);
- int fd, fdmax = getdtablesize();
+ int fd, fdmax = TMIN(getdtablesize(), MAX_CLOSE_FD);
/* close all unwanted open file descriptors */
for(fd = STDERR + 1; fd < fdmax; fd++) {
#define MAX_USER_ENVS 1000 /* maximum environment variables in user's crontab */
#define MAX_USER_ENTRIES 1000 /* maximum crontab entries in user's crontab */
#define MAX_GARBAGE 32768 /* max num of chars of comments and whitespaces between entries */
+#define MAX_CLOSE_FD 10000 /* max fd num to close when spawning a child process */
/* NOTE: these correspond to DebugFlagNames,
* defined below.
#define LAST_DOW 7
#define DOW_COUNT (LAST_DOW - FIRST_DOW + 1)
+#define TMAX(a,b) ((a)>(b)?(a):(b))
+#define TMIN(a,b) ((a)<(b)?(a):(b))
+
/*
* Because crontab/at files may be owned by their respective users we
* take extreme care in opening them. If the OS lacks the O_NOFOLLOW
if (!pids) {
if ((fds = getdtablesize()) <= 0)
return (NULL);
+ if (fds > MAX_CLOSE_FD)
+ fds = MAX_CLOSE_FD; /* avoid allocating too much memory */
if (!(pids = (PID_T *) malloc((u_int) ((size_t)fds * sizeof (PID_T)))))
return (NULL);
memset((char *) pids, 0, (size_t)fds * sizeof (PID_T));
}
if (pipe(pdes) < 0)
return (NULL);
+ if (pdes[0] >= fds || pdes[1] >= fds) {
+ (void) close(pdes[0]);
+ (void) close(pdes[1]);
+ return NULL;
+ }
/* break up string into pieces */
for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL)
}
/* parent; assume fdopen can't fail... */
if (*type == 'r') {
+ fd = pdes[0];
iop = fdopen(pdes[0], type);
(void) close(pdes[1]);
}
else {
+ fd = pdes[1];
iop = fdopen(pdes[1], type);
(void) close(pdes[0]);
}
- pids[fileno(iop)] = pid;
+ pids[fd] = pid;
pfree:
return (iop);
* pclose returns -1 if stream is not associated with a
* `popened' command, or, if already `pclosed'.
*/
- if (pids == NULL || pids[fdes = fileno(iop)] == 0L)
+ fdes = fileno(iop);
+ if (pids == NULL || fdes >= fds || pids[fdes] == 0L)
return (-1);
(void) fclose(iop);