diff options
author | Stef Walter <stef@memberwebs.com> | 2003-12-02 17:20:39 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2003-12-02 17:20:39 +0000 |
commit | 58c696e88cfeb6d97d836b9f328d683b0e187d65 (patch) | |
tree | 1b1a15cdc8cb13fa2ba8d25f0bd4a165524e79db /src/jails.c | |
parent | f38c902303a704db26f8632e6e582af5d65f0a50 (diff) |
Initial Import
Diffstat (limited to 'src/jails.c')
-rw-r--r-- | src/jails.c | 157 |
1 files changed, 157 insertions, 0 deletions
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: <nielsen@memberwebs.com> +// +// CHANGES +// 0.5.1 kvm_openfiles call uses /dev/null so non root users can use +*/ + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/param.h> +#include <sys/sysctl.h> +#include <sys/proc.h> +#include <sys/user.h> + +#include <paths.h> +#include <stdio.h> +#include <err.h> +#include <errno.h> +#include <kvm.h> +#include <limits.h> +#include <fcntl.h> + +#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); +} + |