~lightdm-team/lightdm/1.4

« back to all changes in this revision

Viewing changes to tests/src/test-session.c

  • Committer: Robert Ancell
  • Date: 2014-03-13 02:15:38 UTC
  • Revision ID: robert.ancell@canonical.com-20140313021538-u2mxfxrrfw5u58ic
Tags: 1.4.7
Releasing 1.4.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <glib/gstdio.h>
 
14
 
 
15
#include "status.h"
 
16
 
 
17
static gchar *session_id;
 
18
 
 
19
static GMainLoop *loop;
 
20
 
 
21
static GString *open_fds;
 
22
 
 
23
static GKeyFile *config;
 
24
 
 
25
static xcb_connection_t *connection;
 
26
 
 
27
static void
 
28
quit_cb (int signum)
 
29
{
 
30
    status_notify ("%s TERMINATE SIGNAL=%d", session_id, signum);
 
31
    exit (EXIT_SUCCESS);
 
32
}
 
33
 
 
34
static void
 
35
request_cb (const gchar *request)
 
36
{
 
37
    gchar *r;
 
38
 
 
39
    if (!request)
 
40
    {
 
41
        g_main_loop_quit (loop);
 
42
        return;
 
43
    }
 
44
  
 
45
    r = g_strdup_printf ("%s LOGOUT", session_id);
 
46
    if (strcmp (request, r) == 0)
 
47
        exit (EXIT_SUCCESS);
 
48
    g_free (r);
 
49
  
 
50
    r = g_strdup_printf ("%s CRASH", session_id);
 
51
    if (strcmp (request, r) == 0)
 
52
        kill (getpid (), SIGSEGV);
 
53
    g_free (r);
 
54
 
 
55
    r = g_strdup_printf ("%s LOCK-SEAT", session_id);
 
56
    if (strcmp (request, r) == 0)
 
57
    {
 
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",
 
63
                                     "Lock",
 
64
                                     g_variant_new ("()"),
 
65
                                     G_VARIANT_TYPE ("()"),
 
66
                                     G_DBUS_CALL_FLAGS_NONE,
 
67
                                     1000,
 
68
                                     NULL,
 
69
                                     NULL);
 
70
    }
 
71
    g_free (r);
 
72
 
 
73
    r = g_strdup_printf ("%s LOCK-SESSION", session_id);
 
74
    if (strcmp (request, r) == 0)
 
75
    {
 
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",
 
81
                                     "Lock",
 
82
                                     g_variant_new ("()"),
 
83
                                     G_VARIANT_TYPE ("()"),
 
84
                                     G_DBUS_CALL_FLAGS_NONE,
 
85
                                     1000,
 
86
                                     NULL,
 
87
                                     NULL);
 
88
    }
 
89
    g_free (r);
 
90
 
 
91
    r = g_strdup_printf ("%s LIST-GROUPS", session_id);
 
92
    if (strcmp (request, r) == 0)
 
93
    {
 
94
        int n_groups, i;
 
95
        gid_t *groups;
 
96
        GString *group_list;
 
97
 
 
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++)
 
103
        {
 
104
            struct group *group;
 
105
 
 
106
            if (i != 0)
 
107
                g_string_append (group_list, ",");
 
108
            group = getgrgid (groups[i]);
 
109
            if (group)
 
110
                g_string_append (group_list, group->gr_name);
 
111
            else
 
112
                g_string_append_printf (group_list, "%d", groups[i]);
 
113
        }
 
114
        status_notify ("%s LIST-GROUPS GROUPS=%s", session_id, group_list->str);
 
115
        g_string_free (group_list, TRUE);
 
116
        free (groups);
 
117
    }
 
118
 
 
119
    r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
 
120
    if (g_str_has_prefix (request, r))
 
121
    {
 
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 : "");
 
125
    }
 
126
    g_free (r);
 
127
 
 
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));
 
131
    g_free (r);
 
