~ubuntu-branches/ubuntu/precise/network-manager/precise

« back to all changes in this revision

Viewing changes to src/nm-session-monitor-ck.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-02-09 16:45:41 UTC
  • mfrom: (1.1.53)
  • Revision ID: package-import@ubuntu.com-20120209164541-4h90zknlsfdb7x35
Tags: 0.9.2.0+git201202091925.c721477-0ubuntu1
* upstream snapshot 2012-02-09 19:25:59 (GMT)
  + c721477d11d4fe144111d6d2eec8f93f2e9186c9
* debian/patches/avoid-periodic-disk-wakeups.patch: refreshed.
* debian/patches/nl3-default-ip6-route.patch: refreshed.
* debian/libnm-glib4.symbols: add symbols:
  + nm_active_connection_get_master@Base
  + nm_client_new_async@Base
  + nm_client_new_finish@Base
  + nm_remote_settings_new_async@Base
  + nm_remote_settings_new_finish@Base
  + nm_device_get_state_reason@Base
* debian/libnm-util2.symbols: add symbols:
  + nm_setting_802_1x_get_pac_file@Base
  + nm_setting_infiniband_get_transport_mode@Base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
6
 *
 
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.
 
11
 *
 
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.
 
15
 *
 
16
 * (C) Copyright 2008 - 2010 Red Hat, Inc.
 
17
 * Author: David Zeuthen <davidz@redhat.com>
 
18
 * Author: Dan Williams <dcbw@redhat.com>
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include <errno.h>
 
23
#include <string.h>
 
24
#include <sys/stat.h>
 
25
#include <gio/gio.h>
 
26
#include "nm-logging.h"
 
27
 
 
28
#include "nm-session-utils.h"
 
29
#include "nm-session-monitor.h"
 
30
 
 
31
#define CKDB_PATH "/var/run/ConsoleKit/database"
 
32
 
 
33
/* <internal>
 
34
 * SECTION:nm-session-monitor
 
35
 * @title: NMSessionMonitor
 
36
 * @short_description: Monitor sessions
 
37
 *
 
38
 * The #NMSessionMonitor class is a utility class to track and monitor sessions.
 
39
 */
 
40
 
 
41
struct _NMSessionMonitor {
 
42
        GObject parent_instance;
 
43
 
 
44
        GKeyFile *database;
 
45
        GFileMonitor *database_monitor;
 
46
        time_t database_mtime;
 
47
        GHashTable *sessions_by_uid;
 
48
        GHashTable *sessions_by_user;
 
49
};
 
50
 
 
51
struct _NMSessionMonitorClass {
 
52
        GObjectClass parent_class;
 
53
 
 
54
        void (*changed) (NMSessionMonitor *monitor);
 
55
};
 
56
 
 
57
 
 
58
enum {
 
59
        CHANGED,
 
60
        LAST_SIGNAL,
 
61
};
 
62
static guint signals[LAST_SIGNAL] = {0};
 
63
 
 
64
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
 
65
 
 
66
/********************************************************************/
 
67
 
 
68
typedef struct {
 
69
        char *user;
 
70
        uid_t uid;
 
71
        gboolean local;
 
72
        gboolean active;
 
73
} Session;
 
74
 
 
75
static void
 
76
session_free (Session *s)
 
77
{
 
78
        g_free (s->user);
 
79
        memset (s, 0, sizeof (Session));
 
80
        g_free (s);
 
81
}
 
82
 
 
83
static gboolean
 
84
check_key (GKeyFile *keyfile, const char *group, const char *key, GError **error)
 
85
{
 
86
        if (g_key_file_has_key (keyfile, group, key, error))
 
87
                return TRUE;
 
88
 
 
89
        if (!error) {
 
90
                g_set_error (error,
 
91
                                 NM_SESSION_MONITOR_ERROR,
 
92
                                 NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
 
93
                                 "ConsoleKit database " CKDB_PATH " group '%s' had no '%s' key",
 
94
                                 group, key);
 
95
        }
 
96
        return FALSE;
 
97
}
 
98
 
 
99
static Session *
 
100
session_new (GKeyFile *keyfile, const char *group, GError **error)
 
