#pragma once #include #include #include // The headless pass that answers "does this machine have pending updates?" and, // when it does, raises a desktop notification. // // It exists because the graphical window is not running when the machine // starts: the app installs a systemd user unit (see the autostart helpers here) // that runs `PacManager --check-updates` at login, and that call lands here. // // Two guarantees drive the implementation: // - no password: the check goes through `checkupdates` (pacman-contrib), which // syncs into a throwaway database and therefore needs no root, instead of a // `pacman -Sy` the login unit could never authenticate; // - no display: everything here is Qt Core only, so the unit runs with no // DISPLAY, no Wayland socket and no widget. // // The pure parts (parsing, classification, notification text, unit contents) // are static so they are unit-tested without a package manager, a network or a // session bus. class StartupCheck : public QObject { Q_OBJECT public: // Outcome of one check, so the caller (and the tests) can inspect what // happened without parsing console output. struct Result { int updateCount = 0; bool anyCritical = false; // kernel, glibc, init, graphics stack... bool rebootRequired = false; // a kernel or its firmware is pending QStringList packages; // Non-empty when the check failed, or when it could only run in a // degraded way (the pacman -Qu fallback, which reads a database only // root can refresh). Never a reason to skip the notification. QString error; }; explicit StartupCheck(QObject* parent = nullptr); // Runs the whole headless pass (check, then notify when something is // pending) and returns the exit code for main(). Always 0: a background // check that fails must not fill the user's journal with failed units. int run(); // ---- Pure helpers (unit-tested) ---- // Parses the checked tool's output. `checkupdates` prints // "name oldver -> newver" and `pacman -Qu` prints "name newver"; only the // package name is needed and it is the first whitespace-separated token of // every line, so one parser accepts both. Duplicates are dropped. static QStringList parseUpdates(const QString& output); // A package whose update deserves an urgent notification rather than the // ordinary one: the kernel and its firmware, the C library and init, the // graphics stack, pacman itself — the updates that either need a reboot or // can break the session when they fall behind. Prefix-based on purpose: a // package nobody thought about simply gets the normal notification. static bool isCritical(const QString& package); // A package whose update only takes effect after a reboot: the kernel and // the out-of-tree driver built against the running one. static bool isRebootRequired(const QString& package); // The critical subset of a list, so the notification can name what matters. static QStringList criticalPackages(const QStringList& packages); // Notification title and body for a set of pending updates. static QString notificationTitle(const Result& result); static QString notificationBody(const Result& result); // ---- Check and notify (touch the machine) ---- // Pending updates, using `checkupdates` when available and falling back to // the already-synced `pacman -Qu` otherwise. Never needs root; the fallback // reads a database only root can refresh, so it may miss updates — which is // what `error` records rather than hiding. static Result check(); // Sends the notification through notify-send, waiting briefly for the // notification daemon (the login unit can start before mako). Returns false // when nothing could be sent. static bool notify(const Result& result); // ---- Autostart: a systemd user unit the app installs itself ---- // The unit lives under `configDir` (XDG_CONFIG_HOME, i.e. ~/.config) and is // enabled by a symlink in default.target.wants, which is the per-user // equivalent of `systemctl --user enable` without shelling out to it. // Passing the directory lets the unit test drive install/remove inside a // temporary tree instead of the user's own ~/.config. static QString autostartUnitPath(const QString& configDir); static QString autostartWantsPath(const QString& configDir); // Writes the unit and the enabling symlink. `program` is the binary to run // with --check-updates (the running executable, so it follows the install // wherever it is). Filesystem only: no systemd call, so tests stay hermetic. static bool installAutostart(const QString& configDir, const QString& program, QString* error = nullptr); static bool removeAutostart(const QString& configDir, QString* error = nullptr); static bool autostartEnabled(const QString& configDir); // Asks the user's systemd manager to re-read its units, so a fresh install // takes effect without waiting for the next boot. Best effort; a system // without a running user manager (a test, a container) is not an error. static bool reloadUserManager(); private: // Runs a tool to completion, returning its stdout. stderr is kept out of it // on purpose: a pacman warning is not a package name, so folding the two // would feed the parser lines it must not see — `stderrText` receives them // for the error message instead. `exitCode` receives the status, or -1 when // the process could not be started or timed out. static QString runTool(const QString& program, const QStringList& args, int timeoutMs, int* exitCode, QString* stderrText = nullptr); // Waits (bounded) for the session's notification daemon, then sends through // notify-send with the given urgency. static bool sendNotification(const QString& title, const QString& body, bool urgent); };