From 58c696e88cfeb6d97d836b9f328d683b0e187d65 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 2 Dec 2003 17:20:39 +0000 Subject: Initial Import --- src/jails.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 src/jails.c (limited to 'src/jails.c') diff --git a/src/jails.c b/src/jails.c new file mode 100644 index 0000000..9c99743 --- /dev/null +++ b/src/jails.c @@ -0,0 +1,157 @@ +/* +// AUTHOR +// N. Nielsen +// +// LICENSE +// This software is in the public domain. +// +// The software is provided "as is", without warranty of any kind, +// express or implied, including but not limited to the warranties +// of merchantability, fitness for a particular purpose, and +// noninfringement. In no event shall the author(s) be liable for any +// claim, damages, or other liability, whether in an action of +// contract, tort, or otherwise, arising from, out of, or in connection +// with the software or the use or other dealings in the software. +// +// SUPPORT +// Send bug reports to: +// +// CHANGES +// 0.5.1 kvm_openfiles call uses /dev/null so non root users can use +*/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "getjail.h" + +#ifdef HAVE_CONFIG_H +#include "../config.h" +#endif + +static int listJails(); +static void usage(); + +int main(int argc, char* argv[]) +{ + /* Nice main :) */ + return listJails() >= 0 ? 0 : 1; +} + + +int listJails() +{ + kvm_t *kd; + struct kinfo_proc* kp; + char errbuf[_POSIX2_LINE_MAX]; + char jailName[JAIL_BUFF_SIZE]; + int nentries, i; + + + char* jails = NULL; /* jail list buffer */ + size_t nextJail = 0; /* current write positon */ + size_t endJails = 0; /* size of buffer */ + int numJails = 0; /* return value */ + + + /* Open kernel interface */ + kd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, _PATH_DEVNULL, + O_RDONLY, errbuf); + if(kd == 0) + errx(1, "%s", errbuf); + + /* Get a process listing */ + if((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == 0) + errx(1, "%s", kvm_geterr(kd)); + + + /* Okay now loop through and look for the jails */ + for(i = 0; i < nentries; i++) + { + pid_t pid; + +#if __FreeBSD_version > 500000 + + /* Check the flags first */ + if(!(kp[i].ki_flag & P_JAILED)) + continue; + + pid = kp[i].ki_pid; + +#else + + /* Check the flags first */ + if(!(kp[i].kp_proc.p_flag & P_JAILED)) + continue; + + pid = kp[i].kp_proc.p_pid; + +#endif + + /* Get this processes jail name */ + if(getpidjail(pid, jailName) < 0) + continue; + + if(jails) + { + char* j; + + /* See if that jail name was already taken */ + for(j = jails; *j != NULL; j += strlen(j) + 1) + { + if(!strcmp(jailName, j)) + break; + } + + /* Can't do this in loop above */ + if(*j) + continue; + } + + + /* Okay we got a jail name */ + + + /* Allocate if necessary enough space for it */ + if(nextJail + strlen(jailName) + 2 > endJails) + { + endJails += (JAIL_BUFF_SIZE * 0x2); + jails = (char*)realloc(jails, endJails); + if(!jails) + errx(1, "out of memory"); + } + + + /* Put the jail name in the buffer */ + strcpy(jails + nextJail, jailName); + nextJail += strlen(jailName) + 1; + jails[nextJail] = 0; + numJails++; + + /* And print it */ + fprintf(stdout, "%s\n", jailName); + } + + kvm_close(kd); + + return numJails; +} + +static void usage() +{ + fprintf(stderr, "usage: jails\n"); + exit(2); +} + -- cgit v1.2.3