]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/test-session.c
1455d73f6a17634ef3c03b45589f8182585e8dc3
[sojka/lightdm.git] / tests / src / test-session.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <grp.h>
8 #include <xcb/xcb.h>
9 #include <glib.h>
10 #include <glib-object.h>
11 #include <gio/gio.h>
12 #include <glib-unix.h>
13 #include <glib/gstdio.h>
14
15 #include "status.h"
16
17 static gchar *session_id;
18
19 static GMainLoop *loop;
20
21 static GString *open_fds;
22
23 static GKeyFile *config;
24
25 static xcb_connection_t *connection;
26
27 static gboolean
28 sigint_cb (gpointer user_data)
29 {
30     status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGINT);
31     g_main_loop_quit (loop);
32     return TRUE;
33 }
34
35 static gboolean
36 sigterm_cb (gpointer user_data)
37 {
38     status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGTERM);
39     g_main_loop_quit (loop);
40     return TRUE;
41 }
42
43 static void
44 request_cb (const gchar *name, GHashTable *params)
45 {
46     if (!name)
47     {
48         g_main_loop_quit (loop);
49         return;
50     }
51
52     if (strcmp (name, "LOGOUT") == 0)
53         exit (EXIT_SUCCESS);
54
55     else if (strcmp (name, "CRASH") == 0)
56         kill (getpid (), SIGSEGV);
57
58     else if (strcmp (name, "LOCK-SEAT") == 0)
59     {
60         status_notify ("%s LOCK-SEAT", session_id);
61         g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
62                                      "org.freedesktop.DisplayManager",
63                                      getenv ("XDG_SEAT_PATH"),
64                                      "org.freedesktop.DisplayManager.Seat",
65                                      "Lock",
66                                      g_variant_new ("()"),
67                                      G_VARIANT_TYPE ("()"),
68                                      G_DBUS_CALL_FLAGS_NONE,
69                                      1000,
70                                      NULL,
71                                      NULL);
72     }
73
74     else if (strcmp (name, "LOCK-SESSION") == 0)
75     {
76         status_notify ("%s LOCK-SESSION", session_id);
77         g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
78                                      "org.freedesktop.DisplayManager",
79                                      getenv ("XDG_SESSION_PATH"),
80                                      "org.freedesktop.DisplayManager.Session",
81                                      "Lock",
82                                      g_variant_new ("()"),
83                                      G_VARIANT_TYPE ("()"),
84                                      G_DBUS_CALL_FLAGS_NONE,
85                                      1000,
86                                      NULL,
87                                      NULL);
88     }
89
90     else if (strcmp (name, "LIST-GROUPS") == 0)
91     {
92         int n_groups, i;
93         gid_t *groups;
94         GString *group_list;
95
96         n_groups = getgroups (0, NULL);
97         if (n_groups < 0)
98         {
99             g_printerr ("Failed to get groups: %s", strerror (errno));
100             n_groups = 0;
101         }
102         groups = malloc (sizeof (gid_t) * n_groups);
103         n_groups = getgroups (n_groups, groups);
104         group_list = g_string_new ("");
105         for (i = 0; i < n_groups; i++)
106         {
107             struct group *group;
108
109             if (i != 0)
110                 g_string_append (group_list, ",");
111             group = getgrgid (groups[i]);
112             if (group)
113                 g_string_append (group_list, group->gr_name);
114             else
115                 g_string_append_printf (group_list, "%d", groups[i]);
116         }
117         status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
118         g_string_free (group_list, TRUE);
119         free (groups);
120     }
121
122     else if (strcmp (name, "READ-ENV") == 0)
123     {
124         const gchar *name = g_hash_table_lookup (params, "NAME");
125         const gchar *value = g_getenv (name);
126         status_notify ("%s READ-ENV NAME=%s VALUE=%s", session_id, name, value ? value : "");
127     }
128
129     else if (strcmp (name, "WRITE-STDOUT") == 0)
130         g_print ("%s", (const gchar *) g_hash_table_lookup (params, "TEXT"));
131
132     else if (strcmp (name, "WRITE-STDERR") == 0)
133         g_printerr ("%s", (const gchar *) g_hash_table_lookup (params, "TEXT"));
134
135     else if (strcmp (name, "READ") == 0)
136     {
137         const gchar *name = g_hash_table_lookup (params, "FILE");
138         gchar *contents = NULL;
139         GError *error = NULL;
140
141         if (g_file_get_contents (name, &contents, NULL, &error))
142             status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
143         else
144             status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
145         g_free (contents);
146         g_clear_error (&error);
147     }
148
149     else if (strcmp (name, "LIST-UNKNOWN-FILE-DESCRIPTORS") == 0)
150         status_notify ("%s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", session_id, open_fds->str);
151
152     else if (strcmp (name, "CHECK-X-AUTHORITY") == 0)
153     {
154         gchar *xauthority;
155         GStatBuf file_info;
156         GString *mode_string;
157
158         xauthority = g_strdup (g_getenv ("XAUTHORITY"));
159         if (!xauthority)
160             xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL);
161
162         g_stat (xauthority, &file_info);
163         g_free (xauthority);
164
165         mode_string = g_string_new ("");
166         g_string_append_c (mode_string, file_info.st_mode & S_IRUSR ? 'r' : '-');
167         g_string_append_c (mode_string, file_info.st_mode & S_IWUSR ? 'w' : '-');
168         g_string_append_c (mode_string, file_info.st_mode & S_IXUSR ? 'x' : '-');
169         g_string_append_c (mode_string, file_info.st_mode & S_IRGRP ? 'r' : '-');
170         g_string_append_c (mode_string, file_info.st_mode & S_IWGRP ? 'w' : '-');
171         g_string_append_c (mode_string, file_info.st_mode & S_IXGRP ? 'x' : '-');
172         g_string_append_c (mode_string, file_info.st_mode & S_IROTH ? 'r' : '-');
173         g_string_append_c (mode_string, file_info.st_mode & S_IWOTH ? 'w' : '-');
174         g_string_append_c (mode_string, file_info.st_mode & S_IXOTH ? 'x' : '-');
175         status_notify ("%s CHECK-X-AUTHORITY MODE=%s", session_id, mode_string->str);
176         g_string_free (mode_string, TRUE);
177     }
178
179     else if (strcmp (name, "WRITE-SHARED-DATA") == 0)
180     {
181         const gchar *data = g_hash_table_lookup (params, "DATA");
182         gchar *dir;
183
184         dir = getenv ("XDG_GREETER_DATA_DIR");
185         if (dir)
186         {
187             gchar *path;
188             FILE *f;
189
190             path = g_build_filename (dir, "data", NULL);
191             if (!(f = fopen (path, "w")) || fprintf (f, "%s", data) < 0)
192                 status_notify ("%s WRITE-SHARED-DATA ERROR=%s", session_id, strerror (errno));
193             else
194                 status_notify ("%s WRITE-SHARED-DATA RESULT=TRUE", session_id);
195
196             if (f)
197                 fclose (f);
198             g_free (path);
199         }
200         else
201             status_notify ("%s WRITE-SHARED-DATA ERROR=NO_XDG_GREETER_DATA_DIR", session_id);
202     }
203
204     else if (strcmp (name, "READ-SHARED-DATA") == 0)
205     {
206         gchar *dir;
207
208         dir = getenv ("XDG_GREETER_DATA_DIR");
209         if (dir)
210         {
211             gchar *path;
212             gchar *contents = NULL;
213             GError *error = NULL;
214
215             path = g_build_filename (dir, "data", NULL);
216             if (g_file_get_contents (path, &contents, NULL, &error))
217                 status_notify ("%s READ-SHARED-DATA DATA=%s", session_id, contents);
218             else
219                 status_notify ("%s WRITE-SHARED-DATA ERROR=%s", session_id, error->message);
220             g_free (path);
221             g_free (contents);
222             g_clear_error (&error);
223         }
224         else
225             status_notify ("%s WRITE-SHARED-DATA ERROR=NO_XDG_GREETER_DATA_DIR", session_id);
226     }
227 }
228
229 int
230 main (int argc, char **argv)
231 {
232     gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_greeter_data_dir, *xdg_session_cookie, *xdg_session_class, *xdg_session_type, *xdg_session_desktop, *mir_server_host_socket, *mir_vt, *mir_id;
233     GString *status_text;
234     int fd, open_max;
235
236     display = getenv ("DISPLAY");
237     xdg_seat = getenv ("XDG_SEAT");
238     xdg_vtnr = getenv ("XDG_VTNR");
239     xdg_current_desktop = getenv ("XDG_CURRENT_DESKTOP");
240     xdg_greeter_data_dir = getenv ("XDG_GREETER_DATA_DIR");
241     xdg_session_cookie = getenv ("XDG_SESSION_COOKIE");
242     xdg_session_class = getenv ("XDG_SESSION_CLASS");
243     xdg_session_type = getenv ("XDG_SESSION_TYPE");
244     xdg_session_desktop = getenv ("XDG_SESSION_DESKTOP");
245     mir_server_host_socket = getenv ("MIR_SERVER_HOST_SOCKET");
246     mir_vt = getenv ("MIR_SERVER_VT");
247     mir_id = getenv ("MIR_SERVER_NAME");
248     if (display)
249     {
250         if (display[0] == ':')
251             session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
252         else
253             session_id = g_strdup_printf ("SESSION-X-%s", display);
254     }
255     else if (mir_id)
256         session_id = g_strdup_printf ("SESSION-MIR-%s", mir_id);
257     else if (mir_server_host_socket || mir_vt)
258         session_id = g_strdup ("SESSION-MIR");
259     else if (g_strcmp0 (xdg_session_type, "wayland") == 0)
260         session_id = g_strdup ("SESSION-WAYLAND");
261     else
262         session_id = g_strdup ("SESSION-UNKNOWN");
263
264     open_fds = g_string_new ("");
265     open_max = sysconf (_SC_OPEN_MAX);
266     for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
267     {
268         if (fcntl (fd, F_GETFD) >= 0)
269             g_string_append_printf (open_fds, "%d,", fd);
270     }
271     if (g_str_has_suffix (open_fds->str, ","))
272         open_fds->str[strlen (open_fds->str) - 1] = '\0';
273
274 #if !defined(GLIB_VERSION_2_36)
275     g_type_init ();
276 #endif
277
278     loop = g_main_loop_new (NULL, FALSE);
279
280     g_unix_signal_add (SIGINT, sigint_cb, NULL);
281     g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
282
283     status_connect (request_cb, session_id);
284
285     status_text = g_string_new ("");
286     g_string_printf (status_text, "%s START", session_id);
287     if (xdg_seat)
288         g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
289     if (xdg_vtnr)
290         g_string_append_printf (status_text, " XDG_VTNR=%s", xdg_vtnr);
291     if (xdg_current_desktop)
292         g_string_append_printf (status_text, " XDG_CURRENT_DESKTOP=%s", xdg_current_desktop);
293     if (xdg_greeter_data_dir)
294         g_string_append_printf (status_text, " XDG_GREETER_DATA_DIR=%s", xdg_greeter_data_dir);
295     if (xdg_session_cookie)
296         g_string_append_printf (status_text, " XDG_SESSION_COOKIE=%s", xdg_session_cookie);
297     if (xdg_session_class)
298         g_string_append_printf (status_text, " XDG_SESSION_CLASS=%s", xdg_session_class);
299     if (xdg_session_type)
300         g_string_append_printf (status_text, " XDG_SESSION_TYPE=%s", xdg_session_type);
301     if (xdg_session_desktop)
302         g_string_append_printf (status_text, " XDG_SESSION_DESKTOP=%s", xdg_session_desktop);
303     if (mir_vt > 0)
304         g_string_append_printf (status_text, " MIR_SERVER_VT=%s", mir_vt);
305     if (argc > 1)
306         g_string_append_printf (status_text, " NAME=%s", argv[1]);
307     g_string_append_printf (status_text, " USER=%s", getenv ("USER"));
308     status_notify ("%s", status_text->str);
309     g_string_free (status_text, TRUE);
310
311     config = g_key_file_new ();
312     g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
313
314     if (display)
315     {
316         connection = xcb_connect (NULL, NULL);
317         if (xcb_connection_has_error (connection))
318         {
319             status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
320             return EXIT_FAILURE;
321         }
322         status_notify ("%s CONNECT-XSERVER", session_id);
323     }
324
325     g_main_loop_run (loop);
326
327     return EXIT_SUCCESS;
328 }