Blame SOURCES/cronie-1.4.11-race-on-crontab-modification.patch

68a439
diff -ru cronie-1.4.11/src/database.c cronie-1.4.11_patched/src/database.c
68a439
--- cronie-1.4.11/src/database.c	2018-10-19 15:29:55.630225195 +0200
68a439
+++ cronie-1.4.11_patched/src/database.c	2018-10-19 15:32:14.552093860 +0200
68a439
@@ -48,6 +48,7 @@
68a439
 #include "pathnames.h"
68a439
 
68a439
 #define TMAX(a,b) ((a)>(b)?(a):(b))
68a439
+#define TMIN(a,b) ((a)<(b)?(a):(b))
68a439
 
68a439
 /* size of the event structure, not counting name */
68a439
 #define EVENT_SIZE  (sizeof (struct inotify_event))
68a439
@@ -237,6 +238,8 @@
68a439
 	if ((crontab_fd = check_open(tabname, uname, pw, &mtime)) == -1)
68a439
 		goto next_crontab;
68a439
 
68a439
+	mtime = TMIN(new_db->mtime, mtime);
68a439
+
68a439
 	Debug(DLOAD, ("\t%s:", fname));
68a439
 
68a439
 	if (old_db != NULL)
68a439
@@ -261,7 +264,7 @@
68a439
 		 * we finish with the crontab...
68a439
 		 */
68a439
 		Debug(DLOAD, (" [delete old data]"));
68a439
-			unlink_user(old_db, u);
68a439
+		unlink_user(old_db, u);
68a439
 		free_user(u);
68a439
 		log_it(fname, getpid(), "RELOAD", tabname, 0);
68a439
 	}
68a439
@@ -328,18 +331,18 @@
68a439
 	cron_db new_db;
68a439
 	DIR_T *dp;
68a439
 	DIR *dir;
68a439
-	struct timeval time;
68a439
+	struct timeval timev;
68a439
 	fd_set rfds;
68a439
 	int retval;
68a439
 	char buf[BUF_LEN];
68a439
 	pid_t pid = getpid();
68a439
-	time.tv_sec = 0;
68a439
-	time.tv_usec = 0;
68a439
+	timev.tv_sec = 0;
68a439
+	timev.tv_usec = 0;
68a439
 
68a439
 	FD_ZERO(&rfds);
68a439
 	FD_SET(old_db->ifd, &rfds);
68a439
 
68a439
-	retval = select(old_db->ifd + 1, &rfds, NULL, NULL, &time);
68a439
+	retval = select(old_db->ifd + 1, &rfds, NULL, NULL, &timev;;
68a439
 	if (retval == -1) {
68a439
 		if (errno != EINTR)
68a439
 			log_it("CRON", pid, "INOTIFY", "select failed", errno);
68a439
@@ -348,6 +351,7 @@
68a439
 	else if (FD_ISSET(old_db->ifd, &rfds)) {
68a439
 		new_db.head = new_db.tail = NULL;
68a439
 		new_db.ifd = old_db->ifd;
68a439
+		new_db.mtime = time(NULL) - 1;
68a439
 		while ((retval = read(old_db->ifd, buf, sizeof (buf))) == -1 &&
68a439
 			errno == EINTR) ;
68a439
 
68a439
@@ -452,14 +456,17 @@
68a439
 	DIR *dir;
68a439
 	pid_t pid = getpid();
68a439
 	int is_local = 0;
68a439
+	time_t now;
68a439
 
68a439
 	Debug(DLOAD, ("[%ld] load_database()\n", (long) pid));
68a439
 
68a439
-		/* before we start loading any data, do a stat on SPOOL_DIR
68a439
-		 * so that if anything changes as of this moment (i.e., before we've
68a439
-		 * cached any of the database), we'll see the changes next time.
68a439
-		 */
68a439
-		if (stat(SPOOL_DIR, &statbuf) < OK) {
68a439
+	now = time(NULL);
68a439
+
68a439
+	/* before we start loading any data, do a stat on SPOOL_DIR
68a439
+	 * so that if anything changes as of this moment (i.e., before we've
68a439
+	 * cached any of the database), we'll see the changes next time.
68a439
+	 */
68a439
+	if (stat(SPOOL_DIR, &statbuf) < OK) {
68a439
 		log_it("CRON", pid, "STAT FAILED", SPOOL_DIR, errno);
68a439
 		statbuf.st_mtime = 0;
68a439
 	}
68a439
@@ -492,13 +499,17 @@
68a439
 	 * Note that old_db->mtime is initialized to 0 in main(), and
68a439
 	 * so is guaranteed to be different than the stat() mtime the first
68a439
 	 * time this function is called.
68a439
+	 *
68a439
+	 * We also use now - 1 as the upper bound of timestamp to avoid race,
68a439
+	 * when a crontab is updated twice in a single second when we are
68a439
+         * just reading it.
68a439
 	 */
68a439
-	if (old_db->mtime == TMAX(crond_stat.st_mtime,
68a439
-			TMAX(statbuf.st_mtime, syscron_stat.st_mtime))
68a439
+	if (old_db->mtime == TMIN(now - 1, TMAX(crond_stat.st_mtime,
68a439
+			TMAX(statbuf.st_mtime, syscron_stat.st_mtime)))
68a439
 		) {
68a439
 		Debug(DLOAD, ("[%ld] spool dir mtime unch, no load needed.\n",
68a439
 				(long) pid));
68a439
-			return 0;
68a439
+		return 0;
68a439
 	}
68a439
 
68a439
 	/* something's different.  make a new database, moving unchanged
68a439
@@ -506,8 +517,7 @@
68a439
 	 * actually changed.  Whatever is left in the old database when
68a439
 	 * we're done is chaff -- crontabs that disappeared.
68a439
 	 */
68a439
-	new_db.mtime = TMAX(crond_stat.st_mtime,
68a439
-		TMAX(statbuf.st_mtime, syscron_stat.st_mtime));
68a439
+	new_db.mtime = now - 1;
68a439
 	new_db.head = new_db.tail = NULL;
68a439
 #if defined WITH_INOTIFY
68a439
 	new_db.ifd = old_db->ifd;