973edf
diff -ur -x configure at-3.1.13.orig/atd.c at-3.1.13/atd.c
973edf
--- at-3.1.13.orig/atd.c	2011-11-16 11:30:22.424764253 -0500
973edf
+++ at-3.1.13/atd.c	2011-11-16 16:41:12.102831656 -0500
973edf
@@ -815,6 +815,54 @@
973edf
     return next_job;
973edf
 }
973edf
 
973edf
+#ifdef HAVE_CLOCK_GETTIME
973edf
+timer_t timer;
973edf
+struct itimerspec timeout;
973edf
+
973edf
+void timer_setup()
973edf
+{
973edf
+    struct sigevent sev;
973edf
+
973edf
+    sev.sigev_notify = SIGEV_SIGNAL;
973edf
+    sev.sigev_signo = SIGHUP;
973edf
+    sev.sigev_value.sival_ptr = &timer;
973edf
+
973edf
+    memset(&timeout, 0, sizeof(timeout));
973edf
+
973edf
+    if (timer_create(CLOCK_REALTIME, &sev, &timer) < 0)
973edf
+	    pabort("unable to create timer");
973edf
+}
973edf
+
973edf
+time_t atd_gettime()
973edf
+{
973edf
+    struct timespec curtime;
973edf
+
973edf
+    clock_gettime(CLOCK_REALTIME, &curtime);
973edf
+
973edf
+    return curtime.tv_sec;
973edf
+}
973edf
+
973edf
+void atd_setalarm(time_t next)
973edf
+{
973edf
+    timeout.it_value.tv_sec = next;
973edf
+    timer_settime(timer, TIMER_ABSTIME, &timeout, NULL);
973edf
+    pause();
973edf
+}
973edf
+#else
973edf
+void timer_setup()
973edf
+{
973edf
+}
973edf
+
973edf
+time_t atd_gettime()
973edf
+{
973edf
+    return time(NULL);
973edf
+}
973edf
+
973edf
+void atd_setalarm(time_t next)
973edf
+{
973edf
+    sleep(next - atd_gettime());
973edf
+}
973edf
+#endif
973edf
 /* Global functions */
973edf
 
973edf
 int
973edf
@@ -835,7 +883,6 @@
973edf
     struct sigaction act;
973edf
     struct passwd *pwe;
973edf
     struct group *ge;
973edf
-
973edf
 #ifdef WITH_SELINUX
973edf
     selinux_enabled=is_selinux_enabled();
973edf
 #endif
973edf
@@ -912,7 +959,7 @@
973edf
     sigaction(SIGCHLD, &act, NULL);
973edf
 
973edf
     if (!run_as_daemon) {
973edf
-	now = time(NULL);
973edf
+	now = atd_gettime();
973edf
 	run_loop();
973edf
 	exit(EXIT_SUCCESS);
973edf
     }
973edf
@@ -935,13 +982,15 @@
973edf
     act.sa_handler = set_term;
973edf
     sigaction(SIGINT, &act, NULL);
973edf
 
973edf
+    timer_setup();
973edf
+
973edf
     daemon_setup();
973edf
 
973edf
     do {
973edf
-	now = time(NULL);
973edf
+	now = atd_gettime();
973edf
 	next_invocation = run_loop();
973edf
 	if (next_invocation > now) {
973edf
-	    sleep(next_invocation - now);
973edf
+	    atd_setalarm(next_invocation);
973edf
 	}
973edf
     } while (!term_signal);
973edf
     daemon_cleanup();
973edf
diff -ur -x configure at-3.1.13.orig/config.h.in at-3.1.13/config.h.in
973edf
--- at-3.1.13.orig/config.h.in	2011-11-16 11:30:22.424764253 -0500
973edf
+++ at-3.1.13/config.h.in	2011-11-16 16:32:44.485426754 -0500
973edf
@@ -38,6 +38,9 @@
973edf
 /* Define to 1 if you have the `getloadavg' function. */
973edf
 #undef HAVE_GETLOADAVG
973edf
 
973edf
+/* Define to 1 if you have the `clock_gettime' function. */
973edf
+#undef HAVE_TIMER_CREATE
973edf
+
973edf
 /* Define to 1 if you have the <getopt.h> header file. */
973edf
 #undef HAVE_GETOPT_H
973edf
 
973edf
diff -ur -x configure at-3.1.13.orig/configure.ac at-3.1.13/configure.ac
973edf
--- at-3.1.13.orig/configure.ac	2011-11-16 11:30:22.425764254 -0500
973edf
+++ at-3.1.13/configure.ac	2011-11-16 16:31:29.791561747 -0500
973edf
@@ -274,5 +274,9 @@
973edf
 AC_SUBST(SELINUXLIB)
973edf
 AC_SUBST(WITH_SELINUX)
973edf
 
973edf
+dnl check for POSIX timer functions
973edf
+AC_SEARCH_LIBS([timer_create],[rt])
973edf
+AC_CHECK_FUNCS([timer_create])
973edf
+
973edf
 AC_CONFIG_FILES(Makefile atrun atd.8 atrun.8 at.1 at.allow.5 batch)
973edf
 AC_OUTPUT