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

« back to all changes in this revision

Viewing changes to src/nm-session-monitor-systemd.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
/*
 
3
 * Copyright (C) 2011 Red Hat, Inc.
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General
 
16
 * Public License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Author: Matthias Clasen
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
#include <errno.h>
 
25
#include <pwd.h>
 
26
#include <grp.h>
 
27
#include <string.h>
 
28
#include <glib/gstdio.h>
 
29
#include <systemd/sd-login.h>
 
30
#include <stdlib.h>
 
31
 
 
32
#include "nm-session-utils.h"
 
33
#include "nm-session-monitor.h"
 
34
 
 
35
/********************************************************************/
 
36
 
 
37
typedef struct {
 
38
        GSource source;
 
39
        GPollFD pollfd;
 
40
        sd_login_monitor *monitor;
 
41
} SdSource;
 
42
 
 
43
static gboolean
 
44
sd_source_prepare (GSource *source, gint *timeout)
 
45
{
 
46
        *timeout = -1;
 
47
        return FALSE;
 
48
}
 
49
 
 
50
static gboolean
 
51
sd_source_check (GSource *source)
 
52
{
 
53
        SdSource *sd_source = (SdSource *) source;
 
54
 
 
55
        return sd_source->pollfd.revents != 0;
 
56
}
 
57
 
 
58
static gboolean
 
59
sd_source_dispatch (GSource     *source,
 
60
                    GSourceFunc  callback,
 
61
                    gpointer     user_data)
 
62
 
 
63
{
 
64
        SdSource *sd_source = (SdSource *)source;
 
65
        gboolean ret;
 
66
 
 
67
        g_warn_if_fail (callback != NULL);
 
68
        ret = (*callback) (user_data);
 
69
        sd_login_monitor_flush (sd_source->monitor);
 
70
        return ret;
 
71
}
 
72
 
 
73
static void
 
74
sd_source_finalize (GSource *source)
 
75
{
 
76
        SdSource *sd_source = (SdSource*) source;
 
77
 
 
78
        sd_login_monitor_unref (sd_source->monitor);
 
79
}
 
80
 
 
81
static GSourceFuncs sd_source_funcs = {
 
82
        sd_source_prepare,
 
83
        sd_source_check,
 
84
        sd_source_dispatch,
 
85
        sd_source_finalize
 
86
};
 
87
 
 
88
static GSource *
 
89
sd_source_new (void)
 
90
{
 
91
        GSource *source;
 
92
        SdSource *sd_source;
 
93
        int ret;
 
94
 
 
95
        source = g_source_new (&sd_source_funcs, sizeof (SdSource));
 
96
        sd_source = (SdSource *)source;
 
97
 
 
98
        ret = sd_login_monitor_new (NULL, &sd_source->monitor);
 
99
        if (ret < 0)
 
100
                g_printerr ("Error getting login monitor: %d", ret);
 
101
        else {
 
102
                sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
 
103
                sd_source->pollfd.events = G_IO_IN;
 
104
                g_source_add_poll (source, &sd_source->pollfd);
 
105
        }
 
106
 
 
107
        return source;
 
108
}
 
109
 
 
110
struct _NMSessionMonitor {
 
111
        GObject parent_instance;
 
112
 
 
113
        GSource *sd_source;
 
114
};
 
115
 
 
116
struct _NMSessionMonitorClass {
 
117
        GObjectClass parent_class;
 
118
 
 
119
        void (*changed) (NMSessionMonitor *monitor);
 
120
};
 
121
 
 
122
 
 
123
enum {
 
124
        CHANGED_SIGNAL,
 
125
        LAST_SIGNAL,
 
126
};
 
127
static guint signals[LAST_SIGNAL] = {0};
 
128
 
 
129
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
 
130
 
 
131
/* ---------------------------------------------------------------------------------------------------- */
 
132
 
 
133
static gboolean
 
134
sessions_changed (gpointer user_data)
 
