Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 5 | #include <sys/mount.h> |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 6 | #include <sys/stat.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 9 | #include <base/command_line.h> |
| 10 | #include <base/logging.h> |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame^] | 11 | #include <brillo/process.h> |
| 12 | #include <brillo/syslog_logging.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 13 | #include <chromeos/libminijail.h> |
| 14 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 15 | #include "debugd/src/debug_daemon.h" |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 16 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 17 | namespace { |
| 18 | const char* kHelpers[] = { |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 19 | NULL, |
| 20 | }; |
| 21 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 22 | // @brief Enter a VFS namespace. |
| 23 | // |
| 24 | // We don't want anyone other than our descendants to see our tmpfs. |
| 25 | void enter_vfs_namespace() { |
| 26 | struct minijail* j = minijail_new(); |
| 27 | minijail_namespace_vfs(j); |
| 28 | minijail_enter(j); |
| 29 | minijail_destroy(j); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 32 | // @brief Sets up a tmpfs visible to this program and its descendants. |
| 33 | // |
| 34 | // The created tmpfs is mounted at /debugd. |
| 35 | void make_tmpfs() { |
| 36 | int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC, |
| 37 | NULL); |
| 38 | if (r < 0) |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 39 | PLOG(FATAL) << "mount() failed"; |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 42 | // @brief Sets up directories needed by helper programs. |
| 43 | // |
| 44 | void setup_dirs() { |
| 45 | int r = mkdir("/debugd/touchpad", S_IRWXU); |
| 46 | if (r < 0) |
| 47 | PLOG(FATAL) << "mkdir(\"/debugd/touchpad\") failed"; |
| 48 | } |
| 49 | |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 50 | // @brief Launch all our helper programs. |
| 51 | void launch_helpers() { |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 52 | for (int i = 0; kHelpers[i]; ++i) { |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame^] | 53 | brillo::ProcessImpl p; |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 54 | p.AddArg(kHelpers[i]); |
| 55 | p.Start(); |
| 56 | p.Release(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 60 | // @brief Start the debugd DBus interface. |
| 61 | void 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 Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 70 | } |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 71 | }; // namespace |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 72 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 73 | int __attribute__((visibility("default"))) main(int argc, char* argv[]) { |
Alex Vakulenko | 274c74a | 2015-04-02 14:31:10 -0700 | [diff] [blame] | 74 | base::CommandLine::Init(argc, argv); |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame^] | 75 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 76 | enter_vfs_namespace(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 77 | make_tmpfs(); |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 78 | setup_dirs(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 79 | launch_helpers(); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 80 | start(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 81 | return 0; |
| 82 | } |