Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Socketdir, monotonic and Hurd #282

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
23 changes: 7 additions & 16 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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 <time.h>]],
[[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 \
Expand Down
4 changes: 0 additions & 4 deletions include/os_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/ipc_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
10 changes: 6 additions & 4 deletions lib/ipc_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/ipc_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,23 @@ 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
}
fd = open_mmap_file(path, file_flags);
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;
Expand Down
44 changes: 25 additions & 19 deletions lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/_syslog_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef QB_SYSLOG_OVERRIDE_H_DEFINED
#define QB_SYSLOG_OVERRIDE_H_DEFINED

#include <limits.h>
#include "os_base.h"

extern int _syslog_opened;
extern int _syslog_option;
Expand Down