2 * Copyright (c) 2002-2004 Marko Zec <zec@fer.hr>
3 * Copyright (c) 2009 University of Zagreb
4 * Copyright (c) 2009 FreeBSD Foundation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
34 #include <sys/socket.h>
45 #define VI_CREATE 0x00000001
46 #define VI_DESTROY 0x00000002
47 #define VI_SWITCHTO 0x00000008
48 #define VI_IFACE 0x00000010
49 #define VI_GET 0x00000100
50 #define VI_GETNEXT 0x00000200
52 static int getjail(char *name, int lastjid, int *vnet);
55 main(int argc, char **argv)
62 char name[MAXHOSTNAMELEN];
71 if (strcmp(argv[1], "-l") == 0)
73 else if (strcmp(argv[1], "-lr") == 0)
76 strcpy(name, argv[1]);
82 strcpy(name, argv[2]);
83 if (strcmp(argv[1], "-l") == 0)
85 if (strcmp(argv[1], "-c") == 0)
87 if (strcmp(argv[1], "-d") == 0)
92 strcpy(name, argv[2]);
93 if (strcmp(argv[1], "-c") == 0)
95 if (strcmp(argv[1], "-i") == 0)
102 jid = getjail(name, -1, &vnet);
105 printf("%d: %s%s\n", jid, name, vnet ? "" : " (no vnet)");
110 while ((jid = getjail(name, jid, &vnet)) > 0)
111 printf("%d: %s%s\n", jid, name,
112 vnet ? "" : " (no vnet)");
116 s = socket(AF_INET, SOCK_DGRAM, 0);
119 jid = jail_getid(name);
123 strncpy(ifreq.ifr_name, argv[3], sizeof(ifreq.ifr_name));
124 if (ioctl(s, SIOCSIFVNET, (caddr_t)&ifreq) < 0)
126 printf("%s@%s\n", ifreq.ifr_name, name);
130 if (jail_setv(JAIL_CREATE, "name", name, "vnet", NULL,
131 "host", NULL, "persist", NULL, NULL) < 0)
136 jid = jail_getid(name);
139 if (jail_attach(jid) < 0)
143 printf("Switched to jail %s\n", argv[1]);
144 if ((shell = getenv("SHELL")) == NULL)
145 execlp("/bin/sh", argv[0], NULL);
147 execlp(shell, argv[0], NULL);
149 execvp(argv[2], &argv[2]);
153 jid = jail_getid(name);
156 if (jail_remove(jid) < 0)
161 fprintf(stderr, "usage: %s [-cdilr] vi_name [args]\n",
168 fprintf(stderr, "Error: %s\n", jail_errmsg);
175 getjail(char *name, int lastjid, int *vnet)
177 struct jailparam params[3];
181 jid = jail_getid(name);
184 jailparam_init(¶ms[0], "jid");
185 jailparam_import_raw(¶ms[0], &jid, sizeof jid);
187 jailparam_init(¶ms[0], "lastjid");
188 jailparam_import_raw(¶ms[0], &lastjid, sizeof lastjid);
190 jailparam_init(¶ms[1], "name");
191 jailparam_import_raw(¶ms[1], name, MAXHOSTNAMELEN);
193 jailparam_init(¶ms[2], "vnet");
194 jailparam_import_raw(¶ms[2], vnet, sizeof(*vnet));
195 jid = jailparam_get(params, 3, 0);
196 jailparam_free(params, 3);