Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/diagnostics_reporter.h" |
| 6 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 7 | #include <base/file_util.h> |
| 8 | |
| 9 | #include "shill/minijail.h" |
| 10 | #include "shill/process_killer.h" |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 11 | #include "shill/shill_time.h" |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 12 | #include "shill/shims/net_diags_upload.h" |
| 13 | |
| 14 | using base::Closure; |
| 15 | using std::vector; |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | base::LazyInstance<DiagnosticsReporter> g_reporter = LAZY_INSTANCE_INITIALIZER; |
Darin Petkov | bd0dcc8 | 2012-11-29 10:51:12 +0100 | [diff] [blame] | 22 | const char kNetDiagsUpload[] = SHIMDIR "/net-diags-upload"; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 23 | const char kNetDiagsUploadUser[] = "syslog"; |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 24 | |
| 25 | } // namespace |
| 26 | |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 27 | // static |
| 28 | const int DiagnosticsReporter::kLogStashThrottleSeconds = 30 * 60; |
| 29 | |
| 30 | DiagnosticsReporter::DiagnosticsReporter() |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 31 | : minijail_(Minijail::GetInstance()), |
| 32 | process_killer_(ProcessKiller::GetInstance()), |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 33 | time_(Time::GetInstance()), |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 34 | last_log_stash_(0), |
| 35 | stashed_net_log_(shims::kStashedNetLog) {} |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 36 | |
| 37 | DiagnosticsReporter::~DiagnosticsReporter() {} |
| 38 | |
| 39 | // static |
| 40 | DiagnosticsReporter *DiagnosticsReporter::GetInstance() { |
| 41 | return g_reporter.Pointer(); |
| 42 | } |
| 43 | |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 44 | void DiagnosticsReporter::OnConnectivityEvent() { |
| 45 | LOG(INFO) << "Diagnostics event triggered."; |
| 46 | |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 47 | struct timeval now = (const struct timeval){ 0 }; |
| 48 | time_->GetTimeMonotonic(&now); |
Darin Petkov | 19321e4 | 2012-12-17 15:40:59 +0100 | [diff] [blame] | 49 | if (last_log_stash_ && |
| 50 | last_log_stash_ + kLogStashThrottleSeconds > |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 51 | static_cast<uint64>(now.tv_sec)) { |
| 52 | LOG(INFO) << "Diagnostics throttled."; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | last_log_stash_ = now.tv_sec; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 57 | // Delete logs possibly stashed by a different user. |
| 58 | file_util::Delete(stashed_net_log_, false); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 59 | |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 60 | LOG(INFO) << "Spawning " << kNetDiagsUpload << " @ " << last_log_stash_; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 61 | vector<char *> args; |
| 62 | args.push_back(const_cast<char *>(kNetDiagsUpload)); |
| 63 | if (IsReportingEnabled()) { |
| 64 | args.push_back(const_cast<char *>("--upload")); |
| 65 | } |
| 66 | args.push_back(NULL); |
| 67 | pid_t pid = 0; |
| 68 | struct minijail *jail = minijail_->New(); |
| 69 | minijail_->DropRoot(jail, kNetDiagsUploadUser); |
| 70 | if (minijail_->RunAndDestroy(jail, args, &pid)) { |
| 71 | process_killer_->Wait(pid, Closure()); |
| 72 | } else { |
| 73 | LOG(ERROR) << "Unable to spawn " << kNetDiagsUpload; |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 74 | } |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 77 | bool DiagnosticsReporter::IsReportingEnabled() { |
| 78 | // TODO(petkov): Implement this when there's a way to control reporting |
| 79 | // through policy. crosbug.com/35946. |
| 80 | return false; |
| 81 | } |
| 82 | |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 83 | } // namespace shill |