xref: /titanic_41/usr/src/lib/libc/port/sys/zone.c (revision 4496171313bed39e96f21bc2f9faf2868e267ae3)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #pragma weak getzoneid = _getzoneid
29 #pragma weak getzoneidbyname = _getzoneidbyname
30 #pragma weak getzonenamebyid = _getzonenamebyid
31 
32 #include "synonyms.h"
33 #include <sys/types.h>
34 #include <sys/syscall.h>
35 #include <sys/zone.h>
36 #include <sys/priv.h>
37 #include <priv_private.h>
38 #include <zone.h>
39 #include <sys/tsol/label.h>
40 #include <dlfcn.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 
44 zoneid_t
45 zone_create(const char *name, const char *root, const struct priv_set *privs,
46     const char *rctls, size_t rctlsz, const char *zfs, size_t zfssz,
47     int *extended_error, int match, int doi, const bslabel_t *label)
48 {
49 	zone_def  zd;
50 	priv_data_t *d;
51 
52 	LOADPRIVDATA(d);
53 
54 	zd.zone_name = name;
55 	zd.zone_root = root;
56 	zd.zone_privs = privs;
57 	zd.zone_privssz = d->pd_setsize;
58 	zd.rctlbuf = rctls;
59 	zd.rctlbufsz = rctlsz;
60 	zd.zfsbuf = zfs;
61 	zd.zfsbufsz = zfssz;
62 	zd.extended_error = extended_error;
63 	zd.match = match;
64 	zd.doi = doi;
65 	zd.label = label;
66 
67 	return ((zoneid_t)syscall(SYS_zone, ZONE_CREATE, &zd));
68 }
69 
70 int
71 zone_boot(zoneid_t zoneid)
72 {
73 	return (syscall(SYS_zone, ZONE_BOOT, zoneid));
74 }
75 
76 int
77 zone_shutdown(zoneid_t zoneid)
78 {
79 	return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid));
80 }
81 
82 int
83 zone_destroy(zoneid_t zoneid)
84 {
85 	return (syscall(SYS_zone, ZONE_DESTROY, zoneid));
86 }
87 
88 ssize_t
89 zone_getattr(zoneid_t zoneid, int attr, void *valp, size_t size)
90 {
91 	sysret_t rval;
92 	int error;
93 
94 	error = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
95 	    attr, valp, size);
96 	if (error)
97 		(void) __set_errno(error);
98 	return ((ssize_t)rval.sys_rval1);
99 }
100 
101 int
102 zone_setattr(zoneid_t zoneid, int attr, void *valp, size_t size)
103 {
104 	return (syscall(SYS_zone, ZONE_SETATTR, zoneid, attr, valp, size));
105 }
106 
107 int
108 zone_enter(zoneid_t zoneid)
109 {
110 	return (syscall(SYS_zone, ZONE_ENTER, zoneid));
111 }
112 
113 /*
114  * Get id (if any) for specified zone.
115  *
116  * Call the real zone_get_id() in libzonecfg.so.1 if it can be found.
117  * Otherwise, perform a stripped-down version of the function.
118  * Any changes in one version should probably be reflected in the other.
119  *
120  * This stripped-down version of the function only checks for active
121  * (booted) zones, by numeric id or name.
122  */
123 
124 typedef	int (*zone_get_id_t)(const char *, zoneid_t *);
125 static zone_get_id_t real_zone_get_id = NULL;
126 
127 int
128 zone_get_id(const char *str, zoneid_t *zip)
129 {
130 	zoneid_t zoneid;
131 	char *cp;
132 
133 	/*
134 	 * The first time we are called, attempt to dlopen() libzonecfg.so.1
135 	 * and get a pointer to the real zone_get_id().
136 	 * If we fail, set our pointer to -1 so we won't try again.
137 	 */
138 	if (real_zone_get_id == NULL) {
139 		/*
140 		 * There's no harm in doing this more than once, even
141 		 * concurrently.  We will get the same result each time,
142 		 * and the dynamic linker will single-thread the dlopen()
143 		 * with its own internal lock.  The worst that can happen
144 		 * is that the handle gets a reference count greater than
145 		 * one, which doesn't matter since we never dlclose()
146 		 * the handle if we successfully find the symbol; the
147 		 * library just stays in the address space until exit().
148 		 */
149 		void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
150 		void *sym = (void *)(-1);
151 
152 		if (dlhandle != NULL &&
153 		    (sym = dlsym(dlhandle, "zone_get_id")) == NULL) {
154 			sym = (void *)(-1);
155 			(void) dlclose(dlhandle);
156 		}
157 		real_zone_get_id = (zone_get_id_t)sym;
158 	}
159 
160 	/*
161 	 * If we've successfully loaded it, call the real zone_get_id().
162 	 * Otherwise, perform our stripped-down version of the code.
163 	 */
164 	if (real_zone_get_id != (zone_get_id_t)(-1))
165 		return (real_zone_get_id(str, zip));
166 
167 	/* first try looking for active zone by id */
168 	errno = 0;
169 	zoneid = (zoneid_t)strtol(str, &cp, 0);
170 	if (errno == 0 && cp != str && *cp == '\0' &&
171 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
172 		*zip = zoneid;
173 		return (0);
174 	}
175 
176 	/* then look for active zone by name */
177 	if ((zoneid = getzoneidbyname(str)) != -1) {
178 		*zip = zoneid;
179 		return (0);
180 	}
181 
182 	/* not an active zone, return error */
183 	return (-1);
184 }
185 
186 int
187 zone_list(zoneid_t *zonelist, uint_t *numzones)
188 {
189 	return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones));
190 }
191 
192 /*
193  * Underlying implementation for getzoneid and getzoneidbyname.
194  */
195 static zoneid_t
196 zone_lookup(const char *name)
197 {
198 	return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name));
199 }
200 
201 zoneid_t
202 getzoneid(void)
203 {
204 	return (zone_lookup(NULL));
205 }
206 
207 zoneid_t
208 getzoneidbyname(const char *zonename)
209 {
210 	return (zone_lookup(zonename));
211 }
212 
213 ssize_t
214 getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen)
215 {
216 	return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen));
217 }
218 
219 int
220 zone_version(int *version)
221 {
222 	return (syscall(SYS_zone, ZONE_VERSION, version));
223 }
224