~lightdm-team/lightdm/1.2

« back to all changes in this revision

Viewing changes to tests/src/test-session.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:
1
1
#include <stdlib.h>
2
2
#include <stdio.h>
3
3
#include <string.h>
4
 
#include <signal.h>
5
4
#include <sys/types.h>
6
5
#include <unistd.h>
7
6
#include <fcntl.h>
10
9
#include <glib.h>
11
10
#include <glib-object.h>
12
11
#include <gio/gio.h>
 
12
#include <glib-unix.h>
13
13
#include <glib/gstdio.h>
14
14
 
15
15
#include "status.h"
24
24
 
25
25
static xcb_connection_t *connection;
26
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)
 
27
static gboolean
 
28
sigint_cb (gpointer user_data)
 
29
{
 
30
    status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGINT);
 
31
    g_main_loop_quit (loop);
 
32
    return TRUE;
 
33
}
 
34
 
 
35
static gboolean
 
36
sigterm_cb (gpointer user_data)
 
37
{
 
38
    status_notify ("%s TERMINATE SIGNAL=%d", session_id, SIGTERM);
 
39
    g_main_loop_quit (loop);
 
40
    return TRUE;
 
41
}
 
42
 
 
43
static void
 
44
request_cb (const gchar *name, GHashTable *params)
 
45
{
 
46
    if (!name)
40
47
    {
41
48
        g_main_loop_quit (loop);
42
49
        return;
43
50
    }
44
51
  
45
 
    r = g_strdup_printf ("%s LOGOUT", session_id);
46
 
    if (strcmp (request, r) == 0)
 
52
    if (strcmp (name, "LOGOUT") == 0)
47
53
        exit (EXIT_SUCCESS);
48
 
    g_free (r);
49
 
  
50
 
    r = g_strdup_printf ("%s CRASH", session_id);
51
 
    if (strcmp (request, r) == 0)
 
54
 
 
55
    else if (strcmp (name, "CRASH") == 0)
52
56
        kill (getpid (), SIGSEGV);
53
 
    g_free (r);
54
57
 
55
 
    r = g_strdup_printf ("%s LOCK-SEAT", session_id);
56
 
    if (strcmp (request, r) == 0)
 
58
    else if (strcmp (name, "LOCK-SEAT") == 0)
57
59
    {
58
60
        status_notify ("%s LOCK-SEAT", session_id);
59
61
        g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
68
70
                                     NULL,
69
71
                                     NULL);
70
72
    }
71
 
    g_free (r);
72
73
 
73
 
    r = g_strdup_printf ("%s LOCK-SESSION", session_id);
74
 
    if (strcmp (request, r) == 0)
 
74
    else if (strcmp (name, "LOCK-SESSION") == 0)
75
75
    {
76
76
        status_notify ("%s LOCK-SESSION", session_id);
77
77
        g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL),
86
86
                                     NULL,
87
87
                                     NULL);
88
88
    }
89
 
    g_free (r);
90
89
 
91
 
    r = g_strdup_printf ("%s LIST-GROUPS", session_id);
92
 
    if (strcmp (request, r) == 0)
 
90
    else if (strcmp (name, "LIST-GROUPS") == 0)
93
91
    {
94
92
        int n_groups, i;
95
93
        gid_t *groups;
96
94
        GString *group_list;
97
95
 
98
96
        n_groups = getgroups (0, NULL);
 
97
        if (n_groups < 0)
 
98
        {
 
99
            g_printerr ("Failed to get groups: %s", strerror (errno));
 
100
            n_groups = 0;
 
101
        }
99
102
        groups = malloc (sizeof (gid_t) * n_groups);
100
103
        n_groups = getgroups (n_groups, groups);
101
104
        group_list = g_string_new ("");
116
119
        free (groups);
117
120
    }
118
121
 
119
 
    r = g_strdup_printf ("%s READ-ENV NAME=", session_id);
120
 
    if (g_str_has_prefix (request, r))
 
122
    else if (strcmp (name, "READ-ENV") == 0)
