blob: d1e05b9eefb9a45f2c880db7560ba5064a8e626c [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2013 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 */
16
17#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
Tom Cherry0b2a0112019-06-06 13:41:20 -070020#include <linux/capability.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070021#include <poll.h>
Mark Salyzyn882f8562013-12-26 15:13:36 -080022#include <sched.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070023#include <semaphore.h>
24#include <signal.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/capability.h>
Mark Salyzyneb06de72014-10-13 09:59:37 -070029#include <sys/klog.h>
Elliott Hughese5a0f202014-07-18 17:39:41 -070030#include <sys/prctl.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070031#include <sys/resource.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080032#include <sys/stat.h>
33#include <sys/types.h>
Mark Salyzynccbadc62015-03-12 12:25:35 -070034#include <syslog.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080035#include <unistd.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
Mark Salyzynd5600fd2015-06-12 14:59:42 -070037#include <memory>
38
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040039#include <android-base/macros.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080040#include <cutils/android_get_control_file.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080041#include <cutils/properties.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070042#include <cutils/sockets.h>
Mark Salyzynff32f3c2015-04-13 14:24:45 -070043#include <log/event_tag_map.h>
William Robertsaeca97b2015-07-31 13:10:36 -070044#include <packagelistparser/packagelistparser.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070045#include <private/android_filesystem_config.h>
Mark Salyzyn5740a462016-03-28 15:42:08 -070046#include <private/android_logger.h>
Suren Baghdasaryan02843332018-12-21 12:30:16 -080047#include <processgroup/sched_policy.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070048#include <utils/threads.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080049
Mark Salyzyn0175b072014-02-26 09:50:16 -080050#include "CommandListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090051#include "LogAudit.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080052#include "LogBuffer.h"
Mark Salyzyna1aacb72014-10-15 08:49:39 -070053#include "LogKlog.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080054#include "LogListener.h"
Tom Cherry64e90162020-05-07 14:44:43 -070055#include "LogStatistics.h"
Tom Cherry1a12ae32020-05-01 16:13:18 -070056#include "LogTags.h"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070057#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080058
Mark Salyzyn501c3732017-03-10 14:31:54 -080059#define KMSG_PRIORITY(PRI) \
60 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
61 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070062
Tom Cherry0b2a0112019-06-06 13:41:20 -070063// The service is designed to be run by init, it does not respond well to starting up manually. Init
64// has a 'sigstop' feature that sends SIGSTOP to a service immediately before calling exec(). This
65// allows debuggers, etc to be attached to logd at the very beginning, while still having init
66// handle the user, groups, capabilities, files, etc setup.
Mark Salyzynd2b32912016-10-28 15:11:46 -070067static int drop_privs(bool klogd, bool auditd) {
Elliott Hughescef62b42018-06-13 10:33:45 -070068 sched_param param = {};
Mark Salyzyn882f8562013-12-26 15:13:36 -080069
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080070 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070071 android::prdebug("failed to set background scheduling policy");
Elliott Hughescef62b42018-06-13 10:33:45 -070072 return -1;
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080073 }
74
Mark Salyzyn501c3732017-03-10 14:31:54 -080075 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070076 android::prdebug("failed to set batch scheduler");
Elliott Hughescef62b42018-06-13 10:33:45 -070077 return -1;
Mark Salyzyn882f8562013-12-26 15:13:36 -080078 }
79
Mark Salyzyn4b42ea52018-08-15 12:17:18 -070080 if (!__android_logger_property_get_bool("ro.debuggable",
81 BOOL_DEFAULT_FALSE) &&
Elliott Hughescef62b42018-06-13 10:33:45 -070082 prctl(PR_SET_DUMPABLE, 0) == -1) {
Mark Salyzyn6a70ded2016-10-28 14:49:53 -070083 android::prdebug("failed to clear PR_SET_DUMPABLE");
84 return -1;
85 }
86
Tom Cherry0b2a0112019-06-06 13:41:20 -070087 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free);
88 if (cap_clear(caps.get()) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080089 return -1;
90 }
Tom Cherry0b2a0112019-06-06 13:41:20 -070091 std::vector<cap_value_t> cap_value;
92 if (klogd) {
93 cap_value.emplace_back(CAP_SYSLOG);
94 }
95 if (auditd) {
96 cap_value.emplace_back(CAP_AUDIT_CONTROL);
97 }
98
99 if (cap_set_flag(caps.get(), CAP_PERMITTED, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
100 return -1;
101 }
102 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800103 return -1;
104 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700105 if (cap_set_proc(caps.get()) < 0) {
Tom Cherry0b2a0112019-06-06 13:41:20 -0700106 android::prdebug("failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno);
Elliott Hughescef62b42018-06-13 10:33:45 -0700107 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700108 }
109
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110 return 0;
111}
112
Mark Salyzynccbadc62015-03-12 12:25:35 -0700113static int fdDmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800114void android::prdebug(const char* fmt, ...) {
Mark Salyzynd048f112016-02-08 10:28:12 -0800115 if (fdDmesg < 0) {
116 return;
117 }
118
119 static const char message[] = {
120 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
121 };
122 char buffer[256];
123 memcpy(buffer, message, sizeof(message));
124
125 va_list ap;
126 va_start(ap, fmt);
127 int n = vsnprintf(buffer + sizeof(message),
128 sizeof(buffer) - sizeof(message), fmt, ap);
129 va_end(ap);
130 if (n > 0) {
131 buffer[sizeof(buffer) - 1] = '\0';
132 if (!strchr(buffer, '\n')) {
133 buffer[sizeof(buffer) - 2] = '\0';
134 strlcat(buffer, "\n", sizeof(buffer));
135 }
136 write(fdDmesg, buffer, strlen(buffer));
137 }
138}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700139
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140char* android::uidToName(uid_t u) {
Tom Cherry36f53992017-09-06 10:07:37 -0700141 struct Userdata {
142 uid_t uid;
143 char* name;
144 } userdata = {
145 .uid = u,
146 .name = nullptr,
147 };
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700148
Tom Cherry36f53992017-09-06 10:07:37 -0700149 packagelist_parse(
150 [](pkg_info* info, void* callback_parameter) {
151 auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
152 bool result = true;
153 if (info->uid == userdata->uid) {
154 userdata->name = strdup(info->name);
155 // false to stop processing
156 result = false;
157 }
158 packagelist_free(info);
159 return result;
160 },
161 &userdata);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700162
Tom Cherry36f53992017-09-06 10:07:37 -0700163 return userdata.name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700164}
165
Mark Salyzyn501c3732017-03-10 14:31:54 -0800166static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700167 if (!al && !kl) {
168 return;
169 }
170
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700171 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700172 if (rc <= 0) {
173 return;
174 }
175
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700176 // Margin for additional input race or trailing nul
177 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800178 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700179
180 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
181 if (rc <= 0) {
182 return;
183 }
184
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700185 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700186 len = rc + 1;
187 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700188 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700189
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700190 ssize_t sublen;
191 for (char *ptr = nullptr, *tok = buf.get();
192 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
193 tok = nullptr) {
194 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700195 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700196 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700197 }
198 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700199 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700200 }
201 }
202}
203
Mark Salyzynd8f01802016-10-31 13:49:44 -0700204static int issueReinit() {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800205 int sock = TEMP_FAILURE_RETRY(socket_local_client(
206 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700207 if (sock < 0) return -errno;
208
209 static const char reinitStr[] = "reinit";
210 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
211 if (ret < 0) return -errno;
212
213 struct pollfd p;
214 memset(&p, 0, sizeof(p));
215 p.fd = sock;
216 p.events = POLLIN;
217 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
218 if (ret < 0) return -errno;
219 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
220
221 static const char success[] = "success";
222 char buffer[sizeof(success) - 1];
223 memset(buffer, 0, sizeof(buffer));
224 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
225 if (ret < 0) return -errno;
226
227 return strncmp(buffer, success, sizeof(success) - 1) != 0;
228}
229
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700230// Foreground waits for exit of the main persistent threads
231// that are started here. The threads are created to manage
232// UNIX domain client sockets for writing, reading and
233// controlling the user space logger, and for any additional
234// logging plugins like auditd and restart control. Additional
235// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800236int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900237 // logd is written under the assumption that the timezone is UTC.
238 // If TZ is not set, persist.sys.timezone is looked up in some time utility
239 // libc functions, including mktime. It confuses the logd time handling,
240 // so here explicitly set TZ to UTC, which overrides the property.
241 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700242 // issue reinit command. KISS argument parsing.
243 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700244 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700245 }
246
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700247 static const char dev_kmsg[] = "/dev/kmsg";
248 fdDmesg = android_get_control_file(dev_kmsg);
249 if (fdDmesg < 0) {
250 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
251 }
252
253 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800254 bool klogd = __android_logger_property_get_bool(
Siarhei Vishniakoue8ed36b2017-12-28 14:13:22 -0800255 "ro.logd.kernel",
256 BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700257 if (klogd) {
258 static const char proc_kmsg[] = "/proc/kmsg";
259 fdPmesg = android_get_control_file(proc_kmsg);
260 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800261 fdPmesg = TEMP_FAILURE_RETRY(
262 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700263 }
264 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
265 }
266
Tom Cherry0b2a0112019-06-06 13:41:20 -0700267 bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
268 if (drop_privs(klogd, auditd) != 0) {
269 return EXIT_FAILURE;
270 }
271
Tom Cherry1a12ae32020-05-01 16:13:18 -0700272 // A cache of event log tags
273 LogTags log_tags;
Tom Cherry5a3db392020-05-01 17:03:20 -0700274 // Pruning configuration.
275 PruneList prune_list;
Tom Cherry64e90162020-05-07 14:44:43 -0700276 // Partial (required for chatty) or full logging statistics.
277 bool enable_full_log_statistics = __android_logger_property_get_bool(
278 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
279 BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
280 LogStatistics log_statistics(enable_full_log_statistics);
Tom Cherry1a12ae32020-05-01 16:13:18 -0700281
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282 // Serves the purpose of managing the last logs times read on a
283 // socket connection, and as a reader lock on a range of log
284 // entries.
285
Mark Salyzyn501c3732017-03-10 14:31:54 -0800286 LastLogTimes* times = new LastLogTimes();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800287
288 // LogBuffer is the object which is responsible for holding all
289 // log entries.
290
Tom Cherry64e90162020-05-07 14:44:43 -0700291 LogBuffer* logBuf = new LogBuffer(times, &log_tags, &prune_list, &log_statistics);
Mark Salyzyne457b742014-02-19 17:18:31 -0800292
Mark Salyzyn0175b072014-02-26 09:50:16 -0800293 // LogReader listens on /dev/socket/logdr. When a client
294 // connects, log entries in the LogBuffer are written to the client.
295
Mark Salyzyn501c3732017-03-10 14:31:54 -0800296 LogReader* reader = new LogReader(logBuf);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800297 if (reader->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700298 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800299 }
300
301 // LogListener listens on /dev/socket/logdw for client
302 // initiated log messages. New log entries are added to LogBuffer
303 // and LogReader is notified to send updates to connected clients.
304
Mark Salyzyn501c3732017-03-10 14:31:54 -0800305 LogListener* swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800306 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700307 if (swl->startListener(600)) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700308 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800309 }
310
311 // Command listener listens on /dev/socket/logd for incoming logd
312 // administrative commands.
313
Tom Cherry64e90162020-05-07 14:44:43 -0700314 CommandListener* cl = new CommandListener(logBuf, &log_tags, &prune_list, &log_statistics);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800315 if (cl->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700316 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800317 }
318
William Roberts29d238d2013-02-08 09:45:26 +0900319 // LogAudit listens on NETLINK_AUDIT socket for selinux
320 // initiated log messages. New log entries are added to LogBuffer
321 // and LogReader is notified to send updates to connected clients.
322
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700323 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000324 if (auditd) {
Tom Cherry64e90162020-05-07 14:44:43 -0700325 al = new LogAudit(
326 logBuf, reader,
327 __android_logger_property_get_bool("ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
328 ? fdDmesg
329 : -1,
330 &log_statistics);
Sami Tolvanena742d102016-06-14 18:04:43 +0000331 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700332
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700333 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700334 if (klogd) {
Tom Cherry64e90162020-05-07 14:44:43 -0700335 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != nullptr, &log_statistics);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700336 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700337
Sami Tolvanena742d102016-06-14 18:04:43 +0000338 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700339
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700340 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700341
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700342 if (kl && kl->startListener()) {
343 delete kl;
344 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700345
Sami Tolvanena742d102016-06-14 18:04:43 +0000346 if (al && al->startListener()) {
347 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900348 }
349
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700350 TEMP_FAILURE_RETRY(pause());
351
Elliott Hughescef62b42018-06-13 10:33:45 -0700352 return EXIT_SUCCESS;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800353}