blob: 8d519faa75e79dd06867d92c9fee1e6b8f834e71 [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);
Ben Chan02f7c1d2014-10-18 15:18:02 -0700248 unique_ptr<gchar*, 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 }
Ben Chan02f7c1d2014-10-18 15:18:02 -0700263 unique_ptr<gchar, GLibFreeDeleter> standard_error_deleter(standard_error);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700264
265 if (!WIFEXITED(exit_status)) {
266 LOG(ERROR) << "Error spawning '" << utils::StringVectorToString(args)
267 << "': WIFEXITED is false";
268 return false;
269 }
270
Gilad Arnoldccd09572014-10-27 13:37:50 -0700271 // If initctl(8) does not exit normally (exit status other than zero), ensure
272 // that the error message is not benign by scanning stderr; this is a
273 // necessity because initctl does not offer actions such as "start if not
274 // running" or "stop if running".
David Zeuthen27a48bc2013-08-06 12:06:29 -0700275 // TODO(zeuthen,chromium:277051): Avoid doing this.
Gilad Arnoldccd09572014-10-27 13:37:50 -0700276 if (WEXITSTATUS(exit_status) != 0) {
277 const gchar *expected_error_message = should_be_running ?
278 "initctl: Job is already running: p2p\n" :
279 "initctl: Unknown instance \n";
280 if (g_strcmp0(standard_error, expected_error_message) != 0)
281 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700282 }
283
Gilad Arnoldccd09572014-10-27 13:37:50 -0700284 may_be_running_ = should_be_running; // Successful after all.
285 return true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700286}
287
288bool P2PManagerImpl::EnsureP2PRunning() {
289 return EnsureP2P(true);
290}
291
292bool P2PManagerImpl::EnsureP2PNotRunning() {
293 return EnsureP2P(false);
294}
295
296// Returns True if the timestamp in the first pair is greater than the
297// timestamp in the latter. If used with std::sort() this will yield a
298// sequence of elements where newer (high timestamps) elements precede
299// older ones (low timestamps).
300static bool MatchCompareFunc(const pair<FilePath, Time>& a,
301 const pair<FilePath, Time>& b) {
302 return a.second > b.second;
303}
304
305string P2PManagerImpl::GetExt(Visibility visibility) {
306 string ext = string(".") + file_extension_ + kP2PExtension;
307 switch (visibility) {
308 case kVisible:
309 break;
310 case kNonVisible:
311 ext += kTmpExtension;
312 break;
313 // Don't add a default case to let the compiler warn about newly
314 // added enum values.
315 }
316 return ext;
317}
318
319FilePath P2PManagerImpl::GetPath(const string& file_id, Visibility visibility) {
320 return configuration_->GetP2PDir().Append(file_id + GetExt(visibility));
321}
322
David Zeuthen41f2cf52014-11-05 12:29:45 -0500323bool P2PManagerImpl::DeleteP2PFile(const FilePath& path,
324 const std::string& reason) {
325 LOG(INFO) << "Deleting p2p file " << path.value()
326 << " (reason: " << reason << ")";
327 if (unlink(path.value().c_str()) != 0) {
328 PLOG(ERROR) << "Error deleting p2p file " << path.value();
329 return false;
330 }
331 return true;
332}
David Zeuthen27a48bc2013-08-06 12:06:29 -0700333
David Zeuthen41f2cf52014-11-05 12:29:45 -0500334
335bool P2PManagerImpl::PerformHousekeeping() {
336 // Open p2p dir.
Alex Deymof329b932014-10-30 01:37:48 -0700337 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500338 GError* error = nullptr;
339 GDir* dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700340 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700341 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
342 << utils::GetAndFreeGError(&error);
343 return false;
344 }
345
David Zeuthen41f2cf52014-11-05 12:29:45 -0500346 // Go through all files and collect their mtime.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700347 string ext_visible = GetExt(kVisible);
348 string ext_non_visible = GetExt(kNonVisible);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500349 bool deletion_failed = false;
350 const char* name = nullptr;
351 vector<pair<FilePath, Time>> matches;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700352 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700353 if (!(g_str_has_suffix(name, ext_visible.c_str()) ||
354 g_str_has_suffix(name, ext_non_visible.c_str())))
355 continue;
356
357 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700358 FilePath file = p2p_dir.Append(name);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700359 if (stat(file.value().c_str(), &statbuf) != 0) {
360 PLOG(ERROR) << "Error getting file status for " << file.value();
361 continue;
362 }
363
David Zeuthen41f2cf52014-11-05 12:29:45 -0500364 Time time = utils::TimeFromStructTimespec(&statbuf.st_mtim);
365
366 // If instructed to keep only files younger than a given age
367 // (|max_file_age_| != 0), delete files satisfying this criteria
368 // right now. Otherwise add it to a list we'll consider for later.
369 if (clock_ != nullptr && max_file_age_ != base::TimeDelta() &&
370 clock_->GetWallclockTime() - time > max_file_age_) {
371 if (!DeleteP2PFile(file, "file too old"))
372 deletion_failed = true;
373 } else {
374 matches.push_back(std::make_pair(file, time));
375 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700376 }
377 g_dir_close(dir);
378
David Zeuthen41f2cf52014-11-05 12:29:45 -0500379 // If instructed to only keep N files (|max_files_to_keep_ != 0),
380 // sort list of matches, newest (biggest time) to oldest (lowest
381 // time). Then delete starting at element |num_files_to_keep_|.
382 if (num_files_to_keep_ > 0) {
383 std::sort(matches.begin(), matches.end(), MatchCompareFunc);
384 vector<pair<FilePath, Time>>::const_iterator i;
385 for (i = matches.begin() + num_files_to_keep_; i < matches.end(); ++i) {
386 if (!DeleteP2PFile(i->first, "too many files"))
387 deletion_failed = true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700388 }
389 }
390
David Zeuthen41f2cf52014-11-05 12:29:45 -0500391 return !deletion_failed;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700392}
393
394// Helper class for implementing LookupUrlForFile().
395class LookupData {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700396 public:
397 explicit LookupData(P2PManager::LookupCallback callback)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700398 : callback_(callback),
399 pid_(0),
400 stdout_fd_(-1),
401 stdout_channel_source_id_(0),
402 child_watch_source_id_(0),
403 timeout_source_id_(0),
404 reported_(false) {}
405
406 ~LookupData() {
407 if (child_watch_source_id_ != 0)
408 g_source_remove(child_watch_source_id_);
409 if (stdout_channel_source_id_ != 0)
410 g_source_remove(stdout_channel_source_id_);
411 if (timeout_source_id_ != 0)
412 g_source_remove(timeout_source_id_);
413 if (stdout_fd_ != -1)
414 close(stdout_fd_);
415 if (pid_ != 0)
416 kill(pid_, SIGTERM);
417 }
418
419 void InitiateLookup(gchar **argv, TimeDelta timeout) {
420 // NOTE: if we fail early (i.e. in this method), we need to schedule
421 // an idle to report the error. This is because we guarantee that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700422 // the callback is always called from the GLib mainloop (this
David Zeuthen27a48bc2013-08-06 12:06:29 -0700423 // guarantee is useful for testing).
424
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700425 GError *error = nullptr;
426 if (!g_spawn_async_with_pipes(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700427 argv,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700428 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700429 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
430 G_SPAWN_DO_NOT_REAP_CHILD),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700431 nullptr, // child_setup
David Zeuthen27a48bc2013-08-06 12:06:29 -0700432 this,
433 &pid_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700434 nullptr, // standard_input
David Zeuthen27a48bc2013-08-06 12:06:29 -0700435 &stdout_fd_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700436 nullptr, // standard_error
David Zeuthen27a48bc2013-08-06 12:06:29 -0700437 &error)) {
438 LOG(ERROR) << "Error spawning p2p-client: "
439 << utils::GetAndFreeGError(&error);
440 ReportErrorAndDeleteInIdle();
441 return;
442 }
443
444 GIOChannel* io_channel = g_io_channel_unix_new(stdout_fd_);
445 stdout_channel_source_id_ = g_io_add_watch(
446 io_channel,
447 static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),
448 OnIOChannelActivity, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700449 CHECK_NE(stdout_channel_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700450 g_io_channel_unref(io_channel);
451
452 child_watch_source_id_ = g_child_watch_add(pid_, OnChildWatchActivity,
453 this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700454 CHECK_NE(child_watch_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700455
456 if (timeout.ToInternalValue() > 0) {
457 timeout_source_id_ = g_timeout_add(timeout.InMilliseconds(),
458 OnTimeout, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700459 CHECK_NE(timeout_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700460 }
461 }
462
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700463 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700464 void ReportErrorAndDeleteInIdle() {
465 g_idle_add(static_cast<GSourceFunc>(OnIdleForReportErrorAndDelete), this);
466 }
467
468 static gboolean OnIdleForReportErrorAndDelete(gpointer user_data) {
469 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
470 lookup_data->ReportError();
471 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700472 return FALSE; // Remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700473 }
474
475 void IssueCallback(const string& url) {
476 if (!callback_.is_null())
477 callback_.Run(url);
478 }
479
480 void ReportError() {
481 if (reported_)
482 return;
483 IssueCallback("");
484 reported_ = true;
485 }
486
487 void ReportSuccess() {
488 if (reported_)
489 return;
490
491 string url = stdout_;
492 size_t newline_pos = url.find('\n');
493 if (newline_pos != string::npos)
494 url.resize(newline_pos);
495
496 // Since p2p-client(1) is constructing this URL itself strictly
497 // speaking there's no need to validate it... but, anyway, can't
498 // hurt.
499 if (url.compare(0, 7, "http://") == 0) {
500 IssueCallback(url);
501 } else {
502 LOG(ERROR) << "p2p URL '" << url << "' does not look right. Ignoring.";
503 ReportError();
504 }
505
506 reported_ = true;
507 }
508
509 static gboolean OnIOChannelActivity(GIOChannel *source,
510 GIOCondition condition,
511 gpointer user_data) {
512 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700513 gchar* str = nullptr;
514 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700515 GIOStatus status = g_io_channel_read_line(source,
516 &str,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700517 nullptr, // len
518 nullptr, // line_terminator
David Zeuthen27a48bc2013-08-06 12:06:29 -0700519 &error);
520 if (status != G_IO_STATUS_NORMAL) {
521 // Ignore EOF since we usually get that before SIGCHLD and we
522 // need to examine exit status there.
523 if (status != G_IO_STATUS_EOF) {
524 LOG(ERROR) << "Error reading a line from p2p-client: "
525 << utils::GetAndFreeGError(&error);
526 lookup_data->ReportError();
527 delete lookup_data;
528 }
529 } else {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700530 if (str != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700531 lookup_data->stdout_ += str;
532 g_free(str);
533 }
534 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700535 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700536 }
537
538 static void OnChildWatchActivity(GPid pid,
539 gint status,
540 gpointer user_data) {
541 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
542
543 if (!WIFEXITED(status)) {
544 LOG(ERROR) << "Child didn't exit normally";
545 lookup_data->ReportError();
546 } else if (WEXITSTATUS(status) != 0) {
547 LOG(INFO) << "Child exited with non-zero exit code "
548 << WEXITSTATUS(status);
549 lookup_data->ReportError();
550 } else {
551 lookup_data->ReportSuccess();
552 }
553 delete lookup_data;
554 }
555
556 static gboolean OnTimeout(gpointer user_data) {
557 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
558 lookup_data->ReportError();
559 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700560 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700561 }
562
563 P2PManager::LookupCallback callback_;
564 GPid pid_;
565 gint stdout_fd_;
566 guint stdout_channel_source_id_;
567 guint child_watch_source_id_;
568 guint timeout_source_id_;
569 string stdout_;
570 bool reported_;
571};
572
573void P2PManagerImpl::LookupUrlForFile(const string& file_id,
574 size_t minimum_size,
575 TimeDelta max_time_to_wait,
576 LookupCallback callback) {
577 LookupData *lookup_data = new LookupData(callback);
578 string file_id_with_ext = file_id + "." + file_extension_;
579 vector<string> args = configuration_->GetP2PClientArgs(file_id_with_ext,
580 minimum_size);
581 gchar **argv = utils::StringVectorToGStrv(args);
582 lookup_data->InitiateLookup(argv, max_time_to_wait);
583 g_strfreev(argv);
584}
585
586bool P2PManagerImpl::FileShare(const string& file_id,
587 size_t expected_size) {
588 // Check if file already exist.
Alex Deymof329b932014-10-30 01:37:48 -0700589 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700590 if (!path.empty()) {
591 // File exists - double check its expected size though.
592 ssize_t file_expected_size = FileGetExpectedSize(file_id);
593 if (file_expected_size == -1 ||
594 static_cast<size_t>(file_expected_size) != expected_size) {
595 LOG(ERROR) << "Existing p2p file " << path.value()
596 << " with expected_size=" << file_expected_size
597 << " does not match the passed in"
598 << " expected_size=" << expected_size;
599 return false;
600 }
601 return true;
602 }
603
604 // Before creating the file, bail if statvfs(3) indicates that at
605 // least twice the size is not available in P2P_DIR.
606 struct statvfs statvfsbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700607 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700608 if (statvfs(p2p_dir.value().c_str(), &statvfsbuf) != 0) {
609 PLOG(ERROR) << "Error calling statvfs() for dir " << p2p_dir.value();
610 return false;
611 }
612 size_t free_bytes =
613 static_cast<size_t>(statvfsbuf.f_bsize) * statvfsbuf.f_bavail;
614 if (free_bytes < 2 * expected_size) {
615 // This can easily happen and is worth reporting.
616 LOG(INFO) << "Refusing to allocate p2p file of " << expected_size
617 << " bytes since the directory " << p2p_dir.value()
618 << " only has " << free_bytes
619 << " bytes available and this is less than twice the"
620 << " requested size.";
621 return false;
622 }
623
624 // Okie-dokey looks like enough space is available - create the file.
625 path = GetPath(file_id, kNonVisible);
626 int fd = open(path.value().c_str(), O_CREAT | O_RDWR, 0644);
627 if (fd == -1) {
628 PLOG(ERROR) << "Error creating file with path " << path.value();
629 return false;
630 }
631 ScopedFdCloser fd_closer(&fd);
632
633 // If the final size is known, allocate the file (e.g. reserve disk
634 // space) and set the user.cros-p2p-filesize xattr.
635 if (expected_size != 0) {
636 if (fallocate(fd,
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700637 FALLOC_FL_KEEP_SIZE, // Keep file size as 0.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700638 0,
639 expected_size) != 0) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700640 if (errno == ENOSYS || errno == EOPNOTSUPP) {
641 // If the filesystem doesn't support the fallocate, keep
642 // going. This is helpful when running unit tests on build
643 // machines with ancient filesystems and/or OSes.
644 PLOG(WARNING) << "Ignoring fallocate(2) failure";
645 } else {
646 // ENOSPC can happen (funky race though, cf. the statvfs() check
647 // above), handle it gracefully, e.g. use logging level INFO.
648 PLOG(INFO) << "Error allocating " << expected_size
649 << " bytes for file " << path.value();
650 if (unlink(path.value().c_str()) != 0) {
651 PLOG(ERROR) << "Error deleting file with path " << path.value();
652 }
653 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700654 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700655 }
656
Alex Deymof329b932014-10-30 01:37:48 -0700657 string decimal_size = StringPrintf("%zu", expected_size);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700658 if (fsetxattr(fd, kCrosP2PFileSizeXAttrName,
659 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
660 PLOG(ERROR) << "Error setting xattr " << path.value();
661 return false;
662 }
663 }
664
665 return true;
666}
667
668FilePath P2PManagerImpl::FileGetPath(const string& file_id) {
669 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700670 FilePath path;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700671
672 path = GetPath(file_id, kVisible);
673 if (stat(path.value().c_str(), &statbuf) == 0) {
674 return path;
675 }
676
677 path = GetPath(file_id, kNonVisible);
678 if (stat(path.value().c_str(), &statbuf) == 0) {
679 return path;
680 }
681
682 path.clear();
683 return path;
684}
685
686bool P2PManagerImpl::FileGetVisible(const string& file_id,
687 bool *out_result) {
Alex Deymof329b932014-10-30 01:37:48 -0700688 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700689 if (path.empty()) {
690 LOG(ERROR) << "No file for id " << file_id;
691 return false;
692 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700693 if (out_result != nullptr)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700694 *out_result = path.MatchesExtension(kP2PExtension);
695 return true;
696}
697
698bool P2PManagerImpl::FileMakeVisible(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700699 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700700 if (path.empty()) {
701 LOG(ERROR) << "No file for id " << file_id;
702 return false;
703 }
704
705 // Already visible?
706 if (path.MatchesExtension(kP2PExtension))
707 return true;
708
709 LOG_ASSERT(path.MatchesExtension(kTmpExtension));
Alex Deymof329b932014-10-30 01:37:48 -0700710 FilePath new_path = path.RemoveExtension();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700711 LOG_ASSERT(new_path.MatchesExtension(kP2PExtension));
712 if (rename(path.value().c_str(), new_path.value().c_str()) != 0) {
713 PLOG(ERROR) << "Error renaming " << path.value()
714 << " to " << new_path.value();
715 return false;
716 }
717
718 return true;
719}
720
721ssize_t P2PManagerImpl::FileGetSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700722 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700723 if (path.empty())
724 return -1;
725
Gabe Blacka77939e2014-09-09 23:35:08 -0700726 return utils::FileSize(path.value());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700727}
728
729ssize_t P2PManagerImpl::FileGetExpectedSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700730 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700731 if (path.empty())
732 return -1;
733
734 char ea_value[64] = { 0 };
735 ssize_t ea_size;
736 ea_size = getxattr(path.value().c_str(), kCrosP2PFileSizeXAttrName,
737 &ea_value, sizeof(ea_value) - 1);
738 if (ea_size == -1) {
739 PLOG(ERROR) << "Error calling getxattr() on file " << path.value();
740 return -1;
741 }
742
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700743 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700744 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700745 if (*endp != '\0') {
746 LOG(ERROR) << "Error parsing the value '" << ea_value
747 << "' of the xattr " << kCrosP2PFileSizeXAttrName
748 << " as an integer";
749 return -1;
750 }
751
752 return val;
753}
754
755int P2PManagerImpl::CountSharedFiles() {
756 GDir* dir;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700757 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700758 const char* name;
759 int num_files = 0;
760
Alex Deymof329b932014-10-30 01:37:48 -0700761 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700762 dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700763 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700764 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
765 << utils::GetAndFreeGError(&error);
766 return -1;
767 }
768
769 string ext_visible = GetExt(kVisible);
770 string ext_non_visible = GetExt(kNonVisible);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700771 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700772 if (g_str_has_suffix(name, ext_visible.c_str()) ||
773 g_str_has_suffix(name, ext_non_visible.c_str())) {
774 num_files += 1;
775 }
776 }
777 g_dir_close(dir);
778
779 return num_files;
780}
781
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700782void P2PManagerImpl::ScheduleEnabledStatusChange() {
783 if (waiting_for_enabled_status_change_)
784 return;
Gilad Arnoldccd09572014-10-27 13:37:50 -0700785
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700786 Callback<void(EvalStatus, const bool&)> callback = Bind(
787 &P2PManagerImpl::OnEnabledStatusChange, base::Unretained(this));
788 update_manager_->AsyncPolicyRequest(callback, &Policy::P2PEnabledChanged,
789 is_enabled_);
790 waiting_for_enabled_status_change_ = true;
Gilad Arnoldccd09572014-10-27 13:37:50 -0700791}
792
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700793void P2PManagerImpl::OnEnabledStatusChange(EvalStatus status,
794 const bool& result) {
795 waiting_for_enabled_status_change_ = false;
796
797 if (status == EvalStatus::kSucceeded) {
798 if (result == is_enabled_) {
799 LOG(WARNING) << "P2P enabled status did not change, which means that it "
800 "is permanent; not scheduling further checks.";
801 waiting_for_enabled_status_change_ = true;
802 return;
803 }
804
805 is_enabled_ = result;
806
807 // If P2P is running but shouldn't be, make sure it isn't.
808 if (may_be_running_ && !is_enabled_ && !EnsureP2PNotRunning()) {
809 LOG(WARNING) << "Failed to stop P2P service.";
810 }
811 } else {
812 LOG(WARNING)
813 << "P2P enabled tracking failed (possibly timed out); retrying.";
814 }
815
816 ScheduleEnabledStatusChange();
817}
818
819P2PManager* P2PManager::Construct(
820 Configuration *configuration,
821 ClockInterface *clock,
822 UpdateManager* update_manager,
823 const string& file_extension,
824 const int num_files_to_keep,
825 const base::TimeDelta& max_file_age) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700826 return new P2PManagerImpl(configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500827 clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700828 update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700829 file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500830 num_files_to_keep,
831 max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700832}
833
834} // namespace chromeos_update_engine