xref: /titanic_50/usr/src/lib/libc/port/sys/zone.c (revision fdf4286765e129590dce97b37d12188bf7000b58)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include <sys/types.h>
31 #include <sys/syscall.h>
32 #include <sys/zone.h>
33 #include <sys/priv.h>
34 #include <priv_private.h>
35 #include <zone.h>
36 #include <sys/tsol/label.h>
37 #include <dlfcn.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 
41 zoneid_t
42 zone_create(const char *name, const char *root, const struct priv_set *privs,
43     const char *rctls, size_t rctlsz, const char *zfs, size_t zfssz,
44     int *extended_error, int match, int doi, const bslabel_t *label, int flags)
45 {
46 	zone_def  zd;
47 	priv_data_t *d;
48 
49 	LOADPRIVDATA(d);
50 
51 	zd.zone_name = name;
52 	zd.zone_root = root;
53 	zd.zone_privs = privs;
54 	zd.zone_privssz = d->pd_setsize;
55 	zd.rctlbuf = rctls;
56 	zd.rctlbufsz = rctlsz;
57 	zd.zfsbuf = zfs;
58 	zd.zfsbufsz = zfssz;
59 	zd.extended_error = extended_error;
60 	zd.match = match;
61 	zd.doi = doi;
62 	zd.label = label;
63 	zd.flags = flags;
64 
65 	return ((zoneid_t)syscall(SYS_zone, ZONE_CREATE, &zd));
66 }
67 
68 int
69 zone_boot(zoneid_t zoneid)
70 {
71 	return (syscall(SYS_zone, ZONE_BOOT, zoneid));
72 }
73 
74 int
75 zone_shutdown(zoneid_t zoneid)
76 {
77 	return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid));
78 }
79 
80 int
81 zone_destroy(zoneid_t zoneid)
82 {
83 	return (syscall(SYS_zone, ZONE_DESTROY, zoneid));
84 }
85 
86 ssize_t
87 zone_getattr(zoneid_t zoneid, int attr, void *valp, size_t size)
88 {
89 	sysret_t rval;
90 	int error;
91 
92 	error = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
93 	    attr, valp, size);
94 	if (error)
95 		(void) __set_errno(error);
96 	return ((ssize_t)rval.sys_rval1);
97 }
98 
99 int
100 zone_setattr(zoneid_t zoneid, int attr, void *valp, size_t size)
101 {
102 	return (syscall(SYS_zone, ZONE_SETATTR, zoneid, attr, valp, size));
103 }
104 
105 int
106 zone_enter(zoneid_t zoneid)
107 {
108 	return (syscall(SYS_zone, ZONE_ENTER, zoneid));
109 }
110 
111 /*
112  * Get id (if any) for specified zone.
113  *
114  * Call the real zone_get_id() in libzonecfg.so.1 if it can be found.
115  * Otherwise, perform a stripped-down version of the function.
116  * Any changes in one version should probably be reflected in the other.
117  *
118  * This stripped-down version of the function only checks for active
119  * (booted) zones, by numeric id or name.
120  */
121 
122 typedef	int (*zone_get_id_t)(const char *, zoneid_t *);
123 static zone_get_id_t real_zone_get_id = NULL;
124 
125 int
126 zone_get_id(const char *str, zoneid_t *zip)
127 {
128 	zoneid_t zoneid;
129 	char *cp;
130 
131 	/*
132 	 * The first time we are called, attempt to dlopen() libzonecfg.so.1
133 	 * and get a pointer to the real zone_get_id().
134 	 * If we fail, set our pointer to -1 so we won't try again.
135 	 */
136 	if (real_zone_get_id == NULL) {
137 		/*
138 		 * There's no harm in doing this more than once, even
139 		 * concurrently.  We will get the same result each time,
140 		 * and the dynamic linker will single-thread the dlopen()
141 		 * with its own internal lock.  The worst that can happen
142 		 * is that the handle gets a reference count greater than
143 		 * one, which doesn't matter since we never dlclose()
144 		 * the handle if we successfully find the symbol; the
145 		 * library just stays in the address space until exit().
146 		 */
147 		void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
148 		void *sym = (void *)(-1);
149 
150 		if (dlhandle != NULL &&
151 		    (sym = dlsym(dlhandle, "zone_get_id")) == NULL) {
152 			sym = (void *)(-1);
153 			(void) dlclose(dlhandle);
154 		}
155 		real_zone_get_id = (zone_get_id_t)sym;
156 	}
157 
158 	/*
159 	 * If we've successfully loaded it, call the real zone_get_id().
160 	 * Otherwise, perform our stripped-down version of the code.
161 	 */
162 	if (real_zone_get_id != (zone_get_id_t)(-1))
163 		return (real_zone_get_id(str, zip));
164 
165 	/* first try looking for active zone by id */
166 	errno = 0;
167 	zoneid = (zoneid_t)strtol(str, &cp, 0);
168 	if (errno == 0 && cp != str && *cp == '\0' &&
169 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
170 		*zip = zoneid;
171 		return (0);
172 	}
173 
174 	/* then look for active zone by name */
175 	if ((zoneid = getzoneidbyname(str)) != -1) {
176 		*zip = zoneid;
177 		return (0);
178 	}
179 
180 	/* not an active zone, return error */
181 	return (-1);
182 }
183 
184 int
185 zone_list(zoneid_t *zonelist, uint_t *numzones)
186 {
187 	return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones));
188 }
189 
190 /*
191  * Underlying implementation for getzoneid and getzoneidbyname.
192  */
193 static zoneid_t
194 zone_lookup(const char *name)
195 {
196 	return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name));
197 }
198 
199 zoneid_t
200 getzoneid(void)
201 {
202 	return (zone_lookup(NULL));
203 }
204 
205 zoneid_t
206 getzoneidbyname(const char *zonename)
207 {
208 	return (zone_lookup(zonename));
209 }
210 
211 ssize_t
212 getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen)
213 {
214 	return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen));
215 }
216 
217 int
218 zone_version(int *version)
219 {
220 	return (syscall(SYS_zone, ZONE_VERSION, version));
221 }
222 
223 
224 int
225 zone_add_datalink(zoneid_t zoneid, char *dlname)
226 {
227 	return (syscall(SYS_zone, ZONE_ADD_DATALINK, zoneid, dlname));
228 }
229 
230 int
231 zone_remove_datalink(zoneid_t zoneid, char *dlname)
232 {
233 	return (syscall(SYS_zone, ZONE_DEL_DATALINK, zoneid, dlname));
234 }
235 
236 int
237 zone_check_datalink(zoneid_t *zoneidp, char *dlname)
238 {
239 	return (syscall(SYS_zone, ZONE_CHECK_DATALINK, zoneidp, dlname));
240 }
241 
242 int
243 zone_list_datalink(zoneid_t zoneid, int *dlnump, char *buf)
244 {
245 	return (syscall(SYS_zone, ZONE_LIST_DATALINK, zoneid, dlnump, buf));
246 }
247