blob: 5199f2f5297c8c98daa6251c342afaee71586a4e [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Peter Qiu5dd242d2014-10-14 12:23:21 -070016
17#include <vector>
18
19#include <base/bind.h>
20#include <base/command_line.h>
21#include <base/logging.h>
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070022#include <brillo/minijail/minijail.h>
23#include <brillo/syslog_logging.h>
Peter Qiu5dd242d2014-10-14 12:23:21 -070024
25#include "apmanager/daemon.h"
26
27using std::vector;
28
29namespace {
30
31namespace switches {
32
33// Don't daemon()ize; run in foreground.
34const char kForeground[] = "foreground";
35// Flag that causes apmanager to show the help message and exit.
36const char kHelp[] = "help";
37
38// The help message shown if help flag is passed to the program.
39const char kHelpMessage[] = "\n"
40 "Available Switches: \n"
41 " --foreground\n"
42 " Don\'t daemon()ize; run in foreground.\n";
43} // namespace switches
44
45} // namespace
46
47namespace {
48
Peter Qiu771952d2015-09-28 08:08:24 -070049#if !defined(__ANDROID__)
Peter Qiu5dd242d2014-10-14 12:23:21 -070050const char kLoggerCommand[] = "/usr/bin/logger";
51const char kLoggerUser[] = "syslog";
Peter Qiu771952d2015-09-28 08:08:24 -070052#endif // __ANDROID__
53
Peter Qiu58676b62014-12-16 16:47:12 -080054const char kSeccompFilePath[] = "/usr/share/policy/apmanager-seccomp.policy";
Peter Qiu5dd242d2014-10-14 12:23:21 -070055
56} // namespace
57
58// Always logs to the syslog and logs to stderr if
59// we are running in the foreground.
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070060void SetupLogging(brillo::Minijail* minijail,
Peter Qiu5dd242d2014-10-14 12:23:21 -070061 bool foreground,
62 const char* daemon_name) {
63 int log_flags = 0;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070064 log_flags |= brillo::kLogToSyslog;
65 log_flags |= brillo::kLogHeader;
Peter Qiu5dd242d2014-10-14 12:23:21 -070066 if (foreground) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070067 log_flags |= brillo::kLogToStderr;
Peter Qiu5dd242d2014-10-14 12:23:21 -070068 }
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070069 brillo::InitLog(log_flags);
Peter Qiu5dd242d2014-10-14 12:23:21 -070070
Peter Qiu771952d2015-09-28 08:08:24 -070071#if !defined(__ANDROID__)
72 // Logger utility doesn't exist on Android, so do not run it on Android.
73 // TODO(zqiu): add support to redirect stderr logs from child processes
74 // to Android logging facility.
Peter Qiu5dd242d2014-10-14 12:23:21 -070075 if (!foreground) {
76 vector<char*> logger_command_line;
77 int logger_stdin_fd;
78 logger_command_line.push_back(const_cast<char*>(kLoggerCommand));
79 logger_command_line.push_back(const_cast<char*>("--priority"));
80 logger_command_line.push_back(const_cast<char*>("daemon.err"));
81 logger_command_line.push_back(const_cast<char*>("--tag"));
82 logger_command_line.push_back(const_cast<char*>(daemon_name));
83 logger_command_line.push_back(nullptr);
84
85 struct minijail* jail = minijail->New();
86 minijail->DropRoot(jail, kLoggerUser, kLoggerUser);
87
88 if (!minijail->RunPipeAndDestroy(jail, logger_command_line,
89 nullptr, &logger_stdin_fd)) {
90 LOG(ERROR) << "Unable to spawn logger. "
91 << "Writes to stderr will be discarded.";
92 return;
93 }
94
95 // Note that we don't set O_CLOEXEC here. This means that stderr
96 // from any child processes will, by default, be logged to syslog.
97 if (dup2(logger_stdin_fd, fileno(stderr)) != fileno(stderr)) {
98 LOG(ERROR) << "Failed to redirect stderr to syslog: "
99 << strerror(errno);
100 }
101 close(logger_stdin_fd);
102 }
Peter Qiu771952d2015-09-28 08:08:24 -0700103#endif // __ANDROID__
Peter Qiu5dd242d2014-10-14 12:23:21 -0700104}
105
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700106void DropPrivileges(brillo::Minijail* minijail) {
Peter Qiu5dd242d2014-10-14 12:23:21 -0700107 struct minijail* jail = minijail->New();
108 minijail->DropRoot(jail, apmanager::Daemon::kAPManagerUserName,
109 apmanager::Daemon::kAPManagerGroupName);
Peter Qiu58676b62014-12-16 16:47:12 -0800110 // Permissions needed for the daemon and its child processes for managing
111 // network interfaces and binding to network sockets.
112 minijail->UseCapabilities(jail, CAP_TO_MASK(CAP_NET_ADMIN) |
113 CAP_TO_MASK(CAP_NET_RAW) |
114 CAP_TO_MASK(CAP_NET_BIND_SERVICE));
115 minijail->UseSeccompFilter(jail, kSeccompFilePath);
Peter Qiu5dd242d2014-10-14 12:23:21 -0700116 minijail_enter(jail);
117 minijail->Destroy(jail);
118}
119
Alex Vakulenko7f267ab2015-04-02 14:31:10 -0700120void OnStartup(const char* daemon_name, base::CommandLine* cl) {
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700121 brillo::Minijail* minijail = brillo::Minijail::GetInstance();
Peter Qiu5dd242d2014-10-14 12:23:21 -0700122 SetupLogging(minijail, cl->HasSwitch(switches::kForeground), daemon_name);
123
124 LOG(INFO) << __func__ << ": Dropping privileges";
125
Peter Qiu56e03e72015-09-28 13:42:48 -0700126 // TODO(zqiu): apmanager is currently started as the "system" user on Android,
127 // so there is no need to drop privileges to the "system" user again.
128 // Drop user privileges when we're running apmanager under a different
129 // user/group.
130#if !defined(__ANDROID__)
Peter Qiu5dd242d2014-10-14 12:23:21 -0700131 // Now that the daemon has all the resources it needs to run, we can drop
132 // privileges further.
Peter Qiu58676b62014-12-16 16:47:12 -0800133 DropPrivileges(minijail);
Peter Qiu56e03e72015-09-28 13:42:48 -0700134#endif // __ANDROID
Peter Qiu5dd242d2014-10-14 12:23:21 -0700135}
136
137int main(int argc, char* argv[]) {
Alex Vakulenko7f267ab2015-04-02 14:31:10 -0700138 base::CommandLine::Init(argc, argv);
139 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
Peter Qiu5dd242d2014-10-14 12:23:21 -0700140
141 if (cl->HasSwitch(switches::kHelp)) {
142 LOG(INFO) << switches::kHelpMessage;
143 return 0;
144 }
145
Peter Qiu5dd242d2014-10-14 12:23:21 -0700146 apmanager::Daemon daemon(base::Bind(&OnStartup, argv[0], cl));
147
148 daemon.Run();
149
150 return 0;
151}