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