Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(daemon): add native FreeBSD process-image identity support

Pedro Ramos12b928a74e13
diff --git a/src/daemon/runtime.c b/src/daemon/runtime.c
index bdbb2c56b..4572263f1 100644
--- a/src/daemon/runtime.c
+++ b/src/daemon/runtime.c
@@ -42,10 +42,14 @@ void cbm_daemon_runtime_force_peer_image_unverified_for_testing(bool force) {
#include <sys/proc_info.h>
#include <sys/stat.h>
#include <unistd.h>
-#elif defined(__linux__)
+#elif defined(__linux__) || defined(__FreeBSD__)
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
+#if defined(__FreeBSD__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
#endif
enum {
@@ -147,7 +151,7 @@ typedef struct {
HANDLE file;
BY_HANDLE_FILE_INFORMATION information;
LARGE_INTEGER size;
-#elif defined(__APPLE__) || defined(__linux__)
+#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
int fd;
struct stat status;
#endif
@@ -502,7 +506,7 @@ static bool runtime_activation_response_decode(
static uint64_t runtime_current_process_id(void) {
#ifdef _WIN32
return (uint64_t)GetCurrentProcessId();
-#elif defined(__APPLE__) || defined(__linux__)
+#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
return (uint64_t)getpid();
#else
return 0;
@@ -516,7 +520,7 @@ static void runtime_process_image_reference_init(runtime_process_image_reference
memset(reference, 0, sizeof(*reference));
#ifdef _WIN32
reference->file = INVALID_HANDLE_VALUE;
-#elif defined(__APPLE__) || defined(__linux__)
+#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
reference->fd = -1;
#endif
}
@@ -530,7 +534,7 @@ static bool runtime_process_image_reference_release(runtime_process_image_refere
if (reference->file != INVALID_HANDLE_VALUE && !CloseHandle(reference->file)) {
ok = false;
}
-#elif defined(__APPLE__) || defined(__linux__)
+#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
if (reference->fd >= 0 && close(reference->fd) != 0) {
ok = false;
}
@@ -670,9 +674,9 @@ static bool runtime_mac_process_maps_file_executable(int process_id, const struc
return false;
}
-#elif defined(__linux__)
+#elif defined(__linux__) || defined(__FreeBSD__)
-static bool runtime_linux_stat_same_image(const struct stat *first, const struct stat *second) {
+static bool runtime_posix_stat_same_image(const struct stat *first, const struct stat *second) {
return first && second && S_ISREG(first->st_mode) && S_ISREG(second->st_mode) &&
first->st_dev == second->st_dev && first->st_ino == second->st_ino &&
first->st_size == second->st_size && first->st_mtim.tv_sec == second->st_mtim.tv_sec &&
@@ -788,11 +792,11 @@ static bool runtime_process_image_reference_acquire(
(!fingerprint ||
cbm_daemon_build_fingerprint_native_file((uintptr_t)image_fd, fingerprint)) &&
fstat(image_fd, &image_after) == 0 &&
- runtime_linux_stat_same_image(&image_before, &image_after);
+ runtime_posix_stat_same_image(&image_before, &image_after);
int verify_fd = ok ? openat(process_fd, "exe", O_RDONLY | O_CLOEXEC) : -1;
struct stat verify_status;
ok = ok && verify_fd >= 0 && fstat(verify_fd, &verify_status) == 0 &&
- runtime_linux_stat_same_image(&image_after, &verify_status);
+ runtime_posix_stat_same_image(&image_after, &verify_status);
if (verify_fd >= 0 && close(verify_fd) != 0) {
ok = false;
}
@@ -806,6 +810,33 @@ static bool runtime_process_image_reference_acquire(
} else if (image_fd >= 0) {
(void)close(image_fd);
}
+#elif defined(__FreeBSD__)
+ if (process_id > INT_MAX) {
+ return false;
+ }
+ int pid = (int)process_id;
+ int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid};
+ char path[PATH_MAX];
+ size_t path_length = sizeof(path);
+ bool ok = sysctl(mib, 4, path, &path_length, NULL, 0) == 0 && path_length > 0;
+ if (ok) {
+ path[path_length < sizeof(path) ? path_length : sizeof(path) - 1] = '\0';
+ }
+ int image_fd = ok ? open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK) : -1;
+ struct stat image_before;
+ struct stat image_after;
+ ok = image_fd >= 0 && fstat(image_fd, &image_before) == 0 && S_ISREG(image_before.st_mode) &&
+ (!fingerprint ||
+ cbm_daemon_build_fingerprint_native_file((uintptr_t)image_fd, fingerprint)) &&
+ fstat(image_fd, &image_after) == 0 &&
+ runtime_posix_stat_same_image(&image_before, &image_after);
+ if (ok) {
+ reference->held = true;
+ reference->fd = image_fd;
+ reference->status = image_after;
+ } else if (image_fd >= 0) {
+ (void)close(image_fd);
+ }
#else
(void)process_id;
bool ok = false;
@@ -846,14 +877,14 @@ static bool runtime_process_image_reference_matches_process(
runtime_mac_stat_same(&active->status, &peer.status);
bool released = runtime_process_image_reference_release(&peer);
return same && released;
-#elif defined(__linux__)
+#elif defined(__linux__) || defined(__FreeBSD__)
runtime_process_image_reference_t peer;
runtime_process_image_reference_init(&peer);
bool same = runtime_process_image_reference_acquire(process_id, &peer, NULL);
struct stat active_now;
same = same && fstat(active->fd, &active_now) == 0 &&
- runtime_linux_stat_same_image(&active->status, &active_now) &&
- runtime_linux_stat_same_image(&active->status, &peer.status);
+ runtime_posix_stat_same_image(&active->status, &active_now) &&
+ runtime_posix_stat_same_image(&active->status, &peer.status);
bool released = runtime_process_image_reference_release(&peer);
return same && released;
#else