]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blobdiff - tests/src/test-session.c
Merge from trunk
[sojka/lightdm.git] / tests / src / test-session.c
index b4e01bae16fd8e5b43607df862132bd002e94894..5f2a04d444ac5c35571fdd8e141332e835012ce7 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <signal.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <grp.h>
 #include <xcb/xcb.h>
 #include <glib.h>
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <glib-unix.h>
+#include <glib/gstdio.h>
 
 #include "status.h"
 
+static gchar *session_id;
+
+static GMainLoop *loop;
+
+static GString *open_fds;
+
 static GKeyFile *config;
 
-static void
-quit_cb (int signum)
+static xcb_connection_t *connection;
+
+static gboolean
+sigint_cb (gpointer user_data)
 {
-    notify_status ("SESSION %s TERMINATE SIGNAL=%d", getenv ("DISPLAY"), signum);
-    exit (EXIT_SUCCESS);
+    status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGINT);
+    g_main_loop_quit (loop);
+    return TRUE;
 }
 
-int
-main (int argc, char **argv)
+static gboolean
+sigterm_cb (gpointer user_data)
 {
-    GMainLoop *loop;
-    xcb_connection_t *connection;
-    gchar *logout_display;
+    status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGTERM);
+    g_main_loop_quit (loop);
+    return TRUE;
+}
 
-    signal (SIGINT, quit_cb);
-    signal (SIGTERM, quit_cb);
+static void
+request_cb (const gchar *request)
+{
+    gchar *r;
 
-    if (argc > 1)
-        notify_status ("SESSION %s START NAME=%s USER=%s", getenv ("DISPLAY"), argv[1], getenv ("USER"));
-    else
-        notify_status ("SESSION %s START USER=%s", getenv ("DISPLAY"), getenv ("USER"));
+    if (!request)
+    {
+        g_main_loop_quit (loop);
+        return;
+    }
+  
+    r = g_strdup_printf ("%s LOGOUT", session_id);
+    if (strcmp (request, r) == 0)
+        exit (EXIT_SUCCESS);
+    g_free (r);
+  
+    r = g_strdup_printf ("%s CRASH", session_id);
+    if (strcmp (request, r) == 0)
+        kill (getpid (), SIGSEGV);
+    g_free (r);
 
-    config = g_key_file_new ();
-    if (g_getenv ("LIGHTDM_TEST_CONFIG"))
-        g_key_file_load_from_file (config, g_getenv ("LIGHTDM_TEST_CONFIG"), G_KEY_FILE_NONE, NULL);
+    r = g_strdup_printf ("%s LOCK-SEAT", session_id);
+    if (strcmp (request, r) == 0)
+    {
+        status_notify ("%s LOCK-SEAT", session_id);
+        g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
+                                     "org.freedesktop.DisplayManager",
+                                     getenv ("XDG_SEAT_PATH"),
+                                     "org.freedesktop.DisplayManager.Seat",
+                                     "Lock",
+                                     g_variant_new ("()"),
+                                     G_VARIANT_TYPE ("()"),
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     1000,
+                                     NULL,
+                                     NULL);
+    }
+    g_free (r);
 
