Annotation of Gnu-Mach/ipc/mach_port.c, revision 1.1.1.5

1.1       root        1: /*
                      2:  * Mach Operating System
                      3:  * Copyright (c) 1991,1990,1989 Carnegie Mellon University.
                      4:  * Copyright (c) 1993,1994 The University of Utah and
                      5:  * the Computer Systems Laboratory (CSL).
                      6:  * All rights reserved.
                      7:  *
                      8:  * Permission to use, copy, modify and distribute this software and its
                      9:  * documentation is hereby granted, provided that both the copyright
                     10:  * notice and this permission notice appear in all copies of the
                     11:  * software, derivative works or modified versions, and any portions
                     12:  * thereof, and that both notices appear in supporting documentation.
                     13:  *
                     14:  * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
                     15:  * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
                     16:  * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
                     17:  * THIS SOFTWARE.
                     18:  *
                     19:  * Carnegie Mellon requests users of this software to return to
                     20:  *
                     21:  *  Software Distribution Coordinator  or  [email protected]
                     22:  *  School of Computer Science
                     23:  *  Carnegie Mellon University
                     24:  *  Pittsburgh PA 15213-3890
                     25:  *
                     26:  * any improvements or extensions that they make and grant Carnegie Mellon
                     27:  * the rights to redistribute these changes.
                     28:  */
                     29: /*
                     30:  */
                     31: /*
                     32:  *     File:   ipc/mach_port.c
                     33:  *     Author: Rich Draves
                     34:  *     Date:   1989
                     35:  *
                     36:  *     Exported kernel calls.  See mach/mach_port.defs.
                     37:  */
                     38: 
1.1.1.3   root       39: #include <kern/debug.h>
                     40: #include <kern/printf.h>
1.1       root       41: #include <mach/port.h>
                     42: #include <mach/kern_return.h>
                     43: #include <mach/notify.h>
                     44: #include <mach/mach_param.h>
                     45: #include <mach/vm_param.h>
                     46: #include <mach/vm_prot.h>
                     47: #ifdef MIGRATING_THREADS
                     48: #include <mach/rpc.h>
                     49: #include <kern/task.h>
                     50: #include <kern/act.h>
                     51: #endif /* MIGRATING_THREADS */
                     52: #include <vm/vm_map.h>
                     53: #include <vm/vm_kern.h>
                     54: #include <vm/vm_user.h>
                     55: #include <ipc/ipc_entry.h>
                     56: #include <ipc/ipc_space.h>
                     57: #include <ipc/ipc_object.h>
                     58: #include <ipc/ipc_notify.h>
                     59: #include <ipc/ipc_port.h>
                     60: #include <ipc/ipc_pset.h>
                     61: #include <ipc/ipc_right.h>
