xref: /titanic_44/usr/src/lib/libc/port/sys/zone.c (revision f56c1286e5113aa46bd6e723da14d30c123153f2)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 #pragma weak getzoneid = _getzoneid
30 #pragma weak getzoneidbyname = _getzoneidbyname
31 #pragma weak getzonenamebyid = _getzonenamebyid
32 
33 #include "synonyms.h"
34 #include <sys/types.h>
35 #include <sys/syscall.h>
36 #include <sys/zone.h>
37 #include <sys/priv.h>
38 #include <priv_private.h>
39 #include <zone.h>
40 #include <dlfcn.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 
44 extern 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)
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 
64 	return ((zoneid_t)syscall(SYS_zone, ZONE_CREATE, &zd));
65 }
66 
67 int
68 zone_boot(zoneid_t zoneid, const char *bootargs)
69 {
70 	return (syscall(SYS_zone, ZONE_BOOT, zoneid, bootargs));
71 }
72 
73 int
74 zone_shutdown(zoneid_t zoneid)
75 {
76 	return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid));
77 }
78 
79 int
80 zone_destroy(zoneid_t zoneid)
81 {
82 	return (syscall(SYS_zone, ZONE_DESTROY, zoneid));
83 }
84 
85 ssize_t
86 zone_getattr(zoneid_t zoneid, int attr, void *valp, size_t size)
87 {
88 	sysret_t rval;
89 	int error;
90 
91 	error = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
92 	    attr, valp, size);
93 	if (error)
94 		(void) __set_errno(error);
95 	return ((ssize_t)rval.sys_rval1);
96 }
97 
98 int
99 zone_enter(zoneid_t zoneid)
100 {
101 	return (syscall(SYS_zone, ZONE_ENTER, zoneid));
102 }
103 
104 /*
105  * Get id (if any) for specified zone.
106  *
107  * Call the real zone_get_id() in libzonecfg.so.1 if it can be found.
108  * Otherwise, perform a stripped-down version of the function.
109  * Any changes in one version should probably be reflected in the other.
110  *
111  * This stripped-down version of the function only checks for active
112  * (booted) zones, by numeric id or name.
113  */
114 
115 typedef	int (*zone_get_id_t)(const char *, zoneid_t *);
116 static zone_get_id_t real_zone_get_id = NULL;
117 
118 int
119 zone_get_id(const char *str, zoneid_t *zip)
120 {
121 	zoneid_t zoneid;
122 	char *cp;
123 
124 	/*
125 	 * The first time we are called, attempt to dlopen() libzonecfg.so.1
126 	 * and get a pointer to the real zone_get_id().
127 	 * If we fail, set our pointer to -1 so we won't try again.
128 	 */
129 	if (real_zone_get_id == NULL) {
130 		/*
131 		 * There's no harm in doing this more than once, even
132 		 * concurrently.  We will get the same result each time,
133 		 * and the dynamic linker will single-thread the dlopen()
134 		 * with its own internal lock.  The worst that can happen
135 		 * is that the handle gets a reference count greater than
136 		 * one, which doesn't matter since we never dlclose()
137 		 * the handle if we successfully find the symbol; the
138 		 * library just stays in the address space until exit().
139 		 */
140 		void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
141 		void *sym = (void *)(-1);
142 
143 		if (dlhandle != NULL &&
144 		    (sym = dlsym(dlhandle, "zone_get_id")) == NULL) {
145 			sym = (void *)(-1);
146 			(void) dlclose(dlhandle);
147 		}
148 		real_zone_get_id = (zone_get_id_t)sym;
149 	}
150 
151 	/*
152 	 * If we've successfully loaded it, call the real zone_get_id().
153 	 * Otherwise, perform our stripped-down version of the code.
154 	 */
155 	if (real_zone_get_id != (zone_get_id_t)(-1))
156 		return (real_zone_get_id(str, zip));
157 
158 	/* first try looking for active zone by id */
159 	errno = 0;
160 	zoneid = (zoneid_t)strtol(str, &cp, 0);
161 	if (errno == 0 && cp != str && *cp == '\0' &&
162 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
163 		*zip = zoneid;
164 		return (0);
165 	}
166 
167 	/* then look for active zone by name */
168 	if ((zoneid = getzoneidbyname(str)) != -1) {
169 		*zip = zoneid;
170 		return (0);
171 	}
172 
173 	/* not an active zone, return error */
174 	return (-1);
175 }
176 
177 int
178 zone_list(zoneid_t *zonelist, uint_t *numzones)
179 {
180 	return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones));
181 }
182 
183 /*
184  * Underlying implementation for getzoneid and getzoneidbyname.
185  */
186 static zoneid_t
187 zone_lookup(const char *name)
188 {
189 	return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name));
190 }
191 
192 zoneid_t
193 getzoneid(void)
194 {
195 	return (zone_lookup(NULL));
196 }
197 
198 zoneid_t
199 getzoneidbyname(const char *zonename)
200 {
201 	return (zone_lookup(zonename));
202 }
203 
204 ssize_t
205 getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen)
206 {
207 	return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen));
208 }
209 
210 int
211 zone_version(int *version)
212 {
213 	return (syscall(SYS_zone, ZONE_VERSION, version));
214 }
215