summaryrefslogtreecommitdiff
path: root/srcx/jid.c
blob: 9f48b8b413c1d9ef5b3d912c0f7be85c946e1858 (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

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

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "util.h"

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

static void usage();

int main(int argc, char* argv[])
{
	int jid = 0;
	char* e;

	/* Remove the program name */
	argc--;
	argv++;

	if(argc != 1)
		usage();

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

	jid = strtol(argv[0], &e, 10);

	/* If it was all a number ... */
	if(!*e)
	{
		struct xprison* xp;
		size_t len, i;

		len = get_jail_sysctl(&xp);
		if(len > 0)
		{
			for(i = 0; i < len; i++)
			{
				if(xp[i].pr_id == jid)
				{
					printf("%s\n", xp[i].pr_host);
					jid = -1;
					break;
				}
			}

			free(xp);

			if(jid != -1)
				errx(1, "unknown jail id: %s", argv[0]);
		}
	}

	/* otherwise it's a host name */
	else
	{
		jid = translate_jail_name(argv[0]);
		if(jid == -1)
			errx(1, "unknown jail host name: %s", argv[0]);

		printf("%d\n", (int)jid);
	}

	return 0;
}

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