Blame SOURCES/at-3.1.20-lock-locks.patch

ce9709
diff -up at-3.1.20/atd.c.lock-locks at-3.1.20/atd.c
ce9709
--- at-3.1.20/atd.c.lock-locks	2016-07-01 10:41:50.640867692 +0200
ce9709
+++ at-3.1.20/atd.c	2016-07-01 10:42:32.345844967 +0200
ce9709
@@ -74,6 +74,9 @@
ce9709
 #include <syslog.h>
ce9709
 #endif
ce9709
 
ce9709
+#include <sys/file.h>
ce9709
+#include <utime.h>
ce9709
+
ce9709
 /* Local headers */
ce9709
 
ce9709
 #include "privs.h"
ce9709
@@ -288,7 +291,7 @@ run_file(const char *filename, uid_t uid
ce9709
  * mail to the user.
ce9709
  */
ce9709
     pid_t pid;
ce9709
-    int fd_out, fd_in;
ce9709
+    int fd_out, fd_in, fd_std;
ce9709
     char jobbuf[9];
ce9709
     char *mailname = NULL;
ce9709
     int mailsize = 128;
ce9709
@@ -410,6 +413,10 @@ run_file(const char *filename, uid_t uid
ce9709
 
ce9709
     fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
ce9709
 
ce9709
+    if (flock(fd_in, LOCK_EX | LOCK_NB) != 0)
ce9709
+	    perr("Somebody already locked the job %8lu (%.500s) - "
ce9709
+	     "aborting", jobno, filename);
ce9709
+
ce9709
     /*
ce9709
      * If the spool directory is mounted via NFS `atd' isn't able to
ce9709
      * read from the job file and will bump out here.  The file is
ce9709
@@ -553,10 +560,7 @@ run_file(const char *filename, uid_t uid
ce9709
 	PRIV_END
ce9709
     }
ce9709
     /* We're the parent.  Let's wait.
ce9709
-     */
ce9709
-    close(fd_in);
ce9709
-
ce9709
-    /* We inherited the master's SIGCHLD handler, which does a
ce9709
+       We inherited the master's SIGCHLD handler, which does a
ce9709
        non-blocking waitpid. So this blocking one will eventually
ce9709
        return with an ECHILD error. 
ce9709
      */
ce9709
@@ -573,14 +577,14 @@ run_file(const char *filename, uid_t uid
ce9709
     /* some sendmail implementations are confused if stdout, stderr are
ce9709
      * not available, so let them point to /dev/null
ce9709
      */
ce9709
-    if ((fd_in = open("/dev/null", O_WRONLY)) < 0)
ce9709
+    if ((fd_std = open("/dev/null", O_WRONLY)) < 0)
ce9709
 	perr("Could not open /dev/null.");
ce9709
-    if (dup2(fd_in, STDOUT_FILENO) < 0)
ce9709
+    if (dup2(fd_std, STDOUT_FILENO) < 0)
ce9709
 	perr("Could not use /dev/null as standard output.");
ce9709
-    if (dup2(fd_in, STDERR_FILENO) < 0)
ce9709
+    if (dup2(fd_std, STDERR_FILENO) < 0)
ce9709
 	perr("Could not use /dev/null as standard error.");
ce9709
-    if (fd_in != STDOUT_FILENO && fd_in != STDERR_FILENO)
ce9709
-	close(fd_in);
ce9709
+    if (fd_std != STDOUT_FILENO && fd_std != STDERR_FILENO)
ce9709
+	close(fd_std);
ce9709
 
ce9709
     if (unlink(filename) == -1)
ce9709
         syslog(LOG_WARNING, "Warning: removing output file for job %li failed: %s",
ce9709
@@ -588,7 +592,12 @@ run_file(const char *filename, uid_t uid
ce9709
 
ce9709
     /* The job is now finished.  We can delete its input file.
ce9709
      */
ce9709
-    chdir(ATJOB_DIR);
ce9709
+    if (chdir(ATJOB_DIR) != 0)
ce9709
+	perr("Somebody removed %s directory from under us.", ATJOB_DIR);
ce9709
+
ce9709
+    /* This also removes the flock */
ce9709
+    (void)close(fd_in);
ce9709
+
ce9709
     unlink(newname);
ce9709
     free(newname);
ce9709
 
ce9709
@@ -723,16 +732,18 @@ run_loop()
ce9709
 
ce9709
 	/* Skip lock files */
ce9709
 	if (queue == '=') {
ce9709
-            /* FIXME: calhariz */
ce9709
-            /* I think the following code is broken, but commenting
ce9709
-               may haven unknow side effects.  Make a release and see
ce9709
-               in the wild how it works. For more information see:
ce9709
-               https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=818508/*
ce9709
-
ce9709
-	    /* if ((buf.st_nlink == 1) && (run_time + CHECK_INTERVAL <= now)) { */
ce9709
-	    /*     /\* Remove stale lockfile FIXME: lock the lockfile, if you fail, it's still in use. *\/ */
ce9709
-	    /*     unlink(dirent->d_name); */
ce9709
-	    /* } */
ce9709
+	    if ((buf.st_nlink == 1) && (run_time + CHECK_INTERVAL <= now)) {
ce9709
+		int fd;
ce9709
+
ce9709
+		fd = open(dirent->d_name, O_RDONLY);
ce9709
+		if (fd != -1) {
ce9709
+			if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
ce9709
+				unlink(dirent->d_name);
ce9709
+				syslog(LOG_NOTICE, "removing stale lock file %s\n", dirent->d_name);
ce9709
+			}
ce9709
+			(void)close(fd);
ce9709
+		}
ce9709
+	    }
ce9709
 	    continue;
ce9709
 	}
ce9709
 	/* Skip any other file types which may have been invented in