jls.c revision 149276
197403Sobrien/*-
297403Sobrien * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3169691Skan * All rights reserved.
4169691Skan *
597403Sobrien * Redistribution and use in source and binary forms, with or without
697403Sobrien * modification, are permitted provided that the following conditions
797403Sobrien * are met:
897403Sobrien * 1. Redistributions of source code must retain the above copyright
997403Sobrien *    notice, this list of conditions and the following disclaimer.
1097403Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1197403Sobrien *    notice, this list of conditions and the following disclaimer in the
1297403Sobrien *    documentation and/or other materials provided with the distribution.
1397403Sobrien *
1497403Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1597403Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1697403Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1797403Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1897403Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2097403Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2197403Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2297403Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2397403Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2497403Sobrien * SUCH DAMAGE.
2597403Sobrien *
2697403Sobrien * $FreeBSD: head/usr.sbin/jls/jls.c 149276 2005-08-19 11:03:49Z pjd $
2797403Sobrien */
2897403Sobrien
2997403Sobrien#include <sys/param.h>
3097403Sobrien#include <sys/jail.h>
31169691Skan#include <sys/sysctl.h>
32169691Skan
33169691Skan#include <arpa/inet.h>
34169691Skan#include <err.h>
35169691Skan#include <errno.h>
3697403Sobrien#include <limits.h>
3797403Sobrien#include <stdio.h>
3897403Sobrien#include <stdlib.h>
3997403Sobrien
40169691Skan
4197403Sobrienint
42132720Skanmain(void)
43132720Skan{
4497403Sobrien	struct xprison *sxp, *xp;
4597403Sobrien	struct in_addr in;
4697403Sobrien	size_t i, len;
47169691Skan
48169691Skan	if (sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1)
49169691Skan		err(1, "sysctlbyname(): security.jail.list");
5097403Sobrien
5197403Sobrien	for (i = 0; i < 4; i++) {
5297403Sobrien		if (len <= 0)
5397403Sobrien			exit(0);
5497403Sobrien		sxp = xp = malloc(len);
5597403Sobrien		if (sxp == NULL)
5697403Sobrien			err(1, "malloc()");
5797403Sobrien
5897403Sobrien		if (sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1) {
5997403Sobrien			if (errno == ENOMEM) {
6097403Sobrien				free(sxp);
6197403Sobrien				sxp = NULL;
62132720Skan				continue;
63169691Skan			}
64132720Skan			err(1, "sysctlbyname(): security.jail.list");
65132720Skan		}
66132720Skan		break;
67132720Skan	}
68132720Skan	if (sxp == NULL)
69132720Skan		err(1, "sysctlbyname(): security.jail.list");
70132720Skan	if (len < sizeof(*xp) || len % sizeof(*xp) ||
7197403Sobrien	    xp->pr_version != XPRISON_VERSION)
72132720Skan		errx(1, "Kernel and userland out of sync");
7397403Sobrien
7497403Sobrien	printf("   JID  IP Address      Hostname                      Path\n");
7597403Sobrien	for (i = 0; i < len / sizeof(*xp); i++) {
7697403Sobrien		in.s_addr = ntohl(xp->pr_ip);
77103447Skan		printf("%6d  %-15.15s %-29.29s %.74s\n",
78132720Skan		    xp->pr_id, inet_ntoa(in), xp->pr_host, xp->pr_path);
79132720Skan		xp++;
80132720Skan	}
81132720Skan	free(sxp);
8297403Sobrien	exit(0);
83132720Skan}
84132720Skan