Skip to content

Commit

Permalink
Attempt to "Fix" OpenBSD Build
Browse files Browse the repository at this point in the history
Co-authored-by: Benny Baumann <[email protected]>
  • Loading branch information
time-killer-games and BenBE committed Jan 6, 2024
1 parent 278ebbb commit f2a8005
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 51 deletions.
10 changes: 6 additions & 4 deletions openbsd/OpenBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "openbsd/OpenBSDMachine.h"

#include <kvm.h>
Expand Down Expand Up @@ -96,10 +98,10 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {

Machine_init(super, usersTable, userId);

OpenBSDProcessList_updateCPUcount(this);
OpenBSDProcessTable_updateCPUcount(this);

size = sizeof(this->fscale);
if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0) {
if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0 || this->fscale <= 0) {
CRT_fatalError("fscale sysctl call failed");
}

Expand Down Expand Up @@ -129,7 +131,7 @@ void Machine_delete(Machine* super) {
}

static void OpenBSDMachine_scanMemoryInfo(OpenBSDMachine* this) {
Machine* host = &this->super;
Machine* super = &this->super;
const int uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
struct uvmexp uvmexp;
size_t size_uvmexp = sizeof(uvmexp);
Expand Down Expand Up @@ -230,7 +232,7 @@ static void kernelCPUTimesToHtop(const u_int64_t* times, CPUData* cpu) {
}

static void OpenBSDMachine_scanCPUTime(OpenBSDMachine* this) {
Machine* host = &this->super;
Machine* super = &this->super;
u_int64_t kernelTimes[CPUSTATES] = {0};
u_int64_t avg[CPUSTATES] = {0};

Expand Down
30 changes: 21 additions & 9 deletions openbsd/OpenBSDProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "openbsd/OpenBSDProcess.h"

#include <stdlib.h>
Expand Down Expand Up @@ -217,17 +219,20 @@ void Process_delete(Object* cast) {
free(this);
}

static void OpenBSDProcess_writeField(const Process* this, RichString* str, ProcessField field) {
//const OpenBSDProcess* op = (const OpenBSDProcess*) this;
static void OpenBSDProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
const OpenBSDProcess* op = (const OpenBSDProcess*) super;

char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR];
//int n = sizeof(buffer) - 1;
//size_t n = sizeof(buffer) - 1;

switch (field) {
// add OpenBSD-specific fields here
default:
Process_writeField(this, str, field);
Process_writeField(&op->super, str, field);
return;
}

RichString_appendWide(str, attr, buffer);
}

Expand All @@ -247,11 +252,18 @@ static int OpenBSDProcess_compareByKey(const Process* v1, const Process* v2, Pro

const ProcessClass OpenBSDProcess_class = {
.super = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = Process_compare
.super = {
.extends = Class(Process),
.display = Row_display,
.delete = Process_delete,
.compare = Process_compare
},
.isHighlighted = Process_rowIsHighlighted,
.isVisible = Process_rowIsVisible,
.matchesFilter = Process_rowMatchesFilter,
.compareByParent = Process_compareByParent,
.sortKeyString = Process_rowGetSortKey,
.writeField = OpenBSDProcess_rowWriteField
},
.writeField = OpenBSDProcess_writeField,
.compareByKey = OpenBSDProcess_compareByKey
};
56 changes: 29 additions & 27 deletions openbsd/OpenBSDProcessList.c → openbsd/OpenBSDProcessTable.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/*
htop - OpenBSDProcessList.c
htop - OpenBSDProcessTable.c
(C) 2014 Hisham H. Muhammad
(C) 2015 Michael McConville
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "openbsd/OpenBSDProcessList.h"
#include "config.h" // IWYU pragma: keep

#include "openbsd/OpenBSDProcessTable.h"

#include <kvm.h>
#include <limits.h>
Expand All @@ -25,30 +27,30 @@ in the source distribution for its full text.
#include "Macros.h"
#include "Object.h"
#include "Process.h"
#include "ProcessList.h"
#include "ProcessTable.h"
#include "Settings.h"
#include "XUtils.h"
#include "openbsd/OpenBSDMachine.h"
#include "openbsd/OpenBSDProcess.h"


ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList) {
OpenBSDProcessList* this = xCalloc(1, sizeof(OpenBSDProcessList));
ProcessList* super = (ProcessList*) this;
ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
OpenBSDProcessTable* this = xCalloc(1, sizeof(OpenBSDProcessTable));
Object_setClass(this, Class(ProcessTable));

ProcessList_init(super, Class(OpenBSDProcess), host, pidMatchList);
ProcessTable* super = &this->super;
ProcessTable_init(super, Class(OpenBSDProcess), host, pidMatchList);

return this;
}

void ProcessList_delete(ProcessList* super) {
OpenBSDProcessList* this = (OpenBSDProcessList*) super;

ProcessList_done(super);
void ProcessTable_delete(Object* cast) {
OpenBSDProcessTable* this = (OpenBSDProcessTable*) super;
ProcessTable_done(&this->super);
free(this);
}

static void OpenBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
static void OpenBSDProcessTable_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
const int mib[] = { CTL_KERN, KERN_PROC_CWD, kproc->p_pid };
char buffer[2048];
size_t size = sizeof(buffer);
Expand All @@ -68,7 +70,7 @@ static void OpenBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process
free_and_xStrdup(&proc->procCwd, buffer);
}

static void OpenBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
static void OpenBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
Process_updateComm(proc, kproc->p_comm);

/*
Expand Down Expand Up @@ -127,7 +129,7 @@ static double getpcpu(const OpenBSDMachine* ohost, const struct kinfo_proc* kp)
return 100.0 * (double)kp->p_pctcpu / ohost->fscale;
}

static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
static void OpenBSDProcessTable_scanProcs(OpenBSDProcessTable* this) {
Machine* host = this->super.host;
OpenBSDMachine* ohost = (OpenBSDMachine*) host;
const Settings* settings = host->settings;
Expand All @@ -142,7 +144,7 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {

/* Ignore main threads */
if (kproc->p_tid != -1) {
Process* containingProcess = ProcessList_findProcess(&this->super, kproc->p_pid);
Process* containingProcess = ProcessTable_findProcess(&this->super, kproc->p_pid);
if (containingProcess) {
if (((OpenBSDProcess*)containingProcess)->addr == kproc->p_addr)
continue;
Expand All @@ -152,25 +154,25 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
}

bool preExisting = false;
Process* proc = ProcessList_getProcess(&this->super, (kproc->p_tid == -1) ? kproc->p_pid : kproc->p_tid, &preExisting, OpenBSDProcess_new);
Process* proc = ProcessTable_getProcess(&this->super, (kproc->p_tid == -1) ? kproc->p_pid : kproc->p_tid, &preExisting, OpenBSDProcess_new);
OpenBSDProcess* op = (OpenBSDProcess*) proc;

if (!preExisting) {
proc->ppid = kproc->p_ppid;
Process_setParent(proc, kproc->p_ppid);
Process_setThreadGroup(proc, kproc->p_pid);
proc->tpgid = kproc->p_tpgid;
proc->tgid = kproc->p_pid;
proc->session = kproc->p_sid;
proc->pgrp = kproc->p__pgid;
proc->isKernelThread = proc->pgrp == 0;
proc->isUserlandThread = kproc->p_tid != -1;
proc->starttime_ctime = kproc->p_ustart_sec;
Process_fillStarttimeBuffer(proc);
ProcessList_add(&this->super, proc);
ProcessTable_add(&this->super, proc);

OpenBSDProcessList_updateProcessName(ohost->kd, kproc, proc);
OpenBSDProcessTable_updateProcessName(ohost->kd, kproc, proc);

if (settings->ss->flags & PROCESS_FLAG_CWD) {
OpenBSDProcessList_updateCwd(kproc, proc);
OpenBSDProcessTable_updateCwd(kproc, proc);
}

proc->tty_nr = kproc->p_tdev;
Expand All @@ -183,7 +185,7 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
}
} else {
if (settings->updateProcessNames) {
OpenBSDProcessList_updateProcessName(ohost->kd, kproc, proc);
OpenBSDProcessTable_updateProcessName(ohost->kd, kproc, proc);
}
}

Expand Down Expand Up @@ -231,13 +233,13 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
this->super.runningTasks++;
}

proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
proc->updated = true;
proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
proc->super.updated = true;
}
}

void ProcessList_goThroughEntries(ProcessList* super) {
OpenBSDProcessList* this = (OpenBSDProcessList*) super;
void ProcessTable_goThroughEntries(ProcessTable* super) {
OpenBSDProcessTable* this = (OpenBSDProcessTable*) super;

OpenBSDProcessList_scanProcs(this);
OpenBSDProcessTable_scanProcs(this);
}
14 changes: 7 additions & 7 deletions openbsd/OpenBSDProcessList.h → openbsd/OpenBSDProcessTable.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef HEADER_OpenBSDProcessList
#define HEADER_OpenBSDProcessList
#ifndef HEADER_OpenBSDProcessTable
#define HEADER_OpenBSDProcessTable
/*
htop - OpenBSDProcessList.h
htop - OpenBSDProcessTable.h
(C) 2014 Hisham H. Muhammad
(C) 2015 Michael McConville
Released under the GNU GPLv2+, see the COPYING file
Expand All @@ -11,11 +11,11 @@ in the source distribution for its full text.
#include <stdbool.h>
#include <sys/types.h>

#include "ProcessList.h"
#include "ProcessTable.h"


typedef struct OpenBSDProcessList_ {
ProcessList super;
} OpenBSDProcessList;
typedef struct OpenBSDProcessTable_ {
ProcessTable super;
} OpenBSDProcessTable;

#endif
6 changes: 4 additions & 2 deletions openbsd/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "openbsd/Platform.h"

#include <errno.h>
Expand Down Expand Up @@ -175,7 +177,7 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
*fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale;
}

int Platform_getMaxPid(void) {
pid_t Platform_getMaxPid(void) {
return 2 * THREAD_PID_OFFSET;
}

Expand Down Expand Up @@ -229,9 +231,9 @@ void Platform_setMemoryValues(Meter* this) {
usedMem -= buffersMem + cachedMem;
this->total = host->totalMem;
this->values[MEMORY_METER_USED] = usedMem;
this->values[MEMORY_METER_BUFFERS] = buffersMem;
// this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
// this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
this->values[MEMORY_METER_BUFFERS] = buffersMem;
this->values[MEMORY_METER_CACHE] = cachedMem;
// this->values[MEMORY_METER_AVAILABLE] = "available memory"
}
Expand Down
16 changes: 14 additions & 2 deletions openbsd/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int Platform_getUptime(void);

void Platform_getLoadAverage(double* one, double* five, double* fifteen);

int Platform_getMaxPid(void);
pid_t Platform_getMaxPid(void);

double Platform_setCPUValues(Meter* this, unsigned int cpu);

Expand Down Expand Up @@ -109,12 +109,24 @@ static inline Hashtable* Platform_dynamicColumns(void) {

static inline void Platform_dynamicColumnsDone(ATTR_UNUSED Hashtable* table) { }

static inline const char* Platform_dynamicColumnInit(ATTR_UNUSED unsigned int key) {
static inline const char* Platform_dynamicColumnName(ATTR_UNUSED unsigned int key) {
return NULL;
}

static inline bool Platform_dynamicColumnWriteField(ATTR_UNUSED const Process* proc, ATTR_UNUSED RichString* str, ATTR_UNUSED unsigned int key) {
return false;
}

static inline Hashtable* Platform_dynamicScreens(void) {
return NULL;
}

static inline void Platform_defaultDynamicScreens(ATTR_UNUSED Settings* settings) { }

static inline void Platform_addDynamicScreen(ATTR_UNUSED ScreenSettings* ss) { }

static inline void Platform_addDynamicScreenAvailableColumns(ATTR_UNUSED Panel* availableColumns, ATTR_UNUSED const char* screen) { }

static inline void Platform_dynamicScreensDone(ATTR_UNUSED Hashtable* screens) { }

#endif

0 comments on commit f2a8005

Please sign in to comment.