File at-3.1.14-usePOSIXtimers.patch of Package at.23143

Index: at-3.1.20/atd.c
===================================================================
--- at-3.1.20.orig/atd.c
+++ at-3.1.20/atd.c
@@ -787,6 +787,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
@@ -909,7 +957,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);
     }
@@ -932,13 +980,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) && (!hupped)) {
-	    sleep(next_invocation - now);
+	     atd_setalarm(next_invocation);
 	}
 	hupped = 0;
     } while (!term_signal);
Index: at-3.1.20/config.h.in
===================================================================
--- at-3.1.20.orig/config.h.in
+++ at-3.1.20/config.h.in
@@ -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
 
Index: at-3.1.20/configure.ac
===================================================================
--- at-3.1.20.orig/configure.ac
+++ at-3.1.20/configure.ac
@@ -263,6 +263,12 @@ fi
 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_SEARCH_LIBS([clock_gettime],[rt])
+AC_CHECK_FUNCS([clock_gettime])
+
 AC_MSG_CHECKING(groupname to run under)
 AC_ARG_WITH(daemon_groupname,
 [ --with-daemon_groupname=DAEMON_GROUPNAME	Groupname to run under (default daemon) ],
openSUSE Build Service is sponsored by