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