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

« back to all changes in this revision

Viewing changes to src/nm-session-monitor.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 <pwd.h>
24
 
#include <string.h>
25
 
#include <sys/types.h>
26
 
#include <sys/stat.h>
27
 
#include <gio/gio.h>
28
 
#include "nm-logging.h"
29
 
 
30
 
#include "nm-session-monitor.h"
31
 
 
32
 
#define CKDB_PATH "/var/run/ConsoleKit/database"
33
 
 
34
 
/* <internal>
35
 
 * SECTION:nm-session-monitor
36
 
 * @title: NMSessionMonitor
37
 
 * @short_description: Monitor sessions
38
 
 *
39
 
 * The #NMSessionMonitor class is a utility class to track and monitor sessions.
40
 
 */
41
 
 
42
 
struct _NMSessionMonitor {
43
 
        GObject parent_instance;
44
 
 
45
 
        GKeyFile *database;
46
 
        GFileMonitor *database_monitor;
47
 
        time_t database_mtime;
48
 
        GHashTable *sessions_by_uid;
49
 
        GHashTable *sessions_by_user;
50
 
};
51
 
 
52
 
struct _NMSessionMonitorClass {
53
 
        GObjectClass parent_class;
54
 
 
55
 
        void (*changed) (NMSessionMonitor *monitor);
56
 
};
57
 
 
58
 
 
59
 
enum {
60
 
        CHANGED,
61
 
        LAST_SIGNAL,
62
 
};
63
 
static guint signals[LAST_SIGNAL] = {0};
64
 
 
65
 
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
66
 
 
67
 
/********************************************************************/
68
 
 
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;
72
 
 
73
 
typedef enum {
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;
79
 
 
80
 
GQuark
81
 
nm_session_monitor_error_quark (void)
82
 
{
83
 
        static GQuark ret = 0;
84
 
 
85
 
        if (G_UNLIKELY (ret == 0))
86
 
                ret = g_quark_from_static_string ("nm-session-monitor-error");
87
 
        return ret;
88
 
}
89
 
 
90
 
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
91
 
 
92
 
GType
93
 
nm_session_monitor_error_get_type (void)
94
 
{
95
 
        static GType etype = 0;
96
 
 
97
 
        if (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"),
107
 
                        { 0, 0, 0 }
108
 
                };
109
 
 
110
 
                etype = g_enum_register_static ("NMSessionMonitorError", values);
111
 
        }
112
 
        return etype;
113
 
}
114
 
/********************************************************************/
115
 
 
116
 
typedef struct {
117
 
        char *user;
118
 
        uid_t uid;
119
 
        gboolean local;
120
 
        gboolean active;
121
 
} Session;
122
 
 
123
 
static void
124
 
session_free (Session *s)
125
 
{
126
 
        g_free (s->user);
127
 
        memset (s, 0, sizeof (Session));
128
 
        g_free (s);
129
 
}
130
 
 
131
 
static gboolean
132
 
check_key (GKeyFile *keyfile, const char *group, const char *key, GError **error)
133
 
{
134
 
        if (g_key_file_has_key (keyfile, group, key, error))
135
 
                return TRUE;
136
 
 
137
 
        if (!error) {
138
 
                g_set_error (error,
139
 
                                 NM_SESSION_MONITOR_ERROR,
140
 
                                 NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
141
 
                                 "ConsoleKit database " CKDB_PATH " group '%s' had no '%s' key",
142
 
                                 group, key);
143
 
        }
144
 
        return FALSE;
145
 
}
146
 
 
147
 
static Session *
148
 
session_new (GKeyFile *keyfile, const char *group, GError **error)
149
 
{
150
 
        GError *local = NULL;
151
 
        Session *s;
152
 
        struct passwd *pw;
153
 
 
154
 
        s = g_new0 (Session, 1);
155
 
        g_assert (s);
156
 
 
157
 
        s->uid = G_MAXUINT; /* paranoia */
158
 
        if (!check_key (keyfile, group, "uid", &local))
159
 
                goto error;
160
 
        s->uid = (uid_t) g_key_file_get_integer (keyfile, group, "uid", &local);
161
 
        if (local)
162
 
                goto error;
163
 
 
164
 
        if (!check_key (keyfile, group, "is_active", &local))
165
 
                goto error;
166
 
        s->active = g_key_file_get_boolean (keyfile, group, "is_active", &local);
167
 
        if (local)
168
 
                goto error;
169
 
 
170
 
        if (!check_key (keyfile, group, "is_local", &local))
171
 
                goto error;
172
 
        s->local = g_key_file_get_boolean (keyfile, group, "is_local", &local);
173
 
        if (local)
174
 
                goto error;
175
 
 
176
 
        pw = getpwuid (s->uid);
177
 
        if (!pw) {
178
 
                g_set_error (&local,
179
 
                                 NM_SESSION_MONITOR_ERROR,
180
 
                                 NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
181
 
                                 "Could not get username for UID %d",
182
 
                                 s->uid);
183
 
                goto error;
184
 
        }
185
 
        s->user = g_strdup (pw->pw_name);
186
 
 
187
 
        return s;
188
 
 
189
 
error:
190
 
        session_free (s);
191
 
        g_propagate_error (error, local);
192
 
        return NULL;
193
 
}
194
 
 
195
 
