summaryrefslogtreecommitdiff
path: root/src/jails.c
blob: 9c997439b897365908fe71b443e1a417cad4e665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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);
}