11
#include <glib-object.h>
13
#include <glib/gstdio.h>
17
static gchar *session_id;
19
static GMainLoop *loop;
21
static GString *open_fds;
23
static GKeyFile *config;
25
static xcb_connection_t *connection;
30
status_notify ("%s TERMINATE SIGNAL=%d", session_id, signum);
35
request_cb (const gchar *request)
41
g_main_loop_quit (loop);
45
r = g_strdup_printf ("%s LOGOUT", session_id);
46
if (strcmp (request, r) == 0)
50
r = g_strdup_printf ("%s CRASH", session_id);
51
if (strcmp (request, r) == 0)
52
kill (getpid (), SIGSEGV);
55
r = g_strdup_printf ("%s LOCK-SEAT", session_id);
56
if (strcmp (request, r) == 0)
58
status_notify ("%s LOCK-SEAT", session_id);
59
g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
60
"org.freedesktop.DisplayManager",
61
getenv ("XDG_SEAT_PATH"),
62
"org.freedesktop.DisplayManager.Seat",
65
G_VARIANT_TYPE ("()"),
66
G_DBUS_CALL_FLAGS_NONE,
73
r = g_strdup_printf ("%s LOCK-SESSION", session_id);
74
if (strcmp (request, r) == 0)
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",
83
G_VARIANT_TYPE ("()"),
84
G_DBUS_CALL_FLAGS_NONE,
91
r = g_strdup_printf ("%s LIST-GROUPS", session_id);
92
if (strcmp (request, r) == 0)
98
n_groups = getgroups (0, NULL);
99
groups = malloc (sizeof (gid_t) * n_groups);
100
n_groups = getgroups (n_groups, groups);
101
group_list = g_string_new ("");
102
for (i = 0; i < n_groups; i++)
107
g_string_append (group_list, ",");
108
group = getgrgid (groups[i]);
110
g_string_append (group_list, group->gr_name);
112
g_string_append_printf (group_list, "%d", groups[i]);
114
status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
115
g_string_free (group_list, TRUE);
119
r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
120
if (g_str_has_prefix (request, r))
122
const gchar *name = request + strlen (r);
123
const gchar *value = g_getenv (name);
124
status_notify ("%s READ-ENV NAME=%s VALUE=%s", session_id, name, value ? value : "");
128
r = g_strdup_printf ("%s WRITE-STDOUT TEXT=", session_id);
129
if (g_str_has_prefix (request, r))
130
g_print ("%s", request + strlen (r));
133
r = g_strdup_printf ("%s WRITE-STDERR TEXT=", session_id);
134
if (g_str_has_prefix (request, r))
135
g_printerr ("%s", request + strlen (r));
138
r = g_strdup_printf ("%s READ FILE=", session_id);
139
if (g_str_has_prefix (request, r))
141
const gchar *name = request + strlen (r);
143
GError *error = NULL;
145
if (g_file_get_contents (name, &contents, NULL, &error))
146
status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
148
status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
149
g_clear_error (&error);
153
r = g_strdup_printf ("%s LIST-UNKNOWN-FILE-DESCRIPTORS", session_id);
154
if (strcmp (request, r) == 0)
155
status_notify ("%s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", session_id, open_fds->str);
158
r = g_strdup_printf ("%s CHECK-X-AUTHORITY", session_id);
159
if (strcmp (request, r) == 0)
163
GString *mode_string;
165
xauthority = g_strdup (g_getenv ("XAUTHORITY"));
167
xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL);
169
g_stat (xauthority, &file_info);
172
mode_string = g_string_new ("");
173
g_string_append_c (mode_string, file_info.st_mode & S_IRUSR ? 'r' : '-');
174
g_string_append_c (mode_string, file_info.st_mode & S_IWUSR ? 'w' : '-');
175
g_string_append_c (mode_string, file_info.st_mode & S_IXUSR ? 'x' : '-');
176
g_string_append_c (mode_string, file_info.st_mode & S_IRGRP ? 'r' : '-');
177
g_string_append_c (mode_string, file_info.st_mode & S_IWGRP ? 'w' : '-');
178
g_string_append_c (mode_string, file_info.st_mode & S_IXGRP ? 'x' : '-');
179
g_string_append_c (mode_string, file_info.st_mode & S_IROTH ? 'r' : '-');
180
g_string_append_c (mode_string, file_info.st_mode & S_IWOTH ? 'w' : '-');
181
g_string_append_c (mode_string, file_info.st_mode & S_IXOTH ? 'x' : '-');
182
status_notify ("%s CHECK-X-AUTHORITY MODE=%s", session_id, mode_string->str);
183
g_string_free (mode_string, TRUE);
189
main (int argc, char **argv)
191
gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_session_cookie;
192
GString *status_text;
195
display = getenv ("DISPLAY");
196
xdg_seat = getenv ("XDG_SEAT");
197
xdg_vtnr = getenv ("XDG_VTNR");
198
xdg_current_desktop = getenv ("XDG_CURRENT_DESKTOP");
199
xdg_session_cookie = getenv ("XDG_SESSION_COOKIE");
202
if (display[0] == ':')
203
session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
205
session_id = g_strdup_printf ("SESSION-X-%s", display);
208
session_id = g_strdup ("SESSION-?");
210
open_fds = g_string_new ("");
211
open_max = sysconf (_SC_OPEN_MAX);
212
for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
214
if (fcntl (fd, F_GETFD) >= 0)
215
g_string_append_printf (open_fds, "%d,", fd);
217
if (g_str_has_suffix (open_fds->str, ","))
218
open_fds->str[strlen (open_fds->str) - 1] = '\0';
220
signal (SIGINT, quit_cb);
221
signal (SIGTERM, quit_cb);
223
#if !defined(GLIB_VERSION_2_36)
227
loop = g_main_loop_new (NULL, FALSE);
229
status_connect (request_cb);
231
status_text = g_string_new ("");
232
g_string_printf (status_text, "%s START", session_id);
234
g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
236
g_string_append_printf (status_text, " XDG_VTNR=%s", xdg_vtnr);
237
if (xdg_current_desktop)
238
g_string_append_printf (status_text, " XDG_CURRENT_DESKTOP=%s", xdg_current_desktop);
239
if (xdg_session_cookie)
240
g_string_append_printf (status_text, " XDG_SESSION_COOKIE=%s", xdg_session_cookie);
242
g_string_append_printf (status_text, " NAME=%s", argv[1]);
243
g_string_append_printf (status_text, " USER=%s", getenv ("USER"));
244
status_notify (status_text->str);
245
g_string_free (status_text, TRUE);
247
config = g_key_file_new ();
248
g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
252
connection = xcb_connect (NULL, NULL);
253
if (xcb_connection_has_error (connection))
255
status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
258
status_notify ("%s CONNECT-XSERVER", session_id);
261
g_main_loop_run (loop);