blob: 71fc9820ab4382c44184098717c4f72cb6722c48 [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>
Alex Vakulenkoe7696532015-10-16 16:27:29 -070011#include <brillo/process.h>
12#include <brillo/syslog_logging.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040013#include <chromeos/libminijail.h>
14
Alex Vakulenko262be3f2014-07-30 15:25:50 -070015#include "debugd/src/debug_daemon.h"
Elly Jones9aa5eca2011-11-04 14:48:13 -040016
Elly Jonese7cb5b32011-12-01 14:18:32 -050017namespace {
18const char* kHelpers[] = {
Ben Chan64d19b22017-02-06 14:03:47 -080019 nullptr,
Elly Jones9aa5eca2011-11-04 14:48:13 -040020};
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
Elly Jones9aa5eca2011-11-04 14:48:13 -040050// @brief Launch all our helper programs.
51void launch_helpers() {
Elly Jonese7cb5b32011-12-01 14:18:32 -050052 for (int i = 0; kHelpers[i]; ++i) {
Alex Vakulenkoe7696532015-10-16 16:27:29 -070053 brillo::ProcessImpl p;
Elly Jonese7cb5b32011-12-01 14:18:32 -050054 p.AddArg(kHelpers[i]);
55 p.Start();
56 p.Release();
Elly Jones9aa5eca2011-11-04 14:48:13 -040057 }
58}
59
Elly Jonese7cb5b32011-12-01 14:18:32 -050060// @brief Start the debugd DBus interface.
61void start() {
62 DBus::BusDispatcher dispatcher;
63 DBus::default_dispatcher = &dispatcher;
64 DBus::Connection conn = DBus::Connection::SystemBus();
65 debugd::DebugDaemon debugd(&conn, &dispatcher);
66 if (!debugd.Init())
67 LOG(FATAL) << "debugd.Init() failed";
68 debugd.Run();
69 LOG(FATAL) << "debugd.Run() returned";
Elly Jones9aa5eca2011-11-04 14:48:13 -040070}
Elly Jonese7cb5b32011-12-01 14:18:32 -050071}; // namespace
Elly Jones9aa5eca2011-11-04 14:48:13 -040072
Elly Jonese7cb5b32011-12-01 14:18:32 -050073int __attribute__((visibility("default"))) main(int argc, char* argv[]) {
Alex Vakulenko274c74a2015-04-02 14:31:10 -070074 base::CommandLine::Init(argc, argv);
Alex Vakulenkoe7696532015-10-16 16:27:29 -070075 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
Elly Jonese7cb5b32011-12-01 14:18:32 -050076 enter_vfs_namespace();
Elly Jones9aa5eca2011-11-04 14:48:13 -040077 make_tmpfs();
Elly Jonesd31aee22012-07-02 11:09:10 -040078 setup_dirs();
Elly Jones9aa5eca2011-11-04 14:48:13 -040079 launch_helpers();
Elly Jonese7cb5b32011-12-01 14:18:32 -050080 start();
Elly Jones9aa5eca2011-11-04 14:48:13 -040081 return 0;
82}