~lightdm-team/lightdm/1.2

« back to all changes in this revision

Viewing changes to tests/src/X.c

  • Committer: Robert Ancell
  • Date: 2015-11-18 00:23:53 UTC
  • Revision ID: robert.ancell@canonical.com-20151118002353-lyaui7suaci5b298
BackportĀ testĀ improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include <string.h>
4
4
#include <sys/types.h>
5
5
#include <unistd.h>
6
 
#include <signal.h>
7
6
#include <sys/stat.h>
8
7
#include <fcntl.h>
9
8
#include <errno.h>
 
9
#include <glib-unix.h>
10
10
 
11
11
#include "status.h"
12
12
#include "x-server.h"
24
24
/* Path to authority database to use */
25
25
static gchar *auth_path = NULL;
26
26
 
 
27
/* ID to use for test reporting */
 
28
static gchar *id;
 
29
 
27
30
/* Display number being served */
28
31
static int display_number = 0;
29
32
 
36
39
/* XDMCP client */
37
40
static XDMCPClient *xdmcp_client = NULL;
38
41
 
 
42
/* Session ID provided by XDMCP server */
 
43
static guint32 xdmcp_session_id = 0;
 
44
 
39
45
/* Authorization provided by XDMCP server */
40
46
static guint16 xdmcp_cookie_length = 0;
41
47
static guint8 *xdmcp_cookie = NULL;
58
64
    g_main_loop_quit (loop);
59
65
}
60
66
 
61
 
 
62
 
static void
63
 
signal_cb (int signum)
64
 
{
65
 
    if (signum == SIGHUP)
66
 
    {
67
 
        status_notify ("XSERVER-%d DISCONNECT-CLIENTS", display_number);
68
 
    }
69
 
    else
70
 
    {
71
 
        status_notify ("XSERVER-%d TERMINATE SIGNAL=%d", display_number, signum);
72
 
        quit (EXIT_SUCCESS);
73
 
    }
 
67
static gboolean
 
68
sighup_cb (gpointer user_data)
 
69
{
 
70
    status_notify ("%s DISCONNECT-CLIENTS", id);
 
71
    return TRUE;
 
72
}
 
73
 
 
74
static gboolean
 
75
sigint_cb (gpointer user_data)
 
76
{
 
77
    status_notify ("%s TERMINATE SIGNAL=%d", id, SIGINT);
 
78
    quit (EXIT_SUCCESS);
 
79
    return TRUE;
 
80
}
 
81
 
 
82
static gboolean
 
83
sigterm_cb (gpointer user_data)
 
84
{
 
85
    status_notify ("%s TERMINATE SIGNAL=%d", id, SIGTERM);
 
86
    quit (EXIT_SUCCESS);
 
87
    return TRUE;
74
88
}
75
89
 
76
90
static void
80
94
 
81
95
    if (!notified_query)
82
96
    {
83
 
        status_notify ("XSERVER-%d SEND-QUERY", display_number);
 
97
        status_notify ("%s SEND-QUERY", id);
84
98
        notified_query = TRUE;
85
99
    }
86
100
}
88
102
static void
89
103
xdmcp_willing_cb (XDMCPClient *client, XDMCPWilling *message)
90
104
{
91
 
    gchar **authorization_names;
92
 
    GInetAddress *addresses[2];
93
 
 
94
 
    status_notify ("XSERVER-%d GOT-WILLING AUTHENTICATION-NAME=\"%s\" HOSTNAME=\"%s\" STATUS=\"%s\"", display_number, message->authentication_name, message->hostname, message->status);
95
 
 
96
 
    status_notify ("XSERVER-%d SEND-REQUEST DISPLAY-NUMBER=%d AUTHORIZATION-NAME=\"%s\" MFID=\"%s\"", display_number, display_number, "MIT-MAGIC-COOKIE-1", "TEST XSERVER");
97
 
 
98
 
    authorization_names = g_strsplit ("MIT-MAGIC-COOKIE-1", " ", -1);
99
 
    addresses[0] = xdmcp_client_get_local_address (client);
100
 
    addresses[1] = NULL;
101
 
    xdmcp_client_send_request (client, display_number,
102
 
                               addresses,
103
 
                               "", NULL, 0,
104
 
                               authorization_names, "TEST XSERVER");
105
 
    g_strfreev (authorization_names);
 
105
    status_notify ("%s GOT-WILLING AUTHENTICATION-NAME=\"%s\" HOSTNAME=\"%s\" STATUS=\"%s\"", id, message->authentication_name, message->hostname, message->status);
106
106
}
107
107
 
