summaryrefslogtreecommitdiffstats
path: root/daemon/gdm-legacy-display.c
blob: e53d82b25ccd17a031c626e6cf9fc4c120a4ac71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

#include "gdm-common.h"
#include "gdm-display.h"
#include "gdm-launch-environment.h"
#include "gdm-legacy-display.h"
#include "gdm-local-display-glue.h"
#include "gdm-server.h"
#include "gdm-settings-direct.h"
#include "gdm-settings-keys.h"

struct _GdmLegacyDisplay
{
        GdmDisplay           parent;

        GdmDBusLocalDisplay *skeleton;

        GdmServer           *server;
};

static void     gdm_legacy_display_class_init   (GdmLegacyDisplayClass *klass);
static void     gdm_legacy_display_init         (GdmLegacyDisplay      *legacy_display);

G_DEFINE_TYPE (GdmLegacyDisplay, gdm_legacy_display, GDM_TYPE_DISPLAY)

static GObject *
gdm_legacy_display_constructor (GType                  type,
                               guint                  n_construct_properties,
                               GObjectConstructParam *construct_properties)
{
        GdmLegacyDisplay      *display;

        display = GDM_LEGACY_DISPLAY (G_OBJECT_CLASS (gdm_legacy_display_parent_class)->constructor (type,
                                                                                                     n_construct_properties,
                                                                                                     construct_properties));

        display->skeleton = GDM_DBUS_LOCAL_DISPLAY (gdm_dbus_local_display_skeleton_new ());

        g_dbus_object_skeleton_add_interface (gdm_display_get_object_skeleton (GDM_DISPLAY (display)),
                                              G_DBUS_INTERFACE_SKELETON (display->skeleton));

        return G_OBJECT (display);
}

static void
gdm_legacy_display_finalize (GObject *object)
{
        GdmLegacyDisplay *display = GDM_LEGACY_DISPLAY (object);

        g_clear_object (&display->skeleton);
        g_clear_object (&display->server);

        G_OBJECT_CLASS (gdm_legacy_display_parent_class)->finalize (object);
}

static gboolean
gdm_legacy_display_prepare (GdmDisplay *display)
{
        GdmLegacyDisplay *self = GDM_LEGACY_DISPLAY (display);
        GdmLaunchEnvironment *launch_environment;
        char          *display_name;
        char          *seat_id;
        gboolean       doing_initial_setup = FALSE;

        display_name = NULL;
        seat_id = NULL;

        g_object_get (self,
                      "x11-display-name", &display_name,
                      "seat-id", &seat_id,
                      "doing-initial-setup", &doing_initial_setup,
                      NULL);

        if (!doing_initial_setup) {
                launch_environment = gdm_create_greeter_launch_environment (display_name,
                                                                            seat_id,
                                                                            NULL,
                                                                            NULL,
                                                                            TRUE);
        } else {
                launch_environment = gdm_create_initial_setup_launch_environment (display_name,
                                                                                  seat_id,
                                                                                  NULL,
                                                                                  NULL,
                                                                                  TRUE);
        }

        g_object_set (self, "launch-environment", launch_environment, NULL);
        g_object_unref (launch_environment);

        if (!gdm_display_create_authority (display)) {
                g_warning ("Unable to set up access control for display %s",
                           display_name);
                return FALSE;
        }

        return GDM_DISPLAY_CLASS (gdm_legacy_display_parent_class)->prepare (display);
}

static void
on_server_ready (GdmServer       *server,
                 GdmLegacyDisplay *self)
{
        gboolean ret;

        ret = gdm_display_connect (GDM_DISPLAY (self));

        if (!ret) {
                g_debug ("GdmDisplay: could not connect to display");
                gdm_display_unmanage (GDM_DISPLAY (self));
        } else {
                GdmLaunchEnvironment *launch_environment;
                char *display_device;

                display_device = gdm_server_get_display_device (server);

                g_object_get (G_OBJECT (self),
                              "launch-environment", &launch_environment,
                              NULL);
                g_object_set (G_OBJECT (launch_environment),
                              "x11-display-device",
                              display_device,
                              NULL);
                g_clear_pointer(&display_device, g_free);
                g_clear_object (&launch_environment);

                g_debug ("GdmDisplay: connected to display");
                g_object_set (G_OBJECT (self), "status", GDM_DISPLAY_MANAGED, NULL);
        }
}

