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 13 matching lines...) Expand all Loading... |
24 #include <unistd.h> | 24 #include <unistd.h> |
25 | 25 |
26 #include <algorithm> | 26 #include <algorithm> |
27 | 27 |
28 #include "base/logging.h" | 28 #include "base/logging.h" |
29 #include "base/mac/mach_logging.h" | 29 #include "base/mac/mach_logging.h" |
30 #include "base/mac/scoped_mach_port.h" | 30 #include "base/mac/scoped_mach_port.h" |
31 #include "base/posix/eintr_wrapper.h" | 31 #include "base/posix/eintr_wrapper.h" |
32 #include "base/rand_util.h" | 32 #include "base/rand_util.h" |
33 #include "base/strings/stringprintf.h" | 33 #include "base/strings/stringprintf.h" |
34 #include "util/file/fd_io.h" | 34 #include "util/file/file_io.h" |
35 #include "util/mach/child_port.h" | 35 #include "util/mach/child_port.h" |
36 #include "util/mach/mach_extensions.h" | 36 #include "util/mach/mach_extensions.h" |
37 #include "util/mach/mach_message.h" | 37 #include "util/mach/mach_message.h" |
38 #include "util/mach/mach_message_server.h" | 38 #include "util/mach/mach_message_server.h" |
39 | 39 |
40 namespace crashpad { | 40 namespace crashpad { |
41 | 41 |
42 ChildPortHandshake::ChildPortHandshake() | 42 ChildPortHandshake::ChildPortHandshake() |
43 : token_(0), | 43 : token_(0), |
44 pipe_read_(), | 44 pipe_read_(), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 mach_port_t ChildPortHandshake::RunServer() { | 78 mach_port_t ChildPortHandshake::RunServer() { |
79 DCHECK_NE(pipe_read_.get(), -1); | 79 DCHECK_NE(pipe_read_.get(), -1); |
80 pipe_read_.reset(); | 80 pipe_read_.reset(); |
81 | 81 |
82 // Transfer ownership of the write pipe into this method’s scope. | 82 // Transfer ownership of the write pipe into this method’s scope. |
83 base::ScopedFD pipe_write_owner(pipe_write_.release()); | 83 base::ScopedFD pipe_write_owner(pipe_write_.release()); |
84 | 84 |
85 // Initialize the token and share it with the client via the pipe. | 85 // Initialize the token and share it with the client via the pipe. |
86 token_ = base::RandUint64(); | 86 token_ = base::RandUint64(); |
87 int pipe_write = pipe_write_owner.get(); | 87 int pipe_write = pipe_write_owner.get(); |
88 if (!LoggingWriteFD(pipe_write, &token_, sizeof(token_))) { | 88 if (!LoggingWriteFile(pipe_write, &token_, sizeof(token_))) { |
89 LOG(WARNING) << "no client check-in"; | 89 LOG(WARNING) << "no client check-in"; |
90 return MACH_PORT_NULL; | 90 return MACH_PORT_NULL; |
91 } | 91 } |
92 | 92 |
93 // Create a unique name for the bootstrap service mapping. Make it unguessable | 93 // Create a unique name for the bootstrap service mapping. Make it unguessable |
94 // to prevent outsiders from grabbing the name first, which would cause | 94 // to prevent outsiders from grabbing the name first, which would cause |
95 // bootstrap_check_in() to fail. | 95 // bootstrap_check_in() to fail. |
96 uint64_t thread_id; | 96 uint64_t thread_id; |
97 errno = pthread_threadid_np(pthread_self(), &thread_id); | 97 errno = pthread_threadid_np(pthread_self(), &thread_id); |
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 DCHECK_LT(service_name.size(), implicit_cast<size_t>(BOOTSTRAP_MAX_NAME_LEN)); | 104 DCHECK_LT(service_name.size(), implicit_cast<size_t>(BOOTSTRAP_MAX_NAME_LEN)); |
105 | 105 |
106 // Check the new service in with the bootstrap server, obtaining a receive | 106 // Check the new service in with the bootstrap server, obtaining a receive |
107 // right for it. | 107 // right for it. |
108 mach_port_t server_port; | 108 mach_port_t server_port; |
109 kern_return_t kr = | 109 kern_return_t kr = |
110 bootstrap_check_in(bootstrap_port, service_name.c_str(), &server_port); | 110 bootstrap_check_in(bootstrap_port, service_name.c_str(), &server_port); |
111 BOOTSTRAP_CHECK(kr == BOOTSTRAP_SUCCESS, kr) << "bootstrap_check_in"; | 111 BOOTSTRAP_CHECK(kr == BOOTSTRAP_SUCCESS, kr) << "bootstrap_check_in"; |
112 base::mac::ScopedMachReceiveRight server_port_owner(server_port); | 112 base::mac::ScopedMachReceiveRight server_port_owner(server_port); |
113 | 113 |
114 // Share the service name with the client via the pipe. | 114 // Share the service name with the client via the pipe. |
115 uint32_t service_name_length = service_name.size(); | 115 uint32_t service_name_length = service_name.size(); |
116 if (!LoggingWriteFD( | 116 if (!LoggingWriteFile( |
117 pipe_write, &service_name_length, sizeof(service_name_length))) { | 117 pipe_write, &service_name_length, sizeof(service_name_length))) { |
118 LOG(WARNING) << "no client check-in"; | 118 LOG(WARNING) << "no client check-in"; |
119 return MACH_PORT_NULL; | 119 return MACH_PORT_NULL; |
120 } | 120 } |
121 | 121 |
122 if (!LoggingWriteFD(pipe_write, service_name.c_str(), service_name_length)) { | 122 if (!LoggingWriteFile( |
| 123 pipe_write, service_name.c_str(), service_name_length)) { |
123 LOG(WARNING) << "no client check-in"; | 124 LOG(WARNING) << "no client check-in"; |
124 return MACH_PORT_NULL; | 125 return MACH_PORT_NULL; |
125 } | 126 } |
126 | 127 |
127 // A kqueue cannot monitor a raw Mach receive right with EVFILT_MACHPORT. It | 128 // A kqueue cannot monitor a raw Mach receive right with EVFILT_MACHPORT. It |
128 // requires a port set. Create a new port set and add the receive right to it. | 129 // requires a port set. Create a new port set and add the receive right to it. |
129 mach_port_t server_port_set; | 130 mach_port_t server_port_set; |
130 kr = mach_port_allocate( | 131 kr = mach_port_allocate( |
131 mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &server_port_set); | 132 mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &server_port_set); |
132 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_allocate"; | 133 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_port_allocate"; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 | 306 |
306 // Look up the server and check in with it by providing the token and port. | 307 // Look up the server and check in with it by providing the token and port. |
307 RunClientInternal_SendCheckIn(service_name, token, port, right_type); | 308 RunClientInternal_SendCheckIn(service_name, token, port, right_type); |
308 } | 309 } |
309 | 310 |
310 // static | 311 // static |
311 void ChildPortHandshake::RunClientInternal_ReadPipe(int pipe_read, | 312 void ChildPortHandshake::RunClientInternal_ReadPipe(int pipe_read, |
312 child_port_token_t* token, | 313 child_port_token_t* token, |
313 std::string* service_name) { | 314 std::string* service_name) { |
314 // Read the token from the pipe. | 315 // Read the token from the pipe. |
315 CheckedReadFD(pipe_read, token, sizeof(*token)); | 316 CheckedReadFile(pipe_read, token, sizeof(*token)); |
316 | 317 |
317 // Read the service name from the pipe. | 318 // Read the service name from the pipe. |
318 uint32_t service_name_length; | 319 uint32_t service_name_length; |
319 CheckedReadFD(pipe_read, &service_name_length, sizeof(service_name_length)); | 320 CheckedReadFile(pipe_read, &service_name_length, sizeof(service_name_length)); |
320 DCHECK_LT(service_name_length, | 321 DCHECK_LT(service_name_length, |
321 implicit_cast<uint32_t>(BOOTSTRAP_MAX_NAME_LEN)); | 322 implicit_cast<uint32_t>(BOOTSTRAP_MAX_NAME_LEN)); |
322 | 323 |
323 if (service_name_length > 0) { | 324 if (service_name_length > 0) { |
324 service_name->resize(service_name_length); | 325 service_name->resize(service_name_length); |
325 CheckedReadFD(pipe_read, &(*service_name)[0], service_name_length); | 326 CheckedReadFile(pipe_read, &(*service_name)[0], service_name_length); |
326 } | 327 } |
327 } | 328 } |
328 | 329 |
329 // static | 330 // static |
330 void ChildPortHandshake::RunClientInternal_SendCheckIn( | 331 void ChildPortHandshake::RunClientInternal_SendCheckIn( |
331 const std::string& service_name, | 332 const std::string& service_name, |
332 child_port_token_t token, | 333 child_port_token_t token, |
333 mach_port_t port, | 334 mach_port_t port, |
334 mach_msg_type_name_t right_type) { | 335 mach_msg_type_name_t right_type) { |
335 // Get a send right to the server by looking up the service with the bootstrap | 336 // Get a send right to the server by looking up the service with the bootstrap |
336 // server by name. | 337 // server by name. |
337 mach_port_t server_port; | 338 mach_port_t server_port; |
338 kern_return_t kr = | 339 kern_return_t kr = |
339 bootstrap_look_up(bootstrap_port, service_name.c_str(), &server_port); | 340 bootstrap_look_up(bootstrap_port, service_name.c_str(), &server_port); |
340 BOOTSTRAP_CHECK(kr == BOOTSTRAP_SUCCESS, kr) << "bootstrap_look_up"; | 341 BOOTSTRAP_CHECK(kr == BOOTSTRAP_SUCCESS, kr) << "bootstrap_look_up"; |
341 base::mac::ScopedMachSendRight server_port_owner(server_port); | 342 base::mac::ScopedMachSendRight server_port_owner(server_port); |
342 | 343 |
343 // Check in with the server. | 344 // Check in with the server. |
344 kr = child_port_check_in(server_port, token, port, right_type); | 345 kr = child_port_check_in(server_port, token, port, right_type); |
345 MACH_CHECK(kr == KERN_SUCCESS, kr) << "child_port_check_in"; | 346 MACH_CHECK(kr == KERN_SUCCESS, kr) << "child_port_check_in"; |
346 } | 347 } |
347 | 348 |
348 } // namespace crashpad | 349 } // namespace crashpad |
OLD | NEW |