2017-12-11 14:45:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-12-11 14:45:29 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-12-11 14:45:29 +00:00
|
|
|
*/
|
|
|
|
#include "platform/linux/launcher_linux.h"
|
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
#include "core/crash_reports.h"
|
2018-07-11 22:14:44 +00:00
|
|
|
#include "core/update_checker.h"
|
2021-08-25 07:37:20 +00:00
|
|
|
#include "webview/platform/linux/webview_linux_webkit2gtk.h"
|
2017-12-12 07:52:53 +00:00
|
|
|
|
2020-10-28 14:15:43 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2021-04-27 12:16:20 +00:00
|
|
|
#include <sys/wait.h>
|
2017-12-12 07:52:53 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2017-12-11 14:45:29 +00:00
|
|
|
namespace Platform {
|
2017-12-12 07:52:53 +00:00
|
|
|
namespace {
|
|
|
|
|
2022-11-21 04:38:14 +00:00
|
|
|
Launcher *LauncherInstance = nullptr;
|
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
class Arguments {
|
|
|
|
public:
|
|
|
|
void push(QByteArray argument) {
|
|
|
|
argument.append(char(0));
|
|
|
|
_argumentValues.push_back(argument);
|
|
|
|
_arguments.push_back(_argumentValues.back().data());
|
|
|
|
}
|
|
|
|
|
|
|
|
char **result() {
|
|
|
|
_arguments.push_back(nullptr);
|
|
|
|
return _arguments.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<QByteArray> _argumentValues;
|
|
|
|
std::vector<char*> _arguments;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-06-03 19:52:23 +00:00
|
|
|
Launcher::Launcher(int argc, char *argv[])
|
2021-06-22 23:20:40 +00:00
|
|
|
: Core::Launcher(argc, argv)
|
|
|
|
, _arguments(argv, argv + argc) {
|
2022-11-21 04:38:14 +00:00
|
|
|
Expects(LauncherInstance == nullptr);
|
|
|
|
|
|
|
|
LauncherInstance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Launcher &Launcher::Instance() {
|
|
|
|
Expects(LauncherInstance != nullptr);
|
|
|
|
|
|
|
|
return *LauncherInstance;
|
2021-06-22 23:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Launcher::exec() {
|
2021-09-03 15:49:48 +00:00
|
|
|
for (auto i = begin(_arguments), e = end(_arguments); i != e; ++i) {
|
2022-10-03 04:11:20 +00:00
|
|
|
if (*i == "-webviewhelper" && std::distance(i, e) > 1) {
|
|
|
|
Webview::WebKit2Gtk::SetSocketPath(*(i + 1));
|
2021-11-28 19:15:51 +00:00
|
|
|
return Webview::WebKit2Gtk::Exec();
|
2021-06-22 23:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Core::Launcher::exec();
|
2018-06-03 19:52:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:15:43 +00:00
|
|
|
void Launcher::initHook() {
|
2020-12-18 08:43:51 +00:00
|
|
|
QApplication::setAttribute(Qt::AA_DisableSessionManager, true);
|
2020-10-28 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
bool Launcher::launchUpdater(UpdaterLaunch action) {
|
|
|
|
if (cExeName().isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:16:20 +00:00
|
|
|
const auto binaryPath = (action == UpdaterLaunch::JustRelaunch)
|
|
|
|
? (cExeDir() + cExeName())
|
|
|
|
: (cWriteProtected()
|
2022-11-29 21:46:36 +00:00
|
|
|
? (cWorkingDir() + u"tupdates/temp/Updater"_q)
|
|
|
|
: (cExeDir() + u"Updater"_q));
|
2017-12-12 07:52:53 +00:00
|
|
|
|
|
|
|
auto argumentsList = Arguments();
|
2021-05-03 11:48:31 +00:00
|
|
|
if (action == UpdaterLaunch::PerformUpdate && cWriteProtected()) {
|
2021-04-27 12:16:20 +00:00
|
|
|
argumentsList.push("pkexec");
|
|
|
|
}
|
|
|
|
argumentsList.push(QFile::encodeName(binaryPath));
|
2017-12-12 07:52:53 +00:00
|
|
|
|
|
|
|
if (cLaunchMode() == LaunchModeAutoStart) {
|
|
|
|
argumentsList.push("-autostart");
|
|
|
|
}
|
2018-06-05 13:32:26 +00:00
|
|
|
if (Logs::DebugEnabled()) {
|
2017-12-12 07:52:53 +00:00
|
|
|
argumentsList.push("-debug");
|
|
|
|
}
|
|
|
|
if (cStartInTray()) {
|
|
|
|
argumentsList.push("-startintray");
|
|
|
|
}
|
2022-11-29 21:46:36 +00:00
|
|
|
if (cDataFile() != u"data"_q) {
|
2017-12-12 07:52:53 +00:00
|
|
|
argumentsList.push("-key");
|
|
|
|
argumentsList.push(QFile::encodeName(cDataFile()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == UpdaterLaunch::JustRelaunch) {
|
|
|
|
argumentsList.push("-noupdate");
|
|
|
|
argumentsList.push("-tosettings");
|
|
|
|
if (customWorkingDir()) {
|
|
|
|
argumentsList.push("-workdir");
|
|
|
|
argumentsList.push(QFile::encodeName(cWorkingDir()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
argumentsList.push("-workpath");
|
|
|
|
argumentsList.push(QFile::encodeName(cWorkingDir()));
|
|
|
|
argumentsList.push("-exename");
|
|
|
|
argumentsList.push(QFile::encodeName(cExeName()));
|
|
|
|
argumentsList.push("-exepath");
|
|
|
|
argumentsList.push(QFile::encodeName(cExeDir()));
|
|
|
|
if (customWorkingDir()) {
|
|
|
|
argumentsList.push("-workdir_custom");
|
|
|
|
}
|
2021-04-27 12:16:20 +00:00
|
|
|
if (cWriteProtected()) {
|
2021-05-03 10:21:17 +00:00
|
|
|
argumentsList.push("-writeprotected");
|
2021-04-27 12:16:20 +00:00
|
|
|
}
|
2017-12-12 07:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Logs::closeMain();
|
|
|
|
CrashReports::Finish();
|
|
|
|
|
|
|
|
const auto args = argumentsList.result();
|
2017-12-11 14:45:29 +00:00
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case -1: return false;
|
2021-04-27 12:16:20 +00:00
|
|
|
case 0: execvp(args[0], args); return false;
|
2017-12-12 07:52:53 +00:00
|
|
|
}
|
2021-04-27 12:16:20 +00:00
|
|
|
|
|
|
|
// pkexec needs an alive parent
|
2021-05-03 11:48:31 +00:00
|
|
|
if (action == UpdaterLaunch::PerformUpdate && cWriteProtected()) {
|
2021-04-27 12:16:20 +00:00
|
|
|
waitpid(pid, nullptr, 0);
|
2021-05-03 10:21:17 +00:00
|
|
|
// launch new version in the same environment
|
|
|
|
return launchUpdater(UpdaterLaunch::JustRelaunch);
|
2021-04-27 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
return true;
|
2017-12-11 14:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|