blob: be2a6d8c8f908fcd19e11a8c3d3c5d92efa3a8ea [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>
12#include <brillo/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 Torokhovaa485882018-10-19 13:26:06 -070037 proxy_ = bus_->GetObjectProxy(
38 dbus_service_name, dbus::ObjectPath(dbus_service_path));
39 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(
Chirantan Ekbote47428f02018-02-02 17:56:57 -080044 std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<bool>> response) {
45 if (running_) {
46 response->Return(true);
47 return;
48 }
49
Dmitry Torokhovaa485882018-10-19 13:26:06 -070050 LOG(INFO) << "Starting " << name_;
51 brillo::ProcessImpl service;
52 service.AddArg("/sbin/start");
53 service.AddArg(name_);
Chirantan Ekbote47428f02018-02-02 17:56:57 -080054
Dmitry Torokhovaa485882018-10-19 13:26:06 -070055 service.Run();
Chirantan Ekbote47428f02018-02-02 17:56:57 -080056
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 Torokhovaa485882018-10-19 13:26:06 -070060 proxy_->WaitForServiceToBeAvailable(
Chirantan Ekbote47428f02018-02-02 17:56:57 -080061 base::Bind(&ServiceReady, base::Passed(std::move(response))));
62}
63
Dmitry Torokhovaa485882018-10-19 13:26:06 -070064void SimpleServiceTool::StopService() {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080065 if (!running_) {
66 // Nothing to do.
67 return;
68 }
69
Dmitry Torokhovaa485882018-10-19 13:26:06 -070070 LOG(INFO) << "Stopping " << name_;
Chirantan Ekbote47428f02018-02-02 17:56:57 -080071
Dmitry Torokhovaa485882018-10-19 13:26:06 -070072 brillo::ProcessImpl service;
73 service.AddArg("/sbin/stop");
74 service.AddArg(name_);
Chirantan Ekbote47428f02018-02-02 17:56:57 -080075
Dmitry Torokhovaa485882018-10-19 13:26:06 -070076 service.Run();
Chirantan Ekbote47428f02018-02-02 17:56:57 -080077}
78
Dmitry Torokhovaa485882018-10-19 13:26:06 -070079void SimpleServiceTool::HandleNameOwnerChanged(const string& old_owner,
80 const string& new_owner) {
Chirantan Ekbote47428f02018-02-02 17:56:57 -080081 running_ = !new_owner.empty();
82}
83
84} // namespace debugd