2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 James Gritton.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
46 * Return the JID corresponding to a jail name.
49 jail_getid(const char *name)
55 jid = strtoul(name, &ep, 10);
58 * jid == 0 is a special case; it will not appear in the
59 * kernel's jail list, but naturally processes will be assigned
60 * to it because it is prison 0. Trivially return this one
61 * without a trip to the kernel, because it always exists but
62 * the lookup won't succeed.
66 jiov[0].iov_base = __DECONST(char *, "jid");
67 jiov[0].iov_len = sizeof("jid");
68 jiov[1].iov_base = &jid;
69 jiov[1].iov_len = sizeof(jid);
71 jiov[0].iov_base = __DECONST(char *, "name");
72 jiov[0].iov_len = sizeof("name");
73 jiov[1].iov_len = strlen(name) + 1;
74 jiov[1].iov_base = alloca(jiov[1].iov_len);
75 strcpy(jiov[1].iov_base, name);
77 jiov[2].iov_base = __DECONST(char *, "errmsg");
78 jiov[2].iov_len = sizeof("errmsg");
79 jiov[3].iov_base = jail_errmsg;
80 jiov[3].iov_len = JAIL_ERRMSGLEN;
82 jid = jail_get(jiov, 4, 0);
83 if (jid < 0 && !jail_errmsg[0])
84 snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
90 * Return the name corresponding to a JID.
97 char namebuf[MAXHOSTNAMELEN];
99 jiov[0].iov_base = __DECONST(char *, "jid");
100 jiov[0].iov_len = sizeof("jid");
101 jiov[1].iov_base = &jid;
102 jiov[1].iov_len = sizeof(jid);
103 jiov[2].iov_base = __DECONST(char *, "name");
104 jiov[2].iov_len = sizeof("name");
105 jiov[3].iov_base = namebuf;
106 jiov[3].iov_len = sizeof(namebuf);
107 jiov[4].iov_base = __DECONST(char *, "errmsg");
108 jiov[4].iov_len = sizeof("errmsg");
109 jiov[5].iov_base = jail_errmsg;
110 jiov[5].iov_len = JAIL_ERRMSGLEN;
112 jid = jail_get(jiov, 6, 0);
115 snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
119 name = strdup(namebuf);
121 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);