From 343f97460fd536d74e6627b20413685bbc857576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 16 Jun 2019 18:12:45 +0200 Subject: Use #if instead of #ifdef for compatibility checks This has the advantage that it is slightly less verbose, and also any potential typos in the macro names are immediately detected. --- systemd/_daemon.c | 30 ++++++++++-------------------- systemd/_reader.c | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/systemd/_daemon.c b/systemd/_daemon.c index 6b0b670..e83da48 100644 --- a/systemd/_daemon.c +++ b/systemd/_daemon.c @@ -33,21 +33,11 @@ #include "macro.h" #include "util.h" -#if LIBSYSTEMD_VERSION >= 214 -# define HAVE_PID_NOTIFY -#endif - -#if LIBSYSTEMD_VERSION >= 219 -# define HAVE_PID_NOTIFY_WITH_FDS -#endif +#define HAVE_PID_NOTIFY (LIBSYSTEMD_VERSION >= 214) +#define HAVE_PID_NOTIFY_WITH_FDS (LIBSYSTEMD_VERSION >= 219) +#define HAVE_SD_LISTEN_FDS_WITH_NAMES (LIBSYSTEMD_VERSION >= 227) +#define HAVE_IS_SOCKET_SOCKADDR (LIBSYSTEMD_VERSION >= 233) -#if LIBSYSTEMD_VERSION >= 227 -# define HAVE_SD_LISTEN_FDS_WITH_NAMES -#endif - -#if LIBSYSTEMD_VERSION >= 233 -# define HAVE_IS_SOCKET_SOCKADDR -#endif PyDoc_STRVAR(module__doc__, "Python interface to the libsystemd-daemon library.\n\n" @@ -152,14 +142,14 @@ static PyObject* notify(PyObject *self, PyObject *args, PyObject *keywds) { if (pid == 0 && !fds) r = sd_notify(unset, msg); else if (!fds) { -#ifdef HAVE_PID_NOTIFY +#if HAVE_PID_NOTIFY r = sd_pid_notify(pid, unset, msg); #else set_error(-ENOSYS, NULL, "Compiled without support for sd_pid_notify"); return NULL; #endif } else { -#ifdef HAVE_PID_NOTIFY_WITH_FDS +#if HAVE_PID_NOTIFY_WITH_FDS r = sd_pid_notify_with_fds(pid, unset, msg, arr, n_fds); #else set_error(-ENOSYS, NULL, "Compiled without support for sd_pid_notify_with_fds"); @@ -211,7 +201,7 @@ static PyObject* listen_fds(PyObject *self, PyObject *args, PyObject *keywds) { PyDoc_STRVAR(listen_fds_with_names__doc__, "_listen_fds_with_names(unset_environment=True) -> (int, str...)\n\n" "Wraps sd_listen_fds_with_names(3).\n" -#ifdef HAVE_SD_LISTEN_FDS_WITH_NAMES +#if HAVE_SD_LISTEN_FDS_WITH_NAMES "Return the number of descriptors passed to this process by the init system\n" "and their names as part of the socket-based activation logic.\n" #else @@ -248,7 +238,7 @@ static PyObject* listen_fds_with_names(PyObject *self, PyObject *args, PyObject return NULL; #endif -#ifdef HAVE_SD_LISTEN_FDS_WITH_NAMES +#if HAVE_SD_LISTEN_FDS_WITH_NAMES r = sd_listen_fds_with_names(unset, &names); if (set_error(r, NULL, NULL) < 0) return NULL; @@ -398,7 +388,7 @@ static PyObject* is_socket_inet(PyObject *self, PyObject *args) { PyDoc_STRVAR(is_socket_sockaddr__doc__, "_is_socket_sockaddr(fd, address, type=0, flowinfo=0, listening=-1) -> bool\n\n" "Wraps sd_is_socket_inet_sockaddr(3).\n" -#ifdef HAVE_IS_SOCKET_SOCKADDR +#if HAVE_IS_SOCKET_SOCKADDR "`address` is a systemd-style numerical IPv4 or IPv6 address as used in\n" "ListenStream=. A port may be included after a colon (\":\"). See\n" "systemd.socket(5) for details.\n\n" @@ -438,7 +428,7 @@ static PyObject* is_socket_sockaddr(PyObject *self, PyObject *args) { addr.in6.sin6_flowinfo = flowinfo; } -#ifdef HAVE_IS_SOCKET_SOCKADDR +#if HAVE_IS_SOCKET_SOCKADDR r = sd_is_socket_sockaddr(fd, type, &addr.sa, addr_len, listening); if (set_error(r, NULL, NULL) < 0) return NULL; diff --git a/systemd/_reader.c b/systemd/_reader.c index e8ebc56..6803805 100644 --- a/systemd/_reader.c +++ b/systemd/_reader.c @@ -32,22 +32,22 @@ #include "strv.h" #if defined(LIBSYSTEMD_VERSION) || LIBSYSTEMD_JOURNAL_VERSION > 204 -# define HAVE_JOURNAL_OPEN_FILES +# define HAVE_JOURNAL_OPEN_FILES 1 #else # define SD_JOURNAL_SYSTEM (1 << 2) # define SD_JOURNAL_CURRENT_USER (1 << 3) +# define HAVE_JOURNAL_OPEN_FILES 0 #endif -#if LIBSYSTEMD_VERSION >= 229 -# define HAVE_ENUMERATE_FIELDS -# define HAVE_HAS_RUNTIME_FILES -# define HAVE_HAS_PERSISTENT_FILES -#endif +#define HAVE_ENUMERATE_FIELDS (LIBSYSTEMD_VERSION >= 229) +#define HAVE_HAS_RUNTIME_FILES (LIBSYSTEMD_VERSION >= 229) +#define HAVE_HAS_PERSISTENT_FILES (LIBSYSTEMD_VERSION >= 229) #if LIBSYSTEMD_VERSION >= 230 -# define HAVE_JOURNAL_OPEN_DIRECTORY_FD +# define HAVE_JOURNAL_OPEN_DIRECTORY_FD 1 #else # define SD_JOURNAL_OS_ROOT (1 << 4) +# define HAVE_JOURNAL_OPEN_DIRECTORY_FD 0 #endif typedef struct { @@ -263,7 +263,7 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds) { if (long_as_fd(_path, &fd) < 0) return -1; -#ifdef HAVE_JOURNAL_OPEN_DIRECTORY_FD +#if HAVE_JOURNAL_OPEN_DIRECTORY_FD Py_BEGIN_ALLOW_THREADS r = sd_journal_open_directory_fd(&self->j, (int) fd, flags); Py_END_ALLOW_THREADS @@ -292,7 +292,7 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds) { if (!strv_converter(_files, &files)) return -1; -#ifdef HAVE_JOURNAL_OPEN_FILES +#if HAVE_JOURNAL_OPEN_FILES Py_BEGIN_ALLOW_THREADS r = sd_journal_open_files(&self->j, (const char**) files, flags); Py_END_ALLOW_THREADS @@ -306,7 +306,7 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds) { if (!intlist_converter(_files, &fds, &n_fds)) return -1; -#ifdef HAVE_JOURNAL_OPEN_DIRECTORY_FD +#if HAVE_JOURNAL_OPEN_DIRECTORY_FD Py_BEGIN_ALLOW_THREADS r = sd_journal_open_files_fd(&self->j, fds, n_fds, flags); Py_END_ALLOW_THREADS @@ -1016,7 +1016,7 @@ PyDoc_STRVAR(Reader_enumerate_fields__doc__, "Return a set of field names appearing in the journal.\n" "See sd_journal_enumerate_fields(3)."); static PyObject* Reader_enumerate_fields(Reader *self, PyObject *args) { -#ifdef HAVE_ENUMERATE_FIELDS +#if HAVE_ENUMERATE_FIELDS _cleanup_Py_DECREF_ PyObject *_value_set = NULL; PyObject *value_set; int r; @@ -1058,7 +1058,7 @@ PyDoc_STRVAR(Reader_has_runtime_files__doc__, "Returns true if runtime journal files have been found.\n\n" "See man:sd_journal_test_cursor(3)."); static PyObject* Reader_has_runtime_files(Reader *self, PyObject *args) { -#ifdef HAVE_ENUMERATE_FIELDS +#if HAVE_ENUMERATE_FIELDS int r; assert(self); @@ -1079,7 +1079,7 @@ PyDoc_STRVAR(Reader_has_persistent_files__doc__, "Returns true if persistent journal files have been found.\n\n" "See man:sd_journal_test_cursor(3)."); static PyObject* Reader_has_persistent_files(Reader *self, PyObject *args) { -#ifdef HAVE_ENUMERATE_FIELDS +#if HAVE_ENUMERATE_FIELDS int r; assert(self); -- cgit v1.2.1