cvsdist 5b1e33
--- at-3.1.8.orig/atd.c	Tue Jul 16 21:35:17 2002
cvsdist 737cec
+++ at-3.1.8/atd.c	Sat Jul 20 17:26:24 2002
cvsdist 737cec
@@ -113,7 +113,7 @@
cvsdist 737cec
 static char rcsid[] = "$Id: atd.c,v 1.28 1997/05/06 08:31:09 ig25 Exp $";
cvsdist 737cec
 static double load_avg = LOADAVG_MX;
cvsdist 737cec
 static time_t now;
cvsdist 737cec
-static time_t last_chg;
cvsdist 737cec
+//static time_t last_chg;
cvsdist 737cec
 static int nothing_to_do;
cvsdist 737cec
 unsigned int batch_interval;
cvsdist 737cec
 static int run_as_daemon = 0;
cvsdist 5b1e33
@@ -188,7 +188,7 @@
cvsdist 5b1e33
 #endif
cvsdist 5b1e33
 
cvsdist 5b1e33
 static void
cvsdist 5b1e33
-run_file(const char *filename, uid_t uid, gid_t gid)
cvsdist 5b1e33
+run_file(char *filename, uid_t uid, gid_t gid)
cvsdist 5b1e33
 {
cvsdist 5b1e33
 /* Run a file by by spawning off a process which redirects I/O,
cvsdist 5b1e33
  * spawns a subshell, then waits for it to complete and sends
cvsdist 5b1e33
@@ -198,7 +198,7 @@
cvsdist 5b1e33
     int fd_out, fd_in;
cvsdist 5b1e33
     char mailbuf[256], jobbuf[9];
cvsdist 5b1e33
     char *mailname = NULL;
cvsdist 5b1e33
-    char *newname;
cvsdist 5b1e33
+    char newname[256];
cvsdist 5b1e33
     FILE *stream;
cvsdist 5b1e33
     int send_mail = 0;
cvsdist 5b1e33
     struct stat buf, lbuf;
cvsdist 5b1e33
@@ -214,12 +214,18 @@
cvsdist 5b1e33
 
cvsdist 5b1e33
     sprintf(jobbuf, "%8lu", jobno);
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if ((newname = malloc(strlen(filename) + 1)) == NULL)
cvsdist 5b1e33
-	pabort("Job %8lu : out of virtual memory", jobno);
cvsdist 5b1e33
+    if( strlen( filename ) >= sizeof( newname ) - 1 )
cvsdist 5b1e33
+    	pabort("File name too long: %s", filename );
cvsdist 5b1e33
 
cvsdist 5b1e33
     strcpy(newname, filename);
cvsdist 5b1e33
+    
cvsdist 5b1e33
+    newname[0] = '!';
cvsdist 5b1e33
+    
cvsdist 5b1e33
+    if( rename( filename, newname ) < 0 )
cvsdist 5b1e33
+    	perr( "Error renaming job file." );
cvsdist 5b1e33
 
cvsdist 5b1e33
-    newname[0] = '=';
cvsdist 5b1e33
+    filename[0] = '!';
cvsdist 5b1e33
+    newname[0] = '=';    
cvsdist 5b1e33
 
cvsdist 5b1e33
     /* We try to make a hard link to lock the file.  If we fail, then
cvsdist 5b1e33
      * somebody else has already locked it (a second atd?); log the
cvsdist 5b1e33
@@ -234,13 +240,15 @@
cvsdist 5b1e33
 	}
cvsdist 5b1e33
     }
cvsdist 5b1e33
     /* If something goes wrong between here and the unlink() call,
cvsdist 5b1e33
-     * the job gets restarted as soon as the "=" entry is cleared
cvsdist 5b1e33
-     * by the main atd loop.
cvsdist 5b1e33
-     */
cvsdist 5b1e33
+     * the job will remain in the "!" queue.
cvsdist 5b1e33
+     * no point in retrying, and need glaring proof that something went wrong
cvsdist 5b1e33
+     */     
cvsdist 5b1e33
 
cvsdist 5b1e33
     pid = fork();
cvsdist 5b1e33
-    if (pid == -1)
cvsdist 5b1e33
+    if (pid == -1) {
cvsdist 5b1e33
+        unlink( newname );
cvsdist 5b1e33
 	perr("Cannot fork");
cvsdist 5b1e33
+    }
cvsdist 5b1e33
 
cvsdist 5b1e33
     else if (pid != 0) {
cvsdist 5b1e33
 	return;
cvsdist 5b1e33
@@ -252,6 +260,7 @@
cvsdist 5b1e33
 
cvsdist 5b1e33
     pentry = getpwuid(uid);
cvsdist 5b1e33
     if (pentry == NULL) {
cvsdist 5b1e33
+        unlink( newname );
cvsdist 5b1e33
 	pabort("Userid %lu not found - aborting job %8lu (%.500s)",
cvsdist 5b1e33
 	       (unsigned long) uid, jobno, filename);
cvsdist 5b1e33
     }
cvsdist 5b1e33
@@ -261,34 +270,49 @@
cvsdist 5b1e33
 
cvsdist 5b1e33
     PRIV_END
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if (stream == NULL)
cvsdist 5b1e33
+    if (stream == NULL) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Cannot open input file");
cvsdist 5b1e33
+    }
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if ((fd_in = dup(fileno(stream))) < 0)
cvsdist 5b1e33
+    if ((fd_in = dup(fileno(stream))) < 0) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Error duplicating input file descriptor");
cvsdist 5b1e33
+    }
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if (fstat(fd_in, &buf) == -1)
cvsdist 5b1e33
+    if (fstat(fd_in, &buf) == -1) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Error in fstat of input file descriptor");
cvsdist 5b1e33
+    }
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if (lstat(filename, &lbuf) == -1)
cvsdist 5b1e33
+    if (lstat(filename, &lbuf) == -1) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Error in fstat of input file");
cvsdist 5b1e33
+    }
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if (S_ISLNK(lbuf.st_mode))
cvsdist 5b1e33
+    if (S_ISLNK(lbuf.st_mode)) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Symbolic link encountered in job %8lu (%.500s) - aborting",
cvsdist 5b1e33
 	     jobno, filename);
