Skip to content

Commit 2bff501

Browse files
committed
fix(freebsd): treat ${LOCALBASE}/bin as pkg-managed; resolve self via sysctl
Two FreeBSD gaps remain after the runtime rework of install/update (DeusData#1566) and the KERN_PROC_PATHNAME self-path fix landed on main. 1. cli_external_manager_name() matches only positive evidence (mise, Homebrew, nix, asdf, cargo). ${LOCALBASE}/bin was not among them, so a ports/pkg install of cbm was still copied into ~/.local/bin and a shell rc was edited -- exactly what externally-managed detection is meant to prevent. Recognise ${LOCALBASE}/bin and /sbin on FreeBSD so install skips the copy and update defers to `pkg upgrade`. LOCALBASE is configurable, so the FreeBSD port passes its real PREFIX via -DCBM_PKG_PREFIX and we honour it, falling back to the documented /usr/local default for a plain upstream build. --force-binary remains the escape hatch for anyone self-managing that prefix. 2. resolve_self_executable() in the HTTP server still read /proc/self/exe, which FreeBSD does not mount by default. Add a KERN_PROC_PATHNAME sysctl branch, mirroring cbm_detect_self_path(). Test updated: on FreeBSD ${LOCALBASE}/bin/cbm is now externally managed; a non-LOCALBASE prefix (/opt/local) is not. Signed-off-by: Olivier Cochard-Labbé <olivier@cochard.me>
1 parent 31819c9 commit 2bff501

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎src/cli/cli.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9699,6 +9699,27 @@ static const char *cli_external_manager_name(const char *self_path) {
96999699
if (strstr(self_path, "/.cargo/bin/")) {
97009700
return "cargo";
97019701
}
9702+
#ifdef __FreeBSD__
9703+
/* FreeBSD ports/pkg install under ${LOCALBASE} (default /usr/local). pkg owns
9704+
* that file, so install must not drop a second copy in ~/.local/bin or edit a
9705+
* shell rc, and update must defer to pkg(8). Anchor at the start so a manual
9706+
* `install --dir=/opt/...` elsewhere is still treated as ours; --force-binary
9707+
* is the escape hatch for anyone who really does self-manage that prefix.
9708+
*
9709+
* LOCALBASE is configurable, so the port passes its real PREFIX via
9710+
* -DCBM_PKG_PREFIX; when it is absent we fall back to the documented default. */
9711+
#ifdef CBM_PKG_PREFIX
9712+
if (strncmp(self_path, CBM_PKG_PREFIX "/bin/", sizeof(CBM_PKG_PREFIX "/bin/") - 1) == 0 ||
9713+
strncmp(self_path, CBM_PKG_PREFIX "/sbin/", sizeof(CBM_PKG_PREFIX "/sbin/") - 1) == 0) {
9714+
return "FreeBSD pkg";
9715+
}
9716+
#else
9717+
if (strncmp(self_path, "/usr/local/bin/", 15) == 0 ||
9718+
strncmp(self_path, "/usr/local/sbin/", 16) == 0) {
9719+
return "FreeBSD pkg";
9720+
}
9721+
#endif
9722+
#endif
97029723
return NULL;
97039724
}
97049725

@@ -12227,6 +12248,8 @@ int cbm_cmd_update(int argc, char **argv) {
1222712248
(void)fprintf(stderr, " update it with: mise upgrade codebase-memory-mcp\n");
1222812249
} else if (manager && strcmp(manager, "Homebrew") == 0) {
1222912250
(void)fprintf(stderr, " update it with: brew upgrade codebase-memory-mcp\n");
12251+
} else if (manager && strcmp(manager, "FreeBSD pkg") == 0) {
12252+
(void)fprintf(stderr, " update it with: pkg upgrade codebase-memory-mcp\n");
1223012253
} else {
1223112254
(void)fprintf(stderr, " update it through whichever tool installed it.\n");
1223212255
}

‎src/ui/http_server.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
#ifdef __APPLE__
6464
#include <mach-o/dyld.h>
6565
#endif
66+
#ifdef __FreeBSD__
67+
#include <sys/types.h>
68+
#include <sys/sysctl.h>
69+
#endif
6670

6771
/* ── Constants ────────────────────────────────────────────────── */
6872

@@ -1003,6 +1007,15 @@ static bool resolve_self_executable(char *out, size_t outsz) {
10031007
return copy_path(out, outsz, buf);
10041008
}
10051009
return false;
1010+
#elif defined(__FreeBSD__)
1011+
/* No /proc by default on FreeBSD; ask the kernel for our own path. */
1012+
char buf[1024];
1013+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
1014+
size_t cb = sizeof(buf);
1015+
if (sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0) {
1016+
return copy_path(out, outsz, buf);
1017+
}
1018+
return false;
10061019
#else
10071020
char buf[1024];
10081021
ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);

‎tests/test_cli.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12752,8 +12752,16 @@ TEST(cli_external_manager_detection_needs_positive_evidence_issue1566) {
1275212752
ASSERT_NULL(
1275312753
cbm_cli_external_manager_name_for_testing("/Users/x/.local/bin/codebase-memory-mcp"));
1275412754
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/opt/cbm/codebase-memory-mcp"));
12755-
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
1275612755
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("build/c/test-runner"));
12756+
#ifdef __FreeBSD__
12757+
/* On FreeBSD the port/pkg owns ${LOCALBASE}/bin, so a binary there IS
12758+
* externally managed; elsewhere the same path is unremarkable. */
12759+
ASSERT_NOT_NULL(
12760+
cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
12761+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/opt/local/bin/codebase-memory-mcp"));
12762+
#else
12763+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
12764+
#endif
1275712765
ASSERT_NULL(cbm_cli_external_manager_name_for_testing(""));
1275812766
ASSERT_NULL(cbm_cli_external_manager_name_for_testing(NULL));
1275912767
PASS();

0 commit comments

Comments
 (0)