From: Adrian Vovk Date: Fri, 2 Feb 2024 20:03:54 +0000 (-0500) Subject: format-utils: Expose FORMAT_UID and FORMAT_GID X-Git-Tag: v256-rc1~877 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80d07162e7cc87455d7b396b3b74fa5ff0391c89;p=thirdparty%2Fsystemd.git format-utils: Expose FORMAT_UID and FORMAT_GID This pulls this generally useful helper out of sysusers and into the util lib, and updates the places throughout the codebase where it makes sense to use it. --- diff --git a/src/basic/format-util.h b/src/basic/format-util.h index 8719df3e299..ba7cff6a8b5 100644 --- a/src/basic/format-util.h +++ b/src/basic/format-util.h @@ -18,6 +18,14 @@ assert_cc(sizeof(uid_t) == sizeof(uint32_t)); assert_cc(sizeof(gid_t) == sizeof(uint32_t)); #define GID_FMT "%" PRIu32 +/* Note: the lifetime of the compound literal is the immediately surrounding block, + * see C11 §6.5.2.5, and + * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ +#define FORMAT_UID(uid) \ + snprintf_ok((char[DECIMAL_STR_MAX(uid_t)]){}, DECIMAL_STR_MAX(uid_t), UID_FMT, uid) +#define FORMAT_GID(gid) \ + snprintf_ok((char[DECIMAL_STR_MAX(gid_t)]){}, DECIMAL_STR_MAX(gid_t), GID_FMT, gid) + #if SIZEOF_TIME_T == 8 # define PRI_TIME PRIi64 #elif SIZEOF_TIME_T == 4 diff --git a/src/basic/mkdir.c b/src/basic/mkdir.c index c770e5ed32e..5dd56c89a4e 100644 --- a/src/basic/mkdir.c +++ b/src/basic/mkdir.c @@ -70,17 +70,11 @@ int mkdirat_safe_internal( path, st.st_mode & 0777, mode); if ((uid != UID_INVALID && st.st_uid != uid) || - (gid != GID_INVALID && st.st_gid != gid)) { - char u[DECIMAL_STR_MAX(uid_t)] = "-", g[DECIMAL_STR_MAX(gid_t)] = "-"; - - if (uid != UID_INVALID) - xsprintf(u, UID_FMT, uid); - if (gid != UID_INVALID) - xsprintf(g, GID_FMT, gid); + (gid != GID_INVALID && st.st_gid != gid)) return log_full_errno(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, SYNTHETIC_ERRNO(EEXIST), "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.", - path, st.st_uid, st.st_gid, u, g); - } + path, st.st_uid, st.st_gid, uid != UID_INVALID ? FORMAT_UID(uid) : "-", + gid != UID_INVALID ? FORMAT_GID(gid) : "-"); return 0; } diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 2ad29af1055..a25369e930b 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -30,6 +30,7 @@ #include "constants.h" #include "errno-util.h" #include "fd-util.h" +#include "format-util.h" #include "glyph-util.h" #include "hexdecoct.h" #include "hostname-util.h" @@ -1672,10 +1673,7 @@ static int user_and_machine_equivalent(const char *user_and_machine) { return true; /* Otherwise, we have to figure out our user id and name, and compare things with that. */ - char buf[DECIMAL_STR_MAX(uid_t)]; - xsprintf(buf, UID_FMT, uid); - - f = startswith(user_and_machine, buf); + f = startswith(user_and_machine, FORMAT_UID(uid)); if (!f) { un = getusername_malloc(); if (!un) diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index c72fccc229e..4d91ba96f6c 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -579,10 +579,7 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) if (isempty(content)) return 0; - char t[DECIMAL_STR_MAX(uid_t)]; - xsprintf(t, UID_FMT, uid); - - return string_contains_word(content, NULL, t); + return string_contains_word(content, NULL, FORMAT_UID(uid)); } static int uid_get_array(uid_t uid, const char *variable, char ***array) { diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 8e90438ce03..6e28b1cf8ff 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -141,14 +141,6 @@ static void context_done(Context *c) { uid_range_free(c->uid_range); } -/* Note: the lifetime of the compound literal is the immediately surrounding block, - * see C11 §6.5.2.5, and - * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ -#define FORMAT_UID(is_set, uid) \ - ((is_set) ? snprintf_ok((char[DECIMAL_STR_MAX(uid_t)]){}, DECIMAL_STR_MAX(uid_t), UID_FMT, uid) : "(unset)") -#define FORMAT_GID(is_set, gid) \ - ((is_set) ? snprintf_ok((char[DECIMAL_STR_MAX(gid_t)]){}, DECIMAL_STR_MAX(gid_t), GID_FMT, gid) : "(unset)") - static void maybe_emit_login_defs_warning(Context *c) { assert(c); @@ -1620,7 +1612,8 @@ static int item_equivalent(Item *a, Item *b) { (a->uid_set && a->uid != b->uid)) { log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0, "Item not equivalent because UIDs differ (%s vs. %s)", - FORMAT_UID(a->uid_set, a->uid), FORMAT_UID(b->uid_set, b->uid)); + a->uid_set ? FORMAT_UID(a->uid) : "(unset)", + b->uid_set ? FORMAT_UID(b->uid) : "(unset)"); return false; } @@ -1628,7 +1621,8 @@ static int item_equivalent(Item *a, Item *b) { (a->gid_set && a->gid != b->gid)) { log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0, "Item not equivalent because GIDs differ (%s vs. %s)", - FORMAT_GID(a->gid_set, a->gid), FORMAT_GID(b->gid_set, b->gid)); + a->gid_set ? FORMAT_GID(a->gid) : "(unset)", + b->gid_set ? FORMAT_GID(b->gid) : "(unset)"); return false; }