76a39a
diff -up at-3.1.14/atd.c.timers at-3.1.14/atd.c
76a39a
--- at-3.1.14/atd.c.timers	2013-12-02 11:03:01.250080057 +0100
76a39a
+++ at-3.1.14/atd.c	2013-12-02 11:06:15.560243498 +0100
76a39a
@@ -831,6 +831,54 @@ run_loop()
76a39a
     return next_job;
76a39a
 }
76a39a
 
76a39a
+#ifdef HAVE_TIMER_CREATE
76a39a
+timer_t timer;
76a39a
+struct itimerspec timeout;
76a39a
+
76a39a
+void timer_setup()
76a39a
+{
76a39a
+    struct sigevent sev;
76a39a
+
76a39a
+    sev.sigev_notify = SIGEV_SIGNAL;
76a39a
+    sev.sigev_signo = SIGHUP;
76a39a
+    sev.sigev_value.sival_ptr = &timer;
76a39a
+
76a39a
+    memset(&timeout, 0, sizeof(timeout));
76a39a
+
76a39a
+    if (timer_create(CLOCK_REALTIME, &sev, &timer) < 0)
76a39a
+           pabort("unable to create timer");
76a39a
+}
76a39a
+
76a39a
+time_t atd_gettime()
76a39a
+{
76a39a
+    struct timespec curtime;
76a39a
+
76a39a
+    clock_gettime(CLOCK_REALTIME, &curtime);
76a39a
+
76a39a
+    return curtime.tv_sec;
76a39a
+}
76a39a
+
76a39a
+void atd_setalarm(time_t next)
76a39a
+{
76a39a
+    timeout.it_value.tv_sec = next;
76a39a
+    timer_settime(timer, TIMER_ABSTIME, &timeout, NULL);
76a39a
+    pause();
76a39a
+}
76a39a
+#else
76a39a
+void timer_setup()
76a39a
+{
76a39a
+}
76a39a
+
76a39a
+time_t atd_gettime()
76a39a
+{
76a39a
+    return time(NULL);
76a39a
+}
76a39a
+
76a39a
+void atd_setalarm(time_t next)
76a39a
+{
76a39a
+    sleep(next - atd_gettime());
76a39a
+}
76a39a
+#endif
76a39a
 /* Global functions */
76a39a
 
76a39a
 int
76a39a
@@ -936,7 +984,7 @@ main(int argc, char *argv[])
76a39a
     sigaction(SIGCHLD, &act, NULL);
76a39a
 
76a39a
     if (!run_as_daemon) {
76a39a
-	now = time(NULL);
76a39a
+	now = atd_gettime();
76a39a
 	run_loop();
76a39a
 	exit(EXIT_SUCCESS);
76a39a
     }
76a39a
@@ -959,13 +1007,14 @@ main(int argc, char *argv[])
76a39a
     act.sa_handler = set_term;
76a39a
     sigaction(SIGINT, &act, NULL);
76a39a
 
76a39a
+    timer_setup();
76a39a
     daemon_setup();
76a39a
 
76a39a
     do {
76a39a
-	now = time(NULL);
76a39a
+	now = atd_gettime();
76a39a
 	next_invocation = run_loop();
76a39a
 	if (next_invocation > now) {
76a39a
-	    sleep(next_invocation - now);
76a39a
+	    atd_setalarm(next_invocation);
76a39a
 	}
76a39a
     } while (!term_signal);
76a39a
     daemon_cleanup();
76a39a
diff -up at-3.1.14/config.h.in.timers at-3.1.14/config.h.in
76a39a
--- at-3.1.14/config.h.in.timers	2013-12-02 11:00:27.000000000 +0100
76a39a
+++ at-3.1.14/config.h.in	2013-12-02 11:02:06.521033976 +0100
76a39a
@@ -38,6 +38,9 @@
76a39a
 /* Define to 1 if you have the `getloadavg' function. */
76a39a
 #undef HAVE_GETLOADAVG
76a39a
 
76a39a
+/* Define to 1 if you have the `timer_create' function. */
76a39a
+#undef HAVE_TIMER_CREATE
76a39a
+
76a39a
 /* Define to 1 if you have the <getopt.h> header file. */
76a39a
 #undef HAVE_GETOPT_H
76a39a
 
76a39a
diff -up at-3.1.14/configure.ac.timers at-3.1.14/configure.ac
76a39a
--- at-3.1.14/configure.ac.timers	2013-12-02 11:00:27.000000000 +0100
76a39a
+++ at-3.1.14/configure.ac	2013-12-02 11:02:45.217066560 +0100
76a39a
@@ -254,6 +254,10 @@ AC_CHECK_LIB(selinux, is_selinux_enabled
76a39a
 AC_SUBST(SELINUXLIB)
76a39a
 AC_SUBST(WITH_SELINUX)
76a39a
 
76a39a
+dnl check for POSIX timer functions
76a39a
+AC_SEARCH_LIBS([timer_create],[rt])
76a39a
+AC_CHECK_FUNCS([timer_create])
76a39a
+
76a39a
 AC_MSG_CHECKING(groupname to run under)
76a39a
 AC_ARG_WITH(daemon_groupname,
76a39a
 [ --with-daemon_groupname=DAEMON_GROUPNAME	Groupname to run under (default daemon) ],