17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5824c205fSml93401 * Common Development and Distribution License (the "License"). 6824c205fSml93401 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*005d3febSMarek Pospisil * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * Common Inter-Process Communication routines. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Overview 347c478bd9Sstevel@tonic-gate * -------- 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * The System V inter-process communication (IPC) facilities provide 377c478bd9Sstevel@tonic-gate * three services, message queues, semaphore arrays, and shared memory 387c478bd9Sstevel@tonic-gate * segments, which are mananged using filesystem-like namespaces. 397c478bd9Sstevel@tonic-gate * Unlike a filesystem, these namespaces aren't mounted and accessible 407c478bd9Sstevel@tonic-gate * via a path -- a special API is used to interact with the different 417c478bd9Sstevel@tonic-gate * facilities (nothing precludes a VFS-based interface, but the 427c478bd9Sstevel@tonic-gate * standards require the special APIs). Furthermore, these special 437c478bd9Sstevel@tonic-gate * APIs don't use file descriptors, nor do they have an equivalent. 447c478bd9Sstevel@tonic-gate * This means that every operation which acts on an object needs to 457c478bd9Sstevel@tonic-gate * perform the quivalent of a lookup, which in turn means that every 467c478bd9Sstevel@tonic-gate * operation can fail if the specified object doesn't exist in the 477c478bd9Sstevel@tonic-gate * facility's namespace. 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * Objects 507c478bd9Sstevel@tonic-gate * ------- 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * Each object in a namespace has a unique ID, which is assigned by the 537c478bd9Sstevel@tonic-gate * system and is used to identify the object when performing operations 547c478bd9Sstevel@tonic-gate * on it. An object can also have a key, which is selected by the user 557c478bd9Sstevel@tonic-gate * at allocation time and is used as a primitive rendezvous mechanism. 567c478bd9Sstevel@tonic-gate * An object without a key is said to have a "private" key. 577c478bd9Sstevel@tonic-gate * 587c478bd9Sstevel@tonic-gate * To perform an operation on an object given its key, one must first 597c478bd9Sstevel@tonic-gate * perform a lookup and obtain its ID. The ID is then used to identify 607c478bd9Sstevel@tonic-gate * the object when performing the operation. If the object has a 617c478bd9Sstevel@tonic-gate * private key, the ID must be known or obtained by other means. 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * Each object in the namespace has a creator uid and gid, as well as 647c478bd9Sstevel@tonic-gate * an owner uid and gid. Both are initialized with the ruid and rgid 657c478bd9Sstevel@tonic-gate * of the process which created the object. The creator or current 667c478bd9Sstevel@tonic-gate * owner has the ability to change the owner of the object. 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * Each object in the namespace has a set of file-like permissions, 697c478bd9Sstevel@tonic-gate * which, in conjunction with the creator and owner uid and gid, 707c478bd9Sstevel@tonic-gate * control read and write access to the object (execute is ignored). 717c478bd9Sstevel@tonic-gate * 72824c205fSml93401 * Each object also has a creator project and zone, which are used to 73824c205fSml93401 * account for its resource usage. 747c478bd9Sstevel@tonic-gate * 757c478bd9Sstevel@tonic-gate * Operations 767c478bd9Sstevel@tonic-gate * ---------- 777c478bd9Sstevel@tonic-gate * 787c478bd9Sstevel@tonic-gate * There are five operations which all three facilities have in 797c478bd9Sstevel@tonic-gate * common: GET, SET, STAT, RMID, and IDS. 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * GET, like open, is used to allocate a new object or obtain an 827c478bd9Sstevel@tonic-gate * existing one (using its key). It takes a key, a set of flags and 837c478bd9Sstevel@tonic-gate * mode bits, and optionally facility-specific arguments. If the key 847c478bd9Sstevel@tonic-gate * is IPC_PRIVATE, a new object with the requested mode bits and 857c478bd9Sstevel@tonic-gate * facility-specific attributes is created. If the key isn't 867c478bd9Sstevel@tonic-gate * IPC_PRIVATE, the GET will attempt to look up the specified key and 877c478bd9Sstevel@tonic-gate * either return that or create a new key depending on the state of the 887c478bd9Sstevel@tonic-gate * IPC_CREAT and IPC_EXCL flags, much like open. If GET needs to 897c478bd9Sstevel@tonic-gate * allocate an object, it can fail if there is insufficient space in 907c478bd9Sstevel@tonic-gate * the namespace (the maximum number of ids for the facility has been 917c478bd9Sstevel@tonic-gate * exceeded) or if the facility-specific initialization fails. If GET 927c478bd9Sstevel@tonic-gate * finds an object it can return, it can still fail if that object's 937c478bd9Sstevel@tonic-gate * permissions or facility-specific attributes are less than those 947c478bd9Sstevel@tonic-gate * requested. 957c478bd9Sstevel@tonic-gate * 967c478bd9Sstevel@tonic-gate * SET is used to adjust facility-specific parameters of an object, in 977c478bd9Sstevel@tonic-gate * addition to the owner uid and gid, and mode bits. It can fail if 987c478bd9Sstevel@tonic-gate * the caller isn't the creator or owner. 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * STAT is used to obtain information about an object including the 1017c478bd9Sstevel@tonic-gate * general attributes object described as well as facility-specific 1027c478bd9Sstevel@tonic-gate * information. It can fail if the caller doesn't have read 1037c478bd9Sstevel@tonic-gate * permission. 1047c478bd9Sstevel@tonic-gate * 1057c478bd9Sstevel@tonic-gate * RMID removes an object from the namespace. Subsequent operations 1067c478bd9Sstevel@tonic-gate * using the object's ID or key will fail (until another object is 1077c478bd9Sstevel@tonic-gate * created with the same key or ID). Since an RMID may be performed 1087c478bd9Sstevel@tonic-gate * asynchronously with other operations, it is possible that other 1097c478bd9Sstevel@tonic-gate * threads and/or processes will have references to the object. While 1107c478bd9Sstevel@tonic-gate * a facility may have actions which need to be performed at RMID time, 1117c478bd9Sstevel@tonic-gate * only when all references are dropped can the object be destroyed. 1127c478bd9Sstevel@tonic-gate * RMID will fail if the caller isn't the creator or owner. 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * IDS obtains a list of all IDs in a facility's namespace. There are 1157c478bd9Sstevel@tonic-gate * no facility-specific behaviors of IDS. 1167c478bd9Sstevel@tonic-gate * 1177c478bd9Sstevel@tonic-gate * Design 1187c478bd9Sstevel@tonic-gate * ------ 1197c478bd9Sstevel@tonic-gate * 1207c478bd9Sstevel@tonic-gate * Because some IPC facilities provide services whose operations must 1217c478bd9Sstevel@tonic-gate * scale, a mechanism which allows fast, concurrent access to 1227c478bd9Sstevel@tonic-gate * individual objects is needed. Of primary importance is object 1237c478bd9Sstevel@tonic-gate * lookup based on ID (SET, STAT, others). Allocation (GET), 1247c478bd9Sstevel@tonic-gate * deallocation (RMID), ID enumeration (IDS), and key lookups (GET) are 1257c478bd9Sstevel@tonic-gate * lesser concerns, but should be implemented in such a way that ID 1267c478bd9Sstevel@tonic-gate * lookup isn't affected (at least not in the common case). 1277c478bd9Sstevel@tonic-gate * 1287c478bd9Sstevel@tonic-gate * Starting from the bottom up, each object is represented by a 1297c478bd9Sstevel@tonic-gate * structure, the first member of which must be a kipc_perm_t. The 1307c478bd9Sstevel@tonic-gate * kipc_perm_t contains the information described above in "Objects", a 1317c478bd9Sstevel@tonic-gate * reference count (since the object may continue to exist after it has 1327c478bd9Sstevel@tonic-gate * been removed from the namespace), as well as some additional 1337c478bd9Sstevel@tonic-gate * metadata used to manage data structure membership. These objects 1347c478bd9Sstevel@tonic-gate * are dynamically allocated. 1357c478bd9Sstevel@tonic-gate * 1367c478bd9Sstevel@tonic-gate * Above the objects is a power-of-two sized table of ID slots. Each 1377c478bd9Sstevel@tonic-gate * slot contains a pointer to an object, a sequence number, and a 1387c478bd9Sstevel@tonic-gate * lock. An object's ID is a function of its slot's index in the table 1397c478bd9Sstevel@tonic-gate * and its slot's sequence number. Every time a slot is released (via 1407c478bd9Sstevel@tonic-gate * RMID) its sequence number is increased. Strictly speaking, the 1417c478bd9Sstevel@tonic-gate * sequence number is unnecessary. However, checking the sequence 1427c478bd9Sstevel@tonic-gate * number after a lookup provides a certain degree of robustness 1437c478bd9Sstevel@tonic-gate * against the use of stale IDs (useful since nothing else does). When 1447c478bd9Sstevel@tonic-gate * the table fills up, it is resized (see Locking, below). 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * Of an ID's 31 bits (an ID is, as defined by the standards, a signed 1477c478bd9Sstevel@tonic-gate * int) the top IPC_SEQ_BITS are used for the sequence number with the 1487c478bd9Sstevel@tonic-gate * remainder holding the index into the table. The size of the table 1497c478bd9Sstevel@tonic-gate * is therefore bounded at 2 ^ (31 - IPC_SEQ_BITS) slots. 1507c478bd9Sstevel@tonic-gate * 1517c478bd9Sstevel@tonic-gate * Managing this table is the ipc_service structure. It contains a 1527c478bd9Sstevel@tonic-gate * pointer to the dynamically allocated ID table, a namespace-global 1537c478bd9Sstevel@tonic-gate * lock, an id_space for managing the free space in the table, and 1547c478bd9Sstevel@tonic-gate * sundry other metadata necessary for the maintenance of the 1557c478bd9Sstevel@tonic-gate * namespace. An AVL tree of all keyed objects in the table (sorted by 1567c478bd9Sstevel@tonic-gate * key) is used for key lookups. An unordered doubly linked list of 1577c478bd9Sstevel@tonic-gate * all objects in the namespace (keyed or not) is maintained to 1587c478bd9Sstevel@tonic-gate * facilitate ID enumeration. 1597c478bd9Sstevel@tonic-gate * 1607c478bd9Sstevel@tonic-gate * To help visualize these relationships, here's a picture of a 1617c478bd9Sstevel@tonic-gate * namespace with a table of size 8 containing three objects 1627c478bd9Sstevel@tonic-gate * (IPC_SEQ_BITS = 28): 1637c478bd9Sstevel@tonic-gate * 1647c478bd9Sstevel@tonic-gate * 1657c478bd9Sstevel@tonic-gate * +-ipc_service_t--+ 1667c478bd9Sstevel@tonic-gate * | table *---\ 1677c478bd9Sstevel@tonic-gate * | keys *---+----------------------\ 1687c478bd9Sstevel@tonic-gate * | all ids *--\| | 1697c478bd9Sstevel@tonic-gate * | | || | 1707c478bd9Sstevel@tonic-gate * +----------------+ || | 1717c478bd9Sstevel@tonic-gate * || | 1727c478bd9Sstevel@tonic-gate * /-------------------/| | 1737c478bd9Sstevel@tonic-gate * | /---------------/ | 1747c478bd9Sstevel@tonic-gate * | | | 1757c478bd9Sstevel@tonic-gate * | v | 1767c478bd9Sstevel@tonic-gate * | +-0------+-1------+-2------+-3------+-4--+---+-5------+-6------+-7------+ 1777c478bd9Sstevel@tonic-gate * | | Seq=3 | | | Seq=1 | : | | | Seq=6 | 1787c478bd9Sstevel@tonic-gate * | | | | | | : | | | | 1797c478bd9Sstevel@tonic-gate * | +-*------+--------+--------+-*------+----+---+--------+--------+-*------+ 1807c478bd9Sstevel@tonic-gate * | | | | | 1817c478bd9Sstevel@tonic-gate * | | /---/ | /----------------/ 1827c478bd9Sstevel@tonic-gate * | | | | | 1837c478bd9Sstevel@tonic-gate * | v v | v 1847c478bd9Sstevel@tonic-gate * | +-kipc_perm_t-+ +-kipc_perm_t-+ | +-kipc_perm_t-+ 1857c478bd9Sstevel@tonic-gate * | | id=0x30 | | id=0x13 | | | id=0x67 | 1867c478bd9Sstevel@tonic-gate * | | key=0xfeed | | key=0xbeef | | | key=0xcafe | 1877c478bd9Sstevel@tonic-gate * \->| [list] |<------>| [list] |<------>| [list] | 1887c478bd9Sstevel@tonic-gate * /->| [avl left] x /--->| [avl left] x \--->| [avl left] *---\ 1897c478bd9Sstevel@tonic-gate * | | [avl right] x | | [avl right] x | [avl right] *---+-\ 1907c478bd9Sstevel@tonic-gate * | | | | | | | | | | 1917c478bd9Sstevel@tonic-gate * | +-------------+ | +-------------+ +-------------+ | | 1927c478bd9Sstevel@tonic-gate * | \---------------------------------------------/ | 1937c478bd9Sstevel@tonic-gate * \--------------------------------------------------------------------/ 1947c478bd9Sstevel@tonic-gate * 1957c478bd9Sstevel@tonic-gate * Locking 1967c478bd9Sstevel@tonic-gate * ------- 1977c478bd9Sstevel@tonic-gate * 1987c478bd9Sstevel@tonic-gate * There are three locks (or sets of locks) which are used to ensure 1997c478bd9Sstevel@tonic-gate * correctness: the slot locks, the namespace lock, and p_lock (needed 2007c478bd9Sstevel@tonic-gate * when checking resource controls). Their ordering is 2017c478bd9Sstevel@tonic-gate * 2027c478bd9Sstevel@tonic-gate * namespace lock -> slot lock 0 -> ... -> slot lock t -> p_lock 2037c478bd9Sstevel@tonic-gate * 2047c478bd9Sstevel@tonic-gate * Generally speaking, the namespace lock is used to protect allocation 2057c478bd9Sstevel@tonic-gate * and removal from the namespace, ID enumeration, and resizing the ID 2067c478bd9Sstevel@tonic-gate * table. Specifically: 2077c478bd9Sstevel@tonic-gate * 2087c478bd9Sstevel@tonic-gate * - write access to all fields of the ipc_service structure 2097c478bd9Sstevel@tonic-gate * - read access to all variable fields of ipc_service except 2107c478bd9Sstevel@tonic-gate * ipcs_tabsz (table size) and ipcs_table (the table pointer) 2117c478bd9Sstevel@tonic-gate * - read/write access to ipc_avl, ipc_list in visible objects' 2127c478bd9Sstevel@tonic-gate * kipc_perm structures (i.e. objects which have been removed from 2137c478bd9Sstevel@tonic-gate * the namespace don't have this restriction) 2147c478bd9Sstevel@tonic-gate * - write access to ipct_seq and ipct_data in the table entries 2157c478bd9Sstevel@tonic-gate * 2167c478bd9Sstevel@tonic-gate * A slot lock by itself is meaningless (except when resizing). Of 2177c478bd9Sstevel@tonic-gate * greater interest conceptually is the notion of an ID lock -- a 2187c478bd9Sstevel@tonic-gate * "virtual lock" which refers to whichever slot lock an object's ID 2197c478bd9Sstevel@tonic-gate * currently hashes to. 2207c478bd9Sstevel@tonic-gate * 2217c478bd9Sstevel@tonic-gate * An ID lock protects all objects with that ID. Normally there will 2227c478bd9Sstevel@tonic-gate * only be one such object: the one pointed to by the locked slot. 2237c478bd9Sstevel@tonic-gate * However, if an object is removed from the namespace but retains 2247c478bd9Sstevel@tonic-gate * references (e.g. an attached shared memory segment which has been 2257c478bd9Sstevel@tonic-gate * RMIDed), it continues to use the lock associated with its original 2267c478bd9Sstevel@tonic-gate * ID. While this can result in increased contention, operations which 2277c478bd9Sstevel@tonic-gate * require taking the ID lock of removed objects are infrequent. 2287c478bd9Sstevel@tonic-gate * 2297c478bd9Sstevel@tonic-gate * Specifically, an ID lock protects the contents of an object's 2307c478bd9Sstevel@tonic-gate * structure, including the contents of the embedded kipc_perm 2317c478bd9Sstevel@tonic-gate * structure (but excluding those fields protected by the namespace 2327c478bd9Sstevel@tonic-gate * lock). It also protects the ipct_seq and ipct_data fields in its 2337c478bd9Sstevel@tonic-gate * slot (it is really a slot lock, after all). 2347c478bd9Sstevel@tonic-gate * 2357c478bd9Sstevel@tonic-gate * Recall that the table is resizable. To avoid requiring every ID 2367c478bd9Sstevel@tonic-gate * lookup to take a global lock, a scheme much like that employed for 2377c478bd9Sstevel@tonic-gate * file descriptors (see the comment above UF_ENTER in user.h) is 2387c478bd9Sstevel@tonic-gate * used. Note that the sequence number and data pointer are protected 2397c478bd9Sstevel@tonic-gate * by both the namespace lock and their slot lock. When the table is 2407c478bd9Sstevel@tonic-gate * resized, the following operations take place: 2417c478bd9Sstevel@tonic-gate * 2427c478bd9Sstevel@tonic-gate * 1) A new table is allocated. 2437c478bd9Sstevel@tonic-gate * 2) The global lock is taken. 2447c478bd9Sstevel@tonic-gate * 3) All old slots are locked, in order. 2457c478bd9Sstevel@tonic-gate * 4) The first half of the new slots are locked. 2467c478bd9Sstevel@tonic-gate * 5) All table entries are copied to the new table, and cleared from 2477c478bd9Sstevel@tonic-gate * the old table. 2487c478bd9Sstevel@tonic-gate * 6) The ipc_service structure is updated to point to the new table. 2497c478bd9Sstevel@tonic-gate * 7) The ipc_service structure is updated with the new table size. 2507c478bd9Sstevel@tonic-gate * 8) All slot locks (old and new) are dropped. 2517c478bd9Sstevel@tonic-gate * 2527c478bd9Sstevel@tonic-gate * Because the slot locks are embedded in the table, ID lookups and 2537c478bd9Sstevel@tonic-gate * other operations which require taking an slot lock need to verify 2547c478bd9Sstevel@tonic-gate * that the lock taken wasn't part of a stale table. This is 2557c478bd9Sstevel@tonic-gate * accomplished by checking the table size before and after 2567c478bd9Sstevel@tonic-gate * dereferencing the table pointer and taking the lock: if the size 2577c478bd9Sstevel@tonic-gate * changes, the lock must be dropped and reacquired. It is this 2587c478bd9Sstevel@tonic-gate * additional work which distinguishes an ID lock from a slot lock. 2597c478bd9Sstevel@tonic-gate * 2607c478bd9Sstevel@tonic-gate * Because we can't guarantee that threads aren't accessing the old 2617c478bd9Sstevel@tonic-gate * tables' locks, they are never deallocated. To prevent spurious 2627c478bd9Sstevel@tonic-gate * reports of memory leaks, a pointer to the discarded table is stored 2637c478bd9Sstevel@tonic-gate * in the new one in step 5. (Theoretically ipcs_destroy will delete 2647c478bd9Sstevel@tonic-gate * the discarded tables, but it is only ever called from a failed _init 2657c478bd9Sstevel@tonic-gate * invocation; i.e. when there aren't any.) 2667c478bd9Sstevel@tonic-gate * 2677c478bd9Sstevel@tonic-gate * Interfaces 2687c478bd9Sstevel@tonic-gate * ---------- 2697c478bd9Sstevel@tonic-gate * 2707c478bd9Sstevel@tonic-gate * The following interfaces are provided by the ipc module for use by 2717c478bd9Sstevel@tonic-gate * the individual IPC facilities: 2727c478bd9Sstevel@tonic-gate * 2737c478bd9Sstevel@tonic-gate * ipcperm_access 2747c478bd9Sstevel@tonic-gate * 2757c478bd9Sstevel@tonic-gate * Given an object and a cred structure, determines if the requested 2767c478bd9Sstevel@tonic-gate * access type is allowed. 2777c478bd9Sstevel@tonic-gate * 2787c478bd9Sstevel@tonic-gate * ipcperm_set, ipcperm_stat, 2797c478bd9Sstevel@tonic-gate * ipcperm_set64, ipcperm_stat64 2807c478bd9Sstevel@tonic-gate * 2817c478bd9Sstevel@tonic-gate * Performs the common portion of an STAT or SET operation. All 2827c478bd9Sstevel@tonic-gate * (except stat and stat64) can fail, so they should be called before 2837c478bd9Sstevel@tonic-gate * any facility-specific non-reversible changes are made to an 2847c478bd9Sstevel@tonic-gate * object. Similarly, the set operations have side effects, so they 2857c478bd9Sstevel@tonic-gate * should only be called once the possibility of a facility-specific 2867c478bd9Sstevel@tonic-gate * failure is eliminated. 2877c478bd9Sstevel@tonic-gate * 2887c478bd9Sstevel@tonic-gate * ipcs_create 2897c478bd9Sstevel@tonic-gate * 2907c478bd9Sstevel@tonic-gate * Creates an IPC namespace for use by an IPC facility. 2917c478bd9Sstevel@tonic-gate * 2927c478bd9Sstevel@tonic-gate * ipcs_destroy 2937c478bd9Sstevel@tonic-gate * 2947c478bd9Sstevel@tonic-gate * Destroys an IPC namespace. 2957c478bd9Sstevel@tonic-gate * 2967c478bd9Sstevel@tonic-gate * ipcs_lock, ipcs_unlock 2977c478bd9Sstevel@tonic-gate * 2987c478bd9Sstevel@tonic-gate * Takes the namespace lock. Ideally such access wouldn't be 2997c478bd9Sstevel@tonic-gate * necessary, but there may be facility-specific data protected by 3007c478bd9Sstevel@tonic-gate * this lock (e.g. project-wide resource consumption). 3017c478bd9Sstevel@tonic-gate * 3027c478bd9Sstevel@tonic-gate * ipc_lock 3037c478bd9Sstevel@tonic-gate * 3047c478bd9Sstevel@tonic-gate * Takes the lock associated with an ID. Can't fail. 3057c478bd9Sstevel@tonic-gate * 3067c478bd9Sstevel@tonic-gate * ipc_relock 3077c478bd9Sstevel@tonic-gate * 3087c478bd9Sstevel@tonic-gate * Like ipc_lock, but takes a pointer to a held lock. Drops the lock 3097c478bd9Sstevel@tonic-gate * unless it is the one that would have been returned by ipc_lock. 3107c478bd9Sstevel@tonic-gate * Used after calls to cv_wait. 3117c478bd9Sstevel@tonic-gate * 3127c478bd9Sstevel@tonic-gate * ipc_lookup 3137c478bd9Sstevel@tonic-gate * 3147c478bd9Sstevel@tonic-gate * Performs an ID lookup, returns with the ID lock held. Fails if 3157c478bd9Sstevel@tonic-gate * the ID doesn't exist in the namespace. 3167c478bd9Sstevel@tonic-gate * 3177c478bd9Sstevel@tonic-gate * ipc_hold 3187c478bd9Sstevel@tonic-gate * 3197c478bd9Sstevel@tonic-gate * Takes a reference on an object. 3207c478bd9Sstevel@tonic-gate * 3217c478bd9Sstevel@tonic-gate * ipc_rele 3227c478bd9Sstevel@tonic-gate * 3237c478bd9Sstevel@tonic-gate * Releases a reference on an object, and drops the object's lock. 3247c478bd9Sstevel@tonic-gate * Calls the object's destructor if last reference is being 3257c478bd9Sstevel@tonic-gate * released. 3267c478bd9Sstevel@tonic-gate * 3277c478bd9Sstevel@tonic-gate * ipc_rele_locked 3287c478bd9Sstevel@tonic-gate * 3297c478bd9Sstevel@tonic-gate * Releases a reference on an object. Doesn't drop lock, and may 3307c478bd9Sstevel@tonic-gate * only be called when there is more than one reference to the 3317c478bd9Sstevel@tonic-gate * object. 3327c478bd9Sstevel@tonic-gate * 3337c478bd9Sstevel@tonic-gate * ipc_get, ipc_commit_begin, ipc_commit_end, ipc_cleanup 3347c478bd9Sstevel@tonic-gate * 3357c478bd9Sstevel@tonic-gate * Components of a GET operation. ipc_get performs a key lookup, 3367c478bd9Sstevel@tonic-gate * allocating an object if the key isn't found (returning with the 3377c478bd9Sstevel@tonic-gate * namespace lock and p_lock held), and returning the existing object 3387c478bd9Sstevel@tonic-gate * if it is (with the object lock held). ipc_get doesn't modify the 3397c478bd9Sstevel@tonic-gate * namespace. 3407c478bd9Sstevel@tonic-gate * 3417c478bd9Sstevel@tonic-gate * ipc_commit_begin begins the process of inserting an object 3427c478bd9Sstevel@tonic-gate * allocated by ipc_get into the namespace, and can fail. If 3437c478bd9Sstevel@tonic-gate * successful, it returns with the namespace lock and p_lock held. 3447c478bd9Sstevel@tonic-gate * ipc_commit_end completes the process of inserting an object into 3457c478bd9Sstevel@tonic-gate * the namespace and can't fail. The facility can call ipc_cleanup 3467c478bd9Sstevel@tonic-gate * at any time following a successful ipc_get and before 3477c478bd9Sstevel@tonic-gate * ipc_commit_end or a failed ipc_commit_begin to fail the 3487c478bd9Sstevel@tonic-gate * allocation. Pseudocode for the suggested GET implementation: 3497c478bd9Sstevel@tonic-gate * 3507c478bd9Sstevel@tonic-gate * top: 3517c478bd9Sstevel@tonic-gate * 3527c478bd9Sstevel@tonic-gate * ipc_get 3537c478bd9Sstevel@tonic-gate * 3547c478bd9Sstevel@tonic-gate * if failure 3557c478bd9Sstevel@tonic-gate * return 3567c478bd9Sstevel@tonic-gate * 3577c478bd9Sstevel@tonic-gate * if found { 3587c478bd9Sstevel@tonic-gate * 3597c478bd9Sstevel@tonic-gate * if object meets criteria 3607c478bd9Sstevel@tonic-gate * unlock object and return success 3617c478bd9Sstevel@tonic-gate * else 3627c478bd9Sstevel@tonic-gate * unlock object and return failure 3637c478bd9Sstevel@tonic-gate * 3647c478bd9Sstevel@tonic-gate * } else { 3657c478bd9Sstevel@tonic-gate * 3667c478bd9Sstevel@tonic-gate * perform resource control tests 3677c478bd9Sstevel@tonic-gate * drop namespace lock, p_lock 3687c478bd9Sstevel@tonic-gate * if failure 3697c478bd9Sstevel@tonic-gate * ipc_cleanup 3707c478bd9Sstevel@tonic-gate * 3717c478bd9Sstevel@tonic-gate * perform facility-specific initialization 3727c478bd9Sstevel@tonic-gate * if failure { 3737c478bd9Sstevel@tonic-gate * facility-specific cleanup 3747c478bd9Sstevel@tonic-gate * ipc_cleanup 3757c478bd9Sstevel@tonic-gate * } 3767c478bd9Sstevel@tonic-gate * 3777c478bd9Sstevel@tonic-gate * ( At this point the object should be destructible using the 3787c478bd9Sstevel@tonic-gate * destructor given to ipcs_create ) 3797c478bd9Sstevel@tonic-gate * 3807c478bd9Sstevel@tonic-gate * ipc_commit_begin 3817c478bd9Sstevel@tonic-gate * if retry 3827c478bd9Sstevel@tonic-gate * goto top 3837c478bd9Sstevel@tonic-gate * else if failure 3847c478bd9Sstevel@tonic-gate * return 3857c478bd9Sstevel@tonic-gate * 3867c478bd9Sstevel@tonic-gate * perform facility-specific resource control tests/allocations 3877c478bd9Sstevel@tonic-gate * if failure 3887c478bd9Sstevel@tonic-gate * ipc_cleanup 3897c478bd9Sstevel@tonic-gate * 3907c478bd9Sstevel@tonic-gate * ipc_commit_end 3917c478bd9Sstevel@tonic-gate * perform any infallible post-creation actions, unlock, and return 3927c478bd9Sstevel@tonic-gate * 3937c478bd9Sstevel@tonic-gate * } 3947c478bd9Sstevel@tonic-gate * 3957c478bd9Sstevel@tonic-gate * ipc_rmid 3967c478bd9Sstevel@tonic-gate * 3977c478bd9Sstevel@tonic-gate * Performs the common portion of an RMID operation -- looks up an ID 3987c478bd9Sstevel@tonic-gate * removes it, and calls the a facility-specific function to do 3997c478bd9Sstevel@tonic-gate * RMID-time cleanup on the private portions of the object. 4007c478bd9Sstevel@tonic-gate * 4017c478bd9Sstevel@tonic-gate * ipc_ids 4027c478bd9Sstevel@tonic-gate * 4037c478bd9Sstevel@tonic-gate * Performs the common portion of an IDS operation. 4047c478bd9Sstevel@tonic-gate * 4057c478bd9Sstevel@tonic-gate */ 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate #include <sys/types.h> 4087c478bd9Sstevel@tonic-gate #include <sys/param.h> 4097c478bd9Sstevel@tonic-gate #include <sys/cred.h> 4107c478bd9Sstevel@tonic-gate #include <sys/policy.h> 4117c478bd9Sstevel@tonic-gate #include <sys/proc.h> 4127c478bd9Sstevel@tonic-gate #include <sys/user.h> 4137c478bd9Sstevel@tonic-gate #include <sys/ipc.h> 4147c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h> 4157c478bd9Sstevel@tonic-gate #include <sys/errno.h> 4167c478bd9Sstevel@tonic-gate #include <sys/systm.h> 4177c478bd9Sstevel@tonic-gate #include <sys/list.h> 4187c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 4197c478bd9Sstevel@tonic-gate #include <sys/zone.h> 4207c478bd9Sstevel@tonic-gate #include <sys/task.h> 4217c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate #include <c2/audit.h> 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = { 4267c478bd9Sstevel@tonic-gate &mod_miscops, 4277c478bd9Sstevel@tonic-gate "common ipc code", 4287c478bd9Sstevel@tonic-gate }; 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 4317c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlmisc, NULL 4327c478bd9Sstevel@tonic-gate }; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate int 4367c478bd9Sstevel@tonic-gate _init(void) 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate int 4427c478bd9Sstevel@tonic-gate _fini(void) 4437c478bd9Sstevel@tonic-gate { 4447c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate int 4487c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 4497c478bd9Sstevel@tonic-gate { 4507c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate /* 4557c478bd9Sstevel@tonic-gate * Check message, semaphore, or shared memory access permissions. 4567c478bd9Sstevel@tonic-gate * 4577c478bd9Sstevel@tonic-gate * This routine verifies the requested access permission for the current 4587c478bd9Sstevel@tonic-gate * process. The zone ids are compared, and the appropriate bits are 4597c478bd9Sstevel@tonic-gate * checked corresponding to owner, group (including the list of 4607c478bd9Sstevel@tonic-gate * supplementary groups), or everyone. Zero is returned on success. 4617c478bd9Sstevel@tonic-gate * On failure, the security policy is asked to check to override the 4627c478bd9Sstevel@tonic-gate * permissions check; the policy will either return 0 for access granted 4637c478bd9Sstevel@tonic-gate * or EACCES. 4647c478bd9Sstevel@tonic-gate * 4657c478bd9Sstevel@tonic-gate * Access to objects in other zones requires that the caller be in the 4667c478bd9Sstevel@tonic-gate * global zone and have the appropriate IPC_DAC_* privilege, regardless 4677c478bd9Sstevel@tonic-gate * of whether the uid or gid match those of the object. Note that 4687c478bd9Sstevel@tonic-gate * cross-zone accesses will normally never get here since they'll 4697c478bd9Sstevel@tonic-gate * fail in ipc_lookup or ipc_get. 4707c478bd9Sstevel@tonic-gate * 4717c478bd9Sstevel@tonic-gate * The arguments must be set up as follows: 4727c478bd9Sstevel@tonic-gate * p - Pointer to permission structure to verify 4737c478bd9Sstevel@tonic-gate * mode - Desired access permissions 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate int 4767c478bd9Sstevel@tonic-gate ipcperm_access(kipc_perm_t *p, int mode, cred_t *cr) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate int shifts = 0; 4797c478bd9Sstevel@tonic-gate uid_t uid = crgetuid(cr); 4807c478bd9Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate if (p->ipc_zoneid == zoneid) { 4837c478bd9Sstevel@tonic-gate if (uid != p->ipc_uid && uid != p->ipc_cuid) { 4847c478bd9Sstevel@tonic-gate shifts += 3; 4857c478bd9Sstevel@tonic-gate if (!groupmember(p->ipc_gid, cr) && 4867c478bd9Sstevel@tonic-gate !groupmember(p->ipc_cgid, cr)) 4877c478bd9Sstevel@tonic-gate shifts += 3; 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate mode &= ~(p->ipc_mode << shifts); 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate if (mode == 0) 4937c478bd9Sstevel@tonic-gate return (0); 4947c478bd9Sstevel@tonic-gate } else if (zoneid != GLOBAL_ZONEID) 4957c478bd9Sstevel@tonic-gate return (EACCES); 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate return (secpolicy_ipc_access(cr, p, mode)); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate /* 5017c478bd9Sstevel@tonic-gate * There are two versions of the ipcperm_set/stat functions: 5027c478bd9Sstevel@tonic-gate * ipcperm_??? - for use with IPC_SET/STAT 5037c478bd9Sstevel@tonic-gate * ipcperm_???_64 - for use with IPC_SET64/STAT64 5047c478bd9Sstevel@tonic-gate * 5057c478bd9Sstevel@tonic-gate * These functions encapsulate the common portions (copying, permission 5067c478bd9Sstevel@tonic-gate * checks, and auditing) of the set/stat operations. All, except for 5077c478bd9Sstevel@tonic-gate * stat and stat_64 which are void, return 0 on success or a non-zero 5087c478bd9Sstevel@tonic-gate * errno value on error. 5097c478bd9Sstevel@tonic-gate */ 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate int 5127c478bd9Sstevel@tonic-gate ipcperm_set(ipc_service_t *service, struct cred *cr, 5137c478bd9Sstevel@tonic-gate kipc_perm_t *kperm, struct ipc_perm *perm, model_t model) 5147c478bd9Sstevel@tonic-gate { 5157c478bd9Sstevel@tonic-gate STRUCT_HANDLE(ipc_perm, lperm); 5167c478bd9Sstevel@tonic-gate uid_t uid; 5177c478bd9Sstevel@tonic-gate gid_t gid; 5187c478bd9Sstevel@tonic-gate mode_t mode; 519bda89588Sjp151216 zone_t *zone; 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate ASSERT(IPC_LOCKED(service, kperm)); 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate STRUCT_SET_HANDLE(lperm, model, perm); 5247c478bd9Sstevel@tonic-gate uid = STRUCT_FGET(lperm, uid); 5257c478bd9Sstevel@tonic-gate gid = STRUCT_FGET(lperm, gid); 5267c478bd9Sstevel@tonic-gate mode = STRUCT_FGET(lperm, mode); 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate if (secpolicy_ipc_owner(cr, kperm) != 0) 5297c478bd9Sstevel@tonic-gate return (EPERM); 5307c478bd9Sstevel@tonic-gate 531bda89588Sjp151216 zone = crgetzone(cr); 532bda89588Sjp151216 if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone)) 5337c478bd9Sstevel@tonic-gate return (EINVAL); 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate kperm->ipc_uid = uid; 5367c478bd9Sstevel@tonic-gate kperm->ipc_gid = gid; 5377c478bd9Sstevel@tonic-gate kperm->ipc_mode = (mode & 0777) | (kperm->ipc_mode & ~0777); 5387c478bd9Sstevel@tonic-gate 539*005d3febSMarek Pospisil if (AU_AUDITING()) 5407c478bd9Sstevel@tonic-gate audit_ipcget(service->ipcs_atype, kperm); 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate return (0); 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate void 5467c478bd9Sstevel@tonic-gate ipcperm_stat(struct ipc_perm *perm, kipc_perm_t *kperm, model_t model) 5477c478bd9Sstevel@tonic-gate { 5487c478bd9Sstevel@tonic-gate STRUCT_HANDLE(ipc_perm, lperm); 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate STRUCT_SET_HANDLE(lperm, model, perm); 5517c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, uid, kperm->ipc_uid); 5527c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, gid, kperm->ipc_gid); 5537c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, cuid, kperm->ipc_cuid); 5547c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, cgid, kperm->ipc_cgid); 5557c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, mode, kperm->ipc_mode); 5567c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, seq, 0); 5577c478bd9Sstevel@tonic-gate STRUCT_FSET(lperm, key, kperm->ipc_key); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate int 5617c478bd9Sstevel@tonic-gate ipcperm_set64(ipc_service_t *service, struct cred *cr, 5627c478bd9Sstevel@tonic-gate kipc_perm_t *kperm, ipc_perm64_t *perm64) 5637c478bd9Sstevel@tonic-gate { 564bda89588Sjp151216 zone_t *zone; 565bda89588Sjp151216 5667c478bd9Sstevel@tonic-gate ASSERT(IPC_LOCKED(service, kperm)); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (secpolicy_ipc_owner(cr, kperm) != 0) 5697c478bd9Sstevel@tonic-gate return (EPERM); 5707c478bd9Sstevel@tonic-gate 571bda89588Sjp151216 zone = crgetzone(cr); 572bda89588Sjp151216 if (!VALID_UID(perm64->ipcx_uid, zone) || 573bda89588Sjp151216 !VALID_GID(perm64->ipcx_gid, zone)) 5747c478bd9Sstevel@tonic-gate return (EINVAL); 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate kperm->ipc_uid = perm64->ipcx_uid; 5777c478bd9Sstevel@tonic-gate kperm->ipc_gid = perm64->ipcx_gid; 5787c478bd9Sstevel@tonic-gate kperm->ipc_mode = (perm64->ipcx_mode & 0777) | 5797c478bd9Sstevel@tonic-gate (kperm->ipc_mode & ~0777); 5807c478bd9Sstevel@tonic-gate 581*005d3febSMarek Pospisil if (AU_AUDITING()) 5827c478bd9Sstevel@tonic-gate audit_ipcget(service->ipcs_atype, kperm); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate return (0); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate void 5887c478bd9Sstevel@tonic-gate ipcperm_stat64(ipc_perm64_t *perm64, kipc_perm_t *kperm) 5897c478bd9Sstevel@tonic-gate { 5907c478bd9Sstevel@tonic-gate perm64->ipcx_uid = kperm->ipc_uid; 5917c478bd9Sstevel@tonic-gate perm64->ipcx_gid = kperm->ipc_gid; 5927c478bd9Sstevel@tonic-gate perm64->ipcx_cuid = kperm->ipc_cuid; 5937c478bd9Sstevel@tonic-gate perm64->ipcx_cgid = kperm->ipc_cgid; 5947c478bd9Sstevel@tonic-gate perm64->ipcx_mode = kperm->ipc_mode; 5957c478bd9Sstevel@tonic-gate perm64->ipcx_key = kperm->ipc_key; 5967c478bd9Sstevel@tonic-gate perm64->ipcx_projid = kperm->ipc_proj->kpj_id; 5977c478bd9Sstevel@tonic-gate perm64->ipcx_zoneid = kperm->ipc_zoneid; 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate /* 6027c478bd9Sstevel@tonic-gate * ipc key comparator. 6037c478bd9Sstevel@tonic-gate */ 6047c478bd9Sstevel@tonic-gate static int 6057c478bd9Sstevel@tonic-gate ipc_key_compar(const void *a, const void *b) 6067c478bd9Sstevel@tonic-gate { 6077c478bd9Sstevel@tonic-gate kipc_perm_t *aperm = (kipc_perm_t *)a; 6087c478bd9Sstevel@tonic-gate kipc_perm_t *bperm = (kipc_perm_t *)b; 6097c478bd9Sstevel@tonic-gate int ak = aperm->ipc_key; 6107c478bd9Sstevel@tonic-gate int bk = bperm->ipc_key; 6117c478bd9Sstevel@tonic-gate zoneid_t az; 6127c478bd9Sstevel@tonic-gate zoneid_t bz; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate ASSERT(ak != IPC_PRIVATE); 6157c478bd9Sstevel@tonic-gate ASSERT(bk != IPC_PRIVATE); 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* 6187c478bd9Sstevel@tonic-gate * Compare key first, then zoneid. This optimizes performance for 6197c478bd9Sstevel@tonic-gate * systems with only one zone, since the zone checks will only be 6207c478bd9Sstevel@tonic-gate * made when the keys match. 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate if (ak < bk) 6237c478bd9Sstevel@tonic-gate return (-1); 6247c478bd9Sstevel@tonic-gate if (ak > bk) 6257c478bd9Sstevel@tonic-gate return (1); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate /* keys match */ 6287c478bd9Sstevel@tonic-gate az = aperm->ipc_zoneid; 6297c478bd9Sstevel@tonic-gate bz = bperm->ipc_zoneid; 6307c478bd9Sstevel@tonic-gate if (az < bz) 6317c478bd9Sstevel@tonic-gate return (-1); 6327c478bd9Sstevel@tonic-gate if (az > bz) 6337c478bd9Sstevel@tonic-gate return (1); 6347c478bd9Sstevel@tonic-gate return (0); 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate /* 6387c478bd9Sstevel@tonic-gate * Create an ipc service. 6397c478bd9Sstevel@tonic-gate */ 6407c478bd9Sstevel@tonic-gate ipc_service_t * 641824c205fSml93401 ipcs_create(const char *name, rctl_hndl_t proj_rctl, rctl_hndl_t zone_rctl, 642824c205fSml93401 size_t size, ipc_func_t *dtor, ipc_func_t *rmid, int audit_type, 643824c205fSml93401 size_t rctl_offset) 6447c478bd9Sstevel@tonic-gate { 6457c478bd9Sstevel@tonic-gate ipc_service_t *result; 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate result = kmem_alloc(sizeof (ipc_service_t), KM_SLEEP); 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate mutex_init(&result->ipcs_lock, NULL, MUTEX_ADAPTIVE, NULL); 6507c478bd9Sstevel@tonic-gate result->ipcs_count = 0; 6517c478bd9Sstevel@tonic-gate avl_create(&result->ipcs_keys, ipc_key_compar, size, 0); 6527c478bd9Sstevel@tonic-gate result->ipcs_tabsz = IPC_IDS_MIN; 6537c478bd9Sstevel@tonic-gate result->ipcs_table = 6547c478bd9Sstevel@tonic-gate kmem_zalloc(IPC_IDS_MIN * sizeof (ipc_slot_t), KM_SLEEP); 6557c478bd9Sstevel@tonic-gate result->ipcs_ssize = size; 6567c478bd9Sstevel@tonic-gate result->ipcs_ids = id_space_create(name, 0, IPC_IDS_MIN); 6577c478bd9Sstevel@tonic-gate result->ipcs_dtor = dtor; 6587c478bd9Sstevel@tonic-gate result->ipcs_rmid = rmid; 659824c205fSml93401 result->ipcs_proj_rctl = proj_rctl; 660824c205fSml93401 result->ipcs_zone_rctl = zone_rctl; 6617c478bd9Sstevel@tonic-gate result->ipcs_atype = audit_type; 662824c205fSml93401 ASSERT(rctl_offset < sizeof (ipc_rqty_t)); 6637c478bd9Sstevel@tonic-gate result->ipcs_rctlofs = rctl_offset; 6647c478bd9Sstevel@tonic-gate list_create(&result->ipcs_usedids, sizeof (kipc_perm_t), 6657c478bd9Sstevel@tonic-gate offsetof(kipc_perm_t, ipc_list)); 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate return (result); 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate /* 6717c478bd9Sstevel@tonic-gate * Destroy an ipc service. 6727c478bd9Sstevel@tonic-gate */ 6737c478bd9Sstevel@tonic-gate void 6747c478bd9Sstevel@tonic-gate ipcs_destroy(ipc_service_t *service) 6757c478bd9Sstevel@tonic-gate { 6767c478bd9Sstevel@tonic-gate ipc_slot_t *slot, *next; 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate ASSERT(service->ipcs_count == 0); 6817c478bd9Sstevel@tonic-gate avl_destroy(&service->ipcs_keys); 6827c478bd9Sstevel@tonic-gate list_destroy(&service->ipcs_usedids); 6837c478bd9Sstevel@tonic-gate id_space_destroy(service->ipcs_ids); 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate for (slot = service->ipcs_table; slot; slot = next) { 6867c478bd9Sstevel@tonic-gate next = slot[0].ipct_chain; 6877c478bd9Sstevel@tonic-gate kmem_free(slot, service->ipcs_tabsz * sizeof (ipc_slot_t)); 6887c478bd9Sstevel@tonic-gate service->ipcs_tabsz >>= 1; 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate mutex_destroy(&service->ipcs_lock); 6927c478bd9Sstevel@tonic-gate kmem_free(service, sizeof (ipc_service_t)); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate /* 6967c478bd9Sstevel@tonic-gate * Takes the service lock. 6977c478bd9Sstevel@tonic-gate */ 6987c478bd9Sstevel@tonic-gate void 6997c478bd9Sstevel@tonic-gate ipcs_lock(ipc_service_t *service) 7007c478bd9Sstevel@tonic-gate { 7017c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate /* 7057c478bd9Sstevel@tonic-gate * Releases the service lock. 7067c478bd9Sstevel@tonic-gate */ 7077c478bd9Sstevel@tonic-gate void 7087c478bd9Sstevel@tonic-gate ipcs_unlock(ipc_service_t *service) 7097c478bd9Sstevel@tonic-gate { 7107c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate /* 7157c478bd9Sstevel@tonic-gate * Locks the specified ID. Returns the ID's ID table index. 7167c478bd9Sstevel@tonic-gate */ 7177c478bd9Sstevel@tonic-gate static int 7187c478bd9Sstevel@tonic-gate ipc_lock_internal(ipc_service_t *service, uint_t id) 7197c478bd9Sstevel@tonic-gate { 7207c478bd9Sstevel@tonic-gate uint_t tabsz; 7217c478bd9Sstevel@tonic-gate uint_t index; 7227c478bd9Sstevel@tonic-gate kmutex_t *mutex; 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate for (;;) { 7257c478bd9Sstevel@tonic-gate tabsz = service->ipcs_tabsz; 7267c478bd9Sstevel@tonic-gate membar_consumer(); 7277c478bd9Sstevel@tonic-gate index = id & (tabsz - 1); 7287c478bd9Sstevel@tonic-gate mutex = &service->ipcs_table[index].ipct_lock; 7297c478bd9Sstevel@tonic-gate mutex_enter(mutex); 7307c478bd9Sstevel@tonic-gate if (tabsz == service->ipcs_tabsz) 7317c478bd9Sstevel@tonic-gate break; 7327c478bd9Sstevel@tonic-gate mutex_exit(mutex); 7337c478bd9Sstevel@tonic-gate } 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate return (index); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate /* 7397c478bd9Sstevel@tonic-gate * Locks the specified ID. Returns a pointer to the ID's lock. 7407c478bd9Sstevel@tonic-gate */ 7417c478bd9Sstevel@tonic-gate kmutex_t * 7427c478bd9Sstevel@tonic-gate ipc_lock(ipc_service_t *service, int id) 7437c478bd9Sstevel@tonic-gate { 7447c478bd9Sstevel@tonic-gate uint_t index; 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* 7477c478bd9Sstevel@tonic-gate * These assertions don't reflect requirements of the code 7487c478bd9Sstevel@tonic-gate * which follows, but they should never fail nonetheless. 7497c478bd9Sstevel@tonic-gate */ 7507c478bd9Sstevel@tonic-gate ASSERT(id >= 0); 7517c478bd9Sstevel@tonic-gate ASSERT(IPC_INDEX(id) < service->ipcs_tabsz); 7527c478bd9Sstevel@tonic-gate index = ipc_lock_internal(service, id); 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate return (&service->ipcs_table[index].ipct_lock); 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate /* 7587c478bd9Sstevel@tonic-gate * Checks to see if the held lock provided is the current lock for the 7597c478bd9Sstevel@tonic-gate * specified id. If so, we return it instead of dropping it and 7607c478bd9Sstevel@tonic-gate * returning the result of ipc_lock. This is intended to speed up cv 7617c478bd9Sstevel@tonic-gate * wakeups where we are left holding a lock which could be stale, but 7627c478bd9Sstevel@tonic-gate * probably isn't. 7637c478bd9Sstevel@tonic-gate */ 7647c478bd9Sstevel@tonic-gate kmutex_t * 7657c478bd9Sstevel@tonic-gate ipc_relock(ipc_service_t *service, int id, kmutex_t *lock) 7667c478bd9Sstevel@tonic-gate { 7677c478bd9Sstevel@tonic-gate ASSERT(id >= 0); 7687c478bd9Sstevel@tonic-gate ASSERT(IPC_INDEX(id) < service->ipcs_tabsz); 7697c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(lock)); 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate if (&service->ipcs_table[IPC_INDEX(id)].ipct_lock == lock) 7727c478bd9Sstevel@tonic-gate return (lock); 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate mutex_exit(lock); 7757c478bd9Sstevel@tonic-gate return (ipc_lock(service, id)); 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate /* 7797c478bd9Sstevel@tonic-gate * Performs an ID lookup. If the ID doesn't exist or has been removed, 7807c478bd9Sstevel@tonic-gate * or isn't visible to the caller (because of zones), NULL is returned. 7817c478bd9Sstevel@tonic-gate * Otherwise, a pointer to the ID's perm structure and held ID lock are 7827c478bd9Sstevel@tonic-gate * returned. 7837c478bd9Sstevel@tonic-gate */ 7847c478bd9Sstevel@tonic-gate kmutex_t * 7857c478bd9Sstevel@tonic-gate ipc_lookup(ipc_service_t *service, int id, kipc_perm_t **perm) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate kipc_perm_t *result; 7887c478bd9Sstevel@tonic-gate uint_t index; 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* 7917c478bd9Sstevel@tonic-gate * There is no need to check to see if id is in-range (i.e. 7927c478bd9Sstevel@tonic-gate * positive and fits into the table). If it is out-of-range, 7937c478bd9Sstevel@tonic-gate * the id simply won't match the object's. 7947c478bd9Sstevel@tonic-gate */ 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate index = ipc_lock_internal(service, id); 7977c478bd9Sstevel@tonic-gate result = service->ipcs_table[index].ipct_data; 7987c478bd9Sstevel@tonic-gate if (result == NULL || result->ipc_id != (uint_t)id || 7997c478bd9Sstevel@tonic-gate !HASZONEACCESS(curproc, result->ipc_zoneid)) { 8007c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_table[index].ipct_lock); 8017c478bd9Sstevel@tonic-gate return (NULL); 8027c478bd9Sstevel@tonic-gate } 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate ASSERT(IPC_SEQ(id) == service->ipcs_table[index].ipct_seq); 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate *perm = result; 807*005d3febSMarek Pospisil if (AU_AUDITING()) 8087c478bd9Sstevel@tonic-gate audit_ipc(service->ipcs_atype, id, result); 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate return (&service->ipcs_table[index].ipct_lock); 8117c478bd9Sstevel@tonic-gate } 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate /* 8147c478bd9Sstevel@tonic-gate * Increase the reference count on an ID. 8157c478bd9Sstevel@tonic-gate */ 8167c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8177c478bd9Sstevel@tonic-gate void 8187c478bd9Sstevel@tonic-gate ipc_hold(ipc_service_t *s, kipc_perm_t *perm) 8197c478bd9Sstevel@tonic-gate { 8207c478bd9Sstevel@tonic-gate ASSERT(IPC_INDEX(perm->ipc_id) < s->ipcs_tabsz); 8217c478bd9Sstevel@tonic-gate ASSERT(IPC_LOCKED(s, perm)); 8227c478bd9Sstevel@tonic-gate perm->ipc_ref++; 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate /* 8267c478bd9Sstevel@tonic-gate * Decrease the reference count on an ID and drops the ID's lock. 8277c478bd9Sstevel@tonic-gate * Destroys the ID if the new reference count is zero. 8287c478bd9Sstevel@tonic-gate */ 8297c478bd9Sstevel@tonic-gate void 8307c478bd9Sstevel@tonic-gate ipc_rele(ipc_service_t *s, kipc_perm_t *perm) 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate int nref; 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate ASSERT(IPC_INDEX(perm->ipc_id) < s->ipcs_tabsz); 8357c478bd9Sstevel@tonic-gate ASSERT(IPC_LOCKED(s, perm)); 8367c478bd9Sstevel@tonic-gate ASSERT(perm->ipc_ref > 0); 8377c478bd9Sstevel@tonic-gate 8387c478bd9Sstevel@tonic-gate nref = --perm->ipc_ref; 8397c478bd9Sstevel@tonic-gate mutex_exit(&s->ipcs_table[IPC_INDEX(perm->ipc_id)].ipct_lock); 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate if (nref == 0) { 8427c478bd9Sstevel@tonic-gate ASSERT(IPC_FREE(perm)); /* ipc_rmid clears IPC_ALLOC */ 8437c478bd9Sstevel@tonic-gate s->ipcs_dtor(perm); 8447c478bd9Sstevel@tonic-gate project_rele(perm->ipc_proj); 845824c205fSml93401 zone_rele(perm->ipc_zone); 8467c478bd9Sstevel@tonic-gate kmem_free(perm, s->ipcs_ssize); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate } 8497c478bd9Sstevel@tonic-gate 8507c478bd9Sstevel@tonic-gate /* 8517c478bd9Sstevel@tonic-gate * Decrease the reference count on an ID, but don't drop the ID lock. 8527c478bd9Sstevel@tonic-gate * Used in cases where one thread needs to remove many references (on 8537c478bd9Sstevel@tonic-gate * behalf of other parties). 8547c478bd9Sstevel@tonic-gate */ 8557c478bd9Sstevel@tonic-gate void 8567c478bd9Sstevel@tonic-gate ipc_rele_locked(ipc_service_t *s, kipc_perm_t *perm) 8577c478bd9Sstevel@tonic-gate { 8587c478bd9Sstevel@tonic-gate ASSERT(perm->ipc_ref > 1); 8597c478bd9Sstevel@tonic-gate ASSERT(IPC_INDEX(perm->ipc_id) < s->ipcs_tabsz); 8607c478bd9Sstevel@tonic-gate ASSERT(IPC_LOCKED(s, perm)); 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate perm->ipc_ref--; 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate /* 8677c478bd9Sstevel@tonic-gate * Internal function to grow the service ID table. 8687c478bd9Sstevel@tonic-gate */ 8697c478bd9Sstevel@tonic-gate static int 8707c478bd9Sstevel@tonic-gate ipc_grow(ipc_service_t *service) 8717c478bd9Sstevel@tonic-gate { 8727c478bd9Sstevel@tonic-gate ipc_slot_t *new, *old; 8737c478bd9Sstevel@tonic-gate int i, oldsize, newsize; 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 8767c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate if (service->ipcs_tabsz == IPC_IDS_MAX) 8797c478bd9Sstevel@tonic-gate return (ENOSPC); 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate oldsize = service->ipcs_tabsz; 8827c478bd9Sstevel@tonic-gate newsize = oldsize << 1; 8837c478bd9Sstevel@tonic-gate new = kmem_zalloc(newsize * sizeof (ipc_slot_t), KM_NOSLEEP); 8847c478bd9Sstevel@tonic-gate if (new == NULL) 8857c478bd9Sstevel@tonic-gate return (ENOSPC); 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate old = service->ipcs_table; 8887c478bd9Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { 8897c478bd9Sstevel@tonic-gate mutex_enter(&old[i].ipct_lock); 8907c478bd9Sstevel@tonic-gate mutex_enter(&new[i].ipct_lock); 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate new[i].ipct_seq = old[i].ipct_seq; 8937c478bd9Sstevel@tonic-gate new[i].ipct_data = old[i].ipct_data; 8947c478bd9Sstevel@tonic-gate old[i].ipct_data = NULL; 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate new[0].ipct_chain = old; 8987c478bd9Sstevel@tonic-gate service->ipcs_table = new; 8997c478bd9Sstevel@tonic-gate membar_producer(); 9007c478bd9Sstevel@tonic-gate service->ipcs_tabsz = newsize; 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { 9037c478bd9Sstevel@tonic-gate mutex_exit(&old[i].ipct_lock); 9047c478bd9Sstevel@tonic-gate mutex_exit(&new[i].ipct_lock); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate id_space_extend(service->ipcs_ids, oldsize, service->ipcs_tabsz); 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate return (0); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate static int 9147c478bd9Sstevel@tonic-gate ipc_keylookup(ipc_service_t *service, key_t key, int flag, kipc_perm_t **permp) 9157c478bd9Sstevel@tonic-gate { 9167c478bd9Sstevel@tonic-gate kipc_perm_t *perm = NULL; 9177c478bd9Sstevel@tonic-gate avl_index_t where; 9187c478bd9Sstevel@tonic-gate kipc_perm_t template; 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate template.ipc_key = key; 9237c478bd9Sstevel@tonic-gate template.ipc_zoneid = getzoneid(); 9247c478bd9Sstevel@tonic-gate if (perm = avl_find(&service->ipcs_keys, &template, &where)) { 9257c478bd9Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 9267c478bd9Sstevel@tonic-gate if ((flag & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) 9277c478bd9Sstevel@tonic-gate return (EEXIST); 9287c478bd9Sstevel@tonic-gate if ((flag & 0777) & ~perm->ipc_mode) { 929*005d3febSMarek Pospisil if (AU_AUDITING()) 9307c478bd9Sstevel@tonic-gate audit_ipcget(NULL, (void *)perm); 9317c478bd9Sstevel@tonic-gate return (EACCES); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate *permp = perm; 9347c478bd9Sstevel@tonic-gate return (0); 9357c478bd9Sstevel@tonic-gate } else if (flag & IPC_CREAT) { 9367c478bd9Sstevel@tonic-gate *permp = NULL; 9377c478bd9Sstevel@tonic-gate return (0); 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate return (ENOENT); 9407c478bd9Sstevel@tonic-gate } 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate static int 9437c478bd9Sstevel@tonic-gate ipc_alloc_test(ipc_service_t *service, proc_t *pp) 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate /* 9487c478bd9Sstevel@tonic-gate * Resizing the table first would result in a cleaner code 9497c478bd9Sstevel@tonic-gate * path, but would also allow a user to (permanently) double 9507c478bd9Sstevel@tonic-gate * the id table size in cases where the allocation would be 9517c478bd9Sstevel@tonic-gate * denied. Hence we test the rctl first. 9527c478bd9Sstevel@tonic-gate */ 9537c478bd9Sstevel@tonic-gate retry: 9547c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 955824c205fSml93401 if ((rctl_test(service->ipcs_proj_rctl, pp->p_task->tk_proj->kpj_rctls, 956824c205fSml93401 pp, 1, RCA_SAFE) & RCT_DENY) || 957824c205fSml93401 (rctl_test(service->ipcs_zone_rctl, pp->p_zone->zone_rctls, 958824c205fSml93401 pp, 1, RCA_SAFE) & RCT_DENY)) { 9597c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 9607c478bd9Sstevel@tonic-gate return (ENOSPC); 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate if (service->ipcs_count == service->ipcs_tabsz) { 9647c478bd9Sstevel@tonic-gate int error; 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 9677c478bd9Sstevel@tonic-gate if (error = ipc_grow(service)) 9687c478bd9Sstevel@tonic-gate return (error); 9697c478bd9Sstevel@tonic-gate goto retry; 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate return (0); 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate /* 9767c478bd9Sstevel@tonic-gate * Given a key, search for or create the associated identifier. 9777c478bd9Sstevel@tonic-gate * 9787c478bd9Sstevel@tonic-gate * If IPC_CREAT is specified and the key isn't found, or if the key is 9797c478bd9Sstevel@tonic-gate * equal to IPC_PRIVATE, we return 0 and place a pointer to a newly 9807c478bd9Sstevel@tonic-gate * allocated object structure in permp. A pointer to the held service 9817c478bd9Sstevel@tonic-gate * lock is placed in lockp. ipc_mode's IPC_ALLOC bit is clear. 9827c478bd9Sstevel@tonic-gate * 9837c478bd9Sstevel@tonic-gate * If the key is found and no error conditions arise, we return 0 and 9847c478bd9Sstevel@tonic-gate * place a pointer to the existing object structure in permp. A 9857c478bd9Sstevel@tonic-gate * pointer to the held ID lock is placed in lockp. ipc_mode's 9867c478bd9Sstevel@tonic-gate * IPC_ALLOC bit is set. 9877c478bd9Sstevel@tonic-gate * 9887c478bd9Sstevel@tonic-gate * Otherwise, a non-zero errno value is returned. 9897c478bd9Sstevel@tonic-gate */ 9907c478bd9Sstevel@tonic-gate int 9917c478bd9Sstevel@tonic-gate ipc_get(ipc_service_t *service, key_t key, int flag, kipc_perm_t **permp, 9927c478bd9Sstevel@tonic-gate kmutex_t **lockp) 9937c478bd9Sstevel@tonic-gate { 9947c478bd9Sstevel@tonic-gate kipc_perm_t *perm = NULL; 9957c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 9967c478bd9Sstevel@tonic-gate int error, index; 9977c478bd9Sstevel@tonic-gate cred_t *cr = CRED(); 9987c478bd9Sstevel@tonic-gate 9997c478bd9Sstevel@tonic-gate if (key != IPC_PRIVATE) { 10007c478bd9Sstevel@tonic-gate 10017c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 10027c478bd9Sstevel@tonic-gate error = ipc_keylookup(service, key, flag, &perm); 10037c478bd9Sstevel@tonic-gate if (perm != NULL) 10047c478bd9Sstevel@tonic-gate index = ipc_lock_internal(service, perm->ipc_id); 10057c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate if (error) { 10087c478bd9Sstevel@tonic-gate ASSERT(perm == NULL); 10097c478bd9Sstevel@tonic-gate return (error); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate if (perm) { 10137c478bd9Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 10147c478bd9Sstevel@tonic-gate *permp = perm; 10157c478bd9Sstevel@tonic-gate *lockp = &service->ipcs_table[index].ipct_lock; 10167c478bd9Sstevel@tonic-gate return (0); 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* Key not found; fall through */ 10207c478bd9Sstevel@tonic-gate } 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate perm = kmem_zalloc(service->ipcs_ssize, KM_SLEEP); 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 10257c478bd9Sstevel@tonic-gate if (error = ipc_alloc_test(service, pp)) { 10267c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 10277c478bd9Sstevel@tonic-gate kmem_free(perm, service->ipcs_ssize); 10287c478bd9Sstevel@tonic-gate return (error); 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate perm->ipc_cuid = perm->ipc_uid = crgetuid(cr); 10327c478bd9Sstevel@tonic-gate perm->ipc_cgid = perm->ipc_gid = crgetgid(cr); 10337c478bd9Sstevel@tonic-gate perm->ipc_zoneid = getzoneid(); 10347c478bd9Sstevel@tonic-gate perm->ipc_mode = flag & 0777; 10357c478bd9Sstevel@tonic-gate perm->ipc_key = key; 10367c478bd9Sstevel@tonic-gate perm->ipc_ref = 1; 10377c478bd9Sstevel@tonic-gate perm->ipc_id = IPC_ID_INVAL; 10387c478bd9Sstevel@tonic-gate *permp = perm; 10397c478bd9Sstevel@tonic-gate *lockp = &service->ipcs_lock; 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate return (0); 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate /* 10457c478bd9Sstevel@tonic-gate * Attempts to add the a newly created ID to the global namespace. If 10467c478bd9Sstevel@tonic-gate * creating it would cause an error, we return the error. If there is 10477c478bd9Sstevel@tonic-gate * the possibility that we could obtain the existing ID and return it 10487c478bd9Sstevel@tonic-gate * to the user, we return EAGAIN. Otherwise, we return 0 with p_lock 10497c478bd9Sstevel@tonic-gate * and the service lock held. 10507c478bd9Sstevel@tonic-gate * 10517c478bd9Sstevel@tonic-gate * Since this should be only called after all initialization has been 10527c478bd9Sstevel@tonic-gate * completed, on failure we automatically invoke the destructor for the 10537c478bd9Sstevel@tonic-gate * object and deallocate the memory associated with it. 10547c478bd9Sstevel@tonic-gate */ 10557c478bd9Sstevel@tonic-gate int 10567c478bd9Sstevel@tonic-gate ipc_commit_begin(ipc_service_t *service, key_t key, int flag, 10577c478bd9Sstevel@tonic-gate kipc_perm_t *newperm) 10587c478bd9Sstevel@tonic-gate { 10597c478bd9Sstevel@tonic-gate kipc_perm_t *perm; 10607c478bd9Sstevel@tonic-gate int error; 10617c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 10627c478bd9Sstevel@tonic-gate 10637c478bd9Sstevel@tonic-gate ASSERT(newperm->ipc_ref == 1); 10647c478bd9Sstevel@tonic-gate ASSERT(IPC_FREE(newperm)); 10657c478bd9Sstevel@tonic-gate 106667253d2cSsl108498 /* 106767253d2cSsl108498 * Set ipc_proj and ipc_zone so that future calls to ipc_cleanup() 106867253d2cSsl108498 * clean up the necessary state. This must be done before the 106967253d2cSsl108498 * potential call to ipcs_dtor() below. 107067253d2cSsl108498 */ 107167253d2cSsl108498 newperm->ipc_proj = pp->p_task->tk_proj; 107267253d2cSsl108498 newperm->ipc_zone = pp->p_zone; 107367253d2cSsl108498 10747c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 10757c478bd9Sstevel@tonic-gate /* 10767c478bd9Sstevel@tonic-gate * Ensure that no-one has raced with us and created the key. 10777c478bd9Sstevel@tonic-gate */ 10787c478bd9Sstevel@tonic-gate if ((key != IPC_PRIVATE) && 10797c478bd9Sstevel@tonic-gate (((error = ipc_keylookup(service, key, flag, &perm)) != 0) || 10807c478bd9Sstevel@tonic-gate (perm != NULL))) { 10817c478bd9Sstevel@tonic-gate error = error ? error : EAGAIN; 10827c478bd9Sstevel@tonic-gate goto errout; 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate /* 10867c478bd9Sstevel@tonic-gate * Ensure that no-one has raced with us and used the last of 10877c478bd9Sstevel@tonic-gate * the permissible ids, or the last of the free spaces in the 10887c478bd9Sstevel@tonic-gate * id table. 10897c478bd9Sstevel@tonic-gate */ 10907c478bd9Sstevel@tonic-gate if (error = ipc_alloc_test(service, pp)) 10917c478bd9Sstevel@tonic-gate goto errout; 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 10947c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pp->p_lock)); 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate return (0); 10977c478bd9Sstevel@tonic-gate errout: 10987c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 10997c478bd9Sstevel@tonic-gate service->ipcs_dtor(newperm); 11007c478bd9Sstevel@tonic-gate kmem_free(newperm, service->ipcs_ssize); 11017c478bd9Sstevel@tonic-gate return (error); 11027c478bd9Sstevel@tonic-gate } 11037c478bd9Sstevel@tonic-gate 11047c478bd9Sstevel@tonic-gate /* 11057c478bd9Sstevel@tonic-gate * Commit the ID allocation transaction. Called with p_lock and the 11067c478bd9Sstevel@tonic-gate * service lock held, both of which are dropped. Returns the held ID 11077c478bd9Sstevel@tonic-gate * lock so the caller can extract the ID and perform ipcget auditing. 11087c478bd9Sstevel@tonic-gate */ 11097c478bd9Sstevel@tonic-gate kmutex_t * 11107c478bd9Sstevel@tonic-gate ipc_commit_end(ipc_service_t *service, kipc_perm_t *perm) 11117c478bd9Sstevel@tonic-gate { 11127c478bd9Sstevel@tonic-gate ipc_slot_t *slot; 11137c478bd9Sstevel@tonic-gate avl_index_t where; 11147c478bd9Sstevel@tonic-gate int index; 11157c478bd9Sstevel@tonic-gate void *loc; 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 11187c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&curproc->p_lock)); 11197c478bd9Sstevel@tonic-gate 11207c478bd9Sstevel@tonic-gate (void) project_hold(perm->ipc_proj); 1121824c205fSml93401 (void) zone_hold(perm->ipc_zone); 11227c478bd9Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate /* 11257c478bd9Sstevel@tonic-gate * Pick out our slot. 11267c478bd9Sstevel@tonic-gate */ 11277c478bd9Sstevel@tonic-gate service->ipcs_count++; 11287c478bd9Sstevel@tonic-gate index = id_alloc(service->ipcs_ids); 11297c478bd9Sstevel@tonic-gate ASSERT(index < service->ipcs_tabsz); 11307c478bd9Sstevel@tonic-gate slot = &service->ipcs_table[index]; 11317c478bd9Sstevel@tonic-gate mutex_enter(&slot->ipct_lock); 11327c478bd9Sstevel@tonic-gate ASSERT(slot->ipct_data == NULL); 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate /* 11357c478bd9Sstevel@tonic-gate * Update the perm structure. 11367c478bd9Sstevel@tonic-gate */ 11377c478bd9Sstevel@tonic-gate perm->ipc_mode |= IPC_ALLOC; 11387c478bd9Sstevel@tonic-gate perm->ipc_id = (slot->ipct_seq << IPC_SEQ_SHIFT) | index; 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate /* 11417c478bd9Sstevel@tonic-gate * Push into global visibility. 11427c478bd9Sstevel@tonic-gate */ 11437c478bd9Sstevel@tonic-gate slot->ipct_data = perm; 11447c478bd9Sstevel@tonic-gate if (perm->ipc_key != IPC_PRIVATE) { 11457c478bd9Sstevel@tonic-gate loc = avl_find(&service->ipcs_keys, perm, &where); 11467c478bd9Sstevel@tonic-gate ASSERT(loc == NULL); 11477c478bd9Sstevel@tonic-gate avl_insert(&service->ipcs_keys, perm, where); 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate list_insert_head(&service->ipcs_usedids, perm); 11507c478bd9Sstevel@tonic-gate 11517c478bd9Sstevel@tonic-gate /* 11527c478bd9Sstevel@tonic-gate * Update resource consumption. 11537c478bd9Sstevel@tonic-gate */ 1154824c205fSml93401 IPC_PROJ_USAGE(perm, service) += 1; 1155824c205fSml93401 IPC_ZONE_USAGE(perm, service) += 1; 11567c478bd9Sstevel@tonic-gate 11577c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 11587c478bd9Sstevel@tonic-gate return (&slot->ipct_lock); 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate /* 11627c478bd9Sstevel@tonic-gate * Clean up function, in case the allocation fails. If called between 11637c478bd9Sstevel@tonic-gate * ipc_lookup and ipc_commit_begin, perm->ipc_proj will be 0 and we 11647c478bd9Sstevel@tonic-gate * merely free the perm structure. If called after ipc_commit_begin, 11657c478bd9Sstevel@tonic-gate * we also drop locks and call the ID's destructor. 11667c478bd9Sstevel@tonic-gate */ 11677c478bd9Sstevel@tonic-gate void 11687c478bd9Sstevel@tonic-gate ipc_cleanup(ipc_service_t *service, kipc_perm_t *perm) 11697c478bd9Sstevel@tonic-gate { 11707c478bd9Sstevel@tonic-gate ASSERT(IPC_FREE(perm)); 11717c478bd9Sstevel@tonic-gate if (perm->ipc_proj) { 11727c478bd9Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 11737c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 11747c478bd9Sstevel@tonic-gate service->ipcs_dtor(perm); 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate kmem_free(perm, service->ipcs_ssize); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate 11807c478bd9Sstevel@tonic-gate /* 11817c478bd9Sstevel@tonic-gate * Common code to remove an IPC object. This should be called after 11827c478bd9Sstevel@tonic-gate * all permissions checks have been performed, and with the service 11837c478bd9Sstevel@tonic-gate * and ID locked. Note that this does not remove the object from 11847c478bd9Sstevel@tonic-gate * the ipcs_usedids list (this needs to be done by the caller before 11857c478bd9Sstevel@tonic-gate * dropping the service lock). 11867c478bd9Sstevel@tonic-gate */ 11877c478bd9Sstevel@tonic-gate static void 11887c478bd9Sstevel@tonic-gate ipc_remove(ipc_service_t *service, kipc_perm_t *perm) 11897c478bd9Sstevel@tonic-gate { 11907c478bd9Sstevel@tonic-gate int id = perm->ipc_id; 11917c478bd9Sstevel@tonic-gate int index; 11927c478bd9Sstevel@tonic-gate 11937c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&service->ipcs_lock)); 11947c478bd9Sstevel@tonic-gate ASSERT(IPC_LOCKED(service, perm)); 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate index = IPC_INDEX(id); 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate service->ipcs_table[index].ipct_data = NULL; 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate if (perm->ipc_key != IPC_PRIVATE) 12017c478bd9Sstevel@tonic-gate avl_remove(&service->ipcs_keys, perm); 12027c478bd9Sstevel@tonic-gate list_remove(&service->ipcs_usedids, perm); 12037c478bd9Sstevel@tonic-gate perm->ipc_mode &= ~IPC_ALLOC; 12047c478bd9Sstevel@tonic-gate 12057c478bd9Sstevel@tonic-gate id_free(service->ipcs_ids, index); 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate if (service->ipcs_table[index].ipct_seq++ == IPC_SEQ_MASK) 12087c478bd9Sstevel@tonic-gate service->ipcs_table[index].ipct_seq = 0; 12097c478bd9Sstevel@tonic-gate service->ipcs_count--; 1210824c205fSml93401 ASSERT(IPC_PROJ_USAGE(perm, service) > 0); 1211824c205fSml93401 ASSERT(IPC_ZONE_USAGE(perm, service) > 0); 1212824c205fSml93401 IPC_PROJ_USAGE(perm, service) -= 1; 1213824c205fSml93401 IPC_ZONE_USAGE(perm, service) -= 1; 1214824c205fSml93401 ASSERT(service->ipcs_count || ((IPC_PROJ_USAGE(perm, service) == 0) && 1215824c205fSml93401 (IPC_ZONE_USAGE(perm, service) == 0))); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate 12197c478bd9Sstevel@tonic-gate /* 12207c478bd9Sstevel@tonic-gate * Common code to perform an IPC_RMID. Returns an errno value on 12217c478bd9Sstevel@tonic-gate * failure, 0 on success. 12227c478bd9Sstevel@tonic-gate */ 12237c478bd9Sstevel@tonic-gate int 12247c478bd9Sstevel@tonic-gate ipc_rmid(ipc_service_t *service, int id, cred_t *cr) 12257c478bd9Sstevel@tonic-gate { 12267c478bd9Sstevel@tonic-gate kipc_perm_t *perm; 12277c478bd9Sstevel@tonic-gate kmutex_t *lock; 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate lock = ipc_lookup(service, id, &perm); 12327c478bd9Sstevel@tonic-gate if (lock == NULL) { 12337c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 12347c478bd9Sstevel@tonic-gate return (EINVAL); 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate ASSERT(service->ipcs_count > 0); 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate if (secpolicy_ipc_owner(cr, perm) != 0) { 12407c478bd9Sstevel@tonic-gate mutex_exit(lock); 12417c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 12427c478bd9Sstevel@tonic-gate return (EPERM); 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate 12457c478bd9Sstevel@tonic-gate /* 12467c478bd9Sstevel@tonic-gate * Nothing can fail from this point on. 12477c478bd9Sstevel@tonic-gate */ 12487c478bd9Sstevel@tonic-gate ipc_remove(service, perm); 12497c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate /* perform any per-service removal actions */ 12527c478bd9Sstevel@tonic-gate service->ipcs_rmid(perm); 12537c478bd9Sstevel@tonic-gate 12547c478bd9Sstevel@tonic-gate ipc_rele(service, perm); 12557c478bd9Sstevel@tonic-gate 12567c478bd9Sstevel@tonic-gate return (0); 12577c478bd9Sstevel@tonic-gate } 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate /* 12607c478bd9Sstevel@tonic-gate * Implementation for shmids, semids, and msgids. buf is the address 12617c478bd9Sstevel@tonic-gate * of the user buffer, nids is the size, and pnids is a pointer to 12627c478bd9Sstevel@tonic-gate * where we write the actual number of ids that [would] have been 12637c478bd9Sstevel@tonic-gate * copied out. 12647c478bd9Sstevel@tonic-gate */ 12657c478bd9Sstevel@tonic-gate int 12667c478bd9Sstevel@tonic-gate ipc_ids(ipc_service_t *service, int *buf, uint_t nids, uint_t *pnids) 12677c478bd9Sstevel@tonic-gate { 12687c478bd9Sstevel@tonic-gate kipc_perm_t *perm; 12697c478bd9Sstevel@tonic-gate size_t idsize = 0; 12707c478bd9Sstevel@tonic-gate int error = 0; 12717c478bd9Sstevel@tonic-gate int idcount; 12727c478bd9Sstevel@tonic-gate int *ids; 12737c478bd9Sstevel@tonic-gate int numids = 0; 12747c478bd9Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 12757c478bd9Sstevel@tonic-gate int global = INGLOBALZONE(curproc); 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate if (buf == NULL) 12787c478bd9Sstevel@tonic-gate nids = 0; 12797c478bd9Sstevel@tonic-gate 12807c478bd9Sstevel@tonic-gate /* 12817c478bd9Sstevel@tonic-gate * Get an accurate count of the total number of ids, and allocate a 12827c478bd9Sstevel@tonic-gate * staging buffer. Since ipcs_count is always sane, we don't have 12837c478bd9Sstevel@tonic-gate * to take ipcs_lock for our first guess. If there are no ids, or 12847c478bd9Sstevel@tonic-gate * we're in the global zone and the number of ids is greater than 12857c478bd9Sstevel@tonic-gate * the size of the specified buffer, we shunt to the end. Otherwise, 12867c478bd9Sstevel@tonic-gate * we go through the id list looking for (and counting) what is 12877c478bd9Sstevel@tonic-gate * visible in the specified zone. 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate idcount = service->ipcs_count; 12907c478bd9Sstevel@tonic-gate for (;;) { 12917c478bd9Sstevel@tonic-gate if ((global && idcount > nids) || idcount == 0) { 12927c478bd9Sstevel@tonic-gate numids = idcount; 12937c478bd9Sstevel@tonic-gate nids = 0; 12947c478bd9Sstevel@tonic-gate goto out; 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate idsize = idcount * sizeof (int); 12987c478bd9Sstevel@tonic-gate ids = kmem_alloc(idsize, KM_SLEEP); 12997c478bd9Sstevel@tonic-gate 13007c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 13017c478bd9Sstevel@tonic-gate if (idcount >= service->ipcs_count) 13027c478bd9Sstevel@tonic-gate break; 13037c478bd9Sstevel@tonic-gate idcount = service->ipcs_count; 13047c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 13057c478bd9Sstevel@tonic-gate 13067c478bd9Sstevel@tonic-gate if (idsize != 0) { 13077c478bd9Sstevel@tonic-gate kmem_free(ids, idsize); 13087c478bd9Sstevel@tonic-gate idsize = 0; 13097c478bd9Sstevel@tonic-gate } 13107c478bd9Sstevel@tonic-gate } 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate for (perm = list_head(&service->ipcs_usedids); perm != NULL; 13137c478bd9Sstevel@tonic-gate perm = list_next(&service->ipcs_usedids, perm)) { 13147c478bd9Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 13157c478bd9Sstevel@tonic-gate if (global || perm->ipc_zoneid == zoneid) 13167c478bd9Sstevel@tonic-gate ids[numids++] = perm->ipc_id; 13177c478bd9Sstevel@tonic-gate } 13187c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 13197c478bd9Sstevel@tonic-gate 13207c478bd9Sstevel@tonic-gate /* 13217c478bd9Sstevel@tonic-gate * If there isn't enough space to hold all of the ids, just 13227c478bd9Sstevel@tonic-gate * return the number of ids without copying out any of them. 13237c478bd9Sstevel@tonic-gate */ 13247c478bd9Sstevel@tonic-gate if (nids < numids) 13257c478bd9Sstevel@tonic-gate nids = 0; 13267c478bd9Sstevel@tonic-gate 13277c478bd9Sstevel@tonic-gate out: 13287c478bd9Sstevel@tonic-gate if (suword32(pnids, (uint32_t)numids) || 13297c478bd9Sstevel@tonic-gate (nids != 0 && copyout(ids, buf, numids * sizeof (int)))) 13307c478bd9Sstevel@tonic-gate error = EFAULT; 13317c478bd9Sstevel@tonic-gate if (idsize != 0) 13327c478bd9Sstevel@tonic-gate kmem_free(ids, idsize); 13337c478bd9Sstevel@tonic-gate return (error); 13347c478bd9Sstevel@tonic-gate } 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate /* 13377c478bd9Sstevel@tonic-gate * Destroy IPC objects from the given service that are associated with 13387c478bd9Sstevel@tonic-gate * the given zone. 13397c478bd9Sstevel@tonic-gate * 13407c478bd9Sstevel@tonic-gate * We can't hold on to the service lock when freeing objects, so we 13417c478bd9Sstevel@tonic-gate * first search the service and move all the objects to a private 13427c478bd9Sstevel@tonic-gate * list, then walk through and free them after dropping the lock. 13437c478bd9Sstevel@tonic-gate */ 13447c478bd9Sstevel@tonic-gate void 13457c478bd9Sstevel@tonic-gate ipc_remove_zone(ipc_service_t *service, zoneid_t zoneid) 13467c478bd9Sstevel@tonic-gate { 13477c478bd9Sstevel@tonic-gate kipc_perm_t *perm, *next; 13487c478bd9Sstevel@tonic-gate list_t rmlist; 13497c478bd9Sstevel@tonic-gate kmutex_t *lock; 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate list_create(&rmlist, sizeof (kipc_perm_t), 13527c478bd9Sstevel@tonic-gate offsetof(kipc_perm_t, ipc_list)); 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate mutex_enter(&service->ipcs_lock); 13557c478bd9Sstevel@tonic-gate for (perm = list_head(&service->ipcs_usedids); perm != NULL; 13567c478bd9Sstevel@tonic-gate perm = next) { 13577c478bd9Sstevel@tonic-gate next = list_next(&service->ipcs_usedids, perm); 13587c478bd9Sstevel@tonic-gate if (perm->ipc_zoneid != zoneid) 13597c478bd9Sstevel@tonic-gate continue; 13607c478bd9Sstevel@tonic-gate 13617c478bd9Sstevel@tonic-gate /* 13627c478bd9Sstevel@tonic-gate * Remove the object from the service, then put it on 13637c478bd9Sstevel@tonic-gate * the removal list so we can defer the call to 13647c478bd9Sstevel@tonic-gate * ipc_rele (which will actually free the structure). 13657c478bd9Sstevel@tonic-gate * We need to do this since the destructor may grab 13667c478bd9Sstevel@tonic-gate * the service lock. 13677c478bd9Sstevel@tonic-gate */ 13687c478bd9Sstevel@tonic-gate ASSERT(!IPC_FREE(perm)); 13697c478bd9Sstevel@tonic-gate lock = ipc_lock(service, perm->ipc_id); 13707c478bd9Sstevel@tonic-gate ipc_remove(service, perm); 13717c478bd9Sstevel@tonic-gate mutex_exit(lock); 13727c478bd9Sstevel@tonic-gate list_insert_tail(&rmlist, perm); 13737c478bd9Sstevel@tonic-gate } 13747c478bd9Sstevel@tonic-gate mutex_exit(&service->ipcs_lock); 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate /* 13777c478bd9Sstevel@tonic-gate * Now that we've dropped the service lock, loop through the 13787c478bd9Sstevel@tonic-gate * private list freeing removed objects. 13797c478bd9Sstevel@tonic-gate */ 13807c478bd9Sstevel@tonic-gate for (perm = list_head(&rmlist); perm != NULL; perm = next) { 13817c478bd9Sstevel@tonic-gate next = list_next(&rmlist, perm); 13827c478bd9Sstevel@tonic-gate list_remove(&rmlist, perm); 13837c478bd9Sstevel@tonic-gate 13847c478bd9Sstevel@tonic-gate (void) ipc_lock(service, perm->ipc_id); 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate /* perform any per-service removal actions */ 13877c478bd9Sstevel@tonic-gate service->ipcs_rmid(perm); 13887c478bd9Sstevel@tonic-gate 13897c478bd9Sstevel@tonic-gate /* release reference */ 13907c478bd9Sstevel@tonic-gate ipc_rele(service, perm); 13917c478bd9Sstevel@tonic-gate } 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate list_destroy(&rmlist); 13947c478bd9Sstevel@tonic-gate } 1395