cvsdist 5b1e33
+    }			     
cvsdist 5b1e33
 
cvsdist 5b1e33
     if ((lbuf.st_dev != buf.st_dev) || (lbuf.st_ino != buf.st_ino) ||
cvsdist 5b1e33
 	(lbuf.st_uid != buf.st_uid) || (lbuf.st_gid != buf.st_gid) ||
cvsdist 5b1e33
-	(lbuf.st_size != buf.st_size))
cvsdist 5b1e33
+	(lbuf.st_size != buf.st_size)) {
cvsdist 5b1e33
+	unlink( newname );
cvsdist 5b1e33
 	perr("Somebody changed files from under us for job %8lu (%.500s) - "
cvsdist 5b1e33
 	     "aborting", jobno, filename);
cvsdist 5b1e33
+    }	    
cvsdist 5b1e33
 
cvsdist 5b1e33
     if (buf.st_nlink > 2) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Somebody is trying to run a linked script for job %8lu (%.500s)",
cvsdist 5b1e33
 	     filename);
cvsdist 5b1e33
     }
cvsdist 5b1e33
-    if ((fflags = fcntl(fd_in, F_GETFD)) < 0)
cvsdist 5b1e33
+    if ((fflags = fcntl(fd_in, F_GETFD)) < 0) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	perr("Error in fcntl");
cvsdist 5b1e33
+    }		
cvsdist 5b1e33
 
cvsdist 5b1e33
     fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
cvsdist 5b1e33
 
cvsdist 5b1e33
@@ -300,33 +324,47 @@
cvsdist 5b1e33
      * the bug is located.  -Joey
cvsdist 5b1e33
      */
cvsdist 5b1e33
     if (fscanf(stream, "#!/bin/sh\n# atrun uid=%d gid=%d\n# mail %255s %d",
cvsdist 5b1e33
-	       &nuid, &ngid, mailbuf, &send_mail) != 4)
cvsdist 5b1e33
+	       &nuid, &ngid, mailbuf, &send_mail) != 4) {
cvsdist 5b1e33
+	unlink( newname );
cvsdist 5b1e33
 	pabort("File %.500s is in wrong format - aborting",
cvsdist 5b1e33
 	       filename);
cvsdist 5b1e33
+    }	       
cvsdist 5b1e33
     mailbuf[255] = '\0';
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if (mailbuf[0] == '-')
cvsdist 5b1e33
+    if (mailbuf[0] == '-') {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	pabort("illegal mail name %.300s in job %8lu (%.300s)", mailbuf,
cvsdist 5b1e33
 	       jobno, filename);
cvsdist 5b1e33
+    }	       
cvsdist 5b1e33
 
cvsdist 5b1e33
     mailname = mailbuf;
cvsdist 5b1e33
-    if (nuid != uid)
cvsdist 5b1e33
+    if (nuid != uid) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	pabort("Job %8lu (%.500s) - userid %d does not match file uid %d",
cvsdist 5b1e33
 	       jobno, filename, nuid, uid);
cvsdist 5b1e33
+    }	       
cvsdist 5b1e33
 