-    loop = g_main_loop_new (NULL, FALSE);
+    r = g_strdup_printf ("%s LOCK-SESSION", session_id);
+    if (strcmp (request, r) == 0)
+    {
+        status_notify ("%s LOCK-SESSION", session_id);
+        g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
+                                     "org.freedesktop.DisplayManager",
+                                     getenv ("XDG_SESSION_PATH"),
+                                     "org.freedesktop.DisplayManager.Session",
+                                     "Lock",
+                                     g_variant_new ("()"),
+                                     G_VARIANT_TYPE ("()"),
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     1000,
+                                     NULL,
+                                     NULL);
+    }
+    g_free (r);
+
+    r = g_strdup_printf ("%s LIST-GROUPS", session_id);
+    if (strcmp (request, r) == 0)
+    {
+        int n_groups, i;
+        gid_t *groups;
+        GString *group_list;
 
-    connection = xcb_connect (NULL, NULL);
+        n_groups = getgroups (0, NULL);
+        if (n_groups < 0)
+        {
+            g_printerr ("Failed to get groups: %s", strerror (errno));
+            n_groups = 0;
+        }
+        groups = malloc (sizeof (gid_t) * n_groups);
+        n_groups = getgroups (n_groups, groups);
+        group_list = g_string_new ("");
+        for (i = 0; i < n_groups; i++)
+        {
+            struct group *group;
 
-    if (xcb_connection_has_error (connection))
+            if (i != 0)
+                g_string_append (group_list, ",");
+            group = getgrgid (groups[i]);
+            if (group)
+                g_string_append (group_list, group->gr_name);
+            else
+                g_string_append_printf (group_list, "%d", groups[i]);
+        }
+        status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
+        g_string_free (group_list, TRUE);
+        free (groups);
+    }
+
+    r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
+    if (g_str_has_prefix (request, r))
     {
-        notify_status ("SESSION %s CONNECT-XSERVER-ERROR", getenv ("DISPLAY"));
-        return EXIT_FAILURE;
+        const gchar *name = request + strlen (r);
+        const gchar *value = g_getenv (name);
+        status_notify ("%s READ-ENV NAME=%s VALUE=%s", session_id, name, value ? value : "");
     }
+    g_free (r);
+
+    r = g_strdup_printf ("%s WRITE-STDOUT TEXT=", session_id);
+    if (g_str_has_prefix (request, r))
+        g_print ("%s", request + strlen (r));
+    g_free (r);
 
-    notify_status ("SESSION %s CONNECT-XSERVER", getenv ("DISPLAY"));
+    r = g_strdup_printf ("%s WRITE-STDERR TEXT=", session_id);
+    if (g_str_has_prefix (request, r))
+        g_printerr ("%s", request + strlen (r));
+    g_free (r);
 
-    if (g_key_file_get_boolean (config, "test-session-config", "crash-xserver", NULL))
+    r = g_strdup_printf ("%s READ FILE=", session_id);
+    if (g_str_has_prefix (request, r))
     {
-        const gchar *name = "SIGSEGV";
-        notify_status ("SESSION %s CRASH-XSERVER", getenv ("DISPLAY"));
-        xcb_intern_atom (connection, FALSE, strlen (name), name);
-        xcb_flush (connection);
+        const gchar *name = request + strlen (r);
+        gchar *contents;
+        GError *error = NULL;
+
+        if (g_file_get_contents (name, &contents, NULL, &error))
+            status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
+        else
+            status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
+        g_clear_error (&error);
     }
+    g_free (r);
 
-    logout_display = g_key_file_get_string (config, "test-session-config", "logout-display", NULL);
-    if (logout_display && strcmp (logout_display, getenv ("DISPLAY")) == 0)
+    r = g_strdup_printf ("%s LIST-UNKNOWN-FILE-DESCRIPTORS", session_id);
+    if (strcmp (request, r) == 0)
+        status_notify ("%s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", session_id, open_fds->str);
+    g_free (r);
+
+    r = g_strdup_printf ("%s CHECK-X-AUTHORITY", session_id);
+    if (strcmp (request, r) == 0)
     {
-        sleep (1);
-        notify_status ("SESSION %s LOGOUT", getenv ("DISPLAY"));
-        return EXIT_SUCCESS;
+        gchar *xauthority;
+        GStatBuf file_info;
+        GString *mode_string;
+
+        xauthority = g_strdup (g_getenv ("XAUTHORITY"));
+        if (!xauthority)
+            xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL);
+
+        g_stat (xauthority, &file_info);
+        g_free (xauthority);
+
+        mode_string = g_string_new ("");
+        g_string_append_c (mode_string, file_info.st_mode & S_IRUSR ? 'r' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IWUSR ? 'w' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IXUSR ? 'x' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IRGRP ? 'r' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IWGRP ? 'w' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IXGRP ? 'x' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IROTH ? 'r' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IWOTH ? 'w' : '-');
+        g_string_append_c (mode_string, file_info.st_mode & S_IXOTH ? 'x' : '-');
+        status_notify ("%s CHECK-X-AUTHORITY MODE=%s", session_id, mode_string->str);
+        g_string_free (mode_string, TRUE);
     }
