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