2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2015-01-23 18:23:24 +01:00
|
|
|
|
|
|
|
|
#include "journaldwatcher.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QSocketNotifier>
|
|
|
|
|
|
|
|
|
|
#include <systemd/sd-journal.h>
|
|
|
|
|
|
|
|
|
|
namespace ProjectExplorer {
|
|
|
|
|
|
2016-04-13 15:52:14 +02:00
|
|
|
JournaldWatcher *JournaldWatcher::m_instance = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class JournaldWatcherPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-04-13 15:52:14 +02:00
|
|
|
JournaldWatcherPrivate() = default;
|
2015-01-23 18:23:24 +01:00
|
|
|
~JournaldWatcherPrivate()
|
|
|
|
|
{
|
|
|
|
|
teardown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool setup();
|
|
|
|
|
void teardown();
|
|
|
|
|
|
|
|
|
|
JournaldWatcher::LogEntry retrieveEntry();
|
|
|
|
|
|
|
|
|
|
class SubscriberInformation {
|
|
|
|
|
public:
|
|
|
|
|
SubscriberInformation(QObject *sr, const JournaldWatcher::Subscription &sn) :
|
|
|
|
|
subscriber(sr), subscription(sn)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
QObject *subscriber;
|
|
|
|
|
JournaldWatcher::Subscription subscription;
|
|
|
|
|
};
|
|
|
|
|
QList<SubscriberInformation> m_subscriptions;
|
|
|
|
|
|
2016-04-13 15:52:14 +02:00
|
|
|
sd_journal *m_journalContext = nullptr;
|
|
|
|
|
QSocketNotifier *m_notifier = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool JournaldWatcherPrivate::setup()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!m_journalContext, return false);
|
|
|
|
|
int r = sd_journal_open(&m_journalContext, 0);
|
|
|
|
|
if (r != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
r = sd_journal_seek_tail(m_journalContext);
|
|
|
|
|
if (r != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Work around https://bugs.freedesktop.org/show_bug.cgi?id=64614
|
|
|
|
|
sd_journal_previous(m_journalContext);
|
|
|
|
|
|
|
|
|
|
int fd = sd_journal_get_fd(m_journalContext);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JournaldWatcherPrivate::teardown()
|
|
|
|
|
{
|
|
|
|
|
delete m_notifier;
|
2016-04-13 15:52:14 +02:00
|
|
|
m_notifier = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
|
|
|
|
|
if (m_journalContext) {
|
|
|
|
|
sd_journal_close(m_journalContext);
|
2016-04-13 15:52:14 +02:00
|
|
|
m_journalContext = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JournaldWatcher::LogEntry JournaldWatcherPrivate::retrieveEntry()
|
|
|
|
|
{
|
|
|
|
|
JournaldWatcher::LogEntry result;
|
|
|
|
|
|
|
|
|
|
// Advance:
|
|
|
|
|
int r = sd_journal_next(m_journalContext);
|
|
|
|
|
if (r == 0)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
const void *rawData;
|
|
|
|
|
size_t length;
|
|
|
|
|
SD_JOURNAL_FOREACH_DATA(m_journalContext, rawData, length) {
|
|
|
|
|
QByteArray tmp = QByteArray::fromRawData(static_cast<const char *>(rawData), length);
|
|
|
|
|
int offset = tmp.indexOf('=');
|
|
|
|
|
if (offset < 0)
|
|
|
|
|
continue;
|
|
|
|
|
result.insert(tmp.left(offset), tmp.mid(offset + 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
2016-04-13 15:52:14 +02:00
|
|
|
static JournaldWatcherPrivate *d = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
|
|
|
|
|
JournaldWatcher::~JournaldWatcher()
|
|
|
|
|
{
|
|
|
|
|
d->teardown();
|
|
|
|
|
|
2016-04-13 15:52:14 +02:00
|
|
|
m_instance = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
|
|
|
|
|
delete d;
|
2016-04-13 15:52:14 +02:00
|
|
|
d = nullptr;
|
2015-01-23 18:23:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JournaldWatcher *JournaldWatcher::instance()
|
|
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 11:15:17 +01:00
|
|
|
const QByteArray &JournaldWatcher::machineId()
|
|
|
|
|
{
|
|
|
|
|
static QByteArray id;
|
|
|
|
|
if (id.isEmpty()) {
|
|
|
|
|
sd_id128 sdId;
|
2015-06-29 20:01:47 +02:00
|
|
|
if (sd_id128_get_machine(&sdId) == 0) {
|
|
|
|
|
id.resize(32);
|
|
|
|
|
sd_id128_to_string(sdId, id.data());
|
|
|
|
|
}
|
2015-03-02 11:15:17 +01:00
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 18:23:24 +01:00
|
|
|
bool JournaldWatcher::subscribe(QObject *subscriber, const Subscription &subscription)
|
|
|
|
|
{
|
|
|
|
|
// Check that we do not have that subscriber yet:
|
|
|
|
|
int pos = Utils::indexOf(d->m_subscriptions,
|
|
|
|
|
[subscriber](const JournaldWatcherPrivate::SubscriberInformation &info) {
|
|
|
|
|
return info.subscriber == subscriber;
|
|
|
|
|
});
|
2017-02-27 11:49:00 +01:00
|
|
|
QTC_ASSERT(pos < 0, return false);
|
2015-01-23 18:23:24 +01:00
|
|
|
|
|
|
|
|
d->m_subscriptions.append(JournaldWatcherPrivate::SubscriberInformation(subscriber, subscription));
|
|
|
|
|
connect(subscriber, &QObject::destroyed, m_instance, &JournaldWatcher::unsubscribe);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JournaldWatcher::unsubscribe(QObject *subscriber)
|
|
|
|
|
{
|
|
|
|
|
int pos = Utils::indexOf(d->m_subscriptions,
|
|
|
|
|
[subscriber](const JournaldWatcherPrivate::SubscriberInformation &info) {
|
|
|
|
|
return info.subscriber == subscriber;
|
|
|
|
|
});
|
|
|
|
|
if (pos < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->m_subscriptions.removeAt(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JournaldWatcher::JournaldWatcher()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!m_instance, return);
|
|
|
|
|
|
|
|
|
|
d = new JournaldWatcherPrivate;
|
|
|
|
|
m_instance = this;
|
|
|
|
|
|
|
|
|
|
if (!d->setup())
|
|
|
|
|
d->teardown();
|
|
|
|
|
else
|
2016-01-29 16:38:37 +02:00
|
|
|
connect(d->m_notifier, &QSocketNotifier::activated,
|
|
|
|
|
m_instance, &JournaldWatcher::handleEntry);
|
2015-01-23 18:23:24 +01:00
|
|
|
m_instance->handleEntry(); // advance to the end of file...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JournaldWatcher::handleEntry()
|
|
|
|
|
{
|
2015-03-02 11:15:17 +01:00
|
|
|
if (!d->m_notifier)
|
2015-01-23 18:23:24 +01:00
|
|
|
return;
|
|
|
|
|
|
2015-06-29 20:01:47 +02:00
|
|
|
if (sd_journal_process(d->m_journalContext) != SD_JOURNAL_APPEND)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-01-23 18:23:24 +01:00
|
|
|
LogEntry logEntry;
|
|
|
|
|
forever {
|
|
|
|
|
logEntry = d->retrieveEntry();
|
|
|
|
|
if (logEntry.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const JournaldWatcherPrivate::SubscriberInformation &info: std::as_const(d->m_subscriptions))
|
2015-01-23 18:23:24 +01:00
|
|
|
info.subscription(logEntry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ProjectExplorer
|