| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 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, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/win/registration_protocol_win.h" | 15 #include "util/win/registration_protocol_win.h" |
| 16 | 16 |
| 17 #include <windows.h> | 17 #include <windows.h> |
| 18 | 18 |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "util/win/scoped_handle.h" | 20 #include "util/win/scoped_handle.h" |
| 21 | 21 |
| 22 namespace crashpad { | 22 namespace crashpad { |
| 23 | 23 |
| 24 bool SendToCrashHandlerServer(const base::string16& pipe_name, | 24 bool SendToCrashHandlerServer(const base::string16& pipe_name, |
| 25 const crashpad::ClientToServerMessage& message, | 25 const crashpad::ClientToServerMessage& message, |
| 26 crashpad::ServerToClientMessage* response) { | 26 crashpad::ServerToClientMessage* response) { |
| 27 int tries = 5; | 27 int tries = 0; |
| 28 while (tries > 0) { | 28 for (;;) { |
| 29 ScopedFileHANDLE pipe( | 29 ScopedFileHANDLE pipe( |
| 30 CreateFile(pipe_name.c_str(), | 30 CreateFile(pipe_name.c_str(), |
| 31 GENERIC_READ | GENERIC_WRITE, | 31 GENERIC_READ | GENERIC_WRITE, |
| 32 0, | 32 0, |
| 33 nullptr, | 33 nullptr, |
| 34 OPEN_EXISTING, | 34 OPEN_EXISTING, |
| 35 SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, | 35 SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, |
| 36 nullptr)); | 36 nullptr)); |
| 37 if (!pipe.is_valid()) { | 37 if (!pipe.is_valid()) { |
| 38 Sleep(10); | 38 if (++tries == 5 || GetLastError() != ERROR_PIPE_BUSY) { |
| 39 --tries; | 39 PLOG(ERROR) << "CreateFile"; |
| 40 return false; |
| 41 } |
| 42 |
| 43 if (!WaitNamedPipe(pipe_name.c_str(), 1000) && |
| 44 GetLastError() != ERROR_SEM_TIMEOUT) { |
| 45 PLOG(ERROR) << "WaitNamedPipe"; |
| 46 return false; |
| 47 } |
| 48 |
| 40 continue; | 49 continue; |
| 41 } | 50 } |
| 51 |
| 42 DWORD mode = PIPE_READMODE_MESSAGE; | 52 DWORD mode = PIPE_READMODE_MESSAGE; |
| 43 if (!SetNamedPipeHandleState(pipe.get(), &mode, nullptr, nullptr)) { | 53 if (!SetNamedPipeHandleState(pipe.get(), &mode, nullptr, nullptr)) { |
| 44 PLOG(ERROR) << "SetNamedPipeHandleState"; | 54 PLOG(ERROR) << "SetNamedPipeHandleState"; |
| 45 return false; | 55 return false; |
| 46 } | 56 } |
| 47 DWORD bytes_read = 0; | 57 DWORD bytes_read = 0; |
| 48 BOOL result = TransactNamedPipe( | 58 BOOL result = TransactNamedPipe( |
| 49 pipe.get(), | 59 pipe.get(), |
| 50 // This is [in], but is incorrectly declared non-const. | 60 // This is [in], but is incorrectly declared non-const. |
| 51 const_cast<crashpad::ClientToServerMessage*>(&message), | 61 const_cast<crashpad::ClientToServerMessage*>(&message), |
| 52 sizeof(message), | 62 sizeof(message), |
| 53 response, | 63 response, |
| 54 sizeof(*response), | 64 sizeof(*response), |
| 55 &bytes_read, | 65 &bytes_read, |
| 56 nullptr); | 66 nullptr); |
| 57 if (!result) { | 67 if (!result) { |
| 58 PLOG(ERROR) << "TransactNamedPipe"; | 68 LOG(ERROR) << "TransactNamedPipe: expected " << sizeof(*response) |
| 69 << ", observed " << bytes_read; |
| 59 return false; | 70 return false; |
| 60 } | 71 } |
| 61 if (bytes_read != sizeof(*response)) { | 72 if (bytes_read != sizeof(*response)) { |
| 62 LOG(ERROR) << "TransactNamedPipe read incorrect number of bytes"; | 73 LOG(ERROR) << "TransactNamedPipe read incorrect number of bytes"; |
| 63 return false; | 74 return false; |
| 64 } | 75 } |
| 65 return true; | 76 return true; |
| 66 } | 77 } |
| 67 | |
| 68 LOG(ERROR) << "failed to connect after retrying"; | |
| 69 return false; | |
| 70 } | 78 } |
| 71 | 79 |
| 72 } // namespace crashpad | 80 } // namespace crashpad |
| OLD | NEW |