summaryrefslogtreecommitdiff
path: root/srcx/jails.c
blob: ed6b353e07b4c1bf27d553bb707b4562ef2393af (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

#include <sys/types.h>
#include <sys/param.h>
#include <sys/jail.h>
#include <sys/sysctl.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <kvm.h>
#include <paths.h>
#include <errno.h>

#include "util.h"

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

static void usage();
static void list_jails();

int main(int argc, char* argv[])
{
	if(argc > 1)
		usage();

	if(running_in_jail())
		errx(1, "can't run from inside jail");

	list_jails();
	return 0;
}

static void usage()
{
	fprintf(stderr, "usage: jails \n");
	exit(2);
}

static void list_jails()
{
	struct xprison* sxp;
	struct xprison* xp;
	size_t len, i;

	/* ... otherwise it's a name */

  if(sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1)
  	err(1, "couldn't list jails");

retry:

	if(len <= 0)
		return;

	sxp = xp = calloc(len, 1);
	if(sxp == NULL)
		err(1, "out of memory");

	if(sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1)
	{
		if(errno == ENOMEM)
		{
			free(sxp);
			goto retry;
		}

		err(1, "couldn't list jails");
	}

	if(len < sizeof(*xp) || len % sizeof(*xp) || xp->pr_version != XPRISON_VERSION)
		errx(1, "kernel and userland out of sync");

	for(i = 0; i < (len / sizeof(*xp)); i++)
		printf("%s\n", xp[i].pr_host);

	free(sxp);
}