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.
This commit is contained in:
Diederik de Groot 2017-04-20 15:14:33 +02:00
parent b258d6e53e
commit 49af12e7c6
2 changed files with 71 additions and 44 deletions

View File

@ -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.
#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_ {
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) {
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
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;
char *nextpos;
if (kproc->kp_jailid != 0 ){ if (sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1) {
memset(jnamebuf, 0, sizeof(jnamebuf)); fprintf(stderr, "initial sysctlbyname / jail.list failed\n");
#if 0 /*EXCLUDED*/ exit(3);
int jid; }
struct iovec jiov[6]; retry:
*(const void **)&jiov[0].iov_base = "jid"; if (len == 0)
jiov[0].iov_len = sizeof("jid"); return;
jiov[1].iov_base = &kproc->kp_jailid;
jiov[1].iov_len = sizeof(kproc->kp_jailid); jls = xMalloc(len);
*(const void **)&jiov[2].iov_base = "name"; if (jls == NULL) {
jiov[2].iov_len = sizeof("name"); fprintf(stderr, "xMalloc failed\n");
jiov[3].iov_base = jnamebuf; exit(4);
jiov[3].iov_len = sizeof(jnamebuf); }
*(const void **)&jiov[4].iov_base = "errmsg"; if (sysctlbyname("jail.list", jls, &len, NULL, 0) == -1) {
jiov[4].iov_len = sizeof("errmsg"); if (errno == ENOMEM) {
jiov[5].iov_base = jail_errmsg; free(jls);
jiov[5].iov_len = JAIL_ERRMSGLEN; goto retry;
jail_errmsg[0] = 0;
jid = jail_get(jiov, 6, 0); // not available on dragonfly
if (jid < 0) {
if (!jail_errmsg[0])
snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno));
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 fprintf(stderr, "sysctlbyname / jail.list failed\n");
// TODO: need to figure out how to retreive the jail->hostname on dragonfly exit(5);
snprintf(jnamebuf, MAXHOSTNAMELEN, "<jail=%i>", kproc->kp_jailid); }
jname = xStrdup(jnamebuf);
if (dfpl->jails) {
Hashtable_delete(dfpl->jails);
}
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) {
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) {
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;

View File

@ -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_ {
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);
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);