blob: c8f123c164794e96cc31ba789b317aeca6ac6269 [file] [log] [blame]
Darin Petkovf0136cd2012-11-07 16:18:02 +01001// 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 Petkov38066762012-12-17 15:35:45 +01007#include <base/file_util.h>
8
9#include "shill/minijail.h"
10#include "shill/process_killer.h"
Darin Petkov328e19d2012-12-04 15:54:07 +010011#include "shill/shill_time.h"
Darin Petkov38066762012-12-17 15:35:45 +010012#include "shill/shims/net_diags_upload.h"
13
14using base::Closure;
15using std::vector;
Darin Petkovf0136cd2012-11-07 16:18:02 +010016
17namespace shill {
18
19namespace {
20
21base::LazyInstance<DiagnosticsReporter> g_reporter = LAZY_INSTANCE_INITIALIZER;
Darin Petkovbd0dcc82012-11-29 10:51:12 +010022const char kNetDiagsUpload[] = SHIMDIR "/net-diags-upload";
Darin Petkov38066762012-12-17 15:35:45 +010023const char kNetDiagsUploadUser[] = "syslog";
Darin Petkovf0136cd2012-11-07 16:18:02 +010024
25} // namespace
26
Darin Petkov328e19d2012-12-04 15:54:07 +010027// static
28const int DiagnosticsReporter::kLogStashThrottleSeconds = 30 * 60;
29
30DiagnosticsReporter::DiagnosticsReporter()
Darin Petkov38066762012-12-17 15:35:45 +010031 : minijail_(Minijail::GetInstance()),
32 process_killer_(ProcessKiller::GetInstance()),
Darin Petkov328e19d2012-12-04 15:54:07 +010033 time_(Time::GetInstance()),
Darin Petkov38066762012-12-17 15:35:45 +010034 last_log_stash_(0),
35 stashed_net_log_(shims::kStashedNetLog) {}
Darin Petkovf0136cd2012-11-07 16:18:02 +010036
37DiagnosticsReporter::~DiagnosticsReporter() {}
38
39// static
40DiagnosticsReporter *DiagnosticsReporter::GetInstance() {
41 return g_reporter.Pointer();
42}
43
Darin Petkov385b9bc2012-12-03 15:25:05 +010044void DiagnosticsReporter::OnConnectivityEvent() {
45 LOG(INFO) << "Diagnostics event triggered.";
46
Darin Petkov328e19d2012-12-04 15:54:07 +010047 struct timeval now = (const struct timeval){ 0 };
48 time_->GetTimeMonotonic(&now);
Darin Petkov19321e42012-12-17 15:40:59 +010049 if (last_log_stash_ &&
50 last_log_stash_ + kLogStashThrottleSeconds >
Darin Petkov328e19d2012-12-04 15:54:07 +010051 static_cast<uint64>(now.tv_sec)) {
52 LOG(INFO) << "Diagnostics throttled.";
53 return;
54 }
55
56 last_log_stash_ = now.tv_sec;
Darin Petkov38066762012-12-17 15:35:45 +010057 // Delete logs possibly stashed by a different user.
58 file_util::Delete(stashed_net_log_, false);
Darin Petkov385b9bc2012-12-03 15:25:05 +010059
Darin Petkovfac01f02012-12-06 12:44:07 +010060 LOG(INFO) << "Spawning " << kNetDiagsUpload << " @ " << last_log_stash_;
Darin Petkov38066762012-12-17 15:35:45 +010061 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 Petkovfac01f02012-12-06 12:44:07 +010074 }
Darin Petkov385b9bc2012-12-03 15:25:05 +010075}
76
Darin Petkovf0136cd2012-11-07 16:18:02 +010077bool 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 Petkovf0136cd2012-11-07 16:18:02 +010083} // namespace shill