logind: unify how we cast between uid_t and pointers for hashmap keys
authorLennart Poettering <lennart@poettering.net>
Fri, 9 Jan 2015 15:25:47 +0000 (16:25 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 9 Jan 2015 17:35:36 +0000 (18:35 +0100)
src/login/logind-core.c
src/login/logind-dbus.c
src/login/logind-session.c
src/login/logind-user-dbus.c
src/login/logind-user.c
src/shared/macro.h
src/sysusers/sysusers.c
src/test/test-util.c

index 88694f9..a6ff5ad 100644 (file)
@@ -101,7 +101,7 @@ int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **
         assert(m);
         assert(name);
 
-        u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
+        u = hashmap_get(m->users, UID_TO_PTR(uid));
         if (!u) {
                 u = user_new(m, uid, gid, name);
                 if (!u)
index 3ff6cd3..bc985a3 100644 (file)
@@ -92,7 +92,7 @@ int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid,
                         return r;
         }
 
-        user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
+        user = hashmap_get(m->users, UID_TO_PTR(uid));
         if (!user)
                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user "UID_FMT" known or logged in", uid);
 
@@ -1157,7 +1157,7 @@ static int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *us
                 if (r < 0 && errno != ENOENT)
                         return -errno;
 
-                u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
+                u = hashmap_get(m->users, UID_TO_PTR(uid));
                 if (u)
                         user_add_to_gc_queue(u);
         }
index ea1831d..a51f9f3 100644 (file)
@@ -355,7 +355,7 @@ int session_load(Session *s) {
                         return r;
                 }
 
-                user = hashmap_get(s->manager->users, ULONG_TO_PTR((unsigned long) u));
+                user = hashmap_get(s->manager->users, UID_TO_PTR(u));
                 if (!user) {
                         log_error("User of session %s not known.", s->id);
                         return -ENOENT;
index 717740a..bff42e8 100644 (file)
@@ -270,7 +270,7 @@ int user_object_find(sd_bus *bus, const char *path, const char *interface, void
         if (r < 0)
                 return 0;
 
-        user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
+        user = hashmap_get(m->users, UID_TO_PTR(uid));
         if (!user)
                 return 0;
 
index 9ff1302..49c373b 100644 (file)
@@ -56,7 +56,7 @@ User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
         if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
                 goto fail;
 
-        if (hashmap_put(m->users, ULONG_TO_PTR((unsigned long) uid), u) < 0)
+        if (hashmap_put(m->users, UID_TO_PTR(uid), u) < 0)
                 goto fail;
 
         u->manager = m;
@@ -97,7 +97,7 @@ void user_free(User *u) {
 
         free(u->runtime_path);
 
-        hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid));
+        hashmap_remove(u->manager->users, UID_TO_PTR(u->uid));
 
         free(u->name);
         free(u->state_file);
index 6a57428..daa42c4 100644 (file)
@@ -275,6 +275,14 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
 #define PTR_TO_SIZE(p) ((size_t) ((uintptr_t) (p)))
 #define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u)))
 
+/* The following macros add 1 when converting things, since UID 0 is a
+ * valid UID, while the pointer NULL is special */
+#define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
+#define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
+
+#define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
+#define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
+
 #define memzero(x,l) (memset((x), 0, (l)))
 #define zero(x) (memzero(&(x), sizeof(x)))
 
index 5d5f5ea..f25ece0 100644 (file)
@@ -81,12 +81,6 @@ static uid_t search_uid = UID_INVALID;
 static UidRange *uid_range = NULL;
 static unsigned n_uid_range = 0;
 
-#define UID_TO_PTR(u) (ULONG_TO_PTR(u+1))
-#define PTR_TO_UID(u) ((uid_t) (PTR_TO_ULONG(u)-1))
-
-#define GID_TO_PTR(g) (ULONG_TO_PTR(g+1))
-#define PTR_TO_GID(g) ((gid_t) (PTR_TO_ULONG(g)-1))
-
 #define fix_root(x) (arg_root ? strappenda(arg_root, x) : x)
 
 static int load_user_database(void) {
index 3c79f8f..d529a21 100644 (file)
@@ -1415,6 +1415,15 @@ static void test_same_fd(void) {
         assert_se(same_fd(b, a) == 0);
 }
 
+static void test_uid_ptr(void) {
+
+        assert_se(UID_TO_PTR(0) != NULL);
+        assert_se(UID_TO_PTR(1000) != NULL);
+
+        assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
+        assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -1490,6 +1499,7 @@ int main(int argc, char *argv[]) {
         test_parse_proc_cmdline();
         test_raw_clone();
         test_same_fd();
+        test_uid_ptr();
 
         return 0;
 }