static void
on_server_exited (GdmServer  *server,
                  int         exit_code,
                  GdmDisplay *self)
{
        g_debug ("GdmDisplay: server exited with code %d\n", exit_code);

        gdm_display_unmanage (GDM_DISPLAY (self));
}

static void
on_server_died (GdmServer  *server,
                int         signal_number,
                GdmDisplay *self)
{
        g_debug ("GdmDisplay: server died with signal %d, (%s)",
                 signal_number,
                 g_strsignal (signal_number));

        gdm_display_unmanage (GDM_DISPLAY (self));
}

static void
gdm_legacy_display_manage (GdmDisplay *display)
{
        GdmLegacyDisplay *self = GDM_LEGACY_DISPLAY (display);
        char            *display_name;
        char            *auth_file;
        char            *seat_id;
        gboolean         is_initial;
        gboolean         res;
        gboolean         disable_tcp;

        g_object_get (G_OBJECT (self),
                      "x11-display-name", &display_name,
                      "x11-authority-file", &auth_file,
                      "seat-id", &seat_id,
                      "is-initial", &is_initial,
                      NULL);

        self->server = gdm_server_new (display_name, seat_id, auth_file, is_initial);

        g_free (display_name);
        g_free (auth_file);
        g_free (seat_id);

        disable_tcp = TRUE;
        if (gdm_settings_direct_get_boolean (GDM_KEY_DISALLOW_TCP, &disable_tcp)) {
                g_object_set (self->server,
                              "disable-tcp", disable_tcp,
                              NULL);
        }

        g_signal_connect (self->server,
                          "exited",
                          G_CALLBACK (on_server_exited),
                          self);
        g_signal_connect (self->server,
                          "died",
                          G_CALLBACK (on_server_died),
                          self);
        g_signal_connect (self->server,
                          "ready",
                          G_CALLBACK (on_server_ready),
                          self);

        res = gdm_server_start (self->server);
        if (! res) {
                g_warning (_("Could not start the X "
                             "server (your graphical environment) "
                             "due to an internal error. "
                             "Please contact your system administrator "
                             "or check your syslog to diagnose. "
                             "In the meantime this display will be "
                             "disabled.  Please restart GDM when "
                             "the problem is corrected."));
                gdm_display_unmanage (GDM_DISPLAY (self));
        }

        g_debug ("GdmDisplay: Started X server");

}

static void
gdm_legacy_display_class_init (GdmLegacyDisplayClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GdmDisplayClass *display_class = GDM_DISPLAY_CLASS (klass);

        object_class->constructor = gdm_legacy_display_constructor;
        object_class->finalize = gdm_legacy_display_finalize;

        display_class->prepare = gdm_legacy_display_prepare;
        display_class->manage = gdm_legacy_display_manage;
}

static void
on_display_status_changed (GdmLegacyDisplay *self)
{
        int status;

        status = gdm_display_get_status (GDM_DISPLAY (self));

        switch (status) {
            case GDM_DISPLAY_UNMANAGED:
                if (self->server != NULL)
                        gdm_server_stop (self->server);
                break;
            default:
                break;
        }
}

static void
gdm_legacy_display_init (GdmLegacyDisplay *legacy_display)
{
        g_signal_connect (legacy_display, "notify::status",
                          G_CALLBACK (on_display_status_changed),
                          NULL);
}

GdmDisplay *
gdm_legacy_display_new (int display_number)
{
        GObject *object;
        char    *x11_display;

        x11_display = g_strdup_printf (":%d", display_number);
        object = g_object_new (GDM_TYPE_LEGACY_DISPLAY,
                               "x11-display-number", display_number,
                               "x11-display-name", x11_display,
                               NULL);
        g_free (x11_display);

        return GDM_DISPLAY (object);
}