X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Floginctl.c;h=89762b66b01cf8f9592ba417313d4ec6eaccc2fe;hb=4d8a7798e7f12c6400495cbc4d0ad57ed20ce90a;hp=829213e3d0c97f3d821ad60104ce31a888bd8499;hpb=de07ab16c6b919cead26c9a5209a362127ff6142;p=elogind.git diff --git a/src/loginctl.c b/src/loginctl.c index 829213e3d..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) @@ -1058,19 +1060,13 @@ static int show(DBusConnection *bus, char **args, unsigned n) { } } else if (strstr(args[0], "user")) { - uint32_t uid; - - if (safe_atou(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_t uid; + uint32_t u; - 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( @@ -1084,8 +1080,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) { goto finish; } + u = (uint32_t) uid; if (!dbus_message_append_args(m, - DBUS_TYPE_UINT32, &uid, + DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID)) { log_error("Could not append arguments to message."); ret = -ENOMEM; @@ -1150,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; @@ -1161,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", @@ -1199,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; @@ -1222,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", @@ -1259,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; @@ -1282,7 +1277,9 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { b = streq(args[0], "enable-linger"); for (i = 1; i < n; i++) { - uint32_t uid; + DBusMessage *reply; + uint32_t u; + uid_t uid; m = dbus_message_new_method_call( "org.freedesktop.login1", @@ -1295,22 +1292,15 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (safe_atou32(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; if (!dbus_message_append_args(m, - DBUS_TYPE_UINT32, &uid, + DBUS_TYPE_UINT32, &u, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_BOOLEAN, &interactive, DBUS_TYPE_INVALID)) { @@ -1331,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; @@ -1356,6 +1345,8 @@ 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", @@ -1368,20 +1359,13 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (safe_atou32(args[i], &u) < 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; - } - - u = 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; if (!dbus_message_append_args(m, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID)) { @@ -1402,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; @@ -1429,6 +1412,8 @@ 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; m = dbus_message_new_method_call( @@ -1442,20 +1427,13 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (safe_atou32(args[i], &u) < 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; - } - - u = 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; if (!dbus_message_append_args(m, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INT32, arg_signal, @@ -1477,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; @@ -1502,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", @@ -1539,9 +1518,6 @@ finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret; @@ -1597,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; @@ -1608,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", @@ -1643,9 +1621,6 @@ finish: if (m) dbus_message_unref(m); - if (reply) - dbus_message_unref(reply); - dbus_error_free(&error); return ret;