summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-01-09 16:25:47 +0100
committerLennart Poettering <lennart@poettering.net>2015-01-09 18:35:36 +0100
commit8cb4ab0058e51f1fba93683d145ef95f97c2fa86 (patch)
tree022bbd937efd224d8de1e2c5db66331a6149ed74
parent309a29dfd24f4175de334ca1593e3fe2436ab082 (diff)
logind: unify how we cast between uid_t and pointers for hashmap keys
-rw-r--r--src/login/logind-core.c2
-rw-r--r--src/login/logind-dbus.c4
-rw-r--r--src/login/logind-session.c2
-rw-r--r--src/login/logind-user-dbus.c2
-rw-r--r--src/login/logind-user.c4
-rw-r--r--src/shared/macro.h8
-rw-r--r--src/sysusers/sysusers.c6
-rw-r--r--src/test/test-util.c10
8 files changed, 25 insertions, 13 deletions
diff --git a/src/login/logind-core.c b/src/login/logind-core.c
index 88694f9bac..a6ff5add95 100644
--- a/src/login/logind-core.c
+++ b/src/login/logind-core.c
@@ -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)
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
index 3ff6cd398f..bc985a3754 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -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);
}
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
index ea1831dac6..a51f9f3e0f 100644
--- a/src/login/logind-session.c
+++ b/src/login/logind-session.c
@@ -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;
diff --git a/src/login/logind-user-dbus.c b/src/login/logind-user-dbus.c
index 717740ad04..bff42e89ad 100644
--- a/src/login/logind-user-dbus.c
+++ b/src/login/logind-user-dbus.c
@@ -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;
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
index 9ff1302663..49c373b50d 100644
--- a/src/login/logind-user.c
+++ b/src/login/logind-user.c
@@ -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);
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 6a57428244..daa42c4c68 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -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)))
diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c
index 5d5f5ea794..f25ece0674 100644
--- a/src/sysusers/sysusers.c
+++ b/src/sysusers/sysusers.c
@@ -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) {
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 3c79f8f4d9..d529a213cc 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -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;
}