11 #include <glib-object.h>
16 static GString *open_fds;
18 static GKeyFile *config;
20 static xcb_connection_t *connection;
25 status_notify ("SESSION %s TERMINATE SIGNAL=%d", getenv ("DISPLAY"), signum);
30 request_cb (const gchar *request)
34 r = g_strdup_printf ("SESSION %s LOGOUT", getenv ("DISPLAY"));
35 if (strcmp (request, r) == 0)
39 r = g_strdup_printf ("SESSION %s CRASH", getenv ("DISPLAY"));
40 if (strcmp (request, r) == 0)
41 kill (getpid (), SIGSEGV);
44 r = g_strdup_printf ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
45 if (strcmp (request, r) == 0)
47 status_notify ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
48 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
49 "org.freedesktop.DisplayManager",
50 getenv ("XDG_SEAT_PATH"),
51 "org.freedesktop.DisplayManager.Seat",
54 G_VARIANT_TYPE ("()"),
55 G_DBUS_CALL_FLAGS_NONE,
62 r = g_strdup_printf ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
63 if (strcmp (request, r) == 0)
65 status_notify ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
66 g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
67 "org.freedesktop.DisplayManager",
68 getenv ("XDG_SESSION_PATH"),
69 "org.freedesktop.DisplayManager.Session",
72 G_VARIANT_TYPE ("()"),
73 G_DBUS_CALL_FLAGS_NONE,
80 r = g_strdup_printf ("SESSION %s LIST-GROUPS", getenv ("DISPLAY"));
81 if (strcmp (request, r) == 0)
87 n_groups = getgroups (0, NULL);
88 groups = malloc (sizeof (gid_t) * n_groups);
89 n_groups = getgroups (n_groups, groups);
90 group_list = g_string_new ("");
91 for (i = 0; i < n_groups; i++)
96 g_string_append (group_list, ",");
97 group = getgrgid (groups[i]);
99 g_string_append (group_list, group->gr_name);
101 g_string_append_printf (group_list, "%d", groups[i]);
103 status_notify ("SESSION %s LIST-GROUPS GROUPS=%s", getenv ("DISPLAY"), group_list->str);
104 g_string_free (group_list, TRUE);
108 r = g_strdup_printf ("SESSION %s READ-ENV NAME=", getenv ("DISPLAY"));
109 if (g_str_has_prefix (request, r))
111 const gchar *name = request + strlen (r);
112 const gchar *value = g_getenv (name);
113 status_notify ("SESSION %s READ-ENV NAME=%s VALUE=%s", getenv ("DISPLAY"), name, value ? value : "");
117 r = g_strdup_printf ("SESSION %s WRITE-STDOUT TEXT=", getenv ("DISPLAY"));
118 if (g_str_has_prefix (request, r))
119 g_print ("%s", request + strlen (r));
122 r = g_strdup_printf ("SESSION %s WRITE-STDERR TEXT=", getenv ("DISPLAY"));
123 if (g_str_has_prefix (request, r))
124 g_printerr ("%s", request + strlen (r));
127 r = g_strdup_printf ("SESSION %s READ FILE=", getenv ("DISPLAY"));
128 if (g_str_has_prefix (request, r))
130 const gchar *name = request + strlen (r);
132 GError *error = NULL;
134 if (g_file_get_contents (name, &contents, NULL, &error))
135 status_notify ("SESSION %s READ FILE=%s TEXT=%s", getenv ("DISPLAY"), name, contents);
137 status_notify ("SESSION %s READ FILE=%s ERROR=%s", getenv ("DISPLAY"), name, error->message);
138 g_clear_error (&error);
142 r = g_strdup_printf ("SESSION %s LIST-UNKNOWN-FILE-DESCRIPTORS", getenv ("DISPLAY"));
143 if (strcmp (request, r) == 0)
144 status_notify ("SESSION %s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", getenv ("DISPLAY"), open_fds->str);
149 main (int argc, char **argv)
154 open_fds = g_string_new ("");
155 open_max = sysconf (_SC_OPEN_MAX);
156 for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
158 if (fcntl (fd, F_GETFD) >= 0)
159 g_string_append_printf (open_fds, "%d,", fd);
161 if (g_str_has_suffix (open_fds->str, ","))
162 open_fds->str[strlen (open_fds->str) - 1] = '\0';
164 signal (SIGINT, quit_cb);
165 signal (SIGTERM, quit_cb);
169 loop = g_main_loop_new (NULL, FALSE);
171 status_connect (request_cb);
174 status_notify ("SESSION %s START NAME=%s USER=%s", getenv ("DISPLAY"), argv[1], getenv ("USER"));
176 status_notify ("SESSION %s START USER=%s", getenv ("DISPLAY"), getenv ("USER"));
178 config = g_key_file_new ();
179 g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
181 connection = xcb_connect (NULL, NULL);
183 if (xcb_connection_has_error (connection))
185 status_notify ("SESSION %s CONNECT-XSERVER-ERROR", getenv ("DISPLAY"));
189 status_notify ("SESSION %s CONNECT-XSERVER", getenv ("DISPLAY"));
191 g_main_loop_run (loop);