blob: 28aad1866491d8ec2931e0290c5ea2678397a6c1 [file] [log] [blame]
Chirantan Ekbote47428f02018-02-02 17:56:57 -08001// 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 Torokhovdcd630c2018-10-18 16:50:24 -07005#include "debugd/src/simple_service_tool.h"
Chirantan Ekbote47428f02018-02-02 17:56:57 -08006
7#include <utility>
8
9#include <base/bind.h>
hscham4ce3c992021-02-19 16:37:23 +090010#include <base/callback_helpers.h>
Qijiang Fan713061e2021-03-08 15:45:12 +090011#include <base/check.h>
Chirantan Ekbote47428f02018-02-02 17:56:57 -080012#include <base/location.h>
Simon Glass2b1da092020-05-21 12:24:16 -060013#include <brillo/process/process.h>
Chirantan Ekbote47428f02018-02-02 17:56:57 -080014#include <dbus/object_path.h>
15
16using std::string;
17
18namespace debugd {
19namespace {
20
21// Posted to the MessageLoop by dbus::ObjectProxy once the concierge
22// service is available on dbus.
23void ServiceReady(
24 std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<bool>> response,
25 bool is_available) {
26 response->Return(is_available);
27}
28
29} // namespace
30
Dmitry Torokhovaa485882018-10-19 13:26:06 -070031SimpleServiceTool::SimpleServiceTool(const std::string& name,
32 scoped_refptr<dbus::Bus> bus,
33 const std::string& dbus_service_name,
34 const std::string& dbus_service_path)
35 : name_(name), bus_(bus), running_(false) {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080036 CHECK(bus_);
37
Dmitry Torokhov93924ce2019-11-05 09:48:33 -080038 proxy_ = bus_->GetObjectProxy(dbus_service_name,
39 dbus::ObjectPath(dbus_service_path));
Dmitry Torokhovaa485882018-10-19 13:26:06 -070040 proxy_->SetNameOwnerChangedCallback(base::Bind(
41 &SimpleServiceTool::HandleNameOwnerChanged, weak_factory_.GetWeakPtr()));
Chirantan Ekbote47428f02018-02-02 17:56:57 -080042}
43
Dmitry Torokhovaa485882018-10-19 13:26:06 -070044void SimpleServiceTool::StartService(
Dmitry Torokhov93924ce2019-11-05 09:48:33 -080045 std::map<std::string, std::string> args,
Chirantan Ekbote47428f02018-02-02 17:56:57 -080046 std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<bool>> response) {
47 if (running_) {
48 response->Return(true);
49 return;
50 }
51
Dmitry Torokhovaa485882018-10-19 13:26:06 -070052 LOG(INFO) << "Starting " << name_;
53 brillo::ProcessImpl service;
54 service.AddArg("/sbin/start");
55 service.AddArg(name_);
Dmitry Torokhov93924ce2019-11-05 09:48:33 -080056 for (const auto& arg : args) {
57 service.AddArg(
58 base::StringPrintf("%s=%s", arg.first.c_str(), arg.second.c_str()));
59 }
Dmitry Torokhovaa485882018-10-19 13:26:06 -070060 service.Run();
Chirantan Ekbote47428f02018-02-02 17:56:57 -080061
62 // dbus::ObjectProxy keeps a list of WaitForServiceToBeAvailable
63 // callbacks so we can safely call this multiple times if there are multiple
64 // pending dbus requests.
Dmitry Torokhovaa485882018-10-19 13:26:06 -070065 proxy_->WaitForServiceToBeAvailable(
Chirantan Ekbote47428f02018-02-02 17:56:57 -080066 base::Bind(&ServiceReady, base::Passed(std::move(response))));
67}
68
Dmitry Torokhovaa485882018-10-19 13:26:06 -070069void SimpleServiceTool::StopService() {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080070 if (!running_) {
71 // Nothing to do.
72 return;
73 }
74
Dmitry Torokhovaa485882018-10-19 13:26:06 -070075 LOG(INFO) << "Stopping " << name_;
Chirantan Ekbote47428f02018-02-02 17:56:57 -080076
Dmitry Torokhovaa485882018-10-19 13:26:06 -070077 brillo::ProcessImpl service;
78 service.AddArg("/sbin/stop");
79 service.AddArg(name_);
Chirantan Ekbote47428f02018-02-02 17:56:57 -080080
Dmitry Torokhovaa485882018-10-19 13:26:06 -070081 service.Run();
Chirantan Ekbote47428f02018-02-02 17:56:57 -080082}
83
Dmitry Torokhovaa485882018-10-19 13:26:06 -070084void SimpleServiceTool::HandleNameOwnerChanged(const string& old_owner,
85 const string& new_owner) {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080086 running_ = !new_owner.empty();
87}
88
89} // namespace debugd