7#include "fstabwatcher.h"
8#include "fstab_debug.h"
9#include "soliddefs_p.h"
11#include <QCoreApplication>
13#include <QFileSystemWatcher>
14#include <QSocketNotifier>
22Q_GLOBAL_STATIC(FstabWatcher, globalFstabWatcher)
24static const QString s_mtabFile = QStringLiteral(
"/etc/mtab");
25static const QString s_fstabFile = QStringLiteral(
"/etc/fstab");
26static const QString s_fstabPath = QStringLiteral(
"/etc");
28FstabWatcher::FstabWatcher()
31 auto mountMonitor = mnt_new_monitor();
34 qCritical(FSTAB_LOG) <<
"could not start mount monitor";
37 m_mountMonitor = mountMonitor;
39 auto r = mnt_monitor_enable_kernel(m_mountMonitor,
true);
41 mnt_unref_monitor(m_mountMonitor);
42 qCritical(FSTAB_LOG) <<
"Failed to enable watching of kernel mount events:" << strerror(errno);
45 r = mnt_monitor_enable_userspace(m_mountMonitor,
true, NULL);
47 mnt_unref_monitor(m_mountMonitor);
48 qCritical(FSTAB_LOG) <<
"Failed to enable watching of userspace mount events:" << strerror(errno);
51 auto fd = mnt_monitor_get_fd(m_mountMonitor);
53 mnt_unref_monitor(m_mountMonitor);
54 qCritical(FSTAB_LOG) <<
"Failed to acquire watch file descriptor" << strerror(errno);
65 m_isRoutineInstalled =
false;
66 m_fileSystemWatcher =
new QFileSystemWatcher(
this);
68 m_mtabFile =
new QFile(s_mtabFile,
this);
69 if (m_mtabFile && m_mtabFile->symLinkTarget().startsWith(QLatin1String(
"/proc/")) && m_mtabFile->open(
QIODevice::ReadOnly)) {
73 m_fileSystemWatcher->addPath(s_mtabFile);
76 m_fileSystemWatcher->addPath(s_fstabPath);
78 if (!m_isFstabWatched) {
79 m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile);
80 if (m_isFstabWatched) {
81 qCDebug(FSTAB_LOG) <<
"Re-added" << s_fstabFile;
82 Q_EMIT onFileChanged(s_fstabFile);
87 m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile);
92FstabWatcher::~FstabWatcher()
95 mnt_unref_monitor(m_mountMonitor);
102 m_fileSystemWatcher->setParent(
nullptr);
106void FstabWatcher::onQuit()
109 m_fileSystemWatcher->setParent(
nullptr);
113FstabWatcher *FstabWatcher::instance()
116 FstabWatcher *fstabWatcher = globalFstabWatcher;
118 if (fstabWatcher && !fstabWatcher->m_isRoutineInstalled) {
119 qAddPostRoutine(globalFstabWatcher.destroy);
120 fstabWatcher->m_isRoutineInstalled =
true;
124 return globalFstabWatcher;
129void FstabWatcher::onMountChanged()
133 const char *filename;
134 while (mnt_monitor_next_change(m_mountMonitor, &filename, NULL) == 0) {
135 if (!mtab && ((strcmp(filename,
"/proc/self/mountinfo") == 0) || (strcmp(filename,
"/run/mount/utab") == 0))) {
138 if (!fstab && (strcmp(filename,
"/etc/fstab") == 0)) {
151void FstabWatcher::onFileChanged(
const QString &path)
153 if (path == s_mtabFile) {
155 if (!m_fileSystemWatcher->files().contains(s_mtabFile)) {
156 m_fileSystemWatcher->addPath(s_mtabFile);
159 if (path == s_fstabFile) {
161 if (!m_fileSystemWatcher->files().contains(s_fstabFile)) {
162 m_isFstabWatched = m_fileSystemWatcher->addPath(s_fstabFile);
163 qCDebug(FSTAB_LOG) <<
"Fstab removed, re-added:" << m_isFstabWatched;
172#include "moc_fstabwatcher.cpp"
void directoryChanged(const QString &path)
void fileChanged(const QString &path)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void activated(QSocketDescriptor socket, QSocketNotifier::Type type)