summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--src/Makefile.am38
-rw-r--r--src/ck-manager.c4
-rw-r--r--src/ck-session-leader.c243
-rw-r--r--src/mock-ck-collect-session-info.c43
-rw-r--r--src/test-session-leader.c110
6 files changed, 264 insertions, 176 deletions
diff --git a/.gitignore b/.gitignore
index 048c0e1..801c808 100644
--- a/.gitignore
+++ b/.gitignore
@@ -60,6 +60,8 @@ src/ck-seat-generated.h
src/ck-session-generated.c
src/ck-session-generated.h
src/test-session
+src/test-session-leader
+src/ck-collect-session-info
tools/pam-foreground-compat.ck
tools/70-udev-acl.rules
doc/console-kit-daemon.1m
diff --git a/src/Makefile.am b/src/Makefile.am
index 2cd6a17..156cfe6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -187,6 +187,8 @@ noinst_PROGRAMS = \
test-vt-monitor \
test-inhibit \
test-session \
+ ck-collect-session-info \
+ test-session-leader \
$(NULL)
test_event_logger_SOURCES = \
@@ -275,6 +277,40 @@ test_session_LDADD = \
libck.la \
$(NULL)
+ck_collect_session_info_CFLAGS = \
+ $(AM_CFLAGS) \
+ $(NULL)
+
+ck_collect_session_info_SOURCES = \
+ $(BUILT_SOURCES) \
+ mock-ck-collect-session-info.c \
+ $(NULL)
+
+ck_collect_session_info_LDADD = \
+ $(CONSOLE_KIT_LIBS) \
+ $(NULL)
+
+
+test_session_leader_CFLAGS = \
+ $(AM_CFLAGS) \
+ -ULIBEXECDIR \
+ -DLIBEXECDIR=\".\" \
+ $(NULL)
+
+test_session_leader_SOURCES = \
+ $(BUILT_SOURCES) \
+ ck-job.h \
+ ck-job.c \
+ ck-session-leader.c \
+ ck-session-leader.h \
+ test-session-leader.c \
+ $(NULL)
+
+test_session_leader_LDADD = \
+ $(CONSOLE_KIT_LIBS) \
+ libck.la \
+ $(NULL)
+
EXTRA_DIST = \
ck-marshal.list \
@@ -283,6 +319,8 @@ EXTRA_DIST = \
test-open-session \
test-open-session-with-parameters \
test-session \
+ test-session-leader \
+ ck-collect-session-info \
$(NULL)
CLEANFILES = $(BUILT_SOURCES)
diff --git a/src/ck-manager.c b/src/ck-manager.c
index c9f1ac5..348e090 100644
--- a/src/ck-manager.c
+++ b/src/ck-manager.c
@@ -280,7 +280,7 @@ static const GDBusErrorEntry ck_manager_error_entries[] =
};
GQuark
-ck_session_error_quark (void)
+ck_manager_error_quark (void)
{
static volatile gsize quark_volatile = 0;
@@ -294,7 +294,7 @@ ck_session_error_quark (void)
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
GType
-ck_session_error_get_type (void)
+ck_manager_error_get_type (void)
{
static GType etype = 0;
diff --git a/src/ck-session-leader.c b/src/ck-session-leader.c
index fcb4fc7..144e125 100644
--- a/src/ck-session-leader.c
+++ b/src/ck-session-leader.c
@@ -38,18 +38,13 @@
#define CK_SESSION_LEADER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CK_TYPE_SESSION_LEADER, CkSessionLeaderPrivate))
-#define CK_TYPE_PARAMETER_STRUCT (dbus_g_type_get_struct ("GValueArray", \
- G_TYPE_STRING, \
- G_TYPE_VALUE, \
- G_TYPE_INVALID))
-#define CK_TYPE_PARAMETER_LIST (dbus_g_type_get_collection ("GPtrArray", \
- CK_TYPE_PARAMETER_STRUCT))
static struct {
const char *name;
const char *variant_type;
GType gtype;
} parameter_lookup[] = {
+ { "login-session-id", "s", G_TYPE_STRING },
{ "display-device", "s", G_TYPE_STRING },
{ "x11-display-device", "s", G_TYPE_STRING },
{ "x11-display", "s", G_TYPE_STRING },
@@ -59,22 +54,6 @@ static struct {
{ "unix-user", "i", G_TYPE_INT },
};
-static void
-lookup_parameter_type (const char *name, const char **variant_type, GType *gtype)
-{
- int i;
-
- *gtype = G_TYPE_INVALID;
-
- for (i = 0; i < G_N_ELEMENTS (parameter_lookup); i++) {
- if (strcmp (name, parameter_lookup[i].name) == 0) {
- *variant_type = parameter_lookup[i].variant_type;
- *gtype = parameter_lookup[i].gtype;
- break;
- }
- }
-}
-
struct CkSessionLeaderPrivate
{
char *id;
@@ -139,85 +118,6 @@ ck_session_leader_cancel (CkSessionLeader *leader)
leader->priv->cancelled = TRUE;
}
-
-static void
-add_param_int (GPtrArray *parameters,
- const char *key,
- const char *value)
-{
- GValue val = { 0, };
- GValue param_val = { 0, };
- int num;
-
- num = atoi (value);
-
- g_value_init (&val, G_TYPE_INT);
- g_value_set_int (&val, num);
- g_value_init (&param_val, CK_TYPE_PARAMETER_STRUCT);
- g_value_take_boxed (&param_val,
- dbus_g_type_specialized_construct (CK_TYPE_PARAMETER_STRUCT));
- dbus_g_type_struct_set (&param_val,
- 0, key,
- 1, &val,
- G_MAXUINT);
- g_value_unset (&val);
-
- g_ptr_array_add (parameters, g_value_get_boxed (&param_val));
-}
-
-static void
-add_param_boolean (GPtrArray *parameters,
- const char *key,
- const char *value)
-{
- GValue val = { 0, };
- GValue param_val = { 0, };
- gboolean b;
-
- if (value != NULL && strcmp (value, "true") == 0) {
- b = TRUE;
- } else {
- b = FALSE;
- }
-
- g_value_init (&val, G_TYPE_BOOLEAN);
- g_value_set_boolean (&val, b);
- g_value_init (&param_val, CK_TYPE_PARAMETER_STRUCT);
- g_value_take_boxed (&param_val,
- dbus_g_type_specialized_construct (CK_TYPE_PARAMETER_STRUCT));
- dbus_g_type_struct_set (&param_val,
- 0, key,
- 1, &val,
- G_MAXUINT);
- g_value_unset (&val);
-
- g_ptr_array_add (parameters, g_value_get_boxed (&param_val));
-}
-
-static void
-add_param_string (GPtrArray *parameters,
- const char *key,
- const char *value)
-{
- GValue val = { 0, };
- GValue param_val = { 0, };
-
- g_value_init (&val, G_TYPE_STRING);
- g_value_set_string (&val, value);
-
- g_value_init (&param_val, CK_TYPE_PARAMETER_STRUCT);
- g_value_take_boxed (&param_val,
- dbus_g_type_specialized_construct (CK_TYPE_PARAMETER_STRUCT));
-
- dbus_g_type_struct_set (&param_val,
- 0, key,
- 1, &val,
- G_MAXUINT);
- g_value_unset (&val);
-
- g_ptr_array_add (parameters, g_value_get_boxed (&param_val));
-}
-
static gboolean
have_override_parameter (CkSessionLeader *leader,
const char *prop_name)
@@ -241,53 +141,50 @@ have_override_parameter (CkSessionLeader *leader,
}
static void
-add_to_parameters (gpointer key,
- gpointer data,
- GPtrArray *parameters)
+lookup_parameter_type (const char *name, const char **variant_type, GType *gtype)
{
- gpointer data_copy;
+ int i;
- data_copy = g_boxed_copy (CK_TYPE_PARAMETER_STRUCT, data);
- g_ptr_array_add (parameters, data_copy);
-}
+ *gtype = G_TYPE_INVALID;
-typedef void (* CkAddParamFunc) (GPtrArray *arr,
- const char *key,
- const char *value);
+ for (i = 0; i < G_N_ELEMENTS (parameter_lookup); i++) {
+ if (strcmp (name, parameter_lookup[i].name) == 0) {
+ *variant_type = parameter_lookup[i].variant_type;
+ *gtype = parameter_lookup[i].gtype;
+ return;
+ }
+ }
+}
-static struct {
- char *key;
- CkAddParamFunc func;
-} parse_ops[] = {
- { "login-session-id", add_param_string },
- { "display-device", add_param_string },
- { "x11-display-device", add_param_string },
- { "x11-display", add_param_string },
- { "remote-host-name", add_param_string },
- { "session-type", add_param_string },
- { "is-local", add_param_boolean },
- { "unix-user", add_param_int },
-};
+static void
+add_to_parameters (gpointer key,
+ gpointer data,
+ GVariantBuilder *ck_parameters)
+{
+ g_variant_builder_add (ck_parameters, "{sv}", key, (GVariant*) data);
+}
-static GPtrArray *
+static GVariant *
parse_output (CkSessionLeader *leader,
const char *output)
{
- GPtrArray *parameters;
+ GVariantBuilder ck_parameters;
char **lines;
int i;
- int j;
lines = g_strsplit (output, "\n", -1);
if (lines == NULL) {
return NULL;
}
- parameters = g_ptr_array_sized_new (10);
+ g_variant_builder_init (&ck_parameters, G_VARIANT_TYPE ("a{sv}"));
/* first add generated params */
for (i = 0; lines[i] != NULL; i++) {
- char **vals;
+ char **vals;
+ const char *variant_type;
+ GVariant *element;
+ GType gtype;
vals = g_strsplit (lines[i], " = ", 2);
if (vals == NULL || vals[0] == NULL) {
@@ -301,12 +198,16 @@ parse_output (CkSessionLeader *leader,
continue;
}
- for (j = 0; j < G_N_ELEMENTS (parse_ops); j++) {
- if (strcmp (vals[0], parse_ops[j].key) == 0) {
- parse_ops[j].func (parameters, vals[0], vals[1]);
- break;
- }
+ lookup_parameter_type (vals[0], &variant_type, &gtype);
+ if (gtype == G_TYPE_INVALID) {
+ g_warning ("invalid parameter type\n");
+ continue;
}
+
+ element = g_variant_new (variant_type, vals[1]);
+
+ g_variant_builder_add (&ck_parameters, "{sv}", vals[0], element);
+
g_strfreev (vals);
}
g_strfreev (lines);
@@ -314,57 +215,45 @@ parse_output (CkSessionLeader *leader,
/* now overlay the overrides */
g_hash_table_foreach (leader->priv->override_parameters,
(GHFunc)add_to_parameters,
- parameters);
+ &ck_parameters);
- return parameters;
+ return g_variant_builder_end (&ck_parameters);
}
static void
-parameters_free (GPtrArray *parameters)
+parameters_free (GVariant *parameters)
{
- int i;
+ GVariantIter *iter;
+ gchar *key;
+ GVariant *value;
- for (i = 0; i < parameters->len; i++) {
- gpointer data;
- data = g_ptr_array_index (parameters, i);
- if (data != NULL) {
- g_boxed_free (CK_TYPE_PARAMETER_STRUCT, data);
- }
+ g_variant_get (parameters, "a{sv}", &iter);
+
+ while (g_variant_iter_next (iter, "{sv}", &key, &value)) {
+ g_variant_unref (value);
+ g_free (key);
}
+ g_variant_iter_free (iter);
- g_ptr_array_free (parameters, TRUE);
+ g_variant_unref (parameters);
}
static void
save_parameters (CkSessionLeader *leader,
const GVariant *parameters)
{
- GVariantIter *iter;
- int i;
+ GVariantIter *iter;
+ gchar *prop_name;
+ GVariant *value;
- for (i = 0; i < parameters->len; i++) {
- gpointer data;
- data = g_ptr_array_index (parameters, i);
+ g_variant_get ((GVariant *)parameters, "a{sv}", &iter);
- /* filter out the nulls? - sure why not */
-
- if (data != NULL) {
- gpointer data_copy;
- GValue val_struct = { 0, };
- char *prop_name;
- gboolean res;
-
- g_value_init (&val_struct, CK_TYPE_PARAMETER_STRUCT);
- g_value_set_static_boxed (&val_struct, g_ptr_array_index (parameters, i));
+ while (g_variant_iter_next (iter, "{sv}", &prop_name, &value)) {
- res = dbus_g_type_struct_get (&val_struct,
- 0, &prop_name,
- G_MAXUINT);
- if (! res) {
- g_debug ("Unable to extract parameter input");
- g_free (prop_name);
- continue;
- }
+ /* filter out the nulls? - sure why not */
+ if (value != NULL) {
+ const char *variant_type;
+ GType gtype;
if (prop_name == NULL) {
g_debug ("Skipping NULL parameter");
@@ -379,16 +268,22 @@ save_parameters (CkSessionLeader *leader,
continue;
}
- g_debug ("Setting override parameters for: %s", prop_name);
+ lookup_parameter_type (prop_name, &variant_type, &gtype);
+ if (gtype == G_TYPE_INVALID) {
+ g_debug ("Unable to extract parameter input");
+ g_free (prop_name);
+ continue;
+ }
- data_copy = g_boxed_copy (CK_TYPE_PARAMETER_STRUCT, data);
+ g_debug ("Setting override parameters for: %s", prop_name);
/* takes ownership */
g_hash_table_insert (leader->priv->override_parameters,
prop_name,
- data_copy);
+ value);
}
}
+ g_variant_iter_free (iter);
}
typedef struct {
@@ -406,7 +301,7 @@ job_completed (CkJob *job,
g_debug ("Job status: %d", status);
if (status == 0) {
char *output;
- GPtrArray *parameters;
+ GVariant *parameters;
output = NULL;
ck_job_get_stdout (job, &output);
@@ -579,7 +474,7 @@ ck_session_leader_set_service_name (CkSessionLeader *session_leader,
void
ck_session_leader_set_override_parameters (CkSessionLeader *session_leader,
- const GPtrArray *parameters)
+ const GVariant *parameters)
{
g_return_if_fail (CK_IS_SESSION_LEADER (session_leader));
@@ -648,7 +543,7 @@ ck_session_leader_class_init (CkSessionLeaderClass *klass)
static void
parameter_free (gpointer data)
{
- g_boxed_free (CK_TYPE_PARAMETER_STRUCT, data);
+ g_variant_unref ((GVariant*)data);
}
static void
diff --git a/src/mock-ck-collect-session-info.c b/src/mock-ck-collect-session-info.c
new file mode 100644
index 0000000..c54bd41
--- /dev/null
+++ b/src/mock-ck-collect-session-info.c
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2015 Eric Koegel <eric.koegel@gmail.com>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+
+int
+main (int argc,
+ char *argv[])
+{
+ /* This is a mock for the tools/ck-collect-session-info program.
+ * It provides similar output to the real tool for test-session-leader.
+ * You do not need to run this as root, but as it does nothing there's
+ * little to worry about */
+ printf ("unix-user = 9000\n");
+ printf ("x11-display = :0.0\n");
+ printf ("x11-display-device = /dev/tty15\n");
+ printf ("display-device = /dev/pts/0\n");
+ printf ("login-session-id = 99\n");
+
+
+ return 0;
+}
diff --git a/src/test-session-leader.c b/src/test-session-leader.c
new file mode 100644
index 0000000..c467157
--- /dev/null
+++ b/src/test-session-leader.c
@@ -0,0 +1,110 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2014 Eric Koegel <eric.koegel@gmail.com>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <glib.h>
+#include <glib-object.h>
+#include <glib-unix.h>
+#include <glib/gstdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#include "ck-session-leader.h"
+
+#define UID 9000
+#define PID 8999
+#define SERVICE_NAME "test-session-leader"
+#define SESSION_ID "Session9999"
+#define COOKIE "test-session-leader-cookie"
+
+CkSessionLeader *leader = NULL;
+
+static void
+collect_parameters_cb (CkSessionLeader *leader,
+ GVariant *parameters,
+ GDBusMethodInvocation *context,
+ GMainLoop *loop)
+{
+ g_debug ("[collect_parameters_cb] Parameters: %s", g_variant_print (parameters, TRUE));
+ g_main_loop_quit (loop);
+}
+
+static void
+test_leader_init (void)
+{
+ leader = ck_session_leader_new ();
+ ck_session_leader_set_uid (leader, UID);
+ ck_session_leader_set_pid (leader, PID);
+ ck_session_leader_set_service_name (leader, SERVICE_NAME);
+ ck_session_leader_set_session_id (leader, SESSION_ID);
+ ck_session_leader_set_cookie (leader, COOKIE);
+
+ /* Verify! */
+ g_assert (CK_IS_SESSION_LEADER (leader));
+ g_assert (ck_session_leader_get_uid (leader) == UID);
+ g_assert (ck_session_leader_get_pid (leader) == PID);
+ g_assert (g_strcmp0 (ck_session_leader_peek_service_name (leader), SERVICE_NAME) == 0);
+ g_assert (g_strcmp0 (ck_session_leader_peek_session_id (leader), SESSION_ID) == 0);
+ g_assert (g_strcmp0 (ck_session_leader_peek_cookie (leader), COOKIE) == 0);
+}
+
+static void
+test_leader_collect (void)
+{
+ GMainLoop *loop;
+
+ g_debug ("Setting up main loop");
+
+ loop = g_main_loop_new (NULL, FALSE);
+
+ ck_session_leader_collect_parameters (leader, NULL,
+ (CkSessionLeaderDoneFunc)collect_parameters_cb, loop);
+
+ g_main_loop_run (loop);
+}
+
+static void
+test_leader_unref (void)
+{
+ g_object_unref (leader);
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ /* do not run these tests as root */
+ if (getuid () == 0) {
+ g_warning ("You must NOT be root to run this tests");
+ exit (1);
+ }
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/test_session_leader/test_leader_init", test_leader_init);
+ g_test_add_func ("/test_session_leader/test_leader_collect", test_leader_collect);
+ g_test_add_func ("/test_session_leader/test_leader_unref", test_leader_unref);
+
+ return g_test_run();
+}