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>
26
#include "nm-logging.h"
28
#include "nm-session-utils.h"
29
#include "nm-session-monitor.h"
31
#define CKDB_PATH "/var/run/ConsoleKit/database"
34
* SECTION:nm-session-monitor
35
* @title: NMSessionMonitor
36
* @short_description: Monitor sessions
38
* The #NMSessionMonitor class is a utility class to track and monitor sessions.
41
struct _NMSessionMonitor {
42
GObject parent_instance;
45
GFileMonitor *database_monitor;
46
time_t database_mtime;
47
GHashTable *sessions_by_uid;
48
GHashTable *sessions_by_user;
51
struct _NMSessionMonitorClass {
52
GObjectClass parent_class;
54
void (*changed) (NMSessionMonitor *monitor);
62
static guint signals[LAST_SIGNAL] = {0};
64
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
66
/********************************************************************/
76
session_free (Session *s)
79
memset (s, 0, sizeof (Session));
84
check_key (GKeyFile *keyfile, const char *group, const char *key, GError **error)
86
if (g_key_file_has_key (keyfile, group, key, error))
91
NM_SESSION_MONITOR_ERROR,
92
NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
93
"ConsoleKit database " CKDB_PATH " group '%s' had no '%s' key",
100
session_new (GKeyFile *keyfile, const char *group, GError **error)
102
GError *local = NULL;
104
const char *uname = NULL;
106
s = g_new0 (Session, 1);
109
s->uid = G_MAXUINT; /* paranoia */
110
if (!check_key (keyfile, group, "uid", &local))
112
s->uid = (uid_t) g_key_file_get_integer (keyfile, group, "uid", &local);
116
if (!check_key (keyfile, group, "is_active", &local))
118
s->active = g_key_file_get_boolean (keyfile, group, "is_active", &local);
122
if (!check_key (keyfile, group, "is_local", &local))
124
s->local = g_key_file_get_boolean (keyfile, group, "is_local", &local);
128
if (!nm_session_uid_to_user (s->uid, &uname, error))
130
s->user = g_strdup (uname);
136
g_propagate_error (error, local);
141
session_merge (Session *src, Session *dest)
143
g_return_if_fail (src != NULL);
144
g_return_if_fail (dest != NULL);
146
g_warn_if_fail (g_strcmp0 (src->user, dest->user) == 0);
147
g_warn_if_fail (src->uid == dest->uid);
149
dest->local = (dest->local || src->local);
150
dest->active = (dest->active || src->active);
153
/********************************************************************/
156
free_database (NMSessionMonitor *self)
158
if (self->database != NULL) {
159
g_key_file_free (self->database);
160
self->database = NULL;
163
g_hash_table_remove_all (self->sessions_by_uid);
164
g_hash_table_remove_all (self->sessions_by_user);
168
reload_database (NMSessionMonitor *self, GError **error)
171
char **groups = NULL;
175
free_database (self);
178
if (stat (CKDB_PATH, &statbuf) != 0) {
180
NM_SESSION_MONITOR_ERROR,
181
errno == ENOENT ? NM_SESSION_MONITOR_ERROR_NO_DATABASE : NM_SESSION_MONITOR_ERROR_IO_ERROR,
182
"Error statting file " CKDB_PATH ": %s",
186
self->database_mtime = statbuf.st_mtime;
188
self->database = g_key_file_new ();
189
if (!g_key_file_load_from_file (self->database, CKDB_PATH, G_KEY_FILE_NONE, error))
192
groups = g_key_file_get_groups (self->database, &len);
194
g_set_error_literal (error,
195
NM_SESSION_MONITOR_ERROR,
196
NM_SESSION_MONITOR_ERROR_IO_ERROR,
197
"Could not load groups from " CKDB_PATH "");
201
for (i = 0; i < len; i++) {
204
if (!g_str_has_prefix (groups[i], "Session "))
207
s = session_new (self->database, groups[i], error);
211
found = g_hash_table_lookup (self->sessions_by_user, (gpointer) s->user);
213
session_merge (s, found);
216
/* Entirely new user */
217
g_hash_table_insert (self->sessions_by_user, (gpointer) s->user, s);
218
g_hash_table_insert (self->sessions_by_uid, GUINT_TO_POINTER (s->uid), s);
228
free_database (self);
233
ensure_database (NMSessionMonitor *self, GError **error)
235
gboolean ret = FALSE;
237
if (self->database != NULL) {
241
if (stat (CKDB_PATH, &statbuf) != 0) {
243
NM_SESSION_MONITOR_ERROR,
244
errno == ENOENT ? NM_SESSION_MONITOR_ERROR_NO_DATABASE : NM_SESSION_MONITOR_ERROR_IO_ERROR,
245
"Error statting file " CKDB_PATH " to check timestamp: %s",
250
if (statbuf.st_mtime == self->database_mtime) {
256
ret = reload_database (self, error);
263
on_file_monitor_changed (GFileMonitor * file_monitor,
266
GFileMonitorEvent event_type,
269
NMSessionMonitor *self = NM_SESSION_MONITOR (user_data);
271
/* throw away cache */
272
free_database (self);
274
g_signal_emit (self, signals[CHANGED], 0);
278
nm_session_monitor_init (NMSessionMonitor *self)
280
GError *error = NULL;
283
/* Sessions-by-user is responsible for destroying the Session objects */
284
self->sessions_by_user = g_hash_table_new_full (g_str_hash, g_str_equal,
285
NULL, (GDestroyNotify) session_free);
286
self->sessions_by_uid = g_hash_table_new (g_direct_hash, g_direct_equal);
290
if (!ensure_database (self, &error)) {
291
/* Ignore the first error if the CK database isn't found yet */
292
if (g_error_matches (error,
293
NM_SESSION_MONITOR_ERROR,
294
NM_SESSION_MONITOR_ERROR_NO_DATABASE) == FALSE) {
295
nm_log_err (LOGD_CORE, "Error loading " CKDB_PATH ": %s", error->message);
297
g_error_free (error);
301
file = g_file_new_for_path (CKDB_PATH);
302
self->database_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
303
g_object_unref (file);
304
if (self->database_monitor == NULL) {
305
nm_log_err (LOGD_CORE, "Error monitoring " CKDB_PATH ": %s", error->message);
306
g_error_free (error);
308
g_signal_connect (self->database_monitor,
310
G_CALLBACK (on_file_monitor_changed),
316
finalize (GObject *object)
318
NMSessionMonitor *self = NM_SESSION_MONITOR (object);
320
if (self->database_monitor != NULL)
321
g_object_unref (self->database_monitor);
323
free_database (self);
325
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
326
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
330
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
332
GObjectClass *gobject_class;
334
gobject_class = G_OBJECT_CLASS (klass);
336
gobject_class->finalize = finalize;
339
* NMSessionMonitor::changed:
340
* @monitor: A #NMSessionMonitor
342
* Emitted when something changes.
344
signals[CHANGED] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
345
NM_TYPE_SESSION_MONITOR,
347
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
348
NULL, /* accumulator */
349
NULL, /* accumulator data */
350
g_cclosure_marshal_VOID__VOID,
355
nm_session_monitor_get (void)
357
static NMSessionMonitor *singleton = NULL;
360
return g_object_ref (singleton);
362
singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
366
/* ---------------------------------------------------------------------------------------------------- */
369
* nm_session_monitor_user_has_session:
370
* @monitor: A #NMSessionMonitor.
371
* @username: A username.
372
* @error: Return location for error.
374
* Checks whether the given @username is logged into a session or not.
376
* Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
377
* currently logged into a session.
380
nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
381
const char *username,
387
if (!ensure_database (monitor, error))
390
s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
393
NM_SESSION_MONITOR_ERROR,
394
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
395
"No session found for user '%s'",
406
* nm_session_monitor_uid_has_session:
407
* @monitor: A #NMSessionMonitor.
409
* @error: Return location for error.
411
* Checks whether the given @uid is logged into a session or not.
413
* Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
414
* currently logged into a session.
417
nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
419
const char **out_user,
424
if (!ensure_database (monitor, error))
427
s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
430
NM_SESSION_MONITOR_ERROR,
431
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
432
"No session found for uid %d",
443
* nm_session_monitor_user_active:
444
* @monitor: A #NMSessionMonitor.
445
* @username: A username.
446
* @error: Return location for error.
448
* Checks whether the given @username is logged into a active session or not.
450
* Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
451
* logged into an active session.
454
nm_session_monitor_user_active (NMSessionMonitor *monitor,
455
const char *username,
460
if (!ensure_database (monitor, error))
463
s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
466
NM_SESSION_MONITOR_ERROR,
467
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
468
"No session found for user '%s'",
477
* nm_session_monitor_uid_active:
478
* @monitor: A #NMSessionMonitor.
480
* @error: Return location for error.
482
* Checks whether the given @uid is logged into a active session or not.
484
* Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
485
* logged into an active session.
488
nm_session_monitor_uid_active (NMSessionMonitor *monitor,
494
if (!ensure_database (monitor, error))
497
s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
500
NM_SESSION_MONITOR_ERROR,
501
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
502
"No session found for uid '%d'",