summaryrefslogtreecommitdiffstats
path: root/usr.sbin/cron
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-08-24 17:51:36 +0000
committerpjd <pjd@FreeBSD.org>2005-08-24 17:51:36 +0000
commit45a889ae7cd87575cfb3c61357c03b5aa38a495b (patch)
treefb2a6b32b48a5a88421ec0e7e173b487e80a24a9 /usr.sbin/cron
parentca93f2e965682fbc7b5e751fda50492bbf7d9093 (diff)
downloadFreeBSD-src-45a889ae7cd87575cfb3c61357c03b5aa38a495b.zip
FreeBSD-src-45a889ae7cd87575cfb3c61357c03b5aa38a495b.tar.gz
Use pidfile(3) in cron(8).
Note, that when cron(8) cannot create pidfile, it'll exit. I didn't changed this behaviour, but its better to ignore errors other than EEXIST, so daemon can be started on systems where /var/ file system doesn't support locking (like NFS without rpc.lockd(8)).
Diffstat (limited to 'usr.sbin/cron')
-rw-r--r--usr.sbin/cron/cron/cron.c28
-rw-r--r--usr.sbin/cron/cron/cron.h3
-rw-r--r--usr.sbin/cron/cron/do_command.c2
-rw-r--r--usr.sbin/cron/lib/misc.c59
4 files changed, 29 insertions, 63 deletions
diff --git a/usr.sbin/cron/cron/cron.c b/usr.sbin/cron/cron/cron.c
index 17c6894..f092fae 100644
--- a/usr.sbin/cron/cron/cron.c
+++ b/usr.sbin/cron/cron/cron.c
@@ -46,6 +46,7 @@ static void usage __P((void)),
static time_t last_time = 0;
static int dst_enabled = 0;
+struct pidfh *pfh;
static void
usage() {
@@ -63,6 +64,28 @@ usage() {
exit(ERROR_EXIT);
}
+static void
+open_pidfile(void)
+{
+ char pidfile[MAX_FNAME];
+ char buf[MAX_TEMPSTR];
+ int otherpid;
+
+ (void) snprintf(pidfile, sizeof(pidfile), PIDFILE, PIDDIR);
+ pfh = pidfile_open(pidfile, 0644, &otherpid);
+ if (pfh == NULL) {
+ if (errno == EEXIST) {
+ snprintf(buf, sizeof(buf),
+ "cron already running, pid: %d", otherpid);
+ } else {
+ snprintf(buf, sizeof(buf),
+ "can't open or create %s: %s", pidfile,
+ strerror(errno));
+ }
+ log_it("CRON", getpid(), "DEATH", buf);
+ errx(ERROR_EXIT, "%s", buf);
+ }
+}
int
main(argc, argv)
@@ -87,7 +110,7 @@ main(argc, argv)
#endif
(void) signal(SIGHUP, sighup_handler);
- acquire_daemonlock(0);
+ open_pidfile();
set_cron_uid();
set_cron_cwd();
@@ -105,12 +128,13 @@ main(argc, argv)
(void) fprintf(stderr, "[%d] cron started\n", getpid());
} else {
if (daemon(1, 0) == -1) {
+ pidfile_remove(pfh);
log_it("CRON",getpid(),"DEATH","can't become daemon");
exit(0);
}
}
- acquire_daemonlock(0);
+ pidfile_write(pfh);
database.head = NULL;
database.tail = NULL;
database.mtime = (time_t) 0;
diff --git a/usr.sbin/cron/cron/cron.h b/usr.sbin/cron/cron/cron.h
index f7b2b7a..249bec9 100644
--- a/usr.sbin/cron/cron/cron.h
+++ b/usr.sbin/cron/cron/cron.h
@@ -34,6 +34,7 @@
#include <ctype.h>
#include <err.h>
#include <errno.h>
+#include <libutil.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
@@ -210,7 +211,6 @@ void set_cron_uid __P((void)),
env_free __P((char **)),
unget_char __P((int, FILE *)),
free_entry __P((entry *)),
- acquire_daemonlock __P((int)),
skip_comments __P((FILE *)),
log_it __P((char *, int, char *, char *)),
log_close __P((void));
@@ -289,6 +289,7 @@ extern int LineNumber;
extern unsigned Jitter,
RootJitter;
extern time_t TargetTime;
+extern struct pidfh *pfh;
# if DEBUGGING
extern int DebugFlags;
extern char *DebugFlagNames[];
diff --git a/usr.sbin/cron/cron/do_command.c b/usr.sbin/cron/cron/do_command.c
index 592ca50..258e4a2 100644
--- a/usr.sbin/cron/cron/do_command.c
+++ b/usr.sbin/cron/cron/do_command.c
@@ -59,7 +59,7 @@ do_command(e, u)
break;
case 0:
/* child process */
- acquire_daemonlock(1);
+ pidfile_close(pfh);
child_process(e, u);
Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
_exit(OK_EXIT);
diff --git a/usr.sbin/cron/lib/misc.c b/usr.sbin/cron/lib/misc.c
index 5f6b89f..e557afc 100644
--- a/usr.sbin/cron/lib/misc.c
+++ b/usr.sbin/cron/lib/misc.c
@@ -222,65 +222,6 @@ set_cron_cwd()
}
-/* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
- * another daemon is already running, which we detect here.
- *
- * note: main() calls us twice; once before forking, once after.
- * we maintain static storage of the file pointer so that we
- * can rewrite our PID into the PIDFILE after the fork.
- *
- * it would be great if fflush() disassociated the file buffer.
- */
-void
-acquire_daemonlock(closeflag)
- int closeflag;
-{
- static FILE *fp = NULL;
-
- if (closeflag && fp) {
- fclose(fp);
- fp = NULL;
- return;
- }
-
- if (!fp) {
- char pidfile[MAX_FNAME];
- char buf[MAX_TEMPSTR];
- int fd, otherpid;
-
- (void) sprintf(pidfile, PIDFILE, PIDDIR);
- if ((-1 == (fd = open(pidfile, O_RDWR|O_CREAT, 0644)))
- || (NULL == (fp = fdopen(fd, "r+")))
- ) {
- sprintf(buf, "can't open or create %s: %s",
- pidfile, strerror(errno));
- log_it("CRON", getpid(), "DEATH", buf);
- errx(ERROR_EXIT, "%s", buf);
- }
-
- if (flock(fd, LOCK_EX|LOCK_NB) < OK) {
- int save_errno = errno;
-
- fscanf(fp, "%d", &otherpid);
- sprintf(buf, "can't lock %s, otherpid may be %d: %s",
- pidfile, otherpid, strerror(save_errno));
- log_it("CRON", getpid(), "DEATH", buf);
- errx(ERROR_EXIT, "%s", buf);
- }
-
- (void) fcntl(fd, F_SETFD, 1);
- }
-
- rewind(fp);
- fprintf(fp, "%d\n", getpid());
- fflush(fp);
- (void) ftruncate(fileno(fp), ftell(fp));
-
- /* abandon fd and fp even though the file is open. we need to
- * keep it open and locked, but we don't need the handles elsewhere.
- */
-}
-
/* get_char(file) : like getc() but increment LineNumber on newlines
*/
int
OpenPOWER on IntegriCloud