Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 5 | #include "cros-disks/device_ejector.h" |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 6 | |
Mattias Nissler | 6df1b1b | 2018-10-10 15:26:10 +0200 | [diff] [blame] | 7 | #include <linux/capability.h> |
| 8 | |
Ben Chan | 15232cf | 2017-09-26 22:58:03 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 12 | #include <base/check.h> |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 14 | |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 15 | #include "cros-disks/quote.h" |
| 16 | |
Anand K Mistry | 40cff45 | 2019-07-30 10:24:48 +1000 | [diff] [blame] | 17 | namespace cros_disks { |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // Expected location of the 'eject' program. |
| 21 | const char kEjectProgram[] = "/usr/bin/eject"; |
| 22 | |
| 23 | } // namespace |
| 24 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 25 | DeviceEjector::DeviceEjector(brillo::ProcessReaper* process_reaper) |
| 26 | : process_reaper_(process_reaper), weak_ptr_factory_(this) {} |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 27 | |
Ben Chan | fc77d71 | 2019-06-20 12:45:56 -0700 | [diff] [blame] | 28 | DeviceEjector::~DeviceEjector() = default; |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 29 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 30 | bool DeviceEjector::Eject(const std::string& device_path) { |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 31 | CHECK(!device_path.empty()) << "Invalid device path"; |
| 32 | |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 33 | LOG(INFO) << "Eject device " << quote(device_path); |
Qijiang Fan | 5243904 | 2020-06-17 15:34:38 +0900 | [diff] [blame] | 34 | if (base::Contains(eject_process_, device_path)) { |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 35 | LOG(WARNING) << "Device " << quote(device_path) |
| 36 | << " is already being ejected"; |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 39 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 40 | SandboxedProcess* process = &eject_process_[device_path]; |
| 41 | process->SetNoNewPrivileges(); |
| 42 | process->NewIpcNamespace(); |
| 43 | process->NewNetworkNamespace(); |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 44 | process->AddArgument(kEjectProgram); |
| 45 | process->AddArgument(device_path); |
Mattias Nissler | 6df1b1b | 2018-10-10 15:26:10 +0200 | [diff] [blame] | 46 | process->SetCapabilities(CAP_TO_MASK(CAP_SYS_ADMIN)); |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 47 | |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 48 | // TODO(benchan): Set up a timeout to kill a hanging process. |
Anand K Mistry | 5734037 | 2018-08-22 17:12:23 +1000 | [diff] [blame] | 49 | bool started = process->Start(); |
| 50 | if (started) { |
| 51 | process_reaper_->WatchForChild( |
| 52 | FROM_HERE, process->pid(), |
Anand K Mistry | 44b77d4 | 2019-09-27 11:03:20 +1000 | [diff] [blame] | 53 | base::BindOnce(&DeviceEjector::OnEjectProcessTerminated, |
| 54 | weak_ptr_factory_.GetWeakPtr(), device_path)); |
Anand K Mistry | 5734037 | 2018-08-22 17:12:23 +1000 | [diff] [blame] | 55 | } else { |
| 56 | eject_process_.erase(device_path); |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 57 | LOG(WARNING) << "Cannot eject media from device " << quote(device_path); |
Anand K Mistry | 5734037 | 2018-08-22 17:12:23 +1000 | [diff] [blame] | 58 | } |
| 59 | return started; |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 62 | void DeviceEjector::OnEjectProcessTerminated(const std::string& device_path, |
| 63 | const siginfo_t& info) { |
| 64 | eject_process_.erase(device_path); |
| 65 | switch (info.si_code) { |
| 66 | case CLD_EXITED: |
| 67 | if (info.si_status == 0) { |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 68 | LOG(INFO) << "Process " << info.si_pid << " for ejecting " |
| 69 | << quote(device_path) << " completed successfully"; |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 70 | } else { |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 71 | LOG(ERROR) << "Process " << info.si_pid << " for ejecting " |
| 72 | << quote(device_path) << " exited with a status " |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 73 | << info.si_status; |
| 74 | } |
Ben Chan | bba00ef | 2017-01-05 13:26:04 -0800 | [diff] [blame] | 75 | break; |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 76 | |
| 77 | case CLD_DUMPED: |
| 78 | case CLD_KILLED: |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 79 | LOG(ERROR) << "Process " << info.si_pid << " for ejecting " |
| 80 | << quote(device_path) << " killed by a signal " |
| 81 | << info.si_status; |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 82 | break; |
| 83 | |
| 84 | default: |
| 85 | break; |
Ben Chan | bba00ef | 2017-01-05 13:26:04 -0800 | [diff] [blame] | 86 | } |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace cros_disks |