From 6dd3666c62e6508289b40a2a3abd3a465197cefc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 13 May 2017 11:26:55 -0400 Subject: [PATCH] tree-wide: when %m is used in log_*, always specify errno explicitly All those uses were correct, but I think it's better to be explicit. Using implicit errno is too error prone, and with this change we can require (in the sense of a style guideline) that the code is always specified. Helpful query: git grep -n -P 'log_[^s][a-z]+\(.*%m' --- src/basic/selinux-util.c | 22 ++++++++++++++++------ src/core/cgroup.c | 3 +-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/basic/selinux-util.c b/src/basic/selinux-util.c index 79ce6a856..41c8297f9 100644 --- a/src/basic/selinux-util.c +++ b/src/basic/selinux-util.c @@ -50,10 +50,10 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free); static int cached_use = -1; static struct selabel_handle *label_hnd = NULL; -#define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__) +#define log_enforcing(...) log_full_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, errno, __VA_ARGS__) #endif -bool mac_selinux_use(void) { +bool mac_selinux_have(void) { #ifdef HAVE_SELINUX if (cached_use < 0) cached_use = is_selinux_enabled() > 0; @@ -64,6 +64,16 @@ bool mac_selinux_use(void) { #endif } +bool mac_selinux_use(void) { + if (!mac_selinux_have()) + return false; + + /* Never try to configure SELinux features if we aren't + * root */ + + return getuid() == 0; +} + void mac_selinux_retest(void) { #ifdef HAVE_SELINUX cached_use = -1; @@ -196,7 +206,7 @@ int mac_selinux_get_create_label_from_exe(const char *exe, char **label) { assert(exe); assert(label); - if (!mac_selinux_use()) + if (!mac_selinux_have()) return -EOPNOTSUPP; r = getcon_raw(&mycon); @@ -222,7 +232,7 @@ int mac_selinux_get_our_label(char **label) { assert(label); #ifdef HAVE_SELINUX - if (!mac_selinux_use()) + if (!mac_selinux_have()) return -EOPNOTSUPP; r = getcon_raw(label); @@ -246,7 +256,7 @@ int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char * assert(exe); assert(label); - if (!mac_selinux_use()) + if (!mac_selinux_have()) return -EOPNOTSUPP; r = getcon_raw(&mycon); @@ -301,7 +311,7 @@ char* mac_selinux_free(char *label) { if (!label) return NULL; - if (!mac_selinux_use()) + if (!mac_selinux_have()) return NULL; diff --git a/src/core/cgroup.c b/src/core/cgroup.c index bda1d5349..9fa81eaaa 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -399,8 +399,7 @@ static int whitelist_major(const char *path, const char *name, char type, const return 0; fail: - log_warning_errno(errno, "Failed to read /proc/devices: %m"); - return -errno; + return log_warning_errno(errno, "Failed to read /proc/devices: %m"); } static bool cgroup_context_has_cpu_weight(CGroupContext *c) { -- 2.30.2