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