[systemd-commits] 2 commits - Makefile.am man/sd_session_is_active.xml src/login src/shared src/systemctl src/systemd
Lennart Poettering
lennart at kemper.freedesktop.org
Mon Jan 14 18:04:47 PST 2013
Makefile.am | 2 ++
man/sd_session_is_active.xml | 29 +++++++++++++++++++++++------
src/login/libsystemd-login.sym | 5 +++++
src/login/sd-login.c | 4 ++++
src/shared/util.c | 17 +++++++++++++++++
src/shared/util.h | 2 ++
src/systemctl/systemctl.c | 35 ++++++++++++++++++++++++++++++++---
src/systemd/sd-login.h | 3 +++
8 files changed, 88 insertions(+), 9 deletions(-)
New commits:
commit 59164be40e6b520315d87e1ef16a3be65c854224
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Jan 15 03:00:43 2013 +0100
systemctl: when inhibiting shutdown/suspend also check for other login sessions
diff --git a/src/shared/util.c b/src/shared/util.c
index 49b5844..a87828d 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -4444,6 +4444,23 @@ int get_user_creds(
return 0;
}
+char* uid_to_name(uid_t uid) {
+ struct passwd *p;
+ char *r;
+
+ if (uid == 0)
+ return strdup("root");
+
+ p = getpwuid(uid);
+ if (p)
+ return strdup(p->pw_name);
+
+ if (asprintf(&r, "%lu", (unsigned long) uid) < 0)
+ return NULL;
+
+ return r;
+}
+
int get_group_creds(const char **groupname, gid_t *gid) {
struct group *g;
gid_t id;
diff --git a/src/shared/util.h b/src/shared/util.h
index bb6602f..bbf744e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -433,6 +433,8 @@ int get_group_creds(const char **groupname, gid_t *gid);
int in_group(const char *name);
+char* uid_to_name(uid_t uid);
+
int glob_exists(const char *path);
int dirent_ensure_type(DIR *d, struct dirent *de);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 0bae67e..d56b4d6 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -38,6 +38,7 @@
#include <systemd/sd-daemon.h>
#include <systemd/sd-shutdown.h>
+#include <systemd/sd-login.h>
#include "log.h"
#include "util.h"
@@ -1694,6 +1695,8 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
DBusMessageIter iter, sub, sub2;
int r;
unsigned c = 0;
+ _cleanup_strv_free_ char **sessions = NULL;
+ char **s;
if (!bus)
return 0;
@@ -1735,7 +1738,7 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
const char *what, *who, *why, *mode;
uint32_t uid, pid;
_cleanup_strv_free_ char **sv = NULL;
- _cleanup_free_ char *comm = NULL;
+ _cleanup_free_ char *comm = NULL, *user = NULL;
if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
log_error("Failed to parse reply.");
@@ -1769,7 +1772,9 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
goto next;
get_process_comm(pid, &comm);
- log_warning("Operation inhibited by \"%s\" (PID %lu \"%s\", UID %lu), reason is \"%s\".", who, (unsigned long) pid, strna(comm), (unsigned long) uid, why);
+ user = uid_to_name(uid);
+ log_warning("Operation inhibited by \"%s\" (PID %lu \"%s\", user %s), reason is \"%s\".",
+ who, (unsigned long) pid, strna(comm), strna(user), why);
c++;
next:
@@ -1778,10 +1783,34 @@ static int check_inhibitors(DBusConnection *bus, enum action a) {
dbus_message_iter_recurse(&iter, &sub);
+ /* Check for current sessions */
+ sd_get_sessions(&sessions);
+ STRV_FOREACH(s, sessions) {
+ uid_t uid;
+ _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
+
+ if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
+ continue;
+
+ if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
+ continue;
+
+ if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
+ continue;
+
+ sd_session_get_tty(*s, &tty);
+ sd_session_get_seat(*s, &seat);
+ sd_session_get_service(*s, &service);
+ user = uid_to_name(uid);
+
+ log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
+ c++;
+ }
+
if (c <= 0)
return 0;
- log_error("Please try again after closing inhibitors or ignore them with 'systemctl %s -i'.",
+ log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
a == ACTION_HALT ? "halt" :
a == ACTION_POWEROFF ? "poweroff" :
a == ACTION_REBOOT ? "reboot" :
commit c84f5e4a825f17163ead0f60308d548b415334a5
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Jan 15 03:00:33 2013 +0100
login: introduce sd_session_get_tty()
diff --git a/Makefile.am b/Makefile.am
index 3318829..e10e421 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3690,6 +3690,7 @@ MANPAGES_ALIAS += \
man/sd_session_get_type.3 \
man/sd_session_get_class.3 \
man/sd_session_get_display.3 \
+ man/sd_session_get_tty.3 \
man/sd_pid_get_owner_uid.3 \
man/sd_pid_get_unit.3 \
man/sd_uid_is_on_seat.3 \
@@ -3714,6 +3715,7 @@ man/sd_session_get_state.3: man/sd_session_is_active.3
man/sd_session_get_type.3: man/sd_session_is_active.3
man/sd_session_get_class.3: man/sd_session_is_active.3
man/sd_session_get_display.3: man/sd_session_is_active.3
+man/sd_session_get_tty.3: man/sd_session_is_active.3
man/sd_pid_get_owner_uid.3: man/sd_pid_get_session.3
man/sd_pid_get_unit.3: man/sd_pid_get_session.3
man/sd_uid_is_on_seat.3: man/sd_uid_get_state.3
diff --git a/man/sd_session_is_active.xml b/man/sd_session_is_active.xml
index a9107cb..ab48b9e 100644
--- a/man/sd_session_is_active.xml
+++ b/man/sd_session_is_active.xml
@@ -51,6 +51,7 @@
<refname>sd_session_get_type</refname>
<refname>sd_session_get_class</refname>
<refname>sd_session_get_display</refname>
+ <refname>sd_session_get_tty</refname>
<refpurpose>Determine state of a specific session</refpurpose>
</refnamediv>
@@ -104,6 +105,12 @@
<paramdef>const char* <parameter>session</parameter></paramdef>
<paramdef>char** <parameter>display</parameter></paramdef>
</funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_session_get_tty</function></funcdef>
+ <paramdef>const char* <parameter>session</parameter></paramdef>
+ <paramdef>char** <parameter>tty</parameter></paramdef>
+ </funcprototype>
</funcsynopsis>
</refsynopsisdiv>
@@ -186,6 +193,14 @@
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para>
+ <para><function>sd_session_get_tty()</function>
+ may be used to determine the TTY device of the
+ session identified by the specified session
+ identifier. The returned string is one of needs to be
+ freed with the libc
+ <citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ call after use.</para>
+
<para>If the <literal>session</literal> parameter of
any of these functions is passed as NULL the operation
is executed for the session the calling process is a
@@ -203,8 +218,9 @@
<function>sd_session_get_seat()</function>,
<function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>,
- <function>sd_session_get_class()</function> and
- <function>sd_session_get_display()</function> return 0 or
+ <function>sd_session_get_class()</function>,
+ <function>sd_session_get_display()</function> and
+ <function>sd_session_get_tty()</function> return 0 or
a positive integer. On failure, these calls return a
negative errno-style error code.</para>
</refsect1>
@@ -218,10 +234,11 @@
<function>sd_session_get_seat()</function>,
<function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>,
- <function>sd_session_get_class()</function> and
- <function>sd_session_get_display()</function> interfaces
- are available as shared library, which can be compiled
- and linked to with the
+ <function>sd_session_get_class()</function>,
+ <function>sd_session_get_display()</function> and
+ <function>sd_session_get_tty()</function>
+ interfaces are available as shared library, which can
+ be compiled and linked to with the
<literal>libsystemd-login</literal>
<citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
diff --git a/src/login/libsystemd-login.sym b/src/login/libsystemd-login.sym
index ff51be7..272b0e2 100644
--- a/src/login/libsystemd-login.sym
+++ b/src/login/libsystemd-login.sym
@@ -53,3 +53,8 @@ global:
sd_seat_can_tty;
sd_seat_can_graphical;
} LIBSYSTEMD_LOGIN_43;
+
+LIBSYSTEMD_LOGIN_198 {
+global:
+ sd_session_get_tty;
+} LIBSYSTEMD_LOGIN_186;
diff --git a/src/login/sd-login.c b/src/login/sd-login.c
index 45e3bb8..4bc51e7 100644
--- a/src/login/sd-login.c
+++ b/src/login/sd-login.c
@@ -409,6 +409,10 @@ _public_ int sd_session_get_seat(const char *session, char **seat) {
return session_get_string(session, "SEAT", seat);
}
+_public_ int sd_session_get_tty(const char *session, char **tty) {
+ return session_get_string(session, "TTY", tty);
+}
+
_public_ int sd_session_get_service(const char *session, char **service) {
return session_get_string(session, "SERVICE", service);
}
diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h
index 6bd1f2d..3746b45 100644
--- a/src/systemd/sd-login.h
+++ b/src/systemd/sd-login.h
@@ -108,6 +108,9 @@ int sd_session_get_class(const char *session, char **clazz);
/* Determine the X11 display of this session. */
int sd_session_get_display(const char *session, char **display);
+/* Determine the TTY of this session. */
+int sd_session_get_tty(const char *session, char **display);
+
/* Return active session and user of seat */
int sd_seat_get_active(const char *seat, char **session, uid_t *uid);
More information about the systemd-commits
mailing list