108
108
static void
109
109
xdmcp_accept_cb (XDMCPClient *client, XDMCPAccept *message)
110
110
{
111
 
    status_notify ("XSERVER-%d GOT-ACCEPT SESSION-ID=%d AUTHENTICATION-NAME=\"%s\" AUTHORIZATION-NAME=\"%s\"", display_number, message->session_id, message->authentication_name, message->authorization_name);
 
111
    status_notify ("%s GOT-ACCEPT SESSION-ID=%d AUTHENTICATION-NAME=\"%s\" AUTHORIZATION-NAME=\"%s\"", id, message->session_id, message->authentication_name, message->authorization_name);
112
112
 
113
 
    /* Ignore if haven't picked a valid authorization */
114
 
    if (strcmp (message->authorization_name, "MIT-MAGIC-COOKIE-1") != 0)
115
 
        return;
 
113
    xdmcp_session_id = message->session_id;
116
114
 
117
115
    g_free (xdmcp_cookie);
118
116
    xdmcp_cookie_length = message->authorization_data_length;
119
117
    xdmcp_cookie = g_malloc (message->authorization_data_length);
120
118
    memcpy (xdmcp_cookie, message->authorization_data, message->authorization_data_length);
121
 
 
122
 
    status_notify ("XSERVER-%d SEND-MANAGE SESSION-ID=%d DISPLAY-NUMBER=%d DISPLAY-CLASS=\"%s\"", display_number, message->session_id, display_number, "DISPLAY CLASS");
123
 
    xdmcp_client_send_manage (client, message->session_id, display_number, "DISPLAY CLASS");
124
119
}
125
120
 
126
121
static void
127
122
xdmcp_decline_cb (XDMCPClient *client, XDMCPDecline *message)
128
123
{
129
 
    status_notify ("XSERVER-%d GOT-DECLINE STATUS=\"%s\" AUTHENTICATION-NAME=\"%s\"", display_number, message->status, message->authentication_name);  
 
124
    status_notify ("%s GOT-DECLINE STATUS=\"%s\" AUTHENTICATION-NAME=\"%s\"", id, message->status, message->authentication_name);  
130
125
}
131
126
 
132
127
static void
133
128
xdmcp_failed_cb (XDMCPClient *client, XDMCPFailed *message)
134
129
{
135
 
    status_notify ("XSERVER-%d GOT-FAILED SESSION-ID=%d STATUS=\"%s\"", display_number, message->session_id, message->status);
 
130
    status_notify ("%s GOT-FAILED SESSION-ID=%d STATUS=\"%s\"", id, message->session_id, message->status);
136
131
}
137
132
 
138
133
static void
139
134
client_connected_cb (XServer *server, XClient *client)
140
135
{
141
 
    gchar *auth_error = NULL;
142
 
 
143
 
    status_notify ("XSERVER-%d ACCEPT-CONNECT", display_number);
144
 
 
145
 
    if (auth_error)
146
 
        x_client_send_failed (client, auth_error);
147
 
    else
148
 
        x_client_send_success (client);
149
 
    g_free (auth_error);
 
136
    status_notify ("%s ACCEPT-CONNECT", id);
 
137
    x_client_send_success (client);
150
138
}
151
139
 
152
140
static void
156
144
}
157
145
 
158
146
static void
159
 
request_cb (const gchar *request)
 
147
request_cb (const gchar *name, GHashTable *params)
160
148
{
161
 
    gchar *r;
162
 
  
163
 
    if (!request)
 
149
    if (!name)
164
150
    {
165
151
        g_main_loop_quit (loop);
166
152
        return;
167
153
    }
168
154
 
169
 
    r = g_strdup_printf ("XSERVER-%d CRASH", display_number);
170
 
    if (strcmp (request, r) == 0)
 
155
    if (strcmp (name, "CRASH") == 0)
171
156
    {
172
157
        cleanup ();
173
158
        kill (getpid (), SIGSEGV);
174
159
    }
175
 
    g_free (r);
176
 
    r = g_strdup_printf ("XSERVER-%d INDICATE-READY", display_number);
177
 
    if (strcmp (request, r) == 0)
 
160
 
 
161
    else if (strcmp (name, "INDICATE-READY") == 0)
178
162
    {
179
163
        void *handler;
180
164
 
181
165
        handler = signal (SIGUSR1, SIG_IGN);
182
166
        if (handler == SIG_IGN)
183
167
        {
184
 
            status_notify ("XSERVER-%d INDICATE-READY", display_number);
 
168
            status_notify ("%s INDICATE-READY", id);
185
169
            kill (getppid (), SIGUSR1);
186
170
        }
187
171
        signal (SIGUSR1, handler);
188
172
    }
189
 
    g_free (r);
190
 
    r = g_strdup_printf ("XSERVER-%d START-XDMCP", display_number);
191
 
    if (strcmp (request, r) == 0)
 
173
 
 
174
    else if (strcmp (name, "START-XDMCP") == 0)
192
175
    {
193
176
        if (!xdmcp_client_start (xdmcp_client))
194
177
            quit (EXIT_FAILURE);
195
178
    }
196
 
    g_free (r);
 
179
 
 
180
    else if (strcmp (name, "SEND-REQUEST") == 0)
 
181
    {
 
182
        const gchar *addresses_list, *authorization_names_list, *mfid;
 
183
        gchar **list, **authorization_names;
 
184
        gsize list_length;
 
185
        gint i;
 
186
        GInetAddress **addresses;
 
187
 
 
188
        addresses_list = g_hash_table_lookup (params, "ADDRESSES");
 
189
        if (!addresses_list)
 
190
            addresses_list = "";
 
191
        authorization_names_list = g_hash_table_lookup (params, "AUTHORIZATION-NAMES");
 
192
        if (!authorization_names_list)
 
193
            authorization_names_list = "";
 
194
        mfid = g_hash_table_lookup (params, "MFID");
 
195
        if (!mfid)
 
196
            mfid = "";
 
197
 
 
198
        list = g_strsplit (addresses_list, " ", -1);
 
199
        list_length = g_strv_length (list);
 
200
        addresses = g_malloc (sizeof (GInetAddress *) * (list_length + 1));
 
201
        for (i = 0; i < list_length; i++)
 
202
            addresses[i] = g_inet_address_new_from_string (list[i]);
 
203
        addresses[i] = NULL;
 
204
        g_strfreev (list);
 
205
 
 
206
        authorization_names = g_strsplit (authorization_names_list, " ", -1);
 
207
 
 
208
        xdmcp_client_send_request (xdmcp_client, display_number,
 
209
                                   addresses,
 
210
                                   "", NULL, 0,
 
211
                                   authorization_names, mfid);
 
212
        g_strfreev (authorization_names);
 
213
    }
 
214
 
 
215
    else if (strcmp (name, "SEND-MANAGE") == 0)
 
216
    {
 
217
        xdmcp_client_send_manage (xdmcp_client, xdmcp_session_id, display_number, "DISPLAY CLASS");
 
218
    }
197
219
}
198
220
 
