kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / lib / libc / port / sys / zone.c
blob0547ac1a56d8a2f46473bfc08bbd50a19a9d54f1
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
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 <dlfcn.h>
35 #include <stdlib.h>
36 #include <errno.h>
38 zoneid_t
39 zone_create(const char *name, const char *root, const struct priv_set *privs,
40 const char *rctls, size_t rctlsz, const char *zfs, size_t zfssz,
41 int *extended_error, int flags)
43 zone_def zd;
44 priv_data_t *d;
46 LOADPRIVDATA(d);
48 zd.zone_name = name;
49 zd.zone_root = root;
50 zd.zone_privs = privs;
51 zd.zone_privssz = d->pd_setsize;
52 zd.rctlbuf = rctls;
53 zd.rctlbufsz = rctlsz;
54 zd.zfsbuf = zfs;
55 zd.zfsbufsz = zfssz;
56 zd.extended_error = extended_error;
57 zd.flags = flags;
59 return ((zoneid_t)syscall(SYS_zone, ZONE_CREATE, &zd));
62 int
63 zone_boot(zoneid_t zoneid)
65 return (syscall(SYS_zone, ZONE_BOOT, zoneid));
68 int
69 zone_shutdown(zoneid_t zoneid)
71 return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid));
74 int
75 zone_destroy(zoneid_t zoneid)
77 return (syscall(SYS_zone, ZONE_DESTROY, zoneid));
80 ssize_t
81 zone_getattr(zoneid_t zoneid, int attr, void *valp, size_t size)
83 sysret_t rval;
84 int error;
86 error = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid,
87 attr, valp, size);
88 if (error)
89 (void) __set_errno(error);
90 return ((ssize_t)rval.sys_rval1);
93 int
94 zone_setattr(zoneid_t zoneid, int attr, void *valp, size_t size)
96 return (syscall(SYS_zone, ZONE_SETATTR, zoneid, attr, valp, size));
99 int
100 zone_enter(zoneid_t zoneid)
102 return (syscall(SYS_zone, ZONE_ENTER, zoneid));
106 * Get id (if any) for specified zone.
108 * Call the real zone_get_id() in libzonecfg.so.1 if it can be found.
109 * Otherwise, perform a stripped-down version of the function.
110 * Any changes in one version should probably be reflected in the other.
112 * This stripped-down version of the function only checks for active
113 * (booted) zones, by numeric id or name.
116 typedef int (*zone_get_id_t)(const char *, zoneid_t *);
117 static zone_get_id_t real_zone_get_id = NULL;
120 zone_get_id(const char *str, zoneid_t *zip)
122 zoneid_t zoneid;
123 char *cp;
126 * The first time we are called, attempt to dlopen() libzonecfg.so.1
127 * and get a pointer to the real zone_get_id().
128 * If we fail, set our pointer to -1 so we won't try again.
130 if (real_zone_get_id == NULL) {
132 * There's no harm in doing this more than once, even
133 * concurrently. We will get the same result each time,
134 * and the dynamic linker will single-thread the dlopen()
135 * with its own internal lock. The worst that can happen
136 * is that the handle gets a reference count greater than
137 * one, which doesn't matter since we never dlclose()
138 * the handle if we successfully find the symbol; the
139 * library just stays in the address space until exit().
141 void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
142 void *sym = (void *)(-1);
144 if (dlhandle != NULL &&
145 (sym = dlsym(dlhandle, "zone_get_id")) == NULL) {
146 sym = (void *)(-1);
147 (void) dlclose(dlhandle);
149 real_zone_get_id = (zone_get_id_t)sym;
153 * If we've successfully loaded it, call the real zone_get_id().
154 * Otherwise, perform our stripped-down version of the code.
156 if (real_zone_get_id != (zone_get_id_t)(-1))
157 return (real_zone_get_id(str, zip));
159 /* first try looking for active zone by id */
160 errno = 0;
161 zoneid = (zoneid_t)strtol(str, &cp, 0);
162 if (errno == 0 && cp != str && *cp == '\0' &&
163 getzonenamebyid(zoneid, NULL, 0) != -1) {
164 *zip = zoneid;
165 return (0);
168 /* then look for active zone by name */
169 if ((zoneid = getzoneidbyname(str)) != -1) {
170 *zip = zoneid;
171 return (0);
174 /* not an active zone, return error */
175 return (-1);
179 zone_list(zoneid_t *zonelist, uint_t *numzones)
181 return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones));
185 * Underlying implementation for getzoneid and getzoneidbyname.
187 static zoneid_t
188 zone_lookup(const char *name)
190 return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name));
193 zoneid_t
194 getzoneid(void)
196 return (zone_lookup(NULL));
199 zoneid_t
200 getzoneidbyname(const char *zonename)
202 return (zone_lookup(zonename));
205 ssize_t
206 getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen)
208 return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen));
212 zone_version(int *version)
214 return (syscall(SYS_zone, ZONE_VERSION, version));
218 zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid)
220 return (syscall(SYS_zone, ZONE_ADD_DATALINK, zoneid, linkid));
224 zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid)
226 return (syscall(SYS_zone, ZONE_DEL_DATALINK, zoneid, linkid));
230 zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid)
232 return (syscall(SYS_zone, ZONE_CHECK_DATALINK, zoneidp, linkid));
236 zone_list_datalink(zoneid_t zoneid, int *dlnump, datalink_id_t *linkids)
238 return (syscall(SYS_zone, ZONE_LIST_DATALINK, zoneid, dlnump, linkids));