blob: 7dfe4799d2a65cfd2c18f2f211ce454311639cc0 [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
Elly Jonese7cb5b32011-12-01 14:18:32 -050020// @brief Enter a VFS namespace.
21//
22// We don't want anyone other than our descendants to see our tmpfs.
23void enter_vfs_namespace() {
24 struct minijail* j = minijail_new();
25 minijail_namespace_vfs(j);
26 minijail_enter(j);
27 minijail_destroy(j);
Elly Jones9aa5eca2011-11-04 14:48:13 -040028}
29
Elly Jones9aa5eca2011-11-04 14:48:13 -040030// @brief Sets up a tmpfs visible to this program and its descendants.
31//
32// The created tmpfs is mounted at /debugd.
33void make_tmpfs() {
34 int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC,
Ben Chan64d19b22017-02-06 14:03:47 -080035 nullptr);
Elly Jones9aa5eca2011-11-04 14:48:13 -040036 if (r < 0)
Elly Jonese7cb5b32011-12-01 14:18:32 -050037 PLOG(FATAL) << "mount() failed";
Elly Jones9aa5eca2011-11-04 14:48:13 -040038}
39
Elly Jonesd31aee22012-07-02 11:09:10 -040040// @brief Sets up directories needed by helper programs.
41//
42void setup_dirs() {
43 int r = mkdir("/debugd/touchpad", S_IRWXU);
44 if (r < 0)
45 PLOG(FATAL) << "mkdir(\"/debugd/touchpad\") failed";
46}
47
Eric Carusocc7106c2017-04-27 14:22:42 -070048class Daemon : public brillo::DBusServiceDaemon {
49 public:
Eric Caruso51535852017-07-11 14:13:20 -070050 Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName) {}
Eric Carusocc7106c2017-04-27 14:22:42 -070051
52 protected:
53 void RegisterDBusObjectsAsync(
Eric Carusob298bb42017-05-09 10:53:30 -070054 brillo::dbus_utils::AsyncEventSequencer* sequencer) override {
Eric Caruso51535852017-07-11 14:13:20 -070055 adaptor_.reset(new debugd::DebugdDBusAdaptor(bus_));
Eric Carusocc7106c2017-04-27 14:22:42 -070056 adaptor_->RegisterAsync(sequencer->GetHandler(
57 "RegisterAsync() failed.", true));
58 }
59
60 private:
61 std::unique_ptr<debugd::DebugdDBusAdaptor> adaptor_;
62
63 DISALLOW_COPY_AND_ASSIGN(Daemon);
64};
Ben Chanaf125862017-02-08 23:11:18 -080065
66} // namespace
Elly Jones9aa5eca2011-11-04 14:48:13 -040067
Ben Chanc572aff2018-04-10 09:20:27 -070068int main(int argc, char* argv[]) {
Alex Vakulenko274c74a2015-04-02 14:31:10 -070069 base::CommandLine::Init(argc, argv);
Alex Vakulenkoe7696532015-10-16 16:27:29 -070070 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
Elly Jonese7cb5b32011-12-01 14:18:32 -050071 enter_vfs_namespace();
Elly Jones9aa5eca2011-11-04 14:48:13 -040072 make_tmpfs();
Elly Jonesd31aee22012-07-02 11:09:10 -040073 setup_dirs();
Eric Carusocc7106c2017-04-27 14:22:42 -070074 Daemon().Run();
Elly Jones9aa5eca2011-11-04 14:48:13 -040075 return 0;
76}