OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "handler/mac/crash_report_upload_thread.h" |
| 16 |
| 17 #include <errno.h> |
| 18 |
| 19 #include <vector> |
| 20 |
| 21 #include "base/logging.h" |
| 22 |
| 23 namespace crashpad { |
| 24 |
| 25 CrashReportUploadThread::CrashReportUploadThread(CrashReportDatabase* database) |
| 26 : database_(database), |
| 27 semaphore_(0), |
| 28 thread_(0), |
| 29 running_(false) { |
| 30 } |
| 31 |
| 32 CrashReportUploadThread::~CrashReportUploadThread() { |
| 33 DCHECK(!running_); |
| 34 DCHECK(!thread_); |
| 35 } |
| 36 |
| 37 void CrashReportUploadThread::Start() { |
| 38 DCHECK(!running_); |
| 39 DCHECK(!thread_); |
| 40 |
| 41 running_ = true; |
| 42 if ((errno = pthread_create(&thread_, nullptr, RunThreadMain, this)) != 0) { |
| 43 PLOG(ERROR) << "pthread_create"; |
| 44 DCHECK(false); |
| 45 running_ = false; |
| 46 } |
| 47 } |
| 48 |
| 49 void CrashReportUploadThread::Stop() { |
| 50 DCHECK(running_); |
| 51 DCHECK(thread_); |
| 52 |
| 53 if (!running_) { |
| 54 return; |
| 55 } |
| 56 |
| 57 running_ = false; |
| 58 semaphore_.Signal(); |
| 59 |
| 60 if ((errno = pthread_join(thread_, nullptr)) != 0) { |
| 61 PLOG(ERROR) << "pthread_join"; |
| 62 DCHECK(false); |
| 63 } |
| 64 |
| 65 thread_ = 0; |
| 66 } |
| 67 |
| 68 void CrashReportUploadThread::ReportPending() { |
| 69 semaphore_.Signal(); |
| 70 } |
| 71 |
| 72 void CrashReportUploadThread::ThreadMain() { |
| 73 while (running_) { |
| 74 ProcessPendingReports(); |
| 75 |
| 76 // Check for pending reports every 15 minutes, even in the absence of a |
| 77 // signal from the handler thread. This allows for failed uploads to be |
| 78 // retried periodically, and for pending reports written by other processes |
| 79 // to be recognized. |
| 80 semaphore_.TimedWait(15 * 60); |
| 81 } |
| 82 } |
| 83 |
| 84 void CrashReportUploadThread::ProcessPendingReports() { |
| 85 std::vector<const CrashReportDatabase::Report> reports; |
| 86 if (database_->GetPendingReports(&reports) != CrashReportDatabase::kNoError) { |
| 87 // The database is sick. It might be prudent to stop trying to poke it from |
| 88 // this thread by abandoning the thread altogether. On the other hand, if |
| 89 // the problem is transient, it might be possible to talk to it again on the |
| 90 // next pass. For now, take the latter approach. |
| 91 return; |
| 92 } |
| 93 |
| 94 for (const CrashReportDatabase::Report& report : reports) { |
| 95 ProcessPendingReport(report); |
| 96 |
| 97 // Respect Stop() being called after at least one attempt to process a |
| 98 // report. |
| 99 if (!running_) { |
| 100 return; |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 105 void CrashReportUploadThread::ProcessPendingReport( |
| 106 const CrashReportDatabase::Report& report) { |
| 107 // TODO(mark): Actually upload the report, if uploads are enabled. |
| 108 database_->SkipReportUpload(report.uuid); |
| 109 } |
| 110 |
| 111 // static |
| 112 void* CrashReportUploadThread::RunThreadMain(void* arg) { |
| 113 CrashReportUploadThread* self = static_cast<CrashReportUploadThread*>(arg); |
| 114 self->ThreadMain(); |
| 115 return nullptr; |
| 116 } |
| 117 |
| 118 } // namespace crashpad |
OLD | NEW |