blob: 95623e9136b081265e5cfe59ce6d5ff198b2666c [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
75 virtual ~ConfigurationImpl() {}
76
Alex Deymof329b932014-10-30 01:37:48 -070077 virtual FilePath GetP2PDir() {
78 return FilePath(kDefaultP2PDir);
David Zeuthen27a48bc2013-08-06 12:06:29 -070079 }
80
81 virtual vector<string> GetInitctlArgs(bool is_start) {
82 vector<string> args;
83 args.push_back("initctl");
84 args.push_back(is_start ? "start" : "stop");
85 args.push_back("p2p");
86 return args;
87 }
88
89 virtual vector<string> GetP2PClientArgs(const string &file_id,
90 size_t minimum_size) {
91 vector<string> args;
92 args.push_back("p2p-client");
93 args.push_back(string("--get-url=") + file_id);
Alex Deymof329b932014-10-30 01:37:48 -070094 args.push_back(StringPrintf("--minimum-size=%zu", minimum_size));
David Zeuthen27a48bc2013-08-06 12:06:29 -070095 return args;
96 }
97
Alex Vakulenkod2779df2014-06-16 13:19:00 -070098 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070099 DISALLOW_COPY_AND_ASSIGN(ConfigurationImpl);
100};
101
102// The default P2PManager implementation.
103class P2PManagerImpl : public P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700104 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700105 P2PManagerImpl(Configuration *configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500106 ClockInterface *clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700107 UpdateManager* update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700108 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500109 const int num_files_to_keep,
110 const base::TimeDelta& max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700111
112 // P2PManager methods.
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700113 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700114 virtual bool IsP2PEnabled();
115 virtual bool EnsureP2PRunning();
116 virtual bool EnsureP2PNotRunning();
117 virtual bool PerformHousekeeping();
118 virtual void LookupUrlForFile(const string& file_id,
119 size_t minimum_size,
120 TimeDelta max_time_to_wait,
121 LookupCallback callback);
122 virtual bool FileShare(const string& file_id,
123 size_t expected_size);
Alex Deymof329b932014-10-30 01:37:48 -0700124 virtual FilePath FileGetPath(const string& file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700125 virtual ssize_t FileGetSize(const string& file_id);
126 virtual ssize_t FileGetExpectedSize(const string& file_id);
127 virtual bool FileGetVisible(const string& file_id,
128 bool *out_result);
129 virtual bool FileMakeVisible(const string& file_id);
130 virtual int CountSharedFiles();
131
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700132 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700133 // Enumeration for specifying visibility.
134 enum Visibility {
135 kVisible,
136 kNonVisible
137 };
138
139 // Returns "." + |file_extension_| + ".p2p" if |visibility| is
140 // |kVisible|. Returns the same concatenated with ".tmp" otherwise.
141 string GetExt(Visibility visibility);
142
143 // Gets the on-disk path for |file_id| depending on if the file
144 // is visible or not.
Alex Deymof329b932014-10-30 01:37:48 -0700145 FilePath GetPath(const string& file_id, Visibility visibility);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700146
147 // Utility function used by EnsureP2PRunning() and EnsureP2PNotRunning().
148 bool EnsureP2P(bool should_be_running);
149
David Zeuthen41f2cf52014-11-05 12:29:45 -0500150 // Utility function to delete a file given by |path| and log the
151 // path as well as |reason|. Returns false on failure.
152 bool DeleteP2PFile(const FilePath& path, const std::string& reason);
153
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700154 // Schedules an async request for tracking changes in P2P enabled status.
155 void ScheduleEnabledStatusChange();
156
157 // An async callback used by the above.
158 void OnEnabledStatusChange(EvalStatus status, const bool& result);
159
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700160 // The device policy being used or null if no policy is being used.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700161 const policy::DevicePolicy* device_policy_ = nullptr;
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700162
David Zeuthen27a48bc2013-08-06 12:06:29 -0700163 // Configuration object.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700164 unique_ptr<Configuration> configuration_;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700165
David Zeuthen41f2cf52014-11-05 12:29:45 -0500166 // Object for telling the time.
167 ClockInterface* clock_;
168
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700169 // A pointer to the global Update Manager.
170 UpdateManager* update_manager_;
171
David Zeuthen27a48bc2013-08-06 12:06:29 -0700172 // A short string unique to the application (for example "cros_au")
173 // used to mark a file as being owned by a particular application.
174 const string file_extension_;
175
176 // If non-zero, this number denotes how many files in /var/cache/p2p
177 // owned by the application (cf. |file_extension_|) to keep after
178 // performing housekeeping.
179 const int num_files_to_keep_;
180
David Zeuthen41f2cf52014-11-05 12:29:45 -0500181 // If non-zero, files older than this will not be kept after
182 // performing housekeeping.
183 const base::TimeDelta max_file_age_;
184
David Zeuthen27a48bc2013-08-06 12:06:29 -0700185 // The string ".p2p".
186 static const char kP2PExtension[];
187
188 // The string ".tmp".
189 static const char kTmpExtension[];
190
Gilad Arnoldccd09572014-10-27 13:37:50 -0700191 // Whether P2P service may be running; initially, we assume it may be.
192 bool may_be_running_ = true;
193
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700194 // The current known enabled status of the P2P feature (initialized lazily),
195 // and whether an async status check has been scheduled.
196 bool is_enabled_;
197 bool waiting_for_enabled_status_change_ = false;
198
David Zeuthen27a48bc2013-08-06 12:06:29 -0700199 DISALLOW_COPY_AND_ASSIGN(P2PManagerImpl);
200};
201
202const char P2PManagerImpl::kP2PExtension[] = ".p2p";
203
204const char P2PManagerImpl::kTmpExtension[] = ".tmp";
205
206P2PManagerImpl::P2PManagerImpl(Configuration *configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500207 ClockInterface *clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700208 UpdateManager* update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700209 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500210 const int num_files_to_keep,
211 const base::TimeDelta& max_file_age)
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700212 : clock_(clock),
213 update_manager_(update_manager),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700214 file_extension_(file_extension),
David Zeuthen41f2cf52014-11-05 12:29:45 -0500215 num_files_to_keep_(num_files_to_keep),
216 max_file_age_(max_file_age) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700217 configuration_.reset(configuration != nullptr ? configuration :
David Zeuthen27a48bc2013-08-06 12:06:29 -0700218 new ConfigurationImpl());
219}
220
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700221void P2PManagerImpl::SetDevicePolicy(
222 const policy::DevicePolicy* device_policy) {
223 device_policy_ = device_policy;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700224}
225
226bool P2PManagerImpl::IsP2PEnabled() {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700227 if (!waiting_for_enabled_status_change_) {
228 // Get and store an initial value.
229 if (update_manager_->PolicyRequest(&Policy::P2PEnabled, &is_enabled_) ==
230 EvalStatus::kFailed) {
231 is_enabled_ = false;
232 LOG(ERROR) << "Querying P2P enabled status failed, disabling.";
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400233 }
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700234
235 // Track future changes (async).
236 ScheduleEnabledStatusChange();
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400237 }
238
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700239 return is_enabled_;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700240}
241
242bool P2PManagerImpl::EnsureP2P(bool should_be_running) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 gchar *standard_error = nullptr;
244 GError *error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700245 gint exit_status = 0;
246
Gilad Arnoldccd09572014-10-27 13:37:50 -0700247 may_be_running_ = true; // Unless successful, we must be conservative.
248
David Zeuthen27a48bc2013-08-06 12:06:29 -0700249 vector<string> args = configuration_->GetInitctlArgs(should_be_running);
Ben Chan02f7c1d2014-10-18 15:18:02 -0700250 unique_ptr<gchar*, GLibStrvFreeDeleter> argv(
David Zeuthen27a48bc2013-08-06 12:06:29 -0700251 utils::StringVectorToGStrv(args));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700252 if (!g_spawn_sync(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700253 argv.get(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700254 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700255 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700256 nullptr, nullptr, // child_setup, user_data
257 nullptr, // standard_output
David Zeuthen27a48bc2013-08-06 12:06:29 -0700258 &standard_error,
259 &exit_status,
260 &error)) {
261 LOG(ERROR) << "Error spawning " << utils::StringVectorToString(args)
262 << ": " << utils::GetAndFreeGError(&error);
263 return false;
264 }
Ben Chan02f7c1d2014-10-18 15:18:02 -0700265 unique_ptr<gchar, GLibFreeDeleter> standard_error_deleter(standard_error);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700266
267 if (!WIFEXITED(exit_status)) {
268 LOG(ERROR) << "Error spawning '" << utils::StringVectorToString(args)
269 << "': WIFEXITED is false";
270 return false;
271 }
272
Gilad Arnoldccd09572014-10-27 13:37:50 -0700273 // If initctl(8) does not exit normally (exit status other than zero), ensure
274 // that the error message is not benign by scanning stderr; this is a
275 // necessity because initctl does not offer actions such as "start if not
276 // running" or "stop if running".
David Zeuthen27a48bc2013-08-06 12:06:29 -0700277 // TODO(zeuthen,chromium:277051): Avoid doing this.
Gilad Arnoldccd09572014-10-27 13:37:50 -0700278 if (WEXITSTATUS(exit_status) != 0) {
279 const gchar *expected_error_message = should_be_running ?
280 "initctl: Job is already running: p2p\n" :
281 "initctl: Unknown instance \n";
282 if (g_strcmp0(standard_error, expected_error_message) != 0)
283 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700284 }
285
Gilad Arnoldccd09572014-10-27 13:37:50 -0700286 may_be_running_ = should_be_running; // Successful after all.
287 return true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700288}
289
290bool P2PManagerImpl::EnsureP2PRunning() {
291 return EnsureP2P(true);
292}
293
294bool P2PManagerImpl::EnsureP2PNotRunning() {
295 return EnsureP2P(false);
296}
297
298// Returns True if the timestamp in the first pair is greater than the
299// timestamp in the latter. If used with std::sort() this will yield a
300// sequence of elements where newer (high timestamps) elements precede
301// older ones (low timestamps).
302static bool MatchCompareFunc(const pair<FilePath, Time>& a,
303 const pair<FilePath, Time>& b) {
304 return a.second > b.second;
305}
306
307string P2PManagerImpl::GetExt(Visibility visibility) {
308 string ext = string(".") + file_extension_ + kP2PExtension;
309 switch (visibility) {
310 case kVisible:
311 break;
312 case kNonVisible:
313 ext += kTmpExtension;
314 break;
315 // Don't add a default case to let the compiler warn about newly
316 // added enum values.
317 }
318 return ext;
319}
320
321FilePath P2PManagerImpl::GetPath(const string& file_id, Visibility visibility) {
322 return configuration_->GetP2PDir().Append(file_id + GetExt(visibility));
323}
324
David Zeuthen41f2cf52014-11-05 12:29:45 -0500325bool P2PManagerImpl::DeleteP2PFile(const FilePath& path,
326 const std::string& reason) {
327 LOG(INFO) << "Deleting p2p file " << path.value()
328 << " (reason: " << reason << ")";
329 if (unlink(path.value().c_str()) != 0) {
330 PLOG(ERROR) << "Error deleting p2p file " << path.value();
331 return false;
332 }
333 return true;
334}
David Zeuthen27a48bc2013-08-06 12:06:29 -0700335
David Zeuthen41f2cf52014-11-05 12:29:45 -0500336
337bool P2PManagerImpl::PerformHousekeeping() {
338 // Open p2p dir.
Alex Deymof329b932014-10-30 01:37:48 -0700339 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500340 GError* error = nullptr;
341 GDir* dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700342 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700343 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
344 << utils::GetAndFreeGError(&error);
345 return false;
346 }
347
David Zeuthen41f2cf52014-11-05 12:29:45 -0500348 // Go through all files and collect their mtime.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700349 string ext_visible = GetExt(kVisible);
350 string ext_non_visible = GetExt(kNonVisible);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500351 bool deletion_failed = false;
352 const char* name = nullptr;
353 vector<pair<FilePath, Time>> matches;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700354 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700355 if (!(g_str_has_suffix(name, ext_visible.c_str()) ||
356 g_str_has_suffix(name, ext_non_visible.c_str())))
357 continue;
358
359 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700360 FilePath file = p2p_dir.Append(name);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700361 if (stat(file.value().c_str(), &statbuf) != 0) {
362 PLOG(ERROR) << "Error getting file status for " << file.value();
363 continue;
364 }
365
David Zeuthen41f2cf52014-11-05 12:29:45 -0500366 Time time = utils::TimeFromStructTimespec(&statbuf.st_mtim);
367
368 // If instructed to keep only files younger than a given age
369 // (|max_file_age_| != 0), delete files satisfying this criteria
370 // right now. Otherwise add it to a list we'll consider for later.
371 if (clock_ != nullptr && max_file_age_ != base::TimeDelta() &&
372 clock_->GetWallclockTime() - time > max_file_age_) {
373 if (!DeleteP2PFile(file, "file too old"))
374 deletion_failed = true;
375 } else {
376 matches.push_back(std::make_pair(file, time));
377 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700378 }
379 g_dir_close(dir);
380
David Zeuthen41f2cf52014-11-05 12:29:45 -0500381 // If instructed to only keep N files (|max_files_to_keep_ != 0),
382 // sort list of matches, newest (biggest time) to oldest (lowest
383 // time). Then delete starting at element |num_files_to_keep_|.
384 if (num_files_to_keep_ > 0) {
385 std::sort(matches.begin(), matches.end(), MatchCompareFunc);
386 vector<pair<FilePath, Time>>::const_iterator i;
387 for (i = matches.begin() + num_files_to_keep_; i < matches.end(); ++i) {
388 if (!DeleteP2PFile(i->first, "too many files"))
389 deletion_failed = true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700390 }
391 }
392
David Zeuthen41f2cf52014-11-05 12:29:45 -0500393 return !deletion_failed;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700394}
395
396// Helper class for implementing LookupUrlForFile().
397class LookupData {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700398 public:
399 explicit LookupData(P2PManager::LookupCallback callback)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700400 : callback_(callback),
401 pid_(0),
402 stdout_fd_(-1),
403 stdout_channel_source_id_(0),
404 child_watch_source_id_(0),
405 timeout_source_id_(0),
406 reported_(false) {}
407
408 ~LookupData() {
409 if (child_watch_source_id_ != 0)
410 g_source_remove(child_watch_source_id_);
411 if (stdout_channel_source_id_ != 0)
412 g_source_remove(stdout_channel_source_id_);
413 if (timeout_source_id_ != 0)
414 g_source_remove(timeout_source_id_);
415 if (stdout_fd_ != -1)
416 close(stdout_fd_);
417 if (pid_ != 0)
418 kill(pid_, SIGTERM);
419 }
420
421 void InitiateLookup(gchar **argv, TimeDelta timeout) {
422 // NOTE: if we fail early (i.e. in this method), we need to schedule
423 // an idle to report the error. This is because we guarantee that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700424 // the callback is always called from the GLib mainloop (this
David Zeuthen27a48bc2013-08-06 12:06:29 -0700425 // guarantee is useful for testing).
426
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700427 GError *error = nullptr;
428 if (!g_spawn_async_with_pipes(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700429 argv,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700430 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700431 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
432 G_SPAWN_DO_NOT_REAP_CHILD),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700433 nullptr, // child_setup
David Zeuthen27a48bc2013-08-06 12:06:29 -0700434 this,
435 &pid_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700436 nullptr, // standard_input
David Zeuthen27a48bc2013-08-06 12:06:29 -0700437 &stdout_fd_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700438 nullptr, // standard_error
David Zeuthen27a48bc2013-08-06 12:06:29 -0700439 &error)) {
440 LOG(ERROR) << "Error spawning p2p-client: "
441 << utils::GetAndFreeGError(&error);
442 ReportErrorAndDeleteInIdle();
443 return;
444 }
445
446 GIOChannel* io_channel = g_io_channel_unix_new(stdout_fd_);
447 stdout_channel_source_id_ = g_io_add_watch(
448 io_channel,
449 static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),
450 OnIOChannelActivity, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700451 CHECK_NE(stdout_channel_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452 g_io_channel_unref(io_channel);
453
454 child_watch_source_id_ = g_child_watch_add(pid_, OnChildWatchActivity,
455 this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700456 CHECK_NE(child_watch_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700457
458 if (timeout.ToInternalValue() > 0) {
459 timeout_source_id_ = g_timeout_add(timeout.InMilliseconds(),
460 OnTimeout, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700461 CHECK_NE(timeout_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700462 }
463 }
464
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700465 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700466 void ReportErrorAndDeleteInIdle() {
467 g_idle_add(static_cast<GSourceFunc>(OnIdleForReportErrorAndDelete), this);
468 }
469
470 static gboolean OnIdleForReportErrorAndDelete(gpointer user_data) {
471 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
472 lookup_data->ReportError();
473 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700474 return FALSE; // Remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700475 }
476
477 void IssueCallback(const string& url) {
478 if (!callback_.is_null())
479 callback_.Run(url);
480 }
481
482 void ReportError() {
483 if (reported_)
484 return;
485 IssueCallback("");
486 reported_ = true;
487 }
488
489 void ReportSuccess() {
490 if (reported_)
491 return;
492
493 string url = stdout_;
494 size_t newline_pos = url.find('\n');
495 if (newline_pos != string::npos)
496 url.resize(newline_pos);
497
498 // Since p2p-client(1) is constructing this URL itself strictly
499 // speaking there's no need to validate it... but, anyway, can't
500 // hurt.
501 if (url.compare(0, 7, "http://") == 0) {
502 IssueCallback(url);
503 } else {
504 LOG(ERROR) << "p2p URL '" << url << "' does not look right. Ignoring.";
505 ReportError();
506 }
507
508 reported_ = true;
509 }
510
511 static gboolean OnIOChannelActivity(GIOChannel *source,
512 GIOCondition condition,
513 gpointer user_data) {
514 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700515 gchar* str = nullptr;
516 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700517 GIOStatus status = g_io_channel_read_line(source,
518 &str,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700519 nullptr, // len
520 nullptr, // line_terminator
David Zeuthen27a48bc2013-08-06 12:06:29 -0700521 &error);
522 if (status != G_IO_STATUS_NORMAL) {
523 // Ignore EOF since we usually get that before SIGCHLD and we
524 // need to examine exit status there.
525 if (status != G_IO_STATUS_EOF) {
526 LOG(ERROR) << "Error reading a line from p2p-client: "
527 << utils::GetAndFreeGError(&error);
528 lookup_data->ReportError();
529 delete lookup_data;
530 }
531 } else {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700532 if (str != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700533 lookup_data->stdout_ += str;
534 g_free(str);
535 }
536 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700537 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700538 }
539
540 static void OnChildWatchActivity(GPid pid,
541 gint status,
542 gpointer user_data) {
543 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
544
545 if (!WIFEXITED(status)) {
546 LOG(ERROR) << "Child didn't exit normally";
547 lookup_data->ReportError();
548 } else if (WEXITSTATUS(status) != 0) {
549 LOG(INFO) << "Child exited with non-zero exit code "
550 << WEXITSTATUS(status);
551 lookup_data->ReportError();
552 } else {
553 lookup_data->ReportSuccess();
554 }
555 delete lookup_data;
556 }
557
558 static gboolean OnTimeout(gpointer user_data) {
559 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
560 lookup_data->ReportError();
561 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700562 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700563 }
564
565 P2PManager::LookupCallback callback_;
566 GPid pid_;
567 gint stdout_fd_;
568 guint stdout_channel_source_id_;
569 guint child_watch_source_id_;
570 guint timeout_source_id_;
571 string stdout_;
572 bool reported_;
573};
574
575void P2PManagerImpl::LookupUrlForFile(const string& file_id,
576 size_t minimum_size,
577 TimeDelta max_time_to_wait,
578 LookupCallback callback) {
579 LookupData *lookup_data = new LookupData(callback);
580 string file_id_with_ext = file_id + "." + file_extension_;
581 vector<string> args = configuration_->GetP2PClientArgs(file_id_with_ext,
582 minimum_size);
583 gchar **argv = utils::StringVectorToGStrv(args);
584 lookup_data->InitiateLookup(argv, max_time_to_wait);
585 g_strfreev(argv);
586}
587
588bool P2PManagerImpl::FileShare(const string& file_id,
589 size_t expected_size) {
590 // Check if file already exist.
Alex Deymof329b932014-10-30 01:37:48 -0700591 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700592 if (!path.empty()) {
593 // File exists - double check its expected size though.
594 ssize_t file_expected_size = FileGetExpectedSize(file_id);
595 if (file_expected_size == -1 ||
596 static_cast<size_t>(file_expected_size) != expected_size) {
597 LOG(ERROR) << "Existing p2p file " << path.value()
598 << " with expected_size=" << file_expected_size
599 << " does not match the passed in"
600 << " expected_size=" << expected_size;
601 return false;
602 }
603 return true;
604 }
605
606 // Before creating the file, bail if statvfs(3) indicates that at
607 // least twice the size is not available in P2P_DIR.
608 struct statvfs statvfsbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700609 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700610 if (statvfs(p2p_dir.value().c_str(), &statvfsbuf) != 0) {
611 PLOG(ERROR) << "Error calling statvfs() for dir " << p2p_dir.value();
612 return false;
613 }
614 size_t free_bytes =
615 static_cast<size_t>(statvfsbuf.f_bsize) * statvfsbuf.f_bavail;
616 if (free_bytes < 2 * expected_size) {
617 // This can easily happen and is worth reporting.
618 LOG(INFO) << "Refusing to allocate p2p file of " << expected_size
619 << " bytes since the directory " << p2p_dir.value()
620 << " only has " << free_bytes
621 << " bytes available and this is less than twice the"
622 << " requested size.";
623 return false;
624 }
625
626 // Okie-dokey looks like enough space is available - create the file.
627 path = GetPath(file_id, kNonVisible);
628 int fd = open(path.value().c_str(), O_CREAT | O_RDWR, 0644);
629 if (fd == -1) {
630 PLOG(ERROR) << "Error creating file with path " << path.value();
631 return false;
632 }
633 ScopedFdCloser fd_closer(&fd);
634
635 // If the final size is known, allocate the file (e.g. reserve disk
636 // space) and set the user.cros-p2p-filesize xattr.
637 if (expected_size != 0) {
638 if (fallocate(fd,
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700639 FALLOC_FL_KEEP_SIZE, // Keep file size as 0.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700640 0,
641 expected_size) != 0) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700642 if (errno == ENOSYS || errno == EOPNOTSUPP) {
643 // If the filesystem doesn't support the fallocate, keep
644 // going. This is helpful when running unit tests on build
645 // machines with ancient filesystems and/or OSes.
646 PLOG(WARNING) << "Ignoring fallocate(2) failure";
647 } else {
648 // ENOSPC can happen (funky race though, cf. the statvfs() check
649 // above), handle it gracefully, e.g. use logging level INFO.
650 PLOG(INFO) << "Error allocating " << expected_size
651 << " bytes for file " << path.value();
652 if (unlink(path.value().c_str()) != 0) {
653 PLOG(ERROR) << "Error deleting file with path " << path.value();
654 }
655 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700656 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700657 }
658
Alex Deymof329b932014-10-30 01:37:48 -0700659 string decimal_size = StringPrintf("%zu", expected_size);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700660 if (fsetxattr(fd, kCrosP2PFileSizeXAttrName,
661 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
662 PLOG(ERROR) << "Error setting xattr " << path.value();
663 return false;
664 }
665 }
666
667 return true;
668}
669
670FilePath P2PManagerImpl::FileGetPath(const string& file_id) {
671 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700672 FilePath path;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700673
674 path = GetPath(file_id, kVisible);
675 if (stat(path.value().c_str(), &statbuf) == 0) {
676 return path;
677 }
678
679 path = GetPath(file_id, kNonVisible);
680 if (stat(path.value().c_str(), &statbuf) == 0) {
681 return path;
682 }
683
684 path.clear();
685 return path;
686}
687
688bool P2PManagerImpl::FileGetVisible(const string& file_id,
689 bool *out_result) {
Alex Deymof329b932014-10-30 01:37:48 -0700690 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700691 if (path.empty()) {
692 LOG(ERROR) << "No file for id " << file_id;
693 return false;
694 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700695 if (out_result != nullptr)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700696 *out_result = path.MatchesExtension(kP2PExtension);
697 return true;
698}
699
700bool P2PManagerImpl::FileMakeVisible(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700701 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700702 if (path.empty()) {
703 LOG(ERROR) << "No file for id " << file_id;
704 return false;
705 }
706
707 // Already visible?
708 if (path.MatchesExtension(kP2PExtension))
709 return true;
710
711 LOG_ASSERT(path.MatchesExtension(kTmpExtension));
Alex Deymof329b932014-10-30 01:37:48 -0700712 FilePath new_path = path.RemoveExtension();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700713 LOG_ASSERT(new_path.MatchesExtension(kP2PExtension));
714 if (rename(path.value().c_str(), new_path.value().c_str()) != 0) {
715 PLOG(ERROR) << "Error renaming " << path.value()
716 << " to " << new_path.value();
717 return false;
718 }
719
720 return true;
721}
722
723ssize_t P2PManagerImpl::FileGetSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700724 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700725 if (path.empty())
726 return -1;
727
Gabe Blacka77939e2014-09-09 23:35:08 -0700728 return utils::FileSize(path.value());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700729}
730
731ssize_t P2PManagerImpl::FileGetExpectedSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700732 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700733 if (path.empty())
734 return -1;
735
736 char ea_value[64] = { 0 };
737 ssize_t ea_size;
738 ea_size = getxattr(path.value().c_str(), kCrosP2PFileSizeXAttrName,
739 &ea_value, sizeof(ea_value) - 1);
740 if (ea_size == -1) {
741 PLOG(ERROR) << "Error calling getxattr() on file " << path.value();
742 return -1;
743 }
744
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700745 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700746 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700747 if (*endp != '\0') {
748 LOG(ERROR) << "Error parsing the value '" << ea_value
749 << "' of the xattr " << kCrosP2PFileSizeXAttrName
750 << " as an integer";
751 return -1;
752 }
753
754 return val;
755}
756
757int P2PManagerImpl::CountSharedFiles() {
758 GDir* dir;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700759 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700760 const char* name;
761 int num_files = 0;
762
Alex Deymof329b932014-10-30 01:37:48 -0700763 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700764 dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700765 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700766 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
767 << utils::GetAndFreeGError(&error);
768 return -1;
769 }
770
771 string ext_visible = GetExt(kVisible);
772 string ext_non_visible = GetExt(kNonVisible);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700773 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700774 if (g_str_has_suffix(name, ext_visible.c_str()) ||
775 g_str_has_suffix(name, ext_non_visible.c_str())) {
776 num_files += 1;
777 }
778 }
779 g_dir_close(dir);
780
781 return num_files;
782}
783
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700784void P2PManagerImpl::ScheduleEnabledStatusChange() {
785 if (waiting_for_enabled_status_change_)
786 return;
Gilad Arnoldccd09572014-10-27 13:37:50 -0700787
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700788 Callback<void(EvalStatus, const bool&)> callback = Bind(
789 &P2PManagerImpl::OnEnabledStatusChange, base::Unretained(this));
790 update_manager_->AsyncPolicyRequest(callback, &Policy::P2PEnabledChanged,
791 is_enabled_);
792 waiting_for_enabled_status_change_ = true;
Gilad Arnoldccd09572014-10-27 13:37:50 -0700793}
794
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700795void P2PManagerImpl::OnEnabledStatusChange(EvalStatus status,
796 const bool& result) {
797 waiting_for_enabled_status_change_ = false;
798
799 if (status == EvalStatus::kSucceeded) {
800 if (result == is_enabled_) {
801 LOG(WARNING) << "P2P enabled status did not change, which means that it "
802 "is permanent; not scheduling further checks.";
803 waiting_for_enabled_status_change_ = true;
804 return;
805 }
806
807 is_enabled_ = result;
808
809 // If P2P is running but shouldn't be, make sure it isn't.
810 if (may_be_running_ && !is_enabled_ && !EnsureP2PNotRunning()) {
811 LOG(WARNING) << "Failed to stop P2P service.";
812 }
813 } else {
814 LOG(WARNING)
815 << "P2P enabled tracking failed (possibly timed out); retrying.";
816 }
817
818 ScheduleEnabledStatusChange();
819}
820
821P2PManager* P2PManager::Construct(
822 Configuration *configuration,
823 ClockInterface *clock,
824 UpdateManager* update_manager,
825 const string& file_extension,
826 const int num_files_to_keep,
827 const base::TimeDelta& max_file_age) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700828 return new P2PManagerImpl(configuration,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500829 clock,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700830 update_manager,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700831 file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500832 num_files_to_keep,
833 max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700834}
835
836} // namespace chromeos_update_engine