]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libjail/jail_getid.c
Fix some races and screeen clearing in VGLEnd().
[FreeBSD/FreeBSD.git] / lib / libjail / jail_getid.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 James Gritton.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/jail.h>
35 #include <sys/uio.h>
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "jail.h"
43
44
45 /*
46  * Return the JID corresponding to a jail name.
47  */
48 int
49 jail_getid(const char *name)
50 {
51         char *ep;
52         int jid;
53         struct iovec jiov[4];
54
55         jid = strtoul(name, &ep, 10);
56         if (*name && !*ep)
57                 return jid;
58         jiov[0].iov_base = __DECONST(char *, "name");
59         jiov[0].iov_len = sizeof("name");
60         jiov[1].iov_len = strlen(name) + 1;
61         jiov[1].iov_base = alloca(jiov[1].iov_len);
62         strcpy(jiov[1].iov_base, name);
63         jiov[2].iov_base = __DECONST(char *, "errmsg");
64         jiov[2].iov_len = sizeof("errmsg");
65         jiov[3].iov_base = jail_errmsg;
66         jiov[3].iov_len = JAIL_ERRMSGLEN;
67         jail_errmsg[0] = 0;
68         jid = jail_get(jiov, 4, 0);
69         if (jid < 0 && !jail_errmsg[0])
70                 snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
71                     strerror(errno));
72         return jid;
73 }
74
75 /*
76  * Return the name corresponding to a JID.
77  */
78 char *
79 jail_getname(int jid)
80 {
81         struct iovec jiov[6];
82         char *name;
83         char namebuf[MAXHOSTNAMELEN];
84
85         jiov[0].iov_base = __DECONST(char *, "jid");
86         jiov[0].iov_len = sizeof("jid");
87         jiov[1].iov_base = &jid;
88         jiov[1].iov_len = sizeof(jid);
89         jiov[2].iov_base = __DECONST(char *, "name");
90         jiov[2].iov_len = sizeof("name");
91         jiov[3].iov_base = namebuf;
92         jiov[3].iov_len = sizeof(namebuf);
93         jiov[4].iov_base = __DECONST(char *, "errmsg");
94         jiov[4].iov_len = sizeof("errmsg");
95         jiov[5].iov_base = jail_errmsg;
96         jiov[5].iov_len = JAIL_ERRMSGLEN;
97         jail_errmsg[0] = 0;
98         jid = jail_get(jiov, 6, 0);
99         if (jid < 0) {
100                 if (!jail_errmsg[0])
101                         snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
102                             strerror(errno));
103                 return NULL;
104         } else {
105                 name = strdup(namebuf);
106                 if (name == NULL)
107                         strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
108         }
109         return name;
110 }