1.1.1.3   root       62: #include <ipc/mach_port.h>
1.1       root       63: 
                     64: 
                     65: 
                     66: /*
                     67:  *     Routine:        mach_port_names_helper
                     68:  *     Purpose:
                     69:  *             A helper function for mach_port_names.
                     70:  */
                     71: 
                     72: void
                     73: mach_port_names_helper(
                     74:        ipc_port_timestamp_t    timestamp,
                     75:        ipc_entry_t             entry,
                     76:        mach_port_t             name,
                     77:        mach_port_t             *names,
                     78:        mach_port_type_t        *types,
                     79:        ipc_entry_num_t         *actualp)
                     80: {
                     81:        ipc_entry_bits_t bits = entry->ie_bits;
                     82:        ipc_port_request_index_t request = entry->ie_request;
                     83:        mach_port_type_t type;
                     84:        ipc_entry_num_t actual;
                     85: 
                     86:        if (bits & MACH_PORT_TYPE_SEND_RIGHTS) {
                     87:                ipc_port_t port;
                     88:                boolean_t died;
                     89: 
                     90:                port = (ipc_port_t) entry->ie_object;
                     91:                assert(port != IP_NULL);
                     92: 
                     93:                /*
                     94:                 *      The timestamp serializes mach_port_names
                     95:                 *      with ipc_port_destroy.  If the port died,
                     96:                 *      but after mach_port_names started, pretend
                     97:                 *      that it isn't dead.
                     98:                 */
                     99: 
                    100:                ip_lock(port);
                    101:                died = (!ip_active(port) &&
                    102:                        IP_TIMESTAMP_ORDER(port->ip_timestamp, timestamp));
                    103:                ip_unlock(port);
                    104: 
                    105:                if (died) {
                    106:                        /* pretend this is a dead-name entry */
                    107: 
                    108:                        bits &= ~(IE_BITS_TYPE_MASK|IE_BITS_MAREQUEST);
                    109:                        bits |= MACH_PORT_TYPE_DEAD_NAME;
                    110:                        if (request != 0)
                    111:                                bits++;
                    112:                        request = 0;
                    113:                }
                    114:        }
                    115: 
                    116:        type = IE_BITS_TYPE(bits);
                    117:        if (request != 0)
                    118:                type |= MACH_PORT_TYPE_DNREQUEST;
                    119:        if (bits & IE_BITS_MAREQUEST)
                    120:                type |= MACH_PORT_TYPE_MAREQUEST;
                    121: 
                    122:        actual = *actualp;
                    123:        names[actual] = name;
                    124:        types[actual] = type;
                    125:        *actualp = actual+1;
                    126: }
                    127: 
                    128: /*
                    129:  *     Routine:        mach_port_names [kernel call]
                    130:  *     Purpose:
                    131:  *             Retrieves a list of the rights present in the space,
                    132:  *             along with type information.  (Same as returned
                    133:  *             by mach_port_type.)  The names are returned in
                    134:  *             no particular order, but they (and the type info)
                    135:  *             are an accurate snapshot of the space.
                    136:  *     Conditions:
                    137:  *             Nothing locked.
                    138:  *     Returns:
                    139:  *             KERN_SUCCESS            Arrays of names and types returned.
                    140:  *             KERN_INVALID_TASK       The space is null.
                    141:  *             KERN_INVALID_TASK       The space is dead.
                    142:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                    143:  */
                    144: 
                    145: kern_return_t
                    146: mach_port_names(
                    147:        ipc_space_t             space,
                    148:        mach_port_t             **namesp,
                    149:        mach_msg_type_number_t  *namesCnt,
                    150:        mach_port_type_t        **typesp,
                    151:        mach_msg_type_number_t  *typesCnt)
                    152: {
                    153:        ipc_entry_num_t actual; /* this many names */
                    154:        ipc_port_timestamp_t timestamp; /* logical time of this operation */
                    155:        mach_port_t *names;
                    156:        mach_port_type_t *types;
                    157:        kern_return_t kr;
                    158: 
                    159:        vm_size_t size;         /* size of allocated memory */
                    160:        vm_offset_t addr1;      /* allocated memory, for names */
                    161:        vm_offset_t addr2;      /* allocated memory, for types */
                    162:        vm_map_copy_t memory1;  /* copied-in memory, for names */
                    163:        vm_map_copy_t memory2;  /* copied-in memory, for types */
                    164: 
                    165:        /* safe simplifying assumption */
                    166:        assert_static(sizeof(mach_port_t) == sizeof(mach_port_type_t));
                    167: 
                    168:        if (space == IS_NULL)
                    169:                return KERN_INVALID_TASK;
                    170: 
                    171:        size = 0;
                    172: 
                    173:        for (;;) {
                    174:                ipc_entry_num_t bound;
                    175:                vm_size_t size_needed;
                    176: 
                    177:                is_read_lock(space);
                    178:                if (!space->is_active) {
                    179:                        is_read_unlock(space);
                    180:                        if (size != 0) {
                    181:                                kmem_free(ipc_kernel_map, addr1, size);
                    182:                                kmem_free(ipc_kernel_map, addr2, size);
                    183:                        }
                    184:                        return KERN_INVALID_TASK;
                    185:                }
                    186: 
                    187:                /* upper bound on number of names in the space */
                    188: 
1.1.1.5 ! root      189:                bound = space->is_size;
1.1       root      190:                size_needed = round_page(bound * sizeof(mach_port_t));
                    191: 
                    192:                if (size_needed <= size)
                    193:                        break;
                    194: 
                    195:                is_read_unlock(space);
                    196: 
                    197:                if (size != 0) {
                    198:                        kmem_free(ipc_kernel_map, addr1, size);
                    199:                        kmem_free(ipc_kernel_map, addr2, size);
                    200:                }
                    201:                size = size_needed;
                    202: 
                    203:                kr = vm_allocate(ipc_kernel_map, &addr1, size, TRUE);
1.1.1.3   root      204:                if (kr != KERN_SUCCESS) {
                    205:                        printf_once("no more room in ipc_kernel_map\n");
1.1       root      206:                        return KERN_RESOURCE_SHORTAGE;
1.1.1.3   root      207:                }
1.1       root      208: 
                    209:                kr = vm_allocate(ipc_kernel_map, &addr2, size, TRUE);
                    210:                if (kr != KERN_SUCCESS) {
1.1.1.3   root      211:                        printf_once("no more room in ipc_kernel_map\n");
1.1       root      212:                        kmem_free(ipc_kernel_map, addr1, size);
                    213:                        return KERN_RESOURCE_SHORTAGE;
                    214:                }
                    215: 
                    216:                /* can't fault while we hold locks */
                    217: 
                    218:                kr = vm_map_pageable(ipc_kernel_map, addr1, addr1 + size,
                    219:                                     VM_PROT_READ|VM_PROT_WRITE);
                    220:                assert(kr == KERN_SUCCESS);
                    221: 
                    222:                kr = vm_map_pageable(ipc_kernel_map, addr2, addr2 + size,
                    223:                                     VM_PROT_READ|VM_PROT_WRITE);
                    224:                assert(kr == KERN_SUCCESS);
                    225:        }
                    226:        /* space is read-locked and active */
                    227: 
                    228:        names = (mach_port_t *) addr1;
                    229:        types = (mach_port_type_t *) addr2;
                    230:        actual = 0;
                    231: 
                    232:        timestamp = ipc_port_timestamp();
                    233: 
1.1.1.5 ! root      234:        ipc_entry_t entry;
        !           235:        struct rdxtree_iter iter;
        !           236:        rdxtree_for_each(&space->is_map, &iter, entry) {
1.1       root      237:                ipc_entry_bits_t bits = entry->ie_bits;
                    238: 
                    239:                if (IE_BITS_TYPE(bits) != MACH_PORT_TYPE_NONE) {
1.1.1.5 ! root      240:                        mach_port_names_helper(timestamp, entry, entry->ie_name,
1.1       root      241:                                               names, types, &actual);
                    242:                }
                    243:        }
                    244:        is_read_unlock(space);
                    245: 
                    246:        if (actual == 0) {
                    247:                memory1 = VM_MAP_COPY_NULL;
                    248:                memory2 = VM_MAP_COPY_NULL;
                    249: 
                    250:                if (size != 0) {
                    251:                        kmem_free(ipc_kernel_map, addr1, size);
                    252:                        kmem_free(ipc_kernel_map, addr2, size);
                    253:                }
                    254:        } else {
                    255:                vm_size_t size_used;
                    256: 
                    257:                size_used = round_page(actual * sizeof(mach_port_t));
                    258: 
                    259:                /*
                    260:                 *      Make used memory pageable and get it into
                    261:                 *      copied-in form.  Free any unused memory.
                    262:                 */
                    263: 
                    264:                kr = vm_map_pageable(ipc_kernel_map,
                    265:                                     addr1, addr1 + size_used,
                    266:                                     VM_PROT_NONE);
                    267:                assert(kr == KERN_SUCCESS);
                    268: 
                    269:                kr = vm_map_pageable(ipc_kernel_map,
                    270:                                     addr2, addr2 + size_used,
                    271:                                     VM_PROT_NONE);
                    272:                assert(kr == KERN_SUCCESS);
                    273: 
                    274:                kr = vm_map_copyin(ipc_kernel_map, addr1, size_used,
                    275:                                   TRUE, &memory1);
                    276:                assert(kr == KERN_SUCCESS);
                    277: 
                    278:                kr = vm_map_copyin(ipc_kernel_map, addr2, size_used,
                    279:                                   TRUE, &memory2);
                    280:                assert(kr == KERN_SUCCESS);
                    281: 
                    282:                if (size_used != size) {
                    283:                        kmem_free(ipc_kernel_map,
                    284:                                  addr1 + size_used, size - size_used);
                    285:                        kmem_free(ipc_kernel_map,
                    286:                                  addr2 + size_used, size - size_used);
                    287:                }
                    288:        }
                    289: 
                    290:        *namesp = (mach_port_t *) memory1;
                    291:        *namesCnt = actual;
                    292:        *typesp = (mach_port_type_t *) memory2;
                    293:        *typesCnt = actual;
                    294:        return KERN_SUCCESS;
                    295: }
                    296: 
                    297: /*
                    298:  *     Routine:        mach_port_type [kernel call]
                    299:  *     Purpose:
                    300:  *             Retrieves the type of a right in the space.
                    301:  *             The type is a bitwise combination of one or more
                    302:  *             of the following type bits:
                    303:  *                     MACH_PORT_TYPE_SEND
                    304:  *                     MACH_PORT_TYPE_RECEIVE
                    305:  *                     MACH_PORT_TYPE_SEND_ONCE
                    306:  *                     MACH_PORT_TYPE_PORT_SET
                    307:  *                     MACH_PORT_TYPE_DEAD_NAME
                    308:  *             In addition, the following pseudo-type bits may be present:
                    309:  *                     MACH_PORT_TYPE_DNREQUEST
                    310:  *                             A dead-name notification is requested.
                    311:  *                     MACH_PORT_TYPE_MAREQUEST
                    312:  *                             The send/receive right is blocked;
                    313:  *                             a msg-accepted notification is outstanding.
                    314:  *                     MACH_PORT_TYPE_COMPAT
                    315:  *                             This is a compatibility-mode right;
                    316:  *                             when the port dies, it will disappear
                    317:  *                             instead of turning into a dead-name.
                    318:  *     Conditions:
                    319:  *             Nothing locked.
                    320:  *     Returns:
                    321:  *             KERN_SUCCESS            Type is returned.
                    322:  *             KERN_INVALID_TASK       The space is null.
                    323:  *             KERN_INVALID_TASK       The space is dead.
                    324:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    325:  */
                    326: 
                    327: kern_return_t
                    328: mach_port_type(
                    329:        ipc_space_t             space,
                    330:        mach_port_t             name,
                    331:        mach_port_type_t        *typep)
                    332: {
                    333:        mach_port_urefs_t urefs;
                    334:        ipc_entry_t entry;
                    335:        kern_return_t kr;
                    336: 
                    337:        if (space == IS_NULL)
                    338:                return KERN_INVALID_TASK;
                    339: 
                    340:        kr = ipc_right_lookup_write(space, name, &entry);
                    341:        if (kr != KERN_SUCCESS)
                    342:                return kr;
                    343:        /* space is write-locked and active */
                    344: 
                    345:        kr = ipc_right_info(space, name, entry, typep, &urefs);
                    346:        if (kr == KERN_SUCCESS)
                    347:                is_write_unlock(space);
                    348:        /* space is unlocked */
                    349:        return kr;
                    350: }
                    351: 
                    352: /*
                    353:  *     Routine:        mach_port_rename [kernel call]
                    354:  *     Purpose:
                    355:  *             Changes the name denoting a right,
                    356:  *             from oname to nname.
                    357:  *     Conditions:
                    358:  *             Nothing locked.
                    359:  *     Returns:
                    360:  *             KERN_SUCCESS            The right is renamed.
                    361:  *             KERN_INVALID_TASK       The space is null.
                    362:  *             KERN_INVALID_TASK       The space is dead.
                    363:  *             KERN_INVALID_NAME       The oname doesn't denote a right.
                    364:  *             KERN_INVALID_VALUE      The nname isn't a legal name.
                    365:  *             KERN_NAME_EXISTS        The nname already denotes a right.
                    366:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                    367:  */
                    368: 
                    369: kern_return_t
                    370: mach_port_rename(
                    371:        ipc_space_t     space,
                    372:        mach_port_t     oname,
                    373:        mach_port_t     nname)
                    374: {
                    375:        if (space == IS_NULL)
                    376:                return KERN_INVALID_TASK;
                    377: 
                    378:        if (!MACH_PORT_VALID(nname))
                    379:                return KERN_INVALID_VALUE;
                    380: 
                    381:        return ipc_object_rename(space, oname, nname);
                    382: }
                    383: 
                    384: /*
                    385:  *     Routine:        mach_port_allocate_name [kernel call]
                    386:  *     Purpose:
                    387:  *             Allocates a right in a space, using a specific name
                    388:  *             for the new right.  Possible rights:
                    389:  *                     MACH_PORT_RIGHT_RECEIVE
                    390:  *                     MACH_PORT_RIGHT_PORT_SET
                    391:  *                     MACH_PORT_RIGHT_DEAD_NAME
                    392:  *
                    393:  *             A new port (allocated with MACH_PORT_RIGHT_RECEIVE)
                    394:  *             has no extant send or send-once rights and no queued
                    395:  *             messages.  Its queue limit is MACH_PORT_QLIMIT_DEFAULT
                    396:  *             and its make-send count is 0.  It is not a member of
                    397:  *             a port set.  It has no registered no-senders or
                    398:  *             port-destroyed notification requests.
                    399:  *
                    400:  *             A new port set has no members.
                    401:  *
                    402:  *             A new dead name has one user reference.
                    403:  *     Conditions:
                    404:  *             Nothing locked.
                    405:  *     Returns:
                    406:  *             KERN_SUCCESS            The right is allocated.
                    407:  *             KERN_INVALID_TASK       The space is null.
                    408:  *             KERN_INVALID_TASK       The space is dead.
                    409:  *             KERN_INVALID_VALUE      The name isn't a legal name.
                    410:  *             KERN_INVALID_VALUE      "right" isn't a legal kind of right.
                    411:  *             KERN_NAME_EXISTS        The name already denotes a right.
                    412:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                    413:  */
                    414: 
                    415: kern_return_t
