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