| 59 | } |
| 60 | |
| 61 | void Run() { |
| 62 | DCHECK(running_); |
| 63 | |
| 64 | kern_return_t kr; |
| 65 | if (!launchd_) { |
| 66 | // Request that a no-senders notification for exception_port_ be sent to |
| 67 | // notify_port_. |
| 68 | mach_port_t previous; |
| 69 | kr = mach_port_request_notification(mach_task_self(), |
| 70 | exception_port_, |
| 71 | MACH_NOTIFY_NO_SENDERS, |
| 72 | 0, |
| 73 | notify_port_, |
| 74 | MACH_MSG_TYPE_MAKE_SEND_ONCE, |
| 75 | &previous); |
| 76 | MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_request_notification"; |
| 77 | base::apple::ScopedMachSendRight previous_owner(previous); |
| 78 | } |
| 79 | |
| 80 | // A single CompositeMachMessageServer will dispatch both exception messages |
| 81 | // and the no-senders notification. Put both receive rights into a port set. |
| 82 | // |
| 83 | // A single receive right can’t be used because the notification request |
| 84 | // requires a send-once right, which would prevent the no-senders condition |
| 85 | // from ever existing. Using distinct receive rights also allows the handler |
| 86 | // methods to ensure that the messages they process were sent by a holder of |
| 87 | // the proper send right. |
| 88 | base::apple::ScopedMachPortSet server_port_set( |
| 89 | NewMachPort(MACH_PORT_RIGHT_PORT_SET)); |
| 90 | CHECK(server_port_set.is_valid()); |
| 91 | |
| 92 | kr = mach_port_insert_member( |
| 93 | mach_task_self(), exception_port_, server_port_set.get()); |
| 94 | MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_insert_member"; |
| 95 | |
| 96 | kr = mach_port_insert_member( |
| 97 | mach_task_self(), notify_port_, server_port_set.get()); |
| 98 | MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_insert_member"; |
| 99 | |
| 100 | // Run the server in kOneShot mode so that running_ can be reevaluated after |
| 101 | // each message. Receipt of a valid no-senders notification causes it to be |
| 102 | // set to false. |
| 103 | while (running_) { |
| 104 | // This will result in a call to CatchMachException() or |
| 105 | // DoMachNotifyNoSenders() as appropriate. |
| 106 | mach_msg_return_t mr = |
| 107 | MachMessageServer::Run(&composite_mach_message_server_, |
| 108 | server_port_set.get(), |
| 109 | kMachMessageReceiveAuditTrailer, |
| 110 | MachMessageServer::kOneShot, |
| 111 | MachMessageServer::kReceiveLargeIgnore, |
| 112 | kMachMessageTimeoutWaitIndefinitely); |
| 113 | |
| 114 | // MACH_SEND_INVALID_DEST occurs when attempting to reply to a dead name. |
| 115 | // This can happen if a mach_exc or exc client disappears before a reply |
| 116 | // can be sent to it. That’s unusal for kernel-generated requests, but can |
| 117 | // easily happen if a task sends its own exception request (as |
| 118 | // SimulateCrash() does) and dies before the reply is sent. |
nothing calls this directly
no test coverage detected