static void
196
 
session_merge (Session *src, Session *dest)
197
 
{
198
 
        g_return_if_fail (src != NULL);
199
 
        g_return_if_fail (dest != NULL);
200
 
 
201
 
        g_warn_if_fail (g_strcmp0 (src->user, dest->user) == 0);
202
 
        g_warn_if_fail (src->uid == dest->uid);
203
 
 
204
 
        dest->local = (dest->local || src->local);
205
 
        dest->active = (dest->active || src->active);
206
 
}
207
 
 
208
 
/********************************************************************/
209
 
 
210
 
static void
211
 
free_database (NMSessionMonitor *self)
212
 
{
213
 
        if (self->database != NULL) {
214
 
                g_key_file_free (self->database);
215
 
                self->database = NULL;
216
 
        }
217
 
 
218
 
        g_hash_table_remove_all (self->sessions_by_uid);
219
 
        g_hash_table_remove_all (self->sessions_by_user);
220
 
}
221
 
 
222
 
static gboolean
223
 
reload_database (NMSessionMonitor *self, GError **error)
224
 
{
225
 
        struct stat statbuf;
226
 
        char **groups = NULL;
227
 
        gsize len = 0, i;
228
 
        Session *s;
229
 
 
230
 
        free_database (self);
231
 
 
232
 
        errno = 0;
233
 
        if (stat (CKDB_PATH, &statbuf) != 0) {
234
 
                g_set_error (error,
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",
238
 
                             strerror (errno));
239
 
                goto error;
240
 
        }
241
 
        self->database_mtime = statbuf.st_mtime;
242
 
 
243
 
        self->database = g_key_file_new ();
244
 
        if (!g_key_file_load_from_file (self->database, CKDB_PATH, G_KEY_FILE_NONE, error))
245
 
                goto error;
246
 
 
247
 
        groups = g_key_file_get_groups (self->database, &len);
248
 
        if (!groups) {
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 "");
253
 
                goto error;
254
 
        }
255
 
 
256
 
        for (i = 0; i < len; i++) {
257
 
                Session *found;
258
 
 
259
 
                if (!g_str_has_prefix (groups[i], "Session "))
260
 
                        continue;
261
 
 
262
 
                s = session_new (self->database, groups[i], error);
263
 
                if (!s)
264
 
                        goto error;
265
 
 
266
 
                found = g_hash_table_lookup (self->sessions_by_user, (gpointer) s->user);
267
 
                if (found) {
268
 
                        session_merge (s, found);
269
 
                        session_free (s);
270
 
                } else {
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);
274
 
                }
275
 
        }
276
 
 
277
 
        g_strfreev (groups);
278
 
        return TRUE;
279
 
 
280
 
error:
281
 
        if (groups)
282
 
                g_strfreev (groups);
283
 
        free_database (self);
284
 
        return FALSE;
285
 
}
286
 
 
287
 
static gboolean
288
 
ensure_database (NMSessionMonitor *self, GError **error)
289
 
{
290
 
        gboolean ret = FALSE;
291
 
 
292
 
#if NO_CONSOLEKIT
293
 
        return TRUE;
294
 
#endif
295
 
 
296
 
        if (self->database != NULL) {
297
 
                struct stat statbuf;
298
 
 
299
 
                errno = 0;
300
 
                if (stat (CKDB_PATH, &statbuf) != 0) {
301
 
                        g_set_error (error,
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",
305
 
                                     strerror (errno));
306
 
                        goto out;
307
 
                }
308
 
 
309
 
                if (statbuf.st_mtime == self->database_mtime) {
310
 
                        ret = TRUE;
311
 
                        goto out;
312
 
                }
313
 
        }
314
 
 
315
 
        ret = reload_database (self, error);
316
 
 
317
 
out:
318
 
        return ret;
319
 
}
320
 
 
321
 
static void
322
 
on_file_monitor_changed (GFileMonitor *    file_monitor,
323
 
                         GFile *           file,
324
 
                         GFile *           other_file,
325
 
                         GFileMonitorEvent event_type,
326
 
                         gpointer          user_data)
327
 
