Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Add support for OpenBSD #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions process_openbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// +build openbsd

package ps

// #cgo LDFLAGS: -lkvm
// #include "process_openbsd.h"
import "C"

var openbsdProcs []Process

func findProcess(pid int) (Process, error) {
ps, err := processes()
if err != nil {
return nil, err
}

for _, p := range ps {
if p.Pid() == pid {
return p, nil
}
}

return nil, nil
}

type OpenBSDProcess struct {
pid int
ppid int
binary string
}

func newOpenBSDProcess() *OpenBSDProcess {
return &OpenBSDProcess{}
}

func (p *OpenBSDProcess) Pid() int {
return p.pid
}

func (p *OpenBSDProcess) PPid() int {
return p.ppid
}

func (p *OpenBSDProcess) Executable() string {
return p.binary
}

//export go_openbsd_append_proc
func go_openbsd_append_proc(pid C.pid_t, ppid C.pid_t, comm *C.char) {
proc := &OpenBSDProcess{
pid: int(pid),
ppid: int(ppid),
binary: C.GoString(comm),
}

openbsdProcs = append(openbsdProcs, proc)
}

func processes() ([]Process, error) {
openbsdProcs = make([]Process, 0, 50)

_, err := C.openbsdProcesses()
if err != nil {
return nil, err
}

return openbsdProcs, nil
}
31 changes: 31 additions & 0 deletions process_openbsd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// +build openbsd

#include <errno.h>
#include <stdio.h>
#include <kvm.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/sysctl.h>

extern void go_openbsd_append_proc(pid_t, pid_t, char *);

static inline int openbsdProcesses() {
int nentries = 0;
int i = 0;
char errbuf[_POSIX2_LINE_MAX];
struct kinfo_proc *result = NULL;
kvm_t *kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
if (!kernel) {
return errno;
}
result = kvm_getprocs(kernel, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nentries);
for (i = 0; i < nentries; i++) {
struct kinfo_proc *single = &result[i];
go_openbsd_append_proc(
single->p_pid,
single->p_ppid,
single->p_comm);
}

return 0;
}