10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52677Sml93401 * Common Development and Distribution License (the "License"). 62677Sml93401 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*11861SMarek.Pospisil@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 26*11861SMarek.Pospisil@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27*11861SMarek.Pospisil@Sun.COM /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * Common Inter-Process Communication routines. 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * Overview 340Sstevel@tonic-gate * -------- 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * The System V inter-process communication (IPC) facilities provide 370Sstevel@tonic-gate * three services, message queues, semaphore arrays, and shared memory 380Sstevel@tonic-gate * segments, which are mananged using filesystem-like namespaces. 390Sstevel@tonic-gate * Unlike a filesystem, these namespaces aren't mounted and accessible 400Sstevel@tonic-gate * via a path -- a special API is used to interact with the different 410Sstevel@tonic-gate * facilities (nothing precludes a VFS-based interface, but the 420Sstevel@tonic-gate * standards require the special APIs). Furthermore, these special 430Sstevel@tonic-gate * APIs don't use file descriptors, nor do they have an equivalent. 440Sstevel@tonic-gate * This means that every operation which acts on an object needs to 450Sstevel@tonic-gate * perform the quivalent of a lookup, which in turn means that every 460Sstevel@tonic-gate * operation can fail if the specified object doesn't exist in the 470Sstevel@tonic-gate * facility's namespace. 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * Objects 500Sstevel@tonic-gate * ------- 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * Each object in a namespace has a unique ID, which is assigned by the 530Sstevel@tonic-gate * system and is used to identify the object when performing operations 540Sstevel@tonic-gate * on it. An object can also have a key, which is selected by the user 550Sstevel@tonic-gate * at allocation time and is used as a primitive rendezvous mechanism. 560Sstevel@tonic-gate * An object without a key is said to have a "private" key. 570Sstevel@tonic-gate * 580Sstevel@tonic-gate * To perform an operation on an object given its key, one must first 590Sstevel@tonic-gate * perform a lookup and obtain its ID. The ID is then used to identify 600Sstevel@tonic-gate * the object when performing the operation. If the object has a 610Sstevel@tonic-gate * private key, the ID must be known or obtained by other means. 620Sstevel@tonic-gate * 630Sstevel@tonic-gate * Each object in the namespace has a creator uid and gid, as well as 640Sstevel@tonic-gate * an owner uid and gid. Both are initialized with the ruid and rgid 650Sstevel@tonic-gate * of the process which created the object. The creator or current 660Sstevel@tonic-gate * owner has the ability to change the owner of the object. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * Each object in the namespace has a set of file-like permissions, 690Sstevel@tonic-gate * which, in conjunction with the creator and owner uid and gid, 700Sstevel@tonic-gate * control read and write access to the object (execute is ignored). 710Sstevel@tonic-gate * 722677Sml93401 * Each object also has a creator project and zone, which are used to 732677Sml93401 * account for its resource usage. 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * Operations 760Sstevel@tonic-gate * ---------- 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * There are five operations which all three facilities have in 790Sstevel@tonic-gate * common: GET, SET, STAT, RMID, and IDS. 800Sstevel@tonic-gate * 810Sstevel@tonic-gate * GET, like open, is used to allocate a new object or obtain an 820Sstevel@tonic-gate * existing one (using its key). It takes a key, a set of flags and 830Sstevel@tonic-gate * mode bits, and optionally facility-specific arguments. If the key 840Sstevel@tonic-gate * is IPC_PRIVATE, a new object with the requested mode bits and 850Sstevel@tonic-gate * facility-specific attributes is created. If the key isn't 860Sstevel@tonic-gate * IPC_PRIVATE, the GET will attempt to look up the specified key and 870Sstevel@tonic-gate * either return that or create a new key depending on the state of the 880Sstevel@tonic-gate * IPC_CREAT and IPC_EXCL flags, much like open. If GET needs to 890Sstevel@tonic-gate * allocate an object, it can fail if there is insufficient space in 900Sstevel@tonic-gate * the namespace (the maximum number of ids for the facility has been 910Sstevel@tonic-gate * exceeded) or if the facility-specific initialization fails. If GET 920Sstevel@tonic-gate * finds an object it can return, it can still fail if that object's 930Sstevel@tonic-gate * permissions or facility-specific attributes are less than those 940Sstevel@tonic-gate * requested. 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * SET is used to adjust facility-specific parameters of an object, in 970Sstevel@tonic-gate * addition to the owner uid and gid, and mode bits. It can fail if 980Sstevel@tonic-gate * the caller isn't the creator or owner. 990Sstevel@tonic-gate * 1000Sstevel@tonic-gate * STAT is used to obtain information about an object including the 1010Sstevel@tonic-gate * general attributes object described as well as facility-specific 1020Sstevel@tonic-gate * information. It can fail if the caller doesn't have read 1030Sstevel@tonic-gate * permission. 1040Sstevel@tonic-gate * 1050Sstevel@tonic-gate * RMID removes an object from the namespace. Subsequent operations 1060Sstevel@tonic-gate * using the object's ID or key will fail (until another object is 1070Sstevel@tonic-gate * created with the same key or ID). Since an RMID may be performed 1080Sstevel@tonic-gate * asynchronously with other operations, it is possible that other 1090Sstevel@tonic-gate * threads and/or processes will have references to the object. While 1100Sstevel@tonic-gate * a facility may have actions which need to be performed at RMID time, 1110Sstevel@tonic-gate * only when all references are dropped can the object be destroyed. 1120Sstevel@tonic-gate * RMID will fail if the caller isn't the creator or owner. 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * IDS obtains a list of all IDs in a facility's namespace. There are 1150Sstevel@tonic-gate * no facility-specific behaviors of IDS. 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * Design 1180Sstevel@tonic-gate * ------ 1190Sstevel@tonic-gate * 1200Sstevel@tonic-gate * Because some IPC facilities provide services whose operations must 1210Sstevel@tonic-gate * scale, a mechanism which allows fast, concurrent access to 1220Sstevel@tonic-gate * individual objects is needed. Of primary importance is object 1230Sstevel@tonic-gate * lookup based on ID (SET, STAT, others). Allocation (GET), 1240Sstevel@tonic-gate * deallocation (RMID), ID enumeration (IDS), and key lookups (GET) are 1250Sstevel@tonic-gate * lesser concerns, but should be implemented in such a way that ID 1260Sstevel@tonic-gate * lookup isn't affected (at least not in the common case). 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * Starting from the bottom up, each object is represented by a 1290Sstevel@tonic-gate * structure, the first member of which must be a kipc_perm_t. The 1300Sstevel@tonic-gate * kipc_perm_t contains the information described above in "Objects", a 1310Sstevel@tonic-gate * reference count (since the object may continue to exist after it has 1320Sstevel@tonic-gate * been removed from the namespace), as well as some additional 1330Sstevel@tonic-gate * metadata used to manage data structure membership. These objects 1340Sstevel@tonic-gate * are dynamically allocated. 1350Sstevel@tonic-gate * 1360Sstevel@tonic-gate * Above the objects is a power-of-two sized table of ID slots. Each 1370Sstevel@tonic-gate * slot contains a pointer to an object, a sequence number, and a 1380Sstevel@tonic-gate * lock. An object's ID is a function of its slot's index in the table 1390Sstevel@tonic-gate * and its slot's sequence number. Every time a slot is released (via 1400Sstevel@tonic-gate * RMID) its sequence number is increased. Strictly speaking, the 1410Sstevel@tonic-gate * sequence number is unnecessary. However, checking the sequence 1420Sstevel@tonic-gate * number after a lookup provides a certain degree of robustness 1430Sstevel@tonic-gate * against the use of stale IDs (useful since nothing else does). When 1440Sstevel@tonic-gate * the table fills up, it is resized (see Locking, below). 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * Of an ID's 31 bits (an ID is, as defined by the standards, a signed 1470Sstevel@tonic-gate * int) the top IPC_SEQ_BITS are used for the sequence number with the 1480Sstevel@tonic-gate * remainder holding the index into the table. The size of the table 1490Sstevel@tonic-gate * is therefore bounded at 2 ^ (31 - IPC_SEQ_BITS) slots. 1500Sstevel@tonic-gate * 1510Sstevel@tonic-gate * Managing this table is the ipc_service structure. It contains a 1520Sstevel@tonic-gate * pointer to the dynamically allocated ID table, a namespace-global 1530Sstevel@tonic-gate * lock, an id_space for managing the free space in the table, and 1540Sstevel@tonic-gate * sundry other metadata necessary for the maintenance of the 1550Sstevel@tonic-gate * namespace. An AVL tree of all keyed objects in the table (sorted by 1560Sstevel@tonic-gate * key) is used for key lookups. An unordered doubly linked list of 1570Sstevel@tonic-gate * all objects in the namespace (keyed or not) is maintained to 1580Sstevel@tonic-gate * facilitate ID enumeration. 1590Sstevel@tonic-gate * 1600Sstevel@tonic-gate * To help visualize these relationships, here's a picture of a 1610Sstevel@tonic-gate * namespace with a table of size 8 containing three objects 1620Sstevel@tonic-gate * (IPC_SEQ_BITS = 28): 1630Sstevel@tonic-gate * 1640Sstevel@tonic-gate * 1650Sstevel@tonic-gate * +-ipc_service_t--+ 1660Sstevel@tonic-gate * | table *---\ 1670Sstevel@tonic-gate * | keys *---+----------------------\ 1680Sstevel@tonic-gate * | all ids *--\| | 1690Sstevel@tonic-gate * | | || | 1700Sstevel@tonic-gate * +----------------+ || | 1710Sstevel@tonic-gate * || | 1720Sstevel@tonic-gate * /-------------------/| | 1730Sstevel@tonic-gate * | /---------------/ | 1740Sstevel@tonic-gate * | | | 1750Sstevel@tonic-gate * | v | 1760Sstevel@tonic-gate * | +-0------+-1------+-2------+-3------+-4--+---+-5------+-6------+-7------+ 1770Sstevel@tonic-gate * | | Seq=3 | | | Seq=1 | : | | | Seq=6 | 1780Sstevel@tonic-gate * | | | | | | : | | | | 1790Sstevel@tonic-gate * | +-*------+--------+--------+-*------+----+---+--------+--------+-*------+ 1800Sstevel@tonic-gate * | | | | | 1810Sstevel@tonic-gate * | | /---/ | /----------------/ 1820Sstevel@tonic-gate * | | | | | 1830Sstevel@tonic-gate * | v v | v 1840Sstevel@tonic-gate * | +-kipc_perm_t-+ +-kipc_perm_t-+ | +-kipc_perm_t-+ 1850Sstevel@tonic-gate * | | id=0x30 | | id=0x13 | | | id=0x67 | 1860Sstevel@tonic-gate * | | key=0xfeed | | key=0xbeef | | | key=0xcafe | 1870Sstevel@tonic-gate * \->| [list] |<------>| [list] |<------>| [list] | 1880Sstevel@tonic-gate * /->| [avl left] x /--->| [avl left] x \--->| [avl left] *---\ 1890Sstevel@tonic-gate * | | [avl right] x | | [avl right] x | [avl right] *---+-\ 1900Sstevel@tonic-gate * | | | | | | | | | | 1910Sstevel@tonic-gate * | +-------------+ | +-------------+ +-------------+ | | 1920Sstevel@tonic-gate * | \---------------------------------------------/ | 1930Sstevel@tonic-gate * \--------------------------------------------------------------------/ 1940Sstevel@tonic-gate * 1950Sstevel@tonic-gate * Locking 1960Sstevel@tonic-gate * ------- 1970Sstevel@tonic-gate * 1980Sstevel@tonic-gate * There are three locks (or sets of locks) which are used to ensure 1990Sstevel@tonic-gate * correctness: the slot locks, the namespace lock, and p_lock (needed 2000Sstevel@tonic-gate * when checking resource controls). Their ordering is 2010Sstevel@tonic-gate * 2020Sstevel@tonic-gate * namespace lock -> slot lock 0 -> ... -> slot lock t -> p_lock 2030Sstevel@tonic-gate * 2040Sstevel@tonic-gate * Generally speaking, the namespace lock is used to protect allocation 2050Sstevel@tonic-gate * and removal from the namespace, ID enumeration, and resizing the ID 2060Sstevel@tonic-gate * table. Specifically: 2070Sstevel@tonic-gate * 2080Sstevel@tonic-gate * - write access to all fields of the ipc_service structure 2090Sstevel@tonic-gate * - read access to all variable fields of ipc_service except 2100Sstevel@tonic-gate * ipcs_tabsz (table size) and ipcs_table (the table pointer) 2110Sstevel@tonic-gate * - read/write access to ipc_avl, ipc_list in visible objects' 2120Sstevel@tonic-gate * kipc_perm structures (i.e. objects which have been removed from 2130Sstevel@tonic-gate * the namespace don't have this restriction) 2140Sstevel@tonic-gate * - write access to ipct_seq and ipct_data in the table entries 2150Sstevel@tonic-gate * 2160Sstevel@tonic-gate * A slot lock by itself is meaningless (except when resizing). Of 2170Sstevel@tonic-gate * greater interest conceptually is the notion of an ID lock -- a 2180Sstevel@tonic-gate * "virtual lock" which refers to whichever slot lock an object's ID 2190Sstevel@tonic-gate * currently hashes to. 2200Sstevel@tonic-gate * 2210Sstevel@tonic-gate * An ID lock protects all objects with that ID. Normally there will 2220Sstevel@tonic-gate * only be one such object: the one pointed to by the locked slot. 2230Sstevel@tonic-gate * However, if an object is removed from the namespace but retains 2240Sstevel@tonic-gate * references (e.g. an attached shared memory segment which has been 2250Sstevel@tonic-gate * RMIDed), it continues to use the lock associated with its original 2260Sstevel@tonic-gate * ID. While this can result in increased contention, operations which 2270Sstevel@tonic-gate * require taking the ID lock of removed objects are infrequent. 2280Sstevel@tonic-gate * 2290Sstevel@tonic-gate * Specifically, an ID lock protects the contents of an object's 2300Sstevel@tonic-gate * structure, including the contents of the embedded kipc_perm 2310Sstevel@tonic-gate * structure (but excluding those fields protected by the namespace 2320Sstevel@tonic-gate * lock). It also protects the ipct_seq and ipct_data fields in its 2330Sstevel@tonic-gate * slot (it is really a slot lock, after all). 2340Sstevel@tonic-gate * 2350Sstevel@tonic-gate * Recall that the table is resizable. To avoid requiring every ID 2360Sstevel@tonic-gate * lookup to take a global lock, a scheme much like that employed for 2370Sstevel@tonic-gate * file descriptors (see the comment above UF_ENTER in user.h) is 2380Sstevel@tonic-gate * used. Note that the sequence number and data pointer are protected 2390Sstevel@tonic-gate * by both the namespace lock and their slot lock. When the table is 2400Sstevel@tonic-gate * resized, the following operations take place: 2410Sstevel@tonic-gate * 2420Sstevel@tonic-gate * 1) A new table is allocated. 2430Sstevel@tonic-gate * 2) The global lock is taken. 2440Sstevel@tonic-gate * 3) All old slots are locked, in order. 2450Sstevel@tonic-gate * 4) The first half of the new slots are locked. 2460Sstevel@tonic-gate * 5) All table entries are copied to the new table, and cleared from 2470Sstevel@tonic-gate * the old table. 2480Sstevel@tonic-gate * 6) The ipc_service structure is updated to point to the new table. 2490Sstevel@tonic-gate * 7) The ipc_service structure is updated with the new table size. 2500Sstevel@tonic-gate * 8) All slot locks (old and new) are dropped. 2510Sstevel@tonic-gate * 2520Sstevel@tonic-gate * Because the slot locks are embedded in the table, ID lookups and 2530Sstevel@tonic-gate * other operations which require taking an slot lock need to verify 2540Sstevel@tonic-gate * that the lock taken wasn't part of a stale table. This is 2550Sstevel@tonic-gate * accomplished by checking the table size before and after 2560Sstevel@tonic-gate * dereferencing the table pointer and taking the lock: if the size 2570Sstevel@tonic-gate * changes, the lock must be dropped and reacquired. It is this 2580Sstevel@tonic-gate * additional work which distinguishes an ID lock from a slot lock. 2590Sstevel@tonic-gate * 2600Sstevel@tonic-gate * Because we can't guarantee that threads aren't accessing the old 2610Sstevel@tonic-gate * tables' locks, they are never deallocated. To prevent spurious 2620Sstevel@tonic-gate * reports of memory leaks, a pointer to the discarded table is stored 2630Sstevel@tonic-gate * in the new one in step 5. (Theoretically ipcs_destroy will delete 2640Sstevel@tonic-gate * the discarded tables, but it is only ever called from a failed _init 2650Sstevel@tonic-gate * invocation; i.e. when there aren't any.) 2660Sstevel@tonic-gate * 2670Sstevel@tonic-gate * Interfaces 2680Sstevel@tonic-gate * ---------- 2690Sstevel@tonic-gate * 2700Sstevel@tonic-gate * The following interfaces are provided by the ipc module for use by 2710Sstevel@tonic-gate * the individual IPC facilities: 2720Sstevel@tonic-gate * 2730Sstevel@tonic-gate * ipcperm_access 2740Sstevel@tonic-gate * 2750Sstevel@tonic-gate * Given an object and a cred structure, determines if the requested 2760Sstevel@tonic-gate * access type is allowed. 2770Sstevel@tonic-gate * 2780Sstevel@tonic-gate * ipcperm_set, ipcperm_stat, 2790Sstevel@tonic-gate * ipcperm_set64, ipcperm_stat64 2800Sstevel@tonic-gate * 2810Sstevel@tonic-gate * Performs the common portion of an STAT or SET operation. All 2820Sstevel@tonic-gate * (except stat and stat64) can fail, so they should be called before 2830Sstevel@tonic-gate * any facility-specific non-reversible changes are made to an 2840Sstevel@tonic-gate * object. Similarly, the set operations have side effects, so they 2850Sstevel@tonic-gate * should only be called once the possibility of a facility-specific 2860Sstevel@tonic-gate * failure is eliminated. 2870Sstevel@tonic-gate * 2880Sstevel@tonic-gate * ipcs_create 2890Sstevel@tonic-gate * 2900Sstevel@tonic-gate * Creates an IPC namespace for use by an IPC facility. 2910Sstevel@tonic-gate * 2920Sstevel@tonic-gate * ipcs_destroy 2930Sstevel@tonic-gate * 2940Sstevel@tonic-gate * Destroys an IPC namespace. 2950Sstevel@tonic-gate * 2960Sstevel@tonic-gate * ipcs_lock, ipcs_unlock 2970Sstevel@tonic-gate * 2980Sstevel@tonic-gate * Takes the namespace lock. Ideally such access wouldn't be 2990Sstevel@tonic-gate * necessary, but there may be facility-specific data protected by 3000Sstevel@tonic-gate * this lock (e.g. project-wide resource consumption). 3010Sstevel@tonic-gate * 3020Sstevel@tonic-gate * ipc_lock 3030Sstevel@tonic-gate * 3040Sstevel@tonic-gate * Takes the lock associated with an ID. Can't fail. 3050Sstevel@tonic-gate * 3060Sstevel@tonic-gate * ipc_relock 3070Sstevel@tonic-gate * 3080Sstevel@tonic-gate * Like ipc_lock, but takes a pointer to a held lock. Drops the lock 3090Sstevel@tonic-gate * unless it is the one that would have been returned by ipc_lock. 3100Sstevel@tonic-gate * Used after calls to cv_wait. 3110Sstevel@tonic-gate * 3120Sstevel@tonic-gate * ipc_lookup 3130Sstevel@tonic-gate * 3140Sstevel@tonic-gate * Performs an ID lookup, returns with the ID lock held. Fails if 3150Sstevel@tonic-gate * the ID doesn't exist in the namespace. 3160Sstevel@tonic-gate * 3170Sstevel@tonic-gate * ipc_hold 3180Sstevel@tonic-gate * 3190Sstevel@tonic-gate * Takes a reference on an object. 3200Sstevel@tonic-gate * 3210Sstevel@tonic-gate * ipc_rele 3220Sstevel@tonic-gate * 3230Sstevel@tonic-gate * Releases a reference on an object, and drops the object's lock. 3240Sstevel@tonic-gate * Calls the object's destructor if last reference is being 3250Sstevel@tonic-gate * released. 3260Sstevel@tonic-gate * 3270Sstevel@tonic-gate * ipc_rele_locked 3280Sstevel@tonic-gate * 3290Sstevel@tonic-gate * Releases a reference on an object. Doesn't drop lock, and may 3300Sstevel@tonic-gate * only be called when there is more than one reference to the 3310Sstevel@tonic-gate * object. 3320Sstevel@tonic-gate * 3330Sstevel@tonic-gate * ipc_get, ipc_commit_begin, ipc_commit_end, ipc_cleanup 3340Sstevel@tonic-gate * 3350Sstevel@tonic-gate * Components of a GET operation. ipc_get performs a key lookup, 3360Sstevel@tonic-gate * allocating an object if the key isn't found (returning with the 3370Sstevel@tonic-gate * namespace lock and p_lock held), and returning the existing object 3380Sstevel@tonic-gate * if it is (with the object lock held). ipc_get doesn't modify the 3390Sstevel@tonic-gate * namespace. 3400Sstevel@tonic-gate * 3410Sstevel@tonic-gate * ipc_commit_begin begins the process of inserting an object 3420Sstevel@tonic-gate * allocated by ipc_get into the namespace, and can fail. If 3430Sstevel@tonic-gate * successful, it returns with the namespace lock and p_lock held. 3440Sstevel@tonic-gate * ipc_commit_end completes the process of inserting an object into 3450Sstevel@tonic-gate * the namespace and can't fail. The facility can call ipc_cleanup 3460Sstevel@tonic-gate * at any time following a successful ipc_get and before 3470Sstevel@tonic-gate * ipc_commit_end or a failed ipc_commit_begin to fail the 3480Sstevel@tonic-gate * allocation. Pseudocode for the suggested GET implementation: 3490Sstevel@tonic-gate * 3500Sstevel@tonic-gate * top: 3510Sstevel@tonic-gate * 3520Sstevel@tonic-gate * ipc_get 3530Sstevel@tonic-gate * 3540Sstevel@tonic-gate * if failure 3550Sstevel@tonic-gate * return 3560Sstevel@tonic-gate * 3570Sstevel@tonic-gate * if found { 3580Sstevel@tonic-gate * 3590Sstevel@tonic-gate * if object meets criteria 3600Sstevel@tonic-gate * unlock object and return success 3610Sstevel@tonic-gate * else 3620Sstevel@tonic-gate * unlock object and return failure 3630Sstevel@tonic-gate * 3640Sstevel@tonic-gate * } else { 3650Sstevel@tonic-gate * 3660Sstevel@tonic-gate * perform resource control tests 3670Sstevel@tonic-gate * drop namespace lock, p_lock 3680Sstevel@tonic-gate * if failure 3690Sstevel@tonic-gate * ipc_cleanup 3700Sstevel@tonic-gate * 3710Sstevel@tonic-gate * perform facility-specific initialization 3720Sstevel@tonic-gate * if failure { 3730Sstevel@tonic-gate * facility-specific cleanup 3740Sstevel@tonic-gate * ipc_cleanup 3750Sstevel@tonic-gate * } 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate * ( At this point the object should be destructible using the 3780Sstevel@tonic-gate * destructor given to ipcs_create ) 3790Sstevel@tonic-gate * 3800Sstevel@tonic-gate * ipc_commit_begin 3810Sstevel@tonic-gate * if retry 3820Sstevel@tonic-gate * goto top 3830Sstevel@tonic-gate * else if failure 3840Sstevel@tonic-gate * return 3850Sstevel@tonic-gate * 3860Sstevel@tonic-gate * perform facility-specific resource control tests/allocations 3870Sstevel@tonic-gate * if failure 3880Sstevel@tonic-gate * ipc_cleanup 3890Sstevel@tonic-gate * 3900Sstevel@tonic-gate * ipc_commit_end 3910Sstevel@tonic-gate * perform any infallible post-creation actions, unlock, and return 3920Sstevel@tonic-gate * 3930Sstevel@tonic-gate * } 3940Sstevel@tonic-gate * 3950Sstevel@tonic-gate * ipc_rmid 3960Sstevel@tonic-gate * 3970Sstevel@tonic-gate * Performs the common portion of an RMID operation -- looks up an ID 3980Sstevel@tonic-gate * removes it, and calls the a facility-specific function to do 3990Sstevel@tonic-gate * RMID-time cleanup on the private portions of the object. 4000Sstevel@tonic-gate * 4010Sstevel@tonic-gate * ipc_ids 4020Sstevel@tonic-gate * 4030Sstevel@tonic-gate * Performs the common portion of an IDS operation. 4040Sstevel@tonic-gate * 4050Sstevel@tonic-gate */ 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate #include <sys/types.h> 4080Sstevel@tonic-gate #include <sys/param.h> 4090Sstevel@tonic-gate #include <sys/cred.h> 4100Sstevel@tonic-gate #include <sys/policy.h> 4110Sstevel@tonic-gate #include <sys/proc.h> 4120Sstevel@tonic-gate #include <sys/user.h> 4130Sstevel@tonic-gate #include <sys/ipc.h> 4140Sstevel@tonic-gate #include <sys/ipc_impl.h> 4150Sstevel@tonic-gate #include <sys/errno.h> 4160Sstevel@tonic-gate #include <sys/systm.h> 4170Sstevel@tonic-gate #include <sys/list.h> 4180Sstevel@tonic-gate #include <sys/atomic.h> 4190Sstevel@tonic-gate #include <sys/zone.h> 4200Sstevel@tonic-gate #include <sys/task.h> 4210Sstevel@tonic-gate #include <sys/modctl.h> 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate #include <c2/audit.h> 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate static struct modlmisc modlmisc = { 4260Sstevel@tonic-gate &mod_miscops, 4270Sstevel@tonic-gate "common ipc code", 4280Sstevel@tonic-gate }; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate static struct modlinkage modlinkage = { 4310Sstevel@tonic-gate MODREV_1, (void *)&modlmisc, NULL 4320Sstevel@tonic-gate }; 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate int 4360Sstevel@tonic-gate _init(void) 4370Sstevel@tonic-gate { 4380Sstevel@tonic-gate return (mod_install(&modlinkage)); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate int 4420Sstevel@tonic-gate _fini(void) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate return (mod_remove(&modlinkage)); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate int 4480Sstevel@tonic-gate _info(struct modinfo *modinfop) 4490Sstevel@tonic-gate { 4500Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate /* 4550Sstevel@tonic-gate * Check message, semaphore, or shared memory access permissions. 4560Sstevel@tonic-gate * 4570Sstevel@tonic-gate * This routine verifies the requested access permission for the current 4580Sstevel@tonic-gate * process. The zone ids are compared, and the appropriate bits are 4590Sstevel@tonic-gate * checked corresponding to owner, group (including the list of 4600Sstevel@tonic-gate * supplementary groups), or everyone. Zero is returned on success. 4610Sstevel@tonic-gate * On failure, the security policy is asked to check to override the 4620Sstevel@tonic-gate * permissions check; the policy will either return 0 for access granted 4630Sstevel@tonic-gate * or EACCES. 4640Sstevel@tonic-gate * 4650Sstevel@tonic-gate * Access to objects in other zones requires that the caller be in the 4660Sstevel@tonic-gate * global zone and have the appropriate IPC_DAC_* privilege, regardless 4670Sstevel@tonic-gate * of whether the uid or gid match those of the object. Note that 4680Sstevel@tonic-gate * cross-zone accesses will normally never get here since they'll 4690Sstevel@tonic-gate * fail in ipc_lookup or ipc_get. 4700Sstevel@tonic-gate * 4710Sstevel@tonic-gate * The arguments must be set up as follows: 4720Sstevel@tonic-gate * p - Pointer to permission structure to verify 4730Sstevel@tonic-gate * mode - Desired access permissions 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate int 4760Sstevel@tonic-gate ipcperm_access(kipc_perm_t *p, int mode, cred_t *cr) 4770Sstevel@tonic-gate { 4780Sstevel@tonic-gate int shifts = 0; 4790Sstevel@tonic-gate uid_t uid = crgetuid(cr); 4800Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate if (p->ipc_zoneid == zoneid) { 4830Sstevel@tonic-gate if (uid != p->ipc_uid && uid != p->ipc_cuid) { 4840Sstevel@tonic-gate shifts += 3; 4850Sstevel@tonic-gate if (!groupmember(p->ipc_gid, cr) && 4860Sstevel@tonic-gate !groupmember(p->ipc_cgid, cr)) 4870Sstevel@tonic-gate shifts += 3; 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate mode &= ~(p->ipc_mode << shifts); 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate if (mode == 0) 4930Sstevel@tonic-gate return (0); 4940Sstevel@tonic-gate } else if (zoneid != GLOBAL_ZONEID) 4950Sstevel@tonic-gate return (EACCES); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate return (secpolicy_ipc_access(cr, p, mode)); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * There are two versions of the ipcperm_set/stat functions: 5020Sstevel@tonic-gate * ipcperm_??? - for use with IPC_SET/STAT 5030Sstevel@tonic-gate * ipcperm_???_64 - for use with IPC_SET64/STAT64 5040Sstevel@tonic-gate * 5050Sstevel@tonic-gate * These functions encapsulate the common portions (copying, permission 5060Sstevel@tonic-gate * checks, and auditing) of the set/stat operations. All, except for 5070Sstevel@tonic-gate * stat and stat_64 which are void, return 0 on success or a non-zero 5080Sstevel@tonic-gate * errno value on error. 5090Sstevel@tonic-gate */ 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate int 5120Sstevel@tonic-gate ipcperm_set(ipc_service_t *service, struct cred *cr, 5130Sstevel@tonic-gate kipc_perm_t *kperm, struct ipc_perm *perm, model_t model) 5140Sstevel@tonic-gate { 5150Sstevel@tonic-gate STRUCT_HANDLE(ipc_perm, lperm); 5160Sstevel@tonic-gate uid_t uid; 5170Sstevel@tonic-gate gid_t gid; 5180Sstevel@tonic-gate mode_t mode; 5195771Sjp151216 zone_t *zone; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate ASSERT(IPC_LOCKED(service, kperm)); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate STRUCT_SET_HANDLE(lperm, model, perm); 5240Sstevel@tonic-gate uid = STRUCT_FGET(lperm, uid); 5250Sstevel@tonic-gate gid = STRUCT_FGET(lperm, gid); 5260Sstevel@tonic-gate mode = STRUCT_FGET(lperm, mode); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate if (secpolicy_ipc_owner(cr, kperm) != 0) 5290Sstevel@tonic-gate return (EPERM); 5300Sstevel@tonic-gate 5315771Sjp151216 zone = crgetzone(cr); 5325771Sjp151216 if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone)) 5330Sstevel@tonic-gate return (EINVAL); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate kperm->ipc_uid = uid; 5360Sstevel@tonic-gate kperm->ipc_gid = gid; 5370Sstevel@tonic-gate kperm->ipc_mode = (mode & 0777) | (kperm->ipc_mode & ~0777); 5380Sstevel@tonic-gate 539*11861SMarek.Pospisil@Sun.COM if (AU_AUDITING()) 5400Sstevel@tonic-gate audit_ipcget(service->ipcs_atype, kperm); 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate return (0); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate void 5460Sstevel@tonic-gate ipcperm_stat(struct ipc_perm *perm, kipc_perm_t *kperm, model_t model) 5470Sstevel@tonic-gate { 5480Sstevel@tonic-gate STRUCT_HANDLE(ipc_perm, lperm); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate STRUCT_SET_HANDLE(lperm, model, perm); 5510Sstevel@tonic-gate STRUCT_FSET(lperm, uid, kperm->ipc_uid); 5520Sstevel@tonic-gate STRUCT_FSET(lperm, gid, kperm->ipc_gid); 5530Sstevel@tonic-gate STRUCT_FSET(lperm, cuid, kperm->ipc_cuid); 5540Sstevel@tonic-gate STRUCT_FSET(lperm, cgid, kperm->ipc_cgid); 5550Sstevel@tonic-gate STRUCT_FSET(lperm, mode, kperm->ipc_mode); 5560Sstevel@tonic-gate STRUCT_FSET(lperm, seq, 0); 5570Sstevel@tonic-gate STRUCT_FSET(lperm, key, kperm->ipc_key); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate int 5610Sstevel@tonic-gate ipcperm_set64(ipc_service_t *service, struct cred *cr, 5620Sstevel@tonic-gate kipc_perm_t *kperm, ipc_perm64_t *perm64) 5630Sstevel@tonic-gate { 5645771Sjp151216 zone_t *zone; 5655771Sjp151216 5660Sstevel@tonic-gate ASSERT(IPC_LOCKED(service, kperm)); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate if (secpolicy_ipc_owner(cr, kperm) != 0) 5690Sstevel@tonic-gate return (EPERM); 5700Sstevel@tonic-gate 5715771Sjp151216 zone = crgetzone(cr); 5725771Sjp151216 if (!VALID_UID(perm64->ipcx_uid, zone) || 5735771Sjp151216 !VALID_GID(perm64->ipcx_gid, zone)) 5740Sstevel@tonic-gate return (EINVAL); 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate kperm->ipc_uid = perm64->ipcx_uid; 5770Sstevel@tonic-gate kperm->ipc_gid = perm64->ipcx_gid; 5780Sstevel@tonic-gate kperm->ipc_mode = (perm64->ipcx_mode & 0777) | 5790Sstevel@tonic-gate (kperm->ipc_mode & ~0777); 5800Sstevel@tonic-gate 581*11861SMarek.Pospisil@Sun.COM if (AU_AUDITING()) 5820Sstevel@tonic-gate audit_ipcget(service->ipcs_atype, kperm); 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate return (0); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate void 5880Sstevel@tonic-gate ipcperm_stat64(ipc_perm64_t *perm64, kipc_perm_t *kperm) 5890Sstevel@tonic-gate { 5900Sstevel@tonic-gate perm64->ipcx_uid = kperm->ipc_uid; 5910Sstevel@tonic-gate perm64->ipcx_gid = kperm->ipc_gid; 5920Sstevel@tonic-gate perm64->ipcx_cuid = kperm->ipc_cuid; 5930Sstevel@tonic-gate perm64->ipcx_cgid = kperm->ipc_cgid; 5940Sstevel@tonic-gate perm64->ipcx_mode = kperm->ipc_mode; 5950Sstevel@tonic-gate perm64->ipcx_key = kperm->ipc_key; 5960Sstevel@tonic-gate perm64->ipcx_projid = kperm->ipc_proj->kpj_id; 5970Sstevel@tonic-gate perm64->ipcx_zoneid = kperm->ipc_zoneid; 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * ipc key comparator. 6030Sstevel@tonic-gate */ 6040Sstevel@tonic-gate static int 6050Sstevel@tonic-gate ipc_key_compar(const void *a, const void *b) 6060Sstevel@tonic-gate { 6070Sstevel@tonic-gate kipc_perm_t *aperm = (kipc_perm_t *)a; 6080Sstevel@tonic-gate kipc_perm_t *bperm = (kipc_perm_t *)b; 6090Sstevel@tonic-gate int ak = aperm->ipc_key; 6100Sstevel@tonic-gate int bk = bperm->ipc_key; 6110Sstevel@tonic-gate zoneid_t az; 6120Sstevel@tonic-gate zoneid_t bz; 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate ASSERT(ak != IPC_PRIVATE); 6150Sstevel@tonic-gate ASSERT(bk != IPC_PRIVATE); 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * Compare key first, then zoneid. This optimizes performance for 6190Sstevel@tonic-gate * systems with only one zone, since the zone checks will only be 6200Sstevel@tonic-gate * made when the keys match. 6210Sstevel@tonic-gate */ 6220Sstevel@tonic-gate if (ak < bk) 6230Sstevel@tonic-gate return (-1); 6240Sstevel@tonic-gate if (ak > bk) 6250Sstevel@tonic-gate return (1); 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate /* keys match */ 6280Sstevel@tonic-gate az = aperm->ipc_zoneid; 6290Sstevel@tonic-gate bz = bperm->ipc_zoneid; 6300Sstevel@tonic-gate if (az < bz) 6310Sstevel@tonic-gate return (-1); 6320Sstevel@tonic-gate if (az > bz) 6330Sstevel@tonic-gate return (1); 6340Sstevel@tonic-gate return (0); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * Create an ipc service. 6390Sstevel@tonic-gate */ 6400Sstevel@tonic-gate ipc_service_t * 6412677Sml93401 ipcs_create(const char *name, rctl_hndl_t proj_rctl, rctl_hndl_t zone_rctl, 6422677Sml93401 size_t size, ipc_func_t *dtor, ipc_func_t *rmid, int audit_type, 6432677Sml93401 size_t rctl_offset) 6440Sstevel@tonic-gate { 6450Sstevel@tonic-gate ipc_service_t *result; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate result = kmem_alloc(sizeof (ipc_service_t), KM_SLEEP); 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate mutex_init(&result->ipcs_lock, NULL, MUTEX_ADAPTIVE, NULL); 6500Sstevel@tonic-gate result->ipcs_count = 0; 6510Sstevel@tonic-gate avl_create(&result->ipcs_keys, ipc_key_compar, size, 0); 6520Sstevel@tonic-gate result->ipcs_tabsz = IPC_IDS_MIN; 6530Sstevel@tonic-gate result->ipcs_table = 6540Sstevel@tonic-gate kmem_zalloc(IPC_IDS_MIN * sizeof (ipc_slot_t), KM_SLEEP); 6550Sstevel@tonic-gate result->ipcs_ssize = size; 6560Sstevel@tonic-gate result->ipcs_ids = id_space_create(name, 0, IPC_IDS_MIN); 6570Sstevel@tonic-gate result->ipcs_dtor = dtor; 6580Sstevel@tonic-gate result->ipcs_rmid = rmid; 6592677Sml93401 result->ipcs_proj_rctl = proj_rctl; 6602677Sml93401 result->ipcs_zone_rctl = zone_rctl; 6610Sstevel@tonic-gate result->ipcs_atype = audit_type; 6622677Sml93401 ASSERT(rctl_offset < sizeof (ipc_rqty_t)); 6630Sstevel@tonic-gate result->ipcs_rctlofs = rctl_offset; 6640Sstevel@tonic-gate list_create(&result->ipcs_usedids, sizeof (kipc_perm_t), 6650Sstevel@tonic-gate offsetof(kipc_perm_t, ipc_list)); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate return (result); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate /* 6710Sstevel@tonic-gate * Destroy an ipc service. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate void 6740Sstevel@tonic-gate ipcs_destroy(ipc_service_t *service) 6750Sstevel@tonic-gate { 6760Sstevel@tonic-gate ipc_slot_t *slot, *next; 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate ASSERT(service->ipcs_count == 0); 6810Sstevel@tonic-gate avl_destroy(&service->ipcs_keys); 6820Sstevel@tonic-gate list_destroy(&service->ipcs_usedids); 6830Sstevel@tonic-gate id_space_destroy(service->ipcs_ids); 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate for (slot = service->ipcs_table; slot; slot = next) { 6860Sstevel@tonic-gate next = slot[0].ipct_chain; 6870Sstevel@tonic-gate kmem_free(slot, service->ipcs_tabsz * sizeof (ipc_slot_t)); 6880Sstevel@tonic-gate service->ipcs_tabsz >>= 1; 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate mutex_destroy(&service->ipcs_lock); 6920Sstevel@tonic-gate kmem_free(service, sizeof (ipc_service_t)); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate /* 6960Sstevel@tonic-gate * Takes the service lock. 6970Sstevel@tonic-gate */ 6980Sstevel@tonic-gate void 6990Sstevel@tonic-gate ipcs_lock(ipc_service_t *service) 7000Sstevel@tonic-gate { 7010Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * Releases the service lock. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate void 7080Sstevel@tonic-gate ipcs_unlock(ipc_service_t *service) 7090Sstevel@tonic-gate { 7100Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Locks the specified ID. Returns the ID's ID table index. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate static int 7180Sstevel@tonic-gate ipc_lock_internal(ipc_service_t *service, uint_t id) 7190Sstevel@tonic-gate { 7200Sstevel@tonic-gate uint_t tabsz; 7210Sstevel@tonic-gate uint_t index; 7220Sstevel@tonic-gate kmutex_t *mutex; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate for (;;) { 7250Sstevel@tonic-gate tabsz = service->ipcs_tabsz; 7260Sstevel@tonic-gate membar_consumer(); 7270Sstevel@tonic-gate index = id & (tabsz - 1); 7280Sstevel@tonic-gate mutex = &service->ipcs_table[index].ipct_lock; 7290Sstevel@tonic-gate mutex_enter(mutex); 7300Sstevel@tonic-gate if (tabsz == service->ipcs_tabsz) 7310Sstevel@tonic-gate break; 7320Sstevel@tonic-gate mutex_exit(mutex); 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate return (index); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate /* 7390Sstevel@tonic-gate * Locks the specified ID. Returns a pointer to the ID's lock. 7400Sstevel@tonic-gate */ 7410Sstevel@tonic-gate kmutex_t * 7420Sstevel@tonic-gate ipc_lock(ipc_service_t *service, int id) 7430Sstevel@tonic-gate { 7440Sstevel@tonic-gate uint_t index; 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * These assertions don't reflect requirements of the code 7480Sstevel@tonic-gate * which follows, but they should never fail nonetheless. 7490Sstevel@tonic-gate */ 7500Sstevel@tonic-gate ASSERT(id >= 0); 7510Sstevel@tonic-gate ASSERT(IPC_INDEX(id) < service->ipcs_tabsz); 7520Sstevel@tonic-gate index = ipc_lock_internal(service, id); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate return (&service->ipcs_table[index].ipct_lock); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate /* 7580Sstevel@tonic-gate * Checks to see if the held lock provided is the current lock for the 7590Sstevel@tonic-gate * specified id. If so, we return it instead of dropping it and 7600Sstevel@tonic-gate * returning the result of ipc_lock. This is intended to speed up cv 7610Sstevel@tonic-gate * wakeups where we are left holding a lock which could be stale, but 7620Sstevel@tonic-gate * probably isn't. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate kmutex_t * 7650Sstevel@tonic-gate ipc_relock(ipc_service_t *service, int id, kmutex_t *lock) 7660Sstevel@tonic-gate { 7670Sstevel@tonic-gate ASSERT(id >= 0); 7680Sstevel@tonic-gate ASSERT(IPC_INDEX(id) < service->ipcs_tabsz); 7690Sstevel@tonic-gate ASSERT(MUTEX_HELD(lock)); 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate if (&service->ipcs_table[IPC_INDEX(id)].ipct_lock == lock) 7720Sstevel@tonic-gate return (lock); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate mutex_exit(lock); 7750Sstevel@tonic-gate return (ipc_lock(service, id)); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate /* 7790Sstevel@tonic-gate * Performs an ID lookup. If the ID doesn't exist or has been removed, 7800Sstevel@tonic-gate * or isn't visible to the caller (because of zones), NULL is returned. 7810Sstevel@tonic-gate * Otherwise, a pointer to the ID's perm structure and held ID lock are 7820Sstevel@tonic-gate * returned. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate kmutex_t * 7850Sstevel@tonic-gate ipc_lookup(ipc_service_t *service, int id, kipc_perm_t **perm) 7860Sstevel@tonic-gate { 7870Sstevel@tonic-gate kipc_perm_t *result; 7880Sstevel@tonic-gate uint_t index; 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* 7910Sstevel@tonic-gate * There is no need to check to see if id is in-range (i.e. 7920Sstevel@tonic-gate * positive and fits into the table). If it is out-of-range, 7930Sstevel@tonic-gate * the id simply won't match the object's. 7940Sstevel@tonic-gate */ 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate index = ipc_lock_internal(service, id); 7970Sstevel@tonic-gate result = service->ipcs_table[index].ipct_data; 7980Sstevel@tonic-gate if (result == NULL || result->ipc_id != (uint_t)id || 7990Sstevel@tonic-gate !HASZONEACCESS(curproc, result->ipc_zoneid)) { 8000Sstevel@tonic-gate mutex_exit(&service->ipcs_table[index].ipct_lock); 8010Sstevel@tonic-gate return (NULL); 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate ASSERT(IPC_SEQ(id) == service->ipcs_table[index].ipct_seq); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate *perm = result; 807*11861SMarek.Pospisil@Sun.COM if (AU_AUDITING()) 8080Sstevel@tonic-gate audit_ipc(service->ipcs_atype, id, result); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate return (&service->ipcs_table[index].ipct_lock); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* 8140Sstevel@tonic-gate * Increase the reference count on an ID. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate /*ARGSUSED*/ 8170Sstevel@tonic-gate void 8180Sstevel@tonic-gate ipc_hold(ipc_service_t *s, kipc_perm_t *perm) 8190Sstevel@tonic-gate { 8200Sstevel@tonic-gate ASSERT(IPC_INDEX(perm->ipc_id) < s->ipcs_tabsz); 8210Sstevel@tonic-gate ASSERT(IPC_LOCKED(s, perm)); 8220Sstevel@tonic-gate perm->ipc_ref++; 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate /* 8260Sstevel@tonic-gate * Decrease the reference count on an ID and drops the ID's lock. 8270Sstevel@tonic-gate * Destroys the ID if the new reference count is zero. 8280Sstevel@tonic-gate */ 8290Sstevel@tonic-gate void 8300Sstevel@tonic-gate ipc_rele(ipc_service_t *s, kipc_perm_t *perm) 8310Sstevel@tonic-gate { 8320Sstevel@tonic-gate int nref; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate ASSERT(IPC_INDEX(perm->ipc_id) < s->ipcs_tabsz); 8350Sstevel@tonic-gate ASSERT(IPC_LOCKED(s, perm)); 8360Sstevel@tonic-gate ASSERT(perm->ipc_ref > 0); 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate nref = --perm->ipc_ref; 8390Sstevel@tonic-gate mutex_exit(&s->ipcs_table[IPC_INDEX(perm->ipc_id)].ipct_lock); 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate if (nref == 0) { 8420Sstevel@tonic-gate ASSERT(IPC_FREE(perm)); /* ipc_rmid clears IPC_ALLOC */ 8430Sstevel@tonic-gate s->ipcs_dtor(perm); 8440Sstevel@tonic-gate project_rele(perm->ipc_proj); 8452677Sml93401 zone_rele(perm->ipc_zone); 8460Sstevel@tonic-gate kmem_free(perm, s->ipcs_ssize); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * Decrease the reference count on an ID, but don't drop the ID lock. 8520Sstevel@tonic-gate * Used in cases where one thread needs to remove many references (on 8530Sstevel@tonic-gate * behalf of other parties). 8540Sstevel@tonic-gate */ 8550Sstevel@tonic-gate void 8560Sstevel@tonic-gate ipc_rele_locked(ipc_service_t *s, kipc_perm_t *perm) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate ASSERT(perm->ipc_ref > 1); 8590Sstevel@tonic-gate ASSERT(IPC_INDEX(perm->ipc_id) < s->ipcs_tabsz); 8600Sstevel@tonic-gate ASSERT(IPC_LOCKED(s, perm)); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate perm->ipc_ref--; 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate /* 8670Sstevel@tonic-gate * Internal function to grow the service ID table. 8680Sstevel@tonic-gate */ 8690Sstevel@tonic-gate static int 8700Sstevel@tonic-gate ipc_grow(ipc_service_t *service) 8710Sstevel@tonic-gate { 8720Sstevel@tonic-gate ipc_slot_t *new, *old; 8730Sstevel@tonic-gate int i, oldsize, newsize; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 8760Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate if (service->ipcs_tabsz == IPC_IDS_MAX) 8790Sstevel@tonic-gate return (ENOSPC); 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate oldsize = service->ipcs_tabsz; 8820Sstevel@tonic-gate newsize = oldsize << 1; 8830Sstevel@tonic-gate new = kmem_zalloc(newsize * sizeof (ipc_slot_t), KM_NOSLEEP); 8840Sstevel@tonic-gate if (new == NULL) 8850Sstevel@tonic-gate return (ENOSPC); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate old = service->ipcs_table; 8880Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { 8890Sstevel@tonic-gate mutex_enter(&old[i].ipct_lock); 8900Sstevel@tonic-gate mutex_enter(&new[i].ipct_lock); 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate new[i].ipct_seq = old[i].ipct_seq; 8930Sstevel@tonic-gate new[i].ipct_data = old[i].ipct_data; 8940Sstevel@tonic-gate old[i].ipct_data = NULL; 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate new[0].ipct_chain = old; 8980Sstevel@tonic-gate service->ipcs_table = new; 8990Sstevel@tonic-gate membar_producer(); 9000Sstevel@tonic-gate service->ipcs_tabsz = newsize; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { 9030Sstevel@tonic-gate mutex_exit(&old[i].ipct_lock); 9040Sstevel@tonic-gate mutex_exit(&new[i].ipct_lock); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate id_space_extend(service->ipcs_ids, oldsize, service->ipcs_tabsz); 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate return (0); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate static int 9140Sstevel@tonic-gate ipc_keylookup(ipc_service_t *service, key_t key, int flag, kipc_perm_t **permp) 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate kipc_perm_t *perm = NULL; 9170Sstevel@tonic-gate avl_index_t where; 9180Sstevel@tonic-gate kipc_perm_t template; 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate template.ipc_key = key; 9230Sstevel@tonic-gate template.ipc_zoneid = getzoneid(); 9240Sstevel@tonic-gate if (perm = avl_find(&service->ipcs_keys, &template, &where)) { 9250Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 9260Sstevel@tonic-gate if ((flag & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) 9270Sstevel@tonic-gate return (EEXIST); 9280Sstevel@tonic-gate if ((flag & 0777) & ~perm->ipc_mode) { 929*11861SMarek.Pospisil@Sun.COM if (AU_AUDITING()) 9300Sstevel@tonic-gate audit_ipcget(NULL, (void *)perm); 9310Sstevel@tonic-gate return (EACCES); 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate *permp = perm; 9340Sstevel@tonic-gate return (0); 9350Sstevel@tonic-gate } else if (flag & IPC_CREAT) { 9360Sstevel@tonic-gate *permp = NULL; 9370Sstevel@tonic-gate return (0); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate return (ENOENT); 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate static int 9430Sstevel@tonic-gate ipc_alloc_test(ipc_service_t *service, proc_t *pp) 9440Sstevel@tonic-gate { 9450Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate /* 9480Sstevel@tonic-gate * Resizing the table first would result in a cleaner code 9490Sstevel@tonic-gate * path, but would also allow a user to (permanently) double 9500Sstevel@tonic-gate * the id table size in cases where the allocation would be 9510Sstevel@tonic-gate * denied. Hence we test the rctl first. 9520Sstevel@tonic-gate */ 9530Sstevel@tonic-gate retry: 9540Sstevel@tonic-gate mutex_enter(&pp->p_lock); 9552677Sml93401 if ((rctl_test(service->ipcs_proj_rctl, pp->p_task->tk_proj->kpj_rctls, 9562677Sml93401 pp, 1, RCA_SAFE) & RCT_DENY) || 9572677Sml93401 (rctl_test(service->ipcs_zone_rctl, pp->p_zone->zone_rctls, 9582677Sml93401 pp, 1, RCA_SAFE) & RCT_DENY)) { 9590Sstevel@tonic-gate mutex_exit(&pp->p_lock); 9600Sstevel@tonic-gate return (ENOSPC); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate if (service->ipcs_count == service->ipcs_tabsz) { 9640Sstevel@tonic-gate int error; 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate mutex_exit(&pp->p_lock); 9670Sstevel@tonic-gate if (error = ipc_grow(service)) 9680Sstevel@tonic-gate return (error); 9690Sstevel@tonic-gate goto retry; 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate return (0); 9730Sstevel@tonic-gate } 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate /* 9760Sstevel@tonic-gate * Given a key, search for or create the associated identifier. 9770Sstevel@tonic-gate * 9780Sstevel@tonic-gate * If IPC_CREAT is specified and the key isn't found, or if the key is 9790Sstevel@tonic-gate * equal to IPC_PRIVATE, we return 0 and place a pointer to a newly 9800Sstevel@tonic-gate * allocated object structure in permp. A pointer to the held service 9810Sstevel@tonic-gate * lock is placed in lockp. ipc_mode's IPC_ALLOC bit is clear. 9820Sstevel@tonic-gate * 9830Sstevel@tonic-gate * If the key is found and no error conditions arise, we return 0 and 9840Sstevel@tonic-gate * place a pointer to the existing object structure in permp. A 9850Sstevel@tonic-gate * pointer to the held ID lock is placed in lockp. ipc_mode's 9860Sstevel@tonic-gate * IPC_ALLOC bit is set. 9870Sstevel@tonic-gate * 9880Sstevel@tonic-gate * Otherwise, a non-zero errno value is returned. 9890Sstevel@tonic-gate */ 9900Sstevel@tonic-gate int 9910Sstevel@tonic-gate ipc_get(ipc_service_t *service, key_t key, int flag, kipc_perm_t **permp, 9920Sstevel@tonic-gate kmutex_t **lockp) 9930Sstevel@tonic-gate { 9940Sstevel@tonic-gate kipc_perm_t *perm = NULL; 9950Sstevel@tonic-gate proc_t *pp = curproc; 9960Sstevel@tonic-gate int error, index; 9970Sstevel@tonic-gate cred_t *cr = CRED(); 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate if (key != IPC_PRIVATE) { 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 10020Sstevel@tonic-gate error = ipc_keylookup(service, key, flag, &perm); 10030Sstevel@tonic-gate if (perm != NULL) 10040Sstevel@tonic-gate index = ipc_lock_internal(service, perm->ipc_id); 10050Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (error) { 10080Sstevel@tonic-gate ASSERT(perm == NULL); 10090Sstevel@tonic-gate return (error); 10100Sstevel@tonic-gate } 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate if (perm) { 10130Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 10140Sstevel@tonic-gate *permp = perm; 10150Sstevel@tonic-gate *lockp = &service->ipcs_table[index].ipct_lock; 10160Sstevel@tonic-gate return (0); 10170Sstevel@tonic-gate } 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate /* Key not found; fall through */ 10200Sstevel@tonic-gate } 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate perm = kmem_zalloc(service->ipcs_ssize, KM_SLEEP); 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 10250Sstevel@tonic-gate if (error = ipc_alloc_test(service, pp)) { 10260Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 10270Sstevel@tonic-gate kmem_free(perm, service->ipcs_ssize); 10280Sstevel@tonic-gate return (error); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate perm->ipc_cuid = perm->ipc_uid = crgetuid(cr); 10320Sstevel@tonic-gate perm->ipc_cgid = perm->ipc_gid = crgetgid(cr); 10330Sstevel@tonic-gate perm->ipc_zoneid = getzoneid(); 10340Sstevel@tonic-gate perm->ipc_mode = flag & 0777; 10350Sstevel@tonic-gate perm->ipc_key = key; 10360Sstevel@tonic-gate perm->ipc_ref = 1; 10370Sstevel@tonic-gate perm->ipc_id = IPC_ID_INVAL; 10380Sstevel@tonic-gate *permp = perm; 10390Sstevel@tonic-gate *lockp = &service->ipcs_lock; 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate return (0); 10420Sstevel@tonic-gate } 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate /* 10450Sstevel@tonic-gate * Attempts to add the a newly created ID to the global namespace. If 10460Sstevel@tonic-gate * creating it would cause an error, we return the error. If there is 10470Sstevel@tonic-gate * the possibility that we could obtain the existing ID and return it 10480Sstevel@tonic-gate * to the user, we return EAGAIN. Otherwise, we return 0 with p_lock 10490Sstevel@tonic-gate * and the service lock held. 10500Sstevel@tonic-gate * 10510Sstevel@tonic-gate * Since this should be only called after all initialization has been 10520Sstevel@tonic-gate * completed, on failure we automatically invoke the destructor for the 10530Sstevel@tonic-gate * object and deallocate the memory associated with it. 10540Sstevel@tonic-gate */ 10550Sstevel@tonic-gate int 10560Sstevel@tonic-gate ipc_commit_begin(ipc_service_t *service, key_t key, int flag, 10570Sstevel@tonic-gate kipc_perm_t *newperm) 10580Sstevel@tonic-gate { 10590Sstevel@tonic-gate kipc_perm_t *perm; 10600Sstevel@tonic-gate int error; 10610Sstevel@tonic-gate proc_t *pp = curproc; 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate ASSERT(newperm->ipc_ref == 1); 10640Sstevel@tonic-gate ASSERT(IPC_FREE(newperm)); 10650Sstevel@tonic-gate 10663458Ssl108498 /* 10673458Ssl108498 * Set ipc_proj and ipc_zone so that future calls to ipc_cleanup() 10683458Ssl108498 * clean up the necessary state. This must be done before the 10693458Ssl108498 * potential call to ipcs_dtor() below. 10703458Ssl108498 */ 10713458Ssl108498 newperm->ipc_proj = pp->p_task->tk_proj; 10723458Ssl108498 newperm->ipc_zone = pp->p_zone; 10733458Ssl108498 10740Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 10750Sstevel@tonic-gate /* 10760Sstevel@tonic-gate * Ensure that no-one has raced with us and created the key. 10770Sstevel@tonic-gate */ 10780Sstevel@tonic-gate if ((key != IPC_PRIVATE) && 10790Sstevel@tonic-gate (((error = ipc_keylookup(service, key, flag, &perm)) != 0) || 10800Sstevel@tonic-gate (perm != NULL))) { 10810Sstevel@tonic-gate error = error ? error : EAGAIN; 10820Sstevel@tonic-gate goto errout; 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * Ensure that no-one has raced with us and used the last of 10870Sstevel@tonic-gate * the permissible ids, or the last of the free spaces in the 10880Sstevel@tonic-gate * id table. 10890Sstevel@tonic-gate */ 10900Sstevel@tonic-gate if (error = ipc_alloc_test(service, pp)) 10910Sstevel@tonic-gate goto errout; 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 10940Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pp->p_lock)); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate return (0); 10970Sstevel@tonic-gate errout: 10980Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 10990Sstevel@tonic-gate service->ipcs_dtor(newperm); 11000Sstevel@tonic-gate kmem_free(newperm, service->ipcs_ssize); 11010Sstevel@tonic-gate return (error); 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate /* 11050Sstevel@tonic-gate * Commit the ID allocation transaction. Called with p_lock and the 11060Sstevel@tonic-gate * service lock held, both of which are dropped. Returns the held ID 11070Sstevel@tonic-gate * lock so the caller can extract the ID and perform ipcget auditing. 11080Sstevel@tonic-gate */ 11090Sstevel@tonic-gate kmutex_t * 11100Sstevel@tonic-gate ipc_commit_end(ipc_service_t *service, kipc_perm_t *perm) 11110Sstevel@tonic-gate { 11120Sstevel@tonic-gate ipc_slot_t *slot; 11130Sstevel@tonic-gate avl_index_t where; 11140Sstevel@tonic-gate int index; 11150Sstevel@tonic-gate void *loc; 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 11180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&curproc->p_lock)); 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate (void) project_hold(perm->ipc_proj); 11212677Sml93401 (void) zone_hold(perm->ipc_zone); 11220Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate /* 11250Sstevel@tonic-gate * Pick out our slot. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate service->ipcs_count++; 11280Sstevel@tonic-gate index = id_alloc(service->ipcs_ids); 11290Sstevel@tonic-gate ASSERT(index < service->ipcs_tabsz); 11300Sstevel@tonic-gate slot = &service->ipcs_table[index]; 11310Sstevel@tonic-gate mutex_enter(&slot->ipct_lock); 11320Sstevel@tonic-gate ASSERT(slot->ipct_data == NULL); 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate /* 11350Sstevel@tonic-gate * Update the perm structure. 11360Sstevel@tonic-gate */ 11370Sstevel@tonic-gate perm->ipc_mode |= IPC_ALLOC; 11380Sstevel@tonic-gate perm->ipc_id = (slot->ipct_seq << IPC_SEQ_SHIFT) | index; 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate /* 11410Sstevel@tonic-gate * Push into global visibility. 11420Sstevel@tonic-gate */ 11430Sstevel@tonic-gate slot->ipct_data = perm; 11440Sstevel@tonic-gate if (perm->ipc_key != IPC_PRIVATE) { 11450Sstevel@tonic-gate loc = avl_find(&service->ipcs_keys, perm, &where); 11460Sstevel@tonic-gate ASSERT(loc == NULL); 11470Sstevel@tonic-gate avl_insert(&service->ipcs_keys, perm, where); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate list_insert_head(&service->ipcs_usedids, perm); 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate /* 11520Sstevel@tonic-gate * Update resource consumption. 11530Sstevel@tonic-gate */ 11542677Sml93401 IPC_PROJ_USAGE(perm, service) += 1; 11552677Sml93401 IPC_ZONE_USAGE(perm, service) += 1; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 11580Sstevel@tonic-gate return (&slot->ipct_lock); 11590Sstevel@tonic-gate } 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * Clean up function, in case the allocation fails. If called between 11630Sstevel@tonic-gate * ipc_lookup and ipc_commit_begin, perm->ipc_proj will be 0 and we 11640Sstevel@tonic-gate * merely free the perm structure. If called after ipc_commit_begin, 11650Sstevel@tonic-gate * we also drop locks and call the ID's destructor. 11660Sstevel@tonic-gate */ 11670Sstevel@tonic-gate void 11680Sstevel@tonic-gate ipc_cleanup(ipc_service_t *service, kipc_perm_t *perm) 11690Sstevel@tonic-gate { 11700Sstevel@tonic-gate ASSERT(IPC_FREE(perm)); 11710Sstevel@tonic-gate if (perm->ipc_proj) { 11720Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 11730Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 11740Sstevel@tonic-gate service->ipcs_dtor(perm); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate kmem_free(perm, service->ipcs_ssize); 11770Sstevel@tonic-gate } 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate /* 11810Sstevel@tonic-gate * Common code to remove an IPC object. This should be called after 11820Sstevel@tonic-gate * all permissions checks have been performed, and with the service 11830Sstevel@tonic-gate * and ID locked. Note that this does not remove the object from 11840Sstevel@tonic-gate * the ipcs_usedids list (this needs to be done by the caller before 11850Sstevel@tonic-gate * dropping the service lock). 11860Sstevel@tonic-gate */ 11870Sstevel@tonic-gate static void 11880Sstevel@tonic-gate ipc_remove(ipc_service_t *service, kipc_perm_t *perm) 11890Sstevel@tonic-gate { 11900Sstevel@tonic-gate int id = perm->ipc_id; 11910Sstevel@tonic-gate int index; 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 11940Sstevel@tonic-gate ASSERT(IPC_LOCKED(service, perm)); 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate index = IPC_INDEX(id); 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate service->ipcs_table[index].ipct_data = NULL; 11990Sstevel@tonic-gate 12000Sstevel@tonic-gate if (perm->ipc_key != IPC_PRIVATE) 12010Sstevel@tonic-gate avl_remove(&service->ipcs_keys, perm); 12020Sstevel@tonic-gate list_remove(&service->ipcs_usedids, perm); 12030Sstevel@tonic-gate perm->ipc_mode &= ~IPC_ALLOC; 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate id_free(service->ipcs_ids, index); 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate if (service->ipcs_table[index].ipct_seq++ == IPC_SEQ_MASK) 12080Sstevel@tonic-gate service->ipcs_table[index].ipct_seq = 0; 12090Sstevel@tonic-gate service->ipcs_count--; 12102677Sml93401 ASSERT(IPC_PROJ_USAGE(perm, service) > 0); 12112677Sml93401 ASSERT(IPC_ZONE_USAGE(perm, service) > 0); 12122677Sml93401 IPC_PROJ_USAGE(perm, service) -= 1; 12132677Sml93401 IPC_ZONE_USAGE(perm, service) -= 1; 12142677Sml93401 ASSERT(service->ipcs_count || ((IPC_PROJ_USAGE(perm, service) == 0) && 12152677Sml93401 (IPC_ZONE_USAGE(perm, service) == 0))); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate /* 12200Sstevel@tonic-gate * Common code to perform an IPC_RMID. Returns an errno value on 12210Sstevel@tonic-gate * failure, 0 on success. 12220Sstevel@tonic-gate */ 12230Sstevel@tonic-gate int 12240Sstevel@tonic-gate ipc_rmid(ipc_service_t *service, int id, cred_t *cr) 12250Sstevel@tonic-gate { 12260Sstevel@tonic-gate kipc_perm_t *perm; 12270Sstevel@tonic-gate kmutex_t *lock; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 12300Sstevel@tonic-gate 12310Sstevel@tonic-gate lock = ipc_lookup(service, id, &perm); 12320Sstevel@tonic-gate if (lock == NULL) { 12330Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 12340Sstevel@tonic-gate return (EINVAL); 12350Sstevel@tonic-gate } 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate ASSERT(service->ipcs_count > 0); 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate if (secpolicy_ipc_owner(cr, perm) != 0) { 12400Sstevel@tonic-gate mutex_exit(lock); 12410Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 12420Sstevel@tonic-gate return (EPERM); 12430Sstevel@tonic-gate } 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate /* 12460Sstevel@tonic-gate * Nothing can fail from this point on. 12470Sstevel@tonic-gate */ 12480Sstevel@tonic-gate ipc_remove(service, perm); 12490Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate /* perform any per-service removal actions */ 12520Sstevel@tonic-gate service->ipcs_rmid(perm); 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate ipc_rele(service, perm); 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate return (0); 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate /* 12600Sstevel@tonic-gate * Implementation for shmids, semids, and msgids. buf is the address 12610Sstevel@tonic-gate * of the user buffer, nids is the size, and pnids is a pointer to 12620Sstevel@tonic-gate * where we write the actual number of ids that [would] have been 12630Sstevel@tonic-gate * copied out. 12640Sstevel@tonic-gate */ 12650Sstevel@tonic-gate int 12660Sstevel@tonic-gate ipc_ids(ipc_service_t *service, int *buf, uint_t nids, uint_t *pnids) 12670Sstevel@tonic-gate { 12680Sstevel@tonic-gate kipc_perm_t *perm; 12690Sstevel@tonic-gate size_t idsize = 0; 12700Sstevel@tonic-gate int error = 0; 12710Sstevel@tonic-gate int idcount; 12720Sstevel@tonic-gate int *ids; 12730Sstevel@tonic-gate int numids = 0; 12740Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 12750Sstevel@tonic-gate int global = INGLOBALZONE(curproc); 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate if (buf == NULL) 12780Sstevel@tonic-gate nids = 0; 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate /* 12810Sstevel@tonic-gate * Get an accurate count of the total number of ids, and allocate a 12820Sstevel@tonic-gate * staging buffer. Since ipcs_count is always sane, we don't have 12830Sstevel@tonic-gate * to take ipcs_lock for our first guess. If there are no ids, or 12840Sstevel@tonic-gate * we're in the global zone and the number of ids is greater than 12850Sstevel@tonic-gate * the size of the specified buffer, we shunt to the end. Otherwise, 12860Sstevel@tonic-gate * we go through the id list looking for (and counting) what is 12870Sstevel@tonic-gate * visible in the specified zone. 12880Sstevel@tonic-gate */ 12890Sstevel@tonic-gate idcount = service->ipcs_count; 12900Sstevel@tonic-gate for (;;) { 12910Sstevel@tonic-gate if ((global && idcount > nids) || idcount == 0) { 12920Sstevel@tonic-gate numids = idcount; 12930Sstevel@tonic-gate nids = 0; 12940Sstevel@tonic-gate goto out; 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate idsize = idcount * sizeof (int); 12980Sstevel@tonic-gate ids = kmem_alloc(idsize, KM_SLEEP); 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 13010Sstevel@tonic-gate if (idcount >= service->ipcs_count) 13020Sstevel@tonic-gate break; 13030Sstevel@tonic-gate idcount = service->ipcs_count; 13040Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate if (idsize != 0) { 13070Sstevel@tonic-gate kmem_free(ids, idsize); 13080Sstevel@tonic-gate idsize = 0; 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate for (perm = list_head(&service->ipcs_usedids); perm != NULL; 13130Sstevel@tonic-gate perm = list_next(&service->ipcs_usedids, perm)) { 13140Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 13150Sstevel@tonic-gate if (global || perm->ipc_zoneid == zoneid) 13160Sstevel@tonic-gate ids[numids++] = perm->ipc_id; 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate /* 13210Sstevel@tonic-gate * If there isn't enough space to hold all of the ids, just 13220Sstevel@tonic-gate * return the number of ids without copying out any of them. 13230Sstevel@tonic-gate */ 13240Sstevel@tonic-gate if (nids < numids) 13250Sstevel@tonic-gate nids = 0; 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate out: 13280Sstevel@tonic-gate if (suword32(pnids, (uint32_t)numids) || 13290Sstevel@tonic-gate (nids != 0 && copyout(ids, buf, numids * sizeof (int)))) 13300Sstevel@tonic-gate error = EFAULT; 13310Sstevel@tonic-gate if (idsize != 0) 13320Sstevel@tonic-gate kmem_free(ids, idsize); 13330Sstevel@tonic-gate return (error); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * Destroy IPC objects from the given service that are associated with 13380Sstevel@tonic-gate * the given zone. 13390Sstevel@tonic-gate * 13400Sstevel@tonic-gate * We can't hold on to the service lock when freeing objects, so we 13410Sstevel@tonic-gate * first search the service and move all the objects to a private 13420Sstevel@tonic-gate * list, then walk through and free them after dropping the lock. 13430Sstevel@tonic-gate */ 13440Sstevel@tonic-gate void 13450Sstevel@tonic-gate ipc_remove_zone(ipc_service_t *service, zoneid_t zoneid) 13460Sstevel@tonic-gate { 13470Sstevel@tonic-gate kipc_perm_t *perm, *next; 13480Sstevel@tonic-gate list_t rmlist; 13490Sstevel@tonic-gate kmutex_t *lock; 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate list_create(&rmlist, sizeof (kipc_perm_t), 13520Sstevel@tonic-gate offsetof(kipc_perm_t, ipc_list)); 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 13550Sstevel@tonic-gate for (perm = list_head(&service->ipcs_usedids); perm != NULL; 13560Sstevel@tonic-gate perm = next) { 13570Sstevel@tonic-gate next = list_next(&service->ipcs_usedids, perm); 13580Sstevel@tonic-gate if (perm->ipc_zoneid != zoneid) 13590Sstevel@tonic-gate continue; 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate /* 13620Sstevel@tonic-gate * Remove the object from the service, then put it on 13630Sstevel@tonic-gate * the removal list so we can defer the call to 13640Sstevel@tonic-gate * ipc_rele (which will actually free the structure). 13650Sstevel@tonic-gate * We need to do this since the destructor may grab 13660Sstevel@tonic-gate * the service lock. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 13690Sstevel@tonic-gate lock = ipc_lock(service, perm->ipc_id); 13700Sstevel@tonic-gate ipc_remove(service, perm); 13710Sstevel@tonic-gate mutex_exit(lock); 13720Sstevel@tonic-gate list_insert_tail(&rmlist, perm); 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate /* 13770Sstevel@tonic-gate * Now that we've dropped the service lock, loop through the 13780Sstevel@tonic-gate * private list freeing removed objects. 13790Sstevel@tonic-gate */ 13800Sstevel@tonic-gate for (perm = list_head(&rmlist); perm != NULL; perm = next) { 13810Sstevel@tonic-gate next = list_next(&rmlist, perm); 13820Sstevel@tonic-gate list_remove(&rmlist, perm); 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate (void) ipc_lock(service, perm->ipc_id); 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* perform any per-service removal actions */ 13870Sstevel@tonic-gate service->ipcs_rmid(perm); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate /* release reference */ 13900Sstevel@tonic-gate ipc_rele(service, perm); 13910Sstevel@tonic-gate } 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate list_destroy(&rmlist); 13940Sstevel@tonic-gate } 1395