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

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