]> git.hungrycats.org Git - bees/commitdiff
reporter: add ${HOME} report filename prefix for $BEESHOME
authorZygo Blaxell <bees@furryterror.org>
Sun, 30 Aug 2026 06:31:09 +0000 (02:31 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
A report filename resolves against path.reports, or against the mount
namespace root when it begins with '/'.  Neither reaches $BEESHOME, so
the one file bees has always written there -- beesstats.txt -- cannot be
expressed as a report at all.

Add a ${HOME} prefix that binds the report to the open path.home
directory fd.  It is deliberately not a substitution variable: expanding
it to a path would re-walk a directory that may have moved since startup
and lose the open-fd guarantee, while binding the fd bees already holds
matches what the legacy beesstats.txt write did exactly.  The prefix is
stripped before subst(), so ${UUID} and friends still expand in the
remainder.

A ${HOME} report is disabled with a log line rather than failed when
path.home is unavailable -- state.persistent = no leaves no home
directory to write into, and that is a configuration bees supports.

Assisted-by: Claude-Code:claude-opus-5
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
docs/config-file.md
src/bees-config-v1.cc
src/bees-reporter.h
src/bees.cc

index 313901039a3a62ae424fead771eefb163b0357da..e33a1998e72cde9a2f5f079fa73e581d562cc937 100644 (file)
@@ -368,8 +368,16 @@ not empty.
   A filename of `-` emits the report to the log at `level` instead of writing
   a file (no directory is resolved).
 
-  If the filename begins with `/`, the file is written relative to the
-  mount namespace root (`/`) when bees starts up, before any mount
+  If the filename begins with `${HOME}`, the file is written relative to the
+  open `path.home` (`$BEESHOME`) directory.  `${HOME}` is not a substitution
+  variable but a prefix bound to the directory fd bees already holds open, so
+  the write targets the same directory as the hash table regardless of later
+  path changes; any remaining substitution variables after the prefix are
+  still expanded.  If `path.home` is unavailable (for example
+  `state.persistent = no`), a `${HOME}` report is disabled.
+
+  Otherwise, if the filename begins with `/`, the file is written relative to
+  the mount namespace root (`/`) when bees starts up, before any mount
   namespace change inside the bees process; otherwise, the file is
   written relative to `path.reports`.
 
index 298724e49206a73fd7c0bf1d57ce1de9727877ca..ae68015027ba50e6fc8dc52423dddf9a5cee7ba7 100644 (file)
@@ -157,15 +157,11 @@ static const char bees_config_v1[] = R"--v1-config--(
         # chown.  A leading ':' forces a name lookup (e.g. ':1000').
         group =
 
-        # Filename of the report file.
-        # A filename of '-' emits the report to the log (at the level above)
-        # instead of writing a file.
-        # Substitution variables are expanded (${UUID}, ${LABEL}, ${BASENAME}).
-        # If the filename begins with '/', all leading slashes are stripped and
-        # it is resolved via openat(2) against the filesystem root ('/') opened
-        # at startup before any mount namespace change.
-        # Otherwise it is resolved via openat(2) against the path.reports directory.
-        # Empty after interpolation = this output is disabled even if active = yes.
+        # Filename of the report file.  '-' emits to the log instead of
+        # writing a file; empty disables this output.  A relative name
+        # resolves under path.reports, '/...' against the filesystem root,
+        # and a leading ${HOME} against path.home.  ${UUID}, ${LABEL}, and
+        # ${BASENAME} are expanded.
         filename =
 
         # Content tokens written to the file, in order, space- or comma-separated.
index b59d7366adebdcb5bcfa23b548d329a918b46b2e..84d93205ea3a75406f18be41ea2acf2db706a473 100644 (file)
@@ -21,6 +21,7 @@ struct BeesReportConfig {
        double               interval_s  = 1.0;      ///< Target rewrite interval (seconds).
        string               filename;               ///< Report filename as configured (before path resolution).  "-" means emit to the log.
        int                  log_level   = LOG_INFO;  ///< syslog level used when filename is "-".
+       bool                 home_relative = false;  ///< True when the filename used the ${HOME} prefix: dir_fd is bound to the open $BEESHOME fd.
        mode_t               mode        = 0400;     ///< fchmod value applied before rename.
        uid_t                uid         = uid_t(-1); ///< fchown uid; -1 = no chown.
        gid_t                gid         = gid_t(-1); ///< fchown gid; -1 = no chown.
index 249061bd985f9d04902c22aa9df0106ab5a3caa3..c044adff4f6c95ebc441c0b3635534b66f87313a 100644 (file)
@@ -407,8 +407,23 @@ bees_main(int argc, char *argv[])
                        BEESLOGINFO("report." << sec_name << ".active = " << (cfg.active ? "yes" : "no")
                                << " [report." << sec_name << ".active]");
                        cfg.interval_s = bc->get_config().get(prefix + "interval", parse_report_interval);
-                       cfg.filename   = bc->get_config().subst(
-                               bc->get_config().get(prefix + "filename", [](const string &s) { return s; }));
+                       {
+                               auto raw_filename = bc->get_config().get(prefix + "filename", [](const string &s) { return s; });
+                               // A leading ${HOME} selects the open $BEESHOME directory fd at
+                               // resolution time rather than expanding to a path.  Expanding
+                               // to a path would lose the open-fd guarantee (it would re-walk
+                               // a possibly-moved path through the mount-root fd); binding the
+                               // fd bees already holds matches the legacy beesstats.txt write
+                               // exactly.  ${HOME} is not a subst variable, so strip it here
+                               // before subst; the remainder is still subst-expanded so
+                               // ${UUID} etc. work after the prefix.
+                               static const string home_prefix = "${HOME}/";
+                               if (raw_filename.compare(0, home_prefix.size(), home_prefix) == 0) {
+                                       cfg.home_relative = true;
+                                       raw_filename = raw_filename.substr(home_prefix.size());
+                               }
+                               cfg.filename = bc->get_config().subst(raw_filename);
+                       }
                        cfg.mode = bc->get_config().get(prefix + "mode", [](const string &s) {
                                return mode_t(stoul(s, nullptr, 8));
                        });
@@ -496,10 +511,13 @@ bees_main(int argc, char *argv[])
                // Determine which fds are needed based on active report filenames
                bool needs_reports_path = false;
                bool needs_mount_root = false;
+               bool needs_home = false;
                for (const auto &cfg : configs) {
                        if (!cfg.active || cfg.filename.empty()) continue;
                        if (cfg.filename == "-") {
                                // Log sink: emitted via BEESLOG, needs no directory fd.
+                       } else if (cfg.home_relative) {
+                               needs_home = true;
                        } else if (cfg.filename.at(0) == '/') {
                                needs_mount_root = true;
                        } else {
@@ -536,6 +554,15 @@ bees_main(int argc, char *argv[])
                        mount_root_fd = open_or_die("/", FLAGS_OPEN_DIR);
                }
 
+               // The $BEESHOME directory fd, opened here (before any mount namespace
+               // change) so a ${HOME} report writes through the same open fd the
+               // legacy beesstats.txt used.  May be empty when path.home is
+               // unavailable (e.g. state.persistent=no); such reports go inert below.
+               Fd home_reports_fd;
+               if (needs_home) {
+                       home_reports_fd = bc->home_fd();
+               }
+
                auto reporter = make_shared<BeesReporter>(bc);
 
                // Resolve (dir_fd, rel_filename) for each active report before adding
@@ -544,6 +571,16 @@ bees_main(int argc, char *argv[])
                                if (cfg.filename == "-") {
                                        // Log sink: no path resolution; write_report emits via
                                        // BEESLOG at cfg.log_level.
+                               } else if (cfg.home_relative) {
+                                       if (!home_reports_fd) {
+                                               // path.home unavailable: disable this report rather
+                                               // than fail an openat against an empty fd at write time.
+                                               BEESLOGINFO("report '" << cfg.name << "' targets ${HOME} but path.home is unavailable; disabling [report." << cfg.name << ".filename, path.home]");
+                                               cfg.filename.clear();
+                                       } else {
+                                               cfg.dir_fd = home_reports_fd;
+                                               cfg.rel_filename = cfg.filename;
+                                       }
                                } else if (cfg.filename.at(0) == '/') {
                                        cfg.dir_fd = mount_root_fd;
                                        cfg.rel_filename = cfg.filename.substr(cfg.filename.find_first_not_of('/'));