Skip to content

Commit

Permalink
Merge pull request #38 from kambala-decapitator/macos-relaunch-as-root
Browse files Browse the repository at this point in the history
[macOS] simplify restarting app as root with Apple Script
  • Loading branch information
chewitt authored Feb 20, 2024
2 parents 8658979 + 28d89c2 commit 1e9fda8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 57 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ qt_add_executable(LibreELEC.USB-SD.Creator WIN32 MACOSX_BUNDLE
# TODO: use Qt's template <QTDIR>/lib/cmake/Qt6/macos/Info.plist.app.in
set_target_properties(LibreELEC.USB-SD.Creator PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "dmg_osx/template.app/Contents/Info.plist"
MACOSX_BUNDLE_GUI_IDENTIFIER "tv.libreelec.usb-sd-creator"
MACOSX_BUNDLE_BUNDLE_NAME "LibreELEC USB-SD Creator"
MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
MACOSX_BUNDLE_GUI_IDENTIFIER "tv.libreelec.usb-sd-creator"
MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
)

Expand Down
11 changes: 0 additions & 11 deletions dmg_osx/main.scpt.txt

This file was deleted.

2 changes: 1 addition & 1 deletion dmg_osx/template.app/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>LibreELEC</string>
<string>${MACOSX_BUNDLE_BUNDLE_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
Expand Down
24 changes: 0 additions & 24 deletions dmg_osx/template.app/Contents/MacOS/askPass.js

This file was deleted.

39 changes: 19 additions & 20 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,52 @@

#ifdef Q_OS_MACOS
#include "privileges_unix.h"

#include <QLatin1String>
#include <QProcess>
#else
#include "privileges.h"
#endif

#include <QApplication>
#include <QFileInfo>
#include <QDesktopServices>
#include <QProcess>
#include <QProxyStyle>
#include <QNetworkProxy>
#include <QDebug>

#ifndef ALWAYS_DEBUG_OUTPUT
void noMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(type);
Q_UNUSED(context);
Q_UNUSED(msg);
}
#endif

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString argFile = "";

const auto cmdArgs = app.arguments();

#ifndef ALWAYS_DEBUG_OUTPUT
if (app.arguments().contains("--debug") == false)
if (cmdArgs.contains("--debug") == false)
qInstallMessageHandler(noMessageOutput);
#endif

#ifdef Q_OS_MACOS
// If not running with root privileges, relaunch executable with sudo.
if (getuid() != 0 && app.arguments().contains("--elevated") == false)
if (getuid() != 0 && cmdArgs.contains("--elevated") == false)
{
QString askPassCommand = QCoreApplication::applicationDirPath() + "/askPass.js";

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("SUDO_ASKPASS", askPassCommand);
const auto sudoPrompt = QLatin1String{"%1 requires admin permissions."}.arg(app.applicationDisplayName());
const QLatin1String appleScript{"do shell script \"sudo %1\" with prompt \"%2\" with administrator privileges"};

QProcess myProcess;
myProcess.setProcessEnvironment(env);
myProcess.setProgram("sudo");
myProcess.setArguments(QStringList()
<< "-A"
<< QCoreApplication::applicationFilePath()
<< "--elevated");
bool success = myProcess.startDetached();

if (success)
myProcess.setProgram(QLatin1String{"osascript"});
myProcess.setArguments({"-e", appleScript.arg(QCoreApplication::applicationFilePath(), sudoPrompt)});

if (myProcess.startDetached())
{
return 0;
}
Expand All @@ -82,7 +80,7 @@ int main(int argc, char *argv[])

qDebug() << "App data: Version:" << BUILD_VERSION ", Build date: " BUILD_DATE;

if (app.arguments().contains("--no-proxy") == false) {
if (cmdArgs.contains("--no-proxy") == false) {
QNetworkProxyQuery npq(QUrl("http://releases.libreelec.tv/"));
QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(npq);
if (listOfProxies.size()) {
Expand All @@ -94,9 +92,10 @@ int main(int argc, char *argv[])
Privileges privileges = Privileges();
privileges.Whoami();

QString argFile;
// skip program filename
for (int i=1; i<app.arguments().size(); i++) {
QString file = app.arguments().at(i);
for (int i = 1; i < cmdArgs.size(); i++) {
QString file = cmdArgs.at(i);
QFileInfo checkFile(file);

if (checkFile.exists() && checkFile.isFile()) {
Expand Down

0 comments on commit 1e9fda8

Please sign in to comment.