Blob Blame History Raw
diff -up at-3.1.13/atd.c.posix at-3.1.13/atd.c
--- at-3.1.13/atd.c.posix	2022-02-15 15:41:22.766701662 +0100
+++ at-3.1.13/atd.c	2022-02-15 15:41:22.768701680 +0100
@@ -821,6 +821,54 @@ run_loop()
     return next_job;
 }
 
+#ifdef HAVE_CLOCK_GETTIME
+timer_t timer;
+struct itimerspec timeout;
+
+void timer_setup()
+{
+    struct sigevent sev;
+
+    sev.sigev_notify = SIGEV_SIGNAL;
+    sev.sigev_signo = SIGHUP;
+    sev.sigev_value.sival_ptr = &timer;
+
+    memset(&timeout, 0, sizeof(timeout));
+
+    if (timer_create(CLOCK_REALTIME, &sev, &timer) < 0)
+	    pabort("unable to create timer");
+}
+
+time_t atd_gettime()
+{
+    struct timespec curtime;
+
+    clock_gettime(CLOCK_REALTIME, &curtime);
+
+    return curtime.tv_sec;
+}
+
+void atd_setalarm(time_t next)
+{
+    timeout.it_value.tv_sec = next;
+    timer_settime(timer, TIMER_ABSTIME, &timeout, NULL);
+    pause();
+}
+#else
+void timer_setup()
+{
+}
+
+time_t atd_gettime()
+{
+    return time(NULL);
+}
+
+void atd_setalarm(time_t next)
+{
+    sleep(next - atd_gettime());
+}
+#endif
 /* Global functions */
 
 int
@@ -926,7 +974,7 @@ main(int argc, char *argv[])
     sigaction(SIGCHLD, &act, NULL);
 
     if (!run_as_daemon) {
-	now = time(NULL);
+	now = atd_gettime();
 	run_loop();
 	exit(EXIT_SUCCESS);
     }
@@ -949,13 +997,15 @@ main(int argc, char *argv[])
     act.sa_handler = set_term;
     sigaction(SIGINT, &act, NULL);
 
+    timer_setup();
+
     daemon_setup();
 
     do {
-	now = time(NULL);
+	now = atd_gettime();
 	next_invocation = run_loop();
 	if (next_invocation > now) {
-	    sleep(next_invocation - now);
+	    atd_setalarm(next_invocation);
 	}
     } while (!term_signal);
     daemon_cleanup();
diff -up at-3.1.13/config.h.in.posix at-3.1.13/config.h.in
--- at-3.1.13/config.h.in.posix	2022-02-15 15:41:22.758701591 +0100
+++ at-3.1.13/config.h.in	2022-02-15 15:41:22.769701689 +0100
@@ -38,6 +38,9 @@
 /* Define to 1 if you have the `getloadavg' function. */
 #undef HAVE_GETLOADAVG
 
+/* Define to 1 if you have the `clock_gettime' function. */
+#undef HAVE_TIMER_CREATE
+
 /* Define to 1 if you have the <getopt.h> header file. */
 #undef HAVE_GETOPT_H
 
diff -up at-3.1.13/configure.ac.posix at-3.1.13/configure.ac
--- at-3.1.13/configure.ac.posix	2022-02-15 15:41:22.770701698 +0100
+++ at-3.1.13/configure.ac	2022-02-15 15:43:29.945835498 +0100
@@ -253,6 +253,10 @@ AC_CHECK_LIB(selinux, is_selinux_enabled
 AC_SUBST(SELINUXLIB)
 AC_SUBST(WITH_SELINUX)
 
+dnl check for POSIX timer functions
+AC_SEARCH_LIBS([timer_create],[rt])
+AC_CHECK_FUNCS([timer_create])
+
 AC_MSG_CHECKING(groupname to run under)
 AC_ARG_WITH(daemon_groupname,
 [ --with-daemon_groupname=DAEMON_GROUPNAME	Groupname to run under (default daemon) ],