1
0
mirror of https://github.com/systemd/systemd synced 2025-09-21 12:54:44 +02:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Lennart Poettering
1f0c7cd5e1
Merge pull request #14592 from keszybz/simplifications
Simplifications
2020-01-17 12:27:48 +01:00
Zbigniew Jędrzejewski-Szmek
4ca739e20a core: reduce indentation a bit 2020-01-17 08:13:09 +01:00
Zbigniew Jędrzejewski-Szmek
b0a94df963 logind: use loop instead of repeated code
https://github.com/systemd/systemd/pull/14096#discussion_r350953689
2020-01-17 08:13:09 +01:00
Zbigniew Jędrzejewski-Szmek
ddee3ada46 shared/user-record-nss: use macro to avoid repeats
It's easier to read when each field is intialized in exactly one place.
2020-01-17 08:13:09 +01:00
Zbigniew Jędrzejewski-Szmek
192aee3cae shared/user-record-nss: shorten code a bit
free_and_strdup() already does comparison internally.
2020-01-16 21:57:00 +01:00
3 changed files with 88 additions and 128 deletions

View File

@ -405,32 +405,20 @@ static int user_update_slice(User *u) {
if (r < 0) if (r < 0)
return bus_log_create_error(r); return bus_log_create_error(r);
if (u->user_record->tasks_max != UINT64_MAX) { const struct {
r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", u->user_record->tasks_max); const char *name;
if (r < 0) uint64_t value;
return bus_log_create_error(r); } settings[] = {
} { "TasksMax", u->user_record->tasks_max },
{ "MemoryMax", u->user_record->memory_max },
{ "MemoryHigh", u->user_record->memory_high },
{ "CPUWeight", u->user_record->cpu_weight },
{ "IOWeight", u->user_record->io_weight },
};
if (u->user_record->memory_max != UINT64_MAX) { for (size_t i = 0; i < ELEMENTSOF(settings); i++)
r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", u->user_record->memory_max); if (settings[i].value != UINT64_MAX) {
if (r < 0) r = sd_bus_message_append(m, "(sv)", settings[i].name, "t", settings[i].value);
return bus_log_create_error(r);
}
if (u->user_record->memory_high != UINT64_MAX) {
r = sd_bus_message_append(m, "(sv)", "MemoryHigh", "t", u->user_record->memory_high);
if (r < 0)
return bus_log_create_error(r);
}
if (u->user_record->cpu_weight != UINT64_MAX) {
r = sd_bus_message_append(m, "(sv)", "CPUWeight", "t", u->user_record->cpu_weight);
if (r < 0)
return bus_log_create_error(r);
}
if (u->user_record->io_weight != UINT64_MAX) {
r = sd_bus_message_append(m, "(sv)", "IOWeight", "t", u->user_record->io_weight);
if (r < 0) if (r < 0)
return bus_log_create_error(r); return bus_log_create_error(r);
} }

View File

