Blob Blame History Raw
diff -up at-3.1.14/atd.c.timers at-3.1.14/atd.c
--- at-3.1.14/atd.c.timers	2013-12-02 11:03:01.250080057 +0100
+++ at-3.1.14/atd.c	2013-12-02 11:06:15.560243498 +0100
@@ -831,6 +831,54 @@ run_loop()
     return next_job;
 }
 
+#ifdef HAVE_TIMER_CREATE
+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
@@ -936,7 +984,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);
     }
@@ -959,13 +1007,14 @@ 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.14/config.h.in.timers at-3.1.14/config.h.in
--- at-3.1.14/config.h.in.timers	2013-12-02 11:00:27.000000000 +0100
+++ at-3.1.14/config.h.in	2013-12-02 11:02:06.521033976 +0100
@@ -38,6 +38,9 @@
 /* Define to 1 if you have the `getloadavg' function. */
 #undef HAVE_GETLOADAVG
 
+/* Define to 1 if you have the `timer_create' 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.14/configure.ac.timers at-3.1.14/configure.ac
--- at-3.1.14/configure.ac.timers	2013-12-02 11:00:27.000000000 +0100
+++ at-3.1.14/configure.ac	2013-12-02 11:02:45.217066560 +0100
@@ -254,6 +254,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) ],