Back to index

lightdm  1.3.2
test-session.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <signal.h>
00005 #include <sys/types.h>
00006 #include <unistd.h>
00007 #include <fcntl.h>
00008 #include <grp.h>
00009 #include <xcb/xcb.h>
00010 #include <glib.h>
00011 #include <glib-object.h>
00012 #include <gio/gio.h>
00013 
00014 #include "status.h"
00015 
00016 static GString *open_fds;
00017 
00018 static GKeyFile *config;
00019 
00020 static xcb_connection_t *connection;
00021 
00022 static void
00023 quit_cb (int signum)
00024 {
00025     status_notify ("SESSION %s TERMINATE SIGNAL=%d", getenv ("DISPLAY"), signum);
00026     exit (EXIT_SUCCESS);
00027 }
00028 
00029 static void
00030 request_cb (const gchar *request)
00031 {
00032     gchar *r;
00033   
00034     r = g_strdup_printf ("SESSION %s LOGOUT", getenv ("DISPLAY"));
00035     if (strcmp (request, r) == 0)
00036         exit (EXIT_SUCCESS);
00037     g_free (r);
00038   
00039     r = g_strdup_printf ("SESSION %s CRASH", getenv ("DISPLAY"));
00040     if (strcmp (request, r) == 0)
00041         kill (getpid (), SIGSEGV);
00042     g_free (r);
00043 
00044     r = g_strdup_printf ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
00045     if (strcmp (request, r) == 0)
00046     {
00047         g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
00048                                      "org.freedesktop.DisplayManager",
00049                                      getenv ("XDG_SEAT_PATH"),
00050                                      "org.freedesktop.DisplayManager.Seat",
00051                                      "Lock",
00052                                      g_variant_new ("()"),
00053                                      G_VARIANT_TYPE ("()"),
00054                                      G_DBUS_CALL_FLAGS_NONE,
00055                                      1000,
00056                                      NULL,
00057                                      NULL);
00058         status_notify ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
00059     }
00060     g_free (r);
00061 
00062     r = g_strdup_printf ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
00063     if (strcmp (request, r) == 0)
00064     {
00065         g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
00066                                      "org.freedesktop.DisplayManager",
00067                                      getenv ("XDG_SESSION_PATH"),
00068                                      "org.freedesktop.DisplayManager.Session",
00069                                      "Lock",
00070                                      g_variant_new ("()"),
00071                                      G_VARIANT_TYPE ("()"),
00072                                      G_DBUS_CALL_FLAGS_NONE,
00073                                      1000,
00074                                      NULL,
00075                                      NULL);
00076         status_notify ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
00077     }
00078     g_free (r);
00079 
00080     r = g_strdup_printf ("SESSION %s LIST-GROUPS", getenv ("DISPLAY"));
00081     if (strcmp (request, r) == 0)
00082     {
00083         int n_groups, i;
00084         gid_t *groups;
00085         GString *group_list;
00086 
00087         n_groups = getgroups (0, NULL);
00088         groups = malloc (sizeof (gid_t) * n_groups);
00089         n_groups = getgroups (n_groups, groups);
00090         group_list = g_string_new ("");
00091         for (i = 0; i < n_groups; i++)
00092         {
00093             struct group *group;
00094 
00095             if (i != 0)
00096                 g_string_append (group_list, ",");
00097             group = getgrgid (groups[i]);
00098             if (group)
00099                 g_string_append (group_list, group->gr_name);
00100             else
00101                 g_string_append_printf (group_list, "%d", groups[i]);
00102         }
00103         status_notify ("SESSION %s LIST-GROUPS GROUPS=%s", getenv ("DISPLAY"), group_list->str);
00104         g_string_free (group_list, TRUE);
00105         free (groups);
00106     }
00107 
00108     r = g_strdup_printf ("SESSION %s READ-ENV NAME=", getenv ("DISPLAY"));
00109     if (g_str_has_prefix (request, r))
00110     {
00111         const gchar *name = request + strlen (r);
00112         const gchar *value = g_getenv (name);
00113         status_notify ("SESSION %s READ-ENV NAME=%s VALUE=%s", getenv ("DISPLAY"), name, value ? value : "");
00114     }
00115     g_free (r);
00116 
00117     r = g_strdup_printf ("SESSION %s WRITE-STDOUT TEXT=", getenv ("DISPLAY"));
00118     if (g_str_has_prefix (request, r))
00119         g_print ("%s", request + strlen (r));
00120     g_free (r);
00121 
00122     r = g_strdup_printf ("SESSION %s WRITE-STDERR TEXT=", getenv ("DISPLAY"));
00123     if (g_str_has_prefix (request, r))
00124         g_printerr ("%s", request + strlen (r));
00125     g_free (r);
00126 
00127     r = g_strdup_printf ("SESSION %s READ FILE=", getenv ("DISPLAY"));
00128     if (g_str_has_prefix (request, r))
00129     {
00130         const gchar *name = request + strlen (r);
00131         gchar *contents;
00132         GError *error = NULL;
00133 
00134         if (g_file_get_contents (name, &contents, NULL, &error))
00135             status_notify ("SESSION %s READ FILE=%s TEXT=%s", getenv ("DISPLAY"), name, contents);
00136         else
00137             status_notify ("SESSION %s READ FILE=%s ERROR=%s", getenv ("DISPLAY"), name, error->message);
00138         g_clear_error (&error);
00139     }
00140     g_free (r);
00141 
00142     r = g_strdup_printf ("SESSION %s LIST-UNKNOWN-FILE-DESCRIPTORS", getenv ("DISPLAY"));
00143     if (strcmp (request, r) == 0)
00144         status_notify ("SESSION %s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", getenv ("DISPLAY"), open_fds->str);
00145     g_free (r);
00146 }
00147 
00148 int
00149 main (int argc, char **argv)
00150 {
00151     GMainLoop *loop;
00152     int fd, open_max;
00153 
00154     open_fds = g_string_new ("");
00155     open_max = sysconf (_SC_OPEN_MAX);
00156     for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
00157     {
00158         if (fcntl (fd, F_GETFD) >= 0)
00159             g_string_append_printf (open_fds, "%d,", fd);
00160     }
00161     if (g_str_has_suffix (open_fds->str, ","))
00162         open_fds->str[strlen (open_fds->str) - 1] = '\0';
00163 
00164     signal (SIGINT, quit_cb);
00165     signal (SIGTERM, quit_cb);
00166 
00167     g_type_init ();
00168 
00169     loop = g_main_loop_new (NULL, FALSE);
00170 
00171     status_connect (request_cb);
00172 
00173     if (argc > 1)
00174         status_notify ("SESSION %s START NAME=%s USER=%s", getenv ("DISPLAY"), argv[1], getenv ("USER"));
00175     else
00176         status_notify ("SESSION %s START USER=%s", getenv ("DISPLAY"), getenv ("USER"));
00177 
00178     config = g_key_file_new ();
00179     g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
00180 
00181     connection = xcb_connect (NULL, NULL);
00182 
00183     if (xcb_connection_has_error (connection))
00184     {
00185         status_notify ("SESSION %s CONNECT-XSERVER-ERROR", getenv ("DISPLAY"));
00186         return EXIT_FAILURE;
00187     }
00188 
00189     status_notify ("SESSION %s CONNECT-XSERVER", getenv ("DISPLAY"));
00190 
00191     g_main_loop_run (loop);    
00192 
00193     return EXIT_SUCCESS;
00194 }