@ -6,6 +6,9 @@
#include "strv.h" #include "strv.h"
#include "user-record-nss.h" #include "user-record-nss.h"
#define SET_IF(field, condition, value, fallback) \
field = (condition) ? (value) : (fallback)
int nss_passwd_to_user_record( int nss_passwd_to_user_record(
const struct passwd *pwd, const struct passwd *pwd,
const struct spwd *spwd, const struct spwd *spwd,
@ -31,35 +34,23 @@ int nss_passwd_to_user_record(
if (r < 0) if (r < 0)
return r; return r;
if (isempty(pwd->pw_gecos) || streq_ptr(pwd->pw_gecos, hr->user_name)) r = free_and_strdup(&hr->real_name,
hr->real_name = mfree(hr->real_name); streq_ptr(pwd->pw_gecos, hr->user_name) ? NULL : empty_to_null(pwd->pw_gecos));
else {
r = free_and_strdup(&hr->real_name, pwd->pw_gecos);
if (r < 0) if (r < 0)
return r; return r;
}
if (isempty(pwd->pw_dir)) r = free_and_strdup(&hr->home_directory, empty_to_null(pwd->pw_dir));
hr->home_directory = mfree(hr->home_directory);
else {
r = free_and_strdup(&hr->home_directory, pwd->pw_dir);
if (r < 0) if (r < 0)
return r; return r;
}
if (isempty(pwd->pw_shell)) r = free_and_strdup(&hr->shell, empty_to_null(pwd->pw_shell));
hr->shell = mfree(hr->shell);
else {
r = free_and_strdup(&hr->shell, pwd->pw_shell);
if (r < 0) if (r < 0)
return r; return r;
}
hr->uid = pwd->pw_uid; hr->uid = pwd->pw_uid;
hr->gid = pwd->pw_gid; hr->gid = pwd->pw_gid;
if (spwd) { if (spwd && hashed_password_valid(spwd->sp_pwdp)) {
if (hashed_password_valid(spwd->sp_pwdp)) {
strv_free_erase(hr->hashed_password); strv_free_erase(hr->hashed_password);
hr->hashed_password = strv_new(spwd->sp_pwdp); hr->hashed_password = strv_new(spwd->sp_pwdp);
if (!hr->hashed_password) if (!hr->hashed_password)
@ -72,56 +63,37 @@ int nss_passwd_to_user_record(
* just a password instead of the whole account, but that's mostly pointless in times of * just a password instead of the whole account, but that's mostly pointless in times of
* password-less authorization, hence let's not bother. */ * password-less authorization, hence let's not bother. */
if (spwd->sp_expire >= 0) SET_IF(hr->locked,
hr->locked = spwd->sp_expire <= 1; spwd && spwd->sp_expire >= 0,
else spwd->sp_expire <= 1, -1);
hr->locked = -1;
if (spwd->sp_expire > 1 && (uint64_t) spwd->sp_expire < (UINT64_MAX-1)/USEC_PER_DAY) SET_IF(hr->not_after_usec,
hr->not_after_usec = spwd->sp_expire * USEC_PER_DAY; spwd && spwd->sp_expire > 1 && (uint64_t) spwd->sp_expire < (UINT64_MAX-1)/USEC_PER_DAY,
else spwd->sp_expire * USEC_PER_DAY, UINT64_MAX);
hr->not_after_usec = UINT64_MAX;
if (spwd->sp_lstchg >= 0) SET_IF(hr->password_change_now,
hr->password_change_now = spwd->sp_lstchg == 0; spwd && spwd->sp_lstchg >= 0,
else spwd->sp_lstchg == 0, -1);
hr->password_change_now = -1;
if (spwd->sp_lstchg > 0 && (uint64_t) spwd->sp_lstchg <= (UINT64_MAX-1)/USEC_PER_DAY) SET_IF(hr->last_password_change_usec,
hr->last_password_change_usec = spwd->sp_lstchg * USEC_PER_DAY; spwd && spwd->sp_lstchg > 0 && (uint64_t) spwd->sp_lstchg <= (UINT64_MAX-1)/USEC_PER_DAY,
else spwd->sp_lstchg * USEC_PER_DAY, UINT64_MAX);
hr->last_password_change_usec = UINT64_MAX;
if (spwd->sp_min > 0 && (uint64_t) spwd->sp_min <= (UINT64_MAX-1)/USEC_PER_DAY) SET_IF(hr->password_change_min_usec,
hr->password_change_min_usec = spwd->sp_min * USEC_PER_DAY; spwd && spwd->sp_min > 0 && (uint64_t) spwd->sp_min <= (UINT64_MAX-1)/USEC_PER_DAY,
else spwd->sp_min * USEC_PER_DAY, UINT64_MAX);
hr->password_change_min_usec = UINT64_MAX;
if (spwd->sp_max > 0 && (uint64_t) spwd->sp_max <= (UINT64_MAX-1)/USEC_PER_DAY) SET_IF(hr->password_change_max_usec,
hr->password_change_max_usec = spwd->sp_max * USEC_PER_DAY; spwd && spwd->sp_max > 0 && (uint64_t) spwd->sp_max <= (UINT64_MAX-1)/USEC_PER_DAY,
else spwd->sp_max * USEC_PER_DAY, UINT64_MAX);
hr->password_change_max_usec = UINT64_MAX;
if (spwd->sp_warn > 0 && (uint64_t) spwd->sp_warn <= (UINT64_MAX-1)/USEC_PER_DAY) SET_IF(hr->password_change_warn_usec,
hr->password_change_warn_usec = spwd->sp_warn * USEC_PER_DAY; spwd && spwd->sp_warn > 0 && (uint64_t) spwd->sp_warn <= (UINT64_MAX-1)/USEC_PER_DAY,
else spwd->sp_warn * USEC_PER_DAY, UINT64_MAX);
hr->password_change_warn_usec = UINT64_MAX;
if (spwd->sp_inact > 0 && (uint64_t) spwd->sp_inact <= (UINT64_MAX-1)/USEC_PER_DAY) SET_IF(hr->password_change_inactive_usec,
hr->password_change_inactive_usec = spwd->sp_inact * USEC_PER_DAY; spwd && spwd->sp_inact > 0 && (uint64_t) spwd->sp_inact <= (UINT64_MAX-1)/USEC_PER_DAY,
else spwd->sp_inact * USEC_PER_DAY, UINT64_MAX);
hr->password_change_inactive_usec = UINT64_MAX;
} else {
hr->hashed_password = strv_free_erase(hr->hashed_password);
hr->locked = -1;
hr->not_after_usec = UINT64_MAX;
hr->password_change_now = -1,
hr->last_password_change_usec = UINT64_MAX;
hr->password_change_min_usec = UINT64_MAX;
hr->password_change_max_usec = UINT64_MAX;
hr->password_change_warn_usec = UINT64_MAX;
hr->password_change_inactive_usec = UINT64_MAX;
}
hr->json = json_variant_unref(hr->json); hr->json = json_variant_unref(hr->json);
r = json_build(&hr->json, JSON_BUILD_OBJECT( r = json_build(&hr->json, JSON_BUILD_OBJECT(