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);
109 g_printerr ("Failed to get groups: %s", strerror (errno));
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++)
120 g_string_append (group_list, ",");
121 group = getgrgid (groups[i]);
123 g_string_append (group_list, group->gr_name);
125 g_string_append_printf (group_list, "%d", groups[i]);
127 status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
128 g_string_free (group_list, TRUE);
132 r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
133 if (g_str_has_prefix (request, r))
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 : "");
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));
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));
151 r = g_strdup_printf ("%s READ FILE=", session_id);
152 if (g_str_has_prefix (request, r))
154 const gchar *name = request + strlen (r);
156 GError *error = NULL;
158 if (g_file_get_contents (name, &contents, NULL, &error))
159 status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
161 status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
162 g_clear_error (&error);
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);
173 main (int argc, char **argv)
175 gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_session_cookie, *mir_socket, *mir_vt, *mir_id;
176 GString *status_text;
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");
189 if (display[0] == ':')
190 session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
192 session_id = g_strdup_printf ("SESSION-X-%s", display);
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");
199 session_id = g_strdup ("SESSION-?");
201 open_fds = g_string_new ("");
202 open_max = sysconf (_SC_OPEN_MAX);
203 for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
205 if (fcntl (fd, F_GETFD) >= 0)
206 g_string_append_printf (open_fds, "%d,", fd);
208 if (g_str_has_suffix (open_fds->str, ","))
209 open_fds->str[strlen (open_fds->str) - 1] = '\0';
211 #if !defined(GLIB_VERSION_2_36)
215 loop = g_main_loop_new (NULL, FALSE);
217 g_unix_signal_add (SIGINT, sigint_cb, NULL);
218 g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
220 status_connect (request_cb);
222 status_text = g_string_new ("");
223 g_string_printf (status_text, "%s START", session_id);
225 g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
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);
233 g_string_append_printf (status_text, " MIR_SERVER_VT=%s", mir_vt);
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);
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);
245 connection = xcb_connect (NULL, NULL);
246 if (xcb_connection_has_error (connection))
248 status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
251 status_notify ("%s CONNECT-XSERVER", session_id);
254 g_main_loop_run (loop);