OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // StorageMonitorLinux implementation. | 5 // StorageMonitorLinux implementation. |
6 | 6 |
7 #include "components/storage_monitor/storage_monitor_linux.h" | 7 #include "components/storage_monitor/storage_monitor_linux.h" |
8 | 8 |
9 #include <mntent.h> | 9 #include <mntent.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 | |
13 #include <limits> | 12 #include <limits> |
14 #include <list> | 13 #include <list> |
| 14 #include <utility> |
15 | 15 |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/process/kill.h" | 19 #include "base/process/kill.h" |
20 #include "base/process/launch.h" | 20 #include "base/process/launch.h" |
21 #include "base/process/process.h" | 21 #include "base/process/process.h" |
22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 const base::FilePath& mount_point) { | 124 const base::FilePath& mount_point) { |
125 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 125 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
126 DCHECK(!device_path.empty()); | 126 DCHECK(!device_path.empty()); |
127 | 127 |
128 scoped_ptr<StorageInfo> storage_info; | 128 scoped_ptr<StorageInfo> storage_info; |
129 | 129 |
130 ScopedGetDeviceInfoResultRecorder results_recorder; | 130 ScopedGetDeviceInfoResultRecorder results_recorder; |
131 | 131 |
132 device::ScopedUdevPtr udev_obj(device::udev_new()); | 132 device::ScopedUdevPtr udev_obj(device::udev_new()); |
133 if (!udev_obj.get()) | 133 if (!udev_obj.get()) |
134 return storage_info.Pass(); | 134 return storage_info; |
135 | 135 |
136 struct stat device_stat; | 136 struct stat device_stat; |
137 if (stat(device_path.value().c_str(), &device_stat) < 0) | 137 if (stat(device_path.value().c_str(), &device_stat) < 0) |
138 return storage_info.Pass(); | 138 return storage_info; |
139 | 139 |
140 char device_type; | 140 char device_type; |
141 if (S_ISCHR(device_stat.st_mode)) | 141 if (S_ISCHR(device_stat.st_mode)) |
142 device_type = 'c'; | 142 device_type = 'c'; |
143 else if (S_ISBLK(device_stat.st_mode)) | 143 else if (S_ISBLK(device_stat.st_mode)) |
144 device_type = 'b'; | 144 device_type = 'b'; |
145 else | 145 else |
146 return storage_info.Pass(); // Not a supported type. | 146 return storage_info; // Not a supported type. |
147 | 147 |
148 device::ScopedUdevDevicePtr device( | 148 device::ScopedUdevDevicePtr device( |
149 device::udev_device_new_from_devnum(udev_obj.get(), device_type, | 149 device::udev_device_new_from_devnum(udev_obj.get(), device_type, |
150 device_stat.st_rdev)); | 150 device_stat.st_rdev)); |
151 if (!device.get()) | 151 if (!device.get()) |
152 return storage_info.Pass(); | 152 return storage_info; |
153 | 153 |
154 base::string16 volume_label = base::UTF8ToUTF16( | 154 base::string16 volume_label = base::UTF8ToUTF16( |
155 device::UdevDeviceGetPropertyValue(device.get(), kLabel)); | 155 device::UdevDeviceGetPropertyValue(device.get(), kLabel)); |
156 base::string16 vendor_name = base::UTF8ToUTF16( | 156 base::string16 vendor_name = base::UTF8ToUTF16( |
157 device::UdevDeviceGetPropertyValue(device.get(), kVendor)); | 157 device::UdevDeviceGetPropertyValue(device.get(), kVendor)); |
158 base::string16 model_name = base::UTF8ToUTF16( | 158 base::string16 model_name = base::UTF8ToUTF16( |
159 device::UdevDeviceGetPropertyValue(device.get(), kModel)); | 159 device::UdevDeviceGetPropertyValue(device.get(), kModel)); |
160 | 160 |
161 std::string unique_id = MakeDeviceUniqueId(device.get()); | 161 std::string unique_id = MakeDeviceUniqueId(device.get()); |
162 | 162 |
(...skipping 25 matching lines...) Expand all Loading... |
188 | 188 |
189 results_recorder.set_result(true); | 189 results_recorder.set_result(true); |
190 | 190 |
191 storage_info.reset(new StorageInfo( | 191 storage_info.reset(new StorageInfo( |
192 StorageInfo::MakeDeviceId(type, unique_id), | 192 StorageInfo::MakeDeviceId(type, unique_id), |
193 mount_point.value(), | 193 mount_point.value(), |
194 volume_label, | 194 volume_label, |
195 vendor_name, | 195 vendor_name, |
196 model_name, | 196 model_name, |
197 GetDeviceStorageSize(device_path, device.get()))); | 197 GetDeviceStorageSize(device_path, device.get()))); |
198 return storage_info.Pass(); | 198 return storage_info; |
199 } | 199 } |
200 | 200 |
201 MtabWatcherLinux* CreateMtabWatcherLinuxOnFileThread( | 201 MtabWatcherLinux* CreateMtabWatcherLinuxOnFileThread( |
202 const base::FilePath& mtab_path, | 202 const base::FilePath& mtab_path, |
203 base::WeakPtr<MtabWatcherLinux::Delegate> delegate) { | 203 base::WeakPtr<MtabWatcherLinux::Delegate> delegate) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 204 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
205 // Owned by caller. | 205 // Owned by caller. |
206 return new MtabWatcherLinux(mtab_path, delegate); | 206 return new MtabWatcherLinux(mtab_path, delegate); |
207 } | 207 } |
208 | 208 |
(...skipping 10 matching lines...) Expand all Loading... |
219 | 219 |
220 base::LaunchOptions options; | 220 base::LaunchOptions options; |
221 base::Process process = base::LaunchProcess(command, options); | 221 base::Process process = base::LaunchProcess(command, options); |
222 if (!process.IsValid()) | 222 if (!process.IsValid()) |
223 return StorageMonitor::EJECT_FAILURE; | 223 return StorageMonitor::EJECT_FAILURE; |
224 | 224 |
225 int exit_code = -1; | 225 int exit_code = -1; |
226 if (!process.WaitForExitWithTimeout(base::TimeDelta::FromMilliseconds(3000), | 226 if (!process.WaitForExitWithTimeout(base::TimeDelta::FromMilliseconds(3000), |
227 &exit_code)) { | 227 &exit_code)) { |
228 process.Terminate(-1, false); | 228 process.Terminate(-1, false); |
229 base::EnsureProcessTerminated(process.Pass()); | 229 base::EnsureProcessTerminated(std::move(process)); |
230 return StorageMonitor::EJECT_FAILURE; | 230 return StorageMonitor::EJECT_FAILURE; |
231 } | 231 } |
232 | 232 |
233 // TODO(gbillock): Make sure this is found in documentation | 233 // TODO(gbillock): Make sure this is found in documentation |
234 // somewhere. Experimentally it seems to hold that exit code | 234 // somewhere. Experimentally it seems to hold that exit code |
235 // 1 means device is in use. | 235 // 1 means device is in use. |
236 if (exit_code == 1) | 236 if (exit_code == 1) |
237 return StorageMonitor::EJECT_IN_USE; | 237 return StorageMonitor::EJECT_IN_USE; |
238 if (exit_code != 0) | 238 if (exit_code != 0) |
239 return StorageMonitor::EJECT_FAILURE; | 239 return StorageMonitor::EJECT_FAILURE; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 mount_priority_map_[mount_device][mount_point] = removable; | 503 mount_priority_map_[mount_device][mount_point] = removable; |
504 receiver()->ProcessAttach(*storage_info); | 504 receiver()->ProcessAttach(*storage_info); |
505 } | 505 } |
506 | 506 |
507 StorageMonitor* StorageMonitor::CreateInternal() { | 507 StorageMonitor* StorageMonitor::CreateInternal() { |
508 const base::FilePath kDefaultMtabPath("/etc/mtab"); | 508 const base::FilePath kDefaultMtabPath("/etc/mtab"); |
509 return new StorageMonitorLinux(kDefaultMtabPath); | 509 return new StorageMonitorLinux(kDefaultMtabPath); |
510 } | 510 } |
511 | 511 |
512 } // namespace storage_monitor | 512 } // namespace storage_monitor |
OLD | NEW |