Commit 49af12e7 authored by Diederik de Groot's avatar Diederik de Groot
Browse files

implement: readJailName

Note: dragonflybsd does not have 'jail_get' like freebsd does.
It does however provide a sysctl "jail.list" which returns a list of all jails.
parent b258d6e5
...@@ -19,7 +19,7 @@ in the source distribution for its full text. ...@@ -19,7 +19,7 @@ in the source distribution for its full text.
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <sys/param.h>
/*{ /*{
...@@ -31,6 +31,7 @@ in the source distribution for its full text. ...@@ -31,6 +31,7 @@ in the source distribution for its full text.
#include <sys/jail.h> #include <sys/jail.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/resource.h> #include <sys/resource.h>
#include "Hashtable.h"
#include "DragonFlyBSDProcess.h" #include "DragonFlyBSDProcess.h"
#define JAIL_ERRMSGLEN 1024 #define JAIL_ERRMSGLEN 1024
...@@ -64,6 +65,7 @@ typedef struct DragonFlyBSDProcessList_ { ...@@ -64,6 +65,7 @@ typedef struct DragonFlyBSDProcessList_ {
unsigned long *cp_times_o; unsigned long *cp_times_o;
unsigned long *cp_times_n; unsigned long *cp_times_n;
Hashtable *jails;
} DragonFlyBSDProcessList; } DragonFlyBSDProcessList;
}*/ }*/
...@@ -170,6 +172,9 @@ void ProcessList_delete(ProcessList* this) { ...@@ -170,6 +172,9 @@ void ProcessList_delete(ProcessList* this) {
const DragonFlyBSDProcessList* dfpl = (DragonFlyBSDProcessList*) this; const DragonFlyBSDProcessList* dfpl = (DragonFlyBSDProcessList*) this;
if (dfpl->kd) kvm_close(dfpl->kd); if (dfpl->kd) kvm_close(dfpl->kd);
if (dfpl->jails) {
Hashtable_delete(dfpl->jails);
}
free(dfpl->cp_time_o); free(dfpl->cp_time_o);
free(dfpl->cp_time_n); free(dfpl->cp_time_n);
free(dfpl->cp_times_o); free(dfpl->cp_times_o);
...@@ -336,49 +341,68 @@ char* DragonFlyBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kpro ...@@ -336,49 +341,68 @@ char* DragonFlyBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kpro
return comm; return comm;
} }
char* DragonFlyBSDProcessList_readJailName(struct kinfo_proc* kproc) { static inline void DragonFlyBSDProcessList_scanJails(DragonFlyBSDProcessList* dfpl) {
char* jname; size_t len;
char jnamebuf[MAXHOSTNAMELEN]; char *jls; /* Jail list */
char *curpos;
if (kproc->kp_jailid != 0 ){ char *nextpos;
memset(jnamebuf, 0, sizeof(jnamebuf));
#if 0 /*EXCLUDED*/ if (sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1) {
int jid; fprintf(stderr, "initial sysctlbyname / jail.list failed\n");
struct iovec jiov[6]; exit(3);
*(const void **)&jiov[0].iov_base = "jid"; }
jiov[0].iov_len = sizeof("jid"); retry:
jiov[1].iov_base = &kproc->kp_jailid; if (len == 0)
jiov[1].iov_len = sizeof(kproc->kp_jailid); return;
*(const void **)&jiov[2].iov_base = "name";
jiov[2].iov_len = sizeof("name"); jls = xMalloc(len);
jiov[3].iov_base = jnamebuf; if (jls == NULL) {
jiov[3].iov_len = sizeof(jnamebuf); fprintf(stderr, "xMalloc failed\n");
*(const void **)&jiov[4].iov_base = "errmsg"; exit(4);
jiov[4].iov_len = sizeof("errmsg"); }
jiov[5].iov_base = jail_errmsg; if (sysctlbyname("jail.list", jls, &len, NULL, 0) == -1) {
jiov[5].iov_len = JAIL_ERRMSGLEN; if (errno == ENOMEM) {
jail_errmsg[0] = 0; free(jls);
jid = jail_get(jiov, 6, 0); // not available on dragonfly goto retry;
if (jid < 0) { }
if (!jail_errmsg[0]) fprintf(stderr, "sysctlbyname / jail.list failed\n");
snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); exit(5);
return NULL;
} else if (jid == kproc->kp_jailid) {
jname = xStrdup(jnamebuf);
if (jname == NULL)
strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
return jname;
} else {
return NULL;
} }
#endif
// TODO: need to figure out how to retreive the jail->hostname on dragonfly if (dfpl->jails) {
snprintf(jnamebuf, MAXHOSTNAMELEN, "<jail=%i>", kproc->kp_jailid); Hashtable_delete(dfpl->jails);
jname = xStrdup(jnamebuf); }
dfpl->jails = Hashtable_new(20, true);
curpos = jls;
while (curpos) {
int jailid;
char *str_hostname;
nextpos = strchr(curpos, '\n');
if (nextpos)
*nextpos++ = 0;
jailid = atoi(strtok(curpos, " "));
str_hostname = strtok(NULL, " ");
char *jname = (char *) (Hashtable_get(dfpl->jails, jailid));
if (jname == NULL) {
jname = xStrdup(str_hostname);
Hashtable_put(dfpl->jails, jailid, jname);
}
curpos = nextpos;
}
free(jls);
}
char* DragonFlyBSDProcessList_readJailName(DragonFlyBSDProcessList* dfpl, int jailid) {
char* hostname;
char* jname;
if (jailid != 0 && dfpl->jails && (hostname = (char *)Hashtable_get(dfpl->jails, jailid))) {
jname = xStrdup(hostname);
} else { } else {
jnamebuf[0]='-'; jname = xStrdup("-");
jnamebuf[1]='\0';
jname = xStrdup(jnamebuf);
} }
return jname; return jname;
} }
...@@ -391,6 +415,7 @@ void ProcessList_goThroughEntries(ProcessList* this) { ...@@ -391,6 +415,7 @@ void ProcessList_goThroughEntries(ProcessList* this) {
DragonFlyBSDProcessList_scanMemoryInfo(this); DragonFlyBSDProcessList_scanMemoryInfo(this);
DragonFlyBSDProcessList_scanCPUTime(this); DragonFlyBSDProcessList_scanCPUTime(this);
DragonFlyBSDProcessList_scanJails(dfpl);
int count = 0; int count = 0;
...@@ -431,13 +456,13 @@ void ProcessList_goThroughEntries(ProcessList* this) { ...@@ -431,13 +456,13 @@ void ProcessList_goThroughEntries(ProcessList* this) {
ProcessList_add((ProcessList*)this, proc); ProcessList_add((ProcessList*)this, proc);
proc->comm = DragonFlyBSDProcessList_readProcessName(dfpl->kd, kproc, &proc->basenameOffset); proc->comm = DragonFlyBSDProcessList_readProcessName(dfpl->kd, kproc, &proc->basenameOffset);
dfp->jname = DragonFlyBSDProcessList_readJailName(kproc); dfp->jname = DragonFlyBSDProcessList_readJailName(dfpl, kproc->kp_jailid);
} else { } else {
proc->processor = kproc->kp_lwp.kl_cpuid; proc->processor = kproc->kp_lwp.kl_cpuid;
if(dfp->jid != kproc->kp_jailid) { // process can enter jail anytime if(dfp->jid != kproc->kp_jailid) { // process can enter jail anytime
dfp->jid = kproc->kp_jailid; dfp->jid = kproc->kp_jailid;
free(dfp->jname); free(dfp->jname);
dfp->jname = DragonFlyBSDProcessList_readJailName(kproc); dfp->jname = DragonFlyBSDProcessList_readJailName(dfpl, kproc->kp_jailid);
} }
if (proc->ppid != kproc->kp_ppid) { // if there are reapers in the system, process can get reparented anytime if (proc->ppid != kproc->kp_ppid) { // if there are reapers in the system, process can get reparented anytime
proc->ppid = kproc->kp_ppid; proc->ppid = kproc->kp_ppid;
......
...@@ -19,6 +19,7 @@ in the source distribution for its full text. ...@@ -19,6 +19,7 @@ in the source distribution for its full text.
#include <sys/jail.h> #include <sys/jail.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/resource.h> #include <sys/resource.h>
#include "Hashtable.h"
#include "DragonFlyBSDProcess.h" #include "DragonFlyBSDProcess.h"
#define JAIL_ERRMSGLEN 1024 #define JAIL_ERRMSGLEN 1024
...@@ -52,6 +53,7 @@ typedef struct DragonFlyBSDProcessList_ { ...@@ -52,6 +53,7 @@ typedef struct DragonFlyBSDProcessList_ {
unsigned long *cp_times_o; unsigned long *cp_times_o;
unsigned long *cp_times_n; unsigned long *cp_times_n;
Hashtable *jails;
} DragonFlyBSDProcessList; } DragonFlyBSDProcessList;
...@@ -64,7 +66,7 @@ void ProcessList_delete(ProcessList* this); ...@@ -64,7 +66,7 @@ void ProcessList_delete(ProcessList* this);
char* DragonFlyBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd); char* DragonFlyBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd);
char* DragonFlyBSDProcessList_readJailName(struct kinfo_proc* kproc); char* DragonFlyBSDProcessList_readJailName(DragonFlyBSDProcessList* dfpl, int jailid);
void ProcessList_goThroughEntries(ProcessList* this); void ProcessList_goThroughEntries(ProcessList* this);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment