strawberry/app/notifier.cpp
trvon ca9350f0fa
Build on macOS: portable syscalls, sockets and tray
The tree now compiles on macOS. Each Linux-only call gets a Darwin
branch, and the Linux branch stays as it was:

- io::fdatasync uses fcntl(F_FULLFSYNC): fsync alone only reaches the
  drive cache on APFS, and the store's crash atomicity relies on it.
- Control socket: FD_CLOEXEC set after socket/accept/recvmsg (no
  SOCK_CLOEXEC, accept4 or MSG_CMSG_CLOEXEC), SO_NOSIGPIPE, peer
  credentials from LOCAL_PEERCRED + LOCAL_PEERPID, getgrouplist with
  int groups and a growing buffer. launchd has no RuntimeDirectory, so
  the socket directory is created (0750, access group) when missing.
  Default socket path is /var/run/strawberry/control.sock.
- CRC32C detects ARMv8 CRC with hw.optional.armv8_crc32 (no auxv).
- stat mtime via st_mtimespec; the BPF shared header uses stdint
  typedefs off Linux.
- Background threads join the PRIO_DARWIN_BG band (lower CPU and I/O);
  crash safety zeroes RLIMIT_CORE in place of PR_SET_DUMPABLE.
- The desktop app drops Qt DBus off Linux and shows notifications as
  tray messages (app/notifier_tray.cpp).
- make setup passes Homebrew's keg-only libarchive to meson.

Tests: behavior_live (Linux ptrace API) builds on Linux only, the
control-server fd count reads /dev/fd, the crash test checks the core
limit on macOS, and the runtime pipeline test no longer references a
YARA-only helper when YARA is absent (that also broke Linux builds
without libyara).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 20:52:39 -06:00

71 lines
2.6 KiB
C++

#include "notifier.hpp"
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QVariantMap>
namespace strawberry::app {
namespace {
const QString kService = QStringLiteral("org.freedesktop.Notifications");
const QString kPath = QStringLiteral("/org/freedesktop/Notifications");
const QString kInterface = QStringLiteral("org.freedesktop.Notifications");
}
Notifier::Notifier(QObject* parent) : QObject(parent) {
auto bus = QDBusConnection::sessionBus();
bus.connect(kService, kPath, kInterface, QStringLiteral("ActionInvoked"),
this, SLOT(on_action_invoked(uint, QString)));
bus.connect(kService, kPath, kInterface, QStringLiteral("NotificationClosed"),
this, SLOT(on_closed(uint, uint)));
}
void Notifier::notify(const QString& summary, const QString& body, Urgency urgency,
const QString& finding_id) {
auto message = QDBusMessage::createMethodCall(kService, kPath, kInterface, QStringLiteral("Notify"));
QStringList actions;
if (!finding_id.isEmpty()) {
actions << QStringLiteral("default") << QStringLiteral("Open");
}
QVariantMap hints;
hints.insert(QStringLiteral("urgency"), QVariant::fromValue(static_cast<uchar>(urgency)));
hints.insert(QStringLiteral("desktop-entry"), QStringLiteral("strawberry-tray"));
// Notification servers render the body as markup; escape it.
message << QStringLiteral("Strawberry") << uint(0) << QStringLiteral("security-high")
<< summary << body.toHtmlEscaped() << actions << hints
<< int(urgency == Urgency::critical ? 0 : 8000);
auto* watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(message), this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, watcher, finding_id]() {
QDBusPendingReply<uint> reply = *watcher;
if (reply.isValid() && !finding_id.isEmpty()) {
finding_by_notification_.insert(reply.value(), finding_id);
}
watcher->deleteLater();
});
}
void Notifier::attach_tray_icon(QSystemTrayIcon* icon) {
tray_icon_ = icon; // unused: the notification server handles clicks
}
void Notifier::on_action_invoked(uint id, const QString& action_key) {
const auto it = finding_by_notification_.constFind(id);
if (it == finding_by_notification_.constEnd()) {
return; // not ours
}
if (action_key == QStringLiteral("default")) {
emit activated(*it);
}
}
void Notifier::on_closed(uint id, uint) {
finding_by_notification_.remove(id);
}
} // namespace strawberry::app