zone.c revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e
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
44zoneid_t
45zone_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
70int
71zone_boot(zoneid_t zoneid, const char *bootargs)
72{
73	return (syscall(SYS_zone, ZONE_BOOT, zoneid, bootargs));
74}
75
76int
77zone_shutdown(zoneid_t zoneid)
78{
79	return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid));
80}
81
82int
83zone_destroy(zoneid_t zoneid)
84{
85	return (syscall(SYS_zone, ZONE_DESTROY, zoneid));
86}
87
88ssize_t
89zone_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
101int
102zone_enter(zoneid_t zoneid)
103{
104	return (syscall(SYS_zone, ZONE_ENTER, zoneid));
105}
106
107/*
108 * Get id (if any) for specified zone.
109 *
110 * Call the real zone_get_id() in libzonecfg.so.1 if it can be found.
111 * Otherwise, perform a stripped-down version of the function.
112 * Any changes in one version should probably be reflected in the other.
113 *
114 * This stripped-down version of the function only checks for active
115 * (booted) zones, by numeric id or name.
116 */
117
118typedef	int (*zone_get_id_t)(const char *, zoneid_t *);
119static zone_get_id_t real_zone_get_id = NULL;
120
121int
122zone_get_id(const char *str, zoneid_t *zip)
123{
124	zoneid_t zoneid;
125	char *cp;
126
127	/*
128	 * The first time we are called, attempt to dlopen() libzonecfg.so.1
129	 * and get a pointer to the real zone_get_id().
130	 * If we fail, set our pointer to -1 so we won't try again.
131	 */
132	if (real_zone_get_id == NULL) {
133		/*
134		 * There's no harm in doing this more than once, even
135		 * concurrently.  We will get the same result each time,
136		 * and the dynamic linker will single-thread the dlopen()
137		 * with its own internal lock.  The worst that can happen
138		 * is that the handle gets a reference count greater than
139		 * one, which doesn't matter since we never dlclose()
140		 * the handle if we successfully find the symbol; the
141		 * library just stays in the address space until exit().
142		 */
143		void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
144		void *sym = (void *)(-1);
145
146		if (dlhandle != NULL &&
147		    (sym = dlsym(dlhandle, "zone_get_id")) == NULL) {
148			sym = (void *)(-1);
149			(void) dlclose(dlhandle);
150		}
151		real_zone_get_id = (zone_get_id_t)sym;
152	}
153
154	/*
155	 * If we've successfully loaded it, call the real zone_get_id().
156	 * Otherwise, perform our stripped-down version of the code.
157	 */
158	if (real_zone_get_id != (zone_get_id_t)(-1))
159		return (real_zone_get_id(str, zip));
160
161	/* first try looking for active zone by id */
162	errno = 0;
163	zoneid = (zoneid_t)strtol(str, &cp, 0);
164	if (errno == 0 && cp != str && *cp == '\0' &&
165	    getzonenamebyid(zoneid, NULL, 0) != -1) {
166		*zip = zoneid;
167		return (0);
168	}
169
170	/* then look for active zone by name */
171	if ((zoneid = getzoneidbyname(str)) != -1) {
172		*zip = zoneid;
173		return (0);
174	}
175
176	/* not an active zone, return error */
177	return (-1);
178}
179
180int
181zone_list(zoneid_t *zonelist, uint_t *numzones)
182{
183	return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones));
184}
185
186/*
187 * Underlying implementation for getzoneid and getzoneidbyname.
188 */
189static zoneid_t
190zone_lookup(const char *name)
191{
192	return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name));
193}
194
195zoneid_t
196getzoneid(void)
197{
198	return (zone_lookup(NULL));
199}
200
201zoneid_t
202getzoneidbyname(const char *zonename)
203{
204	return (zone_lookup(zonename));
205}
206
207ssize_t
208getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen)
209{
210	return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen));
211}
212
213int
214zone_version(int *version)
215{
216	return (syscall(SYS_zone, ZONE_VERSION, version));
217}
218