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