10 #include <glib-object.h>
12 #include <glib-unix.h>
16 static gchar *session_id;
18 static GMainLoop *loop;
20 static GString *open_fds;
22 static GKeyFile *config;
24 static xcb_connection_t *connection;
27 sigint_cb (gpointer user_data)
29 status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGINT);
30 g_main_loop_quit (loop);
35 sigterm_cb (gpointer user_data)
37 status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGTERM);
38 g_main_loop_quit (loop);
43 request_cb (const gchar *request)
49 g_main_loop_quit (loop);
53 r = g_strdup_printf ("%s LOGOUT", session_id);
54 if (strcmp (request, r) == 0)
58 r = g_strdup_printf ("%s CRASH", session_id);
59 if (strcmp (request, r) == 0)
60 kill (getpid (), SIGSEGV);
63 r = g_strdup_printf ("%s LOCK-SEAT", session_id);
64 if (strcmp (request, r) == 0)
66 status_notify ("%s LOCK-SEAT", session_id);
67 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
68 "org.freedesktop.DisplayManager",
69 getenv ("XDG_SEAT_PATH"),
70 "org.freedesktop.DisplayManager.Seat",
73 G_VARIANT_TYPE ("()"),
74 G_DBUS_CALL_FLAGS_NONE,
81 r = g_strdup_printf ("%s LOCK-SESSION", session_id);
82 if (strcmp (request, r) == 0)
84 status_notify ("%s LOCK-SESSION", session_id);
85 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
86 "org.freedesktop.DisplayManager",
87 getenv ("XDG_SESSION_PATH"),
88 "org.freedesktop.DisplayManager.Session",
91 G_VARIANT_TYPE ("()"),
92 G_DBUS_CALL_FLAGS_NONE,
99 r = g_strdup_printf ("%s LIST-GROUPS", session_id);
100 if (strcmp (request, r) == 0)
106 n_groups = getgroups (0, NULL);
107 groups = malloc (sizeof (gid_t) * n_groups);
108 n_groups = getgroups (n_groups, groups);
109 group_list = g_string_new ("");
110 for (i = 0; i < n_groups; i++)
115 g_string_append (group_list, ",");
116 group = getgrgid (groups[i]);
118 g_string_append (group_list, group->gr_name);
120 g_string_append_printf (group_list, "%d", groups[i]);
122 status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
123 g_string_free (group_list, TRUE);
127 r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
128 if (g_str_has_prefix (request, r))
130 const gchar *name = request + strlen (r);
131 const gchar *value = g_getenv (name);
132 status_notify ("%s READ-ENV NAME=%s VALUE=%s", session_id, name, value ? value : "");
136 r = g_strdup_printf ("%s WRITE-STDOUT TEXT=", session_id);
137 if (g_str_has_prefix (request, r))
138 g_print ("%s", request + strlen (r));
141 r = g_strdup_printf ("%s WRITE-STDERR TEXT=", session_id);
142 if (g_str_has_prefix (request, r))
143 g_printerr ("%s", request + strlen (r));
146 r = g_strdup_printf ("%s READ FILE=", session_id);
147 if (g_str_has_prefix (request, r))
149 const gchar *name = request + strlen (r);
151 GError *error = NULL;
153 if (g_file_get_contents (name, &contents, NULL, &error))
154 status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
156 status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
157 g_clear_error (&error);
161 r = g_strdup_printf ("%s LIST-UNKNOWN-FILE-DESCRIPTORS", session_id);
162 if (strcmp (request, r) == 0)
163 status_notify ("%s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", session_id, open_fds->str);
168 main (int argc, char **argv)
170 gchar *display, *mir_socket;
173 display = getenv ("DISPLAY");
174 mir_socket = getenv ("DISPLAY");
177 if (display[0] == ':')
178 session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
180 session_id = g_strdup_printf ("SESSION-X-%s", display);
183 session_id = g_strdup ("SESSION-MIR");
185 session_id = g_strdup ("SESSION-?");
187 open_fds = g_string_new ("");
188 open_max = sysconf (_SC_OPEN_MAX);
189 for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
191 if (fcntl (fd, F_GETFD) >= 0)
192 g_string_append_printf (open_fds, "%d,", fd);
194 if (g_str_has_suffix (open_fds->str, ","))
195 open_fds->str[strlen (open_fds->str) - 1] = '\0';
197 #if !defined(GLIB_VERSION_2_36)
201 loop = g_main_loop_new (NULL, FALSE);
203 g_unix_signal_add (SIGINT, sigint_cb, NULL);
204 g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
206 status_connect (request_cb);
209 status_notify ("%s START NAME=%s USER=%s", session_id, argv[1], getenv ("USER"));
211 status_notify ("%s START USER=%s", session_id, getenv ("USER"));
213 config = g_key_file_new ();
214 g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
216 connection = xcb_connect (NULL, NULL);
218 if (xcb_connection_has_error (connection))
220 status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
224 status_notify ("%s CONNECT-XSERVER", session_id);
226 g_main_loop_run (loop);