1.1.1.4   root      416: mach_port_allocate_name(
                    417:        ipc_space_t             space,
                    418:        mach_port_right_t       right,
                    419:        mach_port_t             name)
1.1       root      420: {
                    421:        kern_return_t kr;
                    422: 
                    423:        if (space == IS_NULL)
                    424:                return KERN_INVALID_TASK;
                    425: 
                    426:        if (!MACH_PORT_VALID(name))
                    427:                return KERN_INVALID_VALUE;
                    428: 
                    429:        switch (right) {
                    430:            case MACH_PORT_RIGHT_RECEIVE: {
                    431:                ipc_port_t port;
                    432: 
                    433:                kr = ipc_port_alloc_name(space, name, &port);
                    434:                if (kr == KERN_SUCCESS)
                    435:                        ip_unlock(port);
                    436:                break;
                    437:            }
                    438: 
                    439:            case MACH_PORT_RIGHT_PORT_SET: {
                    440:                ipc_pset_t pset;
                    441: 
                    442:                kr = ipc_pset_alloc_name(space, name, &pset);
                    443:                if (kr == KERN_SUCCESS)
                    444:                        ips_unlock(pset);
                    445:                break;
                    446:            }
                    447: 
                    448:            case MACH_PORT_RIGHT_DEAD_NAME:
                    449:                kr = ipc_object_alloc_dead_name(space, name);
                    450:                break;
                    451: 
                    452:            default:
                    453:                kr = KERN_INVALID_VALUE;
                    454:                break;
                    455:        }
                    456: 
                    457:        return kr;
                    458: }
                    459: 
                    460: /*
                    461:  *     Routine:        mach_port_allocate [kernel call]
                    462:  *     Purpose:
                    463:  *             Allocates a right in a space.  Like mach_port_allocate_name,
                    464:  *             except that the implementation picks a name for the right.
                    465:  *             The name may be any legal name in the space that doesn't
                    466:  *             currently denote a right.
                    467:  *     Conditions:
                    468:  *             Nothing locked.
                    469:  *     Returns:
                    470:  *             KERN_SUCCESS            The right is allocated.
                    471:  *             KERN_INVALID_TASK       The space is null.
                    472:  *             KERN_INVALID_TASK       The space is dead.
                    473:  *             KERN_INVALID_VALUE      "right" isn't a legal kind of right.
                    474:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                    475:  *             KERN_NO_SPACE           No room in space for another right.
                    476:  */
                    477: 
                    478: kern_return_t
1.1.1.4   root      479: mach_port_allocate(
                    480:        ipc_space_t             space,
                    481:        mach_port_right_t       right,
                    482:        mach_port_t             *namep)
1.1       root      483: {
                    484:        kern_return_t kr;
                    485: 
                    486:        if (space == IS_NULL)
                    487:                return KERN_INVALID_TASK;
                    488: 
                    489:        switch (right) {
                    490:            case MACH_PORT_RIGHT_RECEIVE: {
                    491:                ipc_port_t port;
                    492: 
                    493:                kr = ipc_port_alloc(space, namep, &port);
                    494:                if (kr == KERN_SUCCESS)
                    495:                        ip_unlock(port);
                    496:                break;
                    497:            }
                    498: 
                    499:            case MACH_PORT_RIGHT_PORT_SET: {
                    500:                ipc_pset_t pset;
                    501: 
                    502:                kr = ipc_pset_alloc(space, namep, &pset);
                    503:                if (kr == KERN_SUCCESS)
                    504:                        ips_unlock(pset);
                    505:                break;
                    506:            }
                    507: 
                    508:            case MACH_PORT_RIGHT_DEAD_NAME:
                    509:                kr = ipc_object_alloc_dead(space, namep);
                    510:                break;
                    511: 
                    512:            default:
                    513:                kr = KERN_INVALID_VALUE;
                    514:                break;
                    515:        }
                    516: 
                    517:        return (kr);
                    518: }
                    519: 
                    520: /*
                    521:  *     Routine:        mach_port_destroy [kernel call]
                    522:  *     Purpose:
                    523:  *             Cleans up and destroys all rights denoted by a name
                    524:  *             in a space.  The destruction of a receive right
                    525:  *             destroys the port, unless a port-destroyed request
                    526:  *             has been made for it; the destruction of a port-set right
                    527:  *             destroys the port set.
                    528:  *     Conditions:
                    529:  *             Nothing locked.
                    530:  *     Returns:
                    531:  *             KERN_SUCCESS            The name is destroyed.
                    532:  *             KERN_INVALID_TASK       The space is null.
                    533:  *             KERN_INVALID_TASK       The space is dead.
                    534:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    535:  */
                    536: 
1.1.1.4   root      537: static volatile boolean_t mach_port_deallocate_debug = FALSE;
1.1.1.3   root      538: 
1.1       root      539: kern_return_t
                    540: mach_port_destroy(
                    541:        ipc_space_t     space,
                    542:        mach_port_t     name)
                    543: {
                    544:        ipc_entry_t entry;
                    545:        kern_return_t kr;
                    546: 
                    547:        if (space == IS_NULL)
                    548:                return KERN_INVALID_TASK;
                    549: 
                    550:        kr = ipc_right_lookup_write(space, name, &entry);
1.1.1.3   root      551:        if (kr != KERN_SUCCESS) {
1.1.1.4   root      552:                if (MACH_PORT_VALID (name) && space == current_space()) {
                    553:                        printf("task %.*s destroying a bogus port %lu, most probably a bug.\n", sizeof current_task()->name, current_task()->name, name);
1.1.1.3   root      554:                        if (mach_port_deallocate_debug)
                    555:                                SoftDebugger("mach_port_deallocate");
                    556:                }
1.1       root      557:                return kr;
1.1.1.3   root      558:        }
1.1       root      559:        /* space is write-locked and active */
                    560: 
                    561:        kr = ipc_right_destroy(space, name, entry); /* unlocks space */
                    562:        return kr;
                    563: }
                    564: 
                    565: /*
                    566:  *     Routine:        mach_port_deallocate [kernel call]
                    567:  *     Purpose:
                    568:  *             Deallocates a user reference from a send right,
                    569:  *             send-once right, or a dead-name right.  May
                    570:  *             deallocate the right, if this is the last uref,
                    571:  *             and destroy the name, if it doesn't denote
                    572:  *             other rights.
                    573:  *     Conditions:
                    574:  *             Nothing locked.
                    575:  *     Returns:
                    576:  *             KERN_SUCCESS            The uref is deallocated.
                    577:  *             KERN_INVALID_TASK       The space is null.
                    578:  *             KERN_INVALID_TASK       The space is dead.
                    579:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    580:  *             KERN_INVALID_RIGHT      The right isn't correct.
                    581:  */
                    582: 
                    583: kern_return_t
                    584: mach_port_deallocate(
                    585:        ipc_space_t     space,
                    586:        mach_port_t     name)
                    587: {
                    588:        ipc_entry_t entry;
                    589:        kern_return_t kr;
                    590: 
                    591:        if (space == IS_NULL)
                    592:                return KERN_INVALID_TASK;
                    593: 
                    594:        kr = ipc_right_lookup_write(space, name, &entry);
1.1.1.3   root      595:        if (kr != KERN_SUCCESS) {
1.1.1.4   root      596:                if (MACH_PORT_VALID (name) && space == current_space()) {
                    597:                        printf("task %.*s deallocating a bogus port %lu, most probably a bug.\n", sizeof current_task()->name, current_task()->name, name);
1.1.1.3   root      598:                        if (mach_port_deallocate_debug)
                    599:                                SoftDebugger("mach_port_deallocate");
                    600:                }
1.1       root      601:                return kr;
1.1.1.3   root      602:        }
1.1       root      603:        /* space is write-locked */
                    604: 
                    605:        kr = ipc_right_dealloc(space, name, entry); /* unlocks space */
                    606:        return kr;
                    607: }
                    608: 
                    609: /*
                    610:  *     Routine:        mach_port_get_refs [kernel call]
                    611:  *     Purpose:
                    612:  *             Retrieves the number of user references held by a right.
                    613:  *             Receive rights, port-set rights, and send-once rights
                    614:  *             always have one user reference.  Returns zero if the
                    615:  *             name denotes a right, but not the queried right.
                    616:  *     Conditions:
                    617:  *             Nothing locked.
                    618:  *     Returns:
                    619:  *             KERN_SUCCESS            Number of urefs returned.
                    620:  *             KERN_INVALID_TASK       The space is null.
                    621:  *             KERN_INVALID_TASK       The space is dead.
                    622:  *             KERN_INVALID_VALUE      "right" isn't a legal value.
                    623:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    624:  */
                    625: 
                    626: kern_return_t
                    627: mach_port_get_refs(
                    628:        ipc_space_t             space,
                    629:        mach_port_t             name,
                    630:        mach_port_right_t       right,
                    631:        mach_port_urefs_t       *urefsp)
                    632: {
                    633:        mach_port_type_t type;
                    634:        mach_port_urefs_t urefs;
                    635:        ipc_entry_t entry;
                    636:        kern_return_t kr;
                    637: 
                    638:        if (space == IS_NULL)
                    639:                return KERN_INVALID_TASK;
                    640: 
                    641:        if (right >= MACH_PORT_RIGHT_NUMBER)
                    642:                return KERN_INVALID_VALUE;
                    643: 
                    644:        kr = ipc_right_lookup_write(space, name, &entry);
                    645:        if (kr != KERN_SUCCESS)
                    646:                return kr;
                    647:        /* space is write-locked and active */
                    648: 
                    649:        kr = ipc_right_info(space, name, entry, &type, &urefs); /* unlocks */
                    650:        if (kr != KERN_SUCCESS)
                    651:                return kr;      /* space is unlocked */
                    652:        is_write_unlock(space);
                    653: 
                    654:        if (type & MACH_PORT_TYPE(right))
                    655:                switch (right) {
                    656:                    case MACH_PORT_RIGHT_SEND_ONCE:
                    657:                        assert(urefs == 1);
                    658:                        /* fall-through */
                    659: 
                    660:                    case MACH_PORT_RIGHT_PORT_SET:
                    661:                    case MACH_PORT_RIGHT_RECEIVE:
                    662:                        *urefsp = 1;
                    663:                        break;
                    664: 
                    665:                    case MACH_PORT_RIGHT_DEAD_NAME:
                    666:                    case MACH_PORT_RIGHT_SEND:
                    667:                        assert(urefs > 0);
                    668:                        *urefsp = urefs;
                    669:                        break;
                    670: 
                    671:                    default:
                    672:                        panic("mach_port_get_refs: strange rights");
                    673:                }
                    674:        else
                    675:                *urefsp = 0;
                    676: 
                    677:        return kr;
                    678: }
                    679: 
                    680: /*
                    681:  *     Routine:        mach_port_mod_refs
                    682:  *     Purpose:
                    683:  *             Modifies the number of user references held by a right.
                    684:  *             The resulting number of user references must be non-negative.
                    685:  *             If it is zero, the right is deallocated.  If the name
                    686:  *             doesn't denote other rights, it is destroyed.
                    687:  *     Conditions:
                    688:  *             Nothing locked.
                    689:  *     Returns:
                    690:  *             KERN_SUCCESS            Modified number of urefs.
                    691:  *             KERN_INVALID_TASK       The space is null.
                    692:  *             KERN_INVALID_TASK       The space is dead.
                    693:  *             KERN_INVALID_VALUE      "right" isn't a legal value.
                    694:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    695:  *             KERN_INVALID_RIGHT      Name doesn't denote specified right.
                    696:  *             KERN_INVALID_VALUE      Impossible modification to urefs.
                    697:  *             KERN_UREFS_OVERFLOW     Urefs would overflow.
                    698:  */
                    699: 
                    700: kern_return_t
                    701: mach_port_mod_refs(
                    702:        ipc_space_t             space,
                    703:        mach_port_t             name,
                    704:        mach_port_right_t       right,
                    705:        mach_port_delta_t       delta)
                    706: {
                    707:        ipc_entry_t entry;
                    708:        kern_return_t kr;
                    709: 
                    710:        if (space == IS_NULL)
                    711:                return KERN_INVALID_TASK;
                    712: 
                    713:        if (right >= MACH_PORT_RIGHT_NUMBER)
                    714:                return KERN_INVALID_VALUE;
                    715: 
                    716:        kr = ipc_right_lookup_write(space, name, &entry);
1.1.1.4   root      717:        if (kr != KERN_SUCCESS) {
                    718:                if (MACH_PORT_VALID (name) && space == current_space()) {
                    719:                        printf("task %.*s %screasing a bogus port "
                    720:                               "%lu by %d, most probably a bug.\n",
                    721:                               sizeof current_task()->name,
                    722:                               current_task()->name,
                    723:                               delta < 0 ? "de" : "in", name,
                    724:                               delta < 0 ? -delta : delta);
                    725:                        if (mach_port_deallocate_debug)
                    726:                                SoftDebugger("mach_port_mod_refs");
                    727:                }
1.1       root      728:                return kr;
1.1.1.4   root      729:        }
1.1       root      730:        /* space is write-locked and active */
                    731: 
                    732:        kr = ipc_right_delta(space, name, entry, right, delta); /* unlocks */
                    733:        return kr;
                    734: }
                    735: 
                    736: /*
                    737:  *     Routine:        mach_port_set_qlimit [kernel call]
                    738:  *     Purpose:
                    739:  *             Changes a receive right's queue limit.
                    740:  *             The new queue limit must be between 0 and
                    741:  *             MACH_PORT_QLIMIT_MAX, inclusive.
                    742:  *     Conditions:
                    743:  *             Nothing locked.
                    744:  *     Returns:
                    745:  *             KERN_SUCCESS            Set queue limit.
                    746:  *             KERN_INVALID_TASK       The space is null.
                    747:  *             KERN_INVALID_TASK       The space is dead.
                    748:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    749:  *             KERN_INVALID_RIGHT      Name doesn't denote receive rights.
                    750:  *             KERN_INVALID_VALUE      Illegal queue limit.
                    751:  */
                    752: 
                    753: kern_return_t
1.1.1.4   root      754: mach_port_set_qlimit(
                    755:        ipc_space_t             space,
                    756:        mach_port_t             name,
                    757:        mach_port_msgcount_t    qlimit)
1.1       root      758: {
                    759:        ipc_port_t port;
                    760:        kern_return_t kr;
                    761: 
                    762:        if (space == IS_NULL)
                    763:                return KERN_INVALID_TASK;
                    764: 
                    765:        if (qlimit > MACH_PORT_QLIMIT_MAX)
                    766:                return KERN_INVALID_VALUE;
                    767: 
                    768:        kr = ipc_port_translate_receive(space, name, &port);
                    769:        if (kr != KERN_SUCCESS)
                    770:                return kr;
                    771:        /* port is locked and active */
                    772: 
                    773:        ipc_port_set_qlimit(port, qlimit);
                    774: 
                    775:        ip_unlock(port);
                    776:        return KERN_SUCCESS;
                    777: }
                    778: 
                    779: /*
                    780:  *     Routine:        mach_port_set_mscount [kernel call]
                    781:  *     Purpose:
                    782:  *             Changes a receive right's make-send count.
                    783:  *     Conditions:
                    784:  *             Nothing locked.
                    785:  *     Returns:
                    786:  *             KERN_SUCCESS            Set make-send count.
                    787:  *             KERN_INVALID_TASK       The space is null.
                    788:  *             KERN_INVALID_TASK       The space is dead.
                    789:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    790:  *             KERN_INVALID_RIGHT      Name doesn't denote receive rights.
                    791:  */
                    792: 
                    793: kern_return_t
                    794: mach_port_set_mscount(
                    795:        ipc_space_t             space,
                    796:        mach_port_t             name,
                    797:        mach_port_mscount_t     mscount)
                    798: {
                    799:        ipc_port_t port;
                    800:        kern_return_t kr;
                    801: 
                    802:        if (space == IS_NULL)
                    803:                return KERN_INVALID_TASK;
                    804: 
                    805:        kr = ipc_port_translate_receive(space, name, &port);
                    806:        if (kr != KERN_SUCCESS)
                    807:                return kr;
                    808:        /* port is locked and active */
                    809: 
                    810:        ipc_port_set_mscount(port, mscount);
                    811: 
                    812:        ip_unlock(port);
                    813:        return KERN_SUCCESS;
                    814: }
                    815: 
                    816: /*
                    817:  *     Routine:        mach_port_set_seqno [kernel call]
                    818:  *     Purpose:
                    819:  *             Changes a receive right's sequence number.
                    820:  *     Conditions:
                    821:  *             Nothing locked.
                    822:  *     Returns:
                    823:  *             KERN_SUCCESS            Set sequence number.
                    824:  *             KERN_INVALID_TASK       The space is null.
                    825:  *             KERN_INVALID_TASK       The space is dead.
                    826:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    827:  *             KERN_INVALID_RIGHT      Name doesn't denote receive rights.
                    828:  */
                    829: 
                    830: kern_return_t
                    831: mach_port_set_seqno(
                    832:        ipc_space_t             space,
                    833:        mach_port_t             name,
                    834:        mach_port_seqno_t       seqno)
                    835: {
                    836:        ipc_port_t port;
                    837:        kern_return_t kr;
                    838: 
                    839:        if (space == IS_NULL)
                    840:                return KERN_INVALID_TASK;
                    841: 
                    842:        kr = ipc_port_translate_receive(space, name, &port);
                    843:        if (kr != KERN_SUCCESS)
                    844:                return kr;
                    845:        /* port is locked and active */
                    846: 
                    847:        ipc_port_set_seqno(port, seqno);
                    848: 
                    849:        ip_unlock(port);
                    850:        return KERN_SUCCESS;
                    851: }
                    852: 
                    853: /*
                    854:  *     Routine:        mach_port_gst_helper
                    855:  *     Purpose:
                    856:  *             A helper function for mach_port_get_set_status.
                    857:  */
                    858: 
                    859: void
                    860: mach_port_gst_helper(
                    861:        ipc_pset_t              pset,
                    862:        ipc_port_t              port,
                    863:        ipc_entry_num_t         maxnames,
                    864:        mach_port_t             *names,
                    865:        ipc_entry_num_t         *actualp)
                    866: {
                    867:        ipc_pset_t ip_pset;
                    868:        mach_port_t name;
                    869: 
                    870:        assert(port != IP_NULL);
                    871: 
                    872:        ip_lock(port);
                    873:        assert(ip_active(port));
                    874: 
                    875:        name = port->ip_receiver_name;
                    876:        assert(name != MACH_PORT_NULL);
                    877:        ip_pset = port->ip_pset;
                    878: 
                    879:        ip_unlock(port);
                    880: 
                    881:        if (pset == ip_pset) {
                    882:                ipc_entry_num_t actual = *actualp;
                    883: 
                    884:                if (actual < maxnames)
                    885:                        names[actual] = name;
                    886: 
                    887:                *actualp = actual+1;
                    888:        }
                    889: }
                    890: 
                    891: /*
                    892:  *     Routine:        mach_port_get_set_status [kernel call]
                    893:  *     Purpose:
                    894:  *             Retrieves a list of members in a port set.
                    895:  *             Returns the space's name for each receive right member.
                    896:  *     Conditions:
                    897:  *             Nothing locked.
                    898:  *     Returns:
                    899:  *             KERN_SUCCESS            Retrieved list of members.
                    900:  *             KERN_INVALID_TASK       The space is null.
                    901:  *             KERN_INVALID_TASK       The space is dead.
                    902:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                    903:  *             KERN_INVALID_RIGHT      Name doesn't denote a port set.
                    904:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                    905:  */
                    906: 
                    907: kern_return_t
                    908: mach_port_get_set_status(
                    909:        ipc_space_t                     space,
                    910:        mach_port_t                     name,
                    911:        mach_port_t                     **members,
                    912:        mach_msg_type_number_t          *membersCnt)
                    913: {
                    914:        ipc_entry_num_t actual;         /* this many members */
                    915:        ipc_entry_num_t maxnames;       /* space for this many members */
                    916:        kern_return_t kr;
                    917: 
                    918:        vm_size_t size;         /* size of allocated memory */
                    919:        vm_offset_t addr;       /* allocated memory */
                    920:        vm_map_copy_t memory;   /* copied-in memory */
                    921: 
                    922:        if (space == IS_NULL)
                    923:                return KERN_INVALID_TASK;
                    924: 
                    925:        size = PAGE_SIZE;       /* initial guess */
                    926: 
                    927:        for (;;) {
1.1.1.5 ! root      928:                ipc_entry_t entry;
1.1       root      929:                mach_port_t *names;
                    930:                ipc_pset_t pset;
                    931: 
                    932:                kr = vm_allocate(ipc_kernel_map, &addr, size, TRUE);
1.1.1.3   root      933:                if (kr != KERN_SUCCESS) {
                    934:                        printf_once("no more room in ipc_kernel_map\n");
1.1       root      935:                        return KERN_RESOURCE_SHORTAGE;
1.1.1.3   root      936:                }
1.1       root      937: 
                    938:                /* can't fault while we hold locks */
                    939: 
                    940:                kr = vm_map_pageable(ipc_kernel_map, addr, addr + size,
                    941:                                     VM_PROT_READ|VM_PROT_WRITE);
                    942:                assert(kr == KERN_SUCCESS);
                    943: 
                    944:                kr = ipc_right_lookup_read(space, name, &entry);
                    945:                if (kr != KERN_SUCCESS) {
                    946:                        kmem_free(ipc_kernel_map, addr, size);
                    947:                        return kr;
                    948:                }
                    949:                /* space is read-locked and active */
                    950: 
                    951:                if (IE_BITS_TYPE(entry->ie_bits) != MACH_PORT_TYPE_PORT_SET) {
                    952:                        is_read_unlock(space);
                    953:                        kmem_free(ipc_kernel_map, addr, size);
                    954:                        return KERN_INVALID_RIGHT;
                    955:                }
                    956: 
                    957:                pset = (ipc_pset_t) entry->ie_object;
                    958:                assert(pset != IPS_NULL);
                    959:                /* the port set must be active */
                    960: 
                    961:                names = (mach_port_t *) addr;
                    962:                maxnames = size / sizeof(mach_port_t);
                    963:                actual = 0;
                    964: 
1.1.1.5 ! root      965:                ipc_entry_t ientry;
        !           966:                struct rdxtree_iter iter;
        !           967:                rdxtree_for_each(&space->is_map, &iter, ientry) {
1.1       root      968:                        ipc_entry_bits_t bits = ientry->ie_bits;
                    969: 
                    970:                        if (bits & MACH_PORT_TYPE_RECEIVE) {
                    971:                                ipc_port_t port =
                    972:                                        (ipc_port_t) ientry->ie_object;
                    973: 
                    974:                                mach_port_gst_helper(pset, port, maxnames,
                    975:                                                     names, &actual);
                    976:                        }
                    977:                }
                    978: 
                    979:                is_read_unlock(space);
                    980: 
                    981:                if (actual <= maxnames)
                    982:                        break;
                    983: 
                    984:                /* didn't have enough memory; allocate more */
                    985: 
                    986:                kmem_free(ipc_kernel_map, addr, size);
                    987:                size = round_page(actual * sizeof(mach_port_t)) + PAGE_SIZE;
                    988:        }
                    989: 
                    990:        if (actual == 0) {
                    991:                memory = VM_MAP_COPY_NULL;
                    992: 
                    993:                kmem_free(ipc_kernel_map, addr, size);
                    994:        } else {
                    995:                vm_size_t size_used;
                    996: 
                    997:                size_used = round_page(actual * sizeof(mach_port_t));
                    998: 
                    999:                /*
                   1000:                 *      Make used memory pageable and get it into
                   1001:                 *      copied-in form.  Free any unused memory.
                   1002:                 */
                   1003: 
                   1004:                kr = vm_map_pageable(ipc_kernel_map,
                   1005:                                     addr, addr + size_used,
                   1006:                                     VM_PROT_NONE);
                   1007:                assert(kr == KERN_SUCCESS);
                   1008: 
                   1009:                kr = vm_map_copyin(ipc_kernel_map, addr, size_used,
                   1010:                                   TRUE, &memory);
                   1011:                assert(kr == KERN_SUCCESS);
                   1012: 
                   1013:                if (size_used != size)
                   1014:                        kmem_free(ipc_kernel_map,
                   1015:                                  addr + size_used, size - size_used);
                   1016:        }
                   1017: 
                   1018:        *members = (mach_port_t *) memory;
                   1019:        *membersCnt = actual;
                   1020:        return KERN_SUCCESS;
                   1021: }
                   1022: 
                   1023: /*
                   1024:  *     Routine:        mach_port_move_member [kernel call]
                   1025:  *     Purpose:
                   1026:  *             If after is MACH_PORT_NULL, removes member
                   1027:  *             from the port set it is in.  Otherwise, adds
                   1028:  *             member to after, removing it from any set
                   1029:  *             it might already be in.
                   1030:  *     Conditions:
                   1031:  *             Nothing locked.
                   1032:  *     Returns:
                   1033:  *             KERN_SUCCESS            Moved the port.
                   1034:  *             KERN_INVALID_TASK       The space is null.
                   1035:  *             KERN_INVALID_TASK       The space is dead.
                   1036:  *             KERN_INVALID_NAME       Member didn't denote a right.
                   1037:  *             KERN_INVALID_RIGHT      Member didn't denote a receive right.
                   1038:  *             KERN_INVALID_NAME       After didn't denote a right.
                   1039:  *             KERN_INVALID_RIGHT      After didn't denote a port set right.
                   1040:  *             KERN_NOT_IN_SET
                   1041:  *                     After is MACH_PORT_NULL and Member isn't in a port set.
                   1042:  */
                   1043: 
                   1044: kern_return_t
                   1045: mach_port_move_member(
                   1046:        ipc_space_t     space,
                   1047:        mach_port_t     member,
                   1048:        mach_port_t     after)
                   1049: {
                   1050:        ipc_entry_t entry;
                   1051:        ipc_port_t port;
                   1052:        ipc_pset_t nset;
                   1053:        kern_return_t kr;
                   1054: 
                   1055:        if (space == IS_NULL)
                   1056:                return KERN_INVALID_TASK;
                   1057: 
                   1058:        kr = ipc_right_lookup_read(space, member, &entry);
                   1059:        if (kr != KERN_SUCCESS)
                   1060:                return kr;
                   1061:        /* space is read-locked and active */
                   1062: 
                   1063:        if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
                   1064:                is_read_unlock(space);
                   1065:                return KERN_INVALID_RIGHT;
                   1066:        }
                   1067: 
                   1068:        port = (ipc_port_t) entry->ie_object;
                   1069:        assert(port != IP_NULL);
                   1070: 
                   1071:        if (after == MACH_PORT_NULL)
                   1072:                nset = IPS_NULL;
                   1073:        else {
                   1074:                entry = ipc_entry_lookup(space, after);
                   1075:                if (entry == IE_NULL) {
                   1076:                        is_read_unlock(space);
                   1077:                        return KERN_INVALID_NAME;
                   1078:                }
                   1079: 
                   1080:                if ((entry->ie_bits & MACH_PORT_TYPE_PORT_SET) == 0) {
                   1081:                        is_read_unlock(space);
                   1082:                        return KERN_INVALID_RIGHT;
                   1083:                }
                   1084: 
                   1085:                nset = (ipc_pset_t) entry->ie_object;
                   1086:                assert(nset != IPS_NULL);
                   1087:        }
                   1088: 
                   1089:        kr = ipc_pset_move(space, port, nset);
                   1090:        /* space is unlocked */
                   1091:        return kr;
                   1092: }
                   1093: 
                   1094: /*
                   1095:  *     Routine:        mach_port_request_notification [kernel call]
                   1096:  *     Purpose:
                   1097:  *             Requests a notification.  The caller supplies
                   1098:  *             a send-once right for the notification to use,
                   1099:  *             and the call returns the previously registered
                   1100:  *             send-once right, if any.  Possible types:
                   1101:  *
                   1102:  *             MACH_NOTIFY_PORT_DESTROYED
                   1103:  *                     Requests a port-destroyed notification
                   1104:  *                     for a receive right.  Sync should be zero.
                   1105:  *             MACH_NOTIFY_NO_SENDERS
                   1106:  *                     Requests a no-senders notification for a
                   1107:  *                     receive right.  If there are currently no
                   1108:  *                     senders, sync is less than or equal to the
                   1109:  *                     current make-send count, and a send-once right
                   1110:  *                     is supplied, then an immediate no-senders
                   1111:  *                     notification is generated.
                   1112:  *             MACH_NOTIFY_DEAD_NAME
                   1113:  *                     Requests a dead-name notification for a send
                   1114:  *                     or receive right.  If the name is already a
                   1115:  *                     dead name, sync is non-zero, and a send-once
                   1116:  *                     right is supplied, then an immediate dead-name
                   1117:  *                     notification is generated.
                   1118:  *     Conditions:
                   1119:  *             Nothing locked.
                   1120:  *     Returns:
                   1121:  *             KERN_SUCCESS            Requested a notification.
                   1122:  *             KERN_INVALID_TASK       The space is null.
                   1123:  *             KERN_INVALID_TASK       The space is dead.
                   1124:  *             KERN_INVALID_VALUE      Bad id value.
                   1125:  *             KERN_INVALID_NAME       Name doesn't denote a right.
                   1126:  *             KERN_INVALID_RIGHT      Name doesn't denote appropriate right.
                   1127:  *             KERN_INVALID_CAPABILITY The notify port is dead.
                   1128:  *     MACH_NOTIFY_PORT_DESTROYED:
                   1129:  *             KERN_INVALID_VALUE      Sync isn't zero.
                   1130:  *     MACH_NOTIFY_DEAD_NAME:
                   1131:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                   1132:  *             KERN_INVALID_ARGUMENT   Name denotes dead name, but
                   1133:  *                     sync is zero or notify is IP_NULL.
                   1134:  *             KERN_UREFS_OVERFLOW     Name denotes dead name, but
                   1135:  *                     generating immediate notif. would overflow urefs.
                   1136:  */
                   1137: 
                   1138: kern_return_t
                   1139: mach_port_request_notification(
                   1140:        ipc_space_t             space,
                   1141:        mach_port_t             name,
                   1142:        mach_msg_id_t           id,
                   1143:        mach_port_mscount_t     sync,
                   1144:        ipc_port_t              notify,
                   1145:        ipc_port_t              *previousp)
                   1146: {
                   1147:        kern_return_t kr;
                   1148: 
                   1149:        if (space == IS_NULL)
                   1150:                return KERN_INVALID_TASK;
                   1151: 
                   1152:        if (notify == IP_DEAD)
                   1153:                return KERN_INVALID_CAPABILITY;
                   1154: 
                   1155:        switch (id) {
                   1156:            case MACH_NOTIFY_PORT_DESTROYED: {
                   1157:                ipc_port_t port, previous;
                   1158: 
                   1159:                if (sync != 0)
                   1160:                        return KERN_INVALID_VALUE;
                   1161: 
                   1162:                kr = ipc_port_translate_receive(space, name, &port);
                   1163:                if (kr != KERN_SUCCESS)
                   1164:                        return kr;
                   1165:                /* port is locked and active */
                   1166: 
                   1167:                ipc_port_pdrequest(port, notify, &previous);
                   1168:                /* port is unlocked */
                   1169: 
                   1170:                *previousp = previous;
                   1171:                break;
                   1172:            }
                   1173: 
                   1174:            case MACH_NOTIFY_NO_SENDERS: {
                   1175:                ipc_port_t port;
                   1176: 
                   1177:                kr = ipc_port_translate_receive(space, name, &port);
                   1178:                if (kr != KERN_SUCCESS)
                   1179:                        return kr;
                   1180:                /* port is locked and active */
                   1181: 
                   1182:                ipc_port_nsrequest(port, sync, notify, previousp);
                   1183:                /* port is unlocked */
                   1184:                break;
                   1185:            }
                   1186: 
                   1187:            case MACH_NOTIFY_DEAD_NAME:
                   1188:                kr = ipc_right_dnrequest(space, name, sync != 0,
                   1189:                                         notify, previousp);
                   1190:                if (kr != KERN_SUCCESS)
                   1191:                        return kr;
                   1192:                break;
                   1193: 
                   1194:            default:
                   1195:                return KERN_INVALID_VALUE;
                   1196:        }
                   1197: 
                   1198:        return KERN_SUCCESS;
                   1199: }
                   1200: 
                   1201: /*
                   1202:  *     Routine:        mach_port_insert_right [kernel call]
                   1203:  *     Purpose:
                   1204:  *             Inserts a right into a space, as if the space
                   1205:  *             voluntarily received the right in a message,
                   1206:  *             except that the right gets the specified name.
                   1207:  *     Conditions:
                   1208:  *             Nothing locked.
                   1209:  *     Returns:
                   1210:  *             KERN_SUCCESS            Inserted the right.
                   1211:  *             KERN_INVALID_TASK       The space is null.
                   1212:  *             KERN_INVALID_TASK       The space is dead.
                   1213:  *             KERN_INVALID_VALUE      The name isn't a legal name.
                   1214:  *             KERN_NAME_EXISTS        The name already denotes a right.
                   1215:  *             KERN_INVALID_VALUE      Message doesn't carry a port right.
                   1216:  *             KERN_INVALID_CAPABILITY Port is null or dead.
                   1217:  *             KERN_UREFS_OVERFLOW     Urefs limit would be exceeded.
                   1218:  *             KERN_RIGHT_EXISTS       Space has rights under another name.
                   1219:  *             KERN_RESOURCE_SHORTAGE  Couldn't allocate memory.
                   1220:  */
                   1221: 
                   1222: kern_return_t
                   1223: mach_port_insert_right(
                   1224:        ipc_space_t             space,
                   1225:        mach_port_t             name,
                   1226:        ipc_port_t              poly,
                   1227:        mach_msg_type_name_t    polyPoly)
                   1228: {
                   1229:        if (space == IS_NULL)
                   1230:                return KERN_INVALID_TASK;
                   1231: 
                   1232:        if (!MACH_PORT_VALID(name) ||
                   1233:            !MACH_MSG_TYPE_PORT_ANY_RIGHT(polyPoly))
                   1234:                return KERN_INVALID_VALUE;
                   1235: 
1.1.1.3   root     1236:        if (!IO_VALID((ipc_object_t)poly))
1.1       root     1237:                return KERN_INVALID_CAPABILITY;
                   1238: 
1.1.1.3   root     1239:        return ipc_object_copyout_name(space, (ipc_object_t)poly,
                   1240:                                       polyPoly, FALSE, name);
1.1       root     1241: }
                   1242: 
                   1243: /*
                   1244:  *     Routine:        mach_port_extract_right [kernel call]
                   1245:  *     Purpose:
                   1246:  *             Extracts a right from a space, as if the space
                   1247:  *             voluntarily sent the right to the caller.
                   1248:  *     Conditions:
                   1249:  *             Nothing locked.
                   1250:  *     Returns:
                   1251:  *             KERN_SUCCESS            Extracted the right.
                   1252:  *             KERN_INVALID_TASK       The space is null.
                   1253:  *             KERN_INVALID_TASK       The space is dead.
                   1254:  *             KERN_INVALID_VALUE      Requested type isn't a port right.
                   1255:  *             KERN_INVALID_NAME       Name doesn't denote a right.
                   1256:  *             KERN_INVALID_RIGHT      Name doesn't denote appropriate right.
                   1257:  */
                   1258: 
                   1259: kern_return_t
                   1260: mach_port_extract_right(
                   1261:        ipc_space_t             space,
                   1262:        mach_port_t             name,
                   1263:        mach_msg_type_name_t    msgt_name,
                   1264:        ipc_port_t              *poly,
                   1265:        mach_msg_type_name_t    *polyPoly)
                   1266: {
                   1267:        kern_return_t kr;
                   1268: 
                   1269:        if (space == IS_NULL)
                   1270:                return KERN_INVALID_TASK;
                   1271: 
                   1272:        if (!MACH_MSG_TYPE_PORT_ANY(msgt_name))
                   1273:                return KERN_INVALID_VALUE;
                   1274: 
                   1275:        kr = ipc_object_copyin(space, name, msgt_name, (ipc_object_t *) poly);
                   1276: 
                   1277:        if (kr == KERN_SUCCESS)
                   1278:                *polyPoly = ipc_object_copyin_type(msgt_name);
                   1279:        return kr;
                   1280: }
                   1281: 
                   1282: /*
                   1283:  *     Routine:        mach_port_get_receive_status [kernel call]
                   1284:  *     Purpose:
                   1285:  *             Retrieves mucho info about a receive right.
                   1286:  *     Conditions:
                   1287:  *             Nothing locked.
                   1288:  *     Returns:
                   1289:  *             KERN_SUCCESS            Retrieved status.
                   1290:  *             KERN_INVALID_TASK       The space is null.
                   1291:  *             KERN_INVALID_TASK       The space is dead.
                   1292:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                   1293:  *             KERN_INVALID_RIGHT      Name doesn't denote receive rights.
                   1294:  */
                   1295: 
                   1296: kern_return_t