199
221
int
208
230
    int lock_file;
209
231
    GString *status_text;
210
232
 
211
 
    signal (SIGINT, signal_cb);
212
 
    signal (SIGTERM, signal_cb);
213
 
    signal (SIGHUP, signal_cb);
214
 
 
215
233
#if !defined(GLIB_VERSION_2_36)
216
234
    g_type_init ();
217
235
#endif
218
236
 
219
237
    loop = g_main_loop_new (NULL, FALSE);
220
238
 
221
 
    status_connect (request_cb);
 
239
    g_unix_signal_add (SIGINT, sigint_cb, NULL);
 
240
    g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
 
241
    g_unix_signal_add (SIGHUP, sighup_cb, NULL);
222
242
 
223
243
    for (i = 1; i < argc; i++)
224
244
    {
290
310
        }
291
311
    }
292
312
 
 
313
    id = g_strdup_printf ("XSERVER-%d", display_number);
 
314
 
 
315
    status_connect (request_cb, id);
 
316
 
293
317
    xserver = x_server_new (display_number);
294
318
    g_signal_connect (xserver, "client-connected", G_CALLBACK (client_connected_cb), NULL);
295
319
    g_signal_connect (xserver, "client-disconnected", G_CALLBACK (client_disconnected_cb), NULL);
296
320
 
297
321
    status_text = g_string_new ("");
298
 
    g_string_printf (status_text, "XSERVER-%d START", display_number);
 
322
    g_string_printf (status_text, "%s START", id);
299
323
    if (vt_number >= 0)
300
324
        g_string_append_printf (status_text, " VT=%d", vt_number);
301
 
    status_notify (status_text->str);
 
325
    status_notify ("%s", status_text->str);
302
326
    g_string_free (status_text, TRUE);
303
327
 
304
328
    config = g_key_file_new ();
307
331
    if (g_key_file_has_key (config, "test-xserver-config", "return-value", NULL))
308
332
    {
309
333
        int return_value = g_key_file_get_integer (config, "test-xserver-config", "return-value", NULL);
310
 
        status_notify ("XSERVER-%d EXIT CODE=%d", display_number, return_value);
 
334
        status_notify ("%s EXIT CODE=%d", id, return_value);
311
335
        return return_value;
312
336
    }
313
337
 
362
386
                 "      and start again.\n", display_number, lock_path);
363
387
        g_free (lock_path);
364
388
        lock_path = NULL;
365
 
        quit (EXIT_FAILURE);
 
389
        return EXIT_FAILURE;
366
390
    }
367
391
    pid_string = g_strdup_printf ("%10ld", (long) getpid ());
368
392
    if (write (lock_file, pid_string, strlen (pid_string)) < 0)
369
393
    {
370
394
        g_warning ("Error writing PID file: %s", strerror (errno));
371
 
        quit (EXIT_FAILURE);
 
395
        return EXIT_FAILURE;
372
396
    }
373
397
    g_free (pid_string);
374
398
 
375
399
    if (!x_server_start (xserver))
376
 
        quit (EXIT_FAILURE);
 
400
        return EXIT_FAILURE;
377
401
 
378
402
    /* Enable XDMCP */
379
403
    if (do_xdmcp)