]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/timer: use separate file for timers
authorKarel Zak <kzak@redhat.com>
Fri, 6 Mar 2015 12:17:20 +0000 (13:17 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 6 Mar 2015 12:17:20 +0000 (13:17 +0100)
It seems that static builds require -lpthread for timer_* functions.
It's better to keep it out of our libs (e.g. libmount) to avoid
unnecessary dependence.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/Makemodule.am
include/monotonic.h
include/timer.h [new file with mode: 0644]
lib/monotonic.c
lib/timer.c [new file with mode: 0644]
sys-utils/Makemodule.am
sys-utils/flock.c

index 8d7c881d56948e2bf60a2160640a844899d826d0..e5fb6d3d040b85e7fab873accfc0e394e278620f 100644 (file)
@@ -4,10 +4,9 @@ dist_noinst_HEADERS += \
        include/at.h \
        include/bitops.h \
        include/blkdev.h \
-       include/monotonic.h \
-       include/c.h \
        include/canonicalize.h \
        include/carefulputc.h \
+       include/c.h \
        include/closestream.h \
        include/colors.h \
        include/cpuset.h \
@@ -28,6 +27,7 @@ dist_noinst_HEADERS += \
        include/mbsalign.h \
        include/md5.h \
        include/minix.h \
+       include/monotonic.h \
        include/namespace.h \
        include/nls.h \
        include/optutils.h \
@@ -36,21 +36,22 @@ dist_noinst_HEADERS += \
        include/path.h \
        include/pathnames.h \
        include/procutils.h \
+       include/pt-bsd.h \
+       include/pt-mbr.h \
+       include/pt-mbr-partnames.h \
+       include/pt-sgi.h \
+       include/pt-sun.h \
        include/randutils.h \
        include/readutmp.h \
        include/rpmatch.h \
        include/setproctitle.h \
+       include/statfs_magic.h
        include/strutils.h \
-       include/swapprober.h \
        include/swapheader.h \
+       include/swapprober.h \
        include/sysfs.h \
+       include/timer.h \
        include/timeutils.h \
        include/ttyutils.h \
        include/widechar.h \
-       include/xalloc.h \
-       include/pt-sgi.h \
-       include/pt-bsd.h \
-       include/pt-mbr.h \
-       include/pt-mbr-partnames.h \
-       include/pt-sun.h \
-       include/statfs_magic.h
+       include/xalloc.h
index bbebfe0b97ea208b898e87f8db8221265d90bfb0..a499fa34ac4c03170a11f3911f2d8c85d756a6ea 100644 (file)
@@ -1,18 +1,10 @@
-#ifndef UTIL_LINUX_BOOTTIME_H
-#define UTIL_LINUX_BOOTTIME_H
+#ifndef UTIL_LINUX_MONOTONIC_H
+#define UTIL_LINUX_MONOTONIC_H
 
-/*
- * Uses clock_gettime() that requires $CLOCKGETTIME_LIBS
- */
-#include <signal.h>
 #include <sys/time.h>
 
 extern int get_boot_time(struct timeval *boot_time);
 
 extern int gettime_monotonic(struct timeval *tv);
 
-extern int setup_timer(timer_t * t_id, struct itimerval *timeout,
-                      void (*timeout_handler)(int, siginfo_t *, void *));
-extern void cancel_timer(timer_t * t_id);
-
-#endif /* UTIL_LINUX_BOOTTIME_H */
+#endif /* UTIL_LINUX_MONOTONIC_H */
diff --git a/include/timer.h b/include/timer.h
new file mode 100644 (file)
index 0000000..aa9f9c0
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef UTIL_LINUX_TIMER_H
+#define UTIL_LINUX_TIMER_H
+
+#include <signal.h>
+#include <sys/time.h>
+
+extern int setup_timer(timer_t * t_id, struct itimerval *timeout,
+                      void (*timeout_handler)(int, siginfo_t *, void *));
+extern void cancel_timer(timer_t * t_id);
+
+#endif /* UTIL_LINUX_TIMER_H */
index c6e0f7572adfc8b4e81bc332ac5dbe636d4003d1..f2aa0de0d5c5198c073ecd192c01264bc7cbd5c8 100644 (file)
@@ -8,7 +8,6 @@
 #include <sys/time.h>
 
 #include "c.h"
-#include "nls.h"
 #include "monotonic.h"
 
 int get_boot_time(struct timeval *boot_time)
@@ -22,10 +21,8 @@ int get_boot_time(struct timeval *boot_time)
        struct sysinfo info;
 #endif
 
-       if (gettimeofday(&now, NULL) != 0) {
-               warn(_("gettimeofday failed"));
+       if (gettimeofday(&now, NULL) != 0)
                return -errno;
-       }
 #ifdef CLOCK_BOOTTIME
        if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) {
                TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
@@ -36,7 +33,7 @@ int get_boot_time(struct timeval *boot_time)
 #ifdef HAVE_SYSINFO
        /* fallback */
        if (sysinfo(&info) != 0)
-               warn(_("sysinfo failed"));
+               return -errno;
 
        boot_time->tv_sec = now.tv_sec - info.uptime;
        boot_time->tv_usec = 0;
@@ -68,37 +65,4 @@ int gettime_monotonic(struct timeval *tv)
 #endif
 }
 
