]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-utils: Expose FORMAT_UID and FORMAT_GID
authorAdrian Vovk <adrianvovk@gmail.com>
Fri, 2 Feb 2024 20:03:54 +0000 (15:03 -0500)
committerLennart Poettering <lennart@poettering.net>
Wed, 14 Feb 2024 08:14:13 +0000 (09:14 +0100)
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.

src/basic/format-util.h
src/basic/mkdir.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-login/sd-login.c
src/sysusers/sysusers.c

index 8719df3e299c4b681fa306427c01cc2bd904ed53..ba7cff6a8b5505035712e582ce688cb3343ecf31 100644 (file)
@@ -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
index c770e5ed32eac0f3c99c89c71406d4a6c72e9ad6..5dd56c89a4ed8f08fdeb823ecb110ca793d4e060 100644 (file)
@@ -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;
 }
index 2ad29af10557eaa5e2560a3235f2c8936bf6cb3d..a25369e930b994082a729b5f65ac2a98f5bf0780 100644 (file)
@@ -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)
index c72fccc229e9d30ba1c21074055e117553b2a695..4d91ba96f6c348dbe900966ea2399d5a7afce74e 100644 (file)
@@ -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) {
index 8e90438ce0388c3b313df90982cd694a0ae84a8f..6e28b1cf8ffde153ae23a8a94df81b4f5e652433 100644 (file)
@@ -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;
         }