]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/ipc_misc.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_misc.c
index 6690db41088b1b79bdd62f858a1743c176ff97e7..16c3c5a51b560b62f62d4a504a338001e180db1a 100644 (file)
@@ -44,35 +44,16 @@ extern void fileport_releasefg(struct fileglob *);
  * Description: Obtain a send right for the given fileglob, which must be
  *             referenced.
  *
- * Parameters:         fg              A fileglob.
+ * Parameters:  fg             A fileglob.
  *
- * Returns:    Port of type IKOT_FILEPORT with fileglob set as its kobject. 
- *             Port is returned with a send right.
+ * Returns:     Port of type IKOT_FILEPORT with fileglob set as its kobject.
+ *              Port is returned with a send right.
  */
 ipc_port_t
 fileport_alloc(struct fileglob *fg)
 {
-       ipc_port_t fileport;
-       ipc_port_t sendport;
-       ipc_port_t notifyport;
-
-       fileport = ipc_port_alloc_kernel();
-       if (fileport == IP_NULL) {
-               goto out;
-       }
-
-       ipc_kobject_set(fileport, (ipc_kobject_t)fg, IKOT_FILEPORT);
-       ip_lock(fileport); /* unlocked by ipc_port_nsrequest */
-       notifyport = ipc_port_make_sonce_locked(fileport);
-       ipc_port_nsrequest(fileport, 1, notifyport, &notifyport);
-
-       sendport = ipc_port_make_send(fileport);
-       if (!IP_VALID(sendport)) {
-               panic("Couldn't allocate send right for fileport!\n");
-       }
-
-out:
-       return fileport;
+       return ipc_kobject_alloc_port((ipc_kobject_t)fg, IKOT_FILEPORT,
+                  IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST);
 }
 
 
@@ -95,12 +76,14 @@ fileport_port_to_fileglob(ipc_port_t port)
 {
        struct fileglob *fg = NULL;
 
-       if (!IP_VALID(port))
+       if (!IP_VALID(port)) {
                return NULL;
+       }
 
        ip_lock(port);
-       if (ip_active(port) && IKOT_FILEPORT == ip_kotype(port))
+       if (ip_active(port) && IKOT_FILEPORT == ip_kotype(port)) {
                fg = (void *)port->ip_kobject;
+       }
        ip_unlock(port);
 
        return fg;
@@ -111,8 +94,8 @@ fileport_port_to_fileglob(ipc_port_t port)
  * fileport_notify
  *
  * Description: Handle a no-senders notification for a fileport.  Unless
- *             the message is spoofed, destroys the port and releases
- *             its reference on the fileglob.
+ *              the message is spoofed, destroys the port and releases
+ *              its reference on the fileglob.
  *
  * Parameters: msg             A Mach no-senders notification message.
  */
@@ -123,19 +106,23 @@ fileport_notify(mach_msg_header_t *msg)
        ipc_port_t port = notification->not_header.msgh_remote_port;
        struct fileglob *fg = NULL;
 
-       if (!IP_VALID(port))
+       if (!IP_VALID(port)) {
                panic("Invalid port passed to fileport_notify()\n");
+       }
 
        ip_lock(port);
 
        fg = (struct fileglob *)port->ip_kobject;
 
-       if (!ip_active(port)) 
+       if (!ip_active(port)) {
                panic("Inactive port passed to fileport_notify()\n");
-       if (ip_kotype(port) != IKOT_FILEPORT) 
+       }
+       if (ip_kotype(port) != IKOT_FILEPORT) {
                panic("Port of type other than IKOT_FILEPORT passed to fileport_notify()\n");
-       if (fg == NULL) 
+       }
+       if (fg == NULL) {
                panic("fileport without an assocated fileglob\n");
+       }
 
        if (port->ip_srights == 0) {
                ip_unlock(port);
@@ -160,24 +147,27 @@ fileport_notify(mach_msg_header_t *msg)
  */
 kern_return_t
 fileport_invoke(task_t task, mach_port_name_t name,
-       int (*action)(mach_port_name_t, struct fileglob *, void *),
-       void *arg, int *rval)
+    int (*action)(mach_port_name_t, struct fileglob *, void *),
+    void *arg, int *rval)
 {
        kern_return_t kr;
        ipc_port_t fileport;
        struct fileglob *fg;
 
        kr = ipc_object_copyin(task->itk_space, name,
-           MACH_MSG_TYPE_COPY_SEND, (ipc_object_t *)&fileport);
-       if (kr != KERN_SUCCESS)
-               return (kr);
+           MACH_MSG_TYPE_COPY_SEND, (ipc_object_t *)&fileport, 0, NULL,
+           IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND);
+       if (kr != KERN_SUCCESS) {
+               return kr;
+       }
 
-       if ((fg = fileport_port_to_fileglob(fileport)) != NULL)
+       if ((fg = fileport_port_to_fileglob(fileport)) != NULL) {
                *rval = (*action)(name, fg, arg);
-       else
+       } else {
                kr = KERN_FAILURE;
+       }
        ipc_port_release_send(fileport);
-       return (kr);
+       return kr;
 }
 
 /*
@@ -191,14 +181,14 @@ fileport_invoke(task_t task, mach_port_name_t name,
  *             and (c) if we could ask for port names by kobject type. Not
  *             clear that it's worth all that complexity, though.
  *
- * Parameters:         task            The target task
+ * Parameters:  task           The target task
  *             action          The function to invoke on each fileport
  *             arg             Anonymous pointer to caller state.
  */
 kern_return_t
 fileport_walk(task_t task,
-       int (*action)(mach_port_name_t, struct fileglob *, void *arg),
-       void *arg)
+    int (*action)(mach_port_name_t, struct fileglob *, void *arg),
+    void *arg)
 {
        mach_port_name_t *names;
        mach_msg_type_number_t ncnt, tcnt;
@@ -217,24 +207,26 @@ fileport_walk(task_t task,
        kr = mach_port_names(task->itk_space,
            (mach_port_name_t **)&map_copy_names, &ncnt,
            (mach_port_type_t **)&map_copy_types, &tcnt);
-       if (kr != KERN_SUCCESS)
-               return (kr);
+       if (kr != KERN_SUCCESS) {
+               return kr;
+       }
 
        vm_map_copy_discard(map_copy_types);
 
        kr = vm_map_copyout(ipc_kernel_map, &map_names, map_copy_names);
        if (kr != KERN_SUCCESS) {
                vm_map_copy_discard(map_copy_names);
-               return (kr);
+               return kr;
        }
        names = (mach_port_name_t *)(uintptr_t)map_names;
 
-       for (rval = 0, i = 0; i < ncnt; i++)
+       for (rval = 0, i = 0; i < ncnt; i++) {
                if (fileport_invoke(task, names[i], action, arg,
-                   &rval) == KERN_SUCCESS && -1 == rval)
-                       break;          /* early termination clause */
-
+                   &rval) == KERN_SUCCESS && -1 == rval) {
+                       break;          /* early termination clause */
+               }
+       }
        vm_deallocate(ipc_kernel_map,
-           (vm_address_t)names, ncnt * sizeof (*names));
-       return (KERN_SUCCESS);
+           (vm_address_t)names, ncnt * sizeof(*names));
+       return KERN_SUCCESS;
 }