Blame SOURCES/at-3.1.13-aborted-jobs.patch

9ccead
diff -up at-3.1.13/atd.c.aborted at-3.1.13/atd.c
9ccead
--- at-3.1.13/atd.c.aborted	2016-04-22 13:30:58.563029540 +0200
9ccead
+++ at-3.1.13/atd.c	2017-09-14 16:00:38.109011916 +0200
9ccead
@@ -74,6 +74,9 @@
9ccead
 #include <syslog.h>
9ccead
 #endif
9ccead
 
9ccead
+#include <sys/file.h>
9ccead
+#include <utime.h>
9ccead
+
9ccead
 /* Local headers */
9ccead
 
9ccead
 #include "privs.h"
9ccead
@@ -285,7 +288,7 @@ run_file(const char *filename, uid_t uid
9ccead
  * mail to the user.
9ccead
  */
9ccead
     pid_t pid;
9ccead
-    int fd_out, fd_in;
9ccead
+    int fd_out, fd_in, fd_std;
9ccead
     char jobbuf[9];
9ccead
     char *mailname = NULL;
9ccead
     int mailsize = 128;
9ccead
@@ -404,6 +407,10 @@ run_file(const char *filename, uid_t uid
9ccead
 
9ccead
     fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
9ccead
 
9ccead
+    if (flock(fd_in, LOCK_EX | LOCK_NB) != 0)
9ccead
+	    perr("Somebody already locked the job %8lu (%.500s) - "
9ccead
+	     "aborting", jobno, filename);
9ccead
+
9ccead
     /*
9ccead
      * If the spool directory is mounted via NFS `atd' isn't able to
9ccead
      * read from the job file and will bump out here.  The file is
9ccead
@@ -563,10 +570,7 @@ run_file(const char *filename, uid_t uid
9ccead
 	PRIV_END
9ccead
     }
9ccead
     /* We're the parent.  Let's wait.
9ccead
-     */
9ccead
-    close(fd_in);
9ccead
-
9ccead
-    /* We inherited the master's SIGCHLD handler, which does a
9ccead
+       We inherited the master's SIGCHLD handler, which does a
9ccead
        non-blocking waitpid. So this blocking one will eventually
9ccead
        return with an ECHILD error. 
9ccead
      */
9ccead
@@ -583,14 +587,14 @@ run_file(const char *filename, uid_t uid
9ccead
     /* some sendmail implementations are confused if stdout, stderr are
9ccead
      * not available, so let them point to /dev/null
9ccead
      */
9ccead
-    if ((fd_in = open("/dev/null", O_WRONLY)) < 0)
9ccead
+    if ((fd_std = open("/dev/null", O_WRONLY)) < 0)
9ccead
 	perr("Could not open /dev/null.");
9ccead
-    if (dup2(fd_in, STDOUT_FILENO) < 0)
9ccead
+    if (dup2(fd_std, STDOUT_FILENO) < 0)
9ccead
 	perr("Could not use /dev/null as standard output.");
9ccead
-    if (dup2(fd_in, STDERR_FILENO) < 0)
9ccead
+    if (dup2(fd_std, STDERR_FILENO) < 0)
9ccead
 	perr("Could not use /dev/null as standard error.");
9ccead
-    if (fd_in != STDOUT_FILENO && fd_in != STDERR_FILENO)
9ccead
-	close(fd_in);
9ccead
+    if (fd_std != STDOUT_FILENO && fd_std != STDERR_FILENO)
9ccead
+	close(fd_std);
9ccead
 
9ccead
     if (unlink(filename) == -1)
9ccead
         syslog(LOG_WARNING, "Warning: removing output file for job %li failed: %s",
9ccead
@@ -598,7 +602,12 @@ run_file(const char *filename, uid_t uid
9ccead
 
9ccead
     /* The job is now finished.  We can delete its input file.
9ccead
      */
9ccead
-    chdir(ATJOB_DIR);
9ccead
+    if (chdir(ATJOB_DIR) != 0)
9ccead
+	perr("Somebody removed %s directory from under us.", ATJOB_DIR);
9ccead
+
9ccead
+    /* This also removes the flock */
9ccead
+    (void)close(fd_in);
9ccead
+
9ccead
     unlink(newname);
9ccead
     free(newname);
9ccead
 
9ccead
@@ -642,7 +651,7 @@ run_file(const char *filename, uid_t uid
9ccead
 	PRIV_END
9ccead
    }
9ccead
    else if ( mail_pid == -1 ) {
9ccead
-           perr("fork of mailer failed");
9ccead
+           syslog(LOG_ERR, "fork of mailer failed: %m");
9ccead
    }
9ccead
    else {
9ccead
            /* Parent */
9ccead
@@ -738,8 +747,16 @@ run_loop()
9ccead
 	/* Skip lock files */
9ccead
 	if (queue == '=') {
9ccead
 	    if ((buf.st_nlink == 1) && (run_time + CHECK_INTERVAL <= now)) {
9ccead
-		/* Remove stale lockfile FIXME: lock the lockfile, if you fail, it's still in use. */
9ccead
-		unlink(dirent->d_name);
9ccead
+		int fd;
9ccead
+
9ccead
+		fd = open(dirent->d_name, O_RDONLY);
9ccead
+		if (fd != -1) {
9ccead
+			if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
9ccead
+				unlink(dirent->d_name);
9ccead
+				syslog(LOG_NOTICE, "removing stale lock file %s\n", dirent->d_name);
9ccead
+			}
9ccead
+			(void)close(fd);
9ccead
+		}
9ccead
 	    }
9ccead
 	    continue;
9ccead
 	}
9ccead
@@ -752,12 +769,17 @@ run_loop()
9ccead
 	/* Is the file already locked?
9ccead
 	 */
9ccead
 	if (buf.st_nlink > 1) {
9ccead
+	    if (run_time < buf.st_mtime)
9ccead
+		run_time = buf.st_mtime;
9ccead
 	    if (run_time + CHECK_INTERVAL <= now) {
9ccead
-
9ccead
 		/* Something went wrong the last time this was executed.
9ccead
 		 * Let's remove the lockfile and reschedule.
9ccead
+		 * We also change the timestamp to avoid rerunning the job more
9ccead
+		 * than once every CHECK_INTERVAL.
9ccead
 		 */
9ccead
 		strncpy(lock_name, dirent->d_name, sizeof(lock_name));
9ccead
+		if (utime(lock_name, 0) < 0)
9ccead
+			syslog(LOG_ERR, "utime couldn't be set for lock file %s\n", lock_name);
9ccead
 		lock_name[sizeof(lock_name)-1] = '\0';
9ccead
 		lock_name[0] = '=';
9ccead
 		unlink(lock_name);