Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 | |
Dmitry Torokhov | dcd630c | 2018-10-18 16:50:24 -0700 | [diff] [blame] | 5 | #include "debugd/src/simple_service_tool.h" |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <base/bind.h> |
| 10 | #include <base/bind_helpers.h> |
| 11 | #include <base/location.h> |
| 12 | #include <brillo/process.h> |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 13 | #include <dbus/object_path.h> |
| 14 | |
| 15 | using std::string; |
| 16 | |
| 17 | namespace debugd { |
| 18 | namespace { |
| 19 | |
| 20 | // Posted to the MessageLoop by dbus::ObjectProxy once the concierge |
| 21 | // service is available on dbus. |
| 22 | void ServiceReady( |
| 23 | std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<bool>> response, |
| 24 | bool is_available) { |
| 25 | response->Return(is_available); |
| 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 30 | SimpleServiceTool::SimpleServiceTool(const std::string& name, |
| 31 | scoped_refptr<dbus::Bus> bus, |
| 32 | const std::string& dbus_service_name, |
| 33 | const std::string& dbus_service_path) |
| 34 | : name_(name), bus_(bus), running_(false) { |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 35 | CHECK(bus_); |
| 36 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 37 | proxy_ = bus_->GetObjectProxy( |
| 38 | dbus_service_name, dbus::ObjectPath(dbus_service_path)); |
| 39 | proxy_->SetNameOwnerChangedCallback(base::Bind( |
| 40 | &SimpleServiceTool::HandleNameOwnerChanged, weak_factory_.GetWeakPtr())); |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 43 | void SimpleServiceTool::StartService( |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 44 | std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<bool>> response) { |
| 45 | if (running_) { |
| 46 | response->Return(true); |
| 47 | return; |
| 48 | } |
| 49 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 50 | LOG(INFO) << "Starting " << name_; |
| 51 | brillo::ProcessImpl service; |
| 52 | service.AddArg("/sbin/start"); |
| 53 | service.AddArg(name_); |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 54 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 55 | service.Run(); |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 56 | |
| 57 | // dbus::ObjectProxy keeps a list of WaitForServiceToBeAvailable |
| 58 | // callbacks so we can safely call this multiple times if there are multiple |
| 59 | // pending dbus requests. |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 60 | proxy_->WaitForServiceToBeAvailable( |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 61 | base::Bind(&ServiceReady, base::Passed(std::move(response)))); |
| 62 | } |
| 63 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 64 | void SimpleServiceTool::StopService() { |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 65 | if (!running_) { |
| 66 | // Nothing to do. |
| 67 | return; |
| 68 | } |
| 69 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 70 | LOG(INFO) << "Stopping " << name_; |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 71 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 72 | brillo::ProcessImpl service; |
| 73 | service.AddArg("/sbin/stop"); |
| 74 | service.AddArg(name_); |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 75 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 76 | service.Run(); |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Dmitry Torokhov | aa48588 | 2018-10-19 13:26:06 -0700 | [diff] [blame] | 79 | void SimpleServiceTool::HandleNameOwnerChanged(const string& old_owner, |
| 80 | const string& new_owner) { |
Chirantan Ekbote | 47428f0 | 2018-02-02 17:56:57 -0800 | [diff] [blame] | 81 | running_ = !new_owner.empty(); |
| 82 | } |
| 83 | |
| 84 | } // namespace debugd |