1.1.1.4   root     1297: mach_port_get_receive_status(
                   1298:        ipc_space_t             space,
                   1299:        mach_port_t             name,
                   1300:        mach_port_status_t      *statusp)
1.1       root     1301: {
                   1302:        ipc_port_t port;
                   1303:        kern_return_t kr;
                   1304: 
                   1305:        if (space == IS_NULL)
                   1306:                return KERN_INVALID_TASK;
                   1307: 
                   1308:        kr = ipc_port_translate_receive(space, name, &port);
                   1309:        if (kr != KERN_SUCCESS)
                   1310:                return kr;
                   1311:        /* port is locked and active */
                   1312: 
                   1313:        if (port->ip_pset != IPS_NULL) {
                   1314:                ipc_pset_t pset = port->ip_pset;
                   1315: 
                   1316:                ips_lock(pset);
                   1317:                if (!ips_active(pset)) {
                   1318:                        ipc_pset_remove(pset, port);
                   1319:                        ips_check_unlock(pset);
                   1320:                        goto no_port_set;
                   1321:                } else {
                   1322:                        statusp->mps_pset = pset->ips_local_name;
                   1323:                        imq_lock(&pset->ips_messages);
                   1324:                        statusp->mps_seqno = port->ip_seqno;
                   1325:                        imq_unlock(&pset->ips_messages);
                   1326:                        ips_unlock(pset);
                   1327:                        assert(MACH_PORT_VALID(statusp->mps_pset));
                   1328:                }
                   1329:        } else {
                   1330:            no_port_set:
                   1331:                statusp->mps_pset = MACH_PORT_NULL;
                   1332:                imq_lock(&port->ip_messages);
                   1333:                statusp->mps_seqno = port->ip_seqno;
                   1334:                imq_unlock(&port->ip_messages);
                   1335:        }
                   1336: 
                   1337:        statusp->mps_mscount = port->ip_mscount;
                   1338:        statusp->mps_qlimit = port->ip_qlimit;
                   1339:        statusp->mps_msgcount = port->ip_msgcount;
                   1340:        statusp->mps_sorights = port->ip_sorights;
                   1341:        statusp->mps_srights = port->ip_srights > 0;
                   1342:        statusp->mps_pdrequest = port->ip_pdrequest != IP_NULL;
                   1343:        statusp->mps_nsrequest = port->ip_nsrequest != IP_NULL;
                   1344:        ip_unlock(port);
                   1345: 
                   1346:        return KERN_SUCCESS;
                   1347: }
                   1348: 
                   1349: #ifdef MIGRATING_THREADS
                   1350: kern_return_t
