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 /*
  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  * Copyright 2011 Joyent Inc.  All rights reserved.
  26  */
  27 
  28 #include "lint.h"
  29 #include "thr_uberdata.h"
  30 #include <sys/types.h>
  31 #include <sys/syscall.h>
  32 #include <sys/zone.h>
  33 #include <sys/priv.h>
  34 #include <priv_private.h>
  35 #include <zone.h>
  36 #include <sys/tsol/label.h>
  37 #include <dlfcn.h>
  38 #include <stdlib.h>
  39 #include <errno.h>
  40 
  41 zoneid_t
  42 zone_create(const char *name, const char *root, const struct priv_set *privs,
  43     const char *rctls, size_t rctlsz, const char *zfs, size_t zfssz,
  44     int *extended_error, int match, int doi, const bslabel_t *label, int flags,
  45     zoneid_t req_zoneid)
  46 {
  47         zone_def  zd;
  48         priv_data_t *d;
  49 
  50         LOADPRIVDATA(d);
  51 
  52         zd.zone_name = name;
  53         zd.zone_root = root;
  54         zd.zone_privs = privs;
  55         zd.zone_privssz = d->pd_setsize;
  56         zd.rctlbuf = rctls;
  57         zd.rctlbufsz = rctlsz;
  58         zd.zfsbuf = zfs;
  59         zd.zfsbufsz = zfssz;
  60         zd.extended_error = extended_error;
  61         zd.match = match;
  62         zd.doi = doi;
  63         zd.label = label;
  64         zd.flags = flags;
  65         zd.zoneid = req_zoneid;
  66 
  67         return ((zoneid_t)syscall(SYS_zone, ZONE_CREATE, &zd));
  68 }
  69 
  70 int
  71 zone_boot(zoneid_t zoneid)
  72 {
  73         return (syscall(SYS_zone, ZONE_BOOT, zoneid));
  74 }
  75 
  76 int
  77 zone_shutdown(zoneid_t zoneid)
  78 {
  79         return (syscall(SYS_zone, ZONE_SHUTDOWN, zoneid));
  80 }
  81 
  82 int
  83 zone_destroy(zoneid_t zoneid)
  84 {
  85         return (syscall(SYS_zone, ZONE_DESTROY, zoneid));
  86 }
  87 
  88 ssize_t
  89 zone_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 
 101 int
 102 zone_setattr(zoneid_t zoneid, int attr, void *valp, size_t size)
 103 {
 104         return (syscall(SYS_zone, ZONE_SETATTR, zoneid, attr, valp, size));
 105 }
 106 
 107 int
 108 zone_enter(zoneid_t zoneid)
 109 {
 110         return (syscall(SYS_zone, ZONE_ENTER, zoneid));
 111 }
 112 
 113 /*
 114  * Get id (if any) for specified zone.
 115  *
 116  * Call the real zone_get_id() in libzonecfg.so.1 if it can be found.
 117  * Otherwise, perform a stripped-down version of the function.
 118  * Any changes in one version should probably be reflected in the other.
 119  *
 120  * This stripped-down version of the function only checks for active
 121  * (booted) zones, by numeric id or name.
 122  */
 123 
 124 typedef int (*zone_get_id_t)(const char *, zoneid_t *);
 125 static zone_get_id_t real_zone_get_id = NULL;
 126 
 127 int
 128 zone_get_id(const char *str, zoneid_t *zip)
 129 {
 130         zoneid_t zoneid;
 131         char *cp;
 132 
 133         /*
 134          * The first time we are called, attempt to dlopen() libzonecfg.so.1
 135          * and get a pointer to the real zone_get_id().
 136          * If we fail, set our pointer to -1 so we won't try again.
 137          */
 138         if (real_zone_get_id == NULL) {
 139                 /*
 140                  * There's no harm in doing this more than once, even
 141                  * concurrently.  We will get the same result each time,
 142                  * and the dynamic linker will single-thread the dlopen()
 143                  * with its own internal lock.  The worst that can happen
 144                  * is that the handle gets a reference count greater than
 145                  * one, which doesn't matter since we never dlclose()
 146                  * the handle if we successfully find the symbol; the
 147                  * library just stays in the address space until exit().
 148                  */
 149                 void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
 150                 void *sym = (void *)(-1);
 151 
 152                 if (dlhandle != NULL &&
 153                     (sym = dlsym(dlhandle, "zone_get_id")) == NULL) {
 154                         sym = (void *)(-1);
 155                         (void) dlclose(dlhandle);
 156                 }
 157                 real_zone_get_id = (zone_get_id_t)sym;
 158         }
 159 
 160         /*
 161          * If we've successfully loaded it, call the real zone_get_id().
 162          * Otherwise, perform our stripped-down version of the code.
 163          */
 164         if (real_zone_get_id != (zone_get_id_t)(-1))
 165                 return (real_zone_get_id(str, zip));
 166 
 167         /* first try looking for active zone by id */
 168         errno = 0;
 169         zoneid = (zoneid_t)strtol(str, &cp, 0);
 170         if (errno == 0 && cp != str && *cp == '\0' &&
 171             getzonenamebyid(zoneid, NULL, 0) != -1) {
 172                 *zip = zoneid;
 173                 return (0);
 174         }
 175 
 176         /* then look for active zone by name */
 177         if ((zoneid = getzoneidbyname(str)) != -1) {
 178                 *zip = zoneid;
 179                 return (0);
 180         }
 181 
 182         /* not an active zone, return error */
 183         return (-1);
 184 }
 185 
 186 int
 187 zone_list(zoneid_t *zonelist, uint_t *numzones)
 188 {
 189         return (syscall(SYS_zone, ZONE_LIST, zonelist, numzones));
 190 }
 191 
 192 /*
 193  * Underlying implementation for getzoneid and getzoneidbyname.
 194  */
 195 static zoneid_t
 196 zone_lookup(const char *name)
 197 {
 198         return ((zoneid_t)syscall(SYS_zone, ZONE_LOOKUP, name));
 199 }
 200 
 201 zoneid_t
 202 getzoneid(void)
 203 {
 204         return (zone_lookup(NULL));
 205 }
 206 
 207 zoneid_t
 208 getzoneidbyname(const char *zonename)
 209 {
 210         return (zone_lookup(zonename));
 211 }
 212 
 213 ssize_t
 214 getzonenamebyid(zoneid_t zoneid, char *buf, size_t buflen)
 215 {
 216         return (zone_getattr(zoneid, ZONE_ATTR_NAME, buf, buflen));
 217 }
 218 
 219 int
 220 zone_version(int *version)
 221 {
 222         return (syscall(SYS_zone, ZONE_VERSION, version));
 223 }
 224 
 225 int
 226 zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid)
 227 {
 228         return (syscall(SYS_zone, ZONE_ADD_DATALINK, zoneid, linkid));
 229 }
 230 
 231 int
 232 zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid)
 233 {
 234         return (syscall(SYS_zone, ZONE_DEL_DATALINK, zoneid, linkid));
 235 }
 236 
 237 int
 238 zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid)
 239 {
 240         return (syscall(SYS_zone, ZONE_CHECK_DATALINK, zoneidp, linkid));
 241 }
 242 
 243 int
 244 zone_list_datalink(zoneid_t zoneid, int *dlnump, datalink_id_t *linkids)
 245 {
 246         return (syscall(SYS_zone, ZONE_LIST_DATALINK, zoneid, dlnump, linkids));
 247 }
 248 
 249 const char *
 250 zone_get_nroot()
 251 {
 252         uberdata_t *udp = curthread->ul_uberdata;
 253         return (udp->ub_broot);
 254 }