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