blob: 5a8f705a821a1c06631dcda7ebb2de338e69dc33 [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jones9aa5eca2011-11-04 14:48:13 -04002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Elly Jones9aa5eca2011-11-04 14:48:13 -04005#include <sys/mount.h>
Elly Jonesd31aee22012-07-02 11:09:10 -04006#include <sys/stat.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -04007#include <unistd.h>
8
Elly Jonese7cb5b32011-12-01 14:18:32 -05009#include <base/command_line.h>
10#include <base/logging.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070011#include <brillo/daemons/dbus_daemon.h>
Alex Vakulenkoe7696532015-10-16 16:27:29 -070012#include <brillo/syslog_logging.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070013#include <chromeos/dbus/service_constants.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040014#include <chromeos/libminijail.h>
15
Eric Caruso63c97432017-04-27 13:23:28 -070016#include "debugd/src/debugd_dbus_adaptor.h"
Elly Jones9aa5eca2011-11-04 14:48:13 -040017
Elly Jonese7cb5b32011-12-01 14:18:32 -050018namespace {
Elly Jones9aa5eca2011-11-04 14:48:13 -040019
Eric Carusocc7106c2017-04-27 14:22:42 -070020const char kObjectServicePath[] = "/org/chromium/debugd/ObjectManager";
21
Elly Jonese7cb5b32011-12-01 14:18:32 -050022// @brief Enter a VFS namespace.
23//
24// We don't want anyone other than our descendants to see our tmpfs.
25void enter_vfs_namespace() {
26 struct minijail* j = minijail_new();
27 minijail_namespace_vfs(j);
28 minijail_enter(j);
29 minijail_destroy(j);
Elly Jones9aa5eca2011-11-04 14:48:13 -040030}
31
Elly Jones9aa5eca2011-11-04 14:48:13 -040032// @brief Sets up a tmpfs visible to this program and its descendants.
33//
34// The created tmpfs is mounted at /debugd.
35void make_tmpfs() {
36 int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC,
Ben Chan64d19b22017-02-06 14:03:47 -080037 nullptr);
Elly Jones9aa5eca2011-11-04 14:48:13 -040038 if (r < 0)
Elly Jonese7cb5b32011-12-01 14:18:32 -050039 PLOG(FATAL) << "mount() failed";
Elly Jones9aa5eca2011-11-04 14:48:13 -040040}
41
Elly Jonesd31aee22012-07-02 11:09:10 -040042// @brief Sets up directories needed by helper programs.
43//
44void setup_dirs() {
45 int r = mkdir("/debugd/touchpad", S_IRWXU);
46 if (r < 0)
47 PLOG(FATAL) << "mkdir(\"/debugd/touchpad\") failed";
48}
49
Eric Carusocc7106c2017-04-27 14:22:42 -070050class Daemon : public brillo::DBusServiceDaemon {
51 public:
52 Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName,
53 dbus::ObjectPath(kObjectServicePath)) {}
54
55 protected:
56 void RegisterDBusObjectsAsync(
Eric Carusob298bb42017-05-09 10:53:30 -070057 brillo::dbus_utils::AsyncEventSequencer* sequencer) override {
Eric Carusocc7106c2017-04-27 14:22:42 -070058 adaptor_.reset(new debugd::DebugdDBusAdaptor(object_manager_.get()));
59 adaptor_->RegisterAsync(sequencer->GetHandler(
60 "RegisterAsync() failed.", true));
61 }
62
63 private:
64 std::unique_ptr<debugd::DebugdDBusAdaptor> adaptor_;
65
66 DISALLOW_COPY_AND_ASSIGN(Daemon);
67};
Ben Chanaf125862017-02-08 23:11:18 -080068
69} // namespace
Elly Jones9aa5eca2011-11-04 14:48:13 -040070
Elly Jonese7cb5b32011-12-01 14:18:32 -050071int __attribute__((visibility("default"))) main(int argc, char* argv[]) {
Alex Vakulenko274c74a2015-04-02 14:31:10 -070072 base::CommandLine::Init(argc, argv);
Alex Vakulenkoe7696532015-10-16 16:27:29 -070073 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
Elly Jonese7cb5b32011-12-01 14:18:32 -050074 enter_vfs_namespace();
Elly Jones9aa5eca2011-11-04 14:48:13 -040075 make_tmpfs();
Elly Jonesd31aee22012-07-02 11:09:10 -040076 setup_dirs();
Eric Carusocc7106c2017-04-27 14:22:42 -070077 Daemon().Run();
Elly Jones9aa5eca2011-11-04 14:48:13 -040078 return 0;
79}