135
{
 
136
        NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
 
137
 
 
138
        g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
 
139
        return TRUE;
 
140
}
 
141
 
 
142
 
 
143
static void
 
144
nm_session_monitor_init (NMSessionMonitor *monitor)
 
145
{
 
146
        monitor->sd_source = sd_source_new ();
 
147
        g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
 
148
        g_source_attach (monitor->sd_source, NULL);
 
149
}
 
150
 
 
151
static void
 
152
nm_session_monitor_finalize (GObject *object)
 
153
{
 
154
        NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
 
155
 
 
156
        if (monitor->sd_source != NULL) {
 
157
                g_source_destroy (monitor->sd_source);
 
158
                g_source_unref (monitor->sd_source);
 
159
        }
 
160
 
 
161
        if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
 
162
                G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
 
163
}
 
164
 
 
165
static void
 
166
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
 
167
{
 
168
        GObjectClass *gobject_class;
 
169
 
 
170
        gobject_class = G_OBJECT_CLASS (klass);
 
171
        gobject_class->finalize = nm_session_monitor_finalize;
 
172
 
 
173
        /**
 
174
         * NMSessionMonitor::changed:
 
175
         * @monitor: A #NMSessionMonitor
 
176
         *
 
177
         * Emitted when something changes.
 
178
         */
 
179
        signals[CHANGED_SIGNAL] = g_signal_new ("changed",
 
180
                                                NM_TYPE_SESSION_MONITOR,
 
181
                                                G_SIGNAL_RUN_LAST,
 
182
                                                G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
 
183
                                                NULL,                   /* accumulator      */
 
184
                                                NULL,                   /* accumulator data */
 
185
                                                g_cclosure_marshal_VOID__VOID,
 
186
                                                G_TYPE_NONE,
 
187
                                                0);
 
188
}
 
189
 
 
190
NMSessionMonitor *
 
191
nm_session_monitor_get (void)
 
192
{
 
193
        static NMSessionMonitor *singleton = NULL;
 
194
 
 
195
        if (singleton)
 
196
                return g_object_ref (singleton);
 
197
 
 
198
        singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
 
199
        g_assert (singleton);
 
200
        return singleton;
 
201
}
 
202
 
 
203
gboolean
 
204
nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
 
205
                                     const char *username,
 
206
                                     uid_t *out_uid,
 
207
                                     GError **error)
 
208
{
 
209
        uid_t uid;
 
210
 
 
211
        if (!nm_session_user_to_uid (username, &uid, error))
 
212
                return FALSE;
 
213
 
 
214
        if (out_uid)
 
215
                *out_uid = uid;
 
216
 
 
217
        return nm_session_monitor_uid_has_session (monitor, uid, NULL, error);
 
218
}
 
219
 
 
220
gboolean
 
221
nm_session_monitor_user_active (NMSessionMonitor *monitor,
 
222
                                const char *username,
 
223
                                GError **error)
 
224
{
 
225
        uid_t uid;
 
226
 
 
227
        if (!nm_session_user_to_uid (username, &uid, error))
 
228
                return FALSE;
 
229
 
 
230
        return nm_session_monitor_uid_active (monitor, uid, error);
 
231
}
 
232
 
 
233
gboolean
 
234
nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
 
235
                                    uid_t uid,
 
236
                                    const char **out_user,
 
237
                                    GError **error)
 
238
{
 
239
        if (!nm_session_uid_to_user (uid, out_user, error))
 
240
                return FALSE;
 
241
 
 
242
        return sd_uid_get_sessions (uid, FALSE, NULL) > 0;
 
243
}
 
244
 
 
245
gboolean
 
246
nm_session_monitor_uid_active (NMSessionMonitor *monitor,
 
247
                               uid_t uid,
 
248
                               GError **error)
 
249
{
 
250
        return sd_uid_get_sessions (uid, TRUE, NULL) > 0;
 
251
}