1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3
* Copyright (C) 2011 Red Hat, Inc.
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.
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.
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.
20
* Author: Matthias Clasen
28
#include <glib/gstdio.h>
29
#include <systemd/sd-login.h>
32
#include "nm-session-utils.h"
33
#include "nm-session-monitor.h"
35
/********************************************************************/
40
sd_login_monitor *monitor;
44
sd_source_prepare (GSource *source, gint *timeout)
51
sd_source_check (GSource *source)
53
SdSource *sd_source = (SdSource *) source;
55
return sd_source->pollfd.revents != 0;
59
sd_source_dispatch (GSource *source,
64
SdSource *sd_source = (SdSource *)source;
67
g_warn_if_fail (callback != NULL);
68
ret = (*callback) (user_data);
69
sd_login_monitor_flush (sd_source->monitor);
74
sd_source_finalize (GSource *source)
76
SdSource *sd_source = (SdSource*) source;
78
sd_login_monitor_unref (sd_source->monitor);
81
static GSourceFuncs sd_source_funcs = {
95
source = g_source_new (&sd_source_funcs, sizeof (SdSource));
96
sd_source = (SdSource *)source;
98
ret = sd_login_monitor_new (NULL, &sd_source->monitor);
100
g_printerr ("Error getting login monitor: %d", ret);
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);
110
struct _NMSessionMonitor {
111
GObject parent_instance;
116
struct _NMSessionMonitorClass {
117
GObjectClass parent_class;
119
void (*changed) (NMSessionMonitor *monitor);
127
static guint signals[LAST_SIGNAL] = {0};
129
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
131
/* ---------------------------------------------------------------------------------------------------- */
134
sessions_changed (gpointer user_data)
136
NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
138
g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
144
nm_session_monitor_init (NMSessionMonitor *monitor)
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);
152
nm_session_monitor_finalize (GObject *object)
154
NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
156
if (monitor->sd_source != NULL) {
157
g_source_destroy (monitor->sd_source);
158
g_source_unref (monitor->sd_source);
161
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
162
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
166
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
168
GObjectClass *gobject_class;
170
gobject_class = G_OBJECT_CLASS (klass);
171
gobject_class->finalize = nm_session_monitor_finalize;
174
* NMSessionMonitor::changed:
175
* @monitor: A #NMSessionMonitor
177
* Emitted when something changes.
179
signals[CHANGED_SIGNAL] = g_signal_new ("changed",
180
NM_TYPE_SESSION_MONITOR,
182
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
183
NULL, /* accumulator */
184
NULL, /* accumulator data */
185
g_cclosure_marshal_VOID__VOID,
191
nm_session_monitor_get (void)
193
static NMSessionMonitor *singleton = NULL;
196
return g_object_ref (singleton);
198
singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
199
g_assert (singleton);
204
nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
205
const char *username,
211
if (!nm_session_user_to_uid (username, &uid, error))
217
return nm_session_monitor_uid_has_session (monitor, uid, NULL, error);
221
nm_session_monitor_user_active (NMSessionMonitor *monitor,
222
const char *username,
227
if (!nm_session_user_to_uid (username, &uid, error))
230
return nm_session_monitor_uid_active (monitor, uid, error);
234
nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
236
const char **out_user,
239
if (!nm_session_uid_to_user (uid, out_user, error))
242
return sd_uid_get_sessions (uid, FALSE, NULL) > 0;
246
nm_session_monitor_uid_active (NMSessionMonitor *monitor,
250
return sd_uid_get_sessions (uid, TRUE, NULL) > 0;