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.c \
|
||||||
nm-dhcp6-config.h \
|
nm-dhcp6-config.h \
|
||||||
nm-rfkill.h \
|
nm-rfkill.h \
|
||||||
nm-session-monitor.h
|
nm-session-monitor.h \
|
||||||
|
nm-session-utils.c \
|
||||||
|
nm-session-utils.h
|
||||||
|
|
||||||
if WITH_SYSTEMD
|
if WITH_SYSTEMD
|
||||||
NetworkManager_SOURCES += \
|
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.
|
* Copyright (C) 2011 Red Hat, Inc.
|
||||||
*
|
*
|
||||||
@@ -28,69 +29,30 @@
|
|||||||
#include <systemd/sd-login.h>
|
#include <systemd/sd-login.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "nm-session-utils.h"
|
||||||
#include "nm-session-monitor.h"
|
#include "nm-session-monitor.h"
|
||||||
|
|
||||||
/********************************************************************/
|
/********************************************************************/
|
||||||
|
|
||||||
#define NM_SESSION_MONITOR_ERROR (nm_session_monitor_error_quark ())
|
typedef struct {
|
||||||
GQuark nm_session_monitor_error_quark (void) G_GNUC_CONST;
|
GSource source;
|
||||||
GType nm_session_monitor_error_get_type (void) G_GNUC_CONST;
|
GPollFD pollfd;
|
||||||
|
sd_login_monitor *monitor;
|
||||||
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;
|
|
||||||
} SdSource;
|
} SdSource;
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
sd_source_prepare (GSource *source,
|
sd_source_prepare (GSource *source, gint *timeout)
|
||||||
gint *timeout)
|
|
||||||
{
|
{
|
||||||
*timeout = -1;
|
*timeout = -1;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
sd_source_check (GSource *source)
|
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
|
static gboolean
|
||||||
@@ -99,78 +61,69 @@ sd_source_dispatch (GSource *source,
|
|||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
|
|
||||||
{
|
{
|
||||||
SdSource *sd_source = (SdSource *)source;
|
SdSource *sd_source = (SdSource *)source;
|
||||||
gboolean ret;
|
gboolean ret;
|
||||||
|
|
||||||
g_warn_if_fail (callback != NULL);
|
g_warn_if_fail (callback != NULL);
|
||||||
|
ret = (*callback) (user_data);
|
||||||
ret = (*callback) (user_data);
|
sd_login_monitor_flush (sd_source->monitor);
|
||||||
|
return ret;
|
||||||
sd_login_monitor_flush (sd_source->monitor);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sd_source_finalize (GSource *source)
|
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 = {
|
static GSourceFuncs sd_source_funcs = {
|
||||||
sd_source_prepare,
|
sd_source_prepare,
|
||||||
sd_source_check,
|
sd_source_check,
|
||||||
sd_source_dispatch,
|
sd_source_dispatch,
|
||||||
sd_source_finalize
|
sd_source_finalize
|
||||||
};
|
};
|
||||||
|
|
||||||
static GSource *
|
static GSource *
|
||||||
sd_source_new (void)
|
sd_source_new (void)
|
||||||
{
|
{
|
||||||
GSource *source;
|
GSource *source;
|
||||||
SdSource *sd_source;
|
SdSource *sd_source;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
source = g_source_new (&sd_source_funcs, sizeof (SdSource));
|
source = g_source_new (&sd_source_funcs, sizeof (SdSource));
|
||||||
sd_source = (SdSource *)source;
|
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);
|
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;
|
||||||
sd_source->pollfd.fd = sd_login_monitor_get_fd (sd_source->monitor);
|
g_source_add_poll (source, &sd_source->pollfd);
|
||||||
sd_source->pollfd.events = G_IO_IN;
|
}
|
||||||
g_source_add_poll (source, &sd_source->pollfd);
|
|
||||||
}
|
|
||||||
|
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _NMSessionMonitor
|
struct _NMSessionMonitor {
|
||||||
{
|
GObject parent_instance;
|
||||||
GObject parent_instance;
|
|
||||||
|
|
||||||
GSource *sd_source;
|
GSource *sd_source;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _NMSessionMonitorClass
|
struct _NMSessionMonitorClass {
|
||||||
{
|
GObjectClass parent_class;
|
||||||
GObjectClass parent_class;
|
|
||||||
|
|
||||||
void (*changed) (NMSessionMonitor *monitor);
|
void (*changed) (NMSessionMonitor *monitor);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
enum
|
enum {
|
||||||
{
|
CHANGED_SIGNAL,
|
||||||
CHANGED_SIGNAL,
|
LAST_SIGNAL,
|
||||||
LAST_SIGNAL,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static guint signals[LAST_SIGNAL] = {0};
|
static guint signals[LAST_SIGNAL] = {0};
|
||||||
|
|
||||||
G_DEFINE_TYPE (NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
|
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
|
static gboolean
|
||||||
sessions_changed (gpointer user_data)
|
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);
|
g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0);
|
||||||
|
return TRUE;
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nm_session_monitor_init (NMSessionMonitor *monitor)
|
nm_session_monitor_init (NMSessionMonitor *monitor)
|
||||||
{
|
{
|
||||||
monitor->sd_source = sd_source_new ();
|
monitor->sd_source = sd_source_new ();
|
||||||
g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
|
g_source_set_callback (monitor->sd_source, sessions_changed, monitor, NULL);
|
||||||
g_source_attach (monitor->sd_source, NULL);
|
g_source_attach (monitor->sd_source, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nm_session_monitor_finalize (GObject *object)
|
nm_session_monitor_finalize (GObject *object)
|
||||||
{
|
{
|
||||||
NMSessionMonitor *monitor = NM_SESSION_MONITOR (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_destroy (monitor->sd_source);
|
g_source_unref (monitor->sd_source);
|
||||||
g_source_unref (monitor->sd_source);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
|
if (G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize != NULL)
|
||||||
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
|
G_OBJECT_CLASS (nm_session_monitor_parent_class)->finalize (object);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nm_session_monitor_class_init (NMSessionMonitorClass *klass)
|
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
|
||||||
* NMSessionMonitor::changed:
|
*
|
||||||
* @monitor: A #NMSessionMonitor
|
* Emitted when something changes.
|
||||||
*
|
*/
|
||||||
* Emitted when something changes.
|
signals[CHANGED_SIGNAL] = g_signal_new ("changed",
|
||||||
*/
|
NM_TYPE_SESSION_MONITOR,
|
||||||
signals[CHANGED_SIGNAL] = g_signal_new ("changed",
|
G_SIGNAL_RUN_LAST,
|
||||||
NM_TYPE_SESSION_MONITOR,
|
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
|
||||||
G_SIGNAL_RUN_LAST,
|
NULL, /* accumulator */
|
||||||
G_STRUCT_OFFSET (NMSessionMonitorClass, changed),
|
NULL, /* accumulator data */
|
||||||
NULL, /* accumulator */
|
g_cclosure_marshal_VOID__VOID,
|
||||||
NULL, /* accumulator data */
|
G_TYPE_NONE,
|
||||||
g_cclosure_marshal_VOID__VOID,
|
0);
|
||||||
G_TYPE_NONE,
|
|
||||||
0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NMSessionMonitor *
|
NMSessionMonitor *
|
||||||
nm_session_monitor_get (void)
|
nm_session_monitor_get (void)
|
||||||
{
|
{
|
||||||
static NMSessionMonitor *singleton = NULL;
|
static NMSessionMonitor *singleton = NULL;
|
||||||
|
|
||||||
if (singleton)
|
if (singleton)
|
||||||
return g_object_ref (singleton);
|
return g_object_ref (singleton);
|
||||||
|
|
||||||
singleton = NM_SESSION_MONITOR (g_object_new (NM_TYPE_SESSION_MONITOR, NULL));
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
@@ -276,15 +204,15 @@ nm_session_monitor_user_has_session (NMSessionMonitor *monitor,
|
|||||||
uid_t *out_uid,
|
uid_t *out_uid,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
|
|
||||||
if (!user_to_uid (username, &uid, error))
|
if (!nm_session_user_to_uid (username, &uid, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
if (out_uid)
|
if (out_uid)
|
||||||
*out_uid = 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
|
gboolean
|
||||||
@@ -292,12 +220,12 @@ nm_session_monitor_user_active (NMSessionMonitor *monitor,
|
|||||||
const char *username,
|
const char *username,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
|
|
||||||
if (!user_to_uid (username, &uid, error))
|
if (!nm_session_user_to_uid (username, &uid, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
return nm_session_monitor_uid_active (monitor, uid, error);
|
return nm_session_monitor_uid_active (monitor, uid, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
@@ -306,7 +234,7 @@ nm_session_monitor_uid_has_session (NMSessionMonitor *monitor,
|
|||||||
const char **out_user,
|
const char **out_user,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
return sd_uid_get_sessions (uid, FALSE, NULL) > 0;
|
return sd_uid_get_sessions (uid, FALSE, NULL) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
@@ -314,5 +242,5 @@ nm_session_monitor_uid_active (NMSessionMonitor *monitor,
|
|||||||
uid_t uid,
|
uid_t uid,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
return sd_uid_get_sessions (uid, TRUE, NULL) > 0;
|
return sd_uid_get_sessions (uid, TRUE, NULL) > 0;
|
||||||
}
|
}
|
||||||
|
@@ -20,13 +20,12 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pwd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
#include "nm-logging.h"
|
#include "nm-logging.h"
|
||||||
|
|
||||||
|
#include "nm-session-utils.h"
|
||||||
#include "nm-session-monitor.h"
|
#include "nm-session-monitor.h"
|
||||||
|
|
||||||
#define CKDB_PATH "/var/run/ConsoleKit/database"
|
#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 {
|
typedef struct {
|
||||||
char *user;
|
char *user;
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
@@ -149,7 +101,7 @@ session_new (GKeyFile *keyfile, const char *group, GError **error)
|
|||||||
{
|
{
|
||||||
GError *local = NULL;
|
GError *local = NULL;
|
||||||
Session *s;
|
Session *s;
|
||||||
struct passwd *pw;
|
const char *uname = NULL;
|
||||||
|
|
||||||
s = g_new0 (Session, 1);
|
s = g_new0 (Session, 1);
|
||||||
g_assert (s);
|
g_assert (s);
|
||||||
@@ -173,16 +125,9 @@ session_new (GKeyFile *keyfile, const char *group, GError **error)
|
|||||||
if (local)
|
if (local)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
pw = getpwuid (s->uid);
|
if (!nm_session_uid_to_user (s->uid, &uname, error))
|
||||||
if (!pw) {
|
return FALSE;
|
||||||
g_set_error (&local,
|
s->user = g_strdup (uname);
|
||||||
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);
|
|
||||||
|
|
||||||
return s;
|
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:
|
* nm_session_monitor_user_has_session:
|
||||||
* @monitor: A #NMSessionMonitor.
|
* @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