+    g_free (r);
+}
+
+int
+main (int argc, char **argv)
+{
+    gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_session_cookie, *xdg_session_class, *desktop_session, *mir_socket, *mir_vt, *mir_id;
+    GString *status_text;
+    int fd, open_max;
 
-    if (g_key_file_get_boolean (config, "test-session-config", "sigsegv", NULL))
+    display = getenv ("DISPLAY");
+    xdg_seat = getenv ("XDG_SEAT");
+    xdg_vtnr = getenv ("XDG_VTNR");
+    xdg_current_desktop = getenv ("XDG_CURRENT_DESKTOP");
+    xdg_session_cookie = getenv ("XDG_SESSION_COOKIE");
+    xdg_session_class = getenv ("XDG_SESSION_CLASS");
+    desktop_session = getenv ("DESKTOP_SESSION");
+    mir_socket = getenv ("MIR_SOCKET");
+    mir_vt = getenv ("MIR_SERVER_VT");
+    mir_id = getenv ("MIR_SERVER_NAME");
+    if (display)
     {
-        notify_status ("SESSION %s CRASH", getenv ("DISPLAY"));
-        kill (getpid (), SIGSEGV);
+        if (display[0] == ':')
+            session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
+        else
+            session_id = g_strdup_printf ("SESSION-X-%s", display);
     }
-  
-    g_main_loop_run (loop);    
+    else if (mir_id)
+        session_id = g_strdup_printf ("SESSION-MIR-%s", mir_id);
+    else if (mir_socket || mir_vt)
+        session_id = g_strdup ("SESSION-MIR");
+    else
+        session_id = g_strdup ("SESSION-UNKNOWN");
+
+    open_fds = g_string_new ("");
+    open_max = sysconf (_SC_OPEN_MAX);
+    for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
+    {
+        if (fcntl (fd, F_GETFD) >= 0)
+            g_string_append_printf (open_fds, "%d,", fd);
+    }
+    if (g_str_has_suffix (open_fds->str, ","))
+        open_fds->str[strlen (open_fds->str) - 1] = '\0';
+
+#if !defined(GLIB_VERSION_2_36)
+    g_type_init ();
+#endif
+
+    loop = g_main_loop_new (NULL, FALSE);
+
+    g_unix_signal_add (SIGINT, sigint_cb, NULL);
+    g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
+
+    status_connect (request_cb);
+
+    status_text = g_string_new ("");
+    g_string_printf (status_text, "%s START", session_id);
+    if (xdg_seat)
+        g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
+    if (xdg_vtnr)
+        g_string_append_printf (status_text, " XDG_VTNR=%s", xdg_vtnr);
+    if (xdg_current_desktop)
+        g_string_append_printf (status_text, " XDG_CURRENT_DESKTOP=%s", xdg_current_desktop);
+    if (xdg_session_cookie)
+        g_string_append_printf (status_text, " XDG_SESSION_COOKIE=%s", xdg_session_cookie);
+    if (xdg_session_class)
+        g_string_append_printf (status_text, " XDG_SESSION_CLASS=%s", xdg_session_class);
+    if (desktop_session)
+        g_string_append_printf (status_text, " DESKTOP_SESSION=%s", desktop_session);
+    if (mir_vt > 0)
+        g_string_append_printf (status_text, " MIR_SERVER_VT=%s", mir_vt);
+    if (argc > 1)
+        g_string_append_printf (status_text, " NAME=%s", argv[1]);
+    g_string_append_printf (status_text, " USER=%s", getenv ("USER"));
+    status_notify (status_text->str);
+    g_string_free (status_text, TRUE);
+
+    config = g_key_file_new ();
+    g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
+
+    if (display)
+    {
+        connection = xcb_connect (NULL, NULL);
+        if (xcb_connection_has_error (connection))
+        {
+            status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
+            return EXIT_FAILURE;
+        }
+        status_notify ("%s CONNECT-XSERVER", session_id);
+    }
+
+    g_main_loop_run (loop);
 
     return EXIT_SUCCESS;
 }