cvsdist 5b1e33
-    if (ngid != gid)
cvsdist 5b1e33
+    if (ngid != gid) {
cvsdist 5b1e33
+    	unlink( newname );
cvsdist 5b1e33
 	pabort("Job %8lu %.500s - groupid %d does not match file gid %d",
cvsdist 5b1e33
 	       jobno, filename, ngid, gid);
cvsdist 5b1e33
+    }	       
cvsdist 5b1e33
 
cvsdist 5b1e33
     /* We are now committed to executing this script.  Unlink the
cvsdist 5b1e33
      * original.
cvsdist 5b1e33
      */
cvsdist 5b1e33
 
cvsdist 5b1e33
     unlink(filename);
cvsdist 5b1e33
+    
cvsdist 5b1e33
+    /* If we bail out from now on, the job gets stuck in "="
cvsdist 5b1e33
+     * The main loop should take care of that.
cvsdist 5b1e33
+     */
cvsdist 5b1e33
 
cvsdist 5b1e33
     fclose(stream);
cvsdist 5b1e33
     if (chdir(ATSPOOL_DIR) < 0)
cvsdist 5b1e33
 	perr("Cannot chdir to " ATSPOOL_DIR);
cvsdist 5b1e33
+	
cvsdist 5b1e33
+    filename[0] = queue;	
cvsdist 5b1e33
 
cvsdist 5b1e33
     /* Create a file to hold the output of the job we are about to run.
cvsdist 5b1e33
      * Write the mail header.  Complain in case 
cvsdist 5b1e33
@@ -417,19 +455,21 @@
cvsdist 5b1e33
     waitpid(pid, (int *) NULL, 0);
cvsdist 5b1e33
 
cvsdist 5b1e33
     /* Send mail.  Unlink the output file after opening it, so it
cvsdist 5b1e33
-     * doesn't hang around after the run.
cvsdist 5b1e33
+     * doesn't hang around after the run (if we are to send mail)
cvsdist 5b1e33
      */
cvsdist 5b1e33
-    stat(filename, &buf;;
cvsdist 5b1e33
-    if (open(filename, O_RDONLY) != STDIN_FILENO)
cvsdist 5b1e33
-	perr("Open of jobfile failed");
cvsdist 5b1e33
+     
cvsdist 5b1e33
+    if( send_mail != -1 ) {
cvsdist 5b1e33
+      stat(filename, &buf;;
cvsdist 5b1e33
+      if (open(filename, O_RDONLY) != STDIN_FILENO)
cvsdist 5b1e33
+	  perr("Open of jobfile failed");
cvsdist 5b1e33
 
cvsdist 5b1e33
-    unlink(filename);
cvsdist 5b1e33
+      unlink(filename);
cvsdist 5b1e33
+    }
cvsdist 5b1e33
 
cvsdist 5b1e33
     /* The job is now finished.  We can delete its input file.
cvsdist 5b1e33
      */
cvsdist 5b1e33
     chdir(ATJOB_DIR);
cvsdist 5b1e33
     unlink(newname);
cvsdist 5b1e33
-    free(newname);
cvsdist 5b1e33
 
cvsdist 5b1e33
     if (((send_mail != -1) && (buf.st_size != size)) || (send_mail == 1)) {
cvsdist 5b1e33
 
cvsdist 737cec
@@ -462,6 +502,8 @@
cvsdist 737cec
     exit(EXIT_SUCCESS);
cvsdist 737cec
 }
cvsdist 737cec
 
cvsdist 737cec
+#define CHECK_INTERVAL_5MIN	300
cvsdist 737cec
+
cvsdist 737cec
 static time_t
cvsdist 737cec
 run_loop()
cvsdist 737cec
 {
cvsdist 737cec
@@ -491,7 +533,7 @@
cvsdist 737cec
      * atrun.
cvsdist 737cec
      */
cvsdist 737cec
 
cvsdist 737cec
-    next_job = now + CHECK_INTERVAL;
cvsdist 737cec
+    next_job = now + CHECK_INTERVAL_5MIN;
cvsdist 737cec
     if (next_batch == 0)
cvsdist 737cec
 	next_batch = now;
cvsdist 737cec
 
cvsdist 737cec
@@ -502,11 +544,11 @@
cvsdist 737cec
 
cvsdist 737cec
     if (stat(".", &buf) == -1)
cvsdist 737cec
 	perr("Cannot stat " ATJOB_DIR);
cvsdist 737cec
-
cvsdist 737cec
+/*
cvsdist 737cec
     if (nothing_to_do && buf.st_mtime <= last_chg)
cvsdist 737cec
 	return next_job;
cvsdist 737cec
     last_chg = buf.st_mtime;
cvsdist 737cec
-
cvsdist 737cec
+*/    
cvsdist 737cec
     if ((spool = opendir(".")) == NULL)
cvsdist 737cec
 	perr("Cannot read " ATJOB_DIR);
cvsdist 737cec