From 68892f94aee1400cebbe9f7360cc4a5804c3c2b1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 25 May 2021 17:40:54 +0200 Subject: [PATCH 1/2] loginctl: kill calling user when invoked with empty string A suggested by: #19711 --- src/login/loginctl.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 7a2b13e442..cbb6518f4d 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -1086,9 +1086,15 @@ static int terminate_user(int argc, char *argv[], void *userdata) { for (int i = 1; i < argc; i++) { uid_t uid; - r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL, 0); - if (r < 0) - return log_error_errno(r, "Failed to look up user %s: %m", argv[i]); + if (isempty(argv[i])) + uid = getuid(); + else { + const char *u = argv[i]; + + r = get_user_creds(&u, &uid, NULL, NULL, NULL, 0); + if (r < 0) + return log_error_errno(r, "Failed to look up user %s: %m", argv[i]); + } r = bus_call_method(bus, bus_login_mgr, "TerminateUser", &error, NULL, "u", (uint32_t) uid); if (r < 0) @@ -1114,9 +1120,15 @@ static int kill_user(int argc, char *argv[], void *userdata) { for (int i = 1; i < argc; i++) { uid_t uid; - r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL, 0); - if (r < 0) - return log_error_errno(r, "Failed to look up user %s: %m", argv[i]); + if (isempty(argv[i])) + uid = getuid(); + else { + const char *u = argv[i]; + + r = get_user_creds(&u, &uid, NULL, NULL, NULL, 0); + if (r < 0) + return log_error_errno(r, "Failed to look up user %s: %m", argv[i]); + } r = bus_call_method( bus, From 9f48b4e40ea69b90f61ac2e35b3b272f9cc31674 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 25 May 2021 17:42:34 +0200 Subject: [PATCH 2/2] man: documet that loginctl {terminate|kill}-{session|user} take the empty string, optionally Fixes: #19711 --- man/loginctl.xml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/man/loginctl.xml b/man/loginctl.xml index 56a86e56ef..bef3be71c9 100644 --- a/man/loginctl.xml +++ b/man/loginctl.xml @@ -113,18 +113,18 @@ terminate-session ID… - Terminates a session. This kills all processes - of the session and deallocates all resources attached to the - session. + Terminates a session. This kills all processes of the session and deallocates all + resources attached to the session. If the argument is specified as empty string the session invoking + the command is terminated. kill-session ID… - Send a signal to one or more processes of the - session. Use to select which - process to kill. Use to select the - signal to send. + Send a signal to one or more processes of the session. Use + to select which process to kill. Use to + select the signal to send. If the argument is specified as empty string the signal is sent to the + session invoking the command. @@ -184,17 +184,17 @@ terminate-user USER… - Terminates all sessions of a user. This kills - all processes of all sessions of the user and deallocates all - runtime resources attached to the user. + Terminates all sessions of a user. This kills all processes of all sessions of the + user and deallocates all runtime resources attached to the user. If the argument is specified as + empty string the sessions of the user invoking the command are terminated. kill-user USER… - Send a signal to all processes of a user. Use - to select the signal to send. - + Send a signal to all processes of a user. Use to select + the signal to send. If the argument is specified as empty string the signal is sent to the sessions + of the user invoking the command.