kdecore: save lock files in the global temporary directory

O_CLOEXEC won't do it but saving the locks in /tmp or other directory that
is purged across reboot will remove stale locks

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-09-09 21:16:04 +03:00
parent f27719e1f8
commit c4ae9e0460
3 changed files with 7 additions and 15 deletions

View file

@ -17,6 +17,8 @@
*/
#include "klockfile.h"
#include "kglobal.h"
#include "kstandarddirs.h"
#include "kdebug.h"
#include "kde_file.h"
@ -48,7 +50,8 @@ KLockFilePrivate::KLockFilePrivate()
KLockFile::KLockFile(const QString &file)
: d(new KLockFilePrivate())
{
d->m_lockfile = QFile::encodeName(file);
d->m_lockfile = QFile::encodeName(KGlobal::dirs()->saveLocation("tmp"));
d->m_lockfile.append(QFile::encodeName(file).toHex());
d->m_lockfile.append(".klockfile");
}
@ -64,11 +67,7 @@ bool KLockFile::tryLock()
return true;
}
#ifdef O_CLOEXEC
d->m_lockfd = KDE_open(d->m_lockfile.constData(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644);
#else
d->m_lockfd = KDE_open(d->m_lockfile.constData(), O_WRONLY | O_CREAT | O_EXCL, 0644);
#endif
return (d->m_lockfd != -1);
}

View file

@ -59,12 +59,6 @@ void kAuthMessageHandler(QtMsgType type, const char *msg)
}
}
static QString kGetLockFile(const QString &helper)
{
const QString lockdir = KGlobal::dirs()->saveLocation("tmp");
return lockdir + helper;
}
class KAuthorizationAdaptor: public QDBusAbstractAdaptor
{
Q_OBJECT
@ -133,7 +127,7 @@ KAuthorization::~KAuthorization()
bool KAuthorization::isAuthorized(const QString &helper)
{
kDebug() << "Checking if" << helper << "is authorized";
KLockFile authorizationlock(kGetLockFile(helper));
KLockFile authorizationlock(helper);
authorizationlock.lock();
QDBusInterface kauthorizationinterface(
helper, QString::fromLatin1("/KAuthorization"), QString::fromLatin1("org.kde.kauthorization"),
@ -152,7 +146,7 @@ bool KAuthorization::isAuthorized(const QString &helper)
int KAuthorization::execute(const QString &helper, const QString &method, const QVariantMap &arguments)
{
kDebug(s_kauthorizationarea) << "Executing" << helper << "method" << method;
KLockFile authorizationlock(kGetLockFile(helper));
KLockFile authorizationlock(helper);
authorizationlock.lock();
QDBusInterface kauthorizationinterface(
helper, QString::fromLatin1("/KAuthorization"), QString::fromLatin1("org.kde.kauthorization"),

View file

@ -19,7 +19,6 @@
#include "kpasswdstore.h"
#include "kconfig.h"
#include "kconfiggroup.h"
#include "kglobal.h"
#include "kstandarddirs.h"
#include "klockfile.h"
#include "ksettings.h"
@ -47,7 +46,7 @@ static QByteArray getCookie()
static QString getLockName(const QByteArray &cookie, const QString &storeid)
{
return QString::fromLatin1("%1/%2-%3").arg(KGlobal::dirs()->saveLocation("tmp"), cookie, storeid);
return QString::fromLatin1("%2-%3").arg(cookie, storeid);
}
class KPasswdStorePrivate