diff options
-rw-r--r-- | include/Makemodule.am | 21 | ||||
-rw-r--r-- | include/monotonic.h | 14 | ||||
-rw-r--r-- | include/timer.h | 11 | ||||
-rw-r--r-- | lib/monotonic.c | 40 | ||||
-rw-r--r-- | lib/timer.c | 47 | ||||
-rw-r--r-- | sys-utils/Makemodule.am | 2 | ||||
-rw-r--r-- | sys-utils/flock.c | 1 |
7 files changed, 76 insertions, 60 deletions
diff --git a/include/Makemodule.am b/include/Makemodule.am index 8d7c881d5..e5fb6d3d0 100644 --- a/include/Makemodule.am +++ b/include/Makemodule.am @@ -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 diff --git a/include/monotonic.h b/include/monotonic.h index bbebfe0b9..a499fa34a 100644 --- a/include/monotonic.h +++ b/include/monotonic.h @@ -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 index 000000000..aa9f9c018 --- /dev/null +++ b/include/timer.h @@ -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 */ diff --git a/lib/monotonic.c b/lib/monotonic.c index c6e0f7572..f2aa0de0d 100644 --- a/lib/monotonic.c +++ b/lib/monotonic.c @@ -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 index 000000000..0ad0cad78 --- /dev/null +++ b/lib/timer.c @@ -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); +} diff --git a/sys-utils/Makemodule.am b/sys-utils/Makemodule.am index bd1b681e8..e06e34a04 100644 --- a/sys-utils/Makemodule.am +++ b/sys-utils/Makemodule.am @@ -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 diff --git a/sys-utils/flock.c b/sys-utils/flock.c index 368ad1a9f..84f1ec486 100644 --- a/sys-utils/flock.c +++ b/sys-utils/flock.c @@ -44,6 +44,7 @@ #include "strutils.h" #include "closestream.h" #include "monotonic.h" +#include "timer.h" static void __attribute__((__noreturn__)) usage(int ex) { |