-int setup_timer(timer_t * t_id, struct itimerval *timeout,
-               void (*timeout_handler)(int, siginfo_t *, void *))
-{
-       struct sigaction sig_a;
-       static struct sigevent sig_e = {
-               .sigev_notify = SIGEV_SIGNAL,
-               .sigev_signo = SIGALRM
-       };
-       struct itimerspec val = {
-               .it_value.tv_sec = timeout->it_value.tv_sec,
-               .it_value.tv_nsec = timeout->it_value.tv_usec * 1000,
-               .it_interval.tv_sec = 0,
-               .it_interval.tv_nsec = 0
-       };
-
-       if (sigemptyset(&sig_a.sa_mask))
-               return 1;
-
-       sig_a.sa_flags = SA_SIGINFO;
-       sig_a.sa_sigaction = timeout_handler;
 
-       if (sigaction(SIGALRM, &sig_a, 0))
-               return 1;
-       if (timer_create(CLOCK_MONOTONIC, &sig_e, t_id))
-               return 1;
-       if (timer_settime(*t_id, SA_SIGINFO, &val, NULL))
-               return 1;
-       return 0;
-}
-
-void cancel_timer(timer_t *t_id)
-{
-       timer_delete(*t_id);
-}
diff --git a/lib/timer.c b/lib/timer.c
new file mode 100644 (file)
index 0000000..0ad0cad
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Please, don't add this file to libcommon because timers requires
+ * -lrt on systems with old libc (and probably also -lpthread for static
+ *  build).
+ */
+#include <time.h>
+#include <signal.h>
+#include <sys/sysinfo.h>
+#include <sys/time.h>
+
+#include "c.h"
+#include "timer.h"
+
+int setup_timer(timer_t * t_id, struct itimerval *timeout,
+               void (*timeout_handler)(int, siginfo_t *, void *))
+{
+       struct sigaction sig_a;
+       static struct sigevent sig_e = {
+               .sigev_notify = SIGEV_SIGNAL,
+               .sigev_signo = SIGALRM
+       };
+       struct itimerspec val = {
+               .it_value.tv_sec = timeout->it_value.tv_sec,
+               .it_value.tv_nsec = timeout->it_value.tv_usec * 1000,
+               .it_interval.tv_sec = 0,
+               .it_interval.tv_nsec = 0
+       };
+
+       if (sigemptyset(&sig_a.sa_mask))
+               return 1;
+
+       sig_a.sa_flags = SA_SIGINFO;
+       sig_a.sa_sigaction = timeout_handler;
+
+       if (sigaction(SIGALRM, &sig_a, 0))
+               return 1;
+       if (timer_create(CLOCK_MONOTONIC, &sig_e, t_id))
+               return 1;
+       if (timer_settime(*t_id, SA_SIGINFO, &val, NULL))
+               return 1;
+       return 0;
+}
+
+void cancel_timer(timer_t *t_id)
+{
+       timer_delete(*t_id);
+}
index bd1b681e85e08afb0f283b3dca2b06f535d3bb5c..e06e34a04c59ee38253dd3b2d91ad1a67412d0a7 100644 (file)
@@ -1,7 +1,7 @@
 if BUILD_FLOCK
 usrbin_exec_PROGRAMS += flock
 dist_man_MANS += sys-utils/flock.1
-flock_SOURCES = sys-utils/flock.c lib/monotonic.c
+flock_SOURCES = sys-utils/flock.c lib/monotonic.c lib/timer.c
 flock_LDADD = $(LDADD) libcommon.la -lrt
 endif
 
index 368ad1a9ff9461c284fc7b60ca65757f5ea3caca..84f1ec4864e13c2ad93d98463544a0ab211e48e0 100644 (file)
@@ -44,6 +44,7 @@
 #include "strutils.h"
 #include "closestream.h"
 #include "monotonic.h"
+#include "timer.h"
 
 static void __attribute__((__noreturn__)) usage(int ex)
 {