1.1.1.4   root     1351: mach_port_set_rpcinfo(
                   1352:        ipc_space_t     space,
                   1353:        mach_port_t     name,
                   1354:        void            *rpc_info,
                   1355:        unsigned int    rpc_info_count)
1.1       root     1356: {
                   1357:        ipc_target_t target;
                   1358:        ipc_object_t object;
                   1359:        kern_return_t kr;
                   1360: 
                   1361:        if (space == IS_NULL)
                   1362:                return KERN_INVALID_TASK;
                   1363: 
                   1364:        kr = ipc_object_translate(space, name,
                   1365:                                  MACH_PORT_RIGHT_PORT_SET, &object);
                   1366:        if (kr == KERN_SUCCESS)
                   1367:                target = &((ipc_pset_t)object)->ips_target;
                   1368:        else {
                   1369:                kr = ipc_object_translate(space, name,
                   1370:                                          MACH_PORT_RIGHT_RECEIVE, &object);
                   1371:                if (kr != KERN_SUCCESS)
                   1372:                        return kr;
                   1373:                target = &((ipc_port_t)object)->ip_target;
                   1374:        }
                   1375: 
                   1376:        /* port/pset is locked and active */
                   1377: 
                   1378:        kr = port_machine_set_rpcinfo(target, rpc_info, rpc_info_count);
                   1379: 
                   1380:        io_unlock(object);
                   1381: 
                   1382:        return kr;
                   1383: }
                   1384: 
                   1385: #if 1
                   1386: int sacts, maxsacts;
                   1387: #endif
                   1388: 