101
{
 
102
        GError *local = NULL;
 
103
        Session *s;
 
104
        const char *uname = NULL;
 
105
 
 
106
        s = g_new0 (Session, 1);
 
107
        g_assert (s);
 
108
 
 
109
        s->uid = G_MAXUINT; /* paranoia */
 
110
        if (!check_key (keyfile, group, "uid", &local))
 
111
                goto error;
 
112
        s->uid = (uid_t) g_key_file_get_integer (keyfile, group, "uid", &local);
 
113
        if (local)
 
114
                goto error;
 
115
 
 
116
        if (!check_key (keyfile, group, "is_active", &local))
 
117
                goto error;
 
118
        s->active = g_key_file_get_boolean (keyfile, group, "is_active", &local);
 
119
        if (local)
 
120
                goto error;
 
121
 
 
122
        if (!check_key (keyfile, group, "is_local", &local))
 
123
                goto error;
 
124
        s->local = g_key_file_get_boolean (keyfile, group, "is_local", &local);
 
125
        if (local)
 
126
                goto error;
 
127
 
 
128
        if (!nm_session_uid_to_user (s->uid, &uname, error))
 
129
                return FALSE;
 
130
        s->user = g_strdup (uname);
 
131
 
 
132
        return s;
 
133
 
 
134
error:
 
135
        session_free (s);
 
136
        g_propagate_error (error, local);
 
137
        return NULL;
 
138
}
 
139
 
 
140
static void
 
141
session_merge (Session *src, Session *dest)
 
142
{
 
143
        g_return_if_fail (src != NULL);
 
144
        g_return_if_fail (dest != NULL);
 
145
 
 
146
        g_warn_if_fail (g_strcmp0 (src->user, dest->user) == 0);
 
147
        g_warn_if_fail (src->uid == dest->uid);
 
148
 
 
149
        dest->local = (dest->local || src->local);
 
150
        dest->active = (dest->active || src->active);
 
151
}
 
152
 
 
153
/********************************************************************/
 
154
 
 
155
static void
 
156
free_database (NMSessionMonitor *self)
 
157
{
 
158
        if (self->database != NULL) {
 
159
                g_key_file_free (self->database);
 
160
                self->database = NULL;
 
161
        }
 
162
 
 
163
        g_hash_table_remove_all (self->sessions_by_uid);
 
164
        g_hash_table_remove_all (self->sessions_by_user);
 
165
}
 
166
 
 
167
static gboolean
 
168
reload_database (NMSessionMonitor *self, GError **error)
 
169
{
 
170
        struct stat statbuf;
 
171
        char **groups = NULL;
 
172
        gsize len = 0, i;
 
173
        Session *s;
 
174
 
 
175
        free_database (self);
 
176
 
 
177
        errno = 0;
 
178
        if (stat (CKDB_PATH, &statbuf) != 0) {
 
179
                g_set_error (error,
 
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",
 
183
                             strerror (errno));
 
184
                goto error;
 
185
        }
 
186
        self->database_mtime = statbuf.st_mtime;
 
187
 
 
188
        self->database = g_key_file_new ();
 
189
        if (!g_key_file_load_from_file (self->database, CKDB_PATH, G_KEY_FILE_NONE, error))
 
190
                goto error;
 
191
 
 
192
        groups = g_key_file_get_groups (self->database, &len);
 
193
        if (!groups) {
 
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 "");
 
198
                goto error;
 
199
        }
 
200
 
 
201
        for (i = 0; i < len; i++) {
 
202
                Session *found;
 
203
 
 
204
                if (!g_str_has_prefix (groups[i], "Session "))
 
205
                        continue;
 
206
 
 
207
                s = session_new (self->database, groups[i], error);
 
208
                if (!s)
 
209
                        goto error;
 
210
 
 
211
                found = g_hash_table_lookup (self->sessions_by_user, (gpointer) s->user);
 
212
                if (found) {
 
213
                        session_merge (s, found);
 
214
                        session_free (s);
 
215
                } else {
 
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);
 
219
                }
 
220
        }
 
221
 
 
222
        g_strfreev (groups);
 
223
        return TRUE;
 
224
 
 
225
error:
 
