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

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