mirror of
https://github.com/systemd/systemd.git
synced 2026-08-03 14:40:31 +00:00
logind: unify how we cast between uid_t and pointers for hashmap keys
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user