226
        if (groups)
 
227
                g_strfreev (groups);
 
228
        free_database (self);
 
229
        return FALSE;
 
230
}
 
231
 
 
232
static gboolean
 
233
ensure_database (NMSessionMonitor *self, GError **error)
 
234
{
 
235
        gboolean ret = FALSE;
 
236
 
 
237
        if (self->database != NULL) {
 
238
                struct stat statbuf;
 
239
 
 
240
                errno = 0;
 
241
                if (stat (CKDB_PATH, &statbuf) != 0) {
 
242
                        g_set_error (error,
 
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",
 
246
                                     strerror (errno));
 
247
                        goto out;
 
248
                }
 
249
 
 
250
                if (statbuf.st_mtime == self->database_mtime) {
 
251
                        ret = TRUE;
 
252
                        goto out;
 
253
                }
 
254
        }
 
255
 
 
256
        ret = reload_database (self, error);
 
257
 
 
258
out:
 
259
        return ret;
 
260
}
 
261
 
 
262
static void
 
263
on_file_monitor_changed (GFileMonitor *    file_monitor,
 
264
                         GFile *           file,
 
265
                         GFile *           other_file,
 
266
                         GFileMonitorEvent event_type,
 
267
                         gpointer          user_data)
 
268
{
 
269
        NMSessionMonitor *self = NM_SESSION_MONITOR (user_data);
 
270
 
 
271
        /* throw away cache */
 
272
        free_database (self);
 
273
 
 
274
        g_signal_emit (self, signals[CHANGED], 0);
 
275
}
 
276
 
 
277
static void
 
278
nm_session_monitor_init (NMSessionMonitor *self)
 
279
{
 
280
        GError *error = NULL;
 
281
        GFile *file;
 
282
 
 
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);
 
287
 
 
288
 
 
289
        error = NULL;
 
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);
 
296
                }
 
297
                g_error_free (error);
 
298
        }
 
299
 
 
300
        error = NULL;
 
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);
 
307
        } else {
 
308
                g_signal_connect (self->database_monitor,
 
309
                                  "changed",
 
310
                                  G_CALLBACK (on_file_monitor_changed),
 
311
                                  self);
 
312
        }
 
313
}
 
314
 
 
315
static void
 
316
finalize (GObject *object)
 
317
{
 
318
        NMSessionMonitor *self = NM_SESSION_MONITOR (object);
 
319
 
 
320
        if (self->database_monitor != NULL)
 
321
                g_object_unref (self->database_monitor);
 
322
 
 
323
        free_database (self);
 
324
 
 
325
        if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
 
326
                G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
 
327
}
 
328
 
 
329
static void
 
330
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
 
331
{
 
332
        GObjectClass *gobject_class;
 
333
 
 
334
        gobject_class = G_OBJECT_CLASS (klass);
 
335
 
 
336
        gobject_class->finalize = finalize;
 
337
 
 
338
        /**
 
339
         * NMSessionMonitor::changed:
 
340
         * @monitor: A #NMSessionMonitor
 
341
         *
 
342
         * Emitted when something changes.
 
343
         */
 
344
        signals[CHANGED] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
 
345
                                         NM_TYPE_SESSION_MONITOR,
 
346
                                         G_SIGNAL_RUN_LAST,
 
347
                                         G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
 
348
                                         NULL,                   /* accumulator      */
 
349
                                         NULL,                   /* accumulator data */
 
350
                                         g_cclosure_marshal_VOID__VOID,
 
351
                                         G_TYPE_NONE, 0);
 
352
}
 
353
 
 
354
NMSessionMonitor *
 
355
nm_session_monitor_get (void)
 
356
{
 
357
        static NMSessionMonitor *singleton = NULL;
 
358
 
 
359
        if (singleton)
 
360
                return g_object_ref (singleton);
 
361
 
 
362
        singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
 
363
        return singleton;
 
364
}
 
365
 
 
366
/* ---------------------------------------------------------------------------------------------------- */
 
