OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" | 5 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "ash/shell.h" | 14 #include "ash/shell.h" |
15 #include "ash/wm/user_activity_detector.h" | 15 #include "ash/wm/user_activity_detector.h" |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/bind_helpers.h" | 17 #include "base/bind_helpers.h" |
18 #include "base/callback.h" | 18 #include "base/callback.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
21 #include "base/files/scoped_file.h" | |
22 #include "base/location.h" | 21 #include "base/location.h" |
23 #include "base/logging.h" | 22 #include "base/logging.h" |
24 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
25 #include "base/path_service.h" | 24 #include "base/path_service.h" |
26 #include "base/posix/eintr_wrapper.h" | 25 #include "base/posix/eintr_wrapper.h" |
27 #include "base/prefs/pref_registry_simple.h" | 26 #include "base/prefs/pref_registry_simple.h" |
28 #include "base/prefs/pref_service.h" | 27 #include "base/prefs/pref_service.h" |
29 #include "base/single_thread_task_runner.h" | 28 #include "base/single_thread_task_runner.h" |
30 #include "base/strings/string_number_conversions.h" | 29 #include "base/strings/string_number_conversions.h" |
31 #include "base/thread_task_runner_handle.h" | 30 #include "base/thread_task_runner_handle.h" |
(...skipping 18 matching lines...) Expand all Loading... |
50 | 49 |
51 namespace { | 50 namespace { |
52 | 51 |
53 const int kMinRebootUptimeMs = 60 * 60 * 1000; // 1 hour. | 52 const int kMinRebootUptimeMs = 60 * 60 * 1000; // 1 hour. |
54 const int kLoginManagerIdleTimeoutMs = 60 * 1000; // 60 seconds. | 53 const int kLoginManagerIdleTimeoutMs = 60 * 1000; // 60 seconds. |
55 const int kGracePeriodMs = 24 * 60 * 60 * 1000; // 24 hours. | 54 const int kGracePeriodMs = 24 * 60 * 60 * 1000; // 24 hours. |
56 const int kOneKilobyte = 1 << 10; // 1 kB in bytes. | 55 const int kOneKilobyte = 1 << 10; // 1 kB in bytes. |
57 | 56 |
58 base::TimeDelta ReadTimeDeltaFromFile(const base::FilePath& path) { | 57 base::TimeDelta ReadTimeDeltaFromFile(const base::FilePath& path) { |
59 base::ThreadRestrictions::AssertIOAllowed(); | 58 base::ThreadRestrictions::AssertIOAllowed(); |
60 base::ScopedFD fd( | 59 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_NOFOLLOW)); |
61 HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_NOFOLLOW))); | 60 if (fd < 0) |
62 if (!fd.is_valid()) | |
63 return base::TimeDelta(); | 61 return base::TimeDelta(); |
| 62 file_util::ScopedFD fd_closer(&fd); |
64 | 63 |
65 std::string contents; | 64 std::string contents; |
66 char buffer[kOneKilobyte]; | 65 char buffer[kOneKilobyte]; |
67 ssize_t length; | 66 ssize_t length; |
68 while ((length = read(fd.get(), buffer, sizeof(buffer))) > 0) | 67 while ((length = read(fd, buffer, sizeof(buffer))) > 0) |
69 contents.append(buffer, length); | 68 contents.append(buffer, length); |
70 | 69 |
71 double seconds; | 70 double seconds; |
72 if (!base::StringToDouble(contents.substr(0, contents.find(' ')), &seconds) || | 71 if (!base::StringToDouble(contents.substr(0, contents.find(' ')), &seconds) || |
73 seconds < 0.0) { | 72 seconds < 0.0) { |
74 return base::TimeDelta(); | 73 return base::TimeDelta(); |
75 } | 74 } |
76 return base::TimeDelta::FromMilliseconds(seconds * 1000.0); | 75 return base::TimeDelta::FromMilliseconds(seconds * 1000.0); |
77 } | 76 } |
78 | 77 |
(...skipping 23 matching lines...) Expand all Loading... |
102 ReadTimeDeltaFromFile(update_reboot_needed_uptime_file); | 101 ReadTimeDeltaFromFile(update_reboot_needed_uptime_file); |
103 if (last_update_reboot_needed_uptime != kZeroTimeDelta) | 102 if (last_update_reboot_needed_uptime != kZeroTimeDelta) |
104 return; | 103 return; |
105 | 104 |
106 base::FilePath uptime_file; | 105 base::FilePath uptime_file; |
107 CHECK(PathService::Get(chromeos::FILE_UPTIME, &uptime_file)); | 106 CHECK(PathService::Get(chromeos::FILE_UPTIME, &uptime_file)); |
108 const base::TimeDelta uptime = ReadTimeDeltaFromFile(uptime_file); | 107 const base::TimeDelta uptime = ReadTimeDeltaFromFile(uptime_file); |
109 if (uptime == kZeroTimeDelta) | 108 if (uptime == kZeroTimeDelta) |
110 return; | 109 return; |
111 | 110 |
112 base::ScopedFD fd(HANDLE_EINTR( | 111 int fd = HANDLE_EINTR(open(update_reboot_needed_uptime_file.value().c_str(), |
113 open(update_reboot_needed_uptime_file.value().c_str(), | 112 O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, |
114 O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, | 113 0666)); |
115 0666))); | 114 if (fd < 0) |
116 if (!fd.is_valid()) | |
117 return; | 115 return; |
| 116 file_util::ScopedFD fd_closer(&fd); |
118 | 117 |
119 std::string update_reboot_needed_uptime = | 118 std::string update_reboot_needed_uptime = |
120 base::DoubleToString(uptime.InSecondsF()); | 119 base::DoubleToString(uptime.InSecondsF()); |
121 base::WriteFileDescriptor(fd.get(), update_reboot_needed_uptime.c_str(), | 120 base::WriteFileDescriptor(fd, update_reboot_needed_uptime.c_str(), |
122 update_reboot_needed_uptime.size()); | 121 update_reboot_needed_uptime.size()); |
123 } | 122 } |
124 | 123 |
125 } // namespace | 124 } // namespace |
126 | 125 |
127 AutomaticRebootManager::SystemEventTimes::SystemEventTimes() | 126 AutomaticRebootManager::SystemEventTimes::SystemEventTimes() |
128 : has_boot_time(false), | 127 : has_boot_time(false), |
129 has_update_reboot_needed_time(false) { | 128 has_update_reboot_needed_time(false) { |
130 } | 129 } |
131 | 130 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 } | 409 } |
411 | 410 |
412 login_screen_idle_timer_.reset(); | 411 login_screen_idle_timer_.reset(); |
413 grace_start_timer_.reset(); | 412 grace_start_timer_.reset(); |
414 grace_end_timer_.reset(); | 413 grace_end_timer_.reset(); |
415 DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart(); | 414 DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart(); |
416 } | 415 } |
417 | 416 |
418 } // namespace system | 417 } // namespace system |
419 } // namespace chromeos | 418 } // namespace chromeos |
OLD | NEW |