blob: 4f227c5868b2bcc4f1d517bd4f7fc6f5ee0c65c9 [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/syslog_logging.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040012#include <chromeos/libminijail.h>
13
Alex Vakulenko262be3f2014-07-30 15:25:50 -070014#include "debugd/src/debug_daemon.h"
Elly Jones9aa5eca2011-11-04 14:48:13 -040015
Elly Jonese7cb5b32011-12-01 14:18:32 -050016namespace {
Elly Jones9aa5eca2011-11-04 14:48:13 -040017
Elly Jonese7cb5b32011-12-01 14:18:32 -050018// @brief Enter a VFS namespace.
19//
20// We don't want anyone other than our descendants to see our tmpfs.
21void enter_vfs_namespace() {
22 struct minijail* j = minijail_new();
23 minijail_namespace_vfs(j);
24 minijail_enter(j);
25 minijail_destroy(j);
Elly Jones9aa5eca2011-11-04 14:48:13 -040026}
27
Elly Jones9aa5eca2011-11-04 14:48:13 -040028// @brief Sets up a tmpfs visible to this program and its descendants.
29//
30// The created tmpfs is mounted at /debugd.
31void make_tmpfs() {
32 int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC,
Ben Chan64d19b22017-02-06 14:03:47 -080033 nullptr);
Elly Jones9aa5eca2011-11-04 14:48:13 -040034 if (r < 0)
Elly Jonese7cb5b32011-12-01 14:18:32 -050035 PLOG(FATAL) << "mount() failed";
Elly Jones9aa5eca2011-11-04 14:48:13 -040036}
37
Elly Jonesd31aee22012-07-02 11:09:10 -040038// @brief Sets up directories needed by helper programs.
39//
40void setup_dirs() {
41 int r = mkdir("/debugd/touchpad", S_IRWXU);
42 if (r < 0)
43 PLOG(FATAL) << "mkdir(\"/debugd/touchpad\") failed";
44}
45
Elly Jonese7cb5b32011-12-01 14:18:32 -050046// @brief Start the debugd DBus interface.
47void start() {
48 DBus::BusDispatcher dispatcher;
49 DBus::default_dispatcher = &dispatcher;
50 DBus::Connection conn = DBus::Connection::SystemBus();
51 debugd::DebugDaemon debugd(&conn, &dispatcher);
52 if (!debugd.Init())
53 LOG(FATAL) << "debugd.Init() failed";
54 debugd.Run();
55 LOG(FATAL) << "debugd.Run() returned";
Elly Jones9aa5eca2011-11-04 14:48:13 -040056}
Ben Chanaf125862017-02-08 23:11:18 -080057
58} // namespace
Elly Jones9aa5eca2011-11-04 14:48:13 -040059
Elly Jonese7cb5b32011-12-01 14:18:32 -050060int __attribute__((visibility("default"))) main(int argc, char* argv[]) {
Alex Vakulenko274c74a2015-04-02 14:31:10 -070061 base::CommandLine::Init(argc, argv);
Alex Vakulenkoe7696532015-10-16 16:27:29 -070062 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
Elly Jonese7cb5b32011-12-01 14:18:32 -050063 enter_vfs_namespace();
Elly Jones9aa5eca2011-11-04 14:48:13 -040064 make_tmpfs();
Elly Jonesd31aee22012-07-02 11:09:10 -040065 setup_dirs();
Elly Jonese7cb5b32011-12-01 14:18:32 -050066 start();
Elly Jones9aa5eca2011-11-04 14:48:13 -040067 return 0;
68}