1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 * (C) Copyright 2008 - 2015 Red Hat, Inc.
17 * Author: David Zeuthen <davidz@redhat.com>
18 * Author: Dan Williams <dcbw@redhat.com>
19 * Author: Matthias Clasen
20 * Author: Pavel Šimerda <psimerda@redhat.com>
22 #include "nm-default.h"
29 #include "nm-session-monitor.h"
30 #include "NetworkManagerUtils.h"
32 #ifdef SESSION_TRACKING_SYSTEMD
33 #include <systemd/sd-login.h>
36 /********************************************************************/
39 * SECTION:nm-session-monitor
40 * @title: NMSessionMonitor
41 * @short_description: Monitor sessions
43 * The #NMSessionMonitor class is a utility class to track and monitor sessions.
45 struct _NMSessionMonitor {
46 GObject parent_instance;
48 #ifdef SESSION_TRACKING_SYSTEMD
50 sd_login_monitor *monitor;
55 #ifdef SESSION_TRACKING_CONSOLEKIT
57 GFileMonitor *monitor;
64 struct _NMSessionMonitorClass {
65 GObjectClass parent_class;
67 void (*changed) (NMSessionMonitor *monitor);
70 G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
77 static guint signals[LAST_SIGNAL] = { 0 };
79 /********************************************************************/
81 #ifdef SESSION_TRACKING_SYSTEMD
83 st_sd_session_exists (NMSessionMonitor *monitor, uid_t uid, gboolean active)
87 if (!monitor->sd.monitor)
90 status = sd_uid_get_sessions (uid, active, NULL);
93 nm_log_err (LOGD_CORE, "Failed to get systemd sessions for uid %d: %d",
100 st_sd_changed (GIOChannel *stream, GIOCondition condition, gpointer user_data)
102 NMSessionMonitor *monitor = user_data;
104 g_signal_emit (monitor, signals[CHANGED], 0);
106 sd_login_monitor_flush (monitor->sd.monitor);
112 st_sd_init (NMSessionMonitor *monitor)
117 if (!g_file_test ("/run/systemd/seats/", G_FILE_TEST_EXISTS))
120 if ((status = sd_login_monitor_new (NULL, &monitor->sd.monitor)) < 0) {
121 nm_log_err (LOGD_CORE, "Failed to create systemd login monitor: %d", status);
125 stream = g_io_channel_unix_new (sd_login_monitor_get_fd (monitor->sd.monitor));
126 monitor->sd.watch = g_io_add_watch (stream, G_IO_IN, st_sd_changed, monitor);
128 g_io_channel_unref (stream);
132 st_sd_finalize (NMSessionMonitor *monitor)
134 g_clear_pointer (&monitor->sd.monitor, sd_login_monitor_unref);
135 g_source_remove (monitor->sd.watch);
137 #endif /* SESSION_TRACKING_SYSTEMD */
139 /********************************************************************/
141 #ifdef SESSION_TRACKING_CONSOLEKIT
147 ck_load_cache (GHashTable *cache)
149 GKeyFile *keyfile = g_key_file_new ();
150 char **groups = NULL;
151 GError *error = NULL;
153 gboolean finished = FALSE;
155 if (!g_key_file_load_from_file (keyfile, CKDB_PATH, G_KEY_FILE_NONE, &error))
158 if (!(groups = g_key_file_get_groups (keyfile, &len))) {
159 nm_log_err (LOGD_CORE, "Could not load groups from " CKDB_PATH);
163 g_hash_table_remove_all (cache);
165 for (i = 0; i < len; i++) {
166 guint uid = G_MAXUINT;
167 CkSession session = { .active = FALSE };
169 if (!g_str_has_prefix (groups[i], "CkSession "))
172 uid = g_key_file_get_integer (keyfile, groups[i], "uid", &error);
176 session.active = g_key_file_get_boolean (keyfile, groups[i], "is_active", &error);
180 g_hash_table_insert (cache, GUINT_TO_POINTER (uid), g_memdup (&session, sizeof session));
186 nm_log_err (LOGD_CORE, "ConsoleKit: Failed to load database: %s", error->message);
187 g_clear_error (&error);
188 g_clear_pointer (&groups, g_strfreev);
189 g_clear_pointer (&keyfile, g_key_file_free);
195 ck_update_cache (NMSessionMonitor *monitor)
199 if (!monitor->ck.cache)
202 /* Check the database file */
203 if (stat (CKDB_PATH, &statbuf) != 0) {
204 nm_log_err (LOGD_CORE, "Failed to check ConsoleKit timestamp: %s", strerror (errno));
207 if (statbuf.st_mtime == monitor->ck.timestamp)
210 /* Update the cache */
211 if (!ck_load_cache (monitor->ck.cache))
214 monitor->ck.timestamp = statbuf.st_mtime;
220 ck_session_exists (NMSessionMonitor *monitor, uid_t uid, gboolean active)
224 if (!ck_update_cache (monitor))
227 session = g_hash_table_lookup (monitor->ck.cache, GUINT_TO_POINTER (uid));
231 if (active && !session->active)
238 ck_changed (GFileMonitor * file_monitor,
241 GFileMonitorEvent event_type,
244 g_signal_emit (user_data, signals[CHANGED], 0);
248 ck_init (NMSessionMonitor *monitor)
250 GFile *file = g_file_new_for_path (CKDB_PATH);
251 GError *error = NULL;
253 if (g_file_query_exists (file, NULL)) {
254 if ((monitor->ck.monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error))) {
255 monitor->ck.cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
256 g_signal_connect (monitor->ck.monitor,
258 G_CALLBACK (ck_changed),
261 nm_log_err (LOGD_CORE, "Error monitoring " CKDB_PATH ": %s", error->message);
262 g_clear_error (&error);
266 g_object_unref (file);
270 ck_finalize (NMSessionMonitor *monitor)
272 g_clear_pointer (&monitor->ck.cache, g_hash_table_unref);
273 g_clear_object (&monitor->ck.monitor);
275 #endif /* SESSION_TRACKING_CONSOLEKIT */
277 /********************************************************************/
279 NM_DEFINE_SINGLETON_GETTER (NMSessionMonitor, nm_session_monitor_get, NM_TYPE_SESSION_MONITOR);
282 * nm_session_monitor_connect:
283 * @self: the session monitor
284 * @callback: The callback.
285 * @user_data: User data for the callback.
287 * Connect a callback to the session monitor.
289 * Returns: Handler ID to be used with nm_session_monitor_disconnect().
292 nm_session_monitor_connect (NMSessionMonitor *self,
293 NMSessionCallback callback,
296 g_return_val_if_fail (NM_IS_SESSION_MONITOR (self), 0);
298 return g_signal_connect (self,
299 NM_SESSION_MONITOR_CHANGED,
300 G_CALLBACK (callback),
305 * nm_session_monitor_disconnect:
306 * @self: the session monitor
307 * @handler_id: Handler ID returned by nm_session_monitor-connect().
309 * Disconnect callback from the session handler.
312 nm_session_monitor_disconnect (NMSessionMonitor *self,
315 g_return_if_fail (NM_IS_SESSION_MONITOR (self));
317 g_signal_handler_disconnect (self, handler_id);
321 * nm_session_monitor_uid_to_user:
323 * @out_user: Return location for user name.
325 * Translates a UID to a user name.
328 nm_session_monitor_uid_to_user (uid_t uid, const char **out_user)
330 struct passwd *pw = getpwuid (uid);
337 *out_user = pw->pw_name;
343 * nm_session_monitor_user_to_uid:
345 * @out_uid: Return location for UID.
347 * Translates a user name to a UID.
350 nm_session_monitor_user_to_uid (const char *user, uid_t *out_uid)
352 struct passwd *pw = getpwnam (user);
359 *out_uid = pw->pw_uid;
365 * nm_session_monitor_session_exists:
366 * @self: the session monitor
368 * @active: Ignore inactive sessions.
370 * Checks whether the given @uid is logged into an active session. Don't
371 * use this feature for security purposes. It is there just to allow you
372 * to prefer an agent from an active session over an agent from an
375 * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
376 * logged into an active session.
379 nm_session_monitor_session_exists (NMSessionMonitor *self,
383 g_return_val_if_fail (NM_IS_SESSION_MONITOR (self), FALSE);
385 #ifdef SESSION_TRACKING_SYSTEMD
386 if (st_sd_session_exists (self, uid, active))
390 #ifdef SESSION_TRACKING_CONSOLEKIT
391 if (ck_session_exists (self, uid, active))
398 /********************************************************************/
401 nm_session_monitor_init (NMSessionMonitor *monitor)
403 #ifdef SESSION_TRACKING_SYSTEMD
404 st_sd_init (monitor);
407 #ifdef SESSION_TRACKING_CONSOLEKIT
413 nm_session_monitor_finalize (GObject *object)
415 #ifdef SESSION_TRACKING_SYSTEMD
416 st_sd_finalize (NM_SESSION_MONITOR (object));
419 #ifdef SESSION_TRACKING_CONSOLEKIT
420 ck_finalize (NM_SESSION_MONITOR (object));
423 if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
424 G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
428 nm_session_monitor_class_init (NMSessionMonitorClass *klass)
430 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
432 gobject_class->finalize = nm_session_monitor_finalize;
435 * NMSessionMonitor::changed:
436 * @monitor: A #NMSessionMonitor
438 * Emitted when something changes.
440 signals[CHANGED] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
441 NM_TYPE_SESSION_MONITOR,
443 G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
444 NULL, /* accumulator */
445 NULL, /* accumulator data */
446 g_cclosure_marshal_VOID__VOID,