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> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 11 | #include <brillo/daemons/dbus_daemon.h> |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame] | 12 | #include <brillo/syslog_logging.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 14 | #include <chromeos/libminijail.h> |
| 15 | |
Eric Caruso | 63c9743 | 2017-04-27 13:23:28 -0700 | [diff] [blame] | 16 | #include "debugd/src/debugd_dbus_adaptor.h" |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 17 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 18 | namespace { |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 19 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 20 | // @brief Enter a VFS namespace. |
| 21 | // |
| 22 | // We don't want anyone other than our descendants to see our tmpfs. |
| 23 | void enter_vfs_namespace() { |
| 24 | struct minijail* j = minijail_new(); |
| 25 | minijail_namespace_vfs(j); |
| 26 | minijail_enter(j); |
| 27 | minijail_destroy(j); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 28 | } |
| 29 | |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 30 | // @brief Sets up a tmpfs visible to this program and its descendants. |
| 31 | // |
| 32 | // The created tmpfs is mounted at /debugd. |
| 33 | void make_tmpfs() { |
| 34 | int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC, |
Ben Chan | 64d19b2 | 2017-02-06 14:03:47 -0800 | [diff] [blame] | 35 | nullptr); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 36 | if (r < 0) |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 37 | PLOG(FATAL) << "mount() failed"; |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 40 | // @brief Sets up directories needed by helper programs. |
| 41 | // |
| 42 | void setup_dirs() { |
| 43 | int r = mkdir("/debugd/touchpad", S_IRWXU); |
| 44 | if (r < 0) |
| 45 | PLOG(FATAL) << "mkdir(\"/debugd/touchpad\") failed"; |
| 46 | } |
| 47 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 48 | class Daemon : public brillo::DBusServiceDaemon { |
| 49 | public: |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame^] | 50 | Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName) {} |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 51 | |
| 52 | protected: |
| 53 | void RegisterDBusObjectsAsync( |
Eric Caruso | b298bb4 | 2017-05-09 10:53:30 -0700 | [diff] [blame] | 54 | brillo::dbus_utils::AsyncEventSequencer* sequencer) override { |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame^] | 55 | adaptor_.reset(new debugd::DebugdDBusAdaptor(bus_)); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 56 | 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 Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 65 | |
| 66 | } // namespace |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 67 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 68 | int __attribute__((visibility("default"))) main(int argc, char* argv[]) { |
Alex Vakulenko | 274c74a | 2015-04-02 14:31:10 -0700 | [diff] [blame] | 69 | base::CommandLine::Init(argc, argv); |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame] | 70 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 71 | enter_vfs_namespace(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 72 | make_tmpfs(); |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 73 | setup_dirs(); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 74 | Daemon().Run(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 75 | return 0; |
| 76 | } |