Skip to content

Commit f140939

Browse files
committed
fix(netbsd): add NetBSD support to daemon IPC and runtime
Extend Linux codepaths in runtime.c to also cover NetBSD, and add NetBSD-specific peer PID retrieval via LOCAL_PEEREID in ipc.c. Signed-off-by: Christof Meerwald <cmeerw@cmeerw.org>
1 parent 4730fe9 commit f140939

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,14 @@ uint64_t cbm_daemon_ipc_connection_peer_pid(const cbm_daemon_ipc_connection_t *c
30233023
return 0;
30243024
}
30253025
return (uint64_t)peer_pid;
3026+
#elif defined(SOL_LOCAL) && defined(LOCAL_PEEREID)
3027+
struct unpcbid peer_id;
3028+
socklen_t length = sizeof(peer_id);
3029+
if (getsockopt(connection->fd, SOL_LOCAL, LOCAL_PEEREID, &peer_id, &length) != 0 ||
3030+
length != sizeof(peer_id) || peer_id.unp_pid <= 0) {
3031+
return 0;
3032+
}
3033+
return (uint64_t)peer_id.unp_pid;
30263034
#else
30273035
return 0;
30283036
#endif

‎src/daemon/runtime.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <sys/proc_info.h>
3131
#include <sys/stat.h>
3232
#include <unistd.h>
33-
#elif defined(__linux__)
33+
#elif defined(__linux__) || defined(__NetBSD__)
3434
#include <fcntl.h>
3535
#include <sys/stat.h>
3636
#include <unistd.h>
@@ -135,7 +135,7 @@ typedef struct {
135135
HANDLE file;
136136
BY_HANDLE_FILE_INFORMATION information;
137137
LARGE_INTEGER size;
138-
#elif defined(__APPLE__) || defined(__linux__)
138+
#elif defined(__APPLE__) || defined(__linux__) || defined(__NetBSD__)
139139
int fd;
140140
struct stat status;
141141
#endif
@@ -490,7 +490,7 @@ static bool runtime_activation_response_decode(
490490
static uint64_t runtime_current_process_id(void) {
491491
#ifdef _WIN32
492492
return (uint64_t)GetCurrentProcessId();
493-
#elif defined(__APPLE__) || defined(__linux__)
493+
#elif defined(__APPLE__) || defined(__linux__) || defined(__NetBSD__)
494494
return (uint64_t)getpid();
495495
#else
496496
return 0;
@@ -504,7 +504,7 @@ static void runtime_process_image_reference_init(runtime_process_image_reference
504504
memset(reference, 0, sizeof(*reference));
505505
#ifdef _WIN32
506506
reference->file = INVALID_HANDLE_VALUE;
507-
#elif defined(__APPLE__) || defined(__linux__)
507+
#elif defined(__APPLE__) || defined(__linux__) || defined(__NetBSD__)
508508
reference->fd = -1;
509509
#endif
510510
}
@@ -518,7 +518,7 @@ static bool runtime_process_image_reference_release(runtime_process_image_refere
518518
if (reference->file != INVALID_HANDLE_VALUE && !CloseHandle(reference->file)) {
519519
ok = false;
520520
}
521-
#elif defined(__APPLE__) || defined(__linux__)
521+
#elif defined(__APPLE__) || defined(__linux__) || defined(__NetBSD__)
522522
if (reference->fd >= 0 && close(reference->fd) != 0) {
523523
ok = false;
524524
}
@@ -658,7 +658,7 @@ static bool runtime_mac_process_maps_file_executable(int process_id, const struc
658658
return false;
659659
}
660660

661-
#elif defined(__linux__)
661+
#elif defined(__linux__) || defined(__NetBSD__)
662662

663663
static bool runtime_linux_stat_same_image(const struct stat *first, const struct stat *second) {
664664
return first && second && S_ISREG(first->st_mode) && S_ISREG(second->st_mode) &&
@@ -753,7 +753,7 @@ static bool runtime_process_image_reference_acquire(
753753
} else if (fd >= 0) {
754754
(void)close(fd);
755755
}
756-
#elif defined(__linux__)
756+
#elif defined(__linux__) || defined(__NetBSD__)
757757
char proc_path[64];
758758
int written =
759759
snprintf(proc_path, sizeof(proc_path), "/proc/%llu", (unsigned long long)process_id);
@@ -827,7 +827,7 @@ static bool runtime_process_image_reference_matches_process(
827827
runtime_mac_stat_same(&active->status, &peer.status);
828828
bool released = runtime_process_image_reference_release(&peer);
829829
return same && released;
830-
#elif defined(__linux__)
830+
#elif defined(__linux__) || defined(__NetBSD__)
831831
runtime_process_image_reference_t peer;
832832
runtime_process_image_reference_init(&peer);
833833
bool same = runtime_process_image_reference_acquire(process_id, &peer, NULL);

0 commit comments

Comments
 (0)