| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 PCHECK(errno == 0) << "pthread_threadid_np"; | 98 PCHECK(errno == 0) << "pthread_threadid_np"; |
| 99 std::string service_name = base::StringPrintf( | 99 std::string service_name = base::StringPrintf( |
| 100 "com.googlecode.crashpad.child_port_handshake.%d.%llu.%016llx", | 100 "com.googlecode.crashpad.child_port_handshake.%d.%llu.%016llx", |
| 101 getpid(), | 101 getpid(), |
| 102 thread_id, | 102 thread_id, |
| 103 base::RandUint64()); | 103 base::RandUint64()); |
| 104 | 104 |
| 105 // Check the new service in with the bootstrap server, obtaining a receive | 105 // Check the new service in with the bootstrap server, obtaining a receive |
| 106 // right for it. | 106 // right for it. |
| 107 base::mac::ScopedMachReceiveRight server_port(BootstrapCheckIn(service_name)); | 107 base::mac::ScopedMachReceiveRight server_port(BootstrapCheckIn(service_name)); |
| 108 CHECK_NE(server_port, kMachPortNull); | 108 CHECK(server_port.is_valid()); |
| 109 | 109 |
| 110 // Share the service name with the client via the pipe. | 110 // Share the service name with the client via the pipe. |
| 111 uint32_t service_name_length = service_name.size(); | 111 uint32_t service_name_length = service_name.size(); |
| 112 if (!LoggingWriteFile( | 112 if (!LoggingWriteFile( |
| 113 pipe_write, &service_name_length, sizeof(service_name_length))) { | 113 pipe_write, &service_name_length, sizeof(service_name_length))) { |
| 114 LOG(WARNING) << "no client check-in"; | 114 LOG(WARNING) << "no client check-in"; |
| 115 return MACH_PORT_NULL; | 115 return MACH_PORT_NULL; |
| 116 } | 116 } |
| 117 | 117 |
| 118 if (!LoggingWriteFile( | 118 if (!LoggingWriteFile( |
| 119 pipe_write, service_name.c_str(), service_name_length)) { | 119 pipe_write, service_name.c_str(), service_name_length)) { |
| 120 LOG(WARNING) << "no client check-in"; | 120 LOG(WARNING) << "no client check-in"; |
| 121 return MACH_PORT_NULL; | 121 return MACH_PORT_NULL; |
| 122 } | 122 } |
| 123 | 123 |
| 124 // A kqueue cannot monitor a raw Mach receive right with EVFILT_MACHPORT. It | 124 // A kqueue cannot monitor a raw Mach receive right with EVFILT_MACHPORT. It |
| 125 // requires a port set. Create a new port set and add the receive right to it. | 125 // requires a port set. Create a new port set and add the receive right to it. |
| 126 base::mac::ScopedMachPortSet server_port_set( | 126 base::mac::ScopedMachPortSet server_port_set( |
| 127 NewMachPort(MACH_PORT_RIGHT_PORT_SET)); | 127 NewMachPort(MACH_PORT_RIGHT_PORT_SET)); |
| 128 CHECK_NE(server_port_set, kMachPortNull); | 128 CHECK(server_port_set.is_valid()); |
| 129 | 129 |
| 130 kern_return_t kr = | 130 kern_return_t kr = mach_port_insert_member( |
| 131 mach_port_insert_member(mach_task_self(), server_port, server_port_set); | 131 mach_task_self(), server_port.get(), server_port_set.get()); |
| 132 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_insert_member"; | 132 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_insert_member"; |
| 133 | 133 |
| 134 // Set up a kqueue to monitor both the server’s receive right and the write | 134 // Set up a kqueue to monitor both the server’s receive right and the write |
| 135 // side of the pipe. Messages from the client will be received via the receive | 135 // side of the pipe. Messages from the client will be received via the receive |
| 136 // right, and the pipe will show EOF if the client closes its read side | 136 // right, and the pipe will show EOF if the client closes its read side |
| 137 // prematurely. | 137 // prematurely. |
| 138 base::ScopedFD kq(kqueue()); | 138 base::ScopedFD kq(kqueue()); |
| 139 PCHECK(kq != -1) << "kqueue"; | 139 PCHECK(kq != -1) << "kqueue"; |
| 140 | 140 |
| 141 struct kevent changelist[2]; | 141 struct kevent changelist[2]; |
| 142 EV_SET(&changelist[0], | 142 EV_SET(&changelist[0], |
| 143 server_port_set, | 143 server_port_set.get(), |
| 144 EVFILT_MACHPORT, | 144 EVFILT_MACHPORT, |
| 145 EV_ADD | EV_CLEAR, | 145 EV_ADD | EV_CLEAR, |
| 146 0, | 146 0, |
| 147 0, | 147 0, |
| 148 nullptr); | 148 nullptr); |
| 149 EV_SET(&changelist[1], | 149 EV_SET(&changelist[1], |
| 150 pipe_write, | 150 pipe_write, |
| 151 EVFILT_WRITE, | 151 EVFILT_WRITE, |
| 152 EV_ADD | EV_CLEAR, | 152 EV_ADD | EV_CLEAR, |
| 153 0, | 153 0, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 187 |
| 188 if (event.flags & EV_ERROR) { | 188 if (event.flags & EV_ERROR) { |
| 189 // kevent() may have put its error here. | 189 // kevent() may have put its error here. |
| 190 errno = event.data; | 190 errno = event.data; |
| 191 PLOG(FATAL) << "kevent"; | 191 PLOG(FATAL) << "kevent"; |
| 192 } | 192 } |
| 193 | 193 |
| 194 switch (event.filter) { | 194 switch (event.filter) { |
| 195 case EVFILT_MACHPORT: { | 195 case EVFILT_MACHPORT: { |
| 196 // There’s something to receive on the port set. | 196 // There’s something to receive on the port set. |
| 197 DCHECK_EQ(event.ident, server_port_set); | 197 DCHECK_EQ(event.ident, server_port_set.get()); |
| 198 | 198 |
| 199 // Run the message server in an inner loop instead of using | 199 // Run the message server in an inner loop instead of using |
| 200 // MachMessageServer::kPersistent. This allows the loop to exit as soon | 200 // MachMessageServer::kPersistent. This allows the loop to exit as soon |
| 201 // as child_port_ is set, even if other messages are queued. This needs | 201 // as child_port_ is set, even if other messages are queued. This needs |
| 202 // to drain all messages, because the use of edge triggering (EV_CLEAR) | 202 // to drain all messages, because the use of edge triggering (EV_CLEAR) |
| 203 // means that if more than one message is in the queue when kevent() | 203 // means that if more than one message is in the queue when kevent() |
| 204 // returns, no more notifications will be generated. | 204 // returns, no more notifications will be generated. |
| 205 while (!checked_in_) { | 205 while (!checked_in_) { |
| 206 // If a proper message is received from child_port_check_in(), | 206 // If a proper message is received from child_port_check_in(), |
| 207 // this will call HandleChildPortCheckIn(). | 207 // this will call HandleChildPortCheckIn(). |
| 208 mach_msg_return_t mr = | 208 mach_msg_return_t mr = |
| 209 MachMessageServer::Run(&child_port_server, | 209 MachMessageServer::Run(&child_port_server, |
| 210 server_port_set, | 210 server_port_set.get(), |
| 211 MACH_MSG_OPTION_NONE, | 211 MACH_MSG_OPTION_NONE, |
| 212 MachMessageServer::kOneShot, | 212 MachMessageServer::kOneShot, |
| 213 MachMessageServer::kReceiveLargeIgnore, | 213 MachMessageServer::kReceiveLargeIgnore, |
| 214 kMachMessageTimeoutNonblocking); | 214 kMachMessageTimeoutNonblocking); |
| 215 if (mr == MACH_RCV_TIMED_OUT) { | 215 if (mr == MACH_RCV_TIMED_OUT) { |
| 216 break; | 216 break; |
| 217 } else if (mr != MACH_MSG_SUCCESS) { | 217 } else if (mr != MACH_MSG_SUCCESS) { |
| 218 MACH_LOG(ERROR, mr) << "MachMessageServer::Run"; | 218 MACH_LOG(ERROR, mr) << "MachMessageServer::Run"; |
| 219 return MACH_PORT_NULL; | 219 return MACH_PORT_NULL; |
| 220 } | 220 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 | 322 |
| 323 // static | 323 // static |
| 324 void ChildPortHandshake::RunClientInternal_SendCheckIn( | 324 void ChildPortHandshake::RunClientInternal_SendCheckIn( |
| 325 const std::string& service_name, | 325 const std::string& service_name, |
| 326 child_port_token_t token, | 326 child_port_token_t token, |
| 327 mach_port_t port, | 327 mach_port_t port, |
| 328 mach_msg_type_name_t right_type) { | 328 mach_msg_type_name_t right_type) { |
| 329 // Get a send right to the server by looking up the service with the bootstrap | 329 // Get a send right to the server by looking up the service with the bootstrap |
| 330 // server by name. | 330 // server by name. |
| 331 base::mac::ScopedMachSendRight server_port(BootstrapLookUp(service_name)); | 331 base::mac::ScopedMachSendRight server_port(BootstrapLookUp(service_name)); |
| 332 CHECK_NE(server_port, kMachPortNull); | 332 CHECK(server_port.is_valid()); |
| 333 | 333 |
| 334 // Check in with the server. | 334 // Check in with the server. |
| 335 kern_return_t kr = child_port_check_in(server_port, token, port, right_type); | 335 kern_return_t kr = |
| 336 child_port_check_in(server_port.get(), token, port, right_type); |
| 336 MACH_CHECK(kr == KERN_SUCCESS, kr) << "child_port_check_in"; | 337 MACH_CHECK(kr == KERN_SUCCESS, kr) << "child_port_check_in"; |
| 337 } | 338 } |
| 338 | 339 |
| 339 } // namespace crashpad | 340 } // namespace crashpad |
| OLD | NEW |