]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/test-session.c
Add greeter-show-remote-login hint
[sojka/lightdm.git] / tests / src / test-session.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <grp.h>
9 #include <xcb/xcb.h>
10 #include <glib.h>
11 #include <glib-object.h>
12 #include <gio/gio.h>
13
14 #include "status.h"
15
16 static GString *open_fds;
17
18 static GKeyFile *config;
19
20 static xcb_connection_t *connection;
21
22 static void
23 quit_cb (int signum)
24 {
25     status_notify ("SESSION %s TERMINATE SIGNAL=%d", getenv ("DISPLAY"), signum);
26     exit (EXIT_SUCCESS);
27 }
28
29 static void
30 request_cb (const gchar *request)
31 {
32     gchar *r;
33   
34     r = g_strdup_printf ("SESSION %s LOGOUT", getenv ("DISPLAY"));
35     if (strcmp (request, r) == 0)
36         exit (EXIT_SUCCESS);
37     g_free (r);
38   
39     r = g_strdup_printf ("SESSION %s CRASH", getenv ("DISPLAY"));
40     if (strcmp (request, r) == 0)
41         kill (getpid (), SIGSEGV);
42     g_free (r);
43
44     r = g_strdup_printf ("SESSION %s LOCK-SEAT", getenv ("DISPLAY"));
45     if (strcmp (request, r) == 0)
46     {
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",
52                                      "Lock",
53                                      g_variant_new ("()"),
54                                      G_VARIANT_TYPE ("()"),
55                                      G_DBUS_CALL_FLAGS_NONE,
56                                      1000,
57                                      NULL,
58                                      NULL);
59     }
60     g_free (r);
61
62     r = g_strdup_printf ("SESSION %s LOCK-SESSION", getenv ("DISPLAY"));
63     if (strcmp (request, r) == 0)
64     {
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",
70                                      "Lock",
71                                      g_variant_new ("()"),
72                                      G_VARIANT_TYPE ("()"),
73                                      G_DBUS_CALL_FLAGS_NONE,
74                                      1000,
75                                      NULL,
76                                      NULL);
77     }
78     g_free (r);
79
80     r = g_strdup_printf ("SESSION %s LIST-GROUPS", getenv ("DISPLAY"));
81     if (strcmp (request, r) == 0)
82     {
83         int n_groups, i;
84         gid_t *groups;
85         GString *group_list;
86
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++)
92         {
93             struct group *group;
94
95             if (i != 0)
96                 g_string_append (group_list, ",");
97             group = getgrgid (groups[i]);
98             if (group)
99                 g_string_append (group_list, group->gr_name);
100             else
101                 g_string_append_printf (group_list, "%d", groups[i]);
102         }
103         status_notify ("SESSION %s LIST-GROUPS GROUPS=%s", getenv ("DISPLAY"), group_list->str);
104         g_string_free (group_list, TRUE);
105         free (groups);
106     }
107
108     r = g_strdup_printf ("SESSION %s READ-ENV NAME=", getenv ("DISPLAY"));
109     if (g_str_has_prefix (request, r))
110     {
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 : "");
114     }
115     g_free (r);
116
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));
120     g_free (r);
121
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));
125     g_free (r);
126
127     r = g_strdup_printf ("SESSION %s READ FILE=", getenv ("DISPLAY"));
128     if (g_str_has_prefix (request, r))
129     {
130         const gchar *name = request + strlen (r);
131         gchar *contents;
132         GError *error = NULL;
133
134         if (g_file_get_contents (name, &contents, NULL, &error))
135             status_notify ("SESSION %s READ FILE=%s TEXT=%s", getenv ("DISPLAY"), name, contents);
136         else
137             status_notify ("SESSION %s READ FILE=%s ERROR=%s", getenv ("DISPLAY"), name, error->message);
138         g_clear_error (&error);
139     }
140     g_free (r);
141
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);
145     g_free (r);
146 }
147
148 int
149 main (int argc, char **argv)
150 {
151     GMainLoop *loop;
152     int fd, open_max;
153
154     open_fds = g_string_new ("");
155     open_max = sysconf (_SC_OPEN_MAX);
156     for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
157     {
158         if (fcntl (fd, F_GETFD) >= 0)
159             g_string_append_printf (open_fds, "%d,", fd);
160     }
161     if (g_str_has_suffix (open_fds->str, ","))
162         open_fds->str[strlen (open_fds->str) - 1] = '\0';
163
164     signal (SIGINT, quit_cb);
165     signal (SIGTERM, quit_cb);
166
167     g_type_init ();
168
169     loop = g_main_loop_new (NULL, FALSE);
170
171     status_connect (request_cb);
172
173     if (argc > 1)
174         status_notify ("SESSION %s START NAME=%s USER=%s", getenv ("DISPLAY"), argv[1], getenv ("USER"));
175     else
176         status_notify ("SESSION %s START USER=%s", getenv ("DISPLAY"), getenv ("USER"));
177
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);
180
181     connection = xcb_connect (NULL, NULL);
182
183     if (xcb_connection_has_error (connection))
184     {
185         status_notify ("SESSION %s CONNECT-XSERVER-ERROR", getenv ("DISPLAY"));
186         return EXIT_FAILURE;
187     }
188
189     status_notify ("SESSION %s CONNECT-XSERVER", getenv ("DISPLAY"));
190
191     g_main_loop_run (loop);    
192
193     return EXIT_SUCCESS;
194 }