Private DBus set launchctl env to fake a session bus
This commit is contained in:
parent
ed5e9301c4
commit
df2ca7f25f
3 changed files with 88 additions and 2 deletions
|
@ -32,6 +32,8 @@
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
#include <CoreFoundation/CFBundle.h>
|
#include <CoreFoundation/CFBundle.h>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QRegularExpressionMatch>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace DbusHelper {
|
namespace DbusHelper {
|
||||||
|
@ -72,6 +74,7 @@ QDBusConnection sessionBus()
|
||||||
void launchDBusDaemon()
|
void launchDBusDaemon()
|
||||||
{
|
{
|
||||||
dbusInstance.launchDBusDaemon();
|
dbusInstance.launchDBusDaemon();
|
||||||
|
qAddPostRoutine(closeDBusDaemon);
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeDBusDaemon()
|
void closeDBusDaemon()
|
||||||
|
@ -79,6 +82,21 @@ void closeDBusDaemon()
|
||||||
dbusInstance.closeDBusDaemon();
|
dbusInstance.closeDBusDaemon();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
void macosUnsetLaunchctlEnv()
|
||||||
|
{
|
||||||
|
// Unset Launchd env
|
||||||
|
QProcess unsetLaunchdDBusEnv;
|
||||||
|
unsetLaunchdDBusEnv.setProgram(QStringLiteral("launchctl"));
|
||||||
|
unsetLaunchdDBusEnv.setArguments({
|
||||||
|
QStringLiteral("unsetenv"),
|
||||||
|
QStringLiteral(KDECONNECT_SESSION_DBUS_LAUNCHD_ENV)
|
||||||
|
});
|
||||||
|
unsetLaunchdDBusEnv.start();
|
||||||
|
unsetLaunchdDBusEnv.waitForFinished();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void DBusInstancePrivate::launchDBusDaemon()
|
void DBusInstancePrivate::launchDBusDaemon()
|
||||||
{
|
{
|
||||||
// Kill old dbus daemon
|
// Kill old dbus daemon
|
||||||
|
@ -118,6 +136,28 @@ void DBusInstancePrivate::launchDBusDaemon()
|
||||||
m_dbusProcess->setStandardOutputFile(KdeConnectConfig::instance()->privateDBusAddressPath());
|
m_dbusProcess->setStandardOutputFile(KdeConnectConfig::instance()->privateDBusAddressPath());
|
||||||
m_dbusProcess->setStandardErrorFile(QProcess::nullDevice());
|
m_dbusProcess->setStandardErrorFile(QProcess::nullDevice());
|
||||||
m_dbusProcess->start();
|
m_dbusProcess->start();
|
||||||
|
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
// Set launchctl env
|
||||||
|
QString privateDBusAddress = KdeConnectConfig::instance()->privateDBusAddress();
|
||||||
|
QRegularExpressionMatch path;
|
||||||
|
if (privateDBusAddress.contains(QRegularExpression(
|
||||||
|
QStringLiteral("path=(?<path>/tmp/dbus-[A-Za-z0-9]+)")
|
||||||
|
), &path)) {
|
||||||
|
qCDebug(KDECONNECT_CORE) << "DBus address: " << path.captured(QStringLiteral("path"));
|
||||||
|
QProcess setLaunchdDBusEnv;
|
||||||
|
setLaunchdDBusEnv.setProgram(QStringLiteral("launchctl"));
|
||||||
|
setLaunchdDBusEnv.setArguments({
|
||||||
|
QStringLiteral("setenv"),
|
||||||
|
QStringLiteral(KDECONNECT_SESSION_DBUS_LAUNCHD_ENV),
|
||||||
|
path.captured(QStringLiteral("path"))
|
||||||
|
});
|
||||||
|
setLaunchdDBusEnv.start();
|
||||||
|
setLaunchdDBusEnv.waitForFinished();
|
||||||
|
} else {
|
||||||
|
qCDebug(KDECONNECT_CORE) << "Cannot get dbus address";
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void DBusInstancePrivate::closeDBusDaemon()
|
void DBusInstancePrivate::closeDBusDaemon()
|
||||||
|
@ -132,6 +172,10 @@ void DBusInstancePrivate::closeDBusDaemon()
|
||||||
QFile privateDBusAddressFile(KdeConnectConfig::instance()->privateDBusAddressPath());
|
QFile privateDBusAddressFile(KdeConnectConfig::instance()->privateDBusAddressPath());
|
||||||
|
|
||||||
if (privateDBusAddressFile.exists()) privateDBusAddressFile.resize(0);
|
if (privateDBusAddressFile.exists()) privateDBusAddressFile.resize(0);
|
||||||
|
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
macosUnsetLaunchctlEnv();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
#define KDECONNECT_PRIVATE_DBUS_ADDR "${KDECONNECT_PRIVATE_DBUS_ADDR}"
|
#define KDECONNECT_PRIVATE_DBUS_ADDR "${KDECONNECT_PRIVATE_DBUS_ADDR}"
|
||||||
#define KDECONNECT_PRIVATE_DBUS_NAME "${KDECONNECT_PRIVATE_DBUS_NAME}"
|
#define KDECONNECT_PRIVATE_DBUS_NAME "${KDECONNECT_PRIVATE_DBUS_NAME}"
|
||||||
|
|
||||||
|
#define KDECONNECT_SESSION_DBUS_LAUNCHD_ENV "DBUS_LAUNCHD_SESSION_BUS_SOCKET"
|
||||||
|
|
||||||
namespace DbusHelper {
|
namespace DbusHelper {
|
||||||
void KDECONNECTCORE_EXPORT filterNonExportableCharacters(QString& s);
|
void KDECONNECTCORE_EXPORT filterNonExportableCharacters(QString& s);
|
||||||
|
|
||||||
|
@ -36,6 +38,9 @@ namespace DbusHelper {
|
||||||
void KDECONNECTCORE_EXPORT closeDBusDaemon();
|
void KDECONNECTCORE_EXPORT closeDBusDaemon();
|
||||||
#endif
|
#endif
|
||||||
QDBusConnection KDECONNECTCORE_EXPORT sessionBus();
|
QDBusConnection KDECONNECTCORE_EXPORT sessionBus();
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
void KDECONNECTCORE_EXPORT macosUnsetLaunchctlEnv();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
#ifdef QSYSTRAY
|
||||||
#include <QSystemTrayIcon>
|
#include <QSystemTrayIcon>
|
||||||
|
@ -61,13 +62,49 @@ int main(int argc, char** argv)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
|
// Unset launchctl env, avoid block
|
||||||
|
DbusHelper::macosUnsetLaunchctlEnv();
|
||||||
|
|
||||||
// Get bundle path
|
// Get bundle path
|
||||||
CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
|
CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
|
||||||
QString basePath = QUrl::fromCFURL(url).path();
|
QString basePath = QUrl::fromCFURL(url).path();
|
||||||
|
|
||||||
|
// Start kdeconnectd
|
||||||
QProcess kdeconnectdProcess;
|
QProcess kdeconnectdProcess;
|
||||||
kdeconnectdProcess.start(basePath + QStringLiteral("Contents/MacOS/kdeconnectd")); // Start kdeconnectd
|
kdeconnectdProcess.start(basePath + QStringLiteral("Contents/MacOS/kdeconnectd"));
|
||||||
QThread::sleep(5); // Wait for kdeconnectd and its dbus-daemon
|
|
||||||
|
// Wait for dbus daemon env
|
||||||
|
QProcess getLaunchdDBusEnv;
|
||||||
|
int retry = 0;
|
||||||
|
do {
|
||||||
|
getLaunchdDBusEnv.setProgram(QStringLiteral("launchctl"));
|
||||||
|
getLaunchdDBusEnv.setArguments({
|
||||||
|
QStringLiteral("getenv"),
|
||||||
|
QStringLiteral(KDECONNECT_SESSION_DBUS_LAUNCHD_ENV)
|
||||||
|
});
|
||||||
|
getLaunchdDBusEnv.start();
|
||||||
|
getLaunchdDBusEnv.waitForFinished();
|
||||||
|
|
||||||
|
QString launchdDBusEnv = QString::fromLocal8Bit(getLaunchdDBusEnv.readAllStandardOutput());
|
||||||
|
|
||||||
|
if (launchdDBusEnv.length() > 0) {
|
||||||
|
break;
|
||||||
|
} else if (retry >= 10) {
|
||||||
|
// Show a warning and exit
|
||||||
|
qCritical() << "Fail to get launchctl" << KDECONNECT_SESSION_DBUS_LAUNCHD_ENV << "env";
|
||||||
|
|
||||||
|
QMessageBox::critical(nullptr, i18n("KDE Connect"),
|
||||||
|
i18n("Cannot connect to DBus\n"
|
||||||
|
"KDE Connect will quit"),
|
||||||
|
QMessageBox::Abort,
|
||||||
|
QMessageBox::Abort);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
QThread::sleep(1); // Retry after 1s
|
||||||
|
retry++;
|
||||||
|
}
|
||||||
|
} while(true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef USE_PRIVATE_DBUS
|
#ifndef USE_PRIVATE_DBUS
|
||||||
|
|
Loading…
Reference in a new issue