blob: c1a2738939c569c43b70cb45176fd6f6d517c962 [file] [log] [blame]
David Zeuthen27a48bc2013-08-06 12:06:29 -07001// Copyright (c) 2013 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
Alex Vakulenko072359c2014-07-18 11:41:07 -07005// This provides access to timestamps with nanosecond resolution in
David Zeuthen27a48bc2013-08-06 12:06:29 -07006// struct stat, See NOTES in stat(2) for details.
7#ifndef _BSD_SOURCE
8#define _BSD_SOURCE
9#endif
10
11#include "update_engine/p2p_manager.h"
12
13#include <attr/xattr.h>
14#include <dirent.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <glib.h>
18#include <linux/falloc.h>
19#include <signal.h>
20#include <string.h>
21#include <sys/stat.h>
22#include <sys/statvfs.h>
23#include <sys/types.h>
24#include <unistd.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070025
Alex Vakulenkod2779df2014-06-16 13:19:00 -070026#include <algorithm>
David Zeuthen27a48bc2013-08-06 12:06:29 -070027#include <map>
Ben Chan02f7c1d2014-10-18 15:18:02 -070028#include <memory>
David Zeuthen27a48bc2013-08-06 12:06:29 -070029#include <utility>
30#include <vector>
31
Gilad Arnold4a0321b2014-10-28 15:57:30 -070032#include <base/bind.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070033#include <base/files/file_path.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070034#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070035#include <base/strings/stringprintf.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070036
Alex Deymo44666f92014-07-22 20:29:24 -070037#include "update_engine/glib_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070038#include "update_engine/update_manager/policy.h"
39#include "update_engine/update_manager/update_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070040#include "update_engine/utils.h"
41
Gilad Arnold4a0321b2014-10-28 15:57:30 -070042using base::Bind;
43using base::Callback;
David Zeuthen27a48bc2013-08-06 12:06:29 -070044using base::FilePath;
45using base::StringPrintf;
46using base::Time;
47using base::TimeDelta;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070048using chromeos_update_manager::EvalStatus;
49using chromeos_update_manager::Policy;
50using chromeos_update_manager::UpdateManager;
David Zeuthen27a48bc2013-08-06 12:06:29 -070051using std::map;
52using std::pair;
53using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070054using std::unique_ptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -070055using std::vector;
56
57namespace chromeos_update_engine {
58
59namespace {
60
61// The default p2p directory.
62const char kDefaultP2PDir[] = "/var/cache/p2p";
63
64// The p2p xattr used for conveying the final size of a file - see the
65// p2p ddoc for details.
66const char kCrosP2PFileSizeXAttrName[] = "user.cros-p2p-filesize";
67
Alex Vakulenkod2779df2014-06-16 13:19:00 -070068} // namespace
David Zeuthen27a48bc2013-08-06 12:06:29 -070069
70// The default P2PManager::Configuration implementation.
71class ConfigurationImpl : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070072 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070073 ConfigurationImpl() {}
74
Alex Deymo610277e2014-11-11 21:18:11 -080075 FilePath GetP2PDir() override {
Alex Deymof329b932014-10-30 01:37:48 -070076 return FilePath(kDefaultP2PDir);
David Zeuthen27a48bc2013-08-06 12:06:29 -070077 }
78
Alex Deymo610277e2014-11-11 21:18:11 -080079 vector<string> GetInitctlArgs(bool is_start) override {
David Zeuthen27a48bc2013-08-06 12:06:29 -070080 vector<string> args;
81 args.push_back("initctl");
82 args.push_back(is_start ? "start" : "stop");
83 args.push_back("p2p");
84 return args;
85 }
86
Alex Deymo610277e2014-11-11 21:18:11 -080087 vector<string> GetP2PClientArgs(const string &file_id,
88 size_t minimum_size) override {
David Zeuthen27a48bc2013-08-06 12:06:29 -070089 vector<string> args;
90 args.push_back("p2p-client");
91 args.push_back(string("--get-url=") + file_id);
Alex Deymof329b932014-10-30 01:37:48 -070092 args.push_back(StringPrintf("--minimum-size=%zu", minimum_size));
David Zeuthen27a48bc2013-08-06 12:06:29 -070093 return args;
94 }
95
Alex Vakulenkod2779df2014-06-16 13:19:00 -070096 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070097 DISALLOW_COPY_AND_ASSIGN(ConfigurationImpl);
98};
99
100// The default P2PManager implementation.
101class P2PManagerImpl : public P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700102 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700103 P2PManagerImpl(Configuration *configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500104 ClockInterface *clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700105 UpdateManager* update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700106 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500107 const int num_files_to_keep,
108 const base::TimeDelta& max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700109
110 // P2PManager methods.
Alex Deymo610277e2014-11-11 21:18:11 -0800111 void SetDevicePolicy(const policy::DevicePolicy* device_policy) override;
112 bool IsP2PEnabled() override;
113 bool EnsureP2PRunning() override;
114 bool EnsureP2PNotRunning() override;
115 bool PerformHousekeeping() override;
116 void LookupUrlForFile(const string& file_id,
117 size_t minimum_size,
118 TimeDelta max_time_to_wait,
119 LookupCallback callback) override;
120 bool FileShare(const string& file_id,
121 size_t expected_size) override;
122 FilePath FileGetPath(const string& file_id) override;
123 ssize_t FileGetSize(const string& file_id) override;
124 ssize_t FileGetExpectedSize(const string& file_id) override;
125 bool FileGetVisible(const string& file_id,
126 bool *out_result) override;
127 bool FileMakeVisible(const string& file_id) override;
128 int CountSharedFiles() override;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700129
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700130 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700131 // Enumeration for specifying visibility.
132 enum Visibility {
133 kVisible,
134 kNonVisible
135 };
136
137 // Returns "." + |file_extension_| + ".p2p" if |visibility| is
138 // |kVisible|. Returns the same concatenated with ".tmp" otherwise.
139 string GetExt(Visibility visibility);
140
141 // Gets the on-disk path for |file_id| depending on if the file
142 // is visible or not.
Alex Deymof329b932014-10-30 01:37:48 -0700143 FilePath GetPath(const string& file_id, Visibility visibility);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700144
145 // Utility function used by EnsureP2PRunning() and EnsureP2PNotRunning().
146 bool EnsureP2P(bool should_be_running);
147
David Zeuthen41f2cf52014-11-05 12:29:45 -0500148 // Utility function to delete a file given by |path| and log the
149 // path as well as |reason|. Returns false on failure.
150 bool DeleteP2PFile(const FilePath& path, const std::string& reason);
151
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700152 // Schedules an async request for tracking changes in P2P enabled status.
153 void ScheduleEnabledStatusChange();
154
155 // An async callback used by the above.
156 void OnEnabledStatusChange(EvalStatus status, const bool& result);
157
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700158 // The device policy being used or null if no policy is being used.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700159 const policy::DevicePolicy* device_policy_ = nullptr;
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700160
David Zeuthen27a48bc2013-08-06 12:06:29 -0700161 // Configuration object.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700162 unique_ptr<Configuration> configuration_;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700163
David Zeuthen41f2cf52014-11-05 12:29:45 -0500164 // Object for telling the time.
165 ClockInterface* clock_;
166
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700167 // A pointer to the global Update Manager.
168 UpdateManager* update_manager_;
169
David Zeuthen27a48bc2013-08-06 12:06:29 -0700170 // A short string unique to the application (for example "cros_au")
171 // used to mark a file as being owned by a particular application.
172 const string file_extension_;
173
174 // If non-zero, this number denotes how many files in /var/cache/p2p
175 // owned by the application (cf. |file_extension_|) to keep after
176 // performing housekeeping.
177 const int num_files_to_keep_;
178
David Zeuthen41f2cf52014-11-05 12:29:45 -0500179 // If non-zero, files older than this will not be kept after
180 // performing housekeeping.
181 const base::TimeDelta max_file_age_;
182
David Zeuthen27a48bc2013-08-06 12:06:29 -0700183 // The string ".p2p".
184 static const char kP2PExtension[];
185
186 // The string ".tmp".
187 static const char kTmpExtension[];
188
Gilad Arnoldccd09572014-10-27 13:37:50 -0700189 // Whether P2P service may be running; initially, we assume it may be.
190 bool may_be_running_ = true;
191
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700192 // The current known enabled status of the P2P feature (initialized lazily),
193 // and whether an async status check has been scheduled.
194 bool is_enabled_;
195 bool waiting_for_enabled_status_change_ = false;
196
David Zeuthen27a48bc2013-08-06 12:06:29 -0700197 DISALLOW_COPY_AND_ASSIGN(P2PManagerImpl);
198};
199
200const char P2PManagerImpl::kP2PExtension[] = ".p2p";
201
202const char P2PManagerImpl::kTmpExtension[] = ".tmp";
203
204P2PManagerImpl::P2PManagerImpl(Configuration *configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500205 ClockInterface *clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700206 UpdateManager* update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700207 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500208 const int num_files_to_keep,
209 const base::TimeDelta& max_file_age)
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700210 : clock_(clock),
211 update_manager_(update_manager),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700212 file_extension_(file_extension),
David Zeuthen41f2cf52014-11-05 12:29:45 -0500213 num_files_to_keep_(num_files_to_keep),
214 max_file_age_(max_file_age) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700215 configuration_.reset(configuration != nullptr ? configuration :
David Zeuthen27a48bc2013-08-06 12:06:29 -0700216 new ConfigurationImpl());
217}
218
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700219void P2PManagerImpl::SetDevicePolicy(
220 const policy::DevicePolicy* device_policy) {
221 device_policy_ = device_policy;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700222}
223
224bool P2PManagerImpl::IsP2PEnabled() {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700225 if (!waiting_for_enabled_status_change_) {
226 // Get and store an initial value.
227 if (update_manager_->PolicyRequest(&Policy::P2PEnabled, &is_enabled_) ==
228 EvalStatus::kFailed) {
229 is_enabled_ = false;
230 LOG(ERROR) << "Querying P2P enabled status failed, disabling.";
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400231 }
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700232
233 // Track future changes (async).
234 ScheduleEnabledStatusChange();
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400235 }
236
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700237 return is_enabled_;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700238}
239
240bool P2PManagerImpl::EnsureP2P(bool should_be_running) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700241 gchar *standard_error = nullptr;
242 GError *error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700243 gint exit_status = 0;
244
Gilad Arnoldccd09572014-10-27 13:37:50 -0700245 may_be_running_ = true; // Unless successful, we must be conservative.
246
David Zeuthen27a48bc2013-08-06 12:06:29 -0700247 vector<string> args = configuration_->GetInitctlArgs(should_be_running);
Alex Deymo8d2bbe32015-06-23 16:28:59 -0700248 unique_ptr<gchar*, utils::GLibStrvFreeDeleter> argv(
David Zeuthen27a48bc2013-08-06 12:06:29 -0700249 utils::StringVectorToGStrv(args));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700250 if (!g_spawn_sync(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700251 argv.get(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700252 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700253 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700254 nullptr, nullptr, // child_setup, user_data
255 nullptr, // standard_output
David Zeuthen27a48bc2013-08-06 12:06:29 -0700256 &standard_error,
257 &exit_status,
258 &error)) {
259 LOG(ERROR) << "Error spawning " << utils::StringVectorToString(args)
260 << ": " << utils::GetAndFreeGError(&error);
261 return false;
262 }
Alex Deymo8d2bbe32015-06-23 16:28:59 -0700263 unique_ptr<gchar, utils::GLibFreeDeleter> standard_error_deleter(
264 standard_error);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700265
266 if (!WIFEXITED(exit_status)) {
267 LOG(ERROR) << "Error spawning '" << utils::StringVectorToString(args)
268 << "': WIFEXITED is false";
269 return false;
270 }
271
Gilad Arnoldccd09572014-10-27 13:37:50 -0700272 // If initctl(8) does not exit normally (exit status other than zero), ensure
273 // that the error message is not benign by scanning stderr; this is a
274 // necessity because initctl does not offer actions such as "start if not
275 // running" or "stop if running".
David Zeuthen27a48bc2013-08-06 12:06:29 -0700276 // TODO(zeuthen,chromium:277051): Avoid doing this.
Gilad Arnoldccd09572014-10-27 13:37:50 -0700277 if (WEXITSTATUS(exit_status) != 0) {
278 const gchar *expected_error_message = should_be_running ?
279 "initctl: Job is already running: p2p\n" :
280 "initctl: Unknown instance \n";
281 if (g_strcmp0(standard_error, expected_error_message) != 0)
282 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700283 }
284
Gilad Arnoldccd09572014-10-27 13:37:50 -0700285 may_be_running_ = should_be_running; // Successful after all.
286 return true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700287}
288
289bool P2PManagerImpl::EnsureP2PRunning() {
290 return EnsureP2P(true);
291}
292
293bool P2PManagerImpl::EnsureP2PNotRunning() {
294 return EnsureP2P(false);
295}
296
297// Returns True if the timestamp in the first pair is greater than the
298// timestamp in the latter. If used with std::sort() this will yield a
299// sequence of elements where newer (high timestamps) elements precede
300// older ones (low timestamps).
301static bool MatchCompareFunc(const pair<FilePath, Time>& a,
302 const pair<FilePath, Time>& b) {
303 return a.second > b.second;
304}
305
306string P2PManagerImpl::GetExt(Visibility visibility) {
307 string ext = string(".") + file_extension_ + kP2PExtension;
308 switch (visibility) {
309 case kVisible:
310 break;
311 case kNonVisible:
312 ext += kTmpExtension;
313 break;
314 // Don't add a default case to let the compiler warn about newly
315 // added enum values.
316 }
317 return ext;
318}
319
320FilePath P2PManagerImpl::GetPath(const string& file_id, Visibility visibility) {
321 return configuration_->GetP2PDir().Append(file_id + GetExt(visibility));
322}
323
David Zeuthen41f2cf52014-11-05 12:29:45 -0500324bool P2PManagerImpl::DeleteP2PFile(const FilePath& path,
325 const std::string& reason) {
326 LOG(INFO) << "Deleting p2p file " << path.value()
327 << " (reason: " << reason << ")";
328 if (unlink(path.value().c_str()) != 0) {
329 PLOG(ERROR) << "Error deleting p2p file " << path.value();
330 return false;
331 }
332 return true;
333}
David Zeuthen27a48bc2013-08-06 12:06:29 -0700334
David Zeuthen41f2cf52014-11-05 12:29:45 -0500335
336bool P2PManagerImpl::PerformHousekeeping() {
337 // Open p2p dir.
Alex Deymof329b932014-10-30 01:37:48 -0700338 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500339 GError* error = nullptr;
340 GDir* dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700341 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700342 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
343 << utils::GetAndFreeGError(&error);
344 return false;
345 }
346
David Zeuthen41f2cf52014-11-05 12:29:45 -0500347 // Go through all files and collect their mtime.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700348 string ext_visible = GetExt(kVisible);
349 string ext_non_visible = GetExt(kNonVisible);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500350 bool deletion_failed = false;
351 const char* name = nullptr;
352 vector<pair<FilePath, Time>> matches;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700353 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700354 if (!(g_str_has_suffix(name, ext_visible.c_str()) ||
355 g_str_has_suffix(name, ext_non_visible.c_str())))
356 continue;
357
358 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700359 FilePath file = p2p_dir.Append(name);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700360 if (stat(file.value().c_str(), &statbuf) != 0) {
361 PLOG(ERROR) << "Error getting file status for " << file.value();
362 continue;
363 }
364
David Zeuthen41f2cf52014-11-05 12:29:45 -0500365 Time time = utils::TimeFromStructTimespec(&statbuf.st_mtim);
366
367 // If instructed to keep only files younger than a given age
368 // (|max_file_age_| != 0), delete files satisfying this criteria
369 // right now. Otherwise add it to a list we'll consider for later.
370 if (clock_ != nullptr && max_file_age_ != base::TimeDelta() &&
371 clock_->GetWallclockTime() - time > max_file_age_) {
372 if (!DeleteP2PFile(file, "file too old"))
373 deletion_failed = true;
374 } else {
375 matches.push_back(std::make_pair(file, time));
376 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700377 }
378 g_dir_close(dir);
379
David Zeuthen41f2cf52014-11-05 12:29:45 -0500380 // If instructed to only keep N files (|max_files_to_keep_ != 0),
381 // sort list of matches, newest (biggest time) to oldest (lowest
382 // time). Then delete starting at element |num_files_to_keep_|.
383 if (num_files_to_keep_ > 0) {
384 std::sort(matches.begin(), matches.end(), MatchCompareFunc);
385 vector<pair<FilePath, Time>>::const_iterator i;
386 for (i = matches.begin() + num_files_to_keep_; i < matches.end(); ++i) {
387 if (!DeleteP2PFile(i->first, "too many files"))
388 deletion_failed = true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700389 }
390 }
391
David Zeuthen41f2cf52014-11-05 12:29:45 -0500392 return !deletion_failed;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700393}
394
395// Helper class for implementing LookupUrlForFile().
396class LookupData {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700397 public:
398 explicit LookupData(P2PManager::LookupCallback callback)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700399 : callback_(callback),
400 pid_(0),
401 stdout_fd_(-1),
402 stdout_channel_source_id_(0),
403 child_watch_source_id_(0),
404 timeout_source_id_(0),
405 reported_(false) {}
406
407 ~LookupData() {
408 if (child_watch_source_id_ != 0)
409 g_source_remove(child_watch_source_id_);
410 if (stdout_channel_source_id_ != 0)
411 g_source_remove(stdout_channel_source_id_);
412 if (timeout_source_id_ != 0)
413 g_source_remove(timeout_source_id_);
414 if (stdout_fd_ != -1)
415 close(stdout_fd_);
416 if (pid_ != 0)
417 kill(pid_, SIGTERM);
418 }
419
420 void InitiateLookup(gchar **argv, TimeDelta timeout) {
421 // NOTE: if we fail early (i.e. in this method), we need to schedule
422 // an idle to report the error. This is because we guarantee that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700423 // the callback is always called from the GLib mainloop (this
David Zeuthen27a48bc2013-08-06 12:06:29 -0700424 // guarantee is useful for testing).
425
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700426 GError *error = nullptr;
427 if (!g_spawn_async_with_pipes(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700428 argv,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700429 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700430 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
431 G_SPAWN_DO_NOT_REAP_CHILD),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700432 nullptr, // child_setup
David Zeuthen27a48bc2013-08-06 12:06:29 -0700433 this,
434 &pid_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700435 nullptr, // standard_input
David Zeuthen27a48bc2013-08-06 12:06:29 -0700436 &stdout_fd_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700437 nullptr, // standard_error
David Zeuthen27a48bc2013-08-06 12:06:29 -0700438 &error)) {
439 LOG(ERROR) << "Error spawning p2p-client: "
440 << utils::GetAndFreeGError(&error);
441 ReportErrorAndDeleteInIdle();
442 return;
443 }
444
445 GIOChannel* io_channel = g_io_channel_unix_new(stdout_fd_);
446 stdout_channel_source_id_ = g_io_add_watch(
447 io_channel,
448 static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),
449 OnIOChannelActivity, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700450 CHECK_NE(stdout_channel_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700451 g_io_channel_unref(io_channel);
452
453 child_watch_source_id_ = g_child_watch_add(pid_, OnChildWatchActivity,
454 this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700455 CHECK_NE(child_watch_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700456
457 if (timeout.ToInternalValue() > 0) {
458 timeout_source_id_ = g_timeout_add(timeout.InMilliseconds(),
459 OnTimeout, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700460 CHECK_NE(timeout_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700461 }
462 }
463
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700464 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700465 void ReportErrorAndDeleteInIdle() {
466 g_idle_add(static_cast<GSourceFunc>(OnIdleForReportErrorAndDelete), this);
467 }
468
469 static gboolean OnIdleForReportErrorAndDelete(gpointer user_data) {
470 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
471 lookup_data->ReportError();
472 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700473 return FALSE; // Remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700474 }
475
476 void IssueCallback(const string& url) {
477 if (!callback_.is_null())
478 callback_.Run(url);
479 }
480
481 void ReportError() {
482 if (reported_)
483 return;
484 IssueCallback("");
485 reported_ = true;
486 }
487
488 void ReportSuccess() {
489 if (reported_)
490 return;
491
492 string url = stdout_;
493 size_t newline_pos = url.find('\n');
494 if (newline_pos != string::npos)
495 url.resize(newline_pos);
496
497 // Since p2p-client(1) is constructing this URL itself strictly
498 // speaking there's no need to validate it... but, anyway, can't
499 // hurt.
500 if (url.compare(0, 7, "http://") == 0) {
501 IssueCallback(url);
502 } else {
503 LOG(ERROR) << "p2p URL '" << url << "' does not look right. Ignoring.";
504 ReportError();
505 }
506
507 reported_ = true;
508 }
509
510 static gboolean OnIOChannelActivity(GIOChannel *source,
511 GIOCondition condition,
512 gpointer user_data) {
513 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700514 gchar* str = nullptr;
515 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700516 GIOStatus status = g_io_channel_read_line(source,
517 &str,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700518 nullptr, // len
519 nullptr, // line_terminator
David Zeuthen27a48bc2013-08-06 12:06:29 -0700520 &error);
521 if (status != G_IO_STATUS_NORMAL) {
522 // Ignore EOF since we usually get that before SIGCHLD and we
523 // need to examine exit status there.
524 if (status != G_IO_STATUS_EOF) {
525 LOG(ERROR) << "Error reading a line from p2p-client: "
526 << utils::GetAndFreeGError(&error);
527 lookup_data->ReportError();
528 delete lookup_data;
529 }
530 } else {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700531 if (str != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700532 lookup_data->stdout_ += str;
533 g_free(str);
534 }
535 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700536 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700537 }
538
539 static void OnChildWatchActivity(GPid pid,
540 gint status,
541 gpointer user_data) {
542 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
543
544 if (!WIFEXITED(status)) {
545 LOG(ERROR) << "Child didn't exit normally";
546 lookup_data->ReportError();
547 } else if (WEXITSTATUS(status) != 0) {
548 LOG(INFO) << "Child exited with non-zero exit code "
549 << WEXITSTATUS(status);
550 lookup_data->ReportError();
551 } else {
552 lookup_data->ReportSuccess();
553 }
554 delete lookup_data;
555 }
556
557 static gboolean OnTimeout(gpointer user_data) {
558 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
559 lookup_data->ReportError();
560 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700561 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700562 }
563
564 P2PManager::LookupCallback callback_;
565 GPid pid_;
566 gint stdout_fd_;
567 guint stdout_channel_source_id_;
568 guint child_watch_source_id_;
569 guint timeout_source_id_;
570 string stdout_;
571 bool reported_;
572};
573
574void P2PManagerImpl::LookupUrlForFile(const string& file_id,
575 size_t minimum_size,
576 TimeDelta max_time_to_wait,
577 LookupCallback callback) {
578 LookupData *lookup_data = new LookupData(callback);
579 string file_id_with_ext = file_id + "." + file_extension_;
580 vector<string> args = configuration_->GetP2PClientArgs(file_id_with_ext,
581 minimum_size);
582 gchar **argv = utils::StringVectorToGStrv(args);
583 lookup_data->InitiateLookup(argv, max_time_to_wait);
584 g_strfreev(argv);
585}
586
587bool P2PManagerImpl::FileShare(const string& file_id,
588 size_t expected_size) {
589 // Check if file already exist.
Alex Deymof329b932014-10-30 01:37:48 -0700590 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700591 if (!path.empty()) {
592 // File exists - double check its expected size though.
593 ssize_t file_expected_size = FileGetExpectedSize(file_id);
594 if (file_expected_size == -1 ||
595 static_cast<size_t>(file_expected_size) != expected_size) {
596 LOG(ERROR) << "Existing p2p file " << path.value()
597 << " with expected_size=" << file_expected_size
598 << " does not match the passed in"
599 << " expected_size=" << expected_size;
600 return false;
601 }
602 return true;
603 }
604
605 // Before creating the file, bail if statvfs(3) indicates that at
606 // least twice the size is not available in P2P_DIR.
607 struct statvfs statvfsbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700608 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700609 if (statvfs(p2p_dir.value().c_str(), &statvfsbuf) != 0) {
610 PLOG(ERROR) << "Error calling statvfs() for dir " << p2p_dir.value();
611 return false;
612 }
613 size_t free_bytes =
614 static_cast<size_t>(statvfsbuf.f_bsize) * statvfsbuf.f_bavail;
615 if (free_bytes < 2 * expected_size) {
616 // This can easily happen and is worth reporting.
617 LOG(INFO) << "Refusing to allocate p2p file of " << expected_size
618 << " bytes since the directory " << p2p_dir.value()
619 << " only has " << free_bytes
620 << " bytes available and this is less than twice the"
621 << " requested size.";
622 return false;
623 }
624
625 // Okie-dokey looks like enough space is available - create the file.
626 path = GetPath(file_id, kNonVisible);
627 int fd = open(path.value().c_str(), O_CREAT | O_RDWR, 0644);
628 if (fd == -1) {
629 PLOG(ERROR) << "Error creating file with path " << path.value();
630 return false;
631 }
632 ScopedFdCloser fd_closer(&fd);
633
634 // If the final size is known, allocate the file (e.g. reserve disk
635 // space) and set the user.cros-p2p-filesize xattr.
636 if (expected_size != 0) {
637 if (fallocate(fd,
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700638 FALLOC_FL_KEEP_SIZE, // Keep file size as 0.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700639 0,
640 expected_size) != 0) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700641 if (errno == ENOSYS || errno == EOPNOTSUPP) {
642 // If the filesystem doesn't support the fallocate, keep
643 // going. This is helpful when running unit tests on build
644 // machines with ancient filesystems and/or OSes.
645 PLOG(WARNING) << "Ignoring fallocate(2) failure";
646 } else {
647 // ENOSPC can happen (funky race though, cf. the statvfs() check
648 // above), handle it gracefully, e.g. use logging level INFO.
649 PLOG(INFO) << "Error allocating " << expected_size
650 << " bytes for file " << path.value();
651 if (unlink(path.value().c_str()) != 0) {
652 PLOG(ERROR) << "Error deleting file with path " << path.value();
653 }
654 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700655 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700656 }
657
Alex Deymof329b932014-10-30 01:37:48 -0700658 string decimal_size = StringPrintf("%zu", expected_size);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700659 if (fsetxattr(fd, kCrosP2PFileSizeXAttrName,
660 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
661 PLOG(ERROR) << "Error setting xattr " << path.value();
662 return false;
663 }
664 }
665
666 return true;
667}
668
669FilePath P2PManagerImpl::FileGetPath(const string& file_id) {
670 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700671 FilePath path;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700672
673 path = GetPath(file_id, kVisible);
674 if (stat(path.value().c_str(), &statbuf) == 0) {
675 return path;
676 }
677
678 path = GetPath(file_id, kNonVisible);
679 if (stat(path.value().c_str(), &statbuf) == 0) {
680 return path;
681 }
682
683 path.clear();
684 return path;
685}
686
687bool P2PManagerImpl::FileGetVisible(const string& file_id,
688 bool *out_result) {
Alex Deymof329b932014-10-30 01:37:48 -0700689 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700690 if (path.empty()) {
691 LOG(ERROR) << "No file for id " << file_id;
692 return false;
693 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700694 if (out_result != nullptr)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700695 *out_result = path.MatchesExtension(kP2PExtension);
696 return true;
697}
698
699bool P2PManagerImpl::FileMakeVisible(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700700 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700701 if (path.empty()) {
702 LOG(ERROR) << "No file for id " << file_id;
703 return false;
704 }
705
706 // Already visible?
707 if (path.MatchesExtension(kP2PExtension))
708 return true;
709
710 LOG_ASSERT(path.MatchesExtension(kTmpExtension));
Alex Deymof329b932014-10-30 01:37:48 -0700711 FilePath new_path = path.RemoveExtension();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700712 LOG_ASSERT(new_path.MatchesExtension(kP2PExtension));
713 if (rename(path.value().c_str(), new_path.value().c_str()) != 0) {
714 PLOG(ERROR) << "Error renaming " << path.value()
715 << " to " << new_path.value();
716 return false;
717 }
718
719 return true;
720}
721
722ssize_t P2PManagerImpl::FileGetSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700723 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700724 if (path.empty())
725 return -1;
726
Gabe Blacka77939e2014-09-09 23:35:08 -0700727 return utils::FileSize(path.value());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700728}
729
730ssize_t P2PManagerImpl::FileGetExpectedSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700731 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700732 if (path.empty())
733 return -1;
734
735 char ea_value[64] = { 0 };
736 ssize_t ea_size;
737 ea_size = getxattr(path.value().c_str(), kCrosP2PFileSizeXAttrName,
738 &ea_value, sizeof(ea_value) - 1);
739 if (ea_size == -1) {
740 PLOG(ERROR) << "Error calling getxattr() on file " << path.value();
741 return -1;
742 }
743
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700744 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700745 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700746 if (*endp != '\0') {
747 LOG(ERROR) << "Error parsing the value '" << ea_value
748 << "' of the xattr " << kCrosP2PFileSizeXAttrName
749 << " as an integer";
750 return -1;
751 }
752
753 return val;
754}
755
756int P2PManagerImpl::CountSharedFiles() {
757 GDir* dir;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700758 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700759 const char* name;
760 int num_files = 0;
761
Alex Deymof329b932014-10-30 01:37:48 -0700762 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700763 dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700764 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700765 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
766 << utils::GetAndFreeGError(&error);
767 return -1;
768 }
769
770 string ext_visible = GetExt(kVisible);
771 string ext_non_visible = GetExt(kNonVisible);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700772 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700773 if (g_str_has_suffix(name, ext_visible.c_str()) ||
774 g_str_has_suffix(name, ext_non_visible.c_str())) {
775 num_files += 1;
776 }
777 }
778 g_dir_close(dir);
779
780 return num_files;
781}
782
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700783void P2PManagerImpl::ScheduleEnabledStatusChange() {
784 if (waiting_for_enabled_status_change_)
785 return;
Gilad Arnoldccd09572014-10-27 13:37:50 -0700786
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700787 Callback<void(EvalStatus, const bool&)> callback = Bind(
788 &P2PManagerImpl::OnEnabledStatusChange, base::Unretained(this));
789 update_manager_->AsyncPolicyRequest(callback, &Policy::P2PEnabledChanged,
790 is_enabled_);
791 waiting_for_enabled_status_change_ = true;
Gilad Arnoldccd09572014-10-27 13:37:50 -0700792}
793
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700794void P2PManagerImpl::OnEnabledStatusChange(EvalStatus status,
795 const bool& result) {
796 waiting_for_enabled_status_change_ = false;
797
798 if (status == EvalStatus::kSucceeded) {
799 if (result == is_enabled_) {
800 LOG(WARNING) << "P2P enabled status did not change, which means that it "
801 "is permanent; not scheduling further checks.";
802 waiting_for_enabled_status_change_ = true;
803 return;
804 }
805
806 is_enabled_ = result;
807
808 // If P2P is running but shouldn't be, make sure it isn't.
809 if (may_be_running_ && !is_enabled_ && !EnsureP2PNotRunning()) {
810 LOG(WARNING) << "Failed to stop P2P service.";
811 }
812 } else {
813 LOG(WARNING)
814 << "P2P enabled tracking failed (possibly timed out); retrying.";
815 }
816
817 ScheduleEnabledStatusChange();
818}
819
820P2PManager* P2PManager::Construct(
821 Configuration *configuration,
822 ClockInterface *clock,
823 UpdateManager* update_manager,
824 const string& file_extension,
825 const int num_files_to_keep,
826 const base::TimeDelta& max_file_age) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700827 return new P2PManagerImpl(configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500828 clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700829 update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700830 file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500831 num_files_to_keep,
832 max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700833}
834
835} // namespace chromeos_update_engine