/
XorgDisplayServer.cpp
312 lines (244 loc) · 9.43 KB
/
XorgDisplayServer.cpp
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
/***************************************************************************
* Copyright (c) 2014 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
* Copyright (c) 2013 Abdurrahman AVCI <abdurrahmanavci@gmail.com>
*
* 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 <QDebug>
#include <QFile>
#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 = 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 = QString("%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;
}
void XorgDisplayServer::addCookie(const QString &file) {
// log message
qDebug() << "Adding cookie to" << file;
// Touch file
QFile file_handler(file);
file_handler.open(QIODevice::WriteOnly);
file_handler.close();
QString cmd = QString("%1 -f %2 -q").arg(mainConfig.XDisplay.XauthPath.get()).arg(file);
// execute xauth
FILE *fp = popen(qPrintable(cmd), "w");
// check file
if (!fp)
return;
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
pclose(fp);
}
bool XorgDisplayServer::start() {
// check flag
if (m_started)
return false;
// create process
process = new QProcess(this);
// delete process on finish
connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished()));
// log message
qDebug() << "Display server starting...";
if (daemonApp->testing()) {
QStringList args;
args << m_display << "-ac" << "-br" << "-noreset" << "-screen" << "800x600";
process->start(mainConfig.XDisplay.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("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;
args << "-auth" << m_authPath
<< "-nolisten" << "tcp"
<< "-background" << "none"
<< "-noreset"
<< "-displayfd" << QString::number(pipeFds[1])
<< QString("vt%1").arg(displayPtr()->terminalId());
qDebug() << "Running:"
<< qPrintable(mainConfig.XDisplay.ServerPath.get())
<< qPrintable(args.join(" "));
process->start(mainConfig.XDisplay.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;
}
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();
displayNumber.prepend(QByteArray(":"));
displayNumber.remove(displayNumber.size() -1, 1); //trim trailing whitespace
m_display= displayNumber;
// close our pipe
close(pipeFds[0]);
emit started();
}
// generate auth file
addCookie(m_authPath);
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.XDisplay.DisplayStopCommand.get();
// create display setup script process
QProcess *displayStopScript = new QProcess();
// set process environment
QProcessEnvironment env;
env.insert("DISPLAY", m_display);
env.insert("HOME", "/");
env.insert("PATH", mainConfig.Users.DefaultPath.get());
env.insert("SHELL", "/bin/sh");
displayStopScript->setProcessEnvironment(env);
// start display setup 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.XDisplay.DisplayCommand.get();
// create display setup script process
QProcess *displayScript = new QProcess();
// set process environment
QProcessEnvironment env;
env.insert("DISPLAY", m_display);
env.insert("HOME", "/");
env.insert("PATH", mainConfig.Users.DefaultPath.get());
env.insert("XAUTHORITY", m_authPath);
env.insert("SHELL", "/bin/sh");
displayScript->setProcessEnvironment(env);
// delete displayScript on finish
connect(displayScript, SIGNAL(finished(int,QProcess::ExitStatus)), displayScript, SLOT(deleteLater()));
// start display setup script
qDebug() << "Running display setup script " << displayCommand;
displayScript->start(displayCommand);
// 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.";
}
}
}