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