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