{
328
 
        NMSessionMonitor *self = NM_SESSION_MONITOR (user_data);
329
 
 
330
 
        /* throw away cache */
331
 
        free_database (self);
332
 
 
333
 
        g_signal_emit (self, signals[CHANGED], 0);
334
 
}
335
 
 
336
 
static void
337
 
nm_session_monitor_init (NMSessionMonitor *self)
338
 
{
339
 
        GError *error = NULL;
340
 
        GFile *file;
341
 
 
342
 
#if NO_CONSOLEKIT
343
 
        return;
344
 
#endif
345
 
 
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);
350
 
 
351
 
 
352
 
        error = NULL;
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);
359
 
                }
360
 
                g_error_free (error);
361
 
        }
362
 
 
363
 
        error = NULL;
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);
370
 
        } else {
371
 
                g_signal_connect (self->database_monitor,
372
 
                                  "changed",
373
 
                                  G_CALLBACK (on_file_monitor_changed),
374
 
                                  self);
375
 
        }
376
 
}
377
 
 
378
 
static void
379
 
finalize (GObject *object)
380
 
{
381
 
        NMSessionMonitor *self = NM_SESSION_MONITOR (object);
382
 
 
383
 
        if (self->database_monitor != NULL)
384
 
                g_object_unref (self->database_monitor);
385
 
 
386
 
        free_database (self);
387
 
 
388
 
        if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
389
 
                G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
390
 
}
391
 
 
392
 
static void
393
 
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
394
 
{
395
 
        GObjectClass *gobject_class;
396
 
 
397
 
        gobject_class = G_OBJECT_CLASS (klass);
398
 
 
399
 
        gobject_class->finalize = finalize;
400
 
 
401
 
        /**
402
 
         * NMSessionMonitor::changed:
403
 
         * @monitor: A #NMSessionMonitor
404
 
         *
405
 
         * Emitted when something changes.
406
 
         */
407
 
        signals[CHANGED] = g_signal_new (NM_SESSION_MONITOR_CHANGED,
408
 
                                         NM_TYPE_SESSION_MONITOR,
409
 
                                         G_SIGNAL_RUN_LAST,
410
 
                                         G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
411
 
                                         NULL,                   /* accumulator      */
412
 
                                         NULL,                   /* accumulator data */
413
 
                                         g_cclosure_marshal_VOID__VOID,
414
 
                                         G_TYPE_NONE, 0);
415
 
}
416
 
 
417
 
NMSessionMonitor *
418
 
nm_session_monitor_get (void)
419
 
{
420
 
        static NMSessionMonitor *singleton = NULL;
421
 
 
422
 
        if (singleton)
423
 
                return g_object_ref (singleton);
424
 
 
425
 
        singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
426
 
        return singleton;
427
 
}
428
 
 
429
 
/* ---------------------------------------------------------------------------------------------------- */
430
 
 
431
 
#if NO_CONSOLEKIT
432
 
static gboolean
433
 
uid_to_user (uid_t uid, const char **out_user, GError **error)
434
 
{
435
 
        struct passwd *pw;
436
 
 
437
 
        pw = getpwuid (uid);
438
 
        if (!pw) {
439
 
                g_set_error (error,
440
 
                                 NM_SESSION_MONITOR_ERROR,
441
 
                                 NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
442
 
                                 "Could not get username for UID %d",
443
 
                                 uid);
444
 
                return FALSE;
445
 
        }
446
 
 
447
 
        /* Ugly, but hey, use ConsoleKit */
448
 
        if (out_user)
449
 
                *out_user = pw->pw_name;
450
 
        return TRUE;
451
 
}
452
 
 
453
 
static gboolean
454
 
user_to_uid (const char *user, uid_t *out_uid, GError **error)
455
 
{
456
 
        struct passwd *pw;
457
 
 
458
 
        pw = getpwnam (user);
459
 
        if (!pw) {
460
 
                g_set_error (error,
461
 
                                 NM_SESSION_MONITOR_ERROR,
462
 
                                 NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
463
 
                                 "Could not get UID for username '%s'",
464
 
                                 user);
465
 
                return FALSE;
466
 
        }
467
 
 
468
 
        /* Ugly, but hey, use ConsoleKit */
469
 
        if (out_uid)
470
 
                *out_uid = pw->pw_uid;
471
 
        return TRUE;
472
 
}
473
 
#endif
474
 
 
475
 
/**
476
 
 * nm_session_monitor_user_has_session:
477
 
 * @monitor: A #NMSessionMonitor.
478
 
 * @username: A username.
479
 
 * @error: Return location for error.
480
 
 *
481
 
 * Checks whether the given @username is logged into a session or not.
482
 
 *
483
 
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
484
 
 * currently logged into a session.
485
 
 */
486
 
gboolean
487
 
nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
488
 
                                     const char *username,
