X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Floginctl.c;h=89762b66b01cf8f9592ba417313d4ec6eaccc2fe;hb=66ccd0387e528567dff92239e85c962d2f140ef1;hp=08ee804f438937e23c22242967080d290a2a01a1;hpb=ddd88763921a1534081ed28e36f6712a85449005;p=elogind.git diff --git a/src/loginctl.c b/src/loginctl.c index 08ee804f4..89762b66b 100644 --- a/src/loginctl.c +++ b/src/loginctl.c @@ -64,6 +64,8 @@ static bool on_tty(void) { } static void pager_open_if_enabled(void) { + + /* Cache result before we open the pager */ on_tty(); if (!arg_no_pager) @@ -1061,17 +1063,10 @@ static int show(DBusConnection *bus, char **args, unsigned n) { uid_t uid; uint32_t u; - if (parse_uid(args[i], &uid) < 0) { - struct passwd *pw; - - pw = getpwnam(args[i]); - if (!pw) { - log_error("User %s unknown.", args[i]); - ret = -ENOENT; - goto finish; - } - - uid = pw->pw_uid; + ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL); + if (ret < 0) { + log_error("User %s unknown.", args[i]); + goto finish; } m = dbus_message_new_method_call( @@ -1152,7 +1147,7 @@ finish: } static int activate(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1163,6 +1158,8 @@ static int activate(DBusConnection *bus, char **args, unsigned n) { dbus_error_init(&error); for (i = 1; i < n; i++) { + DBusMessage *reply; + m = dbus_message_new_method_call( "org.freedesktop.login1", "/org/freedesktop/login1", @@ -1201,16 +1198,13 @@ finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; } static int kill_session(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1224,6 +1218,8 @@ static int kill_session(DBusConnection *bus, char **args, unsigned n) { arg_kill_who = "all"; for (i = 1; i < n; i++) { + DBusMessage *reply; + m = dbus_message_new_method_call( "org.freedesktop.login1", "/org/freedesktop/login1", @@ -1261,16 +1257,13 @@ finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; } static int enable_linger(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1284,6 +1277,7 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { b = streq(args[0], "enable-linger"); for (i = 1; i < n; i++) { + DBusMessage *reply; uint32_t u; uid_t uid; @@ -1298,18 +1292,10 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (parse_uid(args[i], &uid) < 0) { - struct passwd *pw; - - errno = 0; - pw = getpwnam(args[i]); - if (!pw) { - ret = errno ? -errno : -ENOENT; - log_error("Failed to resolve user %s: %s", args[i], strerror(-ret)); - goto finish; - } - - uid = pw->pw_uid; + ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL); + if (ret < 0) { + log_error("Failed to resolve user %s: %s", args[i], strerror(-ret)); + goto finish; } u = (uint32_t) uid; @@ -1335,20 +1321,19 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { m = reply = NULL; } + ret = 0; + finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; } static int terminate_user(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1361,6 +1346,7 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { for (i = 1; i < n; i++) { uint32_t u; uid_t uid; + DBusMessage *reply; m = dbus_message_new_method_call( "org.freedesktop.login1", @@ -1373,18 +1359,10 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (parse_uid(args[i], &uid) < 0) { - struct passwd *pw; - - errno = 0; - pw = getpwnam(args[i]); - if (!pw) { - ret = errno ? -errno : -ENOENT; - log_error("Failed to look up user %s: %s", args[i], strerror(-ret)); - goto finish; - } - - uid = pw->pw_uid; + ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL); + if (ret < 0) { + log_error("Failed to look up user %s: %s", args[i], strerror(-ret)); + goto finish; } u = (uint32_t) uid; @@ -1408,20 +1386,19 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { m = reply = NULL; } + ret = 0; + finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; } static int kill_user(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1435,6 +1412,7 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { arg_kill_who = "all"; for (i = 1; i < n; i++) { + DBusMessage *reply; uid_t uid; uint32_t u; @@ -1449,18 +1427,10 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (parse_uid(args[i], &uid) < 0) { - struct passwd *pw; - - errno = 0; - pw = getpwnam(args[i]); - if (!pw) { - ret = errno ? -errno : -ENOENT; - log_error("Failed to look up user %s: %s", args[i], strerror(-ret)); - goto finish; - } - - uid = pw->pw_uid; + ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL); + if (ret < 0) { + log_error("Failed to look up user %s: %s", args[i], strerror(-ret)); + goto finish; } u = (uint32_t) uid; @@ -1485,20 +1455,19 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { m = reply = NULL; } + ret = 0; + finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; } static int attach(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1510,6 +1479,8 @@ static int attach(DBusConnection *bus, char **args, unsigned n) { dbus_error_init(&error); for (i = 2; i < n; i++) { + DBusMessage *reply; + m = dbus_message_new_method_call( "org.freedesktop.login1", "/org/freedesktop/login1", @@ -1547,9 +1518,6 @@ finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; @@ -1605,7 +1573,7 @@ finish: } static int terminate_seat(DBusConnection *bus, char **args, unsigned n) { - DBusMessage *m = NULL, *reply = NULL; + DBusMessage *m = NULL; int ret = 0; DBusError error; unsigned i; @@ -1616,6 +1584,8 @@ static int terminate_seat(DBusConnection *bus, char **args, unsigned n) { dbus_error_init(&error); for (i = 1; i < n; i++) { + DBusMessage *reply; + m = dbus_message_new_method_call( "org.freedesktop.login1", "/org/freedesktop/login1", @@ -1651,9 +1621,6 @@ finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret;