core: combine common session management utility code
And reformat the systemd session manager for NM code style.
This commit is contained in:
@@ -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 += \
|
||||
|
@@ -1,3 +1,4 @@
|
||||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
* Copyright (C) 2011 Red Hat, Inc.
|
||||
*
|
||||
@@ -28,58 +29,19 @@
|
||||
#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
|
||||
{
|
||||
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;
|
||||
@@ -103,11 +65,8 @@ sd_source_dispatch (GSource *source,
|
||||
gboolean ret;
|
||||
|
||||
g_warn_if_fail (callback != NULL);
|
||||
|
||||
ret = (*callback) (user_data);
|
||||
|
||||
sd_login_monitor_flush (sd_source->monitor);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -136,12 +95,10 @@ sd_source_new (void)
|
||||
source = g_source_new (&sd_source_funcs, sizeof (SdSource));
|
||||
sd_source = (SdSource *)source;
|
||||
|
||||
if ((ret = sd_login_monitor_new (NULL, &sd_source->monitor)) < 0)
|
||||
{
|
||||
ret = sd_login_monitor_new (NULL, &sd_source->monitor);
|
||||
if (ret < 0)
|
||||
g_printerr ("Error getting login monitor: %d", ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
@@ -150,27 +107,23 @@ sd_source_new (void)
|
||||
return source;
|
||||
}
|
||||
|
||||
struct _NMSessionMonitor
|
||||
{
|
||||
struct _NMSessionMonitor {
|
||||
GObject parent_instance;
|
||||
|
||||
GSource *sd_source;
|
||||
};
|
||||
|
||||
struct _NMSessionMonitorClass
|
||||
{
|
||||
struct _NMSessionMonitorClass {
|
||||
GObjectClass parent_class;
|
||||
|
||||
void (*changed) (NMSessionMonitor *monitor);
|
||||
};
|
||||
|
||||
|
||||
enum
|
||||
{
|
||||
enum {
|
||||
CHANGED_SIGNAL,
|
||||
LAST_SIGNAL,
|
||||
};
|
||||
|
||||
static guint signals[LAST_SIGNAL] = {0};
|
||||
|
||||
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
|
||||
@@ -183,7 +136,6 @@ sessions_changed (gpointer user_data)
|
||||
NMSessionMonitor *monitor = NM_SESSION_MONITOR (user_data);
|
||||
|
||||
g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -201,8 +153,7 @@ nm_session_monitor_finalize (GObject *object)
|
||||
{
|
||||
NMSessionMonitor *monitor = NM_SESSION_MONITOR (object);
|
||||
|
||||
if (monitor->sd_source != NULL)
|
||||
{
|
||||
if (monitor->sd_source != NULL) {
|
||||
g_source_destroy (monitor->sd_source);
|
||||
g_source_unref (monitor->sd_source);
|
||||
}
|
||||
@@ -217,7 +168,6 @@ nm_session_monitor_class_init (NMSessionMonitorClass *klass)
|
||||
GObjectClass *gobject_class;
|
||||
|
||||
gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
gobject_class->finalize = nm_session_monitor_finalize;
|
||||
|
||||
/**
|
||||
@@ -245,29 +195,7 @@ nm_session_monitor_get (void)
|
||||
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
|
||||
@@ -278,7 +206,7 @@ nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
|
||||
{
|
||||
uid_t uid;
|
||||
|
||||
if (!user_to_uid (username, &uid, error))
|
||||
if (!nm_session_user_to_uid (username, &uid, error))
|
||||
return FALSE;
|
||||
|
||||
if (out_uid)
|
||||
@@ -294,7 +222,7 @@ nm_session_monitor_user_active (NMSessionMonitor *monitor,
|
||||
{
|
||||
uid_t uid;
|
||||
|
||||
if (!user_to_uid (username, &uid, error))
|
||||
if (!nm_session_user_to_uid (username, &uid, error))
|
||||
return FALSE;
|
||||
|
||||
return nm_session_monitor_uid_active (monitor, uid, error);
|
||||
|
@@ -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
104
src/nm-session-utils.c
Normal 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
41
src/nm-session-utils.h
Normal 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 */
|
Reference in New Issue
Block a user