367
 
 
368
/**
 
369
 * nm_session_monitor_user_has_session:
 
370
 * @monitor: A #NMSessionMonitor.
 
371
 * @username: A username.
 
372
 * @error: Return location for error.
 
373
 *
 
374
 * Checks whether the given @username is logged into a session or not.
 
375
 *
 
376
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
 
377
 * currently logged into a session.
 
378
 */
 
379
gboolean
 
380
nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
 
381
                                     const char *username,
 
382
                                     uid_t *out_uid,
 
383
                                     GError **error)
 
384
{
 
385
        Session *s;
 
386
 
 
387
        if (!ensure_database (monitor, error))
 
388
                return FALSE;
 
389
 
 
390
        s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
 
391
        if (!s) {
 
392
                g_set_error (error,
 
393
                             NM_SESSION_MONITOR_ERROR,
 
394
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
 
395
                             "No session found for user '%s'",
 
396
                             username);
 
397
                return FALSE;
 
398
        }
 
399
 
 
400
        if (out_uid)
 
401
                *out_uid = s->uid;
 
402
        return TRUE;
 
403
}
 
404
 
 
405
/**
 
406
 * nm_session_monitor_uid_has_session:
 
407
 * @monitor: A #NMSessionMonitor.
 
408
 * @uid: A user ID.
 
409
 * @error: Return location for error.
 
410
 *
 
411
 * Checks whether the given @uid is logged into a session or not.
 
412
 *
 
413
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
 
414
 * currently logged into a session.
 
415
 */
 
416
gboolean
 
417
nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
 
418
                                    uid_t uid,
 
419
                                    const char **out_user,
 
420
                                    GError **error)
 
421
{
 
422
        Session *s;
 
423
 
 
424
        if (!ensure_database (monitor, error))
 
425
                return FALSE;
 
426
 
 
427
        s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
 
428
        if (!s) {
 
429
                g_set_error (error,
 
430
                             NM_SESSION_MONITOR_ERROR,
 
431
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
 
432
                             "No session found for uid %d",
 
433
                             uid);
 
434
                return FALSE;
 
435
        }
 
436
 
 
437
        if (out_user)
 
438
                *out_user = s->user;
 
439
        return TRUE;
 
440
}
 
441
 
 
442
/**
 
443
 * nm_session_monitor_user_active:
 
444
 * @monitor: A #NMSessionMonitor.
 
445
 * @username: A username.
 
446
 * @error: Return location for error.
 
447
 *
 
448
 * Checks whether the given @username is logged into a active session or not.
 
449
 *
 
450
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
 
451
 * logged into an active session.
 
452
 */
 
453
gboolean
 
454
nm_session_monitor_user_active (NMSessionMonitor *monitor,
 
455
                                const char *username,
 
456
                                GError **error)
 
457
{
 
458
        Session *s;
 
459
 
 
460
        if (!ensure_database (monitor, error))
 
461
                return FALSE;
 
462
 
 
463
        s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
 
464
        if (!s) {
 
465
                g_set_error (error,
 
466
                             NM_SESSION_MONITOR_ERROR,
 
467
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
 
468
                             "No session found for user '%s'",
 
469
                             username);
 
470
                return FALSE;
 
471
        }
 
472
 
 
473
        return s->active;
 
474
}
 
475
 
 
476
/**
 
477
 * nm_session_monitor_uid_active:
 
478
 * @monitor: A #NMSessionMonitor.
 
479
 * @uid: A user ID.
 
480
 * @error: Return location for error.
 
481
 *
 
482
 * Checks whether the given @uid is logged into a active session or not.
 
483
 *
 
484
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
 
485
 * logged into an active session.
 
486
 */
 
487
gboolean
 
488
nm_session_monitor_uid_active (NMSessionMonitor *monitor,
 
489
                               uid_t uid,
 
490
                               GError **error)
 
491
{
 
492
        Session *s;
 
493
 
 
494
        if (!ensure_database (monitor, error))
 
495
                return FALSE;
 
496
 
 
497
        s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
 
498
        if (!s) {
 
499
                g_set_error (error,
 
500
                             NM_SESSION_MONITOR_ERROR,
 
501
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
 
502
                             "No session found for uid '%d'",
 
503
                             uid);
 
504
                return FALSE;
 
505
        }
 
506
 
 
507
        return s->active;
 
508
}
 
509