Repository navigation
Expand file tree
/
Copy pathXorgDisplayServer.cpp
More file actions
365 lines (290 loc) · 12.1 KB
/
Copy pathXorgDisplayServer.cpp
File metadata and controls
365 lines (290 loc) · 12.1 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/***************************************************************************
* Copyright (c) 2014 Pier Luigi Fiorini <[email protected]>
* Copyright (c) 2013 Abdurrahman AVCI <[email protected]>
*
* 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 "XorgDisplayServer.h"
#include "Configuration.h"
#include "DaemonApp.h"
#include "Display.h"
#include "SignalHandler.h"
#include "Seat.h"
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QProcess>
#include <QUuid>
#include <random>
#include <xcb/xcb.h>
#include <pwd.h>
#include <unistd.h>
namespace SDDM {
XorgDisplayServer::XorgDisplayServer(Display *parent) : DisplayServer(parent) {
// get auth directory
QString authDir = QStringLiteral(RUNTIME_DIR);
// use "." as authdir in test mode
if (daemonApp->testing())
authDir = QStringLiteral(".");
// create auth dir if not existing
QDir().mkpath(authDir);
// set auth path
m_authPath = QStringLiteral("%1/%2").arg(authDir).arg(QUuid::createUuid().toString());
// generate cookie
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 15);
// resever 32 bytes
m_cookie.reserve(32);
// create a random hexadecimal number
const char *digits = "0123456789abcdef";
for (int i = 0; i < 32; ++i)
m_cookie[i] = digits[dis(gen)];
}
XorgDisplayServer::~XorgDisplayServer() {
stop();
}
const QString &XorgDisplayServer::display() const {
return m_display;
}
const QString &XorgDisplayServer::authPath() const {
return m_authPath;
}
QString XorgDisplayServer::sessionType() const {
return QStringLiteral("x11");
}
const QString &XorgDisplayServer::cookie() const {
return m_cookie;
}
bool XorgDisplayServer::addCookie(const QString &file) {
// log message
qDebug() << "Adding cookie to" << file;
// Touch file
QFile file_handler(file);
file_handler.open(QIODevice::Append);
file_handler.close();
QString cmd = QStringLiteral("%1 -f %2 -q").arg(mainConfig.X11.XauthPath.get()).arg(file);
// execute xauth
FILE *fp = popen(qPrintable(cmd), "w");
// check file
if (!fp)
return false;
fprintf(fp, "remove %s\n", qPrintable(m_display));
fprintf(fp, "add %s . %s\n", qPrintable(m_display), qPrintable(m_cookie));
fprintf(fp, "exit\n");
// close pipe
return pclose(fp) == 0;
}
bool XorgDisplayServer::start() {
// check flag
if (m_started)
return false;
// create process
process = new QProcess(this);
// delete process on finish
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &XorgDisplayServer::finished);
// log message
qDebug() << "Display server starting...";
// generate auth file.
// For the X server's copy, the display number doesn't matter.
// An empty file would result in no access control!
m_display = QStringLiteral(":0");
if(!addCookie(m_authPath)) {
qCritical() << "Failed to write xauth file";
return false;
}
if (daemonApp->testing()) {
QStringList args;
QDir x11socketDir(QStringLiteral("/tmp/.X11-unix"));
int display = 100;
while (x11socketDir.exists(QStringLiteral("X%1").arg(display))) {
++display;
}
m_display = QStringLiteral(":%1").arg(display);
args << m_display << QStringLiteral("-auth") << m_authPath << QStringLiteral("-br") << QStringLiteral("-noreset") << QStringLiteral("-screen") << QStringLiteral("800x600");
process->start(mainConfig.X11.XephyrPath.get(), args);
// wait for display server to start
if (!process->waitForStarted()) {
// log message
qCritical() << "Failed to start display server process.";
// return fail
return false;
}
emit started();
} else {
// set process environment
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(QStringLiteral("XCURSOR_THEME"), mainConfig.Theme.CursorTheme.get());
process->setProcessEnvironment(env);
//create pipe for communicating with X server
//0 == read from X, 1== write to from X
int pipeFds[2];
if (pipe(pipeFds) != 0) {
qCritical("Could not create pipe to start X server");
}
// start display server
QStringList args = mainConfig.X11.ServerArguments.get().split(QLatin1Char(' '), QString::SkipEmptyParts);
args << QStringLiteral("-auth") << m_authPath
<< QStringLiteral("-background") << QStringLiteral("none")
<< QStringLiteral("-noreset")
<< QStringLiteral("-displayfd") << QString::number(pipeFds[1])
<< QStringLiteral("-seat") << displayPtr()->seat()->name();
if (displayPtr()->seat()->name() == QLatin1String("seat0")) {
args << QStringLiteral("vt%1").arg(displayPtr()->terminalId());
}
qDebug() << "Running:"
<< qPrintable(mainConfig.X11.ServerPath.get())
<< qPrintable(args.join(QLatin1Char(' ')));
process->start(mainConfig.X11.ServerPath.get(), args);
// wait for display server to start
if (!process->waitForStarted()) {
// log message
qCritical() << "Failed to start display server process.";
// return fail
close(pipeFds[0]);
return false;
}
// close the other side of pipe in our process, otherwise reading
// from it may stuck even X server exit.
close(pipeFds[1]);
QFile readPipe;
if (!readPipe.open(pipeFds[0], QIODevice::ReadOnly)) {
qCritical("Failed to open pipe to start X Server");
close(pipeFds[0]);
return false;
}
QByteArray displayNumber = readPipe.readLine();
if (displayNumber.size() < 2) {
// X server gave nothing (or a whitespace).
qCritical("Failed to read display number from pipe");
close(pipeFds[0]);
return false;
}
displayNumber.prepend(QByteArray(":"));
displayNumber.remove(displayNumber.size() -1, 1); // trim trailing whitespace
m_display = QString::fromLocal8Bit(displayNumber);
// close our pipe
close(pipeFds[0]);
emit started();
}
// The file is also used by the greeter, which does care about the
// display number. Write the proper entry, if it's different.
if(m_display != QStringLiteral(":0")) {
if(!addCookie(m_authPath)) {
qCritical() << "Failed to write xauth file";
return false;
}
}
changeOwner(m_authPath);
// set flag
m_started = true;
// return success
return true;
}
void XorgDisplayServer::stop() {
// check flag
if (!m_started)
return;
// log message
qDebug() << "Display server stopping...";
// terminate process
process->terminate();
// wait for finished
if (!process->waitForFinished(5000))
process->kill();
}
void XorgDisplayServer::finished() {
// check flag
if (!m_started)
return;
// reset flag
m_started = false;
// log message
qDebug() << "Display server stopped.";
QString displayStopCommand = mainConfig.X11.DisplayStopCommand.get();
// create display setup script process
QProcess *displayStopScript = new QProcess();
// set process environment
QProcessEnvironment env;
env.insert(QStringLiteral("DISPLAY"), m_display);
env.insert(QStringLiteral("HOME"), QStringLiteral("/"));
env.insert(QStringLiteral("PATH"), mainConfig.Users.DefaultPath.get());
env.insert(QStringLiteral("SHELL"), QStringLiteral("/bin/sh"));
displayStopScript->setProcessEnvironment(env);
// start display stop script
qDebug() << "Running display stop script " << displayStopCommand;
displayStopScript->start(displayStopCommand);
// wait for finished
if (!displayStopScript->waitForFinished(5000))
displayStopScript->kill();
// clean up the script process
displayStopScript->deleteLater();
displayStopScript = nullptr;
// clean up
process->deleteLater();
process = nullptr;
// remove authority file
QFile::remove(m_authPath);
// emit signal
emit stopped();
}
void XorgDisplayServer::setupDisplay() {
QString displayCommand = mainConfig.X11.DisplayCommand.get();
// create cursor setup process
QProcess *setCursor = new QProcess();
// create display setup script process
QProcess *displayScript = new QProcess();
// set process environment
QProcessEnvironment env;
env.insert(QStringLiteral("DISPLAY"), m_display);
env.insert(QStringLiteral("HOME"), QStringLiteral("/"));
env.insert(QStringLiteral("PATH"), mainConfig.Users.DefaultPath.get());
env.insert(QStringLiteral("XAUTHORITY"), m_authPath);
env.insert(QStringLiteral("SHELL"), QStringLiteral("/bin/sh"));
env.insert(QStringLiteral("XCURSOR_THEME"), mainConfig.Theme.CursorTheme.get());
setCursor->setProcessEnvironment(env);
displayScript->setProcessEnvironment(env);
qDebug() << "Setting default cursor";
setCursor->start(QStringLiteral("xsetroot -cursor_name left_ptr"));
// delete setCursor on finish
connect(setCursor, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), setCursor, &QProcess::deleteLater);
// wait for finished
if (!setCursor->waitForFinished(1000)) {
qWarning() << "Could not setup default cursor";
setCursor->kill();
}
// start display setup script
qDebug() << "Running display setup script " << displayCommand;
displayScript->start(displayCommand);
// delete displayScript on finish
connect(displayScript, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), displayScript, &QProcess::deleteLater);
// wait for finished
if (!displayScript->waitForFinished(30000))
displayScript->kill();
// reload config if needed
mainConfig.load();
}
void XorgDisplayServer::changeOwner(const QString &fileName) {
// change the owner and group of the auth file to the sddm user
struct passwd *pw = getpwnam("sddm");
if (!pw)
qWarning() << "Failed to find the sddm user. Owner of the auth file will not be changed.";
else {
if (chown(qPrintable(fileName), pw->pw_uid, pw->pw_gid) == -1)
qWarning() << "Failed to change owner of the auth file.";
}
}
}