]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/test-session.c
Fix a bunch of small errors picked up by Coverity.
[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
14 #include "status.h"
15
16 static gchar *session_id;
17
18 static GMainLoop *loop;
19
20 static GString *open_fds;
21
22 static GKeyFile *config;
23
24 static xcb_connection_t *connection;
25
26 static gboolean
27 sigint_cb (gpointer user_data)
28 {
29     status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGINT);
30     g_main_loop_quit (loop);
31     return TRUE;
32 }
33
34 static gboolean
35 sigterm_cb (gpointer user_data)
36 {
37     status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGTERM);
38     g_main_loop_quit (loop);
39     return TRUE;
40 }
41
42 static void
43 request_cb (const gchar *request)
44 {
45     gchar *r;
46
47     if (!request)
48     {
49         g_main_loop_quit (loop);
50         return;
51     }
52   
53     r = g_strdup_printf ("%s LOGOUT", session_id);
54     if (strcmp (request, r) == 0)
55         exit (EXIT_SUCCESS);
56     g_free (r);
57   
58     r = g_strdup_printf ("%s CRASH", session_id);
59     if (strcmp (request, r) == 0)
60         kill (getpid (), SIGSEGV);
61     g_free (r);
62
63     r = g_strdup_printf ("%s LOCK-SEAT", session_id);
64     if (strcmp (request, r) == 0)
65     {
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",
71                                      "Lock",
72                                      g_variant_new ("()"),
73                                      G_VARIANT_TYPE ("()"),
74                                      G_DBUS_CALL_FLAGS_NONE,
75                                      1000,
76                                      NULL,
77                                      NULL);
78     }
79     g_free (r);
80
81     r = g_strdup_printf ("%s LOCK-SESSION", session_id);
82     if (strcmp (request, r) == 0)
83     {
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",
89                                      "Lock",
90                                      g_variant_new ("()"),
91                                      G_VARIANT_TYPE ("()"),
92                                      G_DBUS_CALL_FLAGS_NONE,
93                                      1000,
94                                      NULL,
95                                      NULL);
96     }
97     g_free (r);
98
99     r = g_strdup_printf ("%s LIST-GROUPS", session_id);
100     if (strcmp (request, r) == 0)
101     {
102         int n_groups, i;
103         gid_t *groups;
104         GString *group_list;
105
106         n_groups = getgroups (0, NULL);
107         if (n_groups < 0)
108         {
109             g_printerr ("Failed to get groups: %s", strerror (errno));
110             n_groups = 0;
111         }
112         groups = malloc (sizeof (gid_t) * n_groups);
113         n_groups = getgroups (n_groups, groups);
114         group_list = g_string_new ("");
115         for (i = 0; i < n_groups; i++)
116         {
117             struct group *group;
118
119             if (i != 0)
120                 g_string_append (group_list, ",");
121             group = getgrgid (groups[i]);
122             if (group)
123                 g_string_append (group_list, group->gr_name);
124             else
125                 g_string_append_printf (group_list, "%d", groups[i]);
126         }
127         status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
128         g_string_free (group_list, TRUE);
129         free (groups);
130     }
131
132     r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
133     if (g_str_has_prefix (request, r))
134     {
135         const gchar *name = request + strlen (r);
136         const gchar *value = g_getenv (name);
137         status_notify ("%s READ-ENV NAME=%s VALUE=%s", session_id, name, value ? value : "");
138     }
139     g_free (r);
140
141     r = g_strdup_printf ("%s WRITE-STDOUT TEXT=", session_id);
142     if (g_str_has_prefix (request, r))
143         g_print ("%s", request + strlen (r));
144     g_free (r);
145
146     r = g_strdup_printf ("%s WRITE-STDERR TEXT=", session_id);
147     if (g_str_has_prefix (request, r))
148         g_printerr ("%s", request + strlen (r));
149     g_free (r);
150
151     r = g_strdup_printf ("%s READ FILE=", session_id);
152     if (g_str_has_prefix (request, r))
153     {
154         const gchar *name = request + strlen (r);
155         gchar *contents;
156         GError *error = NULL;
157
158         if (g_file_get_contents (name, &contents, NULL, &error))
159             status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
160         else
161             status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
162         g_clear_error (&error);
163     }
164     g_free (r);
165
166     r = g_strdup_printf ("%s LIST-UNKNOWN-FILE-DESCRIPTORS", session_id);
167     if (strcmp (request, r) == 0)
168         status_notify ("%s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", session_id, open_fds->str);
169     g_free (r);
170 }
171
172 int
173 main (int argc, char **argv)
174 {
175     gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_session_cookie, *mir_socket, *mir_vt, *mir_id;
176     GString *status_text;
177     int fd, open_max;
178
179     display = getenv ("DISPLAY");
180     xdg_seat = getenv ("XDG_SEAT");
181     xdg_vtnr = getenv ("XDG_VTNR");
182     xdg_current_desktop = getenv ("XDG_CURRENT_DESKTOP");
183     xdg_session_cookie = getenv ("XDG_SESSION_COOKIE");
184     mir_socket = getenv ("MIR_SERVER_FILE");
185     mir_vt = getenv ("MIR_SERVER_VT");
186     mir_id = getenv ("MIR_ID");
187     if (display)
188     {
189         if (display[0] == ':')
190             session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
191         else
192             session_id = g_strdup_printf ("SESSION-X-%s", display);
193     }
194     else if (mir_id)
195         session_id = g_strdup_printf ("SESSION-MIR-%s", mir_id);
196     else if (mir_socket || mir_vt)
197         session_id = g_strdup ("SESSION-MIR");
198     else
199         session_id = g_strdup ("SESSION-?");
200
201     open_fds = g_string_new ("");
202     open_max = sysconf (_SC_OPEN_MAX);
203     for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
204     {
205         if (fcntl (fd, F_GETFD) >= 0)
206             g_string_append_printf (open_fds, "%d,", fd);
207     }
208     if (g_str_has_suffix (open_fds->str, ","))
209         open_fds->str[strlen (open_fds->str) - 1] = '\0';
210
211 #if !defined(GLIB_VERSION_2_36)
212     g_type_init ();
213 #endif
214
215     loop = g_main_loop_new (NULL, FALSE);
216
217     g_unix_signal_add (SIGINT, sigint_cb, NULL);
218     g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
219
220     status_connect (request_cb);
221
222     status_text = g_string_new ("");
223     g_string_printf (status_text, "%s START", session_id);
224     if (xdg_seat)
225         g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
226     if (xdg_vtnr)
227         g_string_append_printf (status_text, " XDG_VTNR=%s", xdg_vtnr);
228     if (xdg_current_desktop)
229         g_string_append_printf (status_text, " XDG_CURRENT_DESKTOP=%s", xdg_current_desktop);
230     if (xdg_session_cookie)
231         g_string_append_printf (status_text, " XDG_SESSION_COOKIE=%s", xdg_session_cookie);
232     if (mir_vt > 0)
233         g_string_append_printf (status_text, " MIR_SERVER_VT=%s", mir_vt);
234     if (argc > 1)
235         g_string_append_printf (status_text, " NAME=%s", argv[1]);
236     g_string_append_printf (status_text, " USER=%s", getenv ("USER"));
237     status_notify (status_text->str);
238     g_string_free (status_text, TRUE);
239
240     config = g_key_file_new ();
241     g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
242
243     if (display)
244     {
245         connection = xcb_connect (NULL, NULL);
246         if (xcb_connection_has_error (connection))
247         {
248             status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
249             return EXIT_FAILURE;
250         }
251         status_notify ("%s CONNECT-XSERVER", session_id);
252     }
253
254     g_main_loop_run (loop);
255
256     return EXIT_SUCCESS;
257 }