core: combine common session management utility code

And reformat the systemd session manager for NM code style.
This commit is contained in:
Dan Williams 2012-02-01 15:34:32 -06:00
parent 3b75a97ab0
commit e133405a29
5 changed files with 246 additions and 270 deletions

View file

@ -183,7 +183,9 @@ NetworkManager_SOURCES = \
nm-dhcp6-config.c \
nm-dhcp6-config.h \
nm-rfkill.h \
nm-session-monitor.h
nm-session-monitor.h \
nm-session-utils.c \
nm-session-utils.h
if WITH_SYSTEMD
NetworkManager_SOURCES += \

View file

@ -1,3 +1,4 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2011 Red Hat, Inc.
*
@ -28,69 +29,30 @@
#include <systemd/sd-login.h>
#include <stdlib.h>
#include "nm-session-utils.h"
#include "nm-session-monitor.h"
/********************************************************************/
#define NM_SESSION_MONITOR_ERROR (nm_session_monitor_error_quark ())
GQuark nm_session_monitor_error_quark (void) G_GNUC_CONST;
GType nm_session_monitor_error_get_type (void) G_GNUC_CONST;
typedef enum {
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER
} NMSessionMonitorError;
GQuark
nm_session_monitor_error_quark (void)
{
static GQuark ret = 0;
if (G_UNLIKELY (ret == 0))
ret = g_quark_from_static_string ("nm-session-monitor-error");
return ret;
}
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
GType
nm_session_monitor_error_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
/* Username or UID could could not be found */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_UNKNOWN_USER, "UnknownUser"),
{ 0, 0, 0 }
};
etype = g_enum_register_static ("NMSessionMonitorError", values);
}
return etype;
}
/********************************************************************/
typedef struct
{
GSource source;
GPollFD pollfd;
sd_login_monitor *monitor;
typedef struct {
GSource source;
GPollFD pollfd;
sd_login_monitor *monitor;
} SdSource;
static gboolean
sd_source_prepare (GSource *source,
gint *timeout)
sd_source_prepare (GSource *source, gint *timeout)
{
*timeout = -1;
return FALSE;
*timeout = -1;
return FALSE;
}
static gboolean
sd_source_check (GSource *source)
{
SdSource *sd_source = (SdSource *)source;
SdSource *sd_source = (SdSource *) source;
return sd_source->pollfd.revents != 0;
return sd_source->pollfd.revents != 0;
}
static gboolean
@ -99,78 +61,69 @@ sd_source_dispatch (GSource *source,
gpointer user_data)
{
SdSource *sd_source = (SdSource *)source;
gboolean ret;
SdSource *sd_source = (SdSource *)source;
gboolean ret;
g_warn_if_fail (callback != NULL);
ret = (*callback) (user_data);
sd_login_monitor_flush (sd_source->monitor);
return ret;
g_warn_if_fail (callback != NULL);
ret = (*callback) (user_data);
sd_login_monitor_flush (sd_source->monitor);
return ret;
}
static void
sd_source_finalize (GSource *source)
{
SdSource *sd_source = (SdSource*)source;
SdSource *sd_source = (SdSource*) source;
sd_login_monitor_unref (sd_source->monitor);
sd_login_monitor_unref (sd_source->monitor);
}
static GSourceFuncs sd_source_funcs = {
sd_source_prepare,
sd_source_check,
sd_source_dispatch,
sd_source_finalize
sd_source_prepare,
sd_source_check,
sd_source_dispatch,
sd_source_finalize
};
static GSource *
sd_source_new (void)
{
GSource *source;
SdSource *sd_source;
int ret;
GSource *source;
SdSource *sd_source;
int ret;
source = g_source_new (&sd_source_funcs, sizeof (SdSource));
sd_source = (SdSource *)source;
source = g_source_new (&sd_source_funcs, sizeof (SdSource));
sd_source = (SdSource *)source;
if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
{
g_printerr ("Error getting login monitor: %d", ret);
}
else
{
sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
sd_source->pollfd.events = G_IO_IN;
g_source_add_poll (source, &sd_source->pollfd);
}
ret = sd_login_monitor_new (NULL, &sd_source->monitor);
if (ret < 0)
g_printerr ("Error getting login monitor: %d", ret);
else {
sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
sd_source->pollfd.events = G_IO_IN;
g_source_add_poll (source, &sd_source->pollfd);
}
return source;
return source;
}
struct _NMSessionMonitor
{
GObject parent_instance;
struct _NMSessionMonitor {
GObject parent_instance;
GSource *sd_source;
GSource *sd_source;
};
struct _NMSessionMonitorClass
{
GObjectClass parent_class;
struct _NMSessionMonitorClass {
GObjectClass parent_class;
void (*changed) (NMSessionMonitor *monitor);
void (*changed) (NMSessionMonitor *monitor);
};
enum
{
CHANGED_SIGNAL,
LAST_SIGNAL,
enum {
CHANGED_SIGNAL,
LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL] = {0};
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
@ -180,94 +133,69 @@ G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
static gboolean
sessions_changed (gpointer user_data)
{
NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
return TRUE;
g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
return TRUE;
}
static void
nm_session_monitor_init (NMSessionMonitor *monitor)
{
monitor->sd_source = sd_source_new ();
g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
g_source_attach (monitor->sd_source, NULL);
monitor->sd_source = sd_source_new ();
g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
g_source_attach (monitor->sd_source, NULL);
}
static void
nm_session_monitor_finalize (GObject *object)
{
NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
if (monitor->sd_source != NULL)
{
g_source_destroy (monitor->sd_source);
g_source_unref (monitor->sd_source);
}
if (monitor->sd_source != NULL) {
g_source_destroy (monitor->sd_source);
g_source_unref (monitor->sd_source);
}
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
}
static void
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
{
GObjectClass *gobject_class;
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = nm_session_monitor_finalize;
gobject_class->finalize = nm_session_monitor_finalize;
/**
* NMSessionMonitor::changed:
* @monitor: A #NMSessionMonitor
*
* Emitted when something changes.
*/
signals[CHANGED_SIGNAL] = g_signal_new ("changed",
NM_TYPE_SESSION_MONITOR,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
NULL, /* accumulator */
NULL, /* accumulator data */
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
/**
* NMSessionMonitor::changed:
* @monitor: A #NMSessionMonitor
*
* Emitted when something changes.
*/
signals[CHANGED_SIGNAL] = g_signal_new ("changed",
NM_TYPE_SESSION_MONITOR,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
NULL, /* accumulator */
NULL, /* accumulator data */
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
}
NMSessionMonitor *
nm_session_monitor_get (void)
{
static NMSessionMonitor *singleton = NULL;
static NMSessionMonitor *singleton = NULL;
if (singleton)
return g_object_ref (singleton);
if (singleton)
return g_object_ref (singleton);
singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
return singleton;
}
static gboolean
user_to_uid (const char *user, uid_t *out_uid, GError **error)
{
struct passwd *pw;
pw = getpwnam (user);
if (!pw) {
g_set_error (error,
NM_SESSION_MONITOR_ERROR,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
"Could not get UID for username '%s'",
user);
return FALSE;
}
if (out_uid)
*out_uid = pw->pw_uid;
return TRUE;
return NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
}
gboolean
@ -276,15 +204,15 @@ nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
uid_t *out_uid,
GError **error)
{
uid_t uid;
uid_t uid;
if (!user_to_uid (username, &uid, error))
return FALSE;
if (!nm_session_user_to_uid (username, &uid, error))
return FALSE;
if (out_uid)
*out_uid = uid;
if (out_uid)
*out_uid = uid;
return nm_session_monitor_uid_has_session (monitor, uid, NULL, error);
return nm_session_monitor_uid_has_session (monitor, uid, NULL, error);
}
gboolean
@ -292,12 +220,12 @@ nm_session_monitor_user_active (NMSessionMonitor *monitor,
const char *username,
GError **error)
{
uid_t uid;
uid_t uid;
if (!user_to_uid (username, &uid, error))
return FALSE;
if (!nm_session_user_to_uid (username, &uid, error))
return FALSE;
return nm_session_monitor_uid_active (monitor, uid, error);
return nm_session_monitor_uid_active (monitor, uid, error);
}
gboolean
@ -306,7 +234,7 @@ nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
const char **out_user,
GError **error)
{
return sd_uid_get_sessions (uid, FALSE, NULL) > 0;
return sd_uid_get_sessions (uid, FALSE, NULL) > 0;
}
gboolean
@ -314,5 +242,5 @@ nm_session_monitor_uid_active (NMSessionMonitor *monitor,
uid_t uid,
GError **error)
{
return sd_uid_get_sessions (uid, TRUE, NULL) > 0;
return sd_uid_get_sessions (uid, TRUE, NULL) > 0;
}

View file

@ -20,13 +20,12 @@
#include "config.h"
#include <errno.h>
#include <pwd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <gio/gio.h>
#include "nm-logging.h"
#include "nm-session-utils.h"
#include "nm-session-monitor.h"
#define CKDB_PATH "/var/run/ConsoleKit/database"
@ -66,53 +65,6 @@ G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
/********************************************************************/
#define NM_SESSION_MONITOR_ERROR (nm_session_monitor_error_quark ())
GQuark nm_session_monitor_error_quark (void) G_GNUC_CONST;
GType nm_session_monitor_error_get_type (void) G_GNUC_CONST;
typedef enum {
NM_SESSION_MONITOR_ERROR_IO_ERROR = 0,
NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
NM_SESSION_MONITOR_ERROR_NO_DATABASE,
} NMSessionMonitorError;
GQuark
nm_session_monitor_error_quark (void)
{
static GQuark ret = 0;
if (G_UNLIKELY (ret == 0))
ret = g_quark_from_static_string ("nm-session-monitor-error");
return ret;
}
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
GType
nm_session_monitor_error_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
/* Some I/O operation on the CK database failed */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_IO_ERROR, "IOError"),
/* Error parsing the CK database */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE, "MalformedDatabase"),
/* Username or UID could could not be found */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_UNKNOWN_USER, "UnknownUser"),
/* No ConsoleKit database */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_NO_DATABASE, "NoDatabase"),
{ 0, 0, 0 }
};
etype = g_enum_register_static ("NMSessionMonitorError", values);
}
return etype;
}
/********************************************************************/
typedef struct {
char *user;
uid_t uid;
@ -149,7 +101,7 @@ session_new (GKeyFile *keyfile, const char *group, GError **error)
{
GError *local = NULL;
Session *s;
struct passwd *pw;
const char *uname = NULL;
s = g_new0 (Session, 1);
g_assert (s);
@ -173,16 +125,9 @@ session_new (GKeyFile *keyfile, const char *group, GError **error)
if (local)
goto error;
pw = getpwuid (s->uid);
if (!pw) {
g_set_error (&local,
NM_SESSION_MONITOR_ERROR,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
"Could not get username for UID %d",
s->uid);
goto error;
}
s->user = g_strdup (pw->pw_name);
if (!nm_session_uid_to_user (s->uid, &uname, error))
return FALSE;
s->user = g_strdup (uname);
return s;
@ -428,50 +373,6 @@ nm_session_monitor_get (void)
/* ---------------------------------------------------------------------------------------------------- */
#if NO_CONSOLEKIT
static gboolean
uid_to_user (uid_t uid, const char **out_user, GError **error)
{
struct passwd *pw;
pw = getpwuid (uid);
if (!pw) {
g_set_error (error,
NM_SESSION_MONITOR_ERROR,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
"Could not get username for UID %d",
uid);
return FALSE;
}
/* Ugly, but hey, use ConsoleKit */
if (out_user)
*out_user = pw->pw_name;
return TRUE;
}
static gboolean
user_to_uid (const char *user, uid_t *out_uid, GError **error)
{
struct passwd *pw;
pw = getpwnam (user);
if (!pw) {
g_set_error (error,
NM_SESSION_MONITOR_ERROR,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
"Could not get UID for username '%s'",
user);
return FALSE;
}
/* Ugly, but hey, use ConsoleKit */
if (out_uid)
*out_uid = pw->pw_uid;
return TRUE;
}
#endif
/**
* nm_session_monitor_user_has_session:
* @monitor: A #NMSessionMonitor.

104
src/nm-session-utils.c Normal file
View file

@ -0,0 +1,104 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2012 Red Hat, Inc.
* Author: Dan Williams <dcbw@redhat.com>
*/
#include <pwd.h>
#include <sys/types.h>
#include "nm-session-utils.h"
/********************************************************************/
GQuark
nm_session_monitor_error_quark (void)
{
static GQuark ret = 0;
if (G_UNLIKELY (ret == 0))
ret = g_quark_from_static_string ("nm-session-monitor-error");
return ret;
}
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
GType
nm_session_monitor_error_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
/* Some I/O operation on the CK database failed */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_IO_ERROR, "IOError"),
/* Error parsing the CK database */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE, "MalformedDatabase"),
/* Username or UID could could not be found */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_UNKNOWN_USER, "UnknownUser"),
/* No ConsoleKit database */
ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_NO_DATABASE, "NoDatabase"),
{ 0, 0, 0 }
};
etype = g_enum_register_static ("NMSessionMonitorError", values);
}
return etype;
}
/********************************************************************/
gboolean
nm_session_uid_to_user (uid_t uid, const char **out_user, GError **error)
{
struct passwd *pw;
pw = getpwuid (uid);
if (!pw) {
g_set_error (error,
NM_SESSION_MONITOR_ERROR,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
"Could not get username for UID %d",
uid);
return FALSE;
}
if (out_user)
*out_user = pw->pw_name;
return TRUE;
}
gboolean
nm_session_user_to_uid (const char *user, uid_t *out_uid, GError **error)
{
struct passwd *pw;
pw = getpwnam (user);
if (!pw) {
g_set_error (error,
NM_SESSION_MONITOR_ERROR,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
"Could not get UID for username '%s'",
user);
return FALSE;
}
/* Ugly, but hey, use ConsoleKit */
if (out_uid)
*out_uid = pw->pw_uid;
return TRUE;
}

41
src/nm-session-utils.h Normal file
View file

@ -0,0 +1,41 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2012 Red Hat, Inc.
* Author: Dan Williams <dcbw@redhat.com>
*/
#ifndef NM_SESSION_UTILS_H
#define NM_SESSION_UTILS_H
#include <glib.h>
#include <glib-object.h>
#define NM_SESSION_MONITOR_ERROR (nm_session_monitor_error_quark ())
GQuark nm_session_monitor_error_quark (void) G_GNUC_CONST;
GType nm_session_monitor_error_get_type (void) G_GNUC_CONST;
typedef enum {
NM_SESSION_MONITOR_ERROR_IO_ERROR = 0,
NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE,
NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
NM_SESSION_MONITOR_ERROR_NO_DATABASE,
} NMSessionMonitorError;
gboolean nm_session_uid_to_user (uid_t uid, const char **out_user, GError **error);
gboolean nm_session_user_to_uid (const char *user, uid_t *out_uid, GError **error);
#endif /* NM_SESSION_UTILS_H */