489
 
                                     uid_t *out_uid,
490
 
                                     GError **error)
491
 
{
492
 
        Session *s;
493
 
 
494
 
#if NO_CONSOLEKIT
495
 
        if (!user_to_uid (username, out_uid, error))
496
 
                return FALSE;
497
 
        return TRUE;
498
 
#endif
499
 
 
500
 
        if (!ensure_database (monitor, error))
501
 
                return FALSE;
502
 
 
503
 
        s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
504
 
        if (!s) {
505
 
                g_set_error (error,
506
 
                             NM_SESSION_MONITOR_ERROR,
507
 
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
508
 
                             "No session found for user '%s'",
509
 
                             username);
510
 
                return FALSE;
511
 
        }
512
 
 
513
 
        if (out_uid)
514
 
                *out_uid = s->uid;
515
 
        return TRUE;
516
 
}
517
 
 
518
 
/**
519
 
 * nm_session_monitor_uid_has_session:
520
 
 * @monitor: A #NMSessionMonitor.
521
 
 * @uid: A user ID.
522
 
 * @error: Return location for error.
523
 
 *
524
 
 * Checks whether the given @uid is logged into a session or not.
525
 
 *
526
 
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
527
 
 * currently logged into a session.
528
 
 */
529
 
gboolean
530
 
nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
531
 
                                    uid_t uid,
532
 
                                    const char **out_user,
533
 
                                    GError **error)
534
 
{
535
 
        Session *s;
536
 
 
537
 
#if NO_CONSOLEKIT
538
 
        if (!uid_to_user (uid, out_user, error))
539
 
                return FALSE;
540
 
        return TRUE;
541
 
#endif
542
 
 
543
 
        if (!ensure_database (monitor, error))
544
 
                return FALSE;
545
 
 
546
 
        s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
547
 
        if (!s) {
548
 
                g_set_error (error,
549
 
                             NM_SESSION_MONITOR_ERROR,
550
 
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
551
 
                             "No session found for uid %d",
552
 
                             uid);
553
 
                return FALSE;
554
 
        }
555
 
 
556
 
        if (out_user)
557
 
                *out_user = s->user;
558
 
        return TRUE;
559
 
}
560
 
 
561
 
/**
562
 
 * nm_session_monitor_user_active:
563
 
 * @monitor: A #NMSessionMonitor.
564
 
 * @username: A username.
565
 
 * @error: Return location for error.
566
 
 *
567
 
 * Checks whether the given @username is logged into a active session or not.
568
 
 *
569
 
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @username is
570
 
 * logged into an active session.
571
 
 */
572
 
gboolean
573
 
nm_session_monitor_user_active (NMSessionMonitor *monitor,
574
 
                                const char *username,
575
 
                                GError **error)
576
 
{
577
 
        Session *s;
578
 
 
579
 
#if NO_CONSOLEKIT
580
 
        return TRUE;
581
 
#endif
582
 
 
583
 
        if (!ensure_database (monitor, error))
584
 
                return FALSE;
585
 
 
586
 
        s = g_hash_table_lookup (monitor->sessions_by_user, (gpointer) username);
587
 
        if (!s) {
588
 
                g_set_error (error,
589
 
                             NM_SESSION_MONITOR_ERROR,
590
 
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
591
 
                             "No session found for user '%s'",
592
 
                             username);
593
 
                return FALSE;
594
 
        }
595
 
 
596
 
        return s->active;
597
 
}
598
 
 
599
 
/**
600
 
 * nm_session_monitor_uid_active:
601
 
 * @monitor: A #NMSessionMonitor.
602
 
 * @uid: A user ID.
603
 
 * @error: Return location for error.
604
 
 *
605
 
 * Checks whether the given @uid is logged into a active session or not.
606
 
 *
607
 
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
608
 
 * logged into an active session.
609
 
 */
610
 
gboolean
611
 
nm_session_monitor_uid_active (NMSessionMonitor *monitor,
612
 
                               uid_t uid,
613
 
                               GError **error)
614
 
{
615
 
        Session *s;
616
 
 
617
 
#if NO_CONSOLEKIT
618
 
        return TRUE;
619
 
#endif
620
 
 
621
 
        if (!ensure_database (monitor, error))
622
 
                return FALSE;
623
 
 
624
 
        s = g_hash_table_lookup (monitor->sessions_by_uid, GUINT_TO_POINTER (uid));
625
 
        if (!s) {
626
 
                g_set_error (error,
627
 
                             NM_SESSION_MONITOR_ERROR,
628
 
                             NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
629
 
                             "No session found for uid '%d'",
630
 
                             uid);
631
 
                return FALSE;
632
 
        }
633
 
 
634
 
        return s->active;
635
 
}
636