Skip to content

Commit f899770

Browse files
authored
Merge pull request DeusData#1342 from cmeerw/fix/netbsd-daemon-support
fix(netbsd): add NetBSD support to daemon IPC and runtime
2 parents 2cc80f2 + 3e1703d commit f899770

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int cbm_powershell_quote_word(const char *value, char *out, size_t out_si
107107
#include <stdlib.h>
108108
#include <string.h> // strtok_r
109109
#include <sys/stat.h> // mode_t, S_IXUSR
110-
#ifdef __FreeBSD__
110+
#if defined(__FreeBSD__) || defined(__NetBSD__)
111111
#include <sys/types.h>
112112
#include <sys/sysctl.h>
113113
#endif
@@ -9818,8 +9818,12 @@ static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
98189818
if (!exact) {
98199819
buf[0] = '\0';
98209820
}
9821-
#elif defined(__FreeBSD__)
9821+
#elif defined(__FreeBSD__) || defined(__NetBSD__)
9822+
#if defined(__NetBSD__)
9823+
int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
9824+
#else
98229825
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
9826+
#endif
98239827
size_t cb = buf_sz;
98249828
exact = sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0;
98259829
if (!exact) {

‎src/daemon/ipc.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,14 @@ uint64_t cbm_daemon_ipc_connection_peer_pid(const cbm_daemon_ipc_connection_t *c
30783078
return 0;
30793079
}
30803080
return (uint64_t)peer_pid;
3081+
#elif defined(SOL_LOCAL) && defined(LOCAL_PEEREID)
3082+
struct unpcbid peer_id;
3083+
socklen_t length = sizeof(peer_id);
3084+
if (getsockopt(connection->fd, SOL_LOCAL, LOCAL_PEEREID, &peer_id, &length) != 0 ||
3085+
length != sizeof(peer_id) || peer_id.unp_pid <= 0) {
3086+
return 0;
3087+
}
3088+
return (uint64_t)peer_id.unp_pid;
30813089
#else
30823090
return 0;
30833091
#endif

‎src/daemon/runtime.c‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ void cbm_daemon_runtime_force_peer_image_mismatch_for_testing(bool force) {
5050
#include <sys/proc_info.h>
5151
#include <sys/stat.h>
5252
#include <unistd.h>
53-
#elif defined(__linux__) || defined(__FreeBSD__)
53+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
5454
#include <fcntl.h>
5555
#include <sys/stat.h>
5656
#include <unistd.h>
57-
#if defined(__FreeBSD__)
57+
#if defined(__FreeBSD__) || defined(__NetBSD__)
5858
#include <sys/types.h>
5959
#include <sys/sysctl.h>
6060
#endif
@@ -159,7 +159,7 @@ typedef struct {
159159
HANDLE file;
160160
BY_HANDLE_FILE_INFORMATION information;
161161
LARGE_INTEGER size;
162-
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
162+
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
163163
int fd;
164164
struct stat status;
165165
#endif
@@ -514,7 +514,7 @@ static bool runtime_activation_response_decode(
514514
static uint64_t runtime_current_process_id(void) {
515515
#ifdef _WIN32
516516
return (uint64_t)GetCurrentProcessId();
517-
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
517+
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
518518
return (uint64_t)getpid();
519519
#else
520520
return 0;
@@ -528,7 +528,7 @@ static void runtime_process_image_reference_init(runtime_process_image_reference
528528
memset(reference, 0, sizeof(*reference));
529529
#ifdef _WIN32
530530
reference->file = INVALID_HANDLE_VALUE;
531-
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
531+
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
532532
reference->fd = -1;
533533
#endif
534534
}
@@ -542,7 +542,7 @@ static bool runtime_process_image_reference_release(runtime_process_image_refere
542542
if (reference->file != INVALID_HANDLE_VALUE && !CloseHandle(reference->file)) {
543543
ok = false;
544544
}
545-
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
545+
#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
546546
if (reference->fd >= 0 && close(reference->fd) != 0) {
547547
ok = false;
548548
}
@@ -682,7 +682,7 @@ static bool runtime_mac_process_maps_file_executable(int process_id, const struc
682682
return false;
683683
}
684684

685-
#elif defined(__linux__) || defined(__FreeBSD__)
685+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
686686

687687
static bool runtime_posix_stat_same_image(const struct stat *first, const struct stat *second) {
688688
return first && second && S_ISREG(first->st_mode) && S_ISREG(second->st_mode) &&
@@ -818,12 +818,16 @@ static bool runtime_process_image_reference_acquire(
818818
} else if (image_fd >= 0) {
819819
(void)close(image_fd);
820820
}
821-
#elif defined(__FreeBSD__)
821+
#elif defined(__FreeBSD__) || defined(__NetBSD__)
822822
if (process_id > INT_MAX) {
823823
return false;
824824
}
825825
int pid = (int)process_id;
826+
#if defined(__NetBSD__)
827+
int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_PATHNAME};
828+
#else
826829
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid};
830+
#endif
827831
char path[PATH_MAX];
828832
size_t path_length = sizeof(path);
829833
bool ok = sysctl(mib, 4, path, &path_length, NULL, 0) == 0 && path_length > 0;
@@ -885,7 +889,7 @@ static bool runtime_process_image_reference_matches_process(
885889
runtime_mac_stat_same(&active->status, &peer.status);
886890
bool released = runtime_process_image_reference_release(&peer);
887891
return same && released;
888-
#elif defined(__linux__) || defined(__FreeBSD__)
892+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
889893
runtime_process_image_reference_t peer;
890894
runtime_process_image_reference_init(&peer);
891895
bool same = runtime_process_image_reference_acquire(process_id, &peer, NULL);

0 commit comments

Comments
 (0)