Use a higher-level API for restarts/updates on Linux

This gets rid of the custom arguments container
This commit is contained in:
Ilya Fedin 2023-08-14 04:57:05 +04:00 committed by John Preston
parent 082b5ba782
commit 949c486cac

View file

@ -12,38 +12,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "webview/platform/linux/webview_linux_webkitgtk.h" #include "webview/platform/linux/webview_linux_webkitgtk.h"
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <glib/glib.hpp>
#include <sys/stat.h> using namespace gi::repository;
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdlib>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
namespace Platform { namespace Platform {
namespace {
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
Launcher::Launcher(int argc, char *argv[]) Launcher::Launcher(int argc, char *argv[])
: Core::Launcher(argc, argv) { : Core::Launcher(argc, argv) {
@ -74,82 +47,84 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
&& cWriteProtected(); && cWriteProtected();
const auto binaryPath = justRelaunch const auto binaryPath = justRelaunch
? QFile::encodeName(cExeDir() + cExeName()) ? (cExeDir() + cExeName()).toStdString()
: QFile::encodeName(cWriteProtected() : (cWriteProtected()
? (cWorkingDir() + u"tupdates/temp/Updater"_q) ? (cWorkingDir() + u"tupdates/temp/Updater"_q)
: (cExeDir() + u"Updater"_q)); : (cExeDir() + u"Updater"_q)).toStdString();
auto argumentsList = Arguments(); std::vector<std::string> argumentsList;
if (writeProtectedUpdate) { argumentsList.push_back(writeProtectedUpdate ? "pkexec" : binaryPath);
argumentsList.push("pkexec"); argumentsList.push_back((justRelaunch && !arguments().isEmpty())
} ? arguments().first().toStdString()
argumentsList.push((justRelaunch && !arguments().isEmpty())
? QFile::encodeName(arguments().first())
: binaryPath); : binaryPath);
if (cLaunchMode() == LaunchModeAutoStart) { if (cLaunchMode() == LaunchModeAutoStart) {
argumentsList.push("-autostart"); argumentsList.push_back("-autostart");
} }
if (Logs::DebugEnabled()) { if (Logs::DebugEnabled()) {
argumentsList.push("-debug"); argumentsList.push_back("-debug");
} }
if (cStartInTray()) { if (cStartInTray()) {
argumentsList.push("-startintray"); argumentsList.push_back("-startintray");
} }
if (cDataFile() != u"data"_q) { if (cDataFile() != u"data"_q) {
argumentsList.push("-key"); argumentsList.push_back("-key");
argumentsList.push(QFile::encodeName(cDataFile())); argumentsList.push_back(cDataFile().toStdString());
} }
if (justRelaunch) { if (justRelaunch) {
argumentsList.push("-noupdate"); argumentsList.push_back("-noupdate");
argumentsList.push("-tosettings"); argumentsList.push_back("-tosettings");
if (customWorkingDir()) { if (customWorkingDir()) {
argumentsList.push("-workdir"); argumentsList.push_back("-workdir");
argumentsList.push(QFile::encodeName(cWorkingDir())); argumentsList.push_back(cWorkingDir().toStdString());
} }
} else { } else {
argumentsList.push("-workpath"); argumentsList.push_back("-workpath");
argumentsList.push(QFile::encodeName(cWorkingDir())); argumentsList.push_back(cWorkingDir().toStdString());
argumentsList.push("-exename"); argumentsList.push_back("-exename");
argumentsList.push(QFile::encodeName(cExeName())); argumentsList.push_back(cExeName().toStdString());
argumentsList.push("-exepath"); argumentsList.push_back("-exepath");
argumentsList.push(QFile::encodeName(cExeDir())); argumentsList.push_back(cExeDir().toStdString());
if (!arguments().isEmpty()) { if (!arguments().isEmpty()) {
argumentsList.push("-argv0"); argumentsList.push_back("-argv0");
argumentsList.push(QFile::encodeName(arguments().first())); argumentsList.push_back(arguments().first().toStdString());
} }
if (customWorkingDir()) { if (customWorkingDir()) {
argumentsList.push("-workdir_custom"); argumentsList.push_back("-workdir_custom");
} }
if (cWriteProtected()) { if (cWriteProtected()) {
argumentsList.push("-writeprotected"); argumentsList.push_back("-writeprotected");
} }
} }
Logs::closeMain(); Logs::closeMain();
CrashReports::Finish(); CrashReports::Finish();
const auto args = argumentsList.result();
pid_t pid = fork();
switch (pid) {
case -1: return false;
case 0:
execvp(
writeProtectedUpdate ? args[0] : binaryPath.constData(),
args);
return false;
}
// pkexec needs an alive parent // pkexec needs an alive parent
if (writeProtectedUpdate) { if (writeProtectedUpdate) {
waitpid(pid, nullptr, 0); if (!GLib::spawn_sync(
argumentsList,
std::nullopt,
GLib::SpawnFlags::SEARCH_PATH_,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr)) {
return false;
}
// launch new version in the same environment // launch new version in the same environment
return launchUpdater(UpdaterLaunch::JustRelaunch); return launchUpdater(UpdaterLaunch::JustRelaunch);
} }
return true; return GLib::spawn_async(
argumentsList,
std::nullopt,
GLib::SpawnFlags::FILE_AND_ARGV_ZERO_,
nullptr,
nullptr,
nullptr);
} }
} // namespace } // namespace