1.1.1.4   root     1389: void sact_count(void)
1.1       root     1390: {
                   1391:        printf("%d server activations in use, %d max\n", sacts, maxsacts);
                   1392: }
                   1393: 
                   1394: kern_return_t
1.1.1.4   root     1395: mach_port_create_act(
                   1396:        task_t          task,
                   1397:        mach_port_t     name,
                   1398:        vm_offset_t     user_stack,
                   1399:        vm_offset_t     user_rbuf,
                   1400:        vm_size_t       user_rbuf_size,
                   1401:        Act             **out_act)
1.1       root     1402: {
                   1403:        ipc_target_t target;
                   1404:        ipc_space_t space;
                   1405:        ipc_object_t object;
                   1406:        kern_return_t kr;
                   1407:        Act *act;
                   1408: 
                   1409:        if (task == 0)
                   1410:                return KERN_INVALID_TASK;
                   1411: 
                   1412:        /* First create the new activation.  */
                   1413:        kr = act_create(task, user_stack, user_rbuf, user_rbuf_size, &act);
                   1414:        if (kr != KERN_SUCCESS)
                   1415:                return kr;
                   1416: 
                   1417:        space = task->itk_space;
                   1418: 
                   1419:        kr = ipc_object_translate(space, name,
                   1420:                                  MACH_PORT_RIGHT_PORT_SET, &object);
                   1421:        if (kr == KERN_SUCCESS)
                   1422:                target = &((ipc_pset_t)object)->ips_target;
                   1423:        else {
                   1424:                kr = ipc_object_translate(space, name,
                   1425:                                          MACH_PORT_RIGHT_RECEIVE, &object);
                   1426:                if (kr != KERN_SUCCESS) {
                   1427:                        act_terminate(act);
                   1428:                        act_deallocate(act);
                   1429:                        return kr;
                   1430:                }
                   1431:                target = &((ipc_port_t)object)->ip_target;
                   1432:        }
                   1433: 
                   1434:        /* port/pset is locked and active */
                   1435: #if 0
                   1436:        printf("act port/pset %08x ipc_target %08x stack %08x act %08x\n",
                   1437:               object, target, user_stack, act);
                   1438: #endif
                   1439: 
                   1440:        /* Assign the activation to the port's actpool.  */
                   1441:        kr = act_set_target(act, target);
                   1442:        if (kr != KERN_SUCCESS) {
                   1443:                io_unlock(object);
                   1444:                act_terminate(act);
                   1445:                act_deallocate(act);
                   1446:                return kr;
                   1447:        }
                   1448: #if 0
                   1449:        printf(" actpool %08x act %08x\n", target->ip_actpool, act);
                   1450: #endif
                   1451: 
                   1452:        io_unlock(object);
                   1453: 
                   1454:        /* Pass our reference to the activation back to the user.  */
                   1455:        *out_act = act;
                   1456: 
                   1457: #if 1
                   1458:        sacts++;
                   1459:        if (sacts > maxsacts)
                   1460:                maxsacts = sacts;
                   1461:        act->mact.pcb->ss.mpsfu_high = 0x69;
                   1462: #endif
                   1463:        return KERN_SUCCESS;
                   1464: }
                   1465: 
                   1466: #ifdef RPCKERNELSIG
                   1467: kern_return_t
1.1.1.4   root     1468: mach_port_set_syscall_right(
                   1469:        task_t          task,
                   1470:        mach_port_t     name)
1.1       root     1471: {
                   1472:        ipc_entry_t entry;
                   1473:        kern_return_t kr;
                   1474: 
                   1475:        if (task == IS_NULL)
                   1476:                return KERN_INVALID_TASK;
                   1477: 
                   1478:        kr = ipc_right_lookup_write(task, name, &entry);
                   1479:        if (kr != KERN_SUCCESS) {
                   1480:                return kr;
                   1481:        }
                   1482: 
                   1483:        if (!(entry->ie_bits & MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND))) {
                   1484:                is_write_unlock(space);
                   1485:                return KERN_INVALID_RIGHT;
                   1486:        }
                   1487: 
                   1488:        task->syscall_ipc_entry = *entry;
                   1489: 
                   1490:        is_write_unlock(space);
                   1491: 
                   1492:        return KERN_SUCCESS;
                   1493: }
                   1494: #endif
                   1495: #endif /* MIGRATING_THREADS */
1.1.1.4   root     1496: 
                   1497: /*
                   1498:  *     Routine:        mach_port_set_protected_payload [kernel call]
                   1499:  *     Purpose:
                   1500:  *             Changes a receive right's protected payload.
                   1501:  *     Conditions:
                   1502:  *             Nothing locked.
                   1503:  *     Returns:
                   1504:  *             KERN_SUCCESS            Set protected payload.
                   1505:  *             KERN_INVALID_TASK       The space is null.
                   1506:  *             KERN_INVALID_TASK       The space is dead.
                   1507:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                   1508:  *             KERN_INVALID_RIGHT      Name doesn't denote receive rights.
                   1509:  */
                   1510: 
                   1511: kern_return_t
                   1512: mach_port_set_protected_payload(
                   1513:        ipc_space_t             space,
                   1514:        mach_port_t             name,
                   1515:        unsigned long           payload)
                   1516: {
                   1517:        ipc_port_t port;
                   1518:        kern_return_t kr;
                   1519: 
                   1520:        if (space == IS_NULL)
                   1521:                return KERN_INVALID_TASK;
                   1522: 
                   1523:        kr = ipc_port_translate_receive(space, name, &port);
                   1524:        if (kr != KERN_SUCCESS)
                   1525:                return kr;
                   1526:        /* port is locked and active */
                   1527: 
                   1528:        ipc_port_set_protected_payload(port, payload);
                   1529: 
                   1530:        ip_unlock(port);
                   1531:        return KERN_SUCCESS;
                   1532: }
                   1533: 
                   1534: /*
                   1535:  *     Routine:        mach_port_clear_protected_payload [kernel call]
                   1536:  *     Purpose:
                   1537:  *             Clears a receive right's protected payload.
                   1538:  *     Conditions:
                   1539:  *             Nothing locked.
                   1540:  *     Returns:
                   1541:  *             KERN_SUCCESS            Clear protected payload.
                   1542:  *             KERN_INVALID_TASK       The space is null.
                   1543:  *             KERN_INVALID_TASK       The space is dead.
                   1544:  *             KERN_INVALID_NAME       The name doesn't denote a right.
                   1545:  *             KERN_INVALID_RIGHT      Name doesn't denote receive rights.
                   1546:  */
                   1547: 
                   1548: kern_return_t
                   1549: mach_port_clear_protected_payload(
                   1550:        ipc_space_t             space,
                   1551:        mach_port_t             name)
                   1552: {
                   1553:        ipc_port_t port;
                   1554:        kern_return_t kr;
                   1555: 
                   1556:        if (space == IS_NULL)
                   1557:                return KERN_INVALID_TASK;
                   1558: 
                   1559:        kr = ipc_port_translate_receive(space, name, &port);
                   1560:        if (kr != KERN_SUCCESS)
                   1561:                return kr;
                   1562:        /* port is locked and active */
                   1563: 
                   1564:        ipc_port_clear_protected_payload(port);
                   1565: 
                   1566:        ip_unlock(port);
                   1567:        return KERN_SUCCESS;
                   1568: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.