1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Zones 31*0Sstevel@tonic-gate * 32*0Sstevel@tonic-gate * A zone is a named collection of processes, namespace constraints, 33*0Sstevel@tonic-gate * and other system resources which comprise a secure and manageable 34*0Sstevel@tonic-gate * application containment facility. 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * Zones (represented by the reference counted zone_t) are tracked in 37*0Sstevel@tonic-gate * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs 38*0Sstevel@tonic-gate * (zoneid_t) are used to track zone association. Zone IDs are 39*0Sstevel@tonic-gate * dynamically generated when the zone is created; if a persistent 40*0Sstevel@tonic-gate * identifier is needed (core files, accounting logs, audit trail, 41*0Sstevel@tonic-gate * etc.), the zone name should be used. 42*0Sstevel@tonic-gate * 43*0Sstevel@tonic-gate * 44*0Sstevel@tonic-gate * Global Zone: 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate * The global zone (zoneid 0) is automatically associated with all 47*0Sstevel@tonic-gate * system resources that have not been bound to a user-created zone. 48*0Sstevel@tonic-gate * This means that even systems where zones are not in active use 49*0Sstevel@tonic-gate * have a global zone, and all processes, mounts, etc. are 50*0Sstevel@tonic-gate * associated with that zone. The global zone is generally 51*0Sstevel@tonic-gate * unconstrained in terms of privileges and access, though the usual 52*0Sstevel@tonic-gate * credential and privilege based restrictions apply. 53*0Sstevel@tonic-gate * 54*0Sstevel@tonic-gate * 55*0Sstevel@tonic-gate * Zone States: 56*0Sstevel@tonic-gate * 57*0Sstevel@tonic-gate * The states in which a zone may be in and the transitions are as 58*0Sstevel@tonic-gate * follows: 59*0Sstevel@tonic-gate * 60*0Sstevel@tonic-gate * ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially 61*0Sstevel@tonic-gate * initialized zone is added to the list of active zones on the system but 62*0Sstevel@tonic-gate * isn't accessible. 63*0Sstevel@tonic-gate * 64*0Sstevel@tonic-gate * ZONE_IS_READY: zsched (the kernel dummy process for a zone) is 65*0Sstevel@tonic-gate * ready. The zone is made visible after the ZSD constructor callbacks are 66*0Sstevel@tonic-gate * executed. A zone remains in this state until it transitions into 67*0Sstevel@tonic-gate * the ZONE_IS_BOOTING state as a result of a call to zone_boot(). 68*0Sstevel@tonic-gate * 69*0Sstevel@tonic-gate * ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start 70*0Sstevel@tonic-gate * init. Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN 71*0Sstevel@tonic-gate * state. 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * ZONE_IS_RUNNING: The zone is open for business: zsched has 74*0Sstevel@tonic-gate * successfully started init. A zone remains in this state until 75*0Sstevel@tonic-gate * zone_shutdown() is called. 76*0Sstevel@tonic-gate * 77*0Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is 78*0Sstevel@tonic-gate * killing all processes running in the zone. The zone remains 79*0Sstevel@tonic-gate * in this state until there are no more user processes running in the zone. 80*0Sstevel@tonic-gate * zone_create(), zone_enter(), and zone_destroy() on this zone will fail. 81*0Sstevel@tonic-gate * Since zone_shutdown() is restartable, it may be called successfully 82*0Sstevel@tonic-gate * multiple times for the same zone_t. Setting of the zone's state to 83*0Sstevel@tonic-gate * ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check 84*0Sstevel@tonic-gate * the zone's status without worrying about it being a moving target. 85*0Sstevel@tonic-gate * 86*0Sstevel@tonic-gate * ZONE_IS_EMPTY: zone_shutdown() has been called, and there 87*0Sstevel@tonic-gate * are no more user processes in the zone. The zone remains in this 88*0Sstevel@tonic-gate * state until there are no more kernel threads associated with the 89*0Sstevel@tonic-gate * zone. zone_create(), zone_enter(), and zone_destroy() on this zone will 90*0Sstevel@tonic-gate * fail. 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone 93*0Sstevel@tonic-gate * have exited. zone_shutdown() returns. Henceforth it is not possible to 94*0Sstevel@tonic-gate * join the zone or create kernel threads therein. 95*0Sstevel@tonic-gate * 96*0Sstevel@tonic-gate * ZONE_IS_DYING: zone_destroy() has been called on the zone; zone 97*0Sstevel@tonic-gate * remains in this state until zsched exits. Calls to zone_find_by_*() 98*0Sstevel@tonic-gate * return NULL from now on. 99*0Sstevel@tonic-gate * 100*0Sstevel@tonic-gate * ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0). There are no 101*0Sstevel@tonic-gate * processes or threads doing work on behalf of the zone. The zone is 102*0Sstevel@tonic-gate * removed from the list of active zones. zone_destroy() returns, and 103*0Sstevel@tonic-gate * the zone can be recreated. 104*0Sstevel@tonic-gate * 105*0Sstevel@tonic-gate * ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor 106*0Sstevel@tonic-gate * callbacks are executed, and all memory associated with the zone is 107*0Sstevel@tonic-gate * freed. 108*0Sstevel@tonic-gate * 109*0Sstevel@tonic-gate * Threads can wait for the zone to enter a requested state by using 110*0Sstevel@tonic-gate * zone_status_wait() or zone_status_timedwait() with the desired 111*0Sstevel@tonic-gate * state passed in as an argument. Zone state transitions are 112*0Sstevel@tonic-gate * uni-directional; it is not possible to move back to an earlier state. 113*0Sstevel@tonic-gate * 114*0Sstevel@tonic-gate * 115*0Sstevel@tonic-gate * Zone-Specific Data: 116*0Sstevel@tonic-gate * 117*0Sstevel@tonic-gate * Subsystems needing to maintain zone-specific data can store that 118*0Sstevel@tonic-gate * data using the ZSD mechanism. This provides a zone-specific data 119*0Sstevel@tonic-gate * store, similar to thread-specific data (see pthread_getspecific(3C) 120*0Sstevel@tonic-gate * or the TSD code in uts/common/disp/thread.c. Also, ZSD can be used 121*0Sstevel@tonic-gate * to register callbacks to be invoked when a zone is created, shut 122*0Sstevel@tonic-gate * down, or destroyed. This can be used to initialize zone-specific 123*0Sstevel@tonic-gate * data for new zones and to clean up when zones go away. 124*0Sstevel@tonic-gate * 125*0Sstevel@tonic-gate * 126*0Sstevel@tonic-gate * Data Structures: 127*0Sstevel@tonic-gate * 128*0Sstevel@tonic-gate * The per-zone structure (zone_t) is reference counted, and freed 129*0Sstevel@tonic-gate * when all references are released. zone_hold and zone_rele can be 130*0Sstevel@tonic-gate * used to adjust the reference count. In addition, reference counts 131*0Sstevel@tonic-gate * associated with the cred_t structure are tracked separately using 132*0Sstevel@tonic-gate * zone_cred_hold and zone_cred_rele. 133*0Sstevel@tonic-gate * 134*0Sstevel@tonic-gate * Pointers to active zone_t's are stored in two hash tables; one 135*0Sstevel@tonic-gate * for searching by id, the other for searching by name. Lookups 136*0Sstevel@tonic-gate * can be performed on either basis, using zone_find_by_id and 137*0Sstevel@tonic-gate * zone_find_by_name. Both return zone_t pointers with the zone 138*0Sstevel@tonic-gate * held, so zone_rele should be called when the pointer is no longer 139*0Sstevel@tonic-gate * needed. Zones can also be searched by path; zone_find_by_path 140*0Sstevel@tonic-gate * returns the zone with which a path name is associated (global 141*0Sstevel@tonic-gate * zone if the path is not within some other zone's file system 142*0Sstevel@tonic-gate * hierarchy). This currently requires iterating through each zone, 143*0Sstevel@tonic-gate * so it is slower than an id or name search via a hash table. 144*0Sstevel@tonic-gate * 145*0Sstevel@tonic-gate * 146*0Sstevel@tonic-gate * Locking: 147*0Sstevel@tonic-gate * 148*0Sstevel@tonic-gate * zonehash_lock: This is a top-level global lock used to protect the 149*0Sstevel@tonic-gate * zone hash tables and lists. Zones cannot be created or destroyed 150*0Sstevel@tonic-gate * while this lock is held. 151*0Sstevel@tonic-gate * zone_status_lock: This is a global lock protecting zone state. 152*0Sstevel@tonic-gate * Zones cannot change state while this lock is held. It also 153*0Sstevel@tonic-gate * protects the list of kernel threads associated with a zone. 154*0Sstevel@tonic-gate * zone_lock: This is a per-zone lock used to protect several fields of 155*0Sstevel@tonic-gate * the zone_t (see <sys/zone.h> for details). In addition, holding 156*0Sstevel@tonic-gate * this lock means that the zone cannot go away. 157*0Sstevel@tonic-gate * zsd_key_lock: This is a global lock protecting the key state for ZSD. 158*0Sstevel@tonic-gate * zone_deathrow_lock: This is a global lock protecting the "deathrow" 159*0Sstevel@tonic-gate * list (a list of zones in the ZONE_IS_DEAD state). 160*0Sstevel@tonic-gate * 161*0Sstevel@tonic-gate * Ordering requirements: 162*0Sstevel@tonic-gate * pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock --> 163*0Sstevel@tonic-gate * zone_lock --> zsd_key_lock --> pidlock --> p_lock 164*0Sstevel@tonic-gate * 165*0Sstevel@tonic-gate * Blocking memory allocations are permitted while holding any of the 166*0Sstevel@tonic-gate * zone locks. 167*0Sstevel@tonic-gate * 168*0Sstevel@tonic-gate * 169*0Sstevel@tonic-gate * System Call Interface: 170*0Sstevel@tonic-gate * 171*0Sstevel@tonic-gate * The zone subsystem can be managed and queried from user level with 172*0Sstevel@tonic-gate * the following system calls (all subcodes of the primary "zone" 173*0Sstevel@tonic-gate * system call): 174*0Sstevel@tonic-gate * - zone_create: creates a zone with selected attributes (name, 175*0Sstevel@tonic-gate * root path, privileges, resource controls) 176*0Sstevel@tonic-gate * - zone_enter: allows the current process to enter a zone 177*0Sstevel@tonic-gate * - zone_getattr: reports attributes of a zone 178*0Sstevel@tonic-gate * - zone_list: lists all zones active in the system 179*0Sstevel@tonic-gate * - zone_lookup: looks up zone id based on name 180*0Sstevel@tonic-gate * - zone_shutdown: initiates shutdown process (see states above) 181*0Sstevel@tonic-gate * - zone_destroy: completes shutdown process (see states above) 182*0Sstevel@tonic-gate * 183*0Sstevel@tonic-gate */ 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate #include <sys/priv_impl.h> 186*0Sstevel@tonic-gate #include <sys/cred.h> 187*0Sstevel@tonic-gate #include <c2/audit.h> 188*0Sstevel@tonic-gate #include <sys/ddi.h> 189*0Sstevel@tonic-gate #include <sys/debug.h> 190*0Sstevel@tonic-gate #include <sys/file.h> 191*0Sstevel@tonic-gate #include <sys/kmem.h> 192*0Sstevel@tonic-gate #include <sys/mutex.h> 193*0Sstevel@tonic-gate #include <sys/pathname.h> 194*0Sstevel@tonic-gate #include <sys/proc.h> 195*0Sstevel@tonic-gate #include <sys/project.h> 196*0Sstevel@tonic-gate #include <sys/task.h> 197*0Sstevel@tonic-gate #include <sys/systm.h> 198*0Sstevel@tonic-gate #include <sys/types.h> 199*0Sstevel@tonic-gate #include <sys/utsname.h> 200*0Sstevel@tonic-gate #include <sys/vnode.h> 201*0Sstevel@tonic-gate #include <sys/vfs.h> 202*0Sstevel@tonic-gate #include <sys/systeminfo.h> 203*0Sstevel@tonic-gate #include <sys/policy.h> 204*0Sstevel@tonic-gate #include <sys/cred_impl.h> 205*0Sstevel@tonic-gate #include <sys/contract_impl.h> 206*0Sstevel@tonic-gate #include <sys/contract/process_impl.h> 207*0Sstevel@tonic-gate #include <sys/class.h> 208*0Sstevel@tonic-gate #include <sys/pool.h> 209*0Sstevel@tonic-gate #include <sys/pool_pset.h> 210*0Sstevel@tonic-gate #include <sys/pset.h> 211*0Sstevel@tonic-gate #include <sys/log.h> 212*0Sstevel@tonic-gate #include <sys/sysmacros.h> 213*0Sstevel@tonic-gate #include <sys/callb.h> 214*0Sstevel@tonic-gate #include <sys/vmparam.h> 215*0Sstevel@tonic-gate #include <sys/corectl.h> 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate #include <sys/door.h> 218*0Sstevel@tonic-gate #include <sys/cpuvar.h> 219*0Sstevel@tonic-gate #include <sys/fs/snode.h> 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate #include <sys/uadmin.h> 222*0Sstevel@tonic-gate #include <sys/session.h> 223*0Sstevel@tonic-gate #include <sys/cmn_err.h> 224*0Sstevel@tonic-gate #include <sys/modhash.h> 225*0Sstevel@tonic-gate #include <sys/nvpair.h> 226*0Sstevel@tonic-gate #include <sys/rctl.h> 227*0Sstevel@tonic-gate #include <sys/fss.h> 228*0Sstevel@tonic-gate #include <sys/zone.h> 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate /* 231*0Sstevel@tonic-gate * cv used to signal that all references to the zone have been released. This 232*0Sstevel@tonic-gate * needs to be global since there may be multiple waiters, and the first to 233*0Sstevel@tonic-gate * wake up will free the zone_t, hence we cannot use zone->zone_cv. 234*0Sstevel@tonic-gate */ 235*0Sstevel@tonic-gate static kcondvar_t zone_destroy_cv; 236*0Sstevel@tonic-gate /* 237*0Sstevel@tonic-gate * Lock used to serialize access to zone_cv. This could have been per-zone, 238*0Sstevel@tonic-gate * but then we'd need another lock for zone_destroy_cv, and why bother? 239*0Sstevel@tonic-gate */ 240*0Sstevel@tonic-gate static kmutex_t zone_status_lock; 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate /* 243*0Sstevel@tonic-gate * ZSD-related global variables. 244*0Sstevel@tonic-gate */ 245*0Sstevel@tonic-gate static kmutex_t zsd_key_lock; /* protects the following two */ 246*0Sstevel@tonic-gate /* 247*0Sstevel@tonic-gate * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval. 248*0Sstevel@tonic-gate */ 249*0Sstevel@tonic-gate static zone_key_t zsd_keyval = 0; 250*0Sstevel@tonic-gate /* 251*0Sstevel@tonic-gate * Global list of registered keys. We use this when a new zone is created. 252*0Sstevel@tonic-gate */ 253*0Sstevel@tonic-gate static list_t zsd_registered_keys; 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate int zone_hash_size = 256; 256*0Sstevel@tonic-gate static mod_hash_t *zonehashbyname, *zonehashbyid; 257*0Sstevel@tonic-gate static kmutex_t zonehash_lock; 258*0Sstevel@tonic-gate static uint_t zonecount; 259*0Sstevel@tonic-gate static id_space_t *zoneid_space; 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate /* 262*0Sstevel@tonic-gate * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the 263*0Sstevel@tonic-gate * kernel proper runs, and which manages all other zones. 264*0Sstevel@tonic-gate * 265*0Sstevel@tonic-gate * Although not declared as static, the variable "zone0" should not be used 266*0Sstevel@tonic-gate * except for by code that needs to reference the global zone early on in boot, 267*0Sstevel@tonic-gate * before it is fully initialized. All other consumers should use 268*0Sstevel@tonic-gate * 'global_zone'. 269*0Sstevel@tonic-gate */ 270*0Sstevel@tonic-gate zone_t zone0; 271*0Sstevel@tonic-gate zone_t *global_zone = NULL; /* Set when the global zone is initialized */ 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate /* 274*0Sstevel@tonic-gate * List of active zones, protected by zonehash_lock. 275*0Sstevel@tonic-gate */ 276*0Sstevel@tonic-gate static list_t zone_active; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate /* 279*0Sstevel@tonic-gate * List of destroyed zones that still have outstanding cred references. 280*0Sstevel@tonic-gate * Used for debugging. Uses a separate lock to avoid lock ordering 281*0Sstevel@tonic-gate * problems in zone_free. 282*0Sstevel@tonic-gate */ 283*0Sstevel@tonic-gate static list_t zone_deathrow; 284*0Sstevel@tonic-gate static kmutex_t zone_deathrow_lock; 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */ 287*0Sstevel@tonic-gate uint_t maxzones = 8192; 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate /* 290*0Sstevel@tonic-gate * This isn't static so lint doesn't complain. 291*0Sstevel@tonic-gate */ 292*0Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares; 293*0Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps; 294*0Sstevel@tonic-gate /* 295*0Sstevel@tonic-gate * Synchronization primitives used to synchronize between mounts and zone 296*0Sstevel@tonic-gate * creation/destruction. 297*0Sstevel@tonic-gate */ 298*0Sstevel@tonic-gate static int mounts_in_progress; 299*0Sstevel@tonic-gate static kcondvar_t mount_cv; 300*0Sstevel@tonic-gate static kmutex_t mount_lock; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate const char * const zone_initname = "/sbin/init"; 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid); 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate /* 307*0Sstevel@tonic-gate * Certain filesystems (such as NFS and autofs) need to know which zone 308*0Sstevel@tonic-gate * the mount is being placed in. Because of this, we need to be able to 309*0Sstevel@tonic-gate * ensure that a zone isn't in the process of being created such that 310*0Sstevel@tonic-gate * nfs_mount() thinks it is in the global zone, while by the time it 311*0Sstevel@tonic-gate * gets added the list of mounted zones, it ends up on zoneA's mount 312*0Sstevel@tonic-gate * list. 313*0Sstevel@tonic-gate * 314*0Sstevel@tonic-gate * The following functions: block_mounts()/resume_mounts() and 315*0Sstevel@tonic-gate * mount_in_progress()/mount_completed() are used by zones and the VFS 316*0Sstevel@tonic-gate * layer (respectively) to synchronize zone creation and new mounts. 317*0Sstevel@tonic-gate * 318*0Sstevel@tonic-gate * The semantics are like a reader-reader lock such that there may 319*0Sstevel@tonic-gate * either be multiple mounts (or zone creations, if that weren't 320*0Sstevel@tonic-gate * serialized by zonehash_lock) in progress at the same time, but not 321*0Sstevel@tonic-gate * both. 322*0Sstevel@tonic-gate * 323*0Sstevel@tonic-gate * We use cv's so the user can ctrl-C out of the operation if it's 324*0Sstevel@tonic-gate * taking too long. 325*0Sstevel@tonic-gate * 326*0Sstevel@tonic-gate * The semantics are such that there is unfair bias towards the 327*0Sstevel@tonic-gate * "current" operation. This means that zone creations may starve if 328*0Sstevel@tonic-gate * there is a rapid succession of new mounts coming in to the system, or 329*0Sstevel@tonic-gate * there is a remote possibility that zones will be created at such a 330*0Sstevel@tonic-gate * rate that new mounts will not be able to proceed. 331*0Sstevel@tonic-gate */ 332*0Sstevel@tonic-gate /* 333*0Sstevel@tonic-gate * Prevent new mounts from progressing to the point of calling 334*0Sstevel@tonic-gate * VFS_MOUNT(). If there are already mounts in this "region", wait for 335*0Sstevel@tonic-gate * them to complete. 336*0Sstevel@tonic-gate */ 337*0Sstevel@tonic-gate static int 338*0Sstevel@tonic-gate block_mounts(void) 339*0Sstevel@tonic-gate { 340*0Sstevel@tonic-gate int retval = 0; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate /* 343*0Sstevel@tonic-gate * Since it may block for a long time, block_mounts() shouldn't be 344*0Sstevel@tonic-gate * called with zonehash_lock held. 345*0Sstevel@tonic-gate */ 346*0Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 347*0Sstevel@tonic-gate mutex_enter(&mount_lock); 348*0Sstevel@tonic-gate while (mounts_in_progress > 0) { 349*0Sstevel@tonic-gate if (cv_wait_sig(&mount_cv, &mount_lock) == 0) 350*0Sstevel@tonic-gate goto signaled; 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate /* 353*0Sstevel@tonic-gate * A negative value of mounts_in_progress indicates that mounts 354*0Sstevel@tonic-gate * have been blocked by (-mounts_in_progress) different callers. 355*0Sstevel@tonic-gate */ 356*0Sstevel@tonic-gate mounts_in_progress--; 357*0Sstevel@tonic-gate retval = 1; 358*0Sstevel@tonic-gate signaled: 359*0Sstevel@tonic-gate mutex_exit(&mount_lock); 360*0Sstevel@tonic-gate return (retval); 361*0Sstevel@tonic-gate } 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate /* 364*0Sstevel@tonic-gate * The VFS layer may progress with new mounts as far as we're concerned. 365*0Sstevel@tonic-gate * Allow them to progress if we were the last obstacle. 366*0Sstevel@tonic-gate */ 367*0Sstevel@tonic-gate static void 368*0Sstevel@tonic-gate resume_mounts(void) 369*0Sstevel@tonic-gate { 370*0Sstevel@tonic-gate mutex_enter(&mount_lock); 371*0Sstevel@tonic-gate if (++mounts_in_progress == 0) 372*0Sstevel@tonic-gate cv_broadcast(&mount_cv); 373*0Sstevel@tonic-gate mutex_exit(&mount_lock); 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate /* 377*0Sstevel@tonic-gate * The VFS layer is busy with a mount; zones should wait until all 378*0Sstevel@tonic-gate * mounts are completed to progress. 379*0Sstevel@tonic-gate */ 380*0Sstevel@tonic-gate void 381*0Sstevel@tonic-gate mount_in_progress(void) 382*0Sstevel@tonic-gate { 383*0Sstevel@tonic-gate mutex_enter(&mount_lock); 384*0Sstevel@tonic-gate while (mounts_in_progress < 0) 385*0Sstevel@tonic-gate cv_wait(&mount_cv, &mount_lock); 386*0Sstevel@tonic-gate mounts_in_progress++; 387*0Sstevel@tonic-gate mutex_exit(&mount_lock); 388*0Sstevel@tonic-gate } 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate /* 391*0Sstevel@tonic-gate * VFS is done with one mount; wake up any waiting block_mounts() 392*0Sstevel@tonic-gate * callers if this is the last mount. 393*0Sstevel@tonic-gate */ 394*0Sstevel@tonic-gate void 395*0Sstevel@tonic-gate mount_completed(void) 396*0Sstevel@tonic-gate { 397*0Sstevel@tonic-gate mutex_enter(&mount_lock); 398*0Sstevel@tonic-gate if (--mounts_in_progress == 0) 399*0Sstevel@tonic-gate cv_broadcast(&mount_cv); 400*0Sstevel@tonic-gate mutex_exit(&mount_lock); 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate /* 404*0Sstevel@tonic-gate * ZSD routines. 405*0Sstevel@tonic-gate * 406*0Sstevel@tonic-gate * Zone Specific Data (ZSD) is modeled after Thread Specific Data as 407*0Sstevel@tonic-gate * defined by the pthread_key_create() and related interfaces. 408*0Sstevel@tonic-gate * 409*0Sstevel@tonic-gate * Kernel subsystems may register one or more data items and/or 410*0Sstevel@tonic-gate * callbacks to be executed when a zone is created, shutdown, or 411*0Sstevel@tonic-gate * destroyed. 412*0Sstevel@tonic-gate * 413*0Sstevel@tonic-gate * Unlike the thread counterpart, destructor callbacks will be executed 414*0Sstevel@tonic-gate * even if the data pointer is NULL and/or there are no constructor 415*0Sstevel@tonic-gate * callbacks, so it is the responsibility of such callbacks to check for 416*0Sstevel@tonic-gate * NULL data values if necessary. 417*0Sstevel@tonic-gate * 418*0Sstevel@tonic-gate * The locking strategy and overall picture is as follows: 419*0Sstevel@tonic-gate * 420*0Sstevel@tonic-gate * When someone calls zone_key_create(), a template ZSD entry is added to the 421*0Sstevel@tonic-gate * global list "zsd_registered_keys", protected by zsd_key_lock. The 422*0Sstevel@tonic-gate * constructor callback is called immediately on all existing zones, and a 423*0Sstevel@tonic-gate * copy of the ZSD entry added to the per-zone zone_zsd list (protected by 424*0Sstevel@tonic-gate * zone_lock). As this operation requires the list of zones, the list of 425*0Sstevel@tonic-gate * registered keys, and the per-zone list of ZSD entries to remain constant 426*0Sstevel@tonic-gate * throughout the entire operation, it must grab zonehash_lock, zone_lock for 427*0Sstevel@tonic-gate * all existing zones, and zsd_key_lock, in that order. Similar locking is 428*0Sstevel@tonic-gate * needed when zone_key_delete() is called. It is thus sufficient to hold 429*0Sstevel@tonic-gate * zsd_key_lock *or* zone_lock to prevent additions to or removals from the 430*0Sstevel@tonic-gate * per-zone zone_zsd list. 431*0Sstevel@tonic-gate * 432*0Sstevel@tonic-gate * Note that this implementation does not make a copy of the ZSD entry if a 433*0Sstevel@tonic-gate * constructor callback is not provided. A zone_getspecific() on such an 434*0Sstevel@tonic-gate * uninitialized ZSD entry will return NULL. 435*0Sstevel@tonic-gate * 436*0Sstevel@tonic-gate * When new zones are created constructor callbacks for all registered ZSD 437*0Sstevel@tonic-gate * entries will be called. 438*0Sstevel@tonic-gate * 439*0Sstevel@tonic-gate * The framework does not provide any locking around zone_getspecific() and 440*0Sstevel@tonic-gate * zone_setspecific() apart from that needed for internal consistency, so 441*0Sstevel@tonic-gate * callers interested in atomic "test-and-set" semantics will need to provide 442*0Sstevel@tonic-gate * their own locking. 443*0Sstevel@tonic-gate */ 444*0Sstevel@tonic-gate void 445*0Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t), 446*0Sstevel@tonic-gate void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *)) 447*0Sstevel@tonic-gate { 448*0Sstevel@tonic-gate struct zsd_entry *zsdp; 449*0Sstevel@tonic-gate struct zsd_entry *t; 450*0Sstevel@tonic-gate struct zone *zone; 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP); 453*0Sstevel@tonic-gate zsdp->zsd_data = NULL; 454*0Sstevel@tonic-gate zsdp->zsd_create = create; 455*0Sstevel@tonic-gate zsdp->zsd_shutdown = shutdown; 456*0Sstevel@tonic-gate zsdp->zsd_destroy = destroy; 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* stop the world */ 459*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 460*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 461*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 464*0Sstevel@tonic-gate *keyp = zsdp->zsd_key = ++zsd_keyval; 465*0Sstevel@tonic-gate ASSERT(zsd_keyval != 0); 466*0Sstevel@tonic-gate list_insert_tail(&zsd_registered_keys, zsdp); 467*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate if (create != NULL) { 470*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 471*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 472*0Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 473*0Sstevel@tonic-gate t->zsd_key = *keyp; 474*0Sstevel@tonic-gate t->zsd_data = (*create)(zone->zone_id); 475*0Sstevel@tonic-gate t->zsd_create = create; 476*0Sstevel@tonic-gate t->zsd_shutdown = shutdown; 477*0Sstevel@tonic-gate t->zsd_destroy = destroy; 478*0Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 479*0Sstevel@tonic-gate } 480*0Sstevel@tonic-gate } 481*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 482*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 483*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 484*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 485*0Sstevel@tonic-gate } 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gate /* 488*0Sstevel@tonic-gate * Helper function to find the zsd_entry associated with the key in the 489*0Sstevel@tonic-gate * given list. 490*0Sstevel@tonic-gate */ 491*0Sstevel@tonic-gate static struct zsd_entry * 492*0Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key) 493*0Sstevel@tonic-gate { 494*0Sstevel@tonic-gate struct zsd_entry *zsd; 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) { 497*0Sstevel@tonic-gate if (zsd->zsd_key == key) { 498*0Sstevel@tonic-gate /* 499*0Sstevel@tonic-gate * Move to head of list to keep list in MRU order. 500*0Sstevel@tonic-gate */ 501*0Sstevel@tonic-gate if (zsd != list_head(l)) { 502*0Sstevel@tonic-gate list_remove(l, zsd); 503*0Sstevel@tonic-gate list_insert_head(l, zsd); 504*0Sstevel@tonic-gate } 505*0Sstevel@tonic-gate return (zsd); 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate } 508*0Sstevel@tonic-gate return (NULL); 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate /* 512*0Sstevel@tonic-gate * Function called when a module is being unloaded, or otherwise wishes 513*0Sstevel@tonic-gate * to unregister its ZSD key and callbacks. 514*0Sstevel@tonic-gate */ 515*0Sstevel@tonic-gate int 516*0Sstevel@tonic-gate zone_key_delete(zone_key_t key) 517*0Sstevel@tonic-gate { 518*0Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 519*0Sstevel@tonic-gate zone_t *zone; 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); /* Zone create/delete waits for us */ 522*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 523*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 524*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); /* lock all zones */ 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 527*0Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 528*0Sstevel@tonic-gate if (zsdp == NULL) 529*0Sstevel@tonic-gate goto notfound; 530*0Sstevel@tonic-gate list_remove(&zsd_registered_keys, zsdp); 531*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 534*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 535*0Sstevel@tonic-gate struct zsd_entry *del; 536*0Sstevel@tonic-gate void *data; 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gate if (!(zone->zone_flags & ZF_DESTROYED)) { 539*0Sstevel@tonic-gate del = zsd_find(&zone->zone_zsd, key); 540*0Sstevel@tonic-gate if (del != NULL) { 541*0Sstevel@tonic-gate data = del->zsd_data; 542*0Sstevel@tonic-gate ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown); 543*0Sstevel@tonic-gate ASSERT(del->zsd_destroy == zsdp->zsd_destroy); 544*0Sstevel@tonic-gate list_remove(&zone->zone_zsd, del); 545*0Sstevel@tonic-gate kmem_free(del, sizeof (*del)); 546*0Sstevel@tonic-gate } else { 547*0Sstevel@tonic-gate data = NULL; 548*0Sstevel@tonic-gate } 549*0Sstevel@tonic-gate if (zsdp->zsd_shutdown) 550*0Sstevel@tonic-gate zsdp->zsd_shutdown(zone->zone_id, data); 551*0Sstevel@tonic-gate if (zsdp->zsd_destroy) 552*0Sstevel@tonic-gate zsdp->zsd_destroy(zone->zone_id, data); 553*0Sstevel@tonic-gate } 554*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 557*0Sstevel@tonic-gate kmem_free(zsdp, sizeof (*zsdp)); 558*0Sstevel@tonic-gate return (0); 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate notfound: 561*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 562*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 563*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 564*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 565*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 566*0Sstevel@tonic-gate return (-1); 567*0Sstevel@tonic-gate } 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate /* 570*0Sstevel@tonic-gate * ZSD counterpart of pthread_setspecific(). 571*0Sstevel@tonic-gate */ 572*0Sstevel@tonic-gate int 573*0Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data) 574*0Sstevel@tonic-gate { 575*0Sstevel@tonic-gate struct zsd_entry *t; 576*0Sstevel@tonic-gate struct zsd_entry *zsdp = NULL; 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 579*0Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 580*0Sstevel@tonic-gate if (t != NULL) { 581*0Sstevel@tonic-gate /* 582*0Sstevel@tonic-gate * Replace old value with new 583*0Sstevel@tonic-gate */ 584*0Sstevel@tonic-gate t->zsd_data = (void *)data; 585*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 586*0Sstevel@tonic-gate return (0); 587*0Sstevel@tonic-gate } 588*0Sstevel@tonic-gate /* 589*0Sstevel@tonic-gate * If there was no previous value, go through the list of registered 590*0Sstevel@tonic-gate * keys. 591*0Sstevel@tonic-gate * 592*0Sstevel@tonic-gate * We avoid grabbing zsd_key_lock until we are sure we need it; this is 593*0Sstevel@tonic-gate * necessary for shutdown callbacks to be able to execute without fear 594*0Sstevel@tonic-gate * of deadlock. 595*0Sstevel@tonic-gate */ 596*0Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 597*0Sstevel@tonic-gate zsdp = zsd_find(&zsd_registered_keys, key); 598*0Sstevel@tonic-gate if (zsdp == NULL) { /* Key was not registered */ 599*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 600*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 601*0Sstevel@tonic-gate return (-1); 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gate /* 605*0Sstevel@tonic-gate * Add a zsd_entry to this zone, using the template we just retrieved 606*0Sstevel@tonic-gate * to initialize the constructor and destructor(s). 607*0Sstevel@tonic-gate */ 608*0Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 609*0Sstevel@tonic-gate t->zsd_key = key; 610*0Sstevel@tonic-gate t->zsd_data = (void *)data; 611*0Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 612*0Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 613*0Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 614*0Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 615*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 616*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 617*0Sstevel@tonic-gate return (0); 618*0Sstevel@tonic-gate } 619*0Sstevel@tonic-gate 620*0Sstevel@tonic-gate /* 621*0Sstevel@tonic-gate * ZSD counterpart of pthread_getspecific(). 622*0Sstevel@tonic-gate */ 623*0Sstevel@tonic-gate void * 624*0Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone) 625*0Sstevel@tonic-gate { 626*0Sstevel@tonic-gate struct zsd_entry *t; 627*0Sstevel@tonic-gate void *data; 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 630*0Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 631*0Sstevel@tonic-gate data = (t == NULL ? NULL : t->zsd_data); 632*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 633*0Sstevel@tonic-gate return (data); 634*0Sstevel@tonic-gate } 635*0Sstevel@tonic-gate 636*0Sstevel@tonic-gate /* 637*0Sstevel@tonic-gate * Function used to initialize a zone's list of ZSD callbacks and data 638*0Sstevel@tonic-gate * when the zone is being created. The callbacks are initialized from 639*0Sstevel@tonic-gate * the template list (zsd_registered_keys), and the constructor 640*0Sstevel@tonic-gate * callback executed (if one exists). 641*0Sstevel@tonic-gate * 642*0Sstevel@tonic-gate * This is called before the zone is made publicly available, hence no 643*0Sstevel@tonic-gate * need to grab zone_lock. 644*0Sstevel@tonic-gate * 645*0Sstevel@tonic-gate * Although we grab and release zsd_key_lock, new entries cannot be 646*0Sstevel@tonic-gate * added to or removed from the zsd_registered_keys list until we 647*0Sstevel@tonic-gate * release zonehash_lock, so there isn't a window for a 648*0Sstevel@tonic-gate * zone_key_create() to come in after we've dropped zsd_key_lock but 649*0Sstevel@tonic-gate * before the zone is added to the zone list, such that the constructor 650*0Sstevel@tonic-gate * callbacks aren't executed for the new zone. 651*0Sstevel@tonic-gate */ 652*0Sstevel@tonic-gate static void 653*0Sstevel@tonic-gate zone_zsd_configure(zone_t *zone) 654*0Sstevel@tonic-gate { 655*0Sstevel@tonic-gate struct zsd_entry *zsdp; 656*0Sstevel@tonic-gate struct zsd_entry *t; 657*0Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 660*0Sstevel@tonic-gate ASSERT(list_head(&zone->zone_zsd) == NULL); 661*0Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 662*0Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 663*0Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 664*0Sstevel@tonic-gate if (zsdp->zsd_create != NULL) { 665*0Sstevel@tonic-gate t = kmem_alloc(sizeof (*t), KM_SLEEP); 666*0Sstevel@tonic-gate t->zsd_key = zsdp->zsd_key; 667*0Sstevel@tonic-gate t->zsd_create = zsdp->zsd_create; 668*0Sstevel@tonic-gate t->zsd_data = (*t->zsd_create)(zoneid); 669*0Sstevel@tonic-gate t->zsd_shutdown = zsdp->zsd_shutdown; 670*0Sstevel@tonic-gate t->zsd_destroy = zsdp->zsd_destroy; 671*0Sstevel@tonic-gate list_insert_tail(&zone->zone_zsd, t); 672*0Sstevel@tonic-gate } 673*0Sstevel@tonic-gate } 674*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 675*0Sstevel@tonic-gate } 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY }; 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate /* 680*0Sstevel@tonic-gate * Helper function to execute shutdown or destructor callbacks. 681*0Sstevel@tonic-gate */ 682*0Sstevel@tonic-gate static void 683*0Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct) 684*0Sstevel@tonic-gate { 685*0Sstevel@tonic-gate struct zsd_entry *zsdp; 686*0Sstevel@tonic-gate struct zsd_entry *t; 687*0Sstevel@tonic-gate zoneid_t zoneid = zone->zone_id; 688*0Sstevel@tonic-gate 689*0Sstevel@tonic-gate ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY); 690*0Sstevel@tonic-gate ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY); 691*0Sstevel@tonic-gate ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN); 692*0Sstevel@tonic-gate 693*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 694*0Sstevel@tonic-gate if (ct == ZSD_DESTROY) { 695*0Sstevel@tonic-gate if (zone->zone_flags & ZF_DESTROYED) { 696*0Sstevel@tonic-gate /* 697*0Sstevel@tonic-gate * Make sure destructors are only called once. 698*0Sstevel@tonic-gate */ 699*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 700*0Sstevel@tonic-gate return; 701*0Sstevel@tonic-gate } 702*0Sstevel@tonic-gate zone->zone_flags |= ZF_DESTROYED; 703*0Sstevel@tonic-gate } 704*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gate /* 707*0Sstevel@tonic-gate * Both zsd_key_lock and zone_lock need to be held in order to add or 708*0Sstevel@tonic-gate * remove a ZSD key, (either globally as part of 709*0Sstevel@tonic-gate * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is 710*0Sstevel@tonic-gate * possible through zone_setspecific()), so it's sufficient to hold 711*0Sstevel@tonic-gate * zsd_key_lock here. 712*0Sstevel@tonic-gate * 713*0Sstevel@tonic-gate * This is a good thing, since we don't want to recursively try to grab 714*0Sstevel@tonic-gate * zone_lock if a callback attempts to do something like a crfree() or 715*0Sstevel@tonic-gate * zone_rele(). 716*0Sstevel@tonic-gate */ 717*0Sstevel@tonic-gate mutex_enter(&zsd_key_lock); 718*0Sstevel@tonic-gate for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL; 719*0Sstevel@tonic-gate zsdp = list_next(&zsd_registered_keys, zsdp)) { 720*0Sstevel@tonic-gate zone_key_t key = zsdp->zsd_key; 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate /* Skip if no callbacks registered */ 723*0Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL) 724*0Sstevel@tonic-gate continue; 725*0Sstevel@tonic-gate if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL) 726*0Sstevel@tonic-gate continue; 727*0Sstevel@tonic-gate /* 728*0Sstevel@tonic-gate * Call the callback with the zone-specific data if we can find 729*0Sstevel@tonic-gate * any, otherwise with NULL. 730*0Sstevel@tonic-gate */ 731*0Sstevel@tonic-gate t = zsd_find(&zone->zone_zsd, key); 732*0Sstevel@tonic-gate if (t != NULL) { 733*0Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 734*0Sstevel@tonic-gate t->zsd_shutdown(zoneid, t->zsd_data); 735*0Sstevel@tonic-gate } else { 736*0Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 737*0Sstevel@tonic-gate t->zsd_destroy(zoneid, t->zsd_data); 738*0Sstevel@tonic-gate } 739*0Sstevel@tonic-gate } else { 740*0Sstevel@tonic-gate if (ct == ZSD_SHUTDOWN) { 741*0Sstevel@tonic-gate zsdp->zsd_shutdown(zoneid, NULL); 742*0Sstevel@tonic-gate } else { 743*0Sstevel@tonic-gate ASSERT(ct == ZSD_DESTROY); 744*0Sstevel@tonic-gate zsdp->zsd_destroy(zoneid, NULL); 745*0Sstevel@tonic-gate } 746*0Sstevel@tonic-gate } 747*0Sstevel@tonic-gate } 748*0Sstevel@tonic-gate mutex_exit(&zsd_key_lock); 749*0Sstevel@tonic-gate } 750*0Sstevel@tonic-gate 751*0Sstevel@tonic-gate /* 752*0Sstevel@tonic-gate * Called when the zone is going away; free ZSD-related memory, and 753*0Sstevel@tonic-gate * destroy the zone_zsd list. 754*0Sstevel@tonic-gate */ 755*0Sstevel@tonic-gate static void 756*0Sstevel@tonic-gate zone_free_zsd(zone_t *zone) 757*0Sstevel@tonic-gate { 758*0Sstevel@tonic-gate struct zsd_entry *t, *next; 759*0Sstevel@tonic-gate 760*0Sstevel@tonic-gate /* 761*0Sstevel@tonic-gate * Free all the zsd_entry's we had on this zone. 762*0Sstevel@tonic-gate */ 763*0Sstevel@tonic-gate for (t = list_head(&zone->zone_zsd); t != NULL; t = next) { 764*0Sstevel@tonic-gate next = list_next(&zone->zone_zsd, t); 765*0Sstevel@tonic-gate list_remove(&zone->zone_zsd, t); 766*0Sstevel@tonic-gate kmem_free(t, sizeof (*t)); 767*0Sstevel@tonic-gate } 768*0Sstevel@tonic-gate list_destroy(&zone->zone_zsd); 769*0Sstevel@tonic-gate } 770*0Sstevel@tonic-gate 771*0Sstevel@tonic-gate /* 772*0Sstevel@tonic-gate * zone.cpu-shares resource control support. 773*0Sstevel@tonic-gate */ 774*0Sstevel@tonic-gate /*ARGSUSED*/ 775*0Sstevel@tonic-gate static rctl_qty_t 776*0Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p) 777*0Sstevel@tonic-gate { 778*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 779*0Sstevel@tonic-gate return (p->p_zone->zone_shares); 780*0Sstevel@tonic-gate } 781*0Sstevel@tonic-gate 782*0Sstevel@tonic-gate /*ARGSUSED*/ 783*0Sstevel@tonic-gate static int 784*0Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, 785*0Sstevel@tonic-gate rctl_qty_t nv) 786*0Sstevel@tonic-gate { 787*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 788*0Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 789*0Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 790*0Sstevel@tonic-gate return (0); 791*0Sstevel@tonic-gate 792*0Sstevel@tonic-gate e->rcep_p.zone->zone_shares = nv; 793*0Sstevel@tonic-gate return (0); 794*0Sstevel@tonic-gate } 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = { 797*0Sstevel@tonic-gate rcop_no_action, 798*0Sstevel@tonic-gate zone_cpu_shares_usage, 799*0Sstevel@tonic-gate zone_cpu_shares_set, 800*0Sstevel@tonic-gate rcop_no_test 801*0Sstevel@tonic-gate }; 802*0Sstevel@tonic-gate 803*0Sstevel@tonic-gate /*ARGSUSED*/ 804*0Sstevel@tonic-gate static rctl_qty_t 805*0Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p) 806*0Sstevel@tonic-gate { 807*0Sstevel@tonic-gate rctl_qty_t nlwps; 808*0Sstevel@tonic-gate zone_t *zone = p->p_zone; 809*0Sstevel@tonic-gate 810*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 811*0Sstevel@tonic-gate 812*0Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 813*0Sstevel@tonic-gate nlwps = zone->zone_nlwps; 814*0Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 815*0Sstevel@tonic-gate 816*0Sstevel@tonic-gate return (nlwps); 817*0Sstevel@tonic-gate } 818*0Sstevel@tonic-gate 819*0Sstevel@tonic-gate /*ARGSUSED*/ 820*0Sstevel@tonic-gate static int 821*0Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl, 822*0Sstevel@tonic-gate rctl_qty_t incr, uint_t flags) 823*0Sstevel@tonic-gate { 824*0Sstevel@tonic-gate rctl_qty_t nlwps; 825*0Sstevel@tonic-gate 826*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 827*0Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 828*0Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 829*0Sstevel@tonic-gate return (0); 830*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock))); 831*0Sstevel@tonic-gate nlwps = e->rcep_p.zone->zone_nlwps; 832*0Sstevel@tonic-gate 833*0Sstevel@tonic-gate if (nlwps + incr > rcntl->rcv_value) 834*0Sstevel@tonic-gate return (1); 835*0Sstevel@tonic-gate 836*0Sstevel@tonic-gate return (0); 837*0Sstevel@tonic-gate } 838*0Sstevel@tonic-gate 839*0Sstevel@tonic-gate /*ARGSUSED*/ 840*0Sstevel@tonic-gate static int 841*0Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) { 842*0Sstevel@tonic-gate 843*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 844*0Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_ZONE); 845*0Sstevel@tonic-gate if (e->rcep_p.zone == NULL) 846*0Sstevel@tonic-gate return (0); 847*0Sstevel@tonic-gate e->rcep_p.zone->zone_nlwps_ctl = nv; 848*0Sstevel@tonic-gate return (0); 849*0Sstevel@tonic-gate } 850*0Sstevel@tonic-gate 851*0Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = { 852*0Sstevel@tonic-gate rcop_no_action, 853*0Sstevel@tonic-gate zone_lwps_usage, 854*0Sstevel@tonic-gate zone_lwps_set, 855*0Sstevel@tonic-gate zone_lwps_test, 856*0Sstevel@tonic-gate }; 857*0Sstevel@tonic-gate 858*0Sstevel@tonic-gate /* 859*0Sstevel@tonic-gate * Helper function to brand the zone with a unique ID. 860*0Sstevel@tonic-gate */ 861*0Sstevel@tonic-gate static void 862*0Sstevel@tonic-gate zone_uniqid(zone_t *zone) 863*0Sstevel@tonic-gate { 864*0Sstevel@tonic-gate static uint64_t uniqid = 0; 865*0Sstevel@tonic-gate 866*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 867*0Sstevel@tonic-gate zone->zone_uniqid = uniqid++; 868*0Sstevel@tonic-gate } 869*0Sstevel@tonic-gate 870*0Sstevel@tonic-gate /* 871*0Sstevel@tonic-gate * Returns a held pointer to the "kcred" for the specified zone. 872*0Sstevel@tonic-gate */ 873*0Sstevel@tonic-gate struct cred * 874*0Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid) 875*0Sstevel@tonic-gate { 876*0Sstevel@tonic-gate zone_t *zone; 877*0Sstevel@tonic-gate cred_t *cr; 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) 880*0Sstevel@tonic-gate return (NULL); 881*0Sstevel@tonic-gate cr = zone->zone_kcred; 882*0Sstevel@tonic-gate crhold(cr); 883*0Sstevel@tonic-gate zone_rele(zone); 884*0Sstevel@tonic-gate return (cr); 885*0Sstevel@tonic-gate } 886*0Sstevel@tonic-gate 887*0Sstevel@tonic-gate /* 888*0Sstevel@tonic-gate * Called very early on in boot to initialize the ZSD list so that 889*0Sstevel@tonic-gate * zone_key_create() can be called before zone_init(). It also initializes 890*0Sstevel@tonic-gate * portions of zone0 which may be used before zone_init() is called. The 891*0Sstevel@tonic-gate * variable "global_zone" will be set when zone0 is fully initialized by 892*0Sstevel@tonic-gate * zone_init(). 893*0Sstevel@tonic-gate */ 894*0Sstevel@tonic-gate void 895*0Sstevel@tonic-gate zone_zsd_init(void) 896*0Sstevel@tonic-gate { 897*0Sstevel@tonic-gate mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL); 898*0Sstevel@tonic-gate mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL); 899*0Sstevel@tonic-gate list_create(&zsd_registered_keys, sizeof (struct zsd_entry), 900*0Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 901*0Sstevel@tonic-gate list_create(&zone_active, sizeof (zone_t), 902*0Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 903*0Sstevel@tonic-gate list_create(&zone_deathrow, sizeof (zone_t), 904*0Sstevel@tonic-gate offsetof(zone_t, zone_linkage)); 905*0Sstevel@tonic-gate 906*0Sstevel@tonic-gate mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL); 907*0Sstevel@tonic-gate mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 908*0Sstevel@tonic-gate zone0.zone_shares = 1; 909*0Sstevel@tonic-gate zone0.zone_nlwps_ctl = INT_MAX; 910*0Sstevel@tonic-gate zone0.zone_name = GLOBAL_ZONENAME; 911*0Sstevel@tonic-gate zone0.zone_nodename = utsname.nodename; 912*0Sstevel@tonic-gate zone0.zone_domain = srpc_domain; 913*0Sstevel@tonic-gate zone0.zone_ref = 1; 914*0Sstevel@tonic-gate zone0.zone_id = GLOBAL_ZONEID; 915*0Sstevel@tonic-gate zone0.zone_status = ZONE_IS_RUNNING; 916*0Sstevel@tonic-gate zone0.zone_rootpath = "/"; 917*0Sstevel@tonic-gate zone0.zone_rootpathlen = 2; 918*0Sstevel@tonic-gate zone0.zone_psetid = ZONE_PS_INVAL; 919*0Sstevel@tonic-gate zone0.zone_ncpus = 0; 920*0Sstevel@tonic-gate zone0.zone_ncpus_online = 0; 921*0Sstevel@tonic-gate zone0.zone_proc_initpid = 1; 922*0Sstevel@tonic-gate list_create(&zone0.zone_zsd, sizeof (struct zsd_entry), 923*0Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 924*0Sstevel@tonic-gate list_insert_head(&zone_active, &zone0); 925*0Sstevel@tonic-gate 926*0Sstevel@tonic-gate /* 927*0Sstevel@tonic-gate * The root filesystem is not mounted yet, so zone_rootvp cannot be set 928*0Sstevel@tonic-gate * to anything meaningful. It is assigned to be 'rootdir' in 929*0Sstevel@tonic-gate * vfs_mountroot(). 930*0Sstevel@tonic-gate */ 931*0Sstevel@tonic-gate zone0.zone_rootvp = NULL; 932*0Sstevel@tonic-gate zone0.zone_vfslist = NULL; 933*0Sstevel@tonic-gate zone0.zone_bootargs = NULL; 934*0Sstevel@tonic-gate zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 935*0Sstevel@tonic-gate /* 936*0Sstevel@tonic-gate * The global zone has all privileges 937*0Sstevel@tonic-gate */ 938*0Sstevel@tonic-gate priv_fillset(zone0.zone_privset); 939*0Sstevel@tonic-gate /* 940*0Sstevel@tonic-gate * Add p0 to the global zone 941*0Sstevel@tonic-gate */ 942*0Sstevel@tonic-gate zone0.zone_zsched = &p0; 943*0Sstevel@tonic-gate p0.p_zone = &zone0; 944*0Sstevel@tonic-gate } 945*0Sstevel@tonic-gate 946*0Sstevel@tonic-gate /* 947*0Sstevel@tonic-gate * Called by main() to initialize the zones framework. 948*0Sstevel@tonic-gate */ 949*0Sstevel@tonic-gate void 950*0Sstevel@tonic-gate zone_init(void) 951*0Sstevel@tonic-gate { 952*0Sstevel@tonic-gate rctl_dict_entry_t *rde; 953*0Sstevel@tonic-gate rctl_val_t *dval; 954*0Sstevel@tonic-gate rctl_set_t *set; 955*0Sstevel@tonic-gate rctl_alloc_gp_t *gp; 956*0Sstevel@tonic-gate rctl_entity_p_t e; 957*0Sstevel@tonic-gate 958*0Sstevel@tonic-gate ASSERT(curproc == &p0); 959*0Sstevel@tonic-gate 960*0Sstevel@tonic-gate /* 961*0Sstevel@tonic-gate * Create ID space for zone IDs. ID 0 is reserved for the 962*0Sstevel@tonic-gate * global zone. 963*0Sstevel@tonic-gate */ 964*0Sstevel@tonic-gate zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID); 965*0Sstevel@tonic-gate 966*0Sstevel@tonic-gate /* 967*0Sstevel@tonic-gate * Initialize generic zone resource controls, if any. 968*0Sstevel@tonic-gate */ 969*0Sstevel@tonic-gate rc_zone_cpu_shares = rctl_register("zone.cpu-shares", 970*0Sstevel@tonic-gate RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER | 971*0Sstevel@tonic-gate RCTL_GLOBAL_NOBASIC | 972*0Sstevel@tonic-gate RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES, 973*0Sstevel@tonic-gate &zone_cpu_shares_ops); 974*0Sstevel@tonic-gate 975*0Sstevel@tonic-gate rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE, 976*0Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT, 977*0Sstevel@tonic-gate INT_MAX, INT_MAX, &zone_lwps_ops); 978*0Sstevel@tonic-gate /* 979*0Sstevel@tonic-gate * Create a rctl_val with PRIVILEGED, NOACTION, value = 1. Then attach 980*0Sstevel@tonic-gate * this at the head of the rctl_dict_entry for ``zone.cpu-shares''. 981*0Sstevel@tonic-gate */ 982*0Sstevel@tonic-gate dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 983*0Sstevel@tonic-gate bzero(dval, sizeof (rctl_val_t)); 984*0Sstevel@tonic-gate dval->rcv_value = 1; 985*0Sstevel@tonic-gate dval->rcv_privilege = RCPRIV_PRIVILEGED; 986*0Sstevel@tonic-gate dval->rcv_flagaction = RCTL_LOCAL_NOACTION; 987*0Sstevel@tonic-gate dval->rcv_action_recip_pid = -1; 988*0Sstevel@tonic-gate 989*0Sstevel@tonic-gate rde = rctl_dict_lookup("zone.cpu-shares"); 990*0Sstevel@tonic-gate (void) rctl_val_list_insert(&rde->rcd_default_value, dval); 991*0Sstevel@tonic-gate 992*0Sstevel@tonic-gate /* 993*0Sstevel@tonic-gate * Initialize the ``global zone''. 994*0Sstevel@tonic-gate */ 995*0Sstevel@tonic-gate set = rctl_set_create(); 996*0Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 997*0Sstevel@tonic-gate mutex_enter(&p0.p_lock); 998*0Sstevel@tonic-gate e.rcep_p.zone = &zone0; 999*0Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 1000*0Sstevel@tonic-gate zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set, 1001*0Sstevel@tonic-gate gp); 1002*0Sstevel@tonic-gate 1003*0Sstevel@tonic-gate zone0.zone_nlwps = p0.p_lwpcnt; 1004*0Sstevel@tonic-gate zone0.zone_ntasks = 1; 1005*0Sstevel@tonic-gate mutex_exit(&p0.p_lock); 1006*0Sstevel@tonic-gate rctl_prealloc_destroy(gp); 1007*0Sstevel@tonic-gate /* 1008*0Sstevel@tonic-gate * pool_default hasn't been initialized yet, so we let pool_init() take 1009*0Sstevel@tonic-gate * care of making the global zone is in the default pool. 1010*0Sstevel@tonic-gate */ 1011*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1012*0Sstevel@tonic-gate zone_uniqid(&zone0); 1013*0Sstevel@tonic-gate ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID); 1014*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1015*0Sstevel@tonic-gate zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size, 1016*0Sstevel@tonic-gate mod_hash_null_valdtor); 1017*0Sstevel@tonic-gate zonehashbyname = mod_hash_create_strhash("zone_by_name", 1018*0Sstevel@tonic-gate zone_hash_size, mod_hash_null_valdtor); 1019*0Sstevel@tonic-gate zonecount = 1; 1020*0Sstevel@tonic-gate 1021*0Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID, 1022*0Sstevel@tonic-gate (mod_hash_val_t)&zone0); 1023*0Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name, 1024*0Sstevel@tonic-gate (mod_hash_val_t)&zone0); 1025*0Sstevel@tonic-gate /* 1026*0Sstevel@tonic-gate * We avoid setting zone_kcred until now, since kcred is initialized 1027*0Sstevel@tonic-gate * sometime after zone_zsd_init() and before zone_init(). 1028*0Sstevel@tonic-gate */ 1029*0Sstevel@tonic-gate zone0.zone_kcred = kcred; 1030*0Sstevel@tonic-gate /* 1031*0Sstevel@tonic-gate * The global zone is fully initialized (except for zone_rootvp which 1032*0Sstevel@tonic-gate * will be set when the root filesystem is mounted). 1033*0Sstevel@tonic-gate */ 1034*0Sstevel@tonic-gate global_zone = &zone0; 1035*0Sstevel@tonic-gate } 1036*0Sstevel@tonic-gate 1037*0Sstevel@tonic-gate static void 1038*0Sstevel@tonic-gate zone_free(zone_t *zone) 1039*0Sstevel@tonic-gate { 1040*0Sstevel@tonic-gate ASSERT(zone != global_zone); 1041*0Sstevel@tonic-gate ASSERT(zone->zone_ntasks == 0); 1042*0Sstevel@tonic-gate ASSERT(zone->zone_nlwps == 0); 1043*0Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 0); 1044*0Sstevel@tonic-gate ASSERT(zone->zone_kcred == NULL); 1045*0Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_DEAD || 1046*0Sstevel@tonic-gate zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 1047*0Sstevel@tonic-gate 1048*0Sstevel@tonic-gate /* remove from deathrow list */ 1049*0Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_DEAD) { 1050*0Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 1051*0Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 1052*0Sstevel@tonic-gate list_remove(&zone_deathrow, zone); 1053*0Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 1054*0Sstevel@tonic-gate } 1055*0Sstevel@tonic-gate 1056*0Sstevel@tonic-gate zone_free_zsd(zone); 1057*0Sstevel@tonic-gate 1058*0Sstevel@tonic-gate if (zone->zone_rootvp != NULL) 1059*0Sstevel@tonic-gate VN_RELE(zone->zone_rootvp); 1060*0Sstevel@tonic-gate if (zone->zone_rootpath) 1061*0Sstevel@tonic-gate kmem_free(zone->zone_rootpath, zone->zone_rootpathlen); 1062*0Sstevel@tonic-gate if (zone->zone_name != NULL) 1063*0Sstevel@tonic-gate kmem_free(zone->zone_name, ZONENAME_MAX); 1064*0Sstevel@tonic-gate if (zone->zone_nodename != NULL) 1065*0Sstevel@tonic-gate kmem_free(zone->zone_nodename, _SYS_NMLN); 1066*0Sstevel@tonic-gate if (zone->zone_domain != NULL) 1067*0Sstevel@tonic-gate kmem_free(zone->zone_domain, _SYS_NMLN); 1068*0Sstevel@tonic-gate if (zone->zone_privset != NULL) 1069*0Sstevel@tonic-gate kmem_free(zone->zone_privset, sizeof (priv_set_t)); 1070*0Sstevel@tonic-gate if (zone->zone_rctls != NULL) 1071*0Sstevel@tonic-gate rctl_set_free(zone->zone_rctls); 1072*0Sstevel@tonic-gate if (zone->zone_bootargs != NULL) 1073*0Sstevel@tonic-gate kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX); 1074*0Sstevel@tonic-gate id_free(zoneid_space, zone->zone_id); 1075*0Sstevel@tonic-gate mutex_destroy(&zone->zone_lock); 1076*0Sstevel@tonic-gate cv_destroy(&zone->zone_cv); 1077*0Sstevel@tonic-gate kmem_free(zone, sizeof (zone_t)); 1078*0Sstevel@tonic-gate } 1079*0Sstevel@tonic-gate 1080*0Sstevel@tonic-gate /* 1081*0Sstevel@tonic-gate * See block comment at the top of this file for information about zone 1082*0Sstevel@tonic-gate * status values. 1083*0Sstevel@tonic-gate */ 1084*0Sstevel@tonic-gate /* 1085*0Sstevel@tonic-gate * Convenience function for setting zone status. 1086*0Sstevel@tonic-gate */ 1087*0Sstevel@tonic-gate static void 1088*0Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status) 1089*0Sstevel@tonic-gate { 1090*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zone_status_lock)); 1091*0Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE && 1092*0Sstevel@tonic-gate status >= zone_status_get(zone)); 1093*0Sstevel@tonic-gate zone->zone_status = status; 1094*0Sstevel@tonic-gate cv_broadcast(&zone->zone_cv); 1095*0Sstevel@tonic-gate } 1096*0Sstevel@tonic-gate 1097*0Sstevel@tonic-gate /* 1098*0Sstevel@tonic-gate * Public function to retrieve the zone status. The zone status may 1099*0Sstevel@tonic-gate * change after it is retrieved. 1100*0Sstevel@tonic-gate */ 1101*0Sstevel@tonic-gate zone_status_t 1102*0Sstevel@tonic-gate zone_status_get(zone_t *zone) 1103*0Sstevel@tonic-gate { 1104*0Sstevel@tonic-gate return (zone->zone_status); 1105*0Sstevel@tonic-gate } 1106*0Sstevel@tonic-gate 1107*0Sstevel@tonic-gate static int 1108*0Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs) 1109*0Sstevel@tonic-gate { 1110*0Sstevel@tonic-gate char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP); 1111*0Sstevel@tonic-gate size_t len; 1112*0Sstevel@tonic-gate int err; 1113*0Sstevel@tonic-gate 1114*0Sstevel@tonic-gate err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len); 1115*0Sstevel@tonic-gate if (err != 0) { 1116*0Sstevel@tonic-gate kmem_free(bootargs, ZONEBOOTARGS_MAX); 1117*0Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 1118*0Sstevel@tonic-gate } 1119*0Sstevel@tonic-gate bootargs[len] = '\0'; 1120*0Sstevel@tonic-gate 1121*0Sstevel@tonic-gate ASSERT(zone->zone_bootargs == NULL); 1122*0Sstevel@tonic-gate zone->zone_bootargs = bootargs; 1123*0Sstevel@tonic-gate return (0); 1124*0Sstevel@tonic-gate } 1125*0Sstevel@tonic-gate 1126*0Sstevel@tonic-gate /* 1127*0Sstevel@tonic-gate * Block indefinitely waiting for (zone_status >= status) 1128*0Sstevel@tonic-gate */ 1129*0Sstevel@tonic-gate void 1130*0Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status) 1131*0Sstevel@tonic-gate { 1132*0Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 1133*0Sstevel@tonic-gate 1134*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1135*0Sstevel@tonic-gate while (zone->zone_status < status) { 1136*0Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 1137*0Sstevel@tonic-gate } 1138*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1139*0Sstevel@tonic-gate } 1140*0Sstevel@tonic-gate 1141*0Sstevel@tonic-gate /* 1142*0Sstevel@tonic-gate * Private CPR-safe version of zone_status_wait(). 1143*0Sstevel@tonic-gate */ 1144*0Sstevel@tonic-gate static void 1145*0Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str) 1146*0Sstevel@tonic-gate { 1147*0Sstevel@tonic-gate callb_cpr_t cprinfo; 1148*0Sstevel@tonic-gate 1149*0Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 1150*0Sstevel@tonic-gate 1151*0Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr, 1152*0Sstevel@tonic-gate str); 1153*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1154*0Sstevel@tonic-gate while (zone->zone_status < status) { 1155*0Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 1156*0Sstevel@tonic-gate cv_wait(&zone->zone_cv, &zone_status_lock); 1157*0Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock); 1158*0Sstevel@tonic-gate } 1159*0Sstevel@tonic-gate /* 1160*0Sstevel@tonic-gate * zone_status_lock is implicitly released by the following. 1161*0Sstevel@tonic-gate */ 1162*0Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 1163*0Sstevel@tonic-gate } 1164*0Sstevel@tonic-gate 1165*0Sstevel@tonic-gate /* 1166*0Sstevel@tonic-gate * Block until zone enters requested state or signal is received. Return (0) 1167*0Sstevel@tonic-gate * if signaled, non-zero otherwise. 1168*0Sstevel@tonic-gate */ 1169*0Sstevel@tonic-gate int 1170*0Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status) 1171*0Sstevel@tonic-gate { 1172*0Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 1173*0Sstevel@tonic-gate 1174*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1175*0Sstevel@tonic-gate while (zone->zone_status < status) { 1176*0Sstevel@tonic-gate if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) { 1177*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1178*0Sstevel@tonic-gate return (0); 1179*0Sstevel@tonic-gate } 1180*0Sstevel@tonic-gate } 1181*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1182*0Sstevel@tonic-gate return (1); 1183*0Sstevel@tonic-gate } 1184*0Sstevel@tonic-gate 1185*0Sstevel@tonic-gate /* 1186*0Sstevel@tonic-gate * Block until the zone enters the requested state or the timeout expires, 1187*0Sstevel@tonic-gate * whichever happens first. Return (-1) if operation timed out, time remaining 1188*0Sstevel@tonic-gate * otherwise. 1189*0Sstevel@tonic-gate */ 1190*0Sstevel@tonic-gate clock_t 1191*0Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status) 1192*0Sstevel@tonic-gate { 1193*0Sstevel@tonic-gate clock_t timeleft = 0; 1194*0Sstevel@tonic-gate 1195*0Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 1196*0Sstevel@tonic-gate 1197*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1198*0Sstevel@tonic-gate while (zone->zone_status < status && timeleft != -1) { 1199*0Sstevel@tonic-gate timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim); 1200*0Sstevel@tonic-gate } 1201*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1202*0Sstevel@tonic-gate return (timeleft); 1203*0Sstevel@tonic-gate } 1204*0Sstevel@tonic-gate 1205*0Sstevel@tonic-gate /* 1206*0Sstevel@tonic-gate * Block until the zone enters the requested state, the current process is 1207*0Sstevel@tonic-gate * signaled, or the timeout expires, whichever happens first. Return (-1) if 1208*0Sstevel@tonic-gate * operation timed out, 0 if signaled, time remaining otherwise. 1209*0Sstevel@tonic-gate */ 1210*0Sstevel@tonic-gate clock_t 1211*0Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status) 1212*0Sstevel@tonic-gate { 1213*0Sstevel@tonic-gate clock_t timeleft = tim - lbolt; 1214*0Sstevel@tonic-gate 1215*0Sstevel@tonic-gate ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE); 1216*0Sstevel@tonic-gate 1217*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1218*0Sstevel@tonic-gate while (zone->zone_status < status) { 1219*0Sstevel@tonic-gate timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock, 1220*0Sstevel@tonic-gate tim); 1221*0Sstevel@tonic-gate if (timeleft <= 0) 1222*0Sstevel@tonic-gate break; 1223*0Sstevel@tonic-gate } 1224*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1225*0Sstevel@tonic-gate return (timeleft); 1226*0Sstevel@tonic-gate } 1227*0Sstevel@tonic-gate 1228*0Sstevel@tonic-gate /* 1229*0Sstevel@tonic-gate * Zones have two reference counts: one for references from credential 1230*0Sstevel@tonic-gate * structures (zone_cred_ref), and one (zone_ref) for everything else. 1231*0Sstevel@tonic-gate * This is so we can allow a zone to be rebooted while there are still 1232*0Sstevel@tonic-gate * outstanding cred references, since certain drivers cache dblks (which 1233*0Sstevel@tonic-gate * implicitly results in cached creds). We wait for zone_ref to drop to 1234*0Sstevel@tonic-gate * 0 (actually 1), but not zone_cred_ref. The zone structure itself is 1235*0Sstevel@tonic-gate * later freed when the zone_cred_ref drops to 0, though nothing other 1236*0Sstevel@tonic-gate * than the zone id and privilege set should be accessed once the zone 1237*0Sstevel@tonic-gate * is "dead". 1238*0Sstevel@tonic-gate * 1239*0Sstevel@tonic-gate * A debugging flag, zone_wait_for_cred, can be set to a non-zero value 1240*0Sstevel@tonic-gate * to force halt/reboot to block waiting for the zone_cred_ref to drop 1241*0Sstevel@tonic-gate * to 0. This can be useful to flush out other sources of cached creds 1242*0Sstevel@tonic-gate * that may be less innocuous than the driver case. 1243*0Sstevel@tonic-gate */ 1244*0Sstevel@tonic-gate 1245*0Sstevel@tonic-gate int zone_wait_for_cred = 0; 1246*0Sstevel@tonic-gate 1247*0Sstevel@tonic-gate static void 1248*0Sstevel@tonic-gate zone_hold_locked(zone_t *z) 1249*0Sstevel@tonic-gate { 1250*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&z->zone_lock)); 1251*0Sstevel@tonic-gate z->zone_ref++; 1252*0Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 1253*0Sstevel@tonic-gate } 1254*0Sstevel@tonic-gate 1255*0Sstevel@tonic-gate void 1256*0Sstevel@tonic-gate zone_hold(zone_t *z) 1257*0Sstevel@tonic-gate { 1258*0Sstevel@tonic-gate mutex_enter(&z->zone_lock); 1259*0Sstevel@tonic-gate zone_hold_locked(z); 1260*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1261*0Sstevel@tonic-gate } 1262*0Sstevel@tonic-gate 1263*0Sstevel@tonic-gate /* 1264*0Sstevel@tonic-gate * If the non-cred ref count drops to 1 and either the cred ref count 1265*0Sstevel@tonic-gate * is 0 or we aren't waiting for cred references, the zone is ready to 1266*0Sstevel@tonic-gate * be destroyed. 1267*0Sstevel@tonic-gate */ 1268*0Sstevel@tonic-gate #define ZONE_IS_UNREF(zone) ((zone)->zone_ref == 1 && \ 1269*0Sstevel@tonic-gate (!zone_wait_for_cred || (zone)->zone_cred_ref == 0)) 1270*0Sstevel@tonic-gate 1271*0Sstevel@tonic-gate void 1272*0Sstevel@tonic-gate zone_rele(zone_t *z) 1273*0Sstevel@tonic-gate { 1274*0Sstevel@tonic-gate boolean_t wakeup; 1275*0Sstevel@tonic-gate 1276*0Sstevel@tonic-gate mutex_enter(&z->zone_lock); 1277*0Sstevel@tonic-gate ASSERT(z->zone_ref != 0); 1278*0Sstevel@tonic-gate z->zone_ref--; 1279*0Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 1280*0Sstevel@tonic-gate /* no more refs, free the structure */ 1281*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1282*0Sstevel@tonic-gate zone_free(z); 1283*0Sstevel@tonic-gate return; 1284*0Sstevel@tonic-gate } 1285*0Sstevel@tonic-gate /* signal zone_destroy so the zone can finish halting */ 1286*0Sstevel@tonic-gate wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD); 1287*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1288*0Sstevel@tonic-gate 1289*0Sstevel@tonic-gate if (wakeup) { 1290*0Sstevel@tonic-gate /* 1291*0Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 1292*0Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 1293*0Sstevel@tonic-gate */ 1294*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1295*0Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 1296*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1297*0Sstevel@tonic-gate } 1298*0Sstevel@tonic-gate } 1299*0Sstevel@tonic-gate 1300*0Sstevel@tonic-gate void 1301*0Sstevel@tonic-gate zone_cred_hold(zone_t *z) 1302*0Sstevel@tonic-gate { 1303*0Sstevel@tonic-gate mutex_enter(&z->zone_lock); 1304*0Sstevel@tonic-gate z->zone_cred_ref++; 1305*0Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 1306*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1307*0Sstevel@tonic-gate } 1308*0Sstevel@tonic-gate 1309*0Sstevel@tonic-gate void 1310*0Sstevel@tonic-gate zone_cred_rele(zone_t *z) 1311*0Sstevel@tonic-gate { 1312*0Sstevel@tonic-gate boolean_t wakeup; 1313*0Sstevel@tonic-gate 1314*0Sstevel@tonic-gate mutex_enter(&z->zone_lock); 1315*0Sstevel@tonic-gate ASSERT(z->zone_cred_ref != 0); 1316*0Sstevel@tonic-gate z->zone_cred_ref--; 1317*0Sstevel@tonic-gate if (z->zone_ref == 0 && z->zone_cred_ref == 0) { 1318*0Sstevel@tonic-gate /* no more refs, free the structure */ 1319*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1320*0Sstevel@tonic-gate zone_free(z); 1321*0Sstevel@tonic-gate return; 1322*0Sstevel@tonic-gate } 1323*0Sstevel@tonic-gate /* 1324*0Sstevel@tonic-gate * If zone_destroy is waiting for the cred references to drain 1325*0Sstevel@tonic-gate * out, and they have, signal it. 1326*0Sstevel@tonic-gate */ 1327*0Sstevel@tonic-gate wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) && 1328*0Sstevel@tonic-gate zone_status_get(z) >= ZONE_IS_DEAD); 1329*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1330*0Sstevel@tonic-gate 1331*0Sstevel@tonic-gate if (wakeup) { 1332*0Sstevel@tonic-gate /* 1333*0Sstevel@tonic-gate * Grabbing zonehash_lock here effectively synchronizes with 1334*0Sstevel@tonic-gate * zone_destroy() to avoid missed signals. 1335*0Sstevel@tonic-gate */ 1336*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1337*0Sstevel@tonic-gate cv_broadcast(&zone_destroy_cv); 1338*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1339*0Sstevel@tonic-gate } 1340*0Sstevel@tonic-gate } 1341*0Sstevel@tonic-gate 1342*0Sstevel@tonic-gate void 1343*0Sstevel@tonic-gate zone_task_hold(zone_t *z) 1344*0Sstevel@tonic-gate { 1345*0Sstevel@tonic-gate mutex_enter(&z->zone_lock); 1346*0Sstevel@tonic-gate z->zone_ntasks++; 1347*0Sstevel@tonic-gate ASSERT(z->zone_ntasks != 0); 1348*0Sstevel@tonic-gate mutex_exit(&z->zone_lock); 1349*0Sstevel@tonic-gate } 1350*0Sstevel@tonic-gate 1351*0Sstevel@tonic-gate void 1352*0Sstevel@tonic-gate zone_task_rele(zone_t *zone) 1353*0Sstevel@tonic-gate { 1354*0Sstevel@tonic-gate uint_t refcnt; 1355*0Sstevel@tonic-gate 1356*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 1357*0Sstevel@tonic-gate ASSERT(zone->zone_ntasks != 0); 1358*0Sstevel@tonic-gate refcnt = --zone->zone_ntasks; 1359*0Sstevel@tonic-gate if (refcnt > 1) { /* Common case */ 1360*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 1361*0Sstevel@tonic-gate return; 1362*0Sstevel@tonic-gate } 1363*0Sstevel@tonic-gate zone_hold_locked(zone); /* so we can use the zone_t later */ 1364*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 1365*0Sstevel@tonic-gate if (refcnt == 1) { 1366*0Sstevel@tonic-gate /* 1367*0Sstevel@tonic-gate * See if the zone is shutting down. 1368*0Sstevel@tonic-gate */ 1369*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1370*0Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) { 1371*0Sstevel@tonic-gate goto out; 1372*0Sstevel@tonic-gate } 1373*0Sstevel@tonic-gate 1374*0Sstevel@tonic-gate /* 1375*0Sstevel@tonic-gate * Make sure the ntasks didn't change since we 1376*0Sstevel@tonic-gate * dropped zone_lock. 1377*0Sstevel@tonic-gate */ 1378*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 1379*0Sstevel@tonic-gate if (refcnt != zone->zone_ntasks) { 1380*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 1381*0Sstevel@tonic-gate goto out; 1382*0Sstevel@tonic-gate } 1383*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 1384*0Sstevel@tonic-gate 1385*0Sstevel@tonic-gate /* 1386*0Sstevel@tonic-gate * No more user processes in the zone. The zone is empty. 1387*0Sstevel@tonic-gate */ 1388*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 1389*0Sstevel@tonic-gate goto out; 1390*0Sstevel@tonic-gate } 1391*0Sstevel@tonic-gate 1392*0Sstevel@tonic-gate ASSERT(refcnt == 0); 1393*0Sstevel@tonic-gate /* 1394*0Sstevel@tonic-gate * zsched has exited; the zone is dead. 1395*0Sstevel@tonic-gate */ 1396*0Sstevel@tonic-gate zone->zone_zsched = NULL; /* paranoia */ 1397*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1398*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DEAD); 1399*0Sstevel@tonic-gate out: 1400*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1401*0Sstevel@tonic-gate zone_rele(zone); 1402*0Sstevel@tonic-gate } 1403*0Sstevel@tonic-gate 1404*0Sstevel@tonic-gate zoneid_t 1405*0Sstevel@tonic-gate getzoneid(void) 1406*0Sstevel@tonic-gate { 1407*0Sstevel@tonic-gate return (curproc->p_zone->zone_id); 1408*0Sstevel@tonic-gate } 1409*0Sstevel@tonic-gate 1410*0Sstevel@tonic-gate /* 1411*0Sstevel@tonic-gate * Internal versions of zone_find_by_*(). These don't zone_hold() or 1412*0Sstevel@tonic-gate * check the validity of a zone's state. 1413*0Sstevel@tonic-gate */ 1414*0Sstevel@tonic-gate static zone_t * 1415*0Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid) 1416*0Sstevel@tonic-gate { 1417*0Sstevel@tonic-gate mod_hash_val_t hv; 1418*0Sstevel@tonic-gate zone_t *zone = NULL; 1419*0Sstevel@tonic-gate 1420*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 1421*0Sstevel@tonic-gate 1422*0Sstevel@tonic-gate if (mod_hash_find(zonehashbyid, 1423*0Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0) 1424*0Sstevel@tonic-gate zone = (zone_t *)hv; 1425*0Sstevel@tonic-gate return (zone); 1426*0Sstevel@tonic-gate } 1427*0Sstevel@tonic-gate 1428*0Sstevel@tonic-gate static zone_t * 1429*0Sstevel@tonic-gate zone_find_all_by_name(char *name) 1430*0Sstevel@tonic-gate { 1431*0Sstevel@tonic-gate mod_hash_val_t hv; 1432*0Sstevel@tonic-gate zone_t *zone = NULL; 1433*0Sstevel@tonic-gate 1434*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 1435*0Sstevel@tonic-gate 1436*0Sstevel@tonic-gate if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0) 1437*0Sstevel@tonic-gate zone = (zone_t *)hv; 1438*0Sstevel@tonic-gate return (zone); 1439*0Sstevel@tonic-gate } 1440*0Sstevel@tonic-gate 1441*0Sstevel@tonic-gate /* 1442*0Sstevel@tonic-gate * Public interface for looking up a zone by zoneid. Only returns the zone if 1443*0Sstevel@tonic-gate * it is fully initialized, and has not yet begun the zone_destroy() sequence. 1444*0Sstevel@tonic-gate * Caller must call zone_rele() once it is done with the zone. 1445*0Sstevel@tonic-gate * 1446*0Sstevel@tonic-gate * The zone may begin the zone_destroy() sequence immediately after this 1447*0Sstevel@tonic-gate * function returns, but may be safely used until zone_rele() is called. 1448*0Sstevel@tonic-gate */ 1449*0Sstevel@tonic-gate zone_t * 1450*0Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid) 1451*0Sstevel@tonic-gate { 1452*0Sstevel@tonic-gate zone_t *zone; 1453*0Sstevel@tonic-gate zone_status_t status; 1454*0Sstevel@tonic-gate 1455*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1456*0Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 1457*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1458*0Sstevel@tonic-gate return (NULL); 1459*0Sstevel@tonic-gate } 1460*0Sstevel@tonic-gate status = zone_status_get(zone); 1461*0Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 1462*0Sstevel@tonic-gate /* 1463*0Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 1464*0Sstevel@tonic-gate */ 1465*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1466*0Sstevel@tonic-gate return (NULL); 1467*0Sstevel@tonic-gate } 1468*0Sstevel@tonic-gate zone_hold(zone); 1469*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1470*0Sstevel@tonic-gate return (zone); 1471*0Sstevel@tonic-gate } 1472*0Sstevel@tonic-gate 1473*0Sstevel@tonic-gate /* 1474*0Sstevel@tonic-gate * Similar to zone_find_by_id, but using zone name as the key. 1475*0Sstevel@tonic-gate */ 1476*0Sstevel@tonic-gate zone_t * 1477*0Sstevel@tonic-gate zone_find_by_name(char *name) 1478*0Sstevel@tonic-gate { 1479*0Sstevel@tonic-gate zone_t *zone; 1480*0Sstevel@tonic-gate zone_status_t status; 1481*0Sstevel@tonic-gate 1482*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1483*0Sstevel@tonic-gate if ((zone = zone_find_all_by_name(name)) == NULL) { 1484*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1485*0Sstevel@tonic-gate return (NULL); 1486*0Sstevel@tonic-gate } 1487*0Sstevel@tonic-gate status = zone_status_get(zone); 1488*0Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 1489*0Sstevel@tonic-gate /* 1490*0Sstevel@tonic-gate * For all practical purposes the zone doesn't exist. 1491*0Sstevel@tonic-gate */ 1492*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1493*0Sstevel@tonic-gate return (NULL); 1494*0Sstevel@tonic-gate } 1495*0Sstevel@tonic-gate zone_hold(zone); 1496*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1497*0Sstevel@tonic-gate return (zone); 1498*0Sstevel@tonic-gate } 1499*0Sstevel@tonic-gate 1500*0Sstevel@tonic-gate /* 1501*0Sstevel@tonic-gate * Similar to zone_find_by_id(), using the path as a key. For instance, 1502*0Sstevel@tonic-gate * if there is a zone "foo" rooted at /foo/root, and the path argument 1503*0Sstevel@tonic-gate * is "/foo/root/proc", it will return the held zone_t corresponding to 1504*0Sstevel@tonic-gate * zone "foo". 1505*0Sstevel@tonic-gate * 1506*0Sstevel@tonic-gate * zone_find_by_path() always returns a non-NULL value, since at the 1507*0Sstevel@tonic-gate * very least every path will be contained in the global zone. 1508*0Sstevel@tonic-gate * 1509*0Sstevel@tonic-gate * As with the other zone_find_by_*() functions, the caller is 1510*0Sstevel@tonic-gate * responsible for zone_rele()ing the return value of this function. 1511*0Sstevel@tonic-gate */ 1512*0Sstevel@tonic-gate zone_t * 1513*0Sstevel@tonic-gate zone_find_by_path(const char *path) 1514*0Sstevel@tonic-gate { 1515*0Sstevel@tonic-gate zone_t *zone; 1516*0Sstevel@tonic-gate zone_t *zret = NULL; 1517*0Sstevel@tonic-gate zone_status_t status; 1518*0Sstevel@tonic-gate 1519*0Sstevel@tonic-gate if (path == NULL) { 1520*0Sstevel@tonic-gate /* 1521*0Sstevel@tonic-gate * Call from rootconf(). 1522*0Sstevel@tonic-gate */ 1523*0Sstevel@tonic-gate zone_hold(global_zone); 1524*0Sstevel@tonic-gate return (global_zone); 1525*0Sstevel@tonic-gate } 1526*0Sstevel@tonic-gate ASSERT(*path == '/'); 1527*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1528*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 1529*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 1530*0Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(path, zone)) 1531*0Sstevel@tonic-gate zret = zone; 1532*0Sstevel@tonic-gate } 1533*0Sstevel@tonic-gate ASSERT(zret != NULL); 1534*0Sstevel@tonic-gate status = zone_status_get(zret); 1535*0Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) { 1536*0Sstevel@tonic-gate /* 1537*0Sstevel@tonic-gate * Zone practically doesn't exist. 1538*0Sstevel@tonic-gate */ 1539*0Sstevel@tonic-gate zret = global_zone; 1540*0Sstevel@tonic-gate } 1541*0Sstevel@tonic-gate zone_hold(zret); 1542*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1543*0Sstevel@tonic-gate return (zret); 1544*0Sstevel@tonic-gate } 1545*0Sstevel@tonic-gate 1546*0Sstevel@tonic-gate /* 1547*0Sstevel@tonic-gate * Get the number of cpus visible to this zone. The system-wide global 1548*0Sstevel@tonic-gate * 'ncpus' is returned if pools are disabled, the caller is in the 1549*0Sstevel@tonic-gate * global zone, or a NULL zone argument is passed in. 1550*0Sstevel@tonic-gate */ 1551*0Sstevel@tonic-gate int 1552*0Sstevel@tonic-gate zone_ncpus_get(zone_t *zone) 1553*0Sstevel@tonic-gate { 1554*0Sstevel@tonic-gate int myncpus = zone == NULL ? 0 : zone->zone_ncpus; 1555*0Sstevel@tonic-gate 1556*0Sstevel@tonic-gate return (myncpus != 0 ? myncpus : ncpus); 1557*0Sstevel@tonic-gate } 1558*0Sstevel@tonic-gate 1559*0Sstevel@tonic-gate /* 1560*0Sstevel@tonic-gate * Get the number of online cpus visible to this zone. The system-wide 1561*0Sstevel@tonic-gate * global 'ncpus_online' is returned if pools are disabled, the caller 1562*0Sstevel@tonic-gate * is in the global zone, or a NULL zone argument is passed in. 1563*0Sstevel@tonic-gate */ 1564*0Sstevel@tonic-gate int 1565*0Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone) 1566*0Sstevel@tonic-gate { 1567*0Sstevel@tonic-gate int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online; 1568*0Sstevel@tonic-gate 1569*0Sstevel@tonic-gate return (myncpus_online != 0 ? myncpus_online : ncpus_online); 1570*0Sstevel@tonic-gate } 1571*0Sstevel@tonic-gate 1572*0Sstevel@tonic-gate /* 1573*0Sstevel@tonic-gate * Return the pool to which the zone is currently bound. 1574*0Sstevel@tonic-gate */ 1575*0Sstevel@tonic-gate pool_t * 1576*0Sstevel@tonic-gate zone_pool_get(zone_t *zone) 1577*0Sstevel@tonic-gate { 1578*0Sstevel@tonic-gate ASSERT(pool_lock_held()); 1579*0Sstevel@tonic-gate 1580*0Sstevel@tonic-gate return (zone->zone_pool); 1581*0Sstevel@tonic-gate } 1582*0Sstevel@tonic-gate 1583*0Sstevel@tonic-gate /* 1584*0Sstevel@tonic-gate * Set the zone's pool pointer and update the zone's visibility to match 1585*0Sstevel@tonic-gate * the resources in the new pool. 1586*0Sstevel@tonic-gate */ 1587*0Sstevel@tonic-gate void 1588*0Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool) 1589*0Sstevel@tonic-gate { 1590*0Sstevel@tonic-gate ASSERT(pool_lock_held()); 1591*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 1592*0Sstevel@tonic-gate 1593*0Sstevel@tonic-gate zone->zone_pool = pool; 1594*0Sstevel@tonic-gate zone_pset_set(zone, pool->pool_pset->pset_id); 1595*0Sstevel@tonic-gate } 1596*0Sstevel@tonic-gate 1597*0Sstevel@tonic-gate /* 1598*0Sstevel@tonic-gate * Return the cached value of the id of the processor set to which the 1599*0Sstevel@tonic-gate * zone is currently bound. The value will be ZONE_PS_INVAL if the pools 1600*0Sstevel@tonic-gate * facility is disabled. 1601*0Sstevel@tonic-gate */ 1602*0Sstevel@tonic-gate psetid_t 1603*0Sstevel@tonic-gate zone_pset_get(zone_t *zone) 1604*0Sstevel@tonic-gate { 1605*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 1606*0Sstevel@tonic-gate 1607*0Sstevel@tonic-gate return (zone->zone_psetid); 1608*0Sstevel@tonic-gate } 1609*0Sstevel@tonic-gate 1610*0Sstevel@tonic-gate /* 1611*0Sstevel@tonic-gate * Set the cached value of the id of the processor set to which the zone 1612*0Sstevel@tonic-gate * is currently bound. Also update the zone's visibility to match the 1613*0Sstevel@tonic-gate * resources in the new processor set. 1614*0Sstevel@tonic-gate */ 1615*0Sstevel@tonic-gate void 1616*0Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid) 1617*0Sstevel@tonic-gate { 1618*0Sstevel@tonic-gate psetid_t oldpsetid; 1619*0Sstevel@tonic-gate 1620*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 1621*0Sstevel@tonic-gate oldpsetid = zone_pset_get(zone); 1622*0Sstevel@tonic-gate 1623*0Sstevel@tonic-gate if (oldpsetid == newpsetid) 1624*0Sstevel@tonic-gate return; 1625*0Sstevel@tonic-gate /* 1626*0Sstevel@tonic-gate * Global zone sees all. 1627*0Sstevel@tonic-gate */ 1628*0Sstevel@tonic-gate if (zone != global_zone) { 1629*0Sstevel@tonic-gate zone->zone_psetid = newpsetid; 1630*0Sstevel@tonic-gate if (newpsetid != ZONE_PS_INVAL) 1631*0Sstevel@tonic-gate pool_pset_visibility_add(newpsetid, zone); 1632*0Sstevel@tonic-gate if (oldpsetid != ZONE_PS_INVAL) 1633*0Sstevel@tonic-gate pool_pset_visibility_remove(oldpsetid, zone); 1634*0Sstevel@tonic-gate } 1635*0Sstevel@tonic-gate /* 1636*0Sstevel@tonic-gate * Disabling pools, so we should start using the global values 1637*0Sstevel@tonic-gate * for ncpus and ncpus_online. 1638*0Sstevel@tonic-gate */ 1639*0Sstevel@tonic-gate if (newpsetid == ZONE_PS_INVAL) { 1640*0Sstevel@tonic-gate zone->zone_ncpus = 0; 1641*0Sstevel@tonic-gate zone->zone_ncpus_online = 0; 1642*0Sstevel@tonic-gate } 1643*0Sstevel@tonic-gate } 1644*0Sstevel@tonic-gate 1645*0Sstevel@tonic-gate /* 1646*0Sstevel@tonic-gate * Walk the list of active zones and issue the provided callback for 1647*0Sstevel@tonic-gate * each of them. 1648*0Sstevel@tonic-gate * 1649*0Sstevel@tonic-gate * Caller must not be holding any locks that may be acquired under 1650*0Sstevel@tonic-gate * zonehash_lock. See comment at the beginning of the file for a list of 1651*0Sstevel@tonic-gate * common locks and their interactions with zones. 1652*0Sstevel@tonic-gate */ 1653*0Sstevel@tonic-gate int 1654*0Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data) 1655*0Sstevel@tonic-gate { 1656*0Sstevel@tonic-gate zone_t *zone; 1657*0Sstevel@tonic-gate int ret = 0; 1658*0Sstevel@tonic-gate zone_status_t status; 1659*0Sstevel@tonic-gate 1660*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 1661*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 1662*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 1663*0Sstevel@tonic-gate /* 1664*0Sstevel@tonic-gate * Skip zones that shouldn't be externally visible. 1665*0Sstevel@tonic-gate */ 1666*0Sstevel@tonic-gate status = zone_status_get(zone); 1667*0Sstevel@tonic-gate if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) 1668*0Sstevel@tonic-gate continue; 1669*0Sstevel@tonic-gate /* 1670*0Sstevel@tonic-gate * Bail immediately if any callback invocation returns a 1671*0Sstevel@tonic-gate * non-zero value. 1672*0Sstevel@tonic-gate */ 1673*0Sstevel@tonic-gate ret = (*cb)(zone, data); 1674*0Sstevel@tonic-gate if (ret != 0) 1675*0Sstevel@tonic-gate break; 1676*0Sstevel@tonic-gate } 1677*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 1678*0Sstevel@tonic-gate return (ret); 1679*0Sstevel@tonic-gate } 1680*0Sstevel@tonic-gate 1681*0Sstevel@tonic-gate static int 1682*0Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath) 1683*0Sstevel@tonic-gate { 1684*0Sstevel@tonic-gate vnode_t *vp; 1685*0Sstevel@tonic-gate int trycount; 1686*0Sstevel@tonic-gate int error = 0; 1687*0Sstevel@tonic-gate char *path; 1688*0Sstevel@tonic-gate struct pathname upn, pn; 1689*0Sstevel@tonic-gate size_t pathlen; 1690*0Sstevel@tonic-gate 1691*0Sstevel@tonic-gate if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0) 1692*0Sstevel@tonic-gate return (error); 1693*0Sstevel@tonic-gate 1694*0Sstevel@tonic-gate pn_alloc(&pn); 1695*0Sstevel@tonic-gate 1696*0Sstevel@tonic-gate /* prevent infinite loop */ 1697*0Sstevel@tonic-gate trycount = 10; 1698*0Sstevel@tonic-gate for (;;) { 1699*0Sstevel@tonic-gate if (--trycount <= 0) { 1700*0Sstevel@tonic-gate error = ESTALE; 1701*0Sstevel@tonic-gate goto out; 1702*0Sstevel@tonic-gate } 1703*0Sstevel@tonic-gate 1704*0Sstevel@tonic-gate if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) { 1705*0Sstevel@tonic-gate /* 1706*0Sstevel@tonic-gate * VOP_ACCESS() may cover 'vp' with a new 1707*0Sstevel@tonic-gate * filesystem, if 'vp' is an autoFS vnode. 1708*0Sstevel@tonic-gate * Get the new 'vp' if so. 1709*0Sstevel@tonic-gate */ 1710*0Sstevel@tonic-gate if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 && 1711*0Sstevel@tonic-gate (vp->v_vfsmountedhere == NULL || 1712*0Sstevel@tonic-gate (error = traverse(&vp)) == 0)) { 1713*0Sstevel@tonic-gate pathlen = pn.pn_pathlen + 2; 1714*0Sstevel@tonic-gate path = kmem_alloc(pathlen, KM_SLEEP); 1715*0Sstevel@tonic-gate (void) strncpy(path, pn.pn_path, 1716*0Sstevel@tonic-gate pn.pn_pathlen + 1); 1717*0Sstevel@tonic-gate path[pathlen - 2] = '/'; 1718*0Sstevel@tonic-gate path[pathlen - 1] = '\0'; 1719*0Sstevel@tonic-gate pn_free(&pn); 1720*0Sstevel@tonic-gate pn_free(&upn); 1721*0Sstevel@tonic-gate 1722*0Sstevel@tonic-gate /* Success! */ 1723*0Sstevel@tonic-gate break; 1724*0Sstevel@tonic-gate } 1725*0Sstevel@tonic-gate VN_RELE(vp); 1726*0Sstevel@tonic-gate } 1727*0Sstevel@tonic-gate if (error != ESTALE) 1728*0Sstevel@tonic-gate goto out; 1729*0Sstevel@tonic-gate } 1730*0Sstevel@tonic-gate 1731*0Sstevel@tonic-gate ASSERT(error == 0); 1732*0Sstevel@tonic-gate zone->zone_rootvp = vp; /* we hold a reference to vp */ 1733*0Sstevel@tonic-gate zone->zone_rootpath = path; 1734*0Sstevel@tonic-gate zone->zone_rootpathlen = pathlen; 1735*0Sstevel@tonic-gate return (0); 1736*0Sstevel@tonic-gate 1737*0Sstevel@tonic-gate out: 1738*0Sstevel@tonic-gate pn_free(&pn); 1739*0Sstevel@tonic-gate pn_free(&upn); 1740*0Sstevel@tonic-gate return (error); 1741*0Sstevel@tonic-gate } 1742*0Sstevel@tonic-gate 1743*0Sstevel@tonic-gate #define isalnum(c) (((c) >= '0' && (c) <= '9') || \ 1744*0Sstevel@tonic-gate ((c) >= 'a' && (c) <= 'z') || \ 1745*0Sstevel@tonic-gate ((c) >= 'A' && (c) <= 'Z')) 1746*0Sstevel@tonic-gate 1747*0Sstevel@tonic-gate static int 1748*0Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname) 1749*0Sstevel@tonic-gate { 1750*0Sstevel@tonic-gate char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 1751*0Sstevel@tonic-gate size_t len; 1752*0Sstevel@tonic-gate int i, err; 1753*0Sstevel@tonic-gate 1754*0Sstevel@tonic-gate if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) { 1755*0Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 1756*0Sstevel@tonic-gate return (err); /* EFAULT or ENAMETOOLONG */ 1757*0Sstevel@tonic-gate } 1758*0Sstevel@tonic-gate 1759*0Sstevel@tonic-gate /* must be less than ZONENAME_MAX */ 1760*0Sstevel@tonic-gate if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') { 1761*0Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 1762*0Sstevel@tonic-gate return (EINVAL); 1763*0Sstevel@tonic-gate } 1764*0Sstevel@tonic-gate 1765*0Sstevel@tonic-gate /* 1766*0Sstevel@tonic-gate * Name must start with an alphanumeric and must contain only 1767*0Sstevel@tonic-gate * alphanumerics, '-', '_' and '.'. 1768*0Sstevel@tonic-gate */ 1769*0Sstevel@tonic-gate if (!isalnum(kname[0])) { 1770*0Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 1771*0Sstevel@tonic-gate return (EINVAL); 1772*0Sstevel@tonic-gate } 1773*0Sstevel@tonic-gate for (i = 1; i < len - 1; i++) { 1774*0Sstevel@tonic-gate if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' && 1775*0Sstevel@tonic-gate kname[i] != '.') { 1776*0Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 1777*0Sstevel@tonic-gate return (EINVAL); 1778*0Sstevel@tonic-gate } 1779*0Sstevel@tonic-gate } 1780*0Sstevel@tonic-gate 1781*0Sstevel@tonic-gate zone->zone_name = kname; 1782*0Sstevel@tonic-gate return (0); 1783*0Sstevel@tonic-gate } 1784*0Sstevel@tonic-gate 1785*0Sstevel@tonic-gate /* 1786*0Sstevel@tonic-gate * Similar to thread_create(), but makes sure the thread is in the appropriate 1787*0Sstevel@tonic-gate * zone's zsched process (curproc->p_zone->zone_zsched) before returning. 1788*0Sstevel@tonic-gate */ 1789*0Sstevel@tonic-gate /*ARGSUSED*/ 1790*0Sstevel@tonic-gate kthread_t * 1791*0Sstevel@tonic-gate zthread_create( 1792*0Sstevel@tonic-gate caddr_t stk, 1793*0Sstevel@tonic-gate size_t stksize, 1794*0Sstevel@tonic-gate void (*proc)(), 1795*0Sstevel@tonic-gate void *arg, 1796*0Sstevel@tonic-gate size_t len, 1797*0Sstevel@tonic-gate pri_t pri) 1798*0Sstevel@tonic-gate { 1799*0Sstevel@tonic-gate kthread_t *t; 1800*0Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 1801*0Sstevel@tonic-gate proc_t *pp = zone->zone_zsched; 1802*0Sstevel@tonic-gate 1803*0Sstevel@tonic-gate zone_hold(zone); /* Reference to be dropped when thread exits */ 1804*0Sstevel@tonic-gate 1805*0Sstevel@tonic-gate /* 1806*0Sstevel@tonic-gate * No-one should be trying to create threads if the zone is shutting 1807*0Sstevel@tonic-gate * down and there aren't any kernel threads around. See comment 1808*0Sstevel@tonic-gate * in zthread_exit(). 1809*0Sstevel@tonic-gate */ 1810*0Sstevel@tonic-gate ASSERT(!(zone->zone_kthreads == NULL && 1811*0Sstevel@tonic-gate zone_status_get(zone) >= ZONE_IS_EMPTY)); 1812*0Sstevel@tonic-gate /* 1813*0Sstevel@tonic-gate * Create a thread, but don't let it run until we've finished setting 1814*0Sstevel@tonic-gate * things up. 1815*0Sstevel@tonic-gate */ 1816*0Sstevel@tonic-gate t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri); 1817*0Sstevel@tonic-gate ASSERT(t->t_forw == NULL); 1818*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1819*0Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 1820*0Sstevel@tonic-gate t->t_forw = t->t_back = t; 1821*0Sstevel@tonic-gate } else { 1822*0Sstevel@tonic-gate kthread_t *tx = zone->zone_kthreads; 1823*0Sstevel@tonic-gate 1824*0Sstevel@tonic-gate t->t_forw = tx; 1825*0Sstevel@tonic-gate t->t_back = tx->t_back; 1826*0Sstevel@tonic-gate tx->t_back->t_forw = t; 1827*0Sstevel@tonic-gate tx->t_back = t; 1828*0Sstevel@tonic-gate } 1829*0Sstevel@tonic-gate zone->zone_kthreads = t; 1830*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1831*0Sstevel@tonic-gate 1832*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 1833*0Sstevel@tonic-gate t->t_proc_flag |= TP_ZTHREAD; 1834*0Sstevel@tonic-gate project_rele(t->t_proj); 1835*0Sstevel@tonic-gate t->t_proj = project_hold(pp->p_task->tk_proj); 1836*0Sstevel@tonic-gate 1837*0Sstevel@tonic-gate /* 1838*0Sstevel@tonic-gate * Setup complete, let it run. 1839*0Sstevel@tonic-gate */ 1840*0Sstevel@tonic-gate thread_lock(t); 1841*0Sstevel@tonic-gate t->t_schedflag |= TS_ALLSTART; 1842*0Sstevel@tonic-gate setrun_locked(t); 1843*0Sstevel@tonic-gate thread_unlock(t); 1844*0Sstevel@tonic-gate 1845*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 1846*0Sstevel@tonic-gate 1847*0Sstevel@tonic-gate return (t); 1848*0Sstevel@tonic-gate } 1849*0Sstevel@tonic-gate 1850*0Sstevel@tonic-gate /* 1851*0Sstevel@tonic-gate * Similar to thread_exit(). Must be called by threads created via 1852*0Sstevel@tonic-gate * zthread_exit(). 1853*0Sstevel@tonic-gate */ 1854*0Sstevel@tonic-gate void 1855*0Sstevel@tonic-gate zthread_exit(void) 1856*0Sstevel@tonic-gate { 1857*0Sstevel@tonic-gate kthread_t *t = curthread; 1858*0Sstevel@tonic-gate proc_t *pp = curproc; 1859*0Sstevel@tonic-gate zone_t *zone = pp->p_zone; 1860*0Sstevel@tonic-gate 1861*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 1862*0Sstevel@tonic-gate 1863*0Sstevel@tonic-gate /* 1864*0Sstevel@tonic-gate * Reparent to p0 1865*0Sstevel@tonic-gate */ 1866*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 1867*0Sstevel@tonic-gate t->t_proc_flag &= ~TP_ZTHREAD; 1868*0Sstevel@tonic-gate t->t_procp = &p0; 1869*0Sstevel@tonic-gate hat_thread_exit(t); 1870*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 1871*0Sstevel@tonic-gate 1872*0Sstevel@tonic-gate if (t->t_back == t) { 1873*0Sstevel@tonic-gate ASSERT(t->t_forw == t); 1874*0Sstevel@tonic-gate /* 1875*0Sstevel@tonic-gate * If the zone is empty, once the thread count 1876*0Sstevel@tonic-gate * goes to zero no further kernel threads can be 1877*0Sstevel@tonic-gate * created. This is because if the creator is a process 1878*0Sstevel@tonic-gate * in the zone, then it must have exited before the zone 1879*0Sstevel@tonic-gate * state could be set to ZONE_IS_EMPTY. 1880*0Sstevel@tonic-gate * Otherwise, if the creator is a kernel thread in the 1881*0Sstevel@tonic-gate * zone, the thread count is non-zero. 1882*0Sstevel@tonic-gate * 1883*0Sstevel@tonic-gate * This really means that non-zone kernel threads should 1884*0Sstevel@tonic-gate * not create zone kernel threads. 1885*0Sstevel@tonic-gate */ 1886*0Sstevel@tonic-gate zone->zone_kthreads = NULL; 1887*0Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_EMPTY) { 1888*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 1889*0Sstevel@tonic-gate } 1890*0Sstevel@tonic-gate } else { 1891*0Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 1892*0Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 1893*0Sstevel@tonic-gate if (zone->zone_kthreads == t) 1894*0Sstevel@tonic-gate zone->zone_kthreads = t->t_forw; 1895*0Sstevel@tonic-gate } 1896*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 1897*0Sstevel@tonic-gate zone_rele(zone); 1898*0Sstevel@tonic-gate thread_exit(); 1899*0Sstevel@tonic-gate /* NOTREACHED */ 1900*0Sstevel@tonic-gate } 1901*0Sstevel@tonic-gate 1902*0Sstevel@tonic-gate static void 1903*0Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp) 1904*0Sstevel@tonic-gate { 1905*0Sstevel@tonic-gate vnode_t *oldvp; 1906*0Sstevel@tonic-gate 1907*0Sstevel@tonic-gate /* we're going to hold a reference here to the directory */ 1908*0Sstevel@tonic-gate VN_HOLD(vp); 1909*0Sstevel@tonic-gate 1910*0Sstevel@tonic-gate #ifdef C2_AUDIT 1911*0Sstevel@tonic-gate if (audit_active) /* update abs cwd/root path see c2audit.c */ 1912*0Sstevel@tonic-gate audit_chdirec(vp, vpp); 1913*0Sstevel@tonic-gate #endif 1914*0Sstevel@tonic-gate 1915*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 1916*0Sstevel@tonic-gate oldvp = *vpp; 1917*0Sstevel@tonic-gate *vpp = vp; 1918*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 1919*0Sstevel@tonic-gate if (oldvp != NULL) 1920*0Sstevel@tonic-gate VN_RELE(oldvp); 1921*0Sstevel@tonic-gate } 1922*0Sstevel@tonic-gate 1923*0Sstevel@tonic-gate /* 1924*0Sstevel@tonic-gate * Convert an rctl value represented by an nvlist_t into an rctl_val_t. 1925*0Sstevel@tonic-gate */ 1926*0Sstevel@tonic-gate static int 1927*0Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv) 1928*0Sstevel@tonic-gate { 1929*0Sstevel@tonic-gate nvpair_t *nvp = NULL; 1930*0Sstevel@tonic-gate boolean_t priv_set = B_FALSE; 1931*0Sstevel@tonic-gate boolean_t limit_set = B_FALSE; 1932*0Sstevel@tonic-gate boolean_t action_set = B_FALSE; 1933*0Sstevel@tonic-gate 1934*0Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 1935*0Sstevel@tonic-gate const char *name; 1936*0Sstevel@tonic-gate uint64_t ui64; 1937*0Sstevel@tonic-gate 1938*0Sstevel@tonic-gate name = nvpair_name(nvp); 1939*0Sstevel@tonic-gate if (nvpair_type(nvp) != DATA_TYPE_UINT64) 1940*0Sstevel@tonic-gate return (EINVAL); 1941*0Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &ui64); 1942*0Sstevel@tonic-gate if (strcmp(name, "privilege") == 0) { 1943*0Sstevel@tonic-gate /* 1944*0Sstevel@tonic-gate * Currently only privileged values are allowed, but 1945*0Sstevel@tonic-gate * this may change in the future. 1946*0Sstevel@tonic-gate */ 1947*0Sstevel@tonic-gate if (ui64 != RCPRIV_PRIVILEGED) 1948*0Sstevel@tonic-gate return (EINVAL); 1949*0Sstevel@tonic-gate rv->rcv_privilege = ui64; 1950*0Sstevel@tonic-gate priv_set = B_TRUE; 1951*0Sstevel@tonic-gate } else if (strcmp(name, "limit") == 0) { 1952*0Sstevel@tonic-gate rv->rcv_value = ui64; 1953*0Sstevel@tonic-gate limit_set = B_TRUE; 1954*0Sstevel@tonic-gate } else if (strcmp(name, "action") == 0) { 1955*0Sstevel@tonic-gate if (ui64 != RCTL_LOCAL_NOACTION && 1956*0Sstevel@tonic-gate ui64 != RCTL_LOCAL_DENY) 1957*0Sstevel@tonic-gate return (EINVAL); 1958*0Sstevel@tonic-gate rv->rcv_flagaction = ui64; 1959*0Sstevel@tonic-gate action_set = B_TRUE; 1960*0Sstevel@tonic-gate } else { 1961*0Sstevel@tonic-gate return (EINVAL); 1962*0Sstevel@tonic-gate } 1963*0Sstevel@tonic-gate } 1964*0Sstevel@tonic-gate 1965*0Sstevel@tonic-gate if (!(priv_set && limit_set && action_set)) 1966*0Sstevel@tonic-gate return (EINVAL); 1967*0Sstevel@tonic-gate rv->rcv_action_signal = 0; 1968*0Sstevel@tonic-gate rv->rcv_action_recipient = NULL; 1969*0Sstevel@tonic-gate rv->rcv_action_recip_pid = -1; 1970*0Sstevel@tonic-gate rv->rcv_firing_time = 0; 1971*0Sstevel@tonic-gate 1972*0Sstevel@tonic-gate return (0); 1973*0Sstevel@tonic-gate } 1974*0Sstevel@tonic-gate 1975*0Sstevel@tonic-gate void 1976*0Sstevel@tonic-gate zone_icode(void) 1977*0Sstevel@tonic-gate { 1978*0Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 1979*0Sstevel@tonic-gate struct core_globals *cg; 1980*0Sstevel@tonic-gate 1981*0Sstevel@tonic-gate /* 1982*0Sstevel@tonic-gate * For all purposes (ZONE_ATTR_INITPID and restart_init), 1983*0Sstevel@tonic-gate * storing just the pid of init is sufficient. 1984*0Sstevel@tonic-gate */ 1985*0Sstevel@tonic-gate p->p_zone->zone_proc_initpid = p->p_pid; 1986*0Sstevel@tonic-gate 1987*0Sstevel@tonic-gate /* 1988*0Sstevel@tonic-gate * Allocate user address space and stack segment 1989*0Sstevel@tonic-gate */ 1990*0Sstevel@tonic-gate 1991*0Sstevel@tonic-gate p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0; 1992*0Sstevel@tonic-gate p->p_usrstack = (caddr_t)USRSTACK32; 1993*0Sstevel@tonic-gate p->p_model = DATAMODEL_ILP32; 1994*0Sstevel@tonic-gate p->p_stkprot = PROT_ZFOD & ~PROT_EXEC; 1995*0Sstevel@tonic-gate p->p_datprot = PROT_ZFOD & ~PROT_EXEC; 1996*0Sstevel@tonic-gate p->p_stk_ctl = INT32_MAX; 1997*0Sstevel@tonic-gate 1998*0Sstevel@tonic-gate p->p_as = as_alloc(); 1999*0Sstevel@tonic-gate p->p_as->a_userlimit = (caddr_t)USERLIMIT32; 2000*0Sstevel@tonic-gate (void) hat_setup(p->p_as->a_hat, HAT_INIT); 2001*0Sstevel@tonic-gate 2002*0Sstevel@tonic-gate cg = zone_getspecific(core_zone_key, p->p_zone); 2003*0Sstevel@tonic-gate ASSERT(cg != NULL); 2004*0Sstevel@tonic-gate corectl_path_hold(cg->core_default_path); 2005*0Sstevel@tonic-gate corectl_content_hold(cg->core_default_content); 2006*0Sstevel@tonic-gate p->p_corefile = cg->core_default_path; 2007*0Sstevel@tonic-gate p->p_content = cg->core_default_content; 2008*0Sstevel@tonic-gate 2009*0Sstevel@tonic-gate init_mstate(curthread, LMS_SYSTEM); 2010*0Sstevel@tonic-gate 2011*0Sstevel@tonic-gate p->p_zone->zone_boot_err = exec_init(zone_initname, 0, 2012*0Sstevel@tonic-gate p->p_zone->zone_bootargs); 2013*0Sstevel@tonic-gate 2014*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 2015*0Sstevel@tonic-gate if (p->p_zone->zone_boot_err != 0) { 2016*0Sstevel@tonic-gate /* 2017*0Sstevel@tonic-gate * Make sure we are still in the booting state-- we could have 2018*0Sstevel@tonic-gate * raced and already be shutting down, or even further along. 2019*0Sstevel@tonic-gate */ 2020*0Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 2021*0Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN); 2022*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2023*0Sstevel@tonic-gate /* It's gone bad, dispose of the process */ 2024*0Sstevel@tonic-gate if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) { 2025*0Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 2026*0Sstevel@tonic-gate ASSERT(curproc->p_flag & SEXITLWPS); 2027*0Sstevel@tonic-gate lwp_exit(); 2028*0Sstevel@tonic-gate } 2029*0Sstevel@tonic-gate } else { 2030*0Sstevel@tonic-gate if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING) 2031*0Sstevel@tonic-gate zone_status_set(p->p_zone, ZONE_IS_RUNNING); 2032*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2033*0Sstevel@tonic-gate /* cause the process to return to userland. */ 2034*0Sstevel@tonic-gate lwp_rtt(); 2035*0Sstevel@tonic-gate } 2036*0Sstevel@tonic-gate } 2037*0Sstevel@tonic-gate 2038*0Sstevel@tonic-gate struct zsched_arg { 2039*0Sstevel@tonic-gate zone_t *zone; 2040*0Sstevel@tonic-gate nvlist_t *nvlist; 2041*0Sstevel@tonic-gate }; 2042*0Sstevel@tonic-gate 2043*0Sstevel@tonic-gate /* 2044*0Sstevel@tonic-gate * Per-zone "sched" workalike. The similarity to "sched" doesn't have 2045*0Sstevel@tonic-gate * anything to do with scheduling, but rather with the fact that 2046*0Sstevel@tonic-gate * per-zone kernel threads are parented to zsched, just like regular 2047*0Sstevel@tonic-gate * kernel threads are parented to sched (p0). 2048*0Sstevel@tonic-gate * 2049*0Sstevel@tonic-gate * zsched is also responsible for launching init for the zone. 2050*0Sstevel@tonic-gate */ 2051*0Sstevel@tonic-gate static void 2052*0Sstevel@tonic-gate zsched(void *arg) 2053*0Sstevel@tonic-gate { 2054*0Sstevel@tonic-gate struct zsched_arg *za = arg; 2055*0Sstevel@tonic-gate proc_t *pp = curproc; 2056*0Sstevel@tonic-gate proc_t *initp = proc_init; 2057*0Sstevel@tonic-gate zone_t *zone = za->zone; 2058*0Sstevel@tonic-gate cred_t *cr, *oldcred; 2059*0Sstevel@tonic-gate rctl_set_t *set; 2060*0Sstevel@tonic-gate rctl_alloc_gp_t *gp; 2061*0Sstevel@tonic-gate contract_t *ct = NULL; 2062*0Sstevel@tonic-gate task_t *tk, *oldtk; 2063*0Sstevel@tonic-gate rctl_entity_p_t e; 2064*0Sstevel@tonic-gate kproject_t *pj; 2065*0Sstevel@tonic-gate 2066*0Sstevel@tonic-gate nvlist_t *nvl = za->nvlist; 2067*0Sstevel@tonic-gate nvpair_t *nvp = NULL; 2068*0Sstevel@tonic-gate 2069*0Sstevel@tonic-gate bcopy("zsched", u.u_psargs, sizeof ("zsched")); 2070*0Sstevel@tonic-gate bcopy("zsched", u.u_comm, sizeof ("zsched")); 2071*0Sstevel@tonic-gate u.u_argc = 0; 2072*0Sstevel@tonic-gate u.u_argv = NULL; 2073*0Sstevel@tonic-gate u.u_envp = NULL; 2074*0Sstevel@tonic-gate closeall(P_FINFO(pp)); 2075*0Sstevel@tonic-gate 2076*0Sstevel@tonic-gate /* 2077*0Sstevel@tonic-gate * We are this zone's "zsched" process. As the zone isn't generally 2078*0Sstevel@tonic-gate * visible yet we don't need to grab any locks before initializing its 2079*0Sstevel@tonic-gate * zone_proc pointer. 2080*0Sstevel@tonic-gate */ 2081*0Sstevel@tonic-gate zone_hold(zone); /* this hold is released by zone_destroy() */ 2082*0Sstevel@tonic-gate zone->zone_zsched = pp; 2083*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2084*0Sstevel@tonic-gate pp->p_zone = zone; 2085*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2086*0Sstevel@tonic-gate 2087*0Sstevel@tonic-gate /* 2088*0Sstevel@tonic-gate * Disassociate process from its 'parent'; parent ourselves to init 2089*0Sstevel@tonic-gate * (pid 1) and change other values as needed. 2090*0Sstevel@tonic-gate */ 2091*0Sstevel@tonic-gate sess_create(); 2092*0Sstevel@tonic-gate 2093*0Sstevel@tonic-gate mutex_enter(&pidlock); 2094*0Sstevel@tonic-gate proc_detach(pp); 2095*0Sstevel@tonic-gate pp->p_ppid = 1; 2096*0Sstevel@tonic-gate pp->p_flag |= SZONETOP; 2097*0Sstevel@tonic-gate pp->p_ancpid = 1; 2098*0Sstevel@tonic-gate pp->p_parent = initp; 2099*0Sstevel@tonic-gate pp->p_psibling = NULL; 2100*0Sstevel@tonic-gate if (initp->p_child) 2101*0Sstevel@tonic-gate initp->p_child->p_psibling = pp; 2102*0Sstevel@tonic-gate pp->p_sibling = initp->p_child; 2103*0Sstevel@tonic-gate initp->p_child = pp; 2104*0Sstevel@tonic-gate 2105*0Sstevel@tonic-gate /* Decrement what newproc() incremented. */ 2106*0Sstevel@tonic-gate upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID); 2107*0Sstevel@tonic-gate /* 2108*0Sstevel@tonic-gate * Our credentials are about to become kcred-like, so we don't care 2109*0Sstevel@tonic-gate * about the caller's ruid. 2110*0Sstevel@tonic-gate */ 2111*0Sstevel@tonic-gate upcount_inc(crgetruid(kcred), zone->zone_id); 2112*0Sstevel@tonic-gate mutex_exit(&pidlock); 2113*0Sstevel@tonic-gate 2114*0Sstevel@tonic-gate /* 2115*0Sstevel@tonic-gate * getting out of global zone, so decrement lwp counts 2116*0Sstevel@tonic-gate */ 2117*0Sstevel@tonic-gate pj = pp->p_task->tk_proj; 2118*0Sstevel@tonic-gate mutex_enter(&global_zone->zone_nlwps_lock); 2119*0Sstevel@tonic-gate pj->kpj_nlwps -= pp->p_lwpcnt; 2120*0Sstevel@tonic-gate global_zone->zone_nlwps -= pp->p_lwpcnt; 2121*0Sstevel@tonic-gate mutex_exit(&global_zone->zone_nlwps_lock); 2122*0Sstevel@tonic-gate 2123*0Sstevel@tonic-gate /* 2124*0Sstevel@tonic-gate * Create and join a new task in project '0' of this zone. 2125*0Sstevel@tonic-gate * 2126*0Sstevel@tonic-gate * We don't need to call holdlwps() since we know we're the only lwp in 2127*0Sstevel@tonic-gate * this process. 2128*0Sstevel@tonic-gate * 2129*0Sstevel@tonic-gate * task_join() returns with p_lock held. 2130*0Sstevel@tonic-gate */ 2131*0Sstevel@tonic-gate tk = task_create(0, zone); 2132*0Sstevel@tonic-gate mutex_enter(&cpu_lock); 2133*0Sstevel@tonic-gate oldtk = task_join(tk, 0); 2134*0Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 2135*0Sstevel@tonic-gate mutex_exit(&cpu_lock); 2136*0Sstevel@tonic-gate task_rele(oldtk); 2137*0Sstevel@tonic-gate 2138*0Sstevel@tonic-gate /* 2139*0Sstevel@tonic-gate * add lwp counts to zsched's zone, and increment project's task count 2140*0Sstevel@tonic-gate * due to the task created in the above tasksys_settaskid 2141*0Sstevel@tonic-gate */ 2142*0Sstevel@tonic-gate pj = pp->p_task->tk_proj; 2143*0Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 2144*0Sstevel@tonic-gate pj->kpj_nlwps += pp->p_lwpcnt; 2145*0Sstevel@tonic-gate pj->kpj_ntasks += 1; 2146*0Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 2147*0Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 2148*0Sstevel@tonic-gate 2149*0Sstevel@tonic-gate /* 2150*0Sstevel@tonic-gate * The process was created by a process in the global zone, hence the 2151*0Sstevel@tonic-gate * credentials are wrong. We might as well have kcred-ish credentials. 2152*0Sstevel@tonic-gate */ 2153*0Sstevel@tonic-gate cr = zone->zone_kcred; 2154*0Sstevel@tonic-gate crhold(cr); 2155*0Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 2156*0Sstevel@tonic-gate oldcred = pp->p_cred; 2157*0Sstevel@tonic-gate pp->p_cred = cr; 2158*0Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 2159*0Sstevel@tonic-gate crfree(oldcred); 2160*0Sstevel@tonic-gate 2161*0Sstevel@tonic-gate /* 2162*0Sstevel@tonic-gate * Hold credentials again (for thread) 2163*0Sstevel@tonic-gate */ 2164*0Sstevel@tonic-gate crhold(cr); 2165*0Sstevel@tonic-gate 2166*0Sstevel@tonic-gate /* 2167*0Sstevel@tonic-gate * p_lwpcnt can't change since this is a kernel process. 2168*0Sstevel@tonic-gate */ 2169*0Sstevel@tonic-gate crset(pp, cr); 2170*0Sstevel@tonic-gate 2171*0Sstevel@tonic-gate /* 2172*0Sstevel@tonic-gate * Chroot 2173*0Sstevel@tonic-gate */ 2174*0Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp); 2175*0Sstevel@tonic-gate zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp); 2176*0Sstevel@tonic-gate 2177*0Sstevel@tonic-gate /* 2178*0Sstevel@tonic-gate * Initialize zone's rctl set. 2179*0Sstevel@tonic-gate */ 2180*0Sstevel@tonic-gate set = rctl_set_create(); 2181*0Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_ZONE); 2182*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2183*0Sstevel@tonic-gate e.rcep_p.zone = zone; 2184*0Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 2185*0Sstevel@tonic-gate zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp); 2186*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2187*0Sstevel@tonic-gate rctl_prealloc_destroy(gp); 2188*0Sstevel@tonic-gate 2189*0Sstevel@tonic-gate /* 2190*0Sstevel@tonic-gate * Apply the rctls passed in to zone_create(). This is basically a list 2191*0Sstevel@tonic-gate * assignment: all of the old values are removed and the new ones 2192*0Sstevel@tonic-gate * inserted. That is, if an empty list is passed in, all values are 2193*0Sstevel@tonic-gate * removed. 2194*0Sstevel@tonic-gate */ 2195*0Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 2196*0Sstevel@tonic-gate rctl_dict_entry_t *rde; 2197*0Sstevel@tonic-gate rctl_hndl_t hndl; 2198*0Sstevel@tonic-gate char *name; 2199*0Sstevel@tonic-gate nvlist_t **nvlarray; 2200*0Sstevel@tonic-gate uint_t i, nelem; 2201*0Sstevel@tonic-gate int error; /* For ASSERT()s */ 2202*0Sstevel@tonic-gate 2203*0Sstevel@tonic-gate name = nvpair_name(nvp); 2204*0Sstevel@tonic-gate hndl = rctl_hndl_lookup(name); 2205*0Sstevel@tonic-gate ASSERT(hndl != -1); 2206*0Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 2207*0Sstevel@tonic-gate ASSERT(rde != NULL); 2208*0Sstevel@tonic-gate 2209*0Sstevel@tonic-gate for (; /* ever */; ) { 2210*0Sstevel@tonic-gate rctl_val_t oval; 2211*0Sstevel@tonic-gate 2212*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2213*0Sstevel@tonic-gate error = rctl_local_get(hndl, NULL, &oval, pp); 2214*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2215*0Sstevel@tonic-gate ASSERT(error == 0); /* Can't fail for RCTL_FIRST */ 2216*0Sstevel@tonic-gate ASSERT(oval.rcv_privilege != RCPRIV_BASIC); 2217*0Sstevel@tonic-gate if (oval.rcv_privilege == RCPRIV_SYSTEM) 2218*0Sstevel@tonic-gate break; 2219*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2220*0Sstevel@tonic-gate error = rctl_local_delete(hndl, &oval, pp); 2221*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2222*0Sstevel@tonic-gate ASSERT(error == 0); 2223*0Sstevel@tonic-gate } 2224*0Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 2225*0Sstevel@tonic-gate ASSERT(error == 0); 2226*0Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 2227*0Sstevel@tonic-gate rctl_val_t *nvalp; 2228*0Sstevel@tonic-gate 2229*0Sstevel@tonic-gate nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP); 2230*0Sstevel@tonic-gate error = nvlist2rctlval(nvlarray[i], nvalp); 2231*0Sstevel@tonic-gate ASSERT(error == 0); 2232*0Sstevel@tonic-gate /* 2233*0Sstevel@tonic-gate * rctl_local_insert can fail if the value being 2234*0Sstevel@tonic-gate * inserted is a duplicate; this is OK. 2235*0Sstevel@tonic-gate */ 2236*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2237*0Sstevel@tonic-gate if (rctl_local_insert(hndl, nvalp, pp) != 0) 2238*0Sstevel@tonic-gate kmem_cache_free(rctl_val_cache, nvalp); 2239*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2240*0Sstevel@tonic-gate } 2241*0Sstevel@tonic-gate } 2242*0Sstevel@tonic-gate /* 2243*0Sstevel@tonic-gate * Tell the world that we're done setting up. 2244*0Sstevel@tonic-gate * 2245*0Sstevel@tonic-gate * At this point we want to set the zone status to ZONE_IS_READY 2246*0Sstevel@tonic-gate * and atomically set the zone's processor set visibility. Once 2247*0Sstevel@tonic-gate * we drop pool_lock() this zone will automatically get updated 2248*0Sstevel@tonic-gate * to reflect any future changes to the pools configuration. 2249*0Sstevel@tonic-gate */ 2250*0Sstevel@tonic-gate pool_lock(); 2251*0Sstevel@tonic-gate mutex_enter(&cpu_lock); 2252*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 2253*0Sstevel@tonic-gate zone_uniqid(zone); 2254*0Sstevel@tonic-gate zone_zsd_configure(zone); 2255*0Sstevel@tonic-gate if (pool_state == POOL_ENABLED) 2256*0Sstevel@tonic-gate zone_pset_set(zone, pool_default->pool_pset->pset_id); 2257*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 2258*0Sstevel@tonic-gate ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED); 2259*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_READY); 2260*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2261*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2262*0Sstevel@tonic-gate mutex_exit(&cpu_lock); 2263*0Sstevel@tonic-gate pool_unlock(); 2264*0Sstevel@tonic-gate 2265*0Sstevel@tonic-gate /* 2266*0Sstevel@tonic-gate * Once we see the zone transition to the ZONE_IS_BOOTING state, 2267*0Sstevel@tonic-gate * we launch init, and set the state to running. 2268*0Sstevel@tonic-gate */ 2269*0Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched"); 2270*0Sstevel@tonic-gate 2271*0Sstevel@tonic-gate if (zone_status_get(zone) == ZONE_IS_BOOTING) { 2272*0Sstevel@tonic-gate id_t cid; 2273*0Sstevel@tonic-gate 2274*0Sstevel@tonic-gate /* 2275*0Sstevel@tonic-gate * Ok, this is a little complicated. We need to grab the 2276*0Sstevel@tonic-gate * zone's pool's scheduling class ID; note that by now, we 2277*0Sstevel@tonic-gate * are already bound to a pool if we need to be (zoneadmd 2278*0Sstevel@tonic-gate * will have done that to us while we're in the READY 2279*0Sstevel@tonic-gate * state). *But* the scheduling class for the zone's 'init' 2280*0Sstevel@tonic-gate * must be explicitly passed to newproc, which doesn't 2281*0Sstevel@tonic-gate * respect pool bindings. 2282*0Sstevel@tonic-gate * 2283*0Sstevel@tonic-gate * We hold the pool_lock across the call to newproc() to 2284*0Sstevel@tonic-gate * close the obvious race: the pool's scheduling class 2285*0Sstevel@tonic-gate * could change before we manage to create the LWP with 2286*0Sstevel@tonic-gate * classid 'cid'. 2287*0Sstevel@tonic-gate */ 2288*0Sstevel@tonic-gate pool_lock(); 2289*0Sstevel@tonic-gate cid = pool_get_class(zone->zone_pool); 2290*0Sstevel@tonic-gate if (cid == -1) 2291*0Sstevel@tonic-gate cid = defaultcid; 2292*0Sstevel@tonic-gate 2293*0Sstevel@tonic-gate /* 2294*0Sstevel@tonic-gate * If this fails, zone_boot will ultimately fail. The 2295*0Sstevel@tonic-gate * state of the zone will be set to SHUTTING_DOWN-- userland 2296*0Sstevel@tonic-gate * will have to tear down the zone, and fail, or try again. 2297*0Sstevel@tonic-gate */ 2298*0Sstevel@tonic-gate if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid, 2299*0Sstevel@tonic-gate minclsyspri - 1, &ct)) != 0) { 2300*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 2301*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 2302*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2303*0Sstevel@tonic-gate } 2304*0Sstevel@tonic-gate pool_unlock(); 2305*0Sstevel@tonic-gate } 2306*0Sstevel@tonic-gate 2307*0Sstevel@tonic-gate /* 2308*0Sstevel@tonic-gate * Wait for zone_destroy() to be called. This is what we spend 2309*0Sstevel@tonic-gate * most of our life doing. 2310*0Sstevel@tonic-gate */ 2311*0Sstevel@tonic-gate zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched"); 2312*0Sstevel@tonic-gate 2313*0Sstevel@tonic-gate if (ct) 2314*0Sstevel@tonic-gate /* 2315*0Sstevel@tonic-gate * At this point the process contract should be empty. 2316*0Sstevel@tonic-gate * (Though if it isn't, it's not the end of the world.) 2317*0Sstevel@tonic-gate */ 2318*0Sstevel@tonic-gate VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0); 2319*0Sstevel@tonic-gate 2320*0Sstevel@tonic-gate /* 2321*0Sstevel@tonic-gate * Allow kcred to be freed when all referring processes 2322*0Sstevel@tonic-gate * (including this one) go away. We can't just do this in 2323*0Sstevel@tonic-gate * zone_free because we need to wait for the zone_cred_ref to 2324*0Sstevel@tonic-gate * drop to 0 before calling zone_free, and the existence of 2325*0Sstevel@tonic-gate * zone_kcred will prevent that. Thus, we call crfree here to 2326*0Sstevel@tonic-gate * balance the crdup in zone_create. The crhold calls earlier 2327*0Sstevel@tonic-gate * in zsched will be dropped when the thread and process exit. 2328*0Sstevel@tonic-gate */ 2329*0Sstevel@tonic-gate crfree(zone->zone_kcred); 2330*0Sstevel@tonic-gate zone->zone_kcred = NULL; 2331*0Sstevel@tonic-gate 2332*0Sstevel@tonic-gate exit(CLD_EXITED, 0); 2333*0Sstevel@tonic-gate } 2334*0Sstevel@tonic-gate 2335*0Sstevel@tonic-gate /* 2336*0Sstevel@tonic-gate * Helper function to determine if there are any submounts of the 2337*0Sstevel@tonic-gate * provided path. Used to make sure the zone doesn't "inherit" any 2338*0Sstevel@tonic-gate * mounts from before it is created. 2339*0Sstevel@tonic-gate */ 2340*0Sstevel@tonic-gate static uint_t 2341*0Sstevel@tonic-gate zone_mount_count(const char *rootpath) 2342*0Sstevel@tonic-gate { 2343*0Sstevel@tonic-gate vfs_t *vfsp; 2344*0Sstevel@tonic-gate uint_t count = 0; 2345*0Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 2346*0Sstevel@tonic-gate 2347*0Sstevel@tonic-gate /* 2348*0Sstevel@tonic-gate * Holding zonehash_lock prevents race conditions with 2349*0Sstevel@tonic-gate * vfs_list_add()/vfs_list_remove() since we serialize with 2350*0Sstevel@tonic-gate * zone_find_by_path(). 2351*0Sstevel@tonic-gate */ 2352*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 2353*0Sstevel@tonic-gate /* 2354*0Sstevel@tonic-gate * The rootpath must end with a '/' 2355*0Sstevel@tonic-gate */ 2356*0Sstevel@tonic-gate ASSERT(rootpath[rootpathlen - 1] == '/'); 2357*0Sstevel@tonic-gate 2358*0Sstevel@tonic-gate /* 2359*0Sstevel@tonic-gate * This intentionally does not count the rootpath itself if that 2360*0Sstevel@tonic-gate * happens to be a mount point. 2361*0Sstevel@tonic-gate */ 2362*0Sstevel@tonic-gate vfs_list_read_lock(); 2363*0Sstevel@tonic-gate vfsp = rootvfs; 2364*0Sstevel@tonic-gate do { 2365*0Sstevel@tonic-gate if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt), 2366*0Sstevel@tonic-gate rootpathlen) == 0) 2367*0Sstevel@tonic-gate count++; 2368*0Sstevel@tonic-gate vfsp = vfsp->vfs_next; 2369*0Sstevel@tonic-gate } while (vfsp != rootvfs); 2370*0Sstevel@tonic-gate vfs_list_unlock(); 2371*0Sstevel@tonic-gate return (count); 2372*0Sstevel@tonic-gate } 2373*0Sstevel@tonic-gate 2374*0Sstevel@tonic-gate /* 2375*0Sstevel@tonic-gate * Helper function to make sure that a zone created on 'rootpath' 2376*0Sstevel@tonic-gate * wouldn't end up containing other zones' rootpaths. 2377*0Sstevel@tonic-gate */ 2378*0Sstevel@tonic-gate static boolean_t 2379*0Sstevel@tonic-gate zone_is_nested(const char *rootpath) 2380*0Sstevel@tonic-gate { 2381*0Sstevel@tonic-gate zone_t *zone; 2382*0Sstevel@tonic-gate size_t rootpathlen = strlen(rootpath); 2383*0Sstevel@tonic-gate size_t len; 2384*0Sstevel@tonic-gate 2385*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&zonehash_lock)); 2386*0Sstevel@tonic-gate 2387*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 2388*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) { 2389*0Sstevel@tonic-gate if (zone == global_zone) 2390*0Sstevel@tonic-gate continue; 2391*0Sstevel@tonic-gate len = strlen(zone->zone_rootpath); 2392*0Sstevel@tonic-gate if (strncmp(rootpath, zone->zone_rootpath, 2393*0Sstevel@tonic-gate MIN(rootpathlen, len)) == 0) 2394*0Sstevel@tonic-gate return (B_TRUE); 2395*0Sstevel@tonic-gate } 2396*0Sstevel@tonic-gate return (B_FALSE); 2397*0Sstevel@tonic-gate } 2398*0Sstevel@tonic-gate 2399*0Sstevel@tonic-gate static int 2400*0Sstevel@tonic-gate zone_set_privset(zone_t *zone, const priv_set_t *zone_privs) 2401*0Sstevel@tonic-gate { 2402*0Sstevel@tonic-gate priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP); 2403*0Sstevel@tonic-gate 2404*0Sstevel@tonic-gate if (copyin(zone_privs, privs, sizeof (priv_set_t))) { 2405*0Sstevel@tonic-gate kmem_free(privs, sizeof (priv_set_t)); 2406*0Sstevel@tonic-gate return (EFAULT); 2407*0Sstevel@tonic-gate } 2408*0Sstevel@tonic-gate 2409*0Sstevel@tonic-gate zone->zone_privset = privs; 2410*0Sstevel@tonic-gate return (0); 2411*0Sstevel@tonic-gate } 2412*0Sstevel@tonic-gate 2413*0Sstevel@tonic-gate /* 2414*0Sstevel@tonic-gate * We make creative use of nvlists to pass in rctls from userland. The list is 2415*0Sstevel@tonic-gate * a list of the following structures: 2416*0Sstevel@tonic-gate * 2417*0Sstevel@tonic-gate * (name = rctl_name, value = nvpair_list_array) 2418*0Sstevel@tonic-gate * 2419*0Sstevel@tonic-gate * Where each element of the nvpair_list_array is of the form: 2420*0Sstevel@tonic-gate * 2421*0Sstevel@tonic-gate * [(name = "privilege", value = RCPRIV_PRIVILEGED), 2422*0Sstevel@tonic-gate * (name = "limit", value = uint64_t), 2423*0Sstevel@tonic-gate * (name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))] 2424*0Sstevel@tonic-gate */ 2425*0Sstevel@tonic-gate static int 2426*0Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp) 2427*0Sstevel@tonic-gate { 2428*0Sstevel@tonic-gate nvpair_t *nvp = NULL; 2429*0Sstevel@tonic-gate nvlist_t *nvl = NULL; 2430*0Sstevel@tonic-gate char *kbuf; 2431*0Sstevel@tonic-gate int error; 2432*0Sstevel@tonic-gate rctl_val_t rv; 2433*0Sstevel@tonic-gate 2434*0Sstevel@tonic-gate *nvlp = NULL; 2435*0Sstevel@tonic-gate 2436*0Sstevel@tonic-gate if (buflen == 0) 2437*0Sstevel@tonic-gate return (0); 2438*0Sstevel@tonic-gate 2439*0Sstevel@tonic-gate if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL) 2440*0Sstevel@tonic-gate return (ENOMEM); 2441*0Sstevel@tonic-gate if (copyin(ubuf, kbuf, buflen)) { 2442*0Sstevel@tonic-gate error = EFAULT; 2443*0Sstevel@tonic-gate goto out; 2444*0Sstevel@tonic-gate } 2445*0Sstevel@tonic-gate if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) { 2446*0Sstevel@tonic-gate /* 2447*0Sstevel@tonic-gate * nvl may have been allocated/free'd, but the value set to 2448*0Sstevel@tonic-gate * non-NULL, so we reset it here. 2449*0Sstevel@tonic-gate */ 2450*0Sstevel@tonic-gate nvl = NULL; 2451*0Sstevel@tonic-gate error = EINVAL; 2452*0Sstevel@tonic-gate goto out; 2453*0Sstevel@tonic-gate } 2454*0Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 2455*0Sstevel@tonic-gate rctl_dict_entry_t *rde; 2456*0Sstevel@tonic-gate rctl_hndl_t hndl; 2457*0Sstevel@tonic-gate nvlist_t **nvlarray; 2458*0Sstevel@tonic-gate uint_t i, nelem; 2459*0Sstevel@tonic-gate char *name; 2460*0Sstevel@tonic-gate 2461*0Sstevel@tonic-gate error = EINVAL; 2462*0Sstevel@tonic-gate name = nvpair_name(nvp); 2463*0Sstevel@tonic-gate if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1) 2464*0Sstevel@tonic-gate != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) { 2465*0Sstevel@tonic-gate goto out; 2466*0Sstevel@tonic-gate } 2467*0Sstevel@tonic-gate if ((hndl = rctl_hndl_lookup(name)) == -1) { 2468*0Sstevel@tonic-gate goto out; 2469*0Sstevel@tonic-gate } 2470*0Sstevel@tonic-gate rde = rctl_dict_lookup_hndl(hndl); 2471*0Sstevel@tonic-gate error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem); 2472*0Sstevel@tonic-gate ASSERT(error == 0); 2473*0Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 2474*0Sstevel@tonic-gate if (error = nvlist2rctlval(nvlarray[i], &rv)) 2475*0Sstevel@tonic-gate goto out; 2476*0Sstevel@tonic-gate } 2477*0Sstevel@tonic-gate if (rctl_invalid_value(rde, &rv)) { 2478*0Sstevel@tonic-gate error = EINVAL; 2479*0Sstevel@tonic-gate goto out; 2480*0Sstevel@tonic-gate } 2481*0Sstevel@tonic-gate } 2482*0Sstevel@tonic-gate error = 0; 2483*0Sstevel@tonic-gate *nvlp = nvl; 2484*0Sstevel@tonic-gate out: 2485*0Sstevel@tonic-gate kmem_free(kbuf, buflen); 2486*0Sstevel@tonic-gate if (error && nvl != NULL) 2487*0Sstevel@tonic-gate nvlist_free(nvl); 2488*0Sstevel@tonic-gate return (error); 2489*0Sstevel@tonic-gate } 2490*0Sstevel@tonic-gate 2491*0Sstevel@tonic-gate int 2492*0Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) { 2493*0Sstevel@tonic-gate if (er_out != NULL) { 2494*0Sstevel@tonic-gate if (copyout(&er_ext, er_out, sizeof (int))) { 2495*0Sstevel@tonic-gate return (set_errno(EFAULT)); 2496*0Sstevel@tonic-gate } 2497*0Sstevel@tonic-gate } 2498*0Sstevel@tonic-gate return (set_errno(er_error)); 2499*0Sstevel@tonic-gate } 2500*0Sstevel@tonic-gate 2501*0Sstevel@tonic-gate /* 2502*0Sstevel@tonic-gate * System call to create/initialize a new zone named 'zone_name', rooted 2503*0Sstevel@tonic-gate * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs', 2504*0Sstevel@tonic-gate * and initialized with the zone-wide rctls described in 'rctlbuf'. 2505*0Sstevel@tonic-gate * 2506*0Sstevel@tonic-gate * If extended error is non-null, we may use it to return more detailed 2507*0Sstevel@tonic-gate * error information. 2508*0Sstevel@tonic-gate */ 2509*0Sstevel@tonic-gate static zoneid_t 2510*0Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root, 2511*0Sstevel@tonic-gate const priv_set_t *zone_privs, caddr_t rctlbuf, size_t rctlbufsz, 2512*0Sstevel@tonic-gate int *extended_error) 2513*0Sstevel@tonic-gate { 2514*0Sstevel@tonic-gate struct zsched_arg zarg; 2515*0Sstevel@tonic-gate nvlist_t *rctls = NULL; 2516*0Sstevel@tonic-gate proc_t *pp = curproc; 2517*0Sstevel@tonic-gate zone_t *zone, *ztmp; 2518*0Sstevel@tonic-gate zoneid_t zoneid; 2519*0Sstevel@tonic-gate int error; 2520*0Sstevel@tonic-gate int error2 = 0; 2521*0Sstevel@tonic-gate char *str; 2522*0Sstevel@tonic-gate cred_t *zkcr; 2523*0Sstevel@tonic-gate 2524*0Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 2525*0Sstevel@tonic-gate return (set_errno(EPERM)); 2526*0Sstevel@tonic-gate 2527*0Sstevel@tonic-gate /* can't boot zone from within chroot environment */ 2528*0Sstevel@tonic-gate if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir) 2529*0Sstevel@tonic-gate return (zone_create_error(ENOTSUP, ZE_CHROOTED, 2530*0Sstevel@tonic-gate extended_error)); 2531*0Sstevel@tonic-gate 2532*0Sstevel@tonic-gate zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP); 2533*0Sstevel@tonic-gate zoneid = zone->zone_id = id_alloc(zoneid_space); 2534*0Sstevel@tonic-gate zone->zone_status = ZONE_IS_UNINITIALIZED; 2535*0Sstevel@tonic-gate zone->zone_pool = pool_default; 2536*0Sstevel@tonic-gate zone->zone_pool_mod = gethrtime(); 2537*0Sstevel@tonic-gate zone->zone_psetid = ZONE_PS_INVAL; 2538*0Sstevel@tonic-gate zone->zone_ncpus = 0; 2539*0Sstevel@tonic-gate zone->zone_ncpus_online = 0; 2540*0Sstevel@tonic-gate mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL); 2541*0Sstevel@tonic-gate mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL); 2542*0Sstevel@tonic-gate cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL); 2543*0Sstevel@tonic-gate list_create(&zone->zone_zsd, sizeof (struct zsd_entry), 2544*0Sstevel@tonic-gate offsetof(struct zsd_entry, zsd_linkage)); 2545*0Sstevel@tonic-gate 2546*0Sstevel@tonic-gate if ((error = zone_set_name(zone, zone_name)) != 0) { 2547*0Sstevel@tonic-gate zone_free(zone); 2548*0Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 2549*0Sstevel@tonic-gate } 2550*0Sstevel@tonic-gate 2551*0Sstevel@tonic-gate if ((error = zone_set_root(zone, zone_root)) != 0) { 2552*0Sstevel@tonic-gate zone_free(zone); 2553*0Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 2554*0Sstevel@tonic-gate } 2555*0Sstevel@tonic-gate if ((error = zone_set_privset(zone, zone_privs)) != 0) { 2556*0Sstevel@tonic-gate zone_free(zone); 2557*0Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 2558*0Sstevel@tonic-gate } 2559*0Sstevel@tonic-gate 2560*0Sstevel@tonic-gate /* initialize node name to be the same as zone name */ 2561*0Sstevel@tonic-gate zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP); 2562*0Sstevel@tonic-gate (void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN); 2563*0Sstevel@tonic-gate zone->zone_nodename[_SYS_NMLN - 1] = '\0'; 2564*0Sstevel@tonic-gate 2565*0Sstevel@tonic-gate zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP); 2566*0Sstevel@tonic-gate zone->zone_domain[0] = '\0'; 2567*0Sstevel@tonic-gate zone->zone_shares = 1; 2568*0Sstevel@tonic-gate zone->zone_bootargs = NULL; 2569*0Sstevel@tonic-gate 2570*0Sstevel@tonic-gate /* 2571*0Sstevel@tonic-gate * Zsched initializes the rctls. 2572*0Sstevel@tonic-gate */ 2573*0Sstevel@tonic-gate zone->zone_rctls = NULL; 2574*0Sstevel@tonic-gate 2575*0Sstevel@tonic-gate if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) { 2576*0Sstevel@tonic-gate zone_free(zone); 2577*0Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 2578*0Sstevel@tonic-gate } 2579*0Sstevel@tonic-gate 2580*0Sstevel@tonic-gate /* 2581*0Sstevel@tonic-gate * Stop all lwps since that's what normally happens as part of fork(). 2582*0Sstevel@tonic-gate * This needs to happen before we grab any locks to avoid deadlock 2583*0Sstevel@tonic-gate * (another lwp in the process could be waiting for the held lock). 2584*0Sstevel@tonic-gate */ 2585*0Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) { 2586*0Sstevel@tonic-gate zone_free(zone); 2587*0Sstevel@tonic-gate if (rctls) 2588*0Sstevel@tonic-gate nvlist_free(rctls); 2589*0Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 2590*0Sstevel@tonic-gate } 2591*0Sstevel@tonic-gate 2592*0Sstevel@tonic-gate if (block_mounts() == 0) { 2593*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2594*0Sstevel@tonic-gate if (curthread != pp->p_agenttp) 2595*0Sstevel@tonic-gate continuelwps(pp); 2596*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2597*0Sstevel@tonic-gate zone_free(zone); 2598*0Sstevel@tonic-gate if (rctls) 2599*0Sstevel@tonic-gate nvlist_free(rctls); 2600*0Sstevel@tonic-gate return (zone_create_error(error, 0, extended_error)); 2601*0Sstevel@tonic-gate } 2602*0Sstevel@tonic-gate 2603*0Sstevel@tonic-gate /* 2604*0Sstevel@tonic-gate * Set up credential for kernel access. After this, any errors 2605*0Sstevel@tonic-gate * should go through the dance in errout rather than calling 2606*0Sstevel@tonic-gate * zone_free directly. 2607*0Sstevel@tonic-gate */ 2608*0Sstevel@tonic-gate zone->zone_kcred = crdup(kcred); 2609*0Sstevel@tonic-gate crsetzone(zone->zone_kcred, zone); 2610*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred)); 2611*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred)); 2612*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred)); 2613*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred)); 2614*0Sstevel@tonic-gate 2615*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 2616*0Sstevel@tonic-gate /* 2617*0Sstevel@tonic-gate * Make sure zone doesn't already exist. 2618*0Sstevel@tonic-gate */ 2619*0Sstevel@tonic-gate if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL) { 2620*0Sstevel@tonic-gate zone_status_t status; 2621*0Sstevel@tonic-gate 2622*0Sstevel@tonic-gate status = zone_status_get(ztmp); 2623*0Sstevel@tonic-gate if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING) 2624*0Sstevel@tonic-gate error = EEXIST; 2625*0Sstevel@tonic-gate else 2626*0Sstevel@tonic-gate error = EBUSY; 2627*0Sstevel@tonic-gate goto errout; 2628*0Sstevel@tonic-gate } 2629*0Sstevel@tonic-gate 2630*0Sstevel@tonic-gate /* 2631*0Sstevel@tonic-gate * Don't allow zone creations which would cause one zone's rootpath to 2632*0Sstevel@tonic-gate * be accessible from that of another (non-global) zone. 2633*0Sstevel@tonic-gate */ 2634*0Sstevel@tonic-gate if (zone_is_nested(zone->zone_rootpath)) { 2635*0Sstevel@tonic-gate error = EBUSY; 2636*0Sstevel@tonic-gate goto errout; 2637*0Sstevel@tonic-gate } 2638*0Sstevel@tonic-gate 2639*0Sstevel@tonic-gate ASSERT(zonecount != 0); /* check for leaks */ 2640*0Sstevel@tonic-gate if (zonecount + 1 > maxzones) { 2641*0Sstevel@tonic-gate error = ENOMEM; 2642*0Sstevel@tonic-gate goto errout; 2643*0Sstevel@tonic-gate } 2644*0Sstevel@tonic-gate 2645*0Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 2646*0Sstevel@tonic-gate error = EBUSY; 2647*0Sstevel@tonic-gate error2 = ZE_AREMOUNTS; 2648*0Sstevel@tonic-gate goto errout; 2649*0Sstevel@tonic-gate } 2650*0Sstevel@tonic-gate 2651*0Sstevel@tonic-gate /* 2652*0Sstevel@tonic-gate * Zone is still incomplete, but we need to drop all locks while 2653*0Sstevel@tonic-gate * zsched() initializes this zone's kernel process. We 2654*0Sstevel@tonic-gate * optimistically add the zone to the hashtable and associated 2655*0Sstevel@tonic-gate * lists so a parallel zone_create() doesn't try to create the 2656*0Sstevel@tonic-gate * same zone. 2657*0Sstevel@tonic-gate */ 2658*0Sstevel@tonic-gate zonecount++; 2659*0Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyid, 2660*0Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id, 2661*0Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 2662*0Sstevel@tonic-gate str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP); 2663*0Sstevel@tonic-gate (void) strcpy(str, zone->zone_name); 2664*0Sstevel@tonic-gate (void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str, 2665*0Sstevel@tonic-gate (mod_hash_val_t)(uintptr_t)zone); 2666*0Sstevel@tonic-gate /* 2667*0Sstevel@tonic-gate * Insert into active list. At this point there are no 'hold's 2668*0Sstevel@tonic-gate * on the zone, but everyone else knows not to use it, so we can 2669*0Sstevel@tonic-gate * continue to use it. zsched() will do a zone_hold() if the 2670*0Sstevel@tonic-gate * newproc() is successful. 2671*0Sstevel@tonic-gate */ 2672*0Sstevel@tonic-gate list_insert_tail(&zone_active, zone); 2673*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2674*0Sstevel@tonic-gate 2675*0Sstevel@tonic-gate zarg.zone = zone; 2676*0Sstevel@tonic-gate zarg.nvlist = rctls; 2677*0Sstevel@tonic-gate /* 2678*0Sstevel@tonic-gate * The process, task, and project rctls are probably wrong; 2679*0Sstevel@tonic-gate * we need an interface to get the default values of all rctls, 2680*0Sstevel@tonic-gate * and initialize zsched appropriately. I'm not sure that that 2681*0Sstevel@tonic-gate * makes much of a difference, though. 2682*0Sstevel@tonic-gate */ 2683*0Sstevel@tonic-gate if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) { 2684*0Sstevel@tonic-gate /* 2685*0Sstevel@tonic-gate * We need to undo all globally visible state. 2686*0Sstevel@tonic-gate */ 2687*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 2688*0Sstevel@tonic-gate list_remove(&zone_active, zone); 2689*0Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 2690*0Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_name); 2691*0Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 2692*0Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 2693*0Sstevel@tonic-gate ASSERT(zonecount > 1); 2694*0Sstevel@tonic-gate zonecount--; 2695*0Sstevel@tonic-gate goto errout; 2696*0Sstevel@tonic-gate } 2697*0Sstevel@tonic-gate 2698*0Sstevel@tonic-gate /* 2699*0Sstevel@tonic-gate * Zone creation can't fail from now on. 2700*0Sstevel@tonic-gate */ 2701*0Sstevel@tonic-gate 2702*0Sstevel@tonic-gate /* 2703*0Sstevel@tonic-gate * Let the other lwps continue. 2704*0Sstevel@tonic-gate */ 2705*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2706*0Sstevel@tonic-gate if (curthread != pp->p_agenttp) 2707*0Sstevel@tonic-gate continuelwps(pp); 2708*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2709*0Sstevel@tonic-gate 2710*0Sstevel@tonic-gate /* 2711*0Sstevel@tonic-gate * Wait for zsched to finish initializing the zone. 2712*0Sstevel@tonic-gate */ 2713*0Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_READY); 2714*0Sstevel@tonic-gate /* 2715*0Sstevel@tonic-gate * The zone is fully visible, so we can let mounts progress. 2716*0Sstevel@tonic-gate */ 2717*0Sstevel@tonic-gate resume_mounts(); 2718*0Sstevel@tonic-gate if (rctls) 2719*0Sstevel@tonic-gate nvlist_free(rctls); 2720*0Sstevel@tonic-gate 2721*0Sstevel@tonic-gate return (zoneid); 2722*0Sstevel@tonic-gate 2723*0Sstevel@tonic-gate errout: 2724*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2725*0Sstevel@tonic-gate /* 2726*0Sstevel@tonic-gate * Let the other lwps continue. 2727*0Sstevel@tonic-gate */ 2728*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 2729*0Sstevel@tonic-gate if (curthread != pp->p_agenttp) 2730*0Sstevel@tonic-gate continuelwps(pp); 2731*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 2732*0Sstevel@tonic-gate 2733*0Sstevel@tonic-gate resume_mounts(); 2734*0Sstevel@tonic-gate if (rctls) 2735*0Sstevel@tonic-gate nvlist_free(rctls); 2736*0Sstevel@tonic-gate /* 2737*0Sstevel@tonic-gate * There is currently one reference to the zone, a cred_ref from 2738*0Sstevel@tonic-gate * zone_kcred. To free the zone, we call crfree, which will call 2739*0Sstevel@tonic-gate * zone_cred_rele, which will call zone_free. 2740*0Sstevel@tonic-gate */ 2741*0Sstevel@tonic-gate ASSERT(zone->zone_cred_ref == 1); /* for zone_kcred */ 2742*0Sstevel@tonic-gate ASSERT(zone->zone_kcred->cr_ref == 1); 2743*0Sstevel@tonic-gate ASSERT(zone->zone_ref == 0); 2744*0Sstevel@tonic-gate zkcr = zone->zone_kcred; 2745*0Sstevel@tonic-gate zone->zone_kcred = NULL; 2746*0Sstevel@tonic-gate crfree(zkcr); /* triggers call to zone_free */ 2747*0Sstevel@tonic-gate return (zone_create_error(error, error2, extended_error)); 2748*0Sstevel@tonic-gate } 2749*0Sstevel@tonic-gate 2750*0Sstevel@tonic-gate /* 2751*0Sstevel@tonic-gate * Cause the zone to boot. This is pretty simple, since we let zoneadmd do 2752*0Sstevel@tonic-gate * the heavy lifting. 2753*0Sstevel@tonic-gate */ 2754*0Sstevel@tonic-gate static int 2755*0Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs) 2756*0Sstevel@tonic-gate { 2757*0Sstevel@tonic-gate int err; 2758*0Sstevel@tonic-gate zone_t *zone; 2759*0Sstevel@tonic-gate 2760*0Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 2761*0Sstevel@tonic-gate return (set_errno(EPERM)); 2762*0Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 2763*0Sstevel@tonic-gate return (set_errno(EINVAL)); 2764*0Sstevel@tonic-gate 2765*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 2766*0Sstevel@tonic-gate /* 2767*0Sstevel@tonic-gate * Look for zone under hash lock to prevent races with calls to 2768*0Sstevel@tonic-gate * zone_shutdown, zone_destroy, etc. 2769*0Sstevel@tonic-gate */ 2770*0Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 2771*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2772*0Sstevel@tonic-gate return (set_errno(EINVAL)); 2773*0Sstevel@tonic-gate } 2774*0Sstevel@tonic-gate 2775*0Sstevel@tonic-gate if ((err = zone_set_bootargs(zone, bootargs)) != 0) { 2776*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2777*0Sstevel@tonic-gate return (set_errno(err)); 2778*0Sstevel@tonic-gate } 2779*0Sstevel@tonic-gate 2780*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 2781*0Sstevel@tonic-gate if (zone_status_get(zone) != ZONE_IS_READY) { 2782*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2783*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2784*0Sstevel@tonic-gate return (set_errno(EINVAL)); 2785*0Sstevel@tonic-gate } 2786*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_BOOTING); 2787*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2788*0Sstevel@tonic-gate 2789*0Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 2790*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2791*0Sstevel@tonic-gate 2792*0Sstevel@tonic-gate if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) { 2793*0Sstevel@tonic-gate zone_rele(zone); 2794*0Sstevel@tonic-gate return (set_errno(EINTR)); 2795*0Sstevel@tonic-gate } 2796*0Sstevel@tonic-gate 2797*0Sstevel@tonic-gate /* 2798*0Sstevel@tonic-gate * Boot (starting init) might have failed, in which case the zone 2799*0Sstevel@tonic-gate * will go to the SHUTTING_DOWN state; an appropriate errno will 2800*0Sstevel@tonic-gate * be placed in zone->zone_boot_err, and so we return that. 2801*0Sstevel@tonic-gate */ 2802*0Sstevel@tonic-gate err = zone->zone_boot_err; 2803*0Sstevel@tonic-gate zone_rele(zone); 2804*0Sstevel@tonic-gate return (err ? set_errno(err) : 0); 2805*0Sstevel@tonic-gate } 2806*0Sstevel@tonic-gate 2807*0Sstevel@tonic-gate /* 2808*0Sstevel@tonic-gate * Kills all user processes in the zone, waiting for them all to exit 2809*0Sstevel@tonic-gate * before returning. 2810*0Sstevel@tonic-gate */ 2811*0Sstevel@tonic-gate static int 2812*0Sstevel@tonic-gate zone_empty(zone_t *zone) 2813*0Sstevel@tonic-gate { 2814*0Sstevel@tonic-gate int waitstatus; 2815*0Sstevel@tonic-gate 2816*0Sstevel@tonic-gate /* 2817*0Sstevel@tonic-gate * We need to drop zonehash_lock before killing all 2818*0Sstevel@tonic-gate * processes, otherwise we'll deadlock with zone_find_* 2819*0Sstevel@tonic-gate * which can be called from the exit path. 2820*0Sstevel@tonic-gate */ 2821*0Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&zonehash_lock)); 2822*0Sstevel@tonic-gate while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz, 2823*0Sstevel@tonic-gate ZONE_IS_EMPTY)) == -1) { 2824*0Sstevel@tonic-gate killall(zone->zone_id); 2825*0Sstevel@tonic-gate } 2826*0Sstevel@tonic-gate /* 2827*0Sstevel@tonic-gate * return EINTR if we were signaled 2828*0Sstevel@tonic-gate */ 2829*0Sstevel@tonic-gate if (waitstatus == 0) 2830*0Sstevel@tonic-gate return (EINTR); 2831*0Sstevel@tonic-gate return (0); 2832*0Sstevel@tonic-gate } 2833*0Sstevel@tonic-gate 2834*0Sstevel@tonic-gate /* 2835*0Sstevel@tonic-gate * Systemcall to start the zone's halt sequence. By the time this 2836*0Sstevel@tonic-gate * function successfully returns, all user processes and kernel threads 2837*0Sstevel@tonic-gate * executing in it will have exited, ZSD shutdown callbacks executed, 2838*0Sstevel@tonic-gate * and the zone status set to ZONE_IS_DOWN. 2839*0Sstevel@tonic-gate * 2840*0Sstevel@tonic-gate * It is possible that the call will interrupt itself if the caller is the 2841*0Sstevel@tonic-gate * parent of any process running in the zone, and doesn't have SIGCHLD blocked. 2842*0Sstevel@tonic-gate */ 2843*0Sstevel@tonic-gate static int 2844*0Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid) 2845*0Sstevel@tonic-gate { 2846*0Sstevel@tonic-gate int error; 2847*0Sstevel@tonic-gate zone_t *zone; 2848*0Sstevel@tonic-gate zone_status_t status; 2849*0Sstevel@tonic-gate 2850*0Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 2851*0Sstevel@tonic-gate return (set_errno(EPERM)); 2852*0Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 2853*0Sstevel@tonic-gate return (set_errno(EINVAL)); 2854*0Sstevel@tonic-gate 2855*0Sstevel@tonic-gate /* 2856*0Sstevel@tonic-gate * Block mounts so that VFS_MOUNT() can get an accurate view of 2857*0Sstevel@tonic-gate * the zone's status with regards to ZONE_IS_SHUTTING down. 2858*0Sstevel@tonic-gate * 2859*0Sstevel@tonic-gate * e.g. NFS can fail the mount if it determines that the zone 2860*0Sstevel@tonic-gate * has already begun the shutdown sequence. 2861*0Sstevel@tonic-gate */ 2862*0Sstevel@tonic-gate if (block_mounts() == 0) 2863*0Sstevel@tonic-gate return (set_errno(EINTR)); 2864*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 2865*0Sstevel@tonic-gate /* 2866*0Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 2867*0Sstevel@tonic-gate * calls to zone_shutdown and zone_destroy. 2868*0Sstevel@tonic-gate */ 2869*0Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 2870*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2871*0Sstevel@tonic-gate resume_mounts(); 2872*0Sstevel@tonic-gate return (set_errno(EINVAL)); 2873*0Sstevel@tonic-gate } 2874*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 2875*0Sstevel@tonic-gate status = zone_status_get(zone); 2876*0Sstevel@tonic-gate /* 2877*0Sstevel@tonic-gate * Fail if the zone isn't fully initialized yet. 2878*0Sstevel@tonic-gate */ 2879*0Sstevel@tonic-gate if (status < ZONE_IS_READY) { 2880*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2881*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2882*0Sstevel@tonic-gate resume_mounts(); 2883*0Sstevel@tonic-gate return (set_errno(EINVAL)); 2884*0Sstevel@tonic-gate } 2885*0Sstevel@tonic-gate /* 2886*0Sstevel@tonic-gate * If conditions required for zone_shutdown() to return have been met, 2887*0Sstevel@tonic-gate * return success. 2888*0Sstevel@tonic-gate */ 2889*0Sstevel@tonic-gate if (status >= ZONE_IS_DOWN) { 2890*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2891*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2892*0Sstevel@tonic-gate resume_mounts(); 2893*0Sstevel@tonic-gate return (0); 2894*0Sstevel@tonic-gate } 2895*0Sstevel@tonic-gate /* 2896*0Sstevel@tonic-gate * If zone_shutdown() hasn't been called before, go through the motions. 2897*0Sstevel@tonic-gate * If it has, there's nothing to do but wait for the kernel threads to 2898*0Sstevel@tonic-gate * drain. 2899*0Sstevel@tonic-gate */ 2900*0Sstevel@tonic-gate if (status < ZONE_IS_EMPTY) { 2901*0Sstevel@tonic-gate uint_t ntasks; 2902*0Sstevel@tonic-gate 2903*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 2904*0Sstevel@tonic-gate if ((ntasks = zone->zone_ntasks) != 1) { 2905*0Sstevel@tonic-gate /* 2906*0Sstevel@tonic-gate * There's still stuff running. 2907*0Sstevel@tonic-gate */ 2908*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 2909*0Sstevel@tonic-gate } 2910*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 2911*0Sstevel@tonic-gate if (ntasks == 1) { 2912*0Sstevel@tonic-gate /* 2913*0Sstevel@tonic-gate * The only way to create another task is through 2914*0Sstevel@tonic-gate * zone_enter(), which will block until we drop 2915*0Sstevel@tonic-gate * zonehash_lock. The zone is empty. 2916*0Sstevel@tonic-gate */ 2917*0Sstevel@tonic-gate if (zone->zone_kthreads == NULL) { 2918*0Sstevel@tonic-gate /* 2919*0Sstevel@tonic-gate * Skip ahead to ZONE_IS_DOWN 2920*0Sstevel@tonic-gate */ 2921*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 2922*0Sstevel@tonic-gate } else { 2923*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_EMPTY); 2924*0Sstevel@tonic-gate } 2925*0Sstevel@tonic-gate } 2926*0Sstevel@tonic-gate } 2927*0Sstevel@tonic-gate zone_hold(zone); /* so we can use the zone_t later */ 2928*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2929*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 2930*0Sstevel@tonic-gate resume_mounts(); 2931*0Sstevel@tonic-gate 2932*0Sstevel@tonic-gate if (error = zone_empty(zone)) { 2933*0Sstevel@tonic-gate zone_rele(zone); 2934*0Sstevel@tonic-gate return (set_errno(error)); 2935*0Sstevel@tonic-gate } 2936*0Sstevel@tonic-gate /* 2937*0Sstevel@tonic-gate * After the zone status goes to ZONE_IS_DOWN this zone will no 2938*0Sstevel@tonic-gate * longer be notified of changes to the pools configuration, so 2939*0Sstevel@tonic-gate * in order to not end up with a stale pool pointer, we point 2940*0Sstevel@tonic-gate * ourselves at the default pool and remove all resource 2941*0Sstevel@tonic-gate * visibility. This is especially important as the zone_t may 2942*0Sstevel@tonic-gate * languish on the deathrow for a very long time waiting for 2943*0Sstevel@tonic-gate * cred's to drain out. 2944*0Sstevel@tonic-gate * 2945*0Sstevel@tonic-gate * This rebinding of the zone can happen multiple times 2946*0Sstevel@tonic-gate * (presumably due to interrupted or parallel systemcalls) 2947*0Sstevel@tonic-gate * without any adverse effects. 2948*0Sstevel@tonic-gate */ 2949*0Sstevel@tonic-gate if (pool_lock_intr() != 0) { 2950*0Sstevel@tonic-gate zone_rele(zone); 2951*0Sstevel@tonic-gate return (set_errno(EINTR)); 2952*0Sstevel@tonic-gate } 2953*0Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 2954*0Sstevel@tonic-gate mutex_enter(&cpu_lock); 2955*0Sstevel@tonic-gate zone_pool_set(zone, pool_default); 2956*0Sstevel@tonic-gate /* 2957*0Sstevel@tonic-gate * The zone no longer needs to be able to see any cpus. 2958*0Sstevel@tonic-gate */ 2959*0Sstevel@tonic-gate zone_pset_set(zone, ZONE_PS_INVAL); 2960*0Sstevel@tonic-gate mutex_exit(&cpu_lock); 2961*0Sstevel@tonic-gate } 2962*0Sstevel@tonic-gate pool_unlock(); 2963*0Sstevel@tonic-gate 2964*0Sstevel@tonic-gate /* 2965*0Sstevel@tonic-gate * ZSD shutdown callbacks can be executed multiple times, hence 2966*0Sstevel@tonic-gate * it is safe to not be holding any locks across this call. 2967*0Sstevel@tonic-gate */ 2968*0Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_SHUTDOWN); 2969*0Sstevel@tonic-gate 2970*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 2971*0Sstevel@tonic-gate if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN) 2972*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DOWN); 2973*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 2974*0Sstevel@tonic-gate 2975*0Sstevel@tonic-gate /* 2976*0Sstevel@tonic-gate * Wait for kernel threads to drain. 2977*0Sstevel@tonic-gate */ 2978*0Sstevel@tonic-gate if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) { 2979*0Sstevel@tonic-gate zone_rele(zone); 2980*0Sstevel@tonic-gate return (set_errno(EINTR)); 2981*0Sstevel@tonic-gate } 2982*0Sstevel@tonic-gate zone_rele(zone); 2983*0Sstevel@tonic-gate return (0); 2984*0Sstevel@tonic-gate } 2985*0Sstevel@tonic-gate 2986*0Sstevel@tonic-gate /* 2987*0Sstevel@tonic-gate * Systemcall entry point to finalize the zone halt process. The caller 2988*0Sstevel@tonic-gate * must have already successfully callefd zone_shutdown(). 2989*0Sstevel@tonic-gate * 2990*0Sstevel@tonic-gate * Upon successful completion, the zone will have been fully destroyed: 2991*0Sstevel@tonic-gate * zsched will have exited, destructor callbacks executed, and the zone 2992*0Sstevel@tonic-gate * removed from the list of active zones. 2993*0Sstevel@tonic-gate */ 2994*0Sstevel@tonic-gate static int 2995*0Sstevel@tonic-gate zone_destroy(zoneid_t zoneid) 2996*0Sstevel@tonic-gate { 2997*0Sstevel@tonic-gate uint64_t uniqid; 2998*0Sstevel@tonic-gate zone_t *zone; 2999*0Sstevel@tonic-gate zone_status_t status; 3000*0Sstevel@tonic-gate 3001*0Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 3002*0Sstevel@tonic-gate return (set_errno(EPERM)); 3003*0Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 3004*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3005*0Sstevel@tonic-gate 3006*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3007*0Sstevel@tonic-gate /* 3008*0Sstevel@tonic-gate * Look for zone under hash lock to prevent races with other 3009*0Sstevel@tonic-gate * calls to zone_destroy. 3010*0Sstevel@tonic-gate */ 3011*0Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 3012*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3013*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3014*0Sstevel@tonic-gate } 3015*0Sstevel@tonic-gate 3016*0Sstevel@tonic-gate if (zone_mount_count(zone->zone_rootpath) != 0) { 3017*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3018*0Sstevel@tonic-gate return (set_errno(EBUSY)); 3019*0Sstevel@tonic-gate } 3020*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 3021*0Sstevel@tonic-gate status = zone_status_get(zone); 3022*0Sstevel@tonic-gate if (status < ZONE_IS_DOWN) { 3023*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 3024*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3025*0Sstevel@tonic-gate return (set_errno(EBUSY)); 3026*0Sstevel@tonic-gate } else if (status == ZONE_IS_DOWN) { 3027*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */ 3028*0Sstevel@tonic-gate } 3029*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 3030*0Sstevel@tonic-gate zone_hold(zone); 3031*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3032*0Sstevel@tonic-gate 3033*0Sstevel@tonic-gate /* 3034*0Sstevel@tonic-gate * wait for zsched to exit 3035*0Sstevel@tonic-gate */ 3036*0Sstevel@tonic-gate zone_status_wait(zone, ZONE_IS_DEAD); 3037*0Sstevel@tonic-gate zone_zsd_callbacks(zone, ZSD_DESTROY); 3038*0Sstevel@tonic-gate uniqid = zone->zone_uniqid; 3039*0Sstevel@tonic-gate zone_rele(zone); 3040*0Sstevel@tonic-gate zone = NULL; /* potentially free'd */ 3041*0Sstevel@tonic-gate 3042*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3043*0Sstevel@tonic-gate for (; /* ever */; ) { 3044*0Sstevel@tonic-gate boolean_t unref; 3045*0Sstevel@tonic-gate 3046*0Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL || 3047*0Sstevel@tonic-gate zone->zone_uniqid != uniqid) { 3048*0Sstevel@tonic-gate /* 3049*0Sstevel@tonic-gate * The zone has gone away. Necessary conditions 3050*0Sstevel@tonic-gate * are met, so we return success. 3051*0Sstevel@tonic-gate */ 3052*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3053*0Sstevel@tonic-gate return (0); 3054*0Sstevel@tonic-gate } 3055*0Sstevel@tonic-gate mutex_enter(&zone->zone_lock); 3056*0Sstevel@tonic-gate unref = ZONE_IS_UNREF(zone); 3057*0Sstevel@tonic-gate mutex_exit(&zone->zone_lock); 3058*0Sstevel@tonic-gate if (unref) { 3059*0Sstevel@tonic-gate /* 3060*0Sstevel@tonic-gate * There is only one reference to the zone -- that 3061*0Sstevel@tonic-gate * added when the zone was added to the hashtables -- 3062*0Sstevel@tonic-gate * and things will remain this way until we drop 3063*0Sstevel@tonic-gate * zonehash_lock... we can go ahead and cleanup the 3064*0Sstevel@tonic-gate * zone. 3065*0Sstevel@tonic-gate */ 3066*0Sstevel@tonic-gate break; 3067*0Sstevel@tonic-gate } 3068*0Sstevel@tonic-gate 3069*0Sstevel@tonic-gate if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) { 3070*0Sstevel@tonic-gate /* Signaled */ 3071*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3072*0Sstevel@tonic-gate return (set_errno(EINTR)); 3073*0Sstevel@tonic-gate } 3074*0Sstevel@tonic-gate 3075*0Sstevel@tonic-gate } 3076*0Sstevel@tonic-gate 3077*0Sstevel@tonic-gate /* 3078*0Sstevel@tonic-gate * It is now safe to let the zone be recreated; remove it from the 3079*0Sstevel@tonic-gate * lists. The memory will not be freed until the last cred 3080*0Sstevel@tonic-gate * reference goes away. 3081*0Sstevel@tonic-gate */ 3082*0Sstevel@tonic-gate ASSERT(zonecount > 1); /* must be > 1; can't destroy global zone */ 3083*0Sstevel@tonic-gate zonecount--; 3084*0Sstevel@tonic-gate /* remove from active list and hash tables */ 3085*0Sstevel@tonic-gate list_remove(&zone_active, zone); 3086*0Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyname, 3087*0Sstevel@tonic-gate (mod_hash_key_t)zone->zone_name); 3088*0Sstevel@tonic-gate (void) mod_hash_destroy(zonehashbyid, 3089*0Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)zone->zone_id); 3090*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3091*0Sstevel@tonic-gate 3092*0Sstevel@tonic-gate /* add to deathrow list */ 3093*0Sstevel@tonic-gate mutex_enter(&zone_deathrow_lock); 3094*0Sstevel@tonic-gate list_insert_tail(&zone_deathrow, zone); 3095*0Sstevel@tonic-gate mutex_exit(&zone_deathrow_lock); 3096*0Sstevel@tonic-gate 3097*0Sstevel@tonic-gate /* 3098*0Sstevel@tonic-gate * Drop last reference (which was added by zsched()), this will 3099*0Sstevel@tonic-gate * free the zone unless there are outstanding cred references. 3100*0Sstevel@tonic-gate */ 3101*0Sstevel@tonic-gate zone_rele(zone); 3102*0Sstevel@tonic-gate return (0); 3103*0Sstevel@tonic-gate } 3104*0Sstevel@tonic-gate 3105*0Sstevel@tonic-gate /* 3106*0Sstevel@tonic-gate * Systemcall entry point for zone_getattr(2). 3107*0Sstevel@tonic-gate */ 3108*0Sstevel@tonic-gate static ssize_t 3109*0Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize) 3110*0Sstevel@tonic-gate { 3111*0Sstevel@tonic-gate size_t size; 3112*0Sstevel@tonic-gate int error = 0, err; 3113*0Sstevel@tonic-gate zone_t *zone; 3114*0Sstevel@tonic-gate char *zonepath; 3115*0Sstevel@tonic-gate zone_status_t zone_status; 3116*0Sstevel@tonic-gate pid_t initpid; 3117*0Sstevel@tonic-gate boolean_t global = (curproc->p_zone == global_zone); 3118*0Sstevel@tonic-gate 3119*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3120*0Sstevel@tonic-gate if ((zone = zone_find_all_by_id(zoneid)) == NULL) { 3121*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3122*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3123*0Sstevel@tonic-gate } 3124*0Sstevel@tonic-gate zone_status = zone_status_get(zone); 3125*0Sstevel@tonic-gate if (zone_status < ZONE_IS_READY) { 3126*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3127*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3128*0Sstevel@tonic-gate } 3129*0Sstevel@tonic-gate zone_hold(zone); 3130*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3131*0Sstevel@tonic-gate 3132*0Sstevel@tonic-gate /* 3133*0Sstevel@tonic-gate * If not in the global zone, don't show information about other zones. 3134*0Sstevel@tonic-gate */ 3135*0Sstevel@tonic-gate if (!global && curproc->p_zone != zone) { 3136*0Sstevel@tonic-gate zone_rele(zone); 3137*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3138*0Sstevel@tonic-gate } 3139*0Sstevel@tonic-gate 3140*0Sstevel@tonic-gate switch (attr) { 3141*0Sstevel@tonic-gate case ZONE_ATTR_ROOT: 3142*0Sstevel@tonic-gate if (global) { 3143*0Sstevel@tonic-gate /* 3144*0Sstevel@tonic-gate * Copy the path to trim the trailing "/" (except for 3145*0Sstevel@tonic-gate * the global zone). 3146*0Sstevel@tonic-gate */ 3147*0Sstevel@tonic-gate if (zone != global_zone) 3148*0Sstevel@tonic-gate size = zone->zone_rootpathlen - 1; 3149*0Sstevel@tonic-gate else 3150*0Sstevel@tonic-gate size = zone->zone_rootpathlen; 3151*0Sstevel@tonic-gate zonepath = kmem_alloc(size, KM_SLEEP); 3152*0Sstevel@tonic-gate bcopy(zone->zone_rootpath, zonepath, size); 3153*0Sstevel@tonic-gate zonepath[size - 1] = '\0'; 3154*0Sstevel@tonic-gate } else { 3155*0Sstevel@tonic-gate /* 3156*0Sstevel@tonic-gate * Caller is not in the global zone, just return 3157*0Sstevel@tonic-gate * faked-up path for current zone. 3158*0Sstevel@tonic-gate */ 3159*0Sstevel@tonic-gate zonepath = "/"; 3160*0Sstevel@tonic-gate size = 2; 3161*0Sstevel@tonic-gate } 3162*0Sstevel@tonic-gate if (bufsize > size) 3163*0Sstevel@tonic-gate bufsize = size; 3164*0Sstevel@tonic-gate if (buf != NULL) { 3165*0Sstevel@tonic-gate err = copyoutstr(zonepath, buf, bufsize, NULL); 3166*0Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 3167*0Sstevel@tonic-gate error = EFAULT; 3168*0Sstevel@tonic-gate } 3169*0Sstevel@tonic-gate if (global) 3170*0Sstevel@tonic-gate kmem_free(zonepath, size); 3171*0Sstevel@tonic-gate break; 3172*0Sstevel@tonic-gate 3173*0Sstevel@tonic-gate case ZONE_ATTR_NAME: 3174*0Sstevel@tonic-gate size = strlen(zone->zone_name) + 1; 3175*0Sstevel@tonic-gate if (bufsize > size) 3176*0Sstevel@tonic-gate bufsize = size; 3177*0Sstevel@tonic-gate if (buf != NULL) { 3178*0Sstevel@tonic-gate err = copyoutstr(zone->zone_name, buf, bufsize, NULL); 3179*0Sstevel@tonic-gate if (err != 0 && err != ENAMETOOLONG) 3180*0Sstevel@tonic-gate error = EFAULT; 3181*0Sstevel@tonic-gate } 3182*0Sstevel@tonic-gate break; 3183*0Sstevel@tonic-gate 3184*0Sstevel@tonic-gate case ZONE_ATTR_STATUS: 3185*0Sstevel@tonic-gate /* 3186*0Sstevel@tonic-gate * Since we're not holding zonehash_lock, the zone status 3187*0Sstevel@tonic-gate * may be anything; leave it up to userland to sort it out. 3188*0Sstevel@tonic-gate */ 3189*0Sstevel@tonic-gate size = sizeof (zone_status); 3190*0Sstevel@tonic-gate if (bufsize > size) 3191*0Sstevel@tonic-gate bufsize = size; 3192*0Sstevel@tonic-gate zone_status = zone_status_get(zone); 3193*0Sstevel@tonic-gate if (buf != NULL && 3194*0Sstevel@tonic-gate copyout(&zone_status, buf, bufsize) != 0) 3195*0Sstevel@tonic-gate error = EFAULT; 3196*0Sstevel@tonic-gate break; 3197*0Sstevel@tonic-gate case ZONE_ATTR_PRIVSET: 3198*0Sstevel@tonic-gate size = sizeof (priv_set_t); 3199*0Sstevel@tonic-gate if (bufsize > size) 3200*0Sstevel@tonic-gate bufsize = size; 3201*0Sstevel@tonic-gate if (buf != NULL && 3202*0Sstevel@tonic-gate copyout(zone->zone_privset, buf, bufsize) != 0) 3203*0Sstevel@tonic-gate error = EFAULT; 3204*0Sstevel@tonic-gate break; 3205*0Sstevel@tonic-gate case ZONE_ATTR_UNIQID: 3206*0Sstevel@tonic-gate size = sizeof (zone->zone_uniqid); 3207*0Sstevel@tonic-gate if (bufsize > size) 3208*0Sstevel@tonic-gate bufsize = size; 3209*0Sstevel@tonic-gate if (buf != NULL && 3210*0Sstevel@tonic-gate copyout(&zone->zone_uniqid, buf, bufsize) != 0) 3211*0Sstevel@tonic-gate error = EFAULT; 3212*0Sstevel@tonic-gate break; 3213*0Sstevel@tonic-gate case ZONE_ATTR_POOLID: 3214*0Sstevel@tonic-gate { 3215*0Sstevel@tonic-gate pool_t *pool; 3216*0Sstevel@tonic-gate poolid_t poolid; 3217*0Sstevel@tonic-gate 3218*0Sstevel@tonic-gate if (pool_lock_intr() != 0) { 3219*0Sstevel@tonic-gate error = EINTR; 3220*0Sstevel@tonic-gate break; 3221*0Sstevel@tonic-gate } 3222*0Sstevel@tonic-gate pool = zone_pool_get(zone); 3223*0Sstevel@tonic-gate poolid = pool->pool_id; 3224*0Sstevel@tonic-gate pool_unlock(); 3225*0Sstevel@tonic-gate size = sizeof (poolid); 3226*0Sstevel@tonic-gate if (bufsize > size) 3227*0Sstevel@tonic-gate bufsize = size; 3228*0Sstevel@tonic-gate if (buf != NULL && copyout(&poolid, buf, size) != 0) 3229*0Sstevel@tonic-gate error = EFAULT; 3230*0Sstevel@tonic-gate } 3231*0Sstevel@tonic-gate break; 3232*0Sstevel@tonic-gate case ZONE_ATTR_INITPID: 3233*0Sstevel@tonic-gate size = sizeof (initpid); 3234*0Sstevel@tonic-gate if (bufsize > size) 3235*0Sstevel@tonic-gate bufsize = size; 3236*0Sstevel@tonic-gate initpid = zone->zone_proc_initpid; 3237*0Sstevel@tonic-gate if (initpid == -1) { 3238*0Sstevel@tonic-gate error = ESRCH; 3239*0Sstevel@tonic-gate break; 3240*0Sstevel@tonic-gate } 3241*0Sstevel@tonic-gate if (buf != NULL && 3242*0Sstevel@tonic-gate copyout(&initpid, buf, bufsize) != 0) 3243*0Sstevel@tonic-gate error = EFAULT; 3244*0Sstevel@tonic-gate break; 3245*0Sstevel@tonic-gate default: 3246*0Sstevel@tonic-gate error = EINVAL; 3247*0Sstevel@tonic-gate } 3248*0Sstevel@tonic-gate zone_rele(zone); 3249*0Sstevel@tonic-gate 3250*0Sstevel@tonic-gate if (error) 3251*0Sstevel@tonic-gate return (set_errno(error)); 3252*0Sstevel@tonic-gate return ((ssize_t)size); 3253*0Sstevel@tonic-gate } 3254*0Sstevel@tonic-gate 3255*0Sstevel@tonic-gate /* 3256*0Sstevel@tonic-gate * Return zero if the process has at least one vnode mapped in to its 3257*0Sstevel@tonic-gate * address space which shouldn't be allowed to change zones. 3258*0Sstevel@tonic-gate */ 3259*0Sstevel@tonic-gate static int 3260*0Sstevel@tonic-gate as_can_change_zones(void) 3261*0Sstevel@tonic-gate { 3262*0Sstevel@tonic-gate proc_t *pp = curproc; 3263*0Sstevel@tonic-gate struct seg *seg; 3264*0Sstevel@tonic-gate struct as *as = pp->p_as; 3265*0Sstevel@tonic-gate vnode_t *vp; 3266*0Sstevel@tonic-gate int allow = 1; 3267*0Sstevel@tonic-gate 3268*0Sstevel@tonic-gate ASSERT(pp->p_as != &kas); 3269*0Sstevel@tonic-gate AS_LOCK_ENTER(&as, &as->a_lock, RW_READER); 3270*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 3271*0Sstevel@tonic-gate /* 3272*0Sstevel@tonic-gate * if we can't get a backing vnode for this segment then skip 3273*0Sstevel@tonic-gate * it. 3274*0Sstevel@tonic-gate */ 3275*0Sstevel@tonic-gate vp = NULL; 3276*0Sstevel@tonic-gate if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL) 3277*0Sstevel@tonic-gate continue; 3278*0Sstevel@tonic-gate if (!vn_can_change_zones(vp)) { /* bail on first match */ 3279*0Sstevel@tonic-gate allow = 0; 3280*0Sstevel@tonic-gate break; 3281*0Sstevel@tonic-gate } 3282*0Sstevel@tonic-gate } 3283*0Sstevel@tonic-gate AS_LOCK_EXIT(&as, &as->a_lock); 3284*0Sstevel@tonic-gate return (allow); 3285*0Sstevel@tonic-gate } 3286*0Sstevel@tonic-gate 3287*0Sstevel@tonic-gate /* 3288*0Sstevel@tonic-gate * Systemcall entry point for zone_enter(). 3289*0Sstevel@tonic-gate * 3290*0Sstevel@tonic-gate * The current process is injected into said zone. In the process 3291*0Sstevel@tonic-gate * it will change its project membership, privileges, rootdir/cwd, 3292*0Sstevel@tonic-gate * zone-wide rctls, and pool association to match those of the zone. 3293*0Sstevel@tonic-gate * 3294*0Sstevel@tonic-gate * The first zone_enter() called while the zone is in the ZONE_IS_READY 3295*0Sstevel@tonic-gate * state will transition it to ZONE_IS_RUNNING. Processes may only 3296*0Sstevel@tonic-gate * enter a zone that is "ready" or "running". 3297*0Sstevel@tonic-gate */ 3298*0Sstevel@tonic-gate static int 3299*0Sstevel@tonic-gate zone_enter(zoneid_t zoneid) 3300*0Sstevel@tonic-gate { 3301*0Sstevel@tonic-gate zone_t *zone; 3302*0Sstevel@tonic-gate vnode_t *vp; 3303*0Sstevel@tonic-gate proc_t *pp = curproc; 3304*0Sstevel@tonic-gate contract_t *ct; 3305*0Sstevel@tonic-gate cont_process_t *ctp; 3306*0Sstevel@tonic-gate task_t *tk, *oldtk; 3307*0Sstevel@tonic-gate kproject_t *zone_proj0; 3308*0Sstevel@tonic-gate cred_t *cr, *newcr; 3309*0Sstevel@tonic-gate pool_t *oldpool, *newpool; 3310*0Sstevel@tonic-gate sess_t *sp; 3311*0Sstevel@tonic-gate uid_t uid; 3312*0Sstevel@tonic-gate zone_status_t status; 3313*0Sstevel@tonic-gate int err = 0; 3314*0Sstevel@tonic-gate rctl_entity_p_t e; 3315*0Sstevel@tonic-gate 3316*0Sstevel@tonic-gate if (secpolicy_zone_config(CRED()) != 0) 3317*0Sstevel@tonic-gate return (set_errno(EPERM)); 3318*0Sstevel@tonic-gate if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID) 3319*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3320*0Sstevel@tonic-gate 3321*0Sstevel@tonic-gate /* 3322*0Sstevel@tonic-gate * Stop all lwps so we don't need to hold a lock to look at 3323*0Sstevel@tonic-gate * curproc->p_zone. This needs to happen before we grab any 3324*0Sstevel@tonic-gate * locks to avoid deadlock (another lwp in the process could 3325*0Sstevel@tonic-gate * be waiting for the held lock). 3326*0Sstevel@tonic-gate */ 3327*0Sstevel@tonic-gate if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) 3328*0Sstevel@tonic-gate return (set_errno(EINTR)); 3329*0Sstevel@tonic-gate 3330*0Sstevel@tonic-gate /* 3331*0Sstevel@tonic-gate * Make sure we're not changing zones with files open or mapped in 3332*0Sstevel@tonic-gate * to our address space which shouldn't be changing zones. 3333*0Sstevel@tonic-gate */ 3334*0Sstevel@tonic-gate if (!files_can_change_zones()) { 3335*0Sstevel@tonic-gate err = EBADF; 3336*0Sstevel@tonic-gate goto out; 3337*0Sstevel@tonic-gate } 3338*0Sstevel@tonic-gate if (!as_can_change_zones()) { 3339*0Sstevel@tonic-gate err = EFAULT; 3340*0Sstevel@tonic-gate goto out; 3341*0Sstevel@tonic-gate } 3342*0Sstevel@tonic-gate 3343*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3344*0Sstevel@tonic-gate if (pp->p_zone != global_zone) { 3345*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3346*0Sstevel@tonic-gate err = EINVAL; 3347*0Sstevel@tonic-gate goto out; 3348*0Sstevel@tonic-gate } 3349*0Sstevel@tonic-gate 3350*0Sstevel@tonic-gate zone = zone_find_all_by_id(zoneid); 3351*0Sstevel@tonic-gate if (zone == NULL) { 3352*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3353*0Sstevel@tonic-gate err = EINVAL; 3354*0Sstevel@tonic-gate goto out; 3355*0Sstevel@tonic-gate } 3356*0Sstevel@tonic-gate 3357*0Sstevel@tonic-gate /* 3358*0Sstevel@tonic-gate * To prevent processes in a zone from holding contracts on 3359*0Sstevel@tonic-gate * extrazonal resources, and to avoid process contract 3360*0Sstevel@tonic-gate * memberships which span zones, contract holders and processes 3361*0Sstevel@tonic-gate * which aren't the sole members of their encapsulating process 3362*0Sstevel@tonic-gate * contracts are not allowed to zone_enter. 3363*0Sstevel@tonic-gate */ 3364*0Sstevel@tonic-gate ctp = pp->p_ct_process; 3365*0Sstevel@tonic-gate ct = &ctp->conp_contract; 3366*0Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 3367*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 3368*0Sstevel@tonic-gate if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) { 3369*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3370*0Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3371*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3372*0Sstevel@tonic-gate pool_unlock(); 3373*0Sstevel@tonic-gate err = EINVAL; 3374*0Sstevel@tonic-gate goto out; 3375*0Sstevel@tonic-gate } 3376*0Sstevel@tonic-gate 3377*0Sstevel@tonic-gate /* 3378*0Sstevel@tonic-gate * Moreover, we don't allow processes whose encapsulating 3379*0Sstevel@tonic-gate * process contracts have inherited extrazonal contracts. 3380*0Sstevel@tonic-gate * While it would be easier to eliminate all process contracts 3381*0Sstevel@tonic-gate * with inherited contracts, we need to be able to give a 3382*0Sstevel@tonic-gate * restarted init (or other zone-penetrating process) its 3383*0Sstevel@tonic-gate * predecessor's contracts. 3384*0Sstevel@tonic-gate */ 3385*0Sstevel@tonic-gate if (ctp->conp_ninherited != 0) { 3386*0Sstevel@tonic-gate contract_t *next; 3387*0Sstevel@tonic-gate for (next = list_head(&ctp->conp_inherited); next; 3388*0Sstevel@tonic-gate next = list_next(&ctp->conp_inherited, next)) { 3389*0Sstevel@tonic-gate if (contract_getzuniqid(next) != zone->zone_uniqid) { 3390*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3391*0Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3392*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3393*0Sstevel@tonic-gate pool_unlock(); 3394*0Sstevel@tonic-gate err = EINVAL; 3395*0Sstevel@tonic-gate goto out; 3396*0Sstevel@tonic-gate } 3397*0Sstevel@tonic-gate } 3398*0Sstevel@tonic-gate } 3399*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3400*0Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3401*0Sstevel@tonic-gate 3402*0Sstevel@tonic-gate status = zone_status_get(zone); 3403*0Sstevel@tonic-gate if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) { 3404*0Sstevel@tonic-gate /* 3405*0Sstevel@tonic-gate * Can't join 3406*0Sstevel@tonic-gate */ 3407*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3408*0Sstevel@tonic-gate err = EINVAL; 3409*0Sstevel@tonic-gate goto out; 3410*0Sstevel@tonic-gate } 3411*0Sstevel@tonic-gate 3412*0Sstevel@tonic-gate /* 3413*0Sstevel@tonic-gate * Make sure new priv set is within the permitted set for caller 3414*0Sstevel@tonic-gate */ 3415*0Sstevel@tonic-gate if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) { 3416*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3417*0Sstevel@tonic-gate err = EPERM; 3418*0Sstevel@tonic-gate goto out; 3419*0Sstevel@tonic-gate } 3420*0Sstevel@tonic-gate /* 3421*0Sstevel@tonic-gate * We want to momentarily drop zonehash_lock while we optimistically 3422*0Sstevel@tonic-gate * bind curproc to the pool it should be running in. This is safe 3423*0Sstevel@tonic-gate * since the zone can't disappear (we have a hold on it). 3424*0Sstevel@tonic-gate */ 3425*0Sstevel@tonic-gate zone_hold(zone); 3426*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3427*0Sstevel@tonic-gate 3428*0Sstevel@tonic-gate /* 3429*0Sstevel@tonic-gate * Grab pool_lock to keep the pools configuration from changing 3430*0Sstevel@tonic-gate * and to stop ourselves from getting rebound to another pool 3431*0Sstevel@tonic-gate * until we join the zone. 3432*0Sstevel@tonic-gate */ 3433*0Sstevel@tonic-gate if (pool_lock_intr() != 0) { 3434*0Sstevel@tonic-gate zone_rele(zone); 3435*0Sstevel@tonic-gate err = EINTR; 3436*0Sstevel@tonic-gate goto out; 3437*0Sstevel@tonic-gate } 3438*0Sstevel@tonic-gate ASSERT(secpolicy_pool(CRED()) == 0); 3439*0Sstevel@tonic-gate /* 3440*0Sstevel@tonic-gate * Bind ourselves to the pool currently associated with the zone. 3441*0Sstevel@tonic-gate */ 3442*0Sstevel@tonic-gate oldpool = curproc->p_pool; 3443*0Sstevel@tonic-gate newpool = zone_pool_get(zone); 3444*0Sstevel@tonic-gate if (pool_state == POOL_ENABLED && newpool != oldpool && 3445*0Sstevel@tonic-gate (err = pool_do_bind(newpool, P_PID, P_MYID, 3446*0Sstevel@tonic-gate POOL_BIND_ALL)) != 0) { 3447*0Sstevel@tonic-gate pool_unlock(); 3448*0Sstevel@tonic-gate zone_rele(zone); 3449*0Sstevel@tonic-gate goto out; 3450*0Sstevel@tonic-gate } 3451*0Sstevel@tonic-gate 3452*0Sstevel@tonic-gate /* 3453*0Sstevel@tonic-gate * Grab cpu_lock now; we'll need it later when we call 3454*0Sstevel@tonic-gate * task_join(). 3455*0Sstevel@tonic-gate */ 3456*0Sstevel@tonic-gate mutex_enter(&cpu_lock); 3457*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3458*0Sstevel@tonic-gate /* 3459*0Sstevel@tonic-gate * Make sure the zone hasn't moved on since we dropped zonehash_lock. 3460*0Sstevel@tonic-gate */ 3461*0Sstevel@tonic-gate if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 3462*0Sstevel@tonic-gate /* 3463*0Sstevel@tonic-gate * Can't join anymore. 3464*0Sstevel@tonic-gate */ 3465*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3466*0Sstevel@tonic-gate mutex_exit(&cpu_lock); 3467*0Sstevel@tonic-gate if (pool_state == POOL_ENABLED && 3468*0Sstevel@tonic-gate newpool != oldpool) 3469*0Sstevel@tonic-gate (void) pool_do_bind(oldpool, P_PID, P_MYID, 3470*0Sstevel@tonic-gate POOL_BIND_ALL); 3471*0Sstevel@tonic-gate pool_unlock(); 3472*0Sstevel@tonic-gate zone_rele(zone); 3473*0Sstevel@tonic-gate err = EINVAL; 3474*0Sstevel@tonic-gate goto out; 3475*0Sstevel@tonic-gate } 3476*0Sstevel@tonic-gate 3477*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 3478*0Sstevel@tonic-gate zone_proj0 = zone->zone_zsched->p_task->tk_proj; 3479*0Sstevel@tonic-gate /* verify that we do not exceed and task or lwp limits */ 3480*0Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 3481*0Sstevel@tonic-gate /* add new lwps to zone and zone's proj0 */ 3482*0Sstevel@tonic-gate zone_proj0->kpj_nlwps += pp->p_lwpcnt; 3483*0Sstevel@tonic-gate zone->zone_nlwps += pp->p_lwpcnt; 3484*0Sstevel@tonic-gate /* add 1 task to zone's proj0 */ 3485*0Sstevel@tonic-gate zone_proj0->kpj_ntasks += 1; 3486*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3487*0Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 3488*0Sstevel@tonic-gate 3489*0Sstevel@tonic-gate /* remove lwps from proc's old zone and old project */ 3490*0Sstevel@tonic-gate mutex_enter(&pp->p_zone->zone_nlwps_lock); 3491*0Sstevel@tonic-gate pp->p_zone->zone_nlwps -= pp->p_lwpcnt; 3492*0Sstevel@tonic-gate pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt; 3493*0Sstevel@tonic-gate mutex_exit(&pp->p_zone->zone_nlwps_lock); 3494*0Sstevel@tonic-gate 3495*0Sstevel@tonic-gate /* 3496*0Sstevel@tonic-gate * Joining the zone cannot fail from now on. 3497*0Sstevel@tonic-gate * 3498*0Sstevel@tonic-gate * This means that a lot of the following code can be commonized and 3499*0Sstevel@tonic-gate * shared with zsched(). 3500*0Sstevel@tonic-gate */ 3501*0Sstevel@tonic-gate 3502*0Sstevel@tonic-gate /* 3503*0Sstevel@tonic-gate * Reset the encapsulating process contract's zone. 3504*0Sstevel@tonic-gate */ 3505*0Sstevel@tonic-gate ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID); 3506*0Sstevel@tonic-gate contract_setzuniqid(ct, zone->zone_uniqid); 3507*0Sstevel@tonic-gate 3508*0Sstevel@tonic-gate /* 3509*0Sstevel@tonic-gate * Create a new task and associate the process with the project keyed 3510*0Sstevel@tonic-gate * by (projid,zoneid). 3511*0Sstevel@tonic-gate * 3512*0Sstevel@tonic-gate * We might as well be in project 0; the global zone's projid doesn't 3513*0Sstevel@tonic-gate * make much sense in a zone anyhow. 3514*0Sstevel@tonic-gate * 3515*0Sstevel@tonic-gate * This also increments zone_ntasks, and returns with p_lock held. 3516*0Sstevel@tonic-gate */ 3517*0Sstevel@tonic-gate tk = task_create(0, zone); 3518*0Sstevel@tonic-gate oldtk = task_join(tk, 0); 3519*0Sstevel@tonic-gate mutex_exit(&cpu_lock); 3520*0Sstevel@tonic-gate 3521*0Sstevel@tonic-gate pp->p_flag |= SZONETOP; 3522*0Sstevel@tonic-gate pp->p_zone = zone; 3523*0Sstevel@tonic-gate 3524*0Sstevel@tonic-gate /* 3525*0Sstevel@tonic-gate * call RCTLOP_SET functions on this proc 3526*0Sstevel@tonic-gate */ 3527*0Sstevel@tonic-gate e.rcep_p.zone = zone; 3528*0Sstevel@tonic-gate e.rcep_t = RCENTITY_ZONE; 3529*0Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL, 3530*0Sstevel@tonic-gate RCD_CALLBACK); 3531*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3532*0Sstevel@tonic-gate 3533*0Sstevel@tonic-gate /* 3534*0Sstevel@tonic-gate * We don't need to hold any of zsched's locks here; not only do we know 3535*0Sstevel@tonic-gate * the process and zone aren't going away, we know its session isn't 3536*0Sstevel@tonic-gate * changing either. 3537*0Sstevel@tonic-gate * 3538*0Sstevel@tonic-gate * By joining zsched's session here, we mimic the behavior in the 3539*0Sstevel@tonic-gate * global zone of init's sid being the pid of sched. We extend this 3540*0Sstevel@tonic-gate * to all zlogin-like zone_enter()'ing processes as well. 3541*0Sstevel@tonic-gate */ 3542*0Sstevel@tonic-gate mutex_enter(&pidlock); 3543*0Sstevel@tonic-gate sp = zone->zone_zsched->p_sessp; 3544*0Sstevel@tonic-gate SESS_HOLD(sp); 3545*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 3546*0Sstevel@tonic-gate pgexit(pp); 3547*0Sstevel@tonic-gate SESS_RELE(pp->p_sessp); 3548*0Sstevel@tonic-gate pp->p_sessp = sp; 3549*0Sstevel@tonic-gate pgjoin(pp, zone->zone_zsched->p_pidp); 3550*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3551*0Sstevel@tonic-gate mutex_exit(&pidlock); 3552*0Sstevel@tonic-gate 3553*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3554*0Sstevel@tonic-gate /* 3555*0Sstevel@tonic-gate * We're firmly in the zone; let pools progress. 3556*0Sstevel@tonic-gate */ 3557*0Sstevel@tonic-gate pool_unlock(); 3558*0Sstevel@tonic-gate task_rele(oldtk); 3559*0Sstevel@tonic-gate /* 3560*0Sstevel@tonic-gate * We don't need to retain a hold on the zone since we already 3561*0Sstevel@tonic-gate * incremented zone_ntasks, so the zone isn't going anywhere. 3562*0Sstevel@tonic-gate */ 3563*0Sstevel@tonic-gate zone_rele(zone); 3564*0Sstevel@tonic-gate 3565*0Sstevel@tonic-gate /* 3566*0Sstevel@tonic-gate * Chroot 3567*0Sstevel@tonic-gate */ 3568*0Sstevel@tonic-gate vp = zone->zone_rootvp; 3569*0Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_cdir, pp); 3570*0Sstevel@tonic-gate zone_chdir(vp, &PTOU(pp)->u_rdir, pp); 3571*0Sstevel@tonic-gate 3572*0Sstevel@tonic-gate /* 3573*0Sstevel@tonic-gate * Change process credentials 3574*0Sstevel@tonic-gate */ 3575*0Sstevel@tonic-gate newcr = cralloc(); 3576*0Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 3577*0Sstevel@tonic-gate cr = pp->p_cred; 3578*0Sstevel@tonic-gate crcopy_to(cr, newcr); 3579*0Sstevel@tonic-gate crsetzone(newcr, zone); 3580*0Sstevel@tonic-gate pp->p_cred = newcr; 3581*0Sstevel@tonic-gate 3582*0Sstevel@tonic-gate /* 3583*0Sstevel@tonic-gate * Restrict all process privilege sets to zone limit 3584*0Sstevel@tonic-gate */ 3585*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_PPRIV(newcr)); 3586*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_EPRIV(newcr)); 3587*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_IPRIV(newcr)); 3588*0Sstevel@tonic-gate priv_intersect(zone->zone_privset, &CR_LPRIV(newcr)); 3589*0Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 3590*0Sstevel@tonic-gate crset(pp, newcr); 3591*0Sstevel@tonic-gate 3592*0Sstevel@tonic-gate /* 3593*0Sstevel@tonic-gate * Adjust upcount to reflect zone entry. 3594*0Sstevel@tonic-gate */ 3595*0Sstevel@tonic-gate uid = crgetruid(newcr); 3596*0Sstevel@tonic-gate mutex_enter(&pidlock); 3597*0Sstevel@tonic-gate upcount_dec(uid, GLOBAL_ZONEID); 3598*0Sstevel@tonic-gate upcount_inc(uid, zoneid); 3599*0Sstevel@tonic-gate mutex_exit(&pidlock); 3600*0Sstevel@tonic-gate 3601*0Sstevel@tonic-gate /* 3602*0Sstevel@tonic-gate * Set up core file path and content. 3603*0Sstevel@tonic-gate */ 3604*0Sstevel@tonic-gate set_core_defaults(); 3605*0Sstevel@tonic-gate 3606*0Sstevel@tonic-gate out: 3607*0Sstevel@tonic-gate /* 3608*0Sstevel@tonic-gate * Let the other lwps continue. 3609*0Sstevel@tonic-gate */ 3610*0Sstevel@tonic-gate mutex_enter(&pp->p_lock); 3611*0Sstevel@tonic-gate if (curthread != pp->p_agenttp) 3612*0Sstevel@tonic-gate continuelwps(pp); 3613*0Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3614*0Sstevel@tonic-gate 3615*0Sstevel@tonic-gate return (err != 0 ? set_errno(err) : 0); 3616*0Sstevel@tonic-gate } 3617*0Sstevel@tonic-gate 3618*0Sstevel@tonic-gate /* 3619*0Sstevel@tonic-gate * Systemcall entry point for zone_list(2). 3620*0Sstevel@tonic-gate * 3621*0Sstevel@tonic-gate * Processes running in a (non-global) zone only see themselves. 3622*0Sstevel@tonic-gate */ 3623*0Sstevel@tonic-gate static int 3624*0Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones) 3625*0Sstevel@tonic-gate { 3626*0Sstevel@tonic-gate zoneid_t *zoneids; 3627*0Sstevel@tonic-gate zone_t *zone; 3628*0Sstevel@tonic-gate uint_t user_nzones, real_nzones; 3629*0Sstevel@tonic-gate int error = 0; 3630*0Sstevel@tonic-gate uint_t i; 3631*0Sstevel@tonic-gate 3632*0Sstevel@tonic-gate if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0) 3633*0Sstevel@tonic-gate return (set_errno(EFAULT)); 3634*0Sstevel@tonic-gate 3635*0Sstevel@tonic-gate if (curproc->p_zone != global_zone) { 3636*0Sstevel@tonic-gate /* just return current zone */ 3637*0Sstevel@tonic-gate real_nzones = 1; 3638*0Sstevel@tonic-gate zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP); 3639*0Sstevel@tonic-gate zoneids[0] = curproc->p_zone->zone_id; 3640*0Sstevel@tonic-gate } else { 3641*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3642*0Sstevel@tonic-gate real_nzones = zonecount; 3643*0Sstevel@tonic-gate if (real_nzones) { 3644*0Sstevel@tonic-gate zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), 3645*0Sstevel@tonic-gate KM_SLEEP); 3646*0Sstevel@tonic-gate i = 0; 3647*0Sstevel@tonic-gate for (zone = list_head(&zone_active); zone != NULL; 3648*0Sstevel@tonic-gate zone = list_next(&zone_active, zone)) 3649*0Sstevel@tonic-gate zoneids[i++] = zone->zone_id; 3650*0Sstevel@tonic-gate ASSERT(i == real_nzones); 3651*0Sstevel@tonic-gate } 3652*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3653*0Sstevel@tonic-gate } 3654*0Sstevel@tonic-gate 3655*0Sstevel@tonic-gate if (user_nzones > real_nzones) 3656*0Sstevel@tonic-gate user_nzones = real_nzones; 3657*0Sstevel@tonic-gate 3658*0Sstevel@tonic-gate if (copyout(&real_nzones, numzones, sizeof (uint_t)) != 0) 3659*0Sstevel@tonic-gate error = EFAULT; 3660*0Sstevel@tonic-gate else if (zoneidlist != NULL && user_nzones != 0) { 3661*0Sstevel@tonic-gate if (copyout(zoneids, zoneidlist, 3662*0Sstevel@tonic-gate user_nzones * sizeof (zoneid_t)) != 0) 3663*0Sstevel@tonic-gate error = EFAULT; 3664*0Sstevel@tonic-gate } 3665*0Sstevel@tonic-gate 3666*0Sstevel@tonic-gate if (real_nzones) 3667*0Sstevel@tonic-gate kmem_free(zoneids, real_nzones * sizeof (zoneid_t)); 3668*0Sstevel@tonic-gate 3669*0Sstevel@tonic-gate if (error) 3670*0Sstevel@tonic-gate return (set_errno(error)); 3671*0Sstevel@tonic-gate else 3672*0Sstevel@tonic-gate return (0); 3673*0Sstevel@tonic-gate } 3674*0Sstevel@tonic-gate 3675*0Sstevel@tonic-gate /* 3676*0Sstevel@tonic-gate * Systemcall entry point for zone_lookup(2). 3677*0Sstevel@tonic-gate * 3678*0Sstevel@tonic-gate * Non-global zones are only able to see themselves. 3679*0Sstevel@tonic-gate */ 3680*0Sstevel@tonic-gate static zoneid_t 3681*0Sstevel@tonic-gate zone_lookup(const char *zone_name) 3682*0Sstevel@tonic-gate { 3683*0Sstevel@tonic-gate char *kname; 3684*0Sstevel@tonic-gate zone_t *zone; 3685*0Sstevel@tonic-gate zoneid_t zoneid; 3686*0Sstevel@tonic-gate int err; 3687*0Sstevel@tonic-gate 3688*0Sstevel@tonic-gate if (zone_name == NULL) { 3689*0Sstevel@tonic-gate /* return caller's zone id */ 3690*0Sstevel@tonic-gate return (getzoneid()); 3691*0Sstevel@tonic-gate } 3692*0Sstevel@tonic-gate 3693*0Sstevel@tonic-gate kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP); 3694*0Sstevel@tonic-gate if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) { 3695*0Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 3696*0Sstevel@tonic-gate return (set_errno(err)); 3697*0Sstevel@tonic-gate } 3698*0Sstevel@tonic-gate 3699*0Sstevel@tonic-gate mutex_enter(&zonehash_lock); 3700*0Sstevel@tonic-gate zone = zone_find_all_by_name(kname); 3701*0Sstevel@tonic-gate kmem_free(kname, ZONENAME_MAX); 3702*0Sstevel@tonic-gate if (zone == NULL || zone_status_get(zone) < ZONE_IS_READY || 3703*0Sstevel@tonic-gate (curproc->p_zone != global_zone && curproc->p_zone != zone)) { 3704*0Sstevel@tonic-gate /* in non-global zone, can only lookup own name */ 3705*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3706*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3707*0Sstevel@tonic-gate } 3708*0Sstevel@tonic-gate zoneid = zone->zone_id; 3709*0Sstevel@tonic-gate mutex_exit(&zonehash_lock); 3710*0Sstevel@tonic-gate return (zoneid); 3711*0Sstevel@tonic-gate } 3712*0Sstevel@tonic-gate 3713*0Sstevel@tonic-gate /* ARGSUSED */ 3714*0Sstevel@tonic-gate long 3715*0Sstevel@tonic-gate zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5) 3716*0Sstevel@tonic-gate { 3717*0Sstevel@tonic-gate zone_def zs; 3718*0Sstevel@tonic-gate 3719*0Sstevel@tonic-gate switch (cmd) { 3720*0Sstevel@tonic-gate case ZONE_CREATE: 3721*0Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 3722*0Sstevel@tonic-gate if (copyin(arg1, &zs, sizeof (zone_def))) { 3723*0Sstevel@tonic-gate return (set_errno(EFAULT)); 3724*0Sstevel@tonic-gate } 3725*0Sstevel@tonic-gate } else { 3726*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 3727*0Sstevel@tonic-gate zone_def32 zs32; 3728*0Sstevel@tonic-gate 3729*0Sstevel@tonic-gate if (copyin(arg1, &zs32, sizeof (zone_def32))) { 3730*0Sstevel@tonic-gate return (set_errno(EFAULT)); 3731*0Sstevel@tonic-gate } 3732*0Sstevel@tonic-gate zs.zone_name = 3733*0Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_name; 3734*0Sstevel@tonic-gate zs.zone_root = 3735*0Sstevel@tonic-gate (const char *)(unsigned long)zs32.zone_root; 3736*0Sstevel@tonic-gate zs.zone_privs = 3737*0Sstevel@tonic-gate (const struct priv_set *) 3738*0Sstevel@tonic-gate (unsigned long)zs32.zone_privs; 3739*0Sstevel@tonic-gate zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf; 3740*0Sstevel@tonic-gate zs.rctlbufsz = zs32.rctlbufsz; 3741*0Sstevel@tonic-gate zs.extended_error = 3742*0Sstevel@tonic-gate (int *)(unsigned long)zs32.extended_error; 3743*0Sstevel@tonic-gate #else 3744*0Sstevel@tonic-gate panic("get_udatamodel() returned bogus result\n"); 3745*0Sstevel@tonic-gate #endif 3746*0Sstevel@tonic-gate } 3747*0Sstevel@tonic-gate 3748*0Sstevel@tonic-gate return (zone_create(zs.zone_name, zs.zone_root, 3749*0Sstevel@tonic-gate zs.zone_privs, (caddr_t)zs.rctlbuf, zs.rctlbufsz, 3750*0Sstevel@tonic-gate zs.extended_error)); 3751*0Sstevel@tonic-gate case ZONE_BOOT: 3752*0Sstevel@tonic-gate return (zone_boot((zoneid_t)(uintptr_t)arg1, 3753*0Sstevel@tonic-gate (const char *)arg2)); 3754*0Sstevel@tonic-gate case ZONE_DESTROY: 3755*0Sstevel@tonic-gate return (zone_destroy((zoneid_t)(uintptr_t)arg1)); 3756*0Sstevel@tonic-gate case ZONE_GETATTR: 3757*0Sstevel@tonic-gate return (zone_getattr((zoneid_t)(uintptr_t)arg1, 3758*0Sstevel@tonic-gate (int)(uintptr_t)arg2, arg3, (size_t)arg4)); 3759*0Sstevel@tonic-gate case ZONE_ENTER: 3760*0Sstevel@tonic-gate return (zone_enter((zoneid_t)(uintptr_t)arg1)); 3761*0Sstevel@tonic-gate case ZONE_LIST: 3762*0Sstevel@tonic-gate return (zone_list((zoneid_t *)arg1, (uint_t *)arg2)); 3763*0Sstevel@tonic-gate case ZONE_SHUTDOWN: 3764*0Sstevel@tonic-gate return (zone_shutdown((zoneid_t)(uintptr_t)arg1)); 3765*0Sstevel@tonic-gate case ZONE_LOOKUP: 3766*0Sstevel@tonic-gate return (zone_lookup((const char *)arg1)); 3767*0Sstevel@tonic-gate default: 3768*0Sstevel@tonic-gate return (set_errno(EINVAL)); 3769*0Sstevel@tonic-gate } 3770*0Sstevel@tonic-gate } 3771*0Sstevel@tonic-gate 3772*0Sstevel@tonic-gate struct zarg { 3773*0Sstevel@tonic-gate zone_t *zone; 3774*0Sstevel@tonic-gate zone_cmd_arg_t arg; 3775*0Sstevel@tonic-gate }; 3776*0Sstevel@tonic-gate 3777*0Sstevel@tonic-gate static int 3778*0Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp) 3779*0Sstevel@tonic-gate { 3780*0Sstevel@tonic-gate char *buf; 3781*0Sstevel@tonic-gate size_t buflen; 3782*0Sstevel@tonic-gate int error; 3783*0Sstevel@tonic-gate 3784*0Sstevel@tonic-gate buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name); 3785*0Sstevel@tonic-gate buf = kmem_alloc(buflen, KM_SLEEP); 3786*0Sstevel@tonic-gate (void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name); 3787*0Sstevel@tonic-gate error = door_ki_open(buf, doorp); 3788*0Sstevel@tonic-gate kmem_free(buf, buflen); 3789*0Sstevel@tonic-gate return (error); 3790*0Sstevel@tonic-gate } 3791*0Sstevel@tonic-gate 3792*0Sstevel@tonic-gate static void 3793*0Sstevel@tonic-gate zone_release_door(door_handle_t *doorp) 3794*0Sstevel@tonic-gate { 3795*0Sstevel@tonic-gate door_ki_rele(*doorp); 3796*0Sstevel@tonic-gate *doorp = NULL; 3797*0Sstevel@tonic-gate } 3798*0Sstevel@tonic-gate 3799*0Sstevel@tonic-gate static void 3800*0Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp) 3801*0Sstevel@tonic-gate { 3802*0Sstevel@tonic-gate door_handle_t door = NULL; 3803*0Sstevel@tonic-gate door_arg_t darg, save_arg; 3804*0Sstevel@tonic-gate char *zone_name; 3805*0Sstevel@tonic-gate size_t zone_namelen; 3806*0Sstevel@tonic-gate zoneid_t zoneid; 3807*0Sstevel@tonic-gate zone_t *zone; 3808*0Sstevel@tonic-gate zone_cmd_arg_t arg; 3809*0Sstevel@tonic-gate uint64_t uniqid; 3810*0Sstevel@tonic-gate size_t size; 3811*0Sstevel@tonic-gate int error; 3812*0Sstevel@tonic-gate int retry; 3813*0Sstevel@tonic-gate 3814*0Sstevel@tonic-gate zone = zargp->zone; 3815*0Sstevel@tonic-gate arg = zargp->arg; 3816*0Sstevel@tonic-gate kmem_free(zargp, sizeof (*zargp)); 3817*0Sstevel@tonic-gate 3818*0Sstevel@tonic-gate zone_namelen = strlen(zone->zone_name) + 1; 3819*0Sstevel@tonic-gate zone_name = kmem_alloc(zone_namelen, KM_SLEEP); 3820*0Sstevel@tonic-gate bcopy(zone->zone_name, zone_name, zone_namelen); 3821*0Sstevel@tonic-gate zoneid = zone->zone_id; 3822*0Sstevel@tonic-gate uniqid = zone->zone_uniqid; 3823*0Sstevel@tonic-gate /* 3824*0Sstevel@tonic-gate * zoneadmd may be down, but at least we can empty out the zone. 3825*0Sstevel@tonic-gate * We can ignore the return value of zone_empty() since we're called 3826*0Sstevel@tonic-gate * from a kernel thread and know we won't be delivered any signals. 3827*0Sstevel@tonic-gate */ 3828*0Sstevel@tonic-gate ASSERT(curproc == &p0); 3829*0Sstevel@tonic-gate (void) zone_empty(zone); 3830*0Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY); 3831*0Sstevel@tonic-gate zone_rele(zone); 3832*0Sstevel@tonic-gate 3833*0Sstevel@tonic-gate size = sizeof (arg); 3834*0Sstevel@tonic-gate darg.rbuf = (char *)&arg; 3835*0Sstevel@tonic-gate darg.data_ptr = (char *)&arg; 3836*0Sstevel@tonic-gate darg.rsize = size; 3837*0Sstevel@tonic-gate darg.data_size = size; 3838*0Sstevel@tonic-gate darg.desc_ptr = NULL; 3839*0Sstevel@tonic-gate darg.desc_num = 0; 3840*0Sstevel@tonic-gate 3841*0Sstevel@tonic-gate save_arg = darg; 3842*0Sstevel@tonic-gate /* 3843*0Sstevel@tonic-gate * Since we're not holding a reference to the zone, any number of 3844*0Sstevel@tonic-gate * things can go wrong, including the zone disappearing before we get a 3845*0Sstevel@tonic-gate * chance to talk to zoneadmd. 3846*0Sstevel@tonic-gate */ 3847*0Sstevel@tonic-gate for (retry = 0; /* forever */; retry++) { 3848*0Sstevel@tonic-gate if (door == NULL && 3849*0Sstevel@tonic-gate (error = zone_lookup_door(zone_name, &door)) != 0) { 3850*0Sstevel@tonic-gate goto next; 3851*0Sstevel@tonic-gate } 3852*0Sstevel@tonic-gate ASSERT(door != NULL); 3853*0Sstevel@tonic-gate 3854*0Sstevel@tonic-gate if ((error = door_ki_upcall(door, &darg)) == 0) { 3855*0Sstevel@tonic-gate break; 3856*0Sstevel@tonic-gate } 3857*0Sstevel@tonic-gate switch (error) { 3858*0Sstevel@tonic-gate case EINTR: 3859*0Sstevel@tonic-gate /* FALLTHROUGH */ 3860*0Sstevel@tonic-gate case EAGAIN: /* process may be forking */ 3861*0Sstevel@tonic-gate /* 3862*0Sstevel@tonic-gate * Back off for a bit 3863*0Sstevel@tonic-gate */ 3864*0Sstevel@tonic-gate break; 3865*0Sstevel@tonic-gate case EBADF: 3866*0Sstevel@tonic-gate zone_release_door(&door); 3867*0Sstevel@tonic-gate if (zone_lookup_door(zone_name, &door) != 0) { 3868*0Sstevel@tonic-gate /* 3869*0Sstevel@tonic-gate * zoneadmd may be dead, but it may come back to 3870*0Sstevel@tonic-gate * life later. 3871*0Sstevel@tonic-gate */ 3872*0Sstevel@tonic-gate break; 3873*0Sstevel@tonic-gate } 3874*0Sstevel@tonic-gate break; 3875*0Sstevel@tonic-gate default: 3876*0Sstevel@tonic-gate cmn_err(CE_WARN, 3877*0Sstevel@tonic-gate "zone_ki_call_zoneadmd: door_ki_upcall error %d\n", 3878*0Sstevel@tonic-gate error); 3879*0Sstevel@tonic-gate goto out; 3880*0Sstevel@tonic-gate } 3881*0Sstevel@tonic-gate next: 3882*0Sstevel@tonic-gate /* 3883*0Sstevel@tonic-gate * If this isn't the same zone_t that we originally had in mind, 3884*0Sstevel@tonic-gate * then this is the same as if two kadmin requests come in at 3885*0Sstevel@tonic-gate * the same time: the first one wins. This means we lose, so we 3886*0Sstevel@tonic-gate * bail. 3887*0Sstevel@tonic-gate */ 3888*0Sstevel@tonic-gate if ((zone = zone_find_by_id(zoneid)) == NULL) { 3889*0Sstevel@tonic-gate /* 3890*0Sstevel@tonic-gate * Problem is solved. 3891*0Sstevel@tonic-gate */ 3892*0Sstevel@tonic-gate break; 3893*0Sstevel@tonic-gate } 3894*0Sstevel@tonic-gate if (zone->zone_uniqid != uniqid) { 3895*0Sstevel@tonic-gate /* 3896*0Sstevel@tonic-gate * zoneid recycled 3897*0Sstevel@tonic-gate */ 3898*0Sstevel@tonic-gate zone_rele(zone); 3899*0Sstevel@tonic-gate break; 3900*0Sstevel@tonic-gate } 3901*0Sstevel@tonic-gate /* 3902*0Sstevel@tonic-gate * We could zone_status_timedwait(), but there doesn't seem to 3903*0Sstevel@tonic-gate * be much point in doing that (plus, it would mean that 3904*0Sstevel@tonic-gate * zone_free() isn't called until this thread exits). 3905*0Sstevel@tonic-gate */ 3906*0Sstevel@tonic-gate zone_rele(zone); 3907*0Sstevel@tonic-gate delay(hz); 3908*0Sstevel@tonic-gate darg = save_arg; 3909*0Sstevel@tonic-gate } 3910*0Sstevel@tonic-gate out: 3911*0Sstevel@tonic-gate if (door != NULL) { 3912*0Sstevel@tonic-gate zone_release_door(&door); 3913*0Sstevel@tonic-gate } 3914*0Sstevel@tonic-gate kmem_free(zone_name, zone_namelen); 3915*0Sstevel@tonic-gate thread_exit(); 3916*0Sstevel@tonic-gate } 3917*0Sstevel@tonic-gate 3918*0Sstevel@tonic-gate /* 3919*0Sstevel@tonic-gate * Entry point for uadmin() to tell the zone to go away or reboot. The caller 3920*0Sstevel@tonic-gate * is a process in the zone to be modified. 3921*0Sstevel@tonic-gate * 3922*0Sstevel@tonic-gate * In order to shutdown the zone, we will hand off control to zoneadmd 3923*0Sstevel@tonic-gate * (running in the global zone) via a door. We do a half-hearted job at 3924*0Sstevel@tonic-gate * killing all processes in the zone, create a kernel thread to contact 3925*0Sstevel@tonic-gate * zoneadmd, and make note of the "uniqid" of the zone. The uniqid is 3926*0Sstevel@tonic-gate * a form of generation number used to let zoneadmd (as well as 3927*0Sstevel@tonic-gate * zone_destroy()) know exactly which zone they're re talking about. 3928*0Sstevel@tonic-gate */ 3929*0Sstevel@tonic-gate int 3930*0Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp) 3931*0Sstevel@tonic-gate { 3932*0Sstevel@tonic-gate struct zarg *zargp; 3933*0Sstevel@tonic-gate zone_cmd_t zcmd; 3934*0Sstevel@tonic-gate zone_t *zone; 3935*0Sstevel@tonic-gate 3936*0Sstevel@tonic-gate zone = curproc->p_zone; 3937*0Sstevel@tonic-gate ASSERT(getzoneid() != GLOBAL_ZONEID); 3938*0Sstevel@tonic-gate 3939*0Sstevel@tonic-gate switch (cmd) { 3940*0Sstevel@tonic-gate case A_SHUTDOWN: 3941*0Sstevel@tonic-gate switch (fcn) { 3942*0Sstevel@tonic-gate case AD_HALT: 3943*0Sstevel@tonic-gate case AD_POWEROFF: 3944*0Sstevel@tonic-gate zcmd = Z_HALT; 3945*0Sstevel@tonic-gate break; 3946*0Sstevel@tonic-gate case AD_BOOT: 3947*0Sstevel@tonic-gate zcmd = Z_REBOOT; 3948*0Sstevel@tonic-gate break; 3949*0Sstevel@tonic-gate case AD_IBOOT: 3950*0Sstevel@tonic-gate case AD_SBOOT: 3951*0Sstevel@tonic-gate case AD_SIBOOT: 3952*0Sstevel@tonic-gate case AD_NOSYNC: 3953*0Sstevel@tonic-gate return (ENOTSUP); 3954*0Sstevel@tonic-gate default: 3955*0Sstevel@tonic-gate return (EINVAL); 3956*0Sstevel@tonic-gate } 3957*0Sstevel@tonic-gate break; 3958*0Sstevel@tonic-gate case A_REBOOT: 3959*0Sstevel@tonic-gate zcmd = Z_REBOOT; 3960*0Sstevel@tonic-gate break; 3961*0Sstevel@tonic-gate case A_FTRACE: 3962*0Sstevel@tonic-gate case A_REMOUNT: 3963*0Sstevel@tonic-gate case A_FREEZE: 3964*0Sstevel@tonic-gate case A_DUMP: 3965*0Sstevel@tonic-gate return (ENOTSUP); 3966*0Sstevel@tonic-gate default: 3967*0Sstevel@tonic-gate ASSERT(cmd != A_SWAPCTL); /* handled by uadmin() */ 3968*0Sstevel@tonic-gate return (EINVAL); 3969*0Sstevel@tonic-gate } 3970*0Sstevel@tonic-gate 3971*0Sstevel@tonic-gate if (secpolicy_zone_admin(credp, B_FALSE)) 3972*0Sstevel@tonic-gate return (EPERM); 3973*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 3974*0Sstevel@tonic-gate /* 3975*0Sstevel@tonic-gate * zone_status can't be ZONE_IS_EMPTY or higher since curproc 3976*0Sstevel@tonic-gate * is in the zone. 3977*0Sstevel@tonic-gate */ 3978*0Sstevel@tonic-gate ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY); 3979*0Sstevel@tonic-gate if (zone_status_get(zone) > ZONE_IS_RUNNING) { 3980*0Sstevel@tonic-gate /* 3981*0Sstevel@tonic-gate * This zone is already on its way down. 3982*0Sstevel@tonic-gate */ 3983*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 3984*0Sstevel@tonic-gate return (0); 3985*0Sstevel@tonic-gate } 3986*0Sstevel@tonic-gate /* 3987*0Sstevel@tonic-gate * Prevent future zone_enter()s 3988*0Sstevel@tonic-gate */ 3989*0Sstevel@tonic-gate zone_status_set(zone, ZONE_IS_SHUTTING_DOWN); 3990*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 3991*0Sstevel@tonic-gate 3992*0Sstevel@tonic-gate /* 3993*0Sstevel@tonic-gate * Kill everyone now and call zoneadmd later. 3994*0Sstevel@tonic-gate * zone_ki_call_zoneadmd() will do a more thorough job of this 3995*0Sstevel@tonic-gate * later. 3996*0Sstevel@tonic-gate */ 3997*0Sstevel@tonic-gate killall(zone->zone_id); 3998*0Sstevel@tonic-gate /* 3999*0Sstevel@tonic-gate * Now, create the thread to contact zoneadmd and do the rest of the 4000*0Sstevel@tonic-gate * work. This thread can't be created in our zone otherwise 4001*0Sstevel@tonic-gate * zone_destroy() would deadlock. 4002*0Sstevel@tonic-gate */ 4003*0Sstevel@tonic-gate zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP); 4004*0Sstevel@tonic-gate zargp->arg.cmd = zcmd; 4005*0Sstevel@tonic-gate zargp->arg.uniqid = zone->zone_uniqid; 4006*0Sstevel@tonic-gate (void) strcpy(zargp->arg.locale, "C"); 4007*0Sstevel@tonic-gate zone_hold(zargp->zone = zone); 4008*0Sstevel@tonic-gate 4009*0Sstevel@tonic-gate (void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0, 4010*0Sstevel@tonic-gate TS_RUN, minclsyspri); 4011*0Sstevel@tonic-gate exit(CLD_EXITED, 0); 4012*0Sstevel@tonic-gate 4013*0Sstevel@tonic-gate return (EINVAL); 4014*0Sstevel@tonic-gate } 4015*0Sstevel@tonic-gate 4016*0Sstevel@tonic-gate /* 4017*0Sstevel@tonic-gate * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's 4018*0Sstevel@tonic-gate * status to ZONE_IS_SHUTTING_DOWN. 4019*0Sstevel@tonic-gate */ 4020*0Sstevel@tonic-gate void 4021*0Sstevel@tonic-gate zone_shutdown_global(void) 4022*0Sstevel@tonic-gate { 4023*0Sstevel@tonic-gate ASSERT(curproc->p_zone == global_zone); 4024*0Sstevel@tonic-gate 4025*0Sstevel@tonic-gate mutex_enter(&zone_status_lock); 4026*0Sstevel@tonic-gate ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING); 4027*0Sstevel@tonic-gate zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN); 4028*0Sstevel@tonic-gate mutex_exit(&zone_status_lock); 4029*0Sstevel@tonic-gate } 4030