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 - 2010 Red Hat, Inc.
17
* Author: David Zeuthen <davidz@redhat.com>
18
* Author: Dan Williams <dcbw@redhat.com>
25
#include <sys/types.h>
28
#include "nm-logging.h"
30
#include "nm-session-monitor.h"
32
#define CKDB_PATH "/var/run/ConsoleKit/database"
35
* SECTION:nm-session-monitor
36
* @title: NMSessionMonitor
37
* @short_description: Monitor sessions
39
* The #NMSessionMonitor class is a utility class to track and monitor sessions.
42
struct _NMSessionMonitor {
43
GObject parent_instance;
46
GFileMonitor *database_monitor;
47
time_t database_mtime;
48
GHashTable *sessions_by_uid;
49
GHashTable *sessions_by_user;
52
struct _NMSessionMonitorClass {
53
GObjectClass parent_class;
55
void (*changed) (NMSessionMonitor *monitor);
63
static guint signals[LAST_SIGNAL] = {0};
65
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
67
/********************************************************************/
69
#define NM_SESSION_MONITOR_ERROR (nm_session_monitor_error_quark ())
70
GQuark nm_session_monitor_error_quark (void) G_GNUC_CONST;
71
GType nm_session_monitor_error_get_type (void) G_GNUC_CONST;
74
NM_SESSION_MONITOR_ERROR_IO_ERROR = 0,
75
NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
76
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
77
NM_SESSION_MONITOR_ERROR_NO_DATABASE,
78
} NMSessionMonitorError;
81
nm_session_monitor_error_quark (void)
83
static GQuark ret = 0;
85
if (G_UNLIKELY (ret == 0))
86
ret = g_quark_from_static_string ("nm-session-monitor-error");
90
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
93
nm_session_monitor_error_get_type (void)
95
static GType etype = 0;
98
static const GEnumValue values[] = {
99
/* Some I/O operation on the CK database failed */
100
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_IO_ERROR, "IOError"),
101
/* Error parsing the CK database */
102
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE, "MalformedDatabase"),
103
/* Username or UID could could not be found */
104
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_UNKNOWN_USER, "UnknownUser"),
105
/* No ConsoleKit database */
106
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_NO_DATABASE, "NoDatabase"),
110
etype = g_enum_register_static ("NMSessionMonitorError", values);
114
/********************************************************************/
124
session_free (Session *s)
127
memset (s, 0, sizeof (Session));
132
check_key (GKeyFile *keyfile, const char *group, const char *key, GError **error)
134
if (g_key_file_has_key (keyfile, group, key, error))
139
NM_SESSION_MONITOR_ERROR,
140
NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
141
"ConsoleKit database " CKDB_PATH " group '%s' had no '%s' key",
148
session_new (GKeyFile *keyfile, const char *group, GError **error)
150
GError *local = NULL;
154
s = g_new0 (Session, 1);
157
s->uid = G_MAXUINT; /* paranoia */
158
if (!check_key (keyfile, group, "uid", &local))
160
s->uid = (uid_t) g_key_file_get_integer (keyfile, group, "uid", &local);
164
if (!check_key (keyfile, group, "is_active", &local))
166
s->active = g_key_file_get_boolean (keyfile, group, "is_active", &local);
170
if (!check_key (keyfile, group, "is_local", &local))
172
s->local = g_key_file_get_boolean (keyfile, group, "is_local", &local);
176
pw = getpwuid (s->uid);
179
NM_SESSION_MONITOR_ERROR,
180
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
181
"Could not get username for UID %d",
185
s->user = g_strdup (pw->pw_name);
191
g_propagate_error (error, local);
196
session_merge (Session *src, Session *dest)
198
g_return_if_fail (src != NULL);
199
g_return_if_fail (dest != NULL);
201
g_warn_if_fail (g_strcmp0 (src->user, dest->user) == 0);
202
g_warn_if_fail (src->uid == dest->uid);
204
dest->local = (dest->local || src->local);
205
dest->active = (dest->active || src->active);
208
/********************************************************************/
211
free_database (NMSessionMonitor *self)
213
if (self->database != NULL) {
214
g_key_file_free (self->database);
215
self->database = NULL;
218
g_hash_table_remove_all (self->sessions_by_uid);
219
g_hash_table_remove_all (self->sessions_by_user);
223
reload_database (NMSessionMonitor *self, GError **error)
226
char **groups = NULL;
230
free_database (self);
233
if (stat (CKDB_PATH, &statbuf) != 0) {
235
NM_SESSION_MONITOR_ERROR,
236
errno == ENOENT ? NM_SESSION_MONITOR_ERROR_NO_DATABASE : NM_SESSION_MONITOR_ERROR_IO_ERROR,
237
"Error statting file " CKDB_PATH ": %s",
241
self->database_mtime = statbuf.st_mtime;
243
self->database = g_key_file_new ();
244
if (!g_key_file_load_from_file (self->database, CKDB_PATH, G_KEY_FILE_NONE, error))
247
groups = g_key_file_get_groups (self->database, &len);
249
g_set_error_literal (error,
250
NM_SESSION_MONITOR_ERROR,
251
NM_SESSION_MONITOR_ERROR_IO_ERROR,
252
"Could not load groups from " CKDB_PATH "");
256
for (i = 0; i < len; i++) {
259
if (!g_str_has_prefix (groups[i], "Session "))
262
s = session_new (self->database, groups[i], error);
266
found = g_hash_table_lookup (self->sessions_by_user, (gpointer) s->user);
268
session_merge (s, found);
271
/* Entirely new user */
272
g_hash_table_insert (self->sessions_by_user, (gpointer) s->user, s);
273
g_hash_table_insert (self->sessions_by_uid, GUINT_TO_POINTER (s->uid), s);
283
free_database (self);
288
ensure_database (NMSessionMonitor *self, GError **error)
290
gboolean ret = FALSE;
296
if (self->database != NULL) {
300
if (stat (CKDB_PATH, &statbuf) != 0) {
302
NM_SESSION_MONITOR_ERROR,
303
errno == ENOENT ? NM_SESSION_MONITOR_ERROR_NO_DATABASE : NM_SESSION_MONITOR_ERROR_IO_ERROR,
304
"Error statting file " CKDB_PATH " to check timestamp: %s",
309
if (statbuf.st_mtime == self->database_mtime) {
315
ret = reload_database (self, error);
322
on_file_monitor_changed (GFileMonitor * file_monitor,
325
GFileMonitorEvent event_type,
328
NMSessionMonitor *self = NM_SESSION_MONITOR (user_data);
330
/* throw away cache */
331
free_database (self);
333
g_signal_emit (self, signals[CHANGED], 0);
337
nm_session_monitor_init (NMSessionMonitor *self)
339
GError *error = NULL;
346
/* Sessions-by-user is responsible for destroying the Session objects */
347
self->sessions_by_user = g_hash_table_new_full (g_str_hash, g_str_equal,
348
NULL, (GDestroyNotify) session_free);
349
self->sessions_by_uid = g_hash_table_new (g_direct_hash, g_direct_equal);
353
if (!ensure_database (self, &error)) {
354
/* Ignore the first error if the CK database isn't found yet */
355
if (g_error_matches (error,
356
NM_SESSION_MONITOR_ERROR,
357
NM_SESSION_MONITOR_ERROR_NO_DATABASE) == FALSE) {
358
nm_log_err (LOGD_CORE, "Error loading " CKDB_PATH ": %s", error->message);
360
g_error_free (error);
364
file = g_file_new_for_path (CKDB_PATH);
365
self->database_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
366
g_object_unref (file);
367
if (self->database_monitor == NULL) {
368
nm_log_err (LOGD_CORE, "Error monitoring " CKDB_PATH ": %s", error->message);
369
g_error_free (error);
371
g_signal_connect (self->database_monitor,
373
G_CALLBACK (on_file_monitor_changed),
379
finalize (GObject *object)
381
NMSessionMonitor *self = NM_SESSION_MONITOR (object);
383
if (self->database_monitor != NULL)
384
g_object_unref (self->database_monitor);
386
free_database (self);
388
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
389
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
393
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
395
GObjectClass *gobject_class;
397
gobject_class = G_OBJECT_CLASS (klass);
399
gobject_class->finalize = finalize;
402
* NMSessionMonitor::changed:
403
* @monitor: A #NMSessionMonitor
405
* Emitted when something changes.
407
signals[CHANGED] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
408
NM_TYPE_SESSION_MONITOR,
410
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
411
NULL, /* accumulator */
412
NULL, /* accumulator data */
413
g_cclosure_marshal_VOID__VOID,
418
nm_session_monitor_get (void)
420
static NMSessionMonitor *singleton = NULL;
423
return g_object_ref (singleton);
425
singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
429
/* ---------------------------------------------------------------------------------------------------- */
433
uid_to_user (uid_t uid, const char **out_user, GError **error)
440
NM_SESSION_MONITOR_ERROR,
441
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
442
"Could not get username for UID %d",
447
/* Ugly, but hey, use ConsoleKit */
449
*out_user = pw->pw_name;
454
user_to_uid (const char *user, uid_t *out_uid, GError **error)
458
pw = getpwnam (user);
461
NM_SESSION_MONITOR_ERROR,
462
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
463
"Could not get UID for username '%s'",
468
/* Ugly, but hey, use ConsoleKit */
470
*out_uid = pw->pw_uid;
476
* nm_session_monitor_user_has_session:
477
* @monitor: A #NMSessionMonitor.
478
* @username: A username.
479
* @error: Return location for error.
481
* Checks whether the given @username is logged into a session or not.
483
* Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
484
* currently logged into a session.
487
nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
488
const char *username,
495
if (!user_to_uid (username, out_uid, error))
500
if (!ensure_database (monitor, error))
503
s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
506
NM_SESSION_MONITOR_ERROR,
507
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
508
"No session found for user '%s'",
519
* nm_session_monitor_uid_has_session:
520
* @monitor: A #NMSessionMonitor.
522
* @error: Return location for error.
524
* Checks whether the given @uid is logged into a session or not.
526
* Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
527
* currently logged into a session.
530
nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
532
const char **out_user,
538
if (!uid_to_user (uid, out_user, error))
543
if (!ensure_database (monitor, error))
546
s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
549
NM_SESSION_MONITOR_ERROR,
550
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
551
"No session found for uid %d",
562
* nm_session_monitor_user_active:
563
* @monitor: A #NMSessionMonitor.
564
* @username: A username.
565
* @error: Return location for error.
567
* Checks whether the given @username is logged into a active session or not.
569
* Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
570
* logged into an active session.
573
nm_session_monitor_user_active (NMSessionMonitor *monitor,
574
const char *username,
583
if (!ensure_database (monitor, error))
586
s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
589
NM_SESSION_MONITOR_ERROR,
590
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
591
"No session found for user '%s'",
600
* nm_session_monitor_uid_active:
601
* @monitor: A #NMSessionMonitor.
603
* @error: Return location for error.
605
* Checks whether the given @uid is logged into a active session or not.
607
* Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
608
* logged into an active session.
611
nm_session_monitor_uid_active (NMSessionMonitor *monitor,
621
if (!ensure_database (monitor, error))
624
s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
627
NM_SESSION_MONITOR_ERROR,
628
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
629
"No session found for uid '%d'",