lib/timer: use separate file for timers

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>
This commit is contained in:
Karel Zak 2015-03-06 13:17:20 +01:00
parent f33383f7c2
commit 26e8964b9d
7 changed files with 76 additions and 60 deletions

View File

@ -4,10 +4,9 @@ dist_noinst_HEADERS += \
include/at.h \ include/at.h \
include/bitops.h \ include/bitops.h \
include/blkdev.h \ include/blkdev.h \
include/monotonic.h \
include/c.h \
include/canonicalize.h \ include/canonicalize.h \
include/carefulputc.h \ include/carefulputc.h \
include/c.h \
include/closestream.h \ include/closestream.h \
include/colors.h \ include/colors.h \
include/cpuset.h \ include/cpuset.h \
@ -28,6 +27,7 @@ dist_noinst_HEADERS += \
include/mbsalign.h \ include/mbsalign.h \
include/md5.h \ include/md5.h \
include/minix.h \ include/minix.h \
include/monotonic.h \
include/namespace.h \ include/namespace.h \
include/nls.h \ include/nls.h \
include/optutils.h \ include/optutils.h \
@ -36,21 +36,22 @@ dist_noinst_HEADERS += \
include/path.h \ include/path.h \
include/pathnames.h \ include/pathnames.h \
include/procutils.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/randutils.h \
include/readutmp.h \ include/readutmp.h \
include/rpmatch.h \ include/rpmatch.h \
include/setproctitle.h \ include/setproctitle.h \
include/statfs_magic.h
include/strutils.h \ include/strutils.h \
include/swapprober.h \
include/swapheader.h \ include/swapheader.h \
include/swapprober.h \
include/sysfs.h \ include/sysfs.h \
include/timer.h \
include/timeutils.h \ include/timeutils.h \
include/ttyutils.h \ include/ttyutils.h \
include/widechar.h \ include/widechar.h \
include/xalloc.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

View File

@ -1,18 +1,10 @@
#ifndef UTIL_LINUX_BOOTTIME_H #ifndef UTIL_LINUX_MONOTONIC_H
#define UTIL_LINUX_BOOTTIME_H #define UTIL_LINUX_MONOTONIC_H
/*
* Uses clock_gettime() that requires $CLOCKGETTIME_LIBS
*/
#include <signal.h>
#include <sys/time.h> #include <sys/time.h>
extern int get_boot_time(struct timeval *boot_time); extern int get_boot_time(struct timeval *boot_time);
extern int gettime_monotonic(struct timeval *tv); extern int gettime_monotonic(struct timeval *tv);
extern int setup_timer(timer_t * t_id, struct itimerval *timeout, #endif /* UTIL_LINUX_MONOTONIC_H */
void (*timeout_handler)(int, siginfo_t *, void *));
extern void cancel_timer(timer_t * t_id);
#endif /* UTIL_LINUX_BOOTTIME_H */

11
include/timer.h Normal file
View File

@ -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 */

View File

@ -8,7 +8,6 @@
#include <sys/time.h> #include <sys/time.h>
#include "c.h" #include "c.h"
#include "nls.h"
#include "monotonic.h" #include "monotonic.h"
int get_boot_time(struct timeval *boot_time) int get_boot_time(struct timeval *boot_time)
@ -22,10 +21,8 @@ int get_boot_time(struct timeval *boot_time)
struct sysinfo info; struct sysinfo info;
#endif #endif
if (gettimeofday(&now, NULL) != 0) { if (gettimeofday(&now, NULL) != 0)
warn(_("gettimeofday failed"));
return -errno; return -errno;
}
#ifdef CLOCK_BOOTTIME #ifdef CLOCK_BOOTTIME
if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) { if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) {
TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime); TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
@ -36,7 +33,7 @@ int get_boot_time(struct timeval *boot_time)
#ifdef HAVE_SYSINFO #ifdef HAVE_SYSINFO
/* fallback */ /* fallback */
if (sysinfo(&info) != 0) if (sysinfo(&info) != 0)
warn(_("sysinfo failed")); return -errno;
boot_time->tv_sec = now.tv_sec - info.uptime; boot_time->tv_sec = now.tv_sec - info.uptime;
boot_time->tv_usec = 0; boot_time->tv_usec = 0;
@ -68,37 +65,4 @@ int gettime_monotonic(struct timeval *tv)
#endif #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);
}

47
lib/timer.c Normal file
View File

@ -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);
}

View File

@ -1,7 +1,7 @@
if BUILD_FLOCK if BUILD_FLOCK
usrbin_exec_PROGRAMS += flock usrbin_exec_PROGRAMS += flock
dist_man_MANS += sys-utils/flock.1 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 flock_LDADD = $(LDADD) libcommon.la -lrt
endif endif

View File

@ -44,6 +44,7 @@
#include "strutils.h" #include "strutils.h"
#include "closestream.h" #include "closestream.h"
#include "monotonic.h" #include "monotonic.h"
#include "timer.h"
static void __attribute__((__noreturn__)) usage(int ex) static void __attribute__((__noreturn__)) usage(int ex)
{ {