Home | History | Annotate | Line # | Download | only in atrun
atrun.c revision 1.12
      1  1.12       dsl /*	$NetBSD: atrun.c,v 1.12 2003/03/03 17:14:36 dsl Exp $	*/
      2   1.3   thorpej 
      3   1.1       cgd /*
      4   1.5  christos  *  atrun.c - run jobs queued by at; run with root privileges.
      5   1.5  christos  *  Copyright (C) 1993, 1994 Thomas Koenig
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. The name of the author(s) may not be used to endorse or promote
     13   1.1       cgd  *    products derived from this software without specific prior written
     14   1.1       cgd  *    permission.
     15   1.1       cgd  *
     16   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
     17   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.5  christos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1       cgd  * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1       cgd  */
     27   1.1       cgd 
     28   1.1       cgd /* System Headers */
     29   1.4       mrg #include <sys/types.h>
     30   1.1       cgd #include <sys/wait.h>
     31  1.10  christos 
     32   1.5  christos #include <ctype.h>
     33  1.10  christos #include <errno.h>
     34   1.1       cgd #include <dirent.h>
     35   1.6    kleink #include <fcntl.h>
     36   1.1       cgd #include <stdio.h>
     37   1.1       cgd #include <stdlib.h>
     38   1.1       cgd #include <string.h>
     39  1.10  christos #include <limits.h>
     40   1.1       cgd #include <time.h>
     41   1.1       cgd #include <unistd.h>
     42   1.1       cgd #include <syslog.h>
     43  1.10  christos #include <pwd.h>
     44  1.10  christos #include <err.h>
     45   1.1       cgd #include <paths.h>
     46   1.1       cgd 
     47   1.1       cgd /* Local headers */
     48   1.1       cgd 
     49   1.1       cgd #define MAIN
     50   1.1       cgd #include "privs.h"
     51   1.1       cgd #include "pathnames.h"
     52   1.1       cgd #include "atrun.h"
     53   1.1       cgd 
     54   1.1       cgd /* File scope variables */
     55   1.1       cgd 
     56   1.1       cgd static char *namep;
     57   1.5  christos #if 0
     58   1.5  christos static char rcsid[] = "$OpenBSD: atrun.c,v 1.7 1997/09/08 22:12:10 millert Exp $";
     59   1.5  christos #else
     60  1.12       dsl __RCSID("$NetBSD: atrun.c,v 1.12 2003/03/03 17:14:36 dsl Exp $");
     61   1.4       mrg #endif
     62   1.1       cgd 
     63   1.5  christos static int debug = 0;
     64   1.5  christos 
     65   1.1       cgd /* Local functions */
     66   1.5  christos static void perr __P((const char *));
     67   1.5  christos static void perr2 __P((char *, char *));
     68   1.5  christos static int write_string __P((int, const char *));
     69   1.5  christos static void run_file __P((const char *, uid_t, gid_t));
     70   1.5  christos static void become_user __P((struct passwd *, uid_t));
     71   1.5  christos 
     72   1.5  christos int main __P((int, char *[]));
     73   1.5  christos 
     74   1.1       cgd static void
     75   1.1       cgd perr(a)
     76   1.1       cgd 	const char *a;
     77   1.1       cgd {
     78   1.5  christos 	if (debug)
     79  1.11  christos 		warn("%s", a);
     80   1.5  christos 	else
     81   1.5  christos 		syslog(LOG_ERR, "%s: %m", a);
     82   1.5  christos 
     83   1.5  christos 	exit(EXIT_FAILURE);
     84   1.5  christos }
     85   1.5  christos 
     86   1.5  christos static void
     87   1.5  christos perr2(a, b)
     88   1.5  christos 	char *a, *b;
     89   1.5  christos {
     90   1.5  christos 	if (debug) {
     91  1.10  christos 		warn("%s%s", a, b);
     92   1.5  christos 	} else
     93   1.5  christos 		syslog(LOG_ERR, "%s%s: %m", a, b);
     94   1.5  christos 
     95   1.1       cgd 	exit(EXIT_FAILURE);
     96   1.1       cgd }
     97   1.1       cgd 
     98   1.1       cgd static int
     99   1.1       cgd write_string(fd, a)
    100   1.1       cgd 	int fd;
    101   1.1       cgd 	const char *a;
    102   1.1       cgd {
    103   1.5  christos 	return(write(fd, a, strlen(a)));
    104   1.5  christos }
    105   1.5  christos 
    106   1.5  christos static void
    107   1.5  christos become_user(pentry, uid)
    108   1.5  christos 	struct passwd *pentry;
    109   1.5  christos 	uid_t uid;
    110   1.5  christos {
    111   1.5  christos 	if (initgroups(pentry->pw_name, pentry->pw_gid) < 0)
    112   1.5  christos 		perr("Cannot init group list");
    113   1.5  christos 
    114   1.5  christos 	if (setegid(pentry->pw_gid) < 0 || setgid(pentry->pw_gid) < 0)
    115   1.5  christos 		perr("Cannot change primary group");
    116  1.12       dsl 
    117  1.12       dsl 	if (setsid() < 0)
    118  1.12       dsl 		perr("Cannot create a session");
    119   1.5  christos 
    120   1.5  christos 	if (setlogin(pentry->pw_name) < 0)
    121   1.5  christos 		perr("Cannot set login name");
    122   1.5  christos 
    123   1.5  christos 	if (setuid(uid) < 0 || seteuid(uid) < 0)
    124   1.5  christos 		perr("Cannot set user id");
    125   1.5  christos 
    126   1.5  christos 	if (chdir(pentry->pw_dir) < 0)
    127   1.5  christos 		chdir("/");
    128   1.1       cgd }
    129   1.1       cgd 
    130   1.1       cgd static void
    131   1.5  christos run_file(filename, uid, gid)
    132   1.1       cgd 	const char *filename;
    133   1.1       cgd 	uid_t uid;
    134   1.5  christos 	gid_t gid;
    135   1.1       cgd {
    136   1.1       cgd 	/*
    137   1.1       cgd 	 * Run a file by by spawning off a process which redirects I/O,
    138   1.1       cgd 	 * spawns a subshell, then waits for it to complete and spawns another
    139   1.1       cgd 	 * process to send mail to the user.
    140   1.1       cgd 	 */
    141   1.1       cgd 	pid_t pid;
    142   1.1       cgd 	int fd_out, fd_in;
    143   1.1       cgd 	int queue;
    144   1.8    simonb 	char mailbuf[LOGIN_NAME_MAX], fmt[49];
    145   1.1       cgd 	char *mailname = NULL;
    146   1.1       cgd 	FILE *stream;
    147   1.1       cgd 	int send_mail = 0;
    148   1.5  christos 	struct stat buf, lbuf;
    149   1.1       cgd 	off_t size;
    150   1.1       cgd 	struct passwd *pentry;
    151   1.1       cgd 	int fflags;
    152   1.9    martin 	uint nuid;
    153   1.9    martin 	uint ngid;
    154   1.5  christos 
    155   1.5  christos 	PRIV_START
    156   1.5  christos 
    157   1.5  christos 	if (chmod(filename, S_IRUSR) != 0)
    158   1.5  christos 		perr("Cannot change file permissions");
    159   1.5  christos 
    160   1.5  christos 	PRIV_END
    161   1.1       cgd 
    162   1.1       cgd 	pid = fork();
    163   1.1       cgd 	if (pid == -1)
    164   1.1       cgd 		perr("Cannot fork");
    165   1.5  christos 	else if (pid != 0)
    166   1.1       cgd 		return;
    167   1.1       cgd 
    168   1.1       cgd 	/*
    169   1.1       cgd 	 * Let's see who we mail to.  Hopefully, we can read it from the
    170   1.1       cgd 	 * command file; if not, send it to the owner, or, failing that, to
    171   1.1       cgd 	 * root.
    172   1.1       cgd 	 */
    173   1.1       cgd 
    174   1.5  christos 	pentry = getpwuid(uid);
    175   1.5  christos 	if (pentry == NULL) {
    176   1.5  christos 		syslog(LOG_ERR,"Userid %u not found - aborting job %s",
    177   1.5  christos 		    uid, filename);
    178   1.5  christos 		exit(EXIT_FAILURE);
    179   1.5  christos 	}
    180   1.1       cgd 	PRIV_START
    181   1.1       cgd 
    182   1.5  christos 	stream = fopen(filename, "r");
    183   1.1       cgd 
    184   1.1       cgd 	PRIV_END
    185   1.1       cgd 
    186   1.5  christos 	if (pentry->pw_expire && time(NULL) >= pentry->pw_expire) {
    187   1.5  christos 		syslog(LOG_ERR, "Userid %u has expired - aborting job %s",
    188   1.5  christos 		    uid, filename);
    189   1.5  christos 		exit(EXIT_FAILURE);
    190   1.5  christos 	}
    191   1.2       cgd 
    192   1.1       cgd 	if (stream == NULL)
    193   1.1       cgd 		perr("Cannot open input file");
    194   1.1       cgd 
    195   1.1       cgd 	if ((fd_in = dup(fileno(stream))) < 0)
    196   1.1       cgd 		perr("Error duplicating input file descriptor");
    197   1.1       cgd 
    198   1.5  christos 	if (fstat(fd_in, &buf) == -1)
    199   1.5  christos 		perr("Error in fstat of input file descriptor");
    200   1.5  christos 
    201   1.5  christos 	PRIV_START
    202   1.5  christos 
    203   1.5  christos 	if (lstat(filename, &lbuf) == -1)
    204   1.5  christos 		perr("Error in lstat of input file");
    205   1.5  christos 
    206   1.5  christos 	PRIV_END
    207   1.5  christos 
    208   1.5  christos 	if (S_ISLNK(lbuf.st_mode)) {
    209   1.5  christos 		syslog(LOG_ERR, "Symbolic link encountered in job %s - aborting",
    210   1.5  christos 		    filename);
    211   1.5  christos 		exit(EXIT_FAILURE);
    212   1.5  christos 	}
    213   1.5  christos 	if ((lbuf.st_dev != buf.st_dev) || (lbuf.st_ino != buf.st_ino) ||
    214   1.5  christos 	    (lbuf.st_uid != buf.st_uid) || (lbuf.st_gid != buf.st_gid) ||
    215   1.5  christos 	    (lbuf.st_size!=buf.st_size)) {
    216   1.5  christos 		syslog(LOG_ERR, "Somebody changed files from under us for job %s - aborting", filename);
    217   1.5  christos 		exit(EXIT_FAILURE);
    218   1.5  christos 	}
    219   1.5  christos 	if (buf.st_nlink > 1) {
    220   1.5  christos 		syslog(LOG_ERR, "Somebody is trying to run a linked script for job %s",
    221   1.5  christos 		    filename);
    222   1.5  christos 		exit(EXIT_FAILURE);
    223   1.5  christos 	}
    224   1.1       cgd 	if ((fflags = fcntl(fd_in, F_GETFD)) < 0)
    225   1.1       cgd 		perr("Error in fcntl");
    226   1.1       cgd 
    227   1.5  christos 	(void)fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
    228   1.1       cgd 
    229   1.5  christos 	(void)snprintf(fmt, sizeof(fmt),
    230   1.9    martin 	    "#!/bin/sh\n# atrun uid=%%u gid=%%u\n# mail %%%ds %%d",
    231   1.8    simonb 	    LOGIN_NAME_MAX);
    232   1.5  christos 	if (fscanf(stream, fmt, &nuid, &ngid, mailbuf, &send_mail) != 4) {
    233   1.5  christos 		syslog(LOG_ERR, "File %s is in wrong format - aborting",
    234   1.5  christos 		    filename);
    235   1.5  christos 		exit(EXIT_FAILURE);
    236   1.5  christos 	}
    237   1.5  christos 	if (mailbuf[0] == '-') {
    238   1.5  christos 		syslog(LOG_ERR, "illegal mail name %s in %s", mailbuf, filename);
    239   1.5  christos 		exit(EXIT_FAILURE);
    240   1.5  christos 	}
    241   1.5  christos 	mailname = mailbuf;
    242   1.5  christos 	if (nuid != uid) {
    243   1.5  christos 		syslog(LOG_ERR, "Job %s - userid %u does not match file uid %u",
    244   1.5  christos 		    filename, nuid, uid);
    245   1.5  christos 		exit(EXIT_FAILURE);
    246   1.1       cgd 	}
    247   1.5  christos 	if (ngid != gid) {
    248   1.5  christos 		syslog(LOG_ERR, "Job %s - groupid %u does not match file gid %u",
    249   1.5  christos 		    filename, ngid, gid);
    250   1.5  christos 		exit(EXIT_FAILURE);
    251   1.5  christos 	}
    252   1.5  christos 	(void)fclose(stream);
    253   1.5  christos 
    254   1.5  christos 	PRIV_START
    255   1.5  christos 
    256   1.1       cgd 	if (chdir(_PATH_ATSPOOL) < 0)
    257   1.5  christos 		perr2("Cannot chdir to ", _PATH_ATSPOOL);
    258   1.1       cgd 
    259   1.1       cgd 	/*
    260   1.1       cgd 	 * Create a file to hold the output of the job we are  about to
    261   1.1       cgd 	 * run. Write the mail header.
    262   1.1       cgd 	 */
    263   1.5  christos 
    264   1.1       cgd 	if ((fd_out = open(filename,
    265   1.1       cgd 		    O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) < 0)
    266   1.1       cgd 		perr("Cannot create output file");
    267   1.1       cgd 
    268   1.5  christos 	PRIV_END
    269   1.5  christos 
    270   1.5  christos 	write_string(fd_out, "To: ");
    271   1.5  christos 	write_string(fd_out, mailname);
    272   1.5  christos 	write_string(fd_out, "\nSubject: Output from your job ");
    273   1.1       cgd 	write_string(fd_out, filename);
    274   1.1       cgd 	write_string(fd_out, "\n\n");
    275   1.5  christos 	if (fstat(fd_out, &buf) == -1)
    276   1.5  christos 		perr("Error in fstat of output file descriptor");
    277   1.1       cgd 	size = buf.st_size;
    278   1.1       cgd 
    279   1.5  christos 	(void)close(STDIN_FILENO);
    280   1.5  christos 	(void)close(STDOUT_FILENO);
    281   1.5  christos 	(void)close(STDERR_FILENO);
    282   1.1       cgd 
    283   1.1       cgd 	pid = fork();
    284   1.1       cgd 	if (pid < 0)
    285   1.1       cgd 		perr("Error in fork");
    286   1.1       cgd 	else if (pid == 0) {
    287   1.1       cgd 		char *nul = NULL;
    288   1.1       cgd 		char **nenvp = &nul;
    289   1.1       cgd 
    290   1.1       cgd 		/*
    291   1.1       cgd 		 * Set up things for the child; we want standard input from
    292   1.1       cgd 		 * the input file, and standard output and error sent to
    293   1.1       cgd 		 * our output file.
    294   1.1       cgd 		 */
    295   1.1       cgd 		if (lseek(fd_in, (off_t) 0, SEEK_SET) < 0)
    296   1.1       cgd 			perr("Error in lseek");
    297   1.1       cgd 
    298   1.1       cgd 		if (dup(fd_in) != STDIN_FILENO)
    299   1.1       cgd 			perr("Error in I/O redirection");
    300   1.1       cgd 
    301   1.1       cgd 		if (dup(fd_out) != STDOUT_FILENO)
    302   1.1       cgd 			perr("Error in I/O redirection");
    303   1.1       cgd 
    304   1.1       cgd 		if (dup(fd_out) != STDERR_FILENO)
    305   1.1       cgd 			perr("Error in I/O redirection");
    306   1.1       cgd 
    307   1.5  christos 		(void)close(fd_in);
    308   1.5  christos 		(void)close(fd_out);
    309   1.5  christos 
    310   1.5  christos 		PRIV_START
    311   1.5  christos 
    312   1.1       cgd 		if (chdir(_PATH_ATJOBS) < 0)
    313   1.5  christos 			perr2("Cannot chdir to ", _PATH_ATJOBS);
    314   1.1       cgd 
    315   1.1       cgd 		queue = *filename;
    316   1.1       cgd 
    317   1.5  christos 		if (queue > 'b')
    318   1.5  christos 		    nice(queue - 'b');
    319   1.1       cgd 
    320   1.5  christos 		become_user(pentry, uid);
    321   1.1       cgd 
    322   1.5  christos 		if (execle("/bin/sh", "sh", (char *)NULL, nenvp) != 0)
    323   1.5  christos 			perr("Exec failed for /bin/sh");
    324   1.2       cgd 
    325   1.5  christos 		PRIV_END
    326   1.5  christos 	}
    327   1.5  christos 	/* We're the parent.  Let's wait. */
    328   1.5  christos 	(void)close(fd_in);
    329   1.5  christos 	(void)close(fd_out);
    330   1.5  christos 	waitpid(pid, (int *)NULL, 0);
    331   1.1       cgd 
    332   1.5  christos 	/*
    333   1.5  christos 	 * Send mail.  Unlink the output file first, so it is deleted
    334   1.5  christos 	 * after the run.
    335   1.5  christos 	 */
    336   1.5  christos 	PRIV_START
    337   1.1       cgd 
    338   1.5  christos 	if (stat(filename, &buf) == -1)
    339   1.5  christos 		perr("Error in stat of output file");
    340   1.5  christos 	if (open(filename, O_RDONLY) != STDIN_FILENO)
    341   1.5  christos 		perr("Open of jobfile failed");
    342   1.1       cgd 
    343   1.5  christos 	(void)unlink(filename);
    344   1.1       cgd 
    345   1.5  christos 	PRIV_END
    346   1.1       cgd 
    347   1.1       cgd 	if ((buf.st_size != size) || send_mail) {
    348   1.1       cgd 		/* Fork off a child for sending mail */
    349   1.5  christos 
    350   1.5  christos 		PRIV_START
    351   1.5  christos 
    352   1.5  christos 		become_user(pentry, uid);
    353   1.5  christos 
    354   1.5  christos 		execl(_PATH_SENDMAIL, "sendmail", "-F", "Atrun Service",
    355   1.5  christos 		    "-odi", "-oem", "-t", (char *) NULL);
    356   1.5  christos 		perr("Exec failed for mail command");
    357   1.5  christos 
    358   1.5  christos 		PRIV_END
    359   1.1       cgd 	}
    360   1.1       cgd 	exit(EXIT_SUCCESS);
    361   1.1       cgd }
    362   1.1       cgd 
    363   1.1       cgd /* Global functions */
    364   1.1       cgd 
    365   1.1       cgd int
    366   1.1       cgd main(argc, argv)
    367   1.1       cgd 	int argc;
    368   1.1       cgd 	char *argv[];
    369   1.1       cgd {
    370   1.1       cgd 	/*
    371   1.1       cgd 	 * Browse through  _PATH_ATJOBS, checking all the jobfiles wether
    372   1.1       cgd 	 * they should be executed and or deleted. The queue is coded into
    373   1.1       cgd 	 * the first byte of the job filename, the date (in minutes since
    374   1.1       cgd 	 * Eon) as a hex number in the following eight bytes, followed by
    375   1.1       cgd 	 * a dot and a serial number.  A file which has not been executed
    376   1.1       cgd 	 * yet is denoted by its execute - bit set.  For those files which
    377   1.1       cgd 	 * are to be executed, run_file() is called, which forks off a
    378   1.1       cgd 	 * child which takes care of I/O redirection, forks off another
    379   1.1       cgd 	 * child for execution and yet another one, optionally, for sending
    380   1.1       cgd 	 * mail.  Files which already have run are removed during the
    381   1.1       cgd 	 * next invocation.
    382   1.1       cgd 	 */
    383   1.1       cgd 	DIR *spool;
    384   1.1       cgd 	struct dirent *dirent;
    385   1.1       cgd 	struct stat buf;
    386   1.1       cgd 	unsigned long ctm;
    387   1.5  christos 	int jobno;
    388   1.1       cgd 	char queue;
    389   1.5  christos 	time_t now, run_time;
    390   1.5  christos 	char batch_name[] = "Z2345678901234";
    391   1.5  christos 	uid_t batch_uid;
    392   1.5  christos 	gid_t batch_gid;
    393   1.5  christos 	int c;
    394   1.5  christos 	int run_batch;
    395   1.5  christos 	double la, load_avg = ATRUN_MAXLOAD;
    396   1.1       cgd 
    397   1.1       cgd 	/*
    398   1.1       cgd 	 * We don't need root privileges all the time; running under uid
    399   1.7    simonb 	 * and gid nobody is fine except for privileged operations.
    400   1.1       cgd 	 */
    401   1.5  christos 	RELINQUISH_PRIVS_ROOT(NOBODY_UID, NOBODY_GID)
    402   1.1       cgd 
    403   1.1       cgd 	openlog("atrun", LOG_PID, LOG_CRON);
    404   1.1       cgd 
    405   1.5  christos 	opterr = 0;
    406   1.5  christos 	errno = 0;
    407   1.5  christos 	while ((c = getopt(argc, argv, "dl:")) != -1) {
    408   1.5  christos 		switch (c) {
    409   1.5  christos 		case 'l':
    410   1.5  christos 			if (sscanf(optarg, "%lf", &load_avg) != 1)
    411   1.5  christos 				perr("garbled option -l");
    412   1.5  christos 			if (load_avg <= 0.)
    413   1.5  christos 				load_avg = ATRUN_MAXLOAD;
    414   1.5  christos 			break;
    415   1.5  christos 
    416   1.5  christos 		case 'd':
    417   1.5  christos 			debug++;
    418   1.5  christos 			break;
    419   1.5  christos 
    420   1.5  christos 		case '?':
    421   1.5  christos 			perr("unknown option");
    422   1.5  christos 			break;
    423   1.5  christos 
    424   1.5  christos 		default:
    425   1.5  christos 			perr("idiotic option - aborted");
    426   1.5  christos 			break;
    427   1.5  christos 		}
    428   1.5  christos 	}
    429   1.5  christos 
    430   1.1       cgd 	namep = argv[0];
    431   1.5  christos 
    432   1.5  christos 	PRIV_START
    433   1.5  christos 
    434   1.1       cgd 	if (chdir(_PATH_ATJOBS) != 0)
    435   1.5  christos 		perr2("Cannot change to ", _PATH_ATJOBS);
    436   1.5  christos 
    437   1.1       cgd 
    438   1.1       cgd 	/*
    439   1.1       cgd 	 * Main loop. Open spool directory for reading and look over all
    440   1.1       cgd 	 * the files in there. If the filename indicates that the job
    441   1.1       cgd 	 * should be run and the x bit is set, fork off a child which sets
    442   1.1       cgd 	 * its user and group id to that of the files and exec a /bin/sh
    443   1.1       cgd 	 * which executes the shell script. Unlink older files if they
    444   1.1       cgd 	 * should no longer be run.  For deletion, their r bit has to be
    445   1.1       cgd 	 * turned on.
    446   1.5  christos 	 *
    447   1.5  christos 	 * Also, pick the oldest batch job to run, at most one per
    448   1.5  christos 	 * invocation of atrun.
    449   1.1       cgd 	 */
    450   1.1       cgd 	if ((spool = opendir(".")) == NULL)
    451   1.5  christos 		perr2("Cannot read ", _PATH_ATJOBS);
    452   1.5  christos 
    453   1.5  christos 	PRIV_END
    454   1.5  christos 
    455   1.5  christos 	now = time(NULL);
    456   1.5  christos 	run_batch = 0;
    457   1.5  christos 	batch_uid = (uid_t) -1;
    458   1.5  christos 	batch_gid = (gid_t) -1;
    459   1.1       cgd 
    460   1.1       cgd 	while ((dirent = readdir(spool)) != NULL) {
    461   1.5  christos 		PRIV_START
    462   1.1       cgd 
    463   1.1       cgd 		if (stat(dirent->d_name, &buf) != 0)
    464   1.5  christos 			perr2("Cannot stat in ", _PATH_ATJOBS);
    465   1.5  christos 
    466   1.5  christos 		PRIV_END
    467   1.1       cgd 
    468   1.1       cgd 		/* We don't want directories */
    469   1.1       cgd 		if (!S_ISREG(buf.st_mode))
    470   1.1       cgd 			continue;
    471   1.1       cgd 
    472   1.5  christos 		if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) != 3)
    473   1.1       cgd 			continue;
    474   1.1       cgd 
    475   1.5  christos 		run_time = (time_t) ctm * 60;
    476   1.1       cgd 
    477   1.5  christos 		if ((S_IXUSR & buf.st_mode) && (run_time <= now)) {
    478   1.5  christos 			if (isupper(queue) &&
    479   1.5  christos 			    (strcmp(batch_name, dirent->d_name) > 0)) {
    480   1.5  christos 				run_batch = 1;
    481   1.5  christos 				(void)strncpy(batch_name, dirent->d_name,
    482   1.5  christos 				    sizeof(batch_name));
    483   1.5  christos 				batch_uid = buf.st_uid;
    484   1.5  christos 				batch_gid = buf.st_gid;
    485   1.5  christos 			}
    486   1.5  christos 
    487   1.5  christos 			/* The file is executable and old enough */
    488   1.5  christos 			if (islower(queue))
    489   1.5  christos 				run_file(dirent->d_name, buf.st_uid, buf.st_gid);
    490   1.5  christos 		}
    491   1.1       cgd 
    492   1.5  christos 		/* Delete older files */
    493   1.5  christos 		if ((run_time < now) && !(S_IXUSR & buf.st_mode) &&
    494   1.5  christos 		    (S_IRUSR & buf.st_mode)) {
    495   1.1       cgd 			PRIV_START
    496   1.1       cgd 
    497   1.5  christos 			(void)unlink(dirent->d_name);
    498   1.1       cgd 
    499   1.1       cgd 			PRIV_END
    500   1.1       cgd 		}
    501   1.1       cgd 	}
    502   1.5  christos 
    503   1.5  christos 	/* Run the single batch file, if any */
    504   1.5  christos 	if (run_batch && ((getloadavg(&la, 1) == 1) && la < load_avg))
    505   1.5  christos 		run_file(batch_name, batch_uid, batch_gid);
    506   1.5  christos 
    507   1.1       cgd 	closelog();
    508   1.1       cgd 	exit(EXIT_SUCCESS);
    509   1.1       cgd }
    510