From c167c1705c552c4c0a257df608ad65436a9e58b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Thu, 12 Oct 2017 14:50:23 +0200 Subject: [PATCH 1/9] hurd: definition of PATH_MAX must be included separately MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Wágner --- tests/_syslog_override.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From e4a70b9fb4a1ff03ed8e757a66569d0217d04f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Thu, 12 Oct 2017 15:13:25 +0200 Subject: [PATCH 2/9] hurd: /dev/shm is a tmpfs here as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Wágner --- lib/unix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unix.c b/lib/unix.c index 3c8f327c8..c31145ec3 100644 --- a/lib/unix.c +++ b/lib/unix.c @@ -80,7 +80,7 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes, 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); From 869d3255175eb4a32bb1d12416a3e5b3f6761e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Tue, 19 Dec 2017 23:51:11 +0100 Subject: [PATCH 3/9] clock_gettime and clock_getres need -lrt on glibc versions before 2.17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Wágner --- configure.ac | 7 ++++++- lib/Makefile.am | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 540712b7e..2dff29899 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 @@ -205,7 +210,7 @@ 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/lib/Makefile.am b/lib/Makefile.am index 6ca6b1518..19878bdcc 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 From 48b093401ad41c9a25ab32fb3ec138354b6cd90c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Thu, 12 Oct 2017 15:57:23 +0200 Subject: [PATCH 4/9] Monotonic clock support requires runtime check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _POSIX_MONOTONIC_CLOCK is defined as 0 on Linux, calling for a runtime check. After that we fall back to CLOCK_REALTIME if needed. In this commit for clock_getres only, which failed on Hurd glibc until 2.24-18 because of an omission: other functions already had an internal fallback to CLOCK_REALTIME. CLOCK_REALTIME is guaranteed to work by POSIX.1-2001; CLOCK_MONOTONIC is only guaranteed to be a known clockid_t. Signed-off-by: Ferenc Wágner --- configure.ac | 14 -------------- lib/util.c | 11 +++++------ 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index 2dff29899..b73155508 100644 --- a/configure.ac +++ b/configure.ac @@ -191,20 +191,6 @@ 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 diff --git a/lib/util.c b/lib/util.c index a510bd1c9..3dd3a5af4 100644 --- a/lib/util.c +++ b/lib/util.c @@ -170,12 +170,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); From dd0231b43a9cd73ef9671825a08eef92db76616f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Wed, 20 Dec 2017 01:10:24 +0100 Subject: [PATCH 5/9] _POSIX_MONOTONIC_CLOCK >= 0 does not guarantee a working monotonic clock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Wágner --- include/os_base.h | 4 ---- lib/util.c | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) 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/util.c b/lib/util.c index 3dd3a5af4..ed8eae3ec 100644 --- a/lib/util.c +++ b/lib/util.c @@ -136,7 +136,7 @@ qb_timespec_add_ms(struct timespec *ts, int32_t ms) #endif /* S_SPLINT_S */ } -#ifdef HAVE_MONOTONIC_CLOCK +#ifdef HAVE_CLOCK_GETTIME uint64_t qb_util_nano_current_get(void) { @@ -229,7 +229,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; From 297e2e280bf63661781ad9ce58346e7a5d095ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Wed, 20 Dec 2017 03:04:37 +0100 Subject: [PATCH 6/9] Implement runtime fallbacks for all clock_gettime calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Wágner --- lib/util.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/util.c b/lib/util.c index ed8eae3ec..5e3d25149 100644 --- a/lib/util.c +++ b/lib/util.c @@ -137,28 +137,39 @@ qb_timespec_add_ms(struct timespec *ts, int32_t ms) } #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); @@ -185,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 From 6ca9224f8a348e9b69a609ba9366cc41369f0a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Wed, 20 Dec 2017 17:46:27 +0100 Subject: [PATCH 7/9] Don't check twice for sys/time.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Wágner --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index b73155508..ca998ee72 100644 --- a/configure.ac +++ b/configure.ac @@ -148,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 \ From 622db9f1ef7c2f0b90a33b254d7782d868ca8177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Wed, 20 Dec 2017 21:45:22 +0100 Subject: [PATCH 8/9] Use QB_SOCKET_DIR as an environmental override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It could also take over the role of the force-filesystem-sockets file. Signed-off-by: Ferenc Wágner --- lib/ipc_int.h | 1 + lib/ipc_setup.c | 10 ++++++---- lib/ipc_socket.c | 8 +++++++- lib/unix.c | 5 +++-- 4 files changed, 17 insertions(+), 7 deletions(-) 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 c31145ec3..c0542c897 100644 --- a/lib/unix.c +++ b/lib/unix.c @@ -76,6 +76,7 @@ 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); @@ -83,7 +84,7 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes, #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; From 4459fd28e0c7528166ead97b9640d89542dc1fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Thu, 21 Dec 2017 11:27:24 +0100 Subject: [PATCH 9/9] splint: misses clockid_t for some reason --- lib/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Makefile.am b/lib/Makefile.am index 19878bdcc..df21004c2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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)