blob: abfb3d7deaee433b2985f2634b49cd5296675275 [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
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/files/file_path.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070033#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070034#include <base/strings/stringprintf.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070035
Alex Deymo44666f92014-07-22 20:29:24 -070036#include "update_engine/glib_utils.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070037#include "update_engine/utils.h"
38
39using base::FilePath;
40using base::StringPrintf;
41using base::Time;
42using base::TimeDelta;
43using std::map;
44using std::pair;
45using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070046using std::unique_ptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -070047using std::vector;
48
49namespace chromeos_update_engine {
50
51namespace {
52
53// The default p2p directory.
54const char kDefaultP2PDir[] = "/var/cache/p2p";
55
56// The p2p xattr used for conveying the final size of a file - see the
57// p2p ddoc for details.
58const char kCrosP2PFileSizeXAttrName[] = "user.cros-p2p-filesize";
59
Alex Vakulenkod2779df2014-06-16 13:19:00 -070060} // namespace
David Zeuthen27a48bc2013-08-06 12:06:29 -070061
62// The default P2PManager::Configuration implementation.
63class ConfigurationImpl : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070064 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070065 ConfigurationImpl() {}
66
67 virtual ~ConfigurationImpl() {}
68
Alex Deymof329b932014-10-30 01:37:48 -070069 virtual FilePath GetP2PDir() {
70 return FilePath(kDefaultP2PDir);
David Zeuthen27a48bc2013-08-06 12:06:29 -070071 }
72
73 virtual vector<string> GetInitctlArgs(bool is_start) {
74 vector<string> args;
75 args.push_back("initctl");
76 args.push_back(is_start ? "start" : "stop");
77 args.push_back("p2p");
78 return args;
79 }
80
81 virtual vector<string> GetP2PClientArgs(const string &file_id,
82 size_t minimum_size) {
83 vector<string> args;
84 args.push_back("p2p-client");
85 args.push_back(string("--get-url=") + file_id);
Alex Deymof329b932014-10-30 01:37:48 -070086 args.push_back(StringPrintf("--minimum-size=%zu", minimum_size));
David Zeuthen27a48bc2013-08-06 12:06:29 -070087 return args;
88 }
89
Alex Vakulenkod2779df2014-06-16 13:19:00 -070090 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070091 DISALLOW_COPY_AND_ASSIGN(ConfigurationImpl);
92};
93
94// The default P2PManager implementation.
95class P2PManagerImpl : public P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070096 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070097 P2PManagerImpl(Configuration *configuration,
98 PrefsInterface *prefs,
David Zeuthen41f2cf52014-11-05 12:29:45 -050099 ClockInterface *clock,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700100 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500101 const int num_files_to_keep,
102 const base::TimeDelta& max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700103
104 // P2PManager methods.
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700105 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700106 virtual bool IsP2PEnabled();
107 virtual bool EnsureP2PRunning();
108 virtual bool EnsureP2PNotRunning();
109 virtual bool PerformHousekeeping();
110 virtual void LookupUrlForFile(const string& file_id,
111 size_t minimum_size,
112 TimeDelta max_time_to_wait,
113 LookupCallback callback);
114 virtual bool FileShare(const string& file_id,
115 size_t expected_size);
Alex Deymof329b932014-10-30 01:37:48 -0700116 virtual FilePath FileGetPath(const string& file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700117 virtual ssize_t FileGetSize(const string& file_id);
118 virtual ssize_t FileGetExpectedSize(const string& file_id);
119 virtual bool FileGetVisible(const string& file_id,
120 bool *out_result);
121 virtual bool FileMakeVisible(const string& file_id);
122 virtual int CountSharedFiles();
Gilad Arnoldccd09572014-10-27 13:37:50 -0700123 bool SetP2PEnabledPref(bool enabled) override;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700124
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700125 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700126 // Enumeration for specifying visibility.
127 enum Visibility {
128 kVisible,
129 kNonVisible
130 };
131
132 // Returns "." + |file_extension_| + ".p2p" if |visibility| is
133 // |kVisible|. Returns the same concatenated with ".tmp" otherwise.
134 string GetExt(Visibility visibility);
135
136 // Gets the on-disk path for |file_id| depending on if the file
137 // is visible or not.
Alex Deymof329b932014-10-30 01:37:48 -0700138 FilePath GetPath(const string& file_id, Visibility visibility);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700139
140 // Utility function used by EnsureP2PRunning() and EnsureP2PNotRunning().
141 bool EnsureP2P(bool should_be_running);
142
David Zeuthen41f2cf52014-11-05 12:29:45 -0500143 // Utility function to delete a file given by |path| and log the
144 // path as well as |reason|. Returns false on failure.
145 bool DeleteP2PFile(const FilePath& path, const std::string& reason);
146
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700147 // The device policy being used or null if no policy is being used.
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700148 const policy::DevicePolicy* device_policy_;
149
David Zeuthen27a48bc2013-08-06 12:06:29 -0700150 // Configuration object.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700151 unique_ptr<Configuration> configuration_;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700152
153 // Object for persisted state.
154 PrefsInterface* prefs_;
155
David Zeuthen41f2cf52014-11-05 12:29:45 -0500156 // Object for telling the time.
157 ClockInterface* clock_;
158
David Zeuthen27a48bc2013-08-06 12:06:29 -0700159 // A short string unique to the application (for example "cros_au")
160 // used to mark a file as being owned by a particular application.
161 const string file_extension_;
162
163 // If non-zero, this number denotes how many files in /var/cache/p2p
164 // owned by the application (cf. |file_extension_|) to keep after
165 // performing housekeeping.
166 const int num_files_to_keep_;
167
David Zeuthen41f2cf52014-11-05 12:29:45 -0500168 // If non-zero, files older than this will not be kept after
169 // performing housekeeping.
170 const base::TimeDelta max_file_age_;
171
David Zeuthen27a48bc2013-08-06 12:06:29 -0700172 // The string ".p2p".
173 static const char kP2PExtension[];
174
175 // The string ".tmp".
176 static const char kTmpExtension[];
177
Gilad Arnoldccd09572014-10-27 13:37:50 -0700178 // Whether P2P service may be running; initially, we assume it may be.
179 bool may_be_running_ = true;
180
David Zeuthen27a48bc2013-08-06 12:06:29 -0700181 DISALLOW_COPY_AND_ASSIGN(P2PManagerImpl);
182};
183
184const char P2PManagerImpl::kP2PExtension[] = ".p2p";
185
186const char P2PManagerImpl::kTmpExtension[] = ".tmp";
187
188P2PManagerImpl::P2PManagerImpl(Configuration *configuration,
189 PrefsInterface *prefs,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500190 ClockInterface *clock,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700191 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500192 const int num_files_to_keep,
193 const base::TimeDelta& max_file_age)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700194 : device_policy_(nullptr),
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700195 prefs_(prefs),
David Zeuthen41f2cf52014-11-05 12:29:45 -0500196 clock_(clock),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700197 file_extension_(file_extension),
David Zeuthen41f2cf52014-11-05 12:29:45 -0500198 num_files_to_keep_(num_files_to_keep),
199 max_file_age_(max_file_age) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700200 configuration_.reset(configuration != nullptr ? configuration :
David Zeuthen27a48bc2013-08-06 12:06:29 -0700201 new ConfigurationImpl());
202}
203
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700204void P2PManagerImpl::SetDevicePolicy(
205 const policy::DevicePolicy* device_policy) {
206 device_policy_ = device_policy;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700207}
208
209bool P2PManagerImpl::IsP2PEnabled() {
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700210 bool p2p_enabled = false;
211
212 // The logic we want here is additive, e.g. p2p can be enabled by
213 // either the crosh flag OR by Enterprise Policy, e.g. the following
214 // truth table:
215 //
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400216 // crosh_flag == FALSE && enterprise_policy == unset -> use_p2p == *
217 // crosh_flag == TRUE && enterprise_policy == unset -> use_p2p == TRUE
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700218 // crosh_flag == FALSE && enterprise_policy == FALSE -> use_p2p == FALSE
219 // crosh_flag == FALSE && enterprise_policy == TRUE -> use_p2p == TRUE
220 // crosh_flag == TRUE && enterprise_policy == FALSE -> use_p2p == TRUE
221 // crosh_flag == TRUE && enterprise_policy == TRUE -> use_p2p == TRUE
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400222 //
223 // *: TRUE if Enterprise Enrolled, FALSE otherwise.
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700224
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700225 if (prefs_ != nullptr &&
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700226 prefs_->Exists(kPrefsP2PEnabled) &&
227 prefs_->GetBoolean(kPrefsP2PEnabled, &p2p_enabled) &&
228 p2p_enabled) {
229 LOG(INFO) << "The crosh flag indicates that p2p is enabled.";
230 return true;
231 }
232
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400233 if (device_policy_ != nullptr) {
234 if (device_policy_->GetAuP2PEnabled(&p2p_enabled)) {
235 if (p2p_enabled) {
236 LOG(INFO) << "Enterprise Policy indicates that p2p is enabled.";
237 return true;
238 }
239 } else {
240 // Enterprise-enrolled devices have an empty owner in their device policy.
241 string owner;
242 if (!device_policy_->GetOwner(&owner) || owner.empty()) {
243 LOG(INFO) << "No p2p_enabled setting in Enterprise Policy but device "
244 << "is Enterprise Enrolled so allowing p2p.";
245 return true;
246 }
247 }
248 }
249
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700250 LOG(INFO) << "Neither Enterprise Policy nor crosh flag indicates that p2p "
251 << "is enabled.";
252 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700253}
254
255bool P2PManagerImpl::EnsureP2P(bool should_be_running) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700256 gchar *standard_error = nullptr;
257 GError *error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700258 gint exit_status = 0;
259
Gilad Arnoldccd09572014-10-27 13:37:50 -0700260 may_be_running_ = true; // Unless successful, we must be conservative.
261
David Zeuthen27a48bc2013-08-06 12:06:29 -0700262 vector<string> args = configuration_->GetInitctlArgs(should_be_running);
Ben Chan02f7c1d2014-10-18 15:18:02 -0700263 unique_ptr<gchar*, GLibStrvFreeDeleter> argv(
David Zeuthen27a48bc2013-08-06 12:06:29 -0700264 utils::StringVectorToGStrv(args));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700265 if (!g_spawn_sync(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700266 argv.get(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700267 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700268 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700269 nullptr, nullptr, // child_setup, user_data
270 nullptr, // standard_output
David Zeuthen27a48bc2013-08-06 12:06:29 -0700271 &standard_error,
272 &exit_status,
273 &error)) {
274 LOG(ERROR) << "Error spawning " << utils::StringVectorToString(args)
275 << ": " << utils::GetAndFreeGError(&error);
276 return false;
277 }
Ben Chan02f7c1d2014-10-18 15:18:02 -0700278 unique_ptr<gchar, GLibFreeDeleter> standard_error_deleter(standard_error);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700279
280 if (!WIFEXITED(exit_status)) {
281 LOG(ERROR) << "Error spawning '" << utils::StringVectorToString(args)
282 << "': WIFEXITED is false";
283 return false;
284 }
285
Gilad Arnoldccd09572014-10-27 13:37:50 -0700286 // If initctl(8) does not exit normally (exit status other than zero), ensure
287 // that the error message is not benign by scanning stderr; this is a
288 // necessity because initctl does not offer actions such as "start if not
289 // running" or "stop if running".
David Zeuthen27a48bc2013-08-06 12:06:29 -0700290 // TODO(zeuthen,chromium:277051): Avoid doing this.
Gilad Arnoldccd09572014-10-27 13:37:50 -0700291 if (WEXITSTATUS(exit_status) != 0) {
292 const gchar *expected_error_message = should_be_running ?
293 "initctl: Job is already running: p2p\n" :
294 "initctl: Unknown instance \n";
295 if (g_strcmp0(standard_error, expected_error_message) != 0)
296 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700297 }
298
Gilad Arnoldccd09572014-10-27 13:37:50 -0700299 may_be_running_ = should_be_running; // Successful after all.
300 return true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700301}
302
303bool P2PManagerImpl::EnsureP2PRunning() {
304 return EnsureP2P(true);
305}
306
307bool P2PManagerImpl::EnsureP2PNotRunning() {
308 return EnsureP2P(false);
309}
310
311// Returns True if the timestamp in the first pair is greater than the
312// timestamp in the latter. If used with std::sort() this will yield a
313// sequence of elements where newer (high timestamps) elements precede
314// older ones (low timestamps).
315static bool MatchCompareFunc(const pair<FilePath, Time>& a,
316 const pair<FilePath, Time>& b) {
317 return a.second > b.second;
318}
319
320string P2PManagerImpl::GetExt(Visibility visibility) {
321 string ext = string(".") + file_extension_ + kP2PExtension;
322 switch (visibility) {
323 case kVisible:
324 break;
325 case kNonVisible:
326 ext += kTmpExtension;
327 break;
328 // Don't add a default case to let the compiler warn about newly
329 // added enum values.
330 }
331 return ext;
332}
333
334FilePath P2PManagerImpl::GetPath(const string& file_id, Visibility visibility) {
335 return configuration_->GetP2PDir().Append(file_id + GetExt(visibility));
336}
337
David Zeuthen41f2cf52014-11-05 12:29:45 -0500338bool P2PManagerImpl::DeleteP2PFile(const FilePath& path,
339 const std::string& reason) {
340 LOG(INFO) << "Deleting p2p file " << path.value()
341 << " (reason: " << reason << ")";
342 if (unlink(path.value().c_str()) != 0) {
343 PLOG(ERROR) << "Error deleting p2p file " << path.value();
344 return false;
345 }
346 return true;
347}
David Zeuthen27a48bc2013-08-06 12:06:29 -0700348
David Zeuthen41f2cf52014-11-05 12:29:45 -0500349
350bool P2PManagerImpl::PerformHousekeeping() {
351 // Open p2p dir.
Alex Deymof329b932014-10-30 01:37:48 -0700352 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500353 GError* error = nullptr;
354 GDir* dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700355 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700356 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
357 << utils::GetAndFreeGError(&error);
358 return false;
359 }
360
David Zeuthen41f2cf52014-11-05 12:29:45 -0500361 // Go through all files and collect their mtime.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700362 string ext_visible = GetExt(kVisible);
363 string ext_non_visible = GetExt(kNonVisible);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500364 bool deletion_failed = false;
365 const char* name = nullptr;
366 vector<pair<FilePath, Time>> matches;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700367 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700368 if (!(g_str_has_suffix(name, ext_visible.c_str()) ||
369 g_str_has_suffix(name, ext_non_visible.c_str())))
370 continue;
371
372 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700373 FilePath file = p2p_dir.Append(name);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700374 if (stat(file.value().c_str(), &statbuf) != 0) {
375 PLOG(ERROR) << "Error getting file status for " << file.value();
376 continue;
377 }
378
David Zeuthen41f2cf52014-11-05 12:29:45 -0500379 Time time = utils::TimeFromStructTimespec(&statbuf.st_mtim);
380
381 // If instructed to keep only files younger than a given age
382 // (|max_file_age_| != 0), delete files satisfying this criteria
383 // right now. Otherwise add it to a list we'll consider for later.
384 if (clock_ != nullptr && max_file_age_ != base::TimeDelta() &&
385 clock_->GetWallclockTime() - time > max_file_age_) {
386 if (!DeleteP2PFile(file, "file too old"))
387 deletion_failed = true;
388 } else {
389 matches.push_back(std::make_pair(file, time));
390 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700391 }
392 g_dir_close(dir);
393
David Zeuthen41f2cf52014-11-05 12:29:45 -0500394 // If instructed to only keep N files (|max_files_to_keep_ != 0),
395 // sort list of matches, newest (biggest time) to oldest (lowest
396 // time). Then delete starting at element |num_files_to_keep_|.
397 if (num_files_to_keep_ > 0) {
398 std::sort(matches.begin(), matches.end(), MatchCompareFunc);
399 vector<pair<FilePath, Time>>::const_iterator i;
400 for (i = matches.begin() + num_files_to_keep_; i < matches.end(); ++i) {
401 if (!DeleteP2PFile(i->first, "too many files"))
402 deletion_failed = true;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700403 }
404 }
405
David Zeuthen41f2cf52014-11-05 12:29:45 -0500406 return !deletion_failed;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700407}
408
409// Helper class for implementing LookupUrlForFile().
410class LookupData {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700411 public:
412 explicit LookupData(P2PManager::LookupCallback callback)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700413 : callback_(callback),
414 pid_(0),
415 stdout_fd_(-1),
416 stdout_channel_source_id_(0),
417 child_watch_source_id_(0),
418 timeout_source_id_(0),
419 reported_(false) {}
420
421 ~LookupData() {
422 if (child_watch_source_id_ != 0)
423 g_source_remove(child_watch_source_id_);
424 if (stdout_channel_source_id_ != 0)
425 g_source_remove(stdout_channel_source_id_);
426 if (timeout_source_id_ != 0)
427 g_source_remove(timeout_source_id_);
428 if (stdout_fd_ != -1)
429 close(stdout_fd_);
430 if (pid_ != 0)
431 kill(pid_, SIGTERM);
432 }
433
434 void InitiateLookup(gchar **argv, TimeDelta timeout) {
435 // NOTE: if we fail early (i.e. in this method), we need to schedule
436 // an idle to report the error. This is because we guarantee that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700437 // the callback is always called from the GLib mainloop (this
David Zeuthen27a48bc2013-08-06 12:06:29 -0700438 // guarantee is useful for testing).
439
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700440 GError *error = nullptr;
441 if (!g_spawn_async_with_pipes(nullptr, // working_directory
David Zeuthen27a48bc2013-08-06 12:06:29 -0700442 argv,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700443 nullptr, // envp
David Zeuthen27a48bc2013-08-06 12:06:29 -0700444 static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
445 G_SPAWN_DO_NOT_REAP_CHILD),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700446 nullptr, // child_setup
David Zeuthen27a48bc2013-08-06 12:06:29 -0700447 this,
448 &pid_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700449 nullptr, // standard_input
David Zeuthen27a48bc2013-08-06 12:06:29 -0700450 &stdout_fd_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700451 nullptr, // standard_error
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452 &error)) {
453 LOG(ERROR) << "Error spawning p2p-client: "
454 << utils::GetAndFreeGError(&error);
455 ReportErrorAndDeleteInIdle();
456 return;
457 }
458
459 GIOChannel* io_channel = g_io_channel_unix_new(stdout_fd_);
460 stdout_channel_source_id_ = g_io_add_watch(
461 io_channel,
462 static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),
463 OnIOChannelActivity, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700464 CHECK_NE(stdout_channel_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700465 g_io_channel_unref(io_channel);
466
467 child_watch_source_id_ = g_child_watch_add(pid_, OnChildWatchActivity,
468 this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700469 CHECK_NE(child_watch_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700470
471 if (timeout.ToInternalValue() > 0) {
472 timeout_source_id_ = g_timeout_add(timeout.InMilliseconds(),
473 OnTimeout, this);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700474 CHECK_NE(timeout_source_id_, 0u);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700475 }
476 }
477
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700478 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -0700479 void ReportErrorAndDeleteInIdle() {
480 g_idle_add(static_cast<GSourceFunc>(OnIdleForReportErrorAndDelete), this);
481 }
482
483 static gboolean OnIdleForReportErrorAndDelete(gpointer user_data) {
484 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
485 lookup_data->ReportError();
486 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700487 return FALSE; // Remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700488 }
489
490 void IssueCallback(const string& url) {
491 if (!callback_.is_null())
492 callback_.Run(url);
493 }
494
495 void ReportError() {
496 if (reported_)
497 return;
498 IssueCallback("");
499 reported_ = true;
500 }
501
502 void ReportSuccess() {
503 if (reported_)
504 return;
505
506 string url = stdout_;
507 size_t newline_pos = url.find('\n');
508 if (newline_pos != string::npos)
509 url.resize(newline_pos);
510
511 // Since p2p-client(1) is constructing this URL itself strictly
512 // speaking there's no need to validate it... but, anyway, can't
513 // hurt.
514 if (url.compare(0, 7, "http://") == 0) {
515 IssueCallback(url);
516 } else {
517 LOG(ERROR) << "p2p URL '" << url << "' does not look right. Ignoring.";
518 ReportError();
519 }
520
521 reported_ = true;
522 }
523
524 static gboolean OnIOChannelActivity(GIOChannel *source,
525 GIOCondition condition,
526 gpointer user_data) {
527 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700528 gchar* str = nullptr;
529 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700530 GIOStatus status = g_io_channel_read_line(source,
531 &str,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700532 nullptr, // len
533 nullptr, // line_terminator
David Zeuthen27a48bc2013-08-06 12:06:29 -0700534 &error);
535 if (status != G_IO_STATUS_NORMAL) {
536 // Ignore EOF since we usually get that before SIGCHLD and we
537 // need to examine exit status there.
538 if (status != G_IO_STATUS_EOF) {
539 LOG(ERROR) << "Error reading a line from p2p-client: "
540 << utils::GetAndFreeGError(&error);
541 lookup_data->ReportError();
542 delete lookup_data;
543 }
544 } else {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700545 if (str != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700546 lookup_data->stdout_ += str;
547 g_free(str);
548 }
549 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700550 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700551 }
552
553 static void OnChildWatchActivity(GPid pid,
554 gint status,
555 gpointer user_data) {
556 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
557
558 if (!WIFEXITED(status)) {
559 LOG(ERROR) << "Child didn't exit normally";
560 lookup_data->ReportError();
561 } else if (WEXITSTATUS(status) != 0) {
562 LOG(INFO) << "Child exited with non-zero exit code "
563 << WEXITSTATUS(status);
564 lookup_data->ReportError();
565 } else {
566 lookup_data->ReportSuccess();
567 }
568 delete lookup_data;
569 }
570
571 static gboolean OnTimeout(gpointer user_data) {
572 LookupData *lookup_data = reinterpret_cast<LookupData*>(user_data);
573 lookup_data->ReportError();
574 delete lookup_data;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700575 return TRUE; // Don't remove source.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700576 }
577
578 P2PManager::LookupCallback callback_;
579 GPid pid_;
580 gint stdout_fd_;
581 guint stdout_channel_source_id_;
582 guint child_watch_source_id_;
583 guint timeout_source_id_;
584 string stdout_;
585 bool reported_;
586};
587
588void P2PManagerImpl::LookupUrlForFile(const string& file_id,
589 size_t minimum_size,
590 TimeDelta max_time_to_wait,
591 LookupCallback callback) {
592 LookupData *lookup_data = new LookupData(callback);
593 string file_id_with_ext = file_id + "." + file_extension_;
594 vector<string> args = configuration_->GetP2PClientArgs(file_id_with_ext,
595 minimum_size);
596 gchar **argv = utils::StringVectorToGStrv(args);
597 lookup_data->InitiateLookup(argv, max_time_to_wait);
598 g_strfreev(argv);
599}
600
601bool P2PManagerImpl::FileShare(const string& file_id,
602 size_t expected_size) {
603 // Check if file already exist.
Alex Deymof329b932014-10-30 01:37:48 -0700604 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700605 if (!path.empty()) {
606 // File exists - double check its expected size though.
607 ssize_t file_expected_size = FileGetExpectedSize(file_id);
608 if (file_expected_size == -1 ||
609 static_cast<size_t>(file_expected_size) != expected_size) {
610 LOG(ERROR) << "Existing p2p file " << path.value()
611 << " with expected_size=" << file_expected_size
612 << " does not match the passed in"
613 << " expected_size=" << expected_size;
614 return false;
615 }
616 return true;
617 }
618
619 // Before creating the file, bail if statvfs(3) indicates that at
620 // least twice the size is not available in P2P_DIR.
621 struct statvfs statvfsbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700622 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700623 if (statvfs(p2p_dir.value().c_str(), &statvfsbuf) != 0) {
624 PLOG(ERROR) << "Error calling statvfs() for dir " << p2p_dir.value();
625 return false;
626 }
627 size_t free_bytes =
628 static_cast<size_t>(statvfsbuf.f_bsize) * statvfsbuf.f_bavail;
629 if (free_bytes < 2 * expected_size) {
630 // This can easily happen and is worth reporting.
631 LOG(INFO) << "Refusing to allocate p2p file of " << expected_size
632 << " bytes since the directory " << p2p_dir.value()
633 << " only has " << free_bytes
634 << " bytes available and this is less than twice the"
635 << " requested size.";
636 return false;
637 }
638
639 // Okie-dokey looks like enough space is available - create the file.
640 path = GetPath(file_id, kNonVisible);
641 int fd = open(path.value().c_str(), O_CREAT | O_RDWR, 0644);
642 if (fd == -1) {
643 PLOG(ERROR) << "Error creating file with path " << path.value();
644 return false;
645 }
646 ScopedFdCloser fd_closer(&fd);
647
648 // If the final size is known, allocate the file (e.g. reserve disk
649 // space) and set the user.cros-p2p-filesize xattr.
650 if (expected_size != 0) {
651 if (fallocate(fd,
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700652 FALLOC_FL_KEEP_SIZE, // Keep file size as 0.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700653 0,
654 expected_size) != 0) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700655 if (errno == ENOSYS || errno == EOPNOTSUPP) {
656 // If the filesystem doesn't support the fallocate, keep
657 // going. This is helpful when running unit tests on build
658 // machines with ancient filesystems and/or OSes.
659 PLOG(WARNING) << "Ignoring fallocate(2) failure";
660 } else {
661 // ENOSPC can happen (funky race though, cf. the statvfs() check
662 // above), handle it gracefully, e.g. use logging level INFO.
663 PLOG(INFO) << "Error allocating " << expected_size
664 << " bytes for file " << path.value();
665 if (unlink(path.value().c_str()) != 0) {
666 PLOG(ERROR) << "Error deleting file with path " << path.value();
667 }
668 return false;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700669 }
David Zeuthen27a48bc2013-08-06 12:06:29 -0700670 }
671
Alex Deymof329b932014-10-30 01:37:48 -0700672 string decimal_size = StringPrintf("%zu", expected_size);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700673 if (fsetxattr(fd, kCrosP2PFileSizeXAttrName,
674 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
675 PLOG(ERROR) << "Error setting xattr " << path.value();
676 return false;
677 }
678 }
679
680 return true;
681}
682
683FilePath P2PManagerImpl::FileGetPath(const string& file_id) {
684 struct stat statbuf;
Alex Deymof329b932014-10-30 01:37:48 -0700685 FilePath path;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700686
687 path = GetPath(file_id, kVisible);
688 if (stat(path.value().c_str(), &statbuf) == 0) {
689 return path;
690 }
691
692 path = GetPath(file_id, kNonVisible);
693 if (stat(path.value().c_str(), &statbuf) == 0) {
694 return path;
695 }
696
697 path.clear();
698 return path;
699}
700
701bool P2PManagerImpl::FileGetVisible(const string& file_id,
702 bool *out_result) {
Alex Deymof329b932014-10-30 01:37:48 -0700703 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700704 if (path.empty()) {
705 LOG(ERROR) << "No file for id " << file_id;
706 return false;
707 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700708 if (out_result != nullptr)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700709 *out_result = path.MatchesExtension(kP2PExtension);
710 return true;
711}
712
713bool P2PManagerImpl::FileMakeVisible(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700714 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700715 if (path.empty()) {
716 LOG(ERROR) << "No file for id " << file_id;
717 return false;
718 }
719
720 // Already visible?
721 if (path.MatchesExtension(kP2PExtension))
722 return true;
723
724 LOG_ASSERT(path.MatchesExtension(kTmpExtension));
Alex Deymof329b932014-10-30 01:37:48 -0700725 FilePath new_path = path.RemoveExtension();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700726 LOG_ASSERT(new_path.MatchesExtension(kP2PExtension));
727 if (rename(path.value().c_str(), new_path.value().c_str()) != 0) {
728 PLOG(ERROR) << "Error renaming " << path.value()
729 << " to " << new_path.value();
730 return false;
731 }
732
733 return true;
734}
735
736ssize_t P2PManagerImpl::FileGetSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700737 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700738 if (path.empty())
739 return -1;
740
Gabe Blacka77939e2014-09-09 23:35:08 -0700741 return utils::FileSize(path.value());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700742}
743
744ssize_t P2PManagerImpl::FileGetExpectedSize(const string& file_id) {
Alex Deymof329b932014-10-30 01:37:48 -0700745 FilePath path = FileGetPath(file_id);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700746 if (path.empty())
747 return -1;
748
749 char ea_value[64] = { 0 };
750 ssize_t ea_size;
751 ea_size = getxattr(path.value().c_str(), kCrosP2PFileSizeXAttrName,
752 &ea_value, sizeof(ea_value) - 1);
753 if (ea_size == -1) {
754 PLOG(ERROR) << "Error calling getxattr() on file " << path.value();
755 return -1;
756 }
757
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700758 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700759 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
David Zeuthen27a48bc2013-08-06 12:06:29 -0700760 if (*endp != '\0') {
761 LOG(ERROR) << "Error parsing the value '" << ea_value
762 << "' of the xattr " << kCrosP2PFileSizeXAttrName
763 << " as an integer";
764 return -1;
765 }
766
767 return val;
768}
769
770int P2PManagerImpl::CountSharedFiles() {
771 GDir* dir;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700772 GError* error = nullptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700773 const char* name;
774 int num_files = 0;
775
Alex Deymof329b932014-10-30 01:37:48 -0700776 FilePath p2p_dir = configuration_->GetP2PDir();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700777 dir = g_dir_open(p2p_dir.value().c_str(), 0, &error);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700778 if (dir == nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700779 LOG(ERROR) << "Error opening directory " << p2p_dir.value() << ": "
780 << utils::GetAndFreeGError(&error);
781 return -1;
782 }
783
784 string ext_visible = GetExt(kVisible);
785 string ext_non_visible = GetExt(kNonVisible);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700786 while ((name = g_dir_read_name(dir)) != nullptr) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700787 if (g_str_has_suffix(name, ext_visible.c_str()) ||
788 g_str_has_suffix(name, ext_non_visible.c_str())) {
789 num_files += 1;
790 }
791 }
792 g_dir_close(dir);
793
794 return num_files;
795}
796
Gilad Arnoldccd09572014-10-27 13:37:50 -0700797bool P2PManagerImpl::SetP2PEnabledPref(bool enabled) {
798 if (!prefs_->SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, enabled))
799 return false;
800
801 // If P2P should not be running, make sure it isn't.
802 if (may_be_running_ && !IsP2PEnabled())
803 EnsureP2PNotRunning();
804
805 return true;
806}
807
David Zeuthen27a48bc2013-08-06 12:06:29 -0700808P2PManager* P2PManager::Construct(Configuration *configuration,
809 PrefsInterface *prefs,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500810 ClockInterface *clock,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700811 const string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500812 const int num_files_to_keep,
813 const base::TimeDelta& max_file_age) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700814 return new P2PManagerImpl(configuration,
815 prefs,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500816 clock,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700817 file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500818 num_files_to_keep,
819 max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700820}
821
822} // namespace chromeos_update_engine