blob: 86e71a4e2f36d65005bdfea9ad310cc6aa25e346 [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>
10#include <base/bind_helpers.h>
11#include <base/location.h>
Simon Glass2b1da092020-05-21 12:24:16 -060012#include <brillo/process/process.h>
Chirantan Ekbote47428f02018-02-02 17:56:57 -080013#include <dbus/object_path.h>
14
15using std::string;
16
17namespace debugd {
18namespace {
19
20// Posted to the MessageLoop by dbus::ObjectProxy once the concierge
21// service is available on dbus.
22void 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 Torokhovaa485882018-10-19 13:26:06 -070030SimpleServiceTool::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 Ekbote47428f02018-02-02 17:56:57 -080035 CHECK(bus_);
36
Dmitry Torokhov93924ce2019-11-05 09:48:33 -080037 proxy_ = bus_->GetObjectProxy(dbus_service_name,
38 dbus::ObjectPath(dbus_service_path));
Dmitry Torokhovaa485882018-10-19 13:26:06 -070039 proxy_->SetNameOwnerChangedCallback(base::Bind(
40 &SimpleServiceTool::HandleNameOwnerChanged, weak_factory_.GetWeakPtr()));
Chirantan Ekbote47428f02018-02-02 17:56:57 -080041}
42
Dmitry Torokhovaa485882018-10-19 13:26:06 -070043void SimpleServiceTool::StartService(
Dmitry Torokhov93924ce2019-11-05 09:48:33 -080044 std::map<std::string, std::string> args,
Chirantan Ekbote47428f02018-02-02 17:56:57 -080045 std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<bool>> response) {
46 if (running_) {
47 response->Return(true);
48 return;
49 }
50
Dmitry Torokhovaa485882018-10-19 13:26:06 -070051 LOG(INFO) << "Starting " << name_;
52 brillo::ProcessImpl service;
53 service.AddArg("/sbin/start");
54 service.AddArg(name_);
Dmitry Torokhov93924ce2019-11-05 09:48:33 -080055 for (const auto& arg : args) {
56 service.AddArg(
57 base::StringPrintf("%s=%s", arg.first.c_str(), arg.second.c_str()));
58 }
Dmitry Torokhovaa485882018-10-19 13:26:06 -070059 service.Run();
Chirantan Ekbote47428f02018-02-02 17:56:57 -080060
61 // dbus::ObjectProxy keeps a list of WaitForServiceToBeAvailable
62 // callbacks so we can safely call this multiple times if there are multiple
63 // pending dbus requests.
Dmitry Torokhovaa485882018-10-19 13:26:06 -070064 proxy_->WaitForServiceToBeAvailable(
Chirantan Ekbote47428f02018-02-02 17:56:57 -080065 base::Bind(&ServiceReady, base::Passed(std::move(response))));
66}
67
Dmitry Torokhovaa485882018-10-19 13:26:06 -070068void SimpleServiceTool::StopService() {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080069 if (!running_) {
70 // Nothing to do.
71 return;
72 }
73
Dmitry Torokhovaa485882018-10-19 13:26:06 -070074 LOG(INFO) << "Stopping " << name_;
Chirantan Ekbote47428f02018-02-02 17:56:57 -080075
Dmitry Torokhovaa485882018-10-19 13:26:06 -070076 brillo::ProcessImpl service;
77 service.AddArg("/sbin/stop");
78 service.AddArg(name_);
Chirantan Ekbote47428f02018-02-02 17:56:57 -080079
Dmitry Torokhovaa485882018-10-19 13:26:06 -070080 service.Run();
Chirantan Ekbote47428f02018-02-02 17:56:57 -080081}
82
Dmitry Torokhovaa485882018-10-19 13:26:06 -070083void SimpleServiceTool::HandleNameOwnerChanged(const string& old_owner,
84 const string& new_owner) {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080085 running_ = !new_owner.empty();
86}
87
88} // namespace debugd