Skip to content

Commit c38aa35

Browse files
authored
Merge pull request DeusData#1777 from ocochard/feature/freebsd-pkg-managed-detection
fix(freebsd): treat ${LOCALBASE}/bin as pkg-managed; resolve self via sysctl
2 parents 192f296 + 7fedaba commit c38aa35

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
@@ -9925,6 +9925,27 @@ static const char *cli_external_manager_name(const char *self_path) {
99259925
if (strstr(self_path, "/.cargo/bin/")) {
99269926
return "cargo";
99279927
}
9928+
#ifdef __FreeBSD__
9929+
/* FreeBSD ports/pkg install under ${LOCALBASE} (default /usr/local). pkg owns
9930+
* that file, so install must not drop a second copy in ~/.local/bin or edit a
9931+
* shell rc, and update must defer to pkg(8). Anchor at the start so a manual
9932+
* `install --dir=/opt/...` elsewhere is still treated as ours; --force-binary
9933+
* is the escape hatch for anyone who really does self-manage that prefix.
9934+
*
9935+
* LOCALBASE is configurable, so the port passes its real PREFIX via
9936+
* -DCBM_PKG_PREFIX; when it is absent we fall back to the documented default. */
9937+
#ifdef CBM_PKG_PREFIX
9938+
if (strncmp(self_path, CBM_PKG_PREFIX "/bin/", sizeof(CBM_PKG_PREFIX "/bin/") - 1) == 0 ||
9939+
strncmp(self_path, CBM_PKG_PREFIX "/sbin/", sizeof(CBM_PKG_PREFIX "/sbin/") - 1) == 0) {
9940+
return "FreeBSD pkg";
9941+
}
9942+
#else
9943+
if (strncmp(self_path, "/usr/local/bin/", 15) == 0 ||
9944+
strncmp(self_path, "/usr/local/sbin/", 16) == 0) {
9945+
return "FreeBSD pkg";
9946+
}
9947+
#endif
9948+
#endif
99289949
return NULL;
99299950
}
99309951

@@ -12508,6 +12529,8 @@ int cbm_cmd_update(int argc, char **argv) {
1250812529
(void)fprintf(stderr, " update it with: mise upgrade codebase-memory-mcp\n");
1250912530
} else if (manager && strcmp(manager, "Homebrew") == 0) {
1251012531
(void)fprintf(stderr, " update it with: brew upgrade codebase-memory-mcp\n");
12532+
} else if (manager && strcmp(manager, "FreeBSD pkg") == 0) {
12533+
(void)fprintf(stderr, " update it with: pkg upgrade codebase-memory-mcp\n");
1251112534
} else {
1251212535
(void)fprintf(stderr, " update it through whichever tool installed it.\n");
1251312536
}

‎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
@@ -13481,8 +13481,16 @@ TEST(cli_external_manager_detection_needs_positive_evidence_issue1566) {
1348113481
ASSERT_NULL(
1348213482
cbm_cli_external_manager_name_for_testing("/Users/x/.local/bin/codebase-memory-mcp"));
1348313483
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/opt/cbm/codebase-memory-mcp"));
13484-
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
1348513484
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("build/c/test-runner"));
13485+
#ifdef __FreeBSD__
13486+
/* On FreeBSD the port/pkg owns ${LOCALBASE}/bin, so a binary there IS
13487+
* externally managed; elsewhere the same path is unremarkable. */
13488+
ASSERT_NOT_NULL(
13489+
cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
13490+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/opt/local/bin/codebase-memory-mcp"));
13491+
#else
13492+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
13493+
#endif
1348613494
ASSERT_NULL(cbm_cli_external_manager_name_for_testing(""));
1348713495
ASSERT_NULL(cbm_cli_external_manager_name_for_testing(NULL));
1348813496
PASS();

0 commit comments

Comments
 (0)