17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 23*2b24ab6bSSebastien Roy * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277257d1b4Sraf #include "lint.h" 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 307c478bd9Sstevel@tonic-gate #include <sys/zone.h> 317c478bd9Sstevel@tonic-gate #include <sys/priv.h> 32821c4a97Sdp #include <priv_private.h> 337c478bd9Sstevel@tonic-gate #include <zone.h> 3445916cd2Sjpk #include <sys/tsol/label.h> 357c478bd9Sstevel@tonic-gate #include <dlfcn.h> 367c478bd9Sstevel@tonic-gate #include <stdlib.h> 377c478bd9Sstevel@tonic-gate #include <errno.h> 387c478bd9Sstevel@tonic-gate 3945916cd2Sjpk zoneid_t 40fa9e4066Sahrens zone_create(const char *name, const char *root, const struct priv_set *privs, 41fa9e4066Sahrens const char *rctls, size_t rctlsz, const char *zfs, size_t zfssz, 42f4b3ec61Sdh155122 int *extended_error, int match, int doi, const bslabel_t *label, int flags) 437c478bd9Sstevel@tonic-gate { 447c478bd9Sstevel@tonic-gate zone_def zd; 45821c4a97Sdp priv_data_t *d; 46821c4a97Sdp 47821c4a97Sdp LOADPRIVDATA(d); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate zd.zone_name = name; 507c478bd9Sstevel@tonic-gate zd.zone_root = root; 517c478bd9Sstevel@tonic-gate zd.zone_privs = privs; 52821c4a97Sdp zd.zone_privssz = d->pd_setsize; 537c478bd9Sstevel@tonic-gate zd.rctlbuf = rctls; 547c478bd9Sstevel@tonic-gate zd.rctlbufsz = rctlsz; 55fa9e4066Sahrens zd.zfsbuf = zfs; 56fa9e4066Sahrens zd.zfsbufsz = zfssz; 577c478bd9Sstevel@tonic-gate zd.extended_error = extended_error; 5845916cd2Sjpk zd.match = match; 5945916cd2Sjpk zd.doi = doi; 6045916cd2Sjpk zd.label = label; 61f4b3ec61Sdh155122 zd.flags = flags; 627c478bd9Sstevel@tonic-gate 63fa9e4066Sahrens return ((zoneid_t)syscall(SYS_zone, ZONE_CREATE, &zd)); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate int 673f2f09c1Sdp zone_boot(zoneid_t zoneid) 687c478bd9Sstevel@tonic-gate { 693f2f09c1Sdp return (syscall(SYS_zone, ZONE_BOOT, zoneid)); 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate int 737c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid)); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate int 797c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 807c478bd9Sstevel@tonic-gate { 817c478bd9Sstevel@tonic-gate return (syscall(SYS_zone, ZONE_DESTROY, zoneid)); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate ssize_t 857c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *valp, size_t size) 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate sysret_t rval; 887c478bd9Sstevel@tonic-gate int error; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate error = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid, 917c478bd9Sstevel@tonic-gate attr, valp, size); 927c478bd9Sstevel@tonic-gate if (error) 937c478bd9Sstevel@tonic-gate (void) __set_errno(error); 947c478bd9Sstevel@tonic-gate return ((ssize_t)rval.sys_rval1); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate int 983f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *valp, size_t size) 993f2f09c1Sdp { 1003f2f09c1Sdp return (syscall(SYS_zone, ZONE_SETATTR, zoneid, attr, valp, size)); 1013f2f09c1Sdp } 1023f2f09c1Sdp 1033f2f09c1Sdp int 1047c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 1057c478bd9Sstevel@tonic-gate { 1067c478bd9Sstevel@tonic-gate return (syscall(SYS_zone, ZONE_ENTER, zoneid)); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * Get id (if any) for specified zone. 1117c478bd9Sstevel@tonic-gate * 1127c478bd9Sstevel@tonic-gate * Call the real zone_get_id() in libzonecfg.so.1 if it can be found. 1137c478bd9Sstevel@tonic-gate * Otherwise, perform a stripped-down version of the function. 1147c478bd9Sstevel@tonic-gate * Any changes in one version should probably be reflected in the other. 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * This stripped-down version of the function only checks for active 1177c478bd9Sstevel@tonic-gate * (booted) zones, by numeric id or name. 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate typedef int (*zone_get_id_t)(const char *, zoneid_t *); 1217c478bd9Sstevel@tonic-gate static zone_get_id_t real_zone_get_id = NULL; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate int 1247c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate zoneid_t zoneid; 1277c478bd9Sstevel@tonic-gate char *cp; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * The first time we are called, attempt to dlopen() libzonecfg.so.1 1317c478bd9Sstevel@tonic-gate * and get a pointer to the real zone_get_id(). 1327c478bd9Sstevel@tonic-gate * If we fail, set our pointer to -1 so we won't try again. 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate if (real_zone_get_id == NULL) { 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * There's no harm in doing this more than once, even 1377c478bd9Sstevel@tonic-gate * concurrently. We will get the same result each time, 1387c478bd9Sstevel@tonic-gate * and the dynamic linker will single-thread the dlopen() 1397c478bd9Sstevel@tonic-gate * with its own internal lock. The worst that can happen 1407c478bd9Sstevel@tonic-gate * is that the handle gets a reference count greater than 1417c478bd9Sstevel@tonic-gate * one, which doesn't matter since we never dlclose() 1427c478bd9Sstevel@tonic-gate * the handle if we successfully find the symbol; the 1437c478bd9Sstevel@tonic-gate * library just stays in the address space until exit(). 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY); 1467c478bd9Sstevel@tonic-gate void *sym = (void *)(-1); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate if (dlhandle != NULL && 1497c478bd9Sstevel@tonic-gate (sym = dlsym(dlhandle, "zone_get_id")) == NULL) { 1507c478bd9Sstevel@tonic-gate sym = (void *)(-1); 1517c478bd9Sstevel@tonic-gate (void) dlclose(dlhandle); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate real_zone_get_id = (zone_get_id_t)sym; 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * If we've successfully loaded it, call the real zone_get_id(). 1587c478bd9Sstevel@tonic-gate * Otherwise, perform our stripped-down version of the code. 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate if (real_zone_get_id != (zone_get_id_t)(-1)) 1617c478bd9Sstevel@tonic-gate return (real_zone_get_id(str, zip)); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* first try looking for active zone by id */ 1647c478bd9Sstevel@tonic-gate errno = 0; 1657c478bd9Sstevel@tonic-gate zoneid = (zoneid_t)strtol(str, &cp, 0); 1667c478bd9Sstevel@tonic-gate if (errno == 0 && cp != str && *cp == '\0' && 1677c478bd9Sstevel@tonic-gate getzonenamebyid(zoneid, NULL, 0) != -1) { 1687c478bd9Sstevel@tonic-gate *zip = zoneid; 1697c478bd9Sstevel@tonic-gate return (0); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* then look for active zone by name */ 1737c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(str)) != -1) { 1747c478bd9Sstevel@tonic-gate *zip = zoneid; 1757c478bd9Sstevel@tonic-gate return (0); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate /* not an active zone, return error */ 1797c478bd9Sstevel@tonic-gate return (-1); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate int 1837c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zonelist, uint_t *numzones) 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones)); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* 1897c478bd9Sstevel@tonic-gate * Underlying implementation for getzoneid and getzoneidbyname. 1907c478bd9Sstevel@tonic-gate */ 1917c478bd9Sstevel@tonic-gate static zoneid_t 1927c478bd9Sstevel@tonic-gate zone_lookup(const char *name) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name)); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate zoneid_t 1987c478bd9Sstevel@tonic-gate getzoneid(void) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate return (zone_lookup(NULL)); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate zoneid_t 2047c478bd9Sstevel@tonic-gate getzoneidbyname(const char *zonename) 2057c478bd9Sstevel@tonic-gate { 2067c478bd9Sstevel@tonic-gate return (zone_lookup(zonename)); 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate ssize_t 2107c478bd9Sstevel@tonic-gate getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen) 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen)); 2137c478bd9Sstevel@tonic-gate } 214821c4a97Sdp 215821c4a97Sdp int 216821c4a97Sdp zone_version(int *version) 217821c4a97Sdp { 218821c4a97Sdp return (syscall(SYS_zone, ZONE_VERSION, version)); 219821c4a97Sdp } 220f4b3ec61Sdh155122 221f4b3ec61Sdh155122 int 222*2b24ab6bSSebastien Roy zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid) 223f4b3ec61Sdh155122 { 224*2b24ab6bSSebastien Roy return (syscall(SYS_zone, ZONE_ADD_DATALINK, zoneid, linkid)); 225f4b3ec61Sdh155122 } 226f4b3ec61Sdh155122 227f4b3ec61Sdh155122 int 228*2b24ab6bSSebastien Roy zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid) 229f4b3ec61Sdh155122 { 230*2b24ab6bSSebastien Roy return (syscall(SYS_zone, ZONE_DEL_DATALINK, zoneid, linkid)); 231f4b3ec61Sdh155122 } 232f4b3ec61Sdh155122 233f4b3ec61Sdh155122 int 234*2b24ab6bSSebastien Roy zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid) 235f4b3ec61Sdh155122 { 236*2b24ab6bSSebastien Roy return (syscall(SYS_zone, ZONE_CHECK_DATALINK, zoneidp, linkid)); 237f4b3ec61Sdh155122 } 238f4b3ec61Sdh155122 239f4b3ec61Sdh155122 int 240*2b24ab6bSSebastien Roy zone_list_datalink(zoneid_t zoneid, int *dlnump, datalink_id_t *linkids) 241f4b3ec61Sdh155122 { 242*2b24ab6bSSebastien Roy return (syscall(SYS_zone, ZONE_LIST_DATALINK, zoneid, dlnump, linkids)); 243f4b3ec61Sdh155122 } 244