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

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