diff --git a/configure.ac b/configure.ac index 540712b7e..ca998ee72 100644 --- a/configure.ac +++ b/configure.ac @@ -112,6 +112,11 @@ esac # Checks for libraries. AX_PTHREAD(,[AC_MSG_ERROR([POSIX threads support is required])]) AX_SAVE_FLAGS +AC_SEARCH_LIBS([clock_gettime],[rt]) +AC_CHECK_FUNCS([clock_gettime]) +AC_SUBST([rt_LIBS],[$LIBS]) +AX_RESTORE_FLAGS +AX_SAVE_FLAGS AC_SEARCH_LIBS([dlopen],[dl],,[AC_MSG_ERROR([cannot find dlopen() function])]) AC_SUBST([dlopen_LIBS],[$LIBS]) AX_RESTORE_FLAGS @@ -143,7 +148,7 @@ AC_HEADER_SYS_WAIT AC_CHECK_HEADERS([arpa/inet.h link.h fcntl.h inttypes.h limits.h netinet/in.h \ stdint.h stddef.h stdlib.h string.h strings.h \ - dlfcn.h time.h sys/time.h sys/types.h sys/stat.h \ + dlfcn.h time.h sys/types.h sys/stat.h \ sys/param.h sys/socket.h sys/time.h sys/poll.h sys/epoll.h \ sys/uio.h sys/event.h sys/sockio.h sys/un.h sys/resource.h \ syslog.h errno.h unistd.h sys/mman.h \ @@ -186,26 +191,12 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM( ] ) -AC_MSG_CHECKING(for a working clock_getres(CLOCK_MONOTONIC, &ts)) -AC_COMPILE_IFELSE([AC_LANG_PROGRAM( -[[#include ]], -[[struct timespec ts; if(clock_getres(CLOCK_MONOTONIC, &ts)) return -1;]])], - [ - AC_MSG_RESULT([yes]) - AC_DEFINE_UNQUOTED([HAVE_CLOCK_GETRES_MONOTONIC], 1, [Define to 1 if clock_getres(CLOCK_MONOTONIC, &ts) works]) - ], - [ - AC_MSG_RESULT([no]) - ] - ) - - # Checks for library functions AC_FUNC_CHOWN AC_FUNC_FORK AC_FUNC_MMAP AC_FUNC_STRERROR_R -AC_CHECK_FUNCS([alarm clock_gettime \ +AC_CHECK_FUNCS([alarm \ fsync fdatasync ftruncate \ gettimeofday localtime localtime_r \ memset munmap socket \ diff --git a/include/os_base.h b/include/os_base.h index 82e982b02..e7234175c 100644 --- a/include/os_base.h +++ b/include/os_base.h @@ -99,10 +99,6 @@ #endif /* HAVE_SYSLOG_H */ #endif /* S_SPLINT_S */ -#if defined HAVE_CLOCK_GETTIME && defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 -#define HAVE_MONOTONIC_CLOCK 1 -#endif /* have monotonic clock */ - #ifdef HAVE_EPOLL_CREATE1 #define HAVE_EPOLL 1 #endif /* HAVE_EPOLL_CREATE */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 6ca6b1518..df21004c2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -78,7 +78,7 @@ EXTRA_DIST = qblog_script.ld.in qblog_script.la.in qblog_script_noop.ld libqb_la_SOURCES = $(source_to_lint) unix.c libqb_la_CFLAGS = $(PTHREAD_CFLAGS) -libqb_la_LIBADD = $(LTLIBOBJS) $(dlopen_LIBS) $(PTHREAD_LIBS) $(socket_LIBS) +libqb_la_LIBADD = $(LTLIBOBJS) $(dlopen_LIBS) $(PTHREAD_LIBS) $(socket_LIBS) $(rt_LIBS) if NEED_GCC_ATTRIBUTE_SECTION_WORKAROUND libqb_la_LIBADD += qblog_script.la endif @@ -146,6 +146,7 @@ DEB_INCLUDES = -I/usr/include/x86_64-linux-gnu -I/usr/include/i386-linux-gnu ALL_LINT_FLAGS = $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(DEB_INCLUDES) \ $(libqb_la_CPPFLAGS) $(CPPFLAGS) $(AM_CPPFLAGS) \ + -Dclockid_t=int \ $(LINT_FLAGS) # expected for the time being (but SHOULD be fixed eventually): # ipc_setup.c: (in function qb_ipcc_us_setup_connect) diff --git a/lib/ipc_int.h b/lib/ipc_int.h index 67fc444cb..904b8ecae 100644 --- a/lib/ipc_int.h +++ b/lib/ipc_int.h @@ -206,5 +206,6 @@ int32_t qb_ipcs_process_request(struct qb_ipcs_service *s, int32_t qb_ipc_us_sock_error_is_disconnected(int err); int use_filesystem_sockets(void); +const char *qb_socketdir(void); #endif /* QB_IPC_INT_H_DEFINED */ diff --git a/lib/ipc_setup.c b/lib/ipc_setup.c index 57d755b4f..434c89fd4 100644 --- a/lib/ipc_setup.c +++ b/lib/ipc_setup.c @@ -288,7 +288,7 @@ qb_ipcc_stream_sock_connect(const char *socket_name, int32_t * sock_pt) if (!use_filesystem_sockets()) { snprintf(address.sun_path + 1, UNIX_PATH_MAX - 1, "%s", socket_name); } else { - snprintf(address.sun_path, sizeof(address.sun_path), "%s/%s", SOCKETDIR, + snprintf(address.sun_path, sizeof(address.sun_path), "%s/%s", qb_socketdir(), socket_name); } @@ -541,15 +541,17 @@ qb_ipcs_us_publish(struct qb_ipcs_service * s) } else { struct stat stat_out; - res = stat(SOCKETDIR, &stat_out); + const char *socketdir = qb_socketdir(); + + res = stat(socketdir, &stat_out); if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) { res = -errno; qb_util_log(LOG_CRIT, "Required directory not present %s", - SOCKETDIR); + socketdir); goto error_close; } - snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/%s", SOCKETDIR, + snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/%s", socketdir, s->name); unlink(un_addr.sun_path); } diff --git a/lib/ipc_socket.c b/lib/ipc_socket.c index fe2040e2a..aa1c7df8c 100644 --- a/lib/ipc_socket.c +++ b/lib/ipc_socket.c @@ -62,6 +62,12 @@ int use_filesystem_sockets(void) return filesystem_sockets; } +const char *qb_socketdir(void) +{ + const char *socketdir = getenv("QB_SOCKET_DIR"); + return socketdir ? socketdir : SOCKETDIR; +} + static void set_sock_addr(struct sockaddr_un *address, const char *socket_name) { @@ -74,7 +80,7 @@ set_sock_addr(struct sockaddr_un *address, const char *socket_name) if (!use_filesystem_sockets()) { snprintf(address->sun_path + 1, UNIX_PATH_MAX - 1, "%s", socket_name); } else { - snprintf(address->sun_path, sizeof(address->sun_path), "%s/%s", SOCKETDIR, + snprintf(address->sun_path, sizeof(address->sun_path), "%s/%s", qb_socketdir(), socket_name); } } diff --git a/lib/unix.c b/lib/unix.c index 3c8f327c8..c0542c897 100644 --- a/lib/unix.c +++ b/lib/unix.c @@ -76,14 +76,15 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes, ssize_t written; char *buffer = NULL; char *is_absolute = strchr(file, '/'); + const char *socketdir = qb_socketdir(); if (is_absolute) { (void)strlcpy(path, file, PATH_MAX); } else { -#if defined(QB_LINUX) || defined(QB_CYGWIN) +#if defined(QB_LINUX) || defined(QB_CYGWIN) || defined(QB_GNU) snprintf(path, PATH_MAX, "/dev/shm/%s", file); #else - snprintf(path, PATH_MAX, "%s/%s", SOCKETDIR, file); + snprintf(path, PATH_MAX, "%s/%s", socketdir, file); is_absolute = path; #endif } @@ -91,7 +92,7 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes, if (fd < 0 && !is_absolute) { qb_util_perror(LOG_ERR, "couldn't open file %s", path); - snprintf(path, PATH_MAX, "%s/%s", SOCKETDIR, file); + snprintf(path, PATH_MAX, "%s/%s", socketdir, file); fd = open_mmap_file(path, file_flags); if (fd < 0) { res = -errno; diff --git a/lib/util.c b/lib/util.c index a510bd1c9..5e3d25149 100644 --- a/lib/util.c +++ b/lib/util.c @@ -136,29 +136,40 @@ qb_timespec_add_ms(struct timespec *ts, int32_t ms) #endif /* S_SPLINT_S */ } -#ifdef HAVE_MONOTONIC_CLOCK +#ifdef HAVE_CLOCK_GETTIME + +static void +qb_util_clock_gettime_fallback (clockid_t clock, struct timespec *tsp) +{ + memset(tsp, 0, sizeof *tsp); + if (clock_gettime(clock, tsp) != 0 && + clock_gettime(CLOCK_REALTIME, tsp) != 0) { + qb_util_perror(LOG_ERR, "clock_gettime(CLOCK_REALTIME)"); + } +} + uint64_t qb_util_nano_current_get(void) { uint64_t nano_monotonic; struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + qb_util_clock_gettime_fallback(CLOCK_MONOTONIC, &ts); nano_monotonic = (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec; return (nano_monotonic); } +#ifndef CLOCK_REALTIME_COARSE +#define CLOCK_REALTIME_COARSE CLOCK_REALTIME +#endif + uint64_t qb_util_nano_from_epoch_get(void) { uint64_t nano_monotonic; struct timespec ts; -#ifdef CLOCK_REALTIME_COARSE - clock_gettime(CLOCK_REALTIME_COARSE, &ts); -#else - clock_gettime(CLOCK_REALTIME, &ts); -#endif + qb_util_clock_gettime_fallback(CLOCK_REALTIME_COARSE, &ts); nano_monotonic = (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec; return (nano_monotonic); @@ -170,12 +181,11 @@ qb_util_nano_monotonic_hz(void) uint64_t nano_monotonic_hz; struct timespec ts; -#if HAVE_CLOCK_GETRES_MONOTONIC - clock_getres(CLOCK_MONOTONIC, &ts); -#else - if (clock_getres(CLOCK_REALTIME, &ts) != 0) - qb_util_perror(LOG_ERR,"CLOCK_REALTIME"); -#endif + memset(&ts, 0, sizeof ts); + if (clock_getres(CLOCK_MONOTONIC, &ts) != 0 && + clock_getres(CLOCK_REALTIME, &ts) != 0) { + qb_util_perror(LOG_ERR, "clock_getres(CLOCK_REALTIME)"); + } nano_monotonic_hz = QB_TIME_NS_IN_SEC / ((ts.tv_sec * QB_TIME_NS_IN_SEC) + ts.tv_nsec); @@ -186,11 +196,7 @@ qb_util_nano_monotonic_hz(void) void qb_util_timespec_from_epoch_get(struct timespec *ts) { -#ifdef CLOCK_REALTIME_COARSE - clock_gettime(CLOCK_REALTIME_COARSE, ts); -#else - clock_gettime(CLOCK_REALTIME, ts); -#endif + qb_util_clock_gettime_fallback(CLOCK_REALTIME_COARSE, ts); } #else @@ -230,7 +236,7 @@ qb_util_nano_from_epoch_get(void) return (nano_from_epoch); } -#endif /* HAVE_MONOTONIC_CLOCK */ +#endif /* HAVE_CLOCK_GETTIME */ struct qb_util_stopwatch { uint64_t started; diff --git a/tests/_syslog_override.h b/tests/_syslog_override.h index 7b254aea0..923caeaa7 100644 --- a/tests/_syslog_override.h +++ b/tests/_syslog_override.h @@ -24,7 +24,7 @@ #ifndef QB_SYSLOG_OVERRIDE_H_DEFINED #define QB_SYSLOG_OVERRIDE_H_DEFINED -#include +#include "os_base.h" extern int _syslog_opened; extern int _syslog_option;