132
 
 
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));
 
136
    g_free (r);
 
137
 
 
138
    r = g_strdup_printf ("%s READ FILE=", session_id);
 
139
    if (g_str_has_prefix (request, r))
 
140
    {
 
141
        const gchar *name = request + strlen (r);
 
142
        gchar *contents;
 
143
        GError *error = NULL;
 
144
 
 
145
        if (g_file_get_contents (name, &contents, NULL, &error))
 
146
            status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
 
147
        else
 
148
            status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
 
149
        g_clear_error (&error);
 
150
    }
 
151
    g_free (r);
 
152
 
 
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);
 
156
    g_free (r);
 
157
 
 
158
    r = g_strdup_printf ("%s CHECK-X-AUTHORITY", session_id);
 
159
    if (strcmp (request, r) == 0)
 
160
    {
 
161
        gchar *xauthority;
 
162
        GStatBuf file_info;
 
163
        GString *mode_string;
 
164
 
 
165
        xauthority = g_strdup (g_getenv ("XAUTHORITY"));
 
166
        if (!xauthority)
 
167
            xauthority = g_build_filename (g_get_home_dir (), ".Xauthority", NULL);
 
168
 
 
169
        g_stat (xauthority, &file_info);
 
170
        g_free (xauthority);
 
171
 
 
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);
 
184
    }
 
185
    g_free (r);
 
186
}
 
187
 
 
188
int
 
189
main (int argc, char **argv)
 
190
{
 
191
    gchar *display, *xdg_seat, *xdg_vtnr, *xdg_current_desktop, *xdg_session_cookie;
 
192
    GString *status_text;
 
193
    int fd, open_max;
 
194
 
 
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");
 
200
    if (display)
 
201
    {
 
202
        if (display[0] == ':')
 
203
            session_id = g_strdup_printf ("SESSION-X-%s", display + 1);
 
204
        else
 
205
            session_id = g_strdup_printf ("SESSION-X-%s", display);
 
206
    }
 
207
    else
 
208
        session_id = g_strdup ("SESSION-?");
 
209
 
 
210
    open_fds = g_string_new ("");
 
211
    open_max = sysconf (_SC_OPEN_MAX);
 
212
    for (fd = STDERR_FILENO + 1; fd < open_max; fd++)
 
213
    {
 
214
        if (fcntl (fd, F_GETFD) >= 0)
 
215
            g_string_append_printf (open_fds, "%d,", fd);
 
216
    }
 
217
    if (g_str_has_suffix (open_fds->str, ","))
 
218
        open_fds->str[strlen (open_fds->str) - 1] = '\0';
 
219
 
 
220
    signal (SIGINT, quit_cb);
 
221
    signal (SIGTERM, quit_cb);
 
222
 
 
223
#if !defined(GLIB_VERSION_2_36)
 
224
    g_type_init ();
 
225
#endif
 
226
 
 
227
    loop = g_main_loop_new (NULL, FALSE);
 
228
 
 
229
    status_connect (request_cb);
 
230
 
 
231
    status_text = g_string_new ("");
 
232
    g_string_printf (status_text, "%s START", session_id);
 
233
    if (xdg_seat)
 
234
        g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
 
235
    if (xdg_vtnr)
 
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);
 
241
    if (argc > 1)
 
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);
 
246
 
 
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);
 
249
 
 
250
    if (display)
 
251
    {
 
252
        connection = xcb_connect (NULL, NULL);
 
253
        if (xcb_connection_has_error (connection))
 
254
        {
 
255
            status_notify ("%s CONNECT-XSERVER-ERROR", session_id);
 
256
            return EXIT_FAILURE;
 
257
        }
 
258
        status_notify ("%s CONNECT-XSERVER", session_id);
 
259
    }
 
260
 
 
261
    g_main_loop_run (loop);
 
262
 
 
263
    return EXIT_SUCCESS;
 
264
}