121
123
    {
122
 
        const gchar *name = request + strlen (r);
 
124
        const gchar *name = g_hash_table_lookup (params, "NAME");
123
125
        const gchar *value = g_getenv (name);
124
126
        status_notify ("%s READ-ENV NAME=%s VALUE=%s", session_id, name, value ? value : "");
125
127
    }
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))
 
128
 
 
129
    else if (strcmp (name, "WRITE-STDOUT") == 0)
 
130
        g_print ("%s", (const gchar *) g_hash_table_lookup (params, "TEXT"));
 
131
 
 
132
    else if (strcmp (name, "WRITE-STDERR") == 0)
 
133
        g_printerr ("%s", (const gchar *) g_hash_table_lookup (params, "TEXT"));
 
134
 
 
135
    else if (strcmp (name, "READ") == 0)
140
136
    {
141
 
        const gchar *name = request + strlen (r);
142
 
        gchar *contents;
 
137
        const gchar *name = g_hash_table_lookup (params, "FILE");
 
138
        gchar *contents = NULL;
143
139
        GError *error = NULL;
144
140
 
145
141
        if (g_file_get_contents (name, &contents, NULL, &error))
146
142
            status_notify ("%s READ FILE=%s TEXT=%s", session_id, name, contents);
147
143
        else
148
144
            status_notify ("%s READ FILE=%s ERROR=%s", session_id, name, error->message);
 
145
        g_free (contents);
149
146
        g_clear_error (&error);
150
147
    }
151
 
    g_free (r);
152
148
 
153
 
    r = g_strdup_printf ("%s LIST-UNKNOWN-FILE-DESCRIPTORS", session_id);
154
 
    if (strcmp (request, r) == 0)
 
149
    else if (strcmp (name, "LIST-UNKNOWN-FILE-DESCRIPTORS") == 0)
155
150
        status_notify ("%s LIST-UNKNOWN-FILE-DESCRIPTORS FDS=%s", session_id, open_fds->str);
156
 
    g_free (r);
157
151
 
158
 
    r = g_strdup_printf ("%s CHECK-X-AUTHORITY", session_id);
159
 
    if (strcmp (request, r) == 0)
 
152
    else if (strcmp (name, "CHECK-X-AUTHORITY") == 0)
160
153
    {
161
154
        gchar *xauthority;
162
155
        GStatBuf file_info;
182
175
        status_notify ("%s CHECK-X-AUTHORITY MODE=%s", session_id, mode_string->str);
183
176
        g_string_free (mode_string, TRUE);
184
177
    }
185
 
    g_free (r);
186
178
}
187
179
 
188
180
int
205
197
            session_id = g_strdup_printf ("SESSION-X-%s", display);
206
198
    }
207
199
    else
208
 
        session_id = g_strdup ("SESSION-?");
 
200
        session_id = g_strdup ("SESSION-UNKNOWN");
209
201
 
210
202
    open_fds = g_string_new ("");
211
203
    open_max = sysconf (_SC_OPEN_MAX);
217
209
    if (g_str_has_suffix (open_fds->str, ","))
218
210
        open_fds->str[strlen (open_fds->str) - 1] = '\0';
219
211
 
220
 
    signal (SIGINT, quit_cb);
221
 
    signal (SIGTERM, quit_cb);
222
 
 
223
212
#if !defined(GLIB_VERSION_2_36)
224
213
    g_type_init ();
225
214
#endif
226
215
 
227
216
    loop = g_main_loop_new (NULL, FALSE);
228
217
 
229
 
    status_connect (request_cb);
 
218
    g_unix_signal_add (SIGINT, sigint_cb, NULL);
 
219
    g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
 
220
 
 
221
    status_connect (request_cb, session_id);
230
222
 
231
223
    status_text = g_string_new ("");
232
224
    g_string_printf (status_text, "%s START", session_id);
241
233
    if (argc > 1)
242
234
        g_string_append_printf (status_text, " NAME=%s", argv[1]);
243
235
    g_string_append_printf (status_text, " USER=%s", getenv ("USER"));
244
 
    status_notify (status_text->str);
 
236
    status_notify ("%s", status_text->str);
245
237
    g_string_free (status_text, TRUE);
246
238
 
247
239
    config = g_key_file_new ();