blob: 5ae81af1442501872cf60838b00eba4bfa87cd0d [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 Jones9aa5eca2011-11-04 14:48:13 -04006#include <unistd.h>
7
Elly Jonese7cb5b32011-12-01 14:18:32 -05008#include <base/command_line.h>
9#include <base/logging.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040010#include <chromeos/libminijail.h>
Elly Jonese7cb5b32011-12-01 14:18:32 -050011#include <chromeos/process.h>
12#include <chromeos/syslog_logging.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040013
Elly Jonese7cb5b32011-12-01 14:18:32 -050014#include "debug_daemon.h"
Elly Jones9aa5eca2011-11-04 14:48:13 -040015
Elly Jonese7cb5b32011-12-01 14:18:32 -050016namespace {
17const char* kHelpers[] = {
Elly Jones9aa5eca2011-11-04 14:48:13 -040018 NULL,
19};
20
Elly Jonese7cb5b32011-12-01 14:18:32 -050021// @brief Enter a VFS namespace.
22//
23// We don't want anyone other than our descendants to see our tmpfs.
24void enter_vfs_namespace() {
25 struct minijail* j = minijail_new();
26 minijail_namespace_vfs(j);
27 minijail_enter(j);
28 minijail_destroy(j);
Elly Jones9aa5eca2011-11-04 14:48:13 -040029}
30
31// @brief Enter a minijail.
32//
Elly Jonese7cb5b32011-12-01 14:18:32 -050033// We are already in a vfs namespace so that our tmpfs is only visible to us and
Elly Jones9aa5eca2011-11-04 14:48:13 -040034// our descendants, and we don't want to be root. Note that minijail_enter()
35// exits the process if it can't succeed.
36void enter_sandbox() {
Elly Jonese7cb5b32011-12-01 14:18:32 -050037 static const char* kDebugdUser = "debugd";
38 static const char* kDebugdGroup = "debugd";
39 struct minijail* j = minijail_new();
Elly Jones9aa5eca2011-11-04 14:48:13 -040040 minijail_change_user(j, kDebugdUser);
41 minijail_change_group(j, kDebugdGroup);
42 minijail_enter(j);
43 minijail_destroy(j);
44}
45
46// @brief Sets up a tmpfs visible to this program and its descendants.
47//
48// The created tmpfs is mounted at /debugd.
49void make_tmpfs() {
50 int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC,
51 NULL);
52 if (r < 0)
Elly Jonese7cb5b32011-12-01 14:18:32 -050053 PLOG(FATAL) << "mount() failed";
Elly Jones9aa5eca2011-11-04 14:48:13 -040054}
55
56// @brief Launch all our helper programs.
57void launch_helpers() {
Elly Jonese7cb5b32011-12-01 14:18:32 -050058 for (int i = 0; kHelpers[i]; ++i) {
59 chromeos::ProcessImpl p;
60 p.AddArg(kHelpers[i]);
61 p.Start();
62 p.Release();
Elly Jones9aa5eca2011-11-04 14:48:13 -040063 }
64}
65
Elly Jonese7cb5b32011-12-01 14:18:32 -050066// @brief Start the debugd DBus interface.
67void start() {
68 DBus::BusDispatcher dispatcher;
69 DBus::default_dispatcher = &dispatcher;
70 DBus::Connection conn = DBus::Connection::SystemBus();
71 debugd::DebugDaemon debugd(&conn, &dispatcher);
72 if (!debugd.Init())
73 LOG(FATAL) << "debugd.Init() failed";
74 debugd.Run();
75 LOG(FATAL) << "debugd.Run() returned";
Elly Jones9aa5eca2011-11-04 14:48:13 -040076}
Elly Jonese7cb5b32011-12-01 14:18:32 -050077}; // namespace
Elly Jones9aa5eca2011-11-04 14:48:13 -040078
Elly Jonese7cb5b32011-12-01 14:18:32 -050079int __attribute__((visibility("default"))) main(int argc, char* argv[]) {
80 CommandLine::Init(argc, argv);
81 chromeos::InitLog(chromeos::kLogToSyslog | chromeos::kLogToStderr);
82 enter_vfs_namespace();
Elly Jones9aa5eca2011-11-04 14:48:13 -040083 make_tmpfs();
Elly Jonese7cb5b32011-12-01 14:18:32 -050084 enter_sandbox();
Elly Jones9aa5eca2011-11-04 14:48:13 -040085 launch_helpers();
Elly Jonese7cb5b32011-12-01 14:18:32 -050086 start();
Elly Jones9aa5eca2011-11-04 14:48:13 -040087 return 0;
88}