blob: dfbad31fac6e19ce1cc4f88f23c8b475e0fb89ef [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
Tom Cherryd5b38382020-05-12 13:16:41 -070050#include "ChattyLogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080051#include "CommandListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090052#include "LogAudit.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080053#include "LogBuffer.h"
Mark Salyzyna1aacb72014-10-15 08:49:39 -070054#include "LogKlog.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080055#include "LogListener.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070056#include "LogReader.h"
Tom Cherry64e90162020-05-07 14:44:43 -070057#include "LogStatistics.h"
Tom Cherry1a12ae32020-05-01 16:13:18 -070058#include "LogTags.h"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070059#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080060
Mark Salyzyn501c3732017-03-10 14:31:54 -080061#define KMSG_PRIORITY(PRI) \
62 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
63 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070064
Tom Cherry0b2a0112019-06-06 13:41:20 -070065// The service is designed to be run by init, it does not respond well to starting up manually. Init
66// has a 'sigstop' feature that sends SIGSTOP to a service immediately before calling exec(). This
67// allows debuggers, etc to be attached to logd at the very beginning, while still having init
68// handle the user, groups, capabilities, files, etc setup.
Mark Salyzynd2b32912016-10-28 15:11:46 -070069static int drop_privs(bool klogd, bool auditd) {
Elliott Hughescef62b42018-06-13 10:33:45 -070070 sched_param param = {};
Mark Salyzyn882f8562013-12-26 15:13:36 -080071
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080072 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070073 android::prdebug("failed to set background scheduling policy");
Elliott Hughescef62b42018-06-13 10:33:45 -070074 return -1;
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080075 }
76
Mark Salyzyn501c3732017-03-10 14:31:54 -080077 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070078 android::prdebug("failed to set batch scheduler");
Elliott Hughescef62b42018-06-13 10:33:45 -070079 return -1;
Mark Salyzyn882f8562013-12-26 15:13:36 -080080 }
81
Mark Salyzyn4b42ea52018-08-15 12:17:18 -070082 if (!__android_logger_property_get_bool("ro.debuggable",
83 BOOL_DEFAULT_FALSE) &&
Elliott Hughescef62b42018-06-13 10:33:45 -070084 prctl(PR_SET_DUMPABLE, 0) == -1) {
Mark Salyzyn6a70ded2016-10-28 14:49:53 -070085 android::prdebug("failed to clear PR_SET_DUMPABLE");
86 return -1;
87 }
88
Tom Cherry0b2a0112019-06-06 13:41:20 -070089 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free);
90 if (cap_clear(caps.get()) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080091 return -1;
92 }
Tom Cherry0b2a0112019-06-06 13:41:20 -070093 std::vector<cap_value_t> cap_value;
94 if (klogd) {
95 cap_value.emplace_back(CAP_SYSLOG);
96 }
97 if (auditd) {
98 cap_value.emplace_back(CAP_AUDIT_CONTROL);
99 }
100
101 if (cap_set_flag(caps.get(), CAP_PERMITTED, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
102 return -1;
103 }
104 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800105 return -1;
106 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700107 if (cap_set_proc(caps.get()) < 0) {
Tom Cherry0b2a0112019-06-06 13:41:20 -0700108 android::prdebug("failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno);
Elliott Hughescef62b42018-06-13 10:33:45 -0700109 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700110 }
111
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112 return 0;
113}
114
Mark Salyzynccbadc62015-03-12 12:25:35 -0700115static int fdDmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800116void android::prdebug(const char* fmt, ...) {
Mark Salyzynd048f112016-02-08 10:28:12 -0800117 if (fdDmesg < 0) {
118 return;
119 }
120
121 static const char message[] = {
122 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
123 };
124 char buffer[256];
125 memcpy(buffer, message, sizeof(message));
126
127 va_list ap;
128 va_start(ap, fmt);
129 int n = vsnprintf(buffer + sizeof(message),
130 sizeof(buffer) - sizeof(message), fmt, ap);
131 va_end(ap);
132 if (n > 0) {
133 buffer[sizeof(buffer) - 1] = '\0';
134 if (!strchr(buffer, '\n')) {
135 buffer[sizeof(buffer) - 2] = '\0';
136 strlcat(buffer, "\n", sizeof(buffer));
137 }
138 write(fdDmesg, buffer, strlen(buffer));
139 }
140}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700141
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142char* android::uidToName(uid_t u) {
Tom Cherry36f53992017-09-06 10:07:37 -0700143 struct Userdata {
144 uid_t uid;
145 char* name;
146 } userdata = {
147 .uid = u,
148 .name = nullptr,
149 };
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700150
Tom Cherry36f53992017-09-06 10:07:37 -0700151 packagelist_parse(
152 [](pkg_info* info, void* callback_parameter) {
153 auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
154 bool result = true;
155 if (info->uid == userdata->uid) {
156 userdata->name = strdup(info->name);
157 // false to stop processing
158 result = false;
159 }
160 packagelist_free(info);
161 return result;
162 },
163 &userdata);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700164
Tom Cherry36f53992017-09-06 10:07:37 -0700165 return userdata.name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700166}
167
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700169 if (!al && !kl) {
170 return;
171 }
172
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700173 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700174 if (rc <= 0) {
175 return;
176 }
177
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700178 // Margin for additional input race or trailing nul
179 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800180 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700181
182 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
183 if (rc <= 0) {
184 return;
185 }
186
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700187 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700188 len = rc + 1;
189 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700190 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700191
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700192 ssize_t sublen;
193 for (char *ptr = nullptr, *tok = buf.get();
194 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
195 tok = nullptr) {
196 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700197 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700198 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700199 }
200 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700201 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700202 }
203 }
204}
205
Mark Salyzynd8f01802016-10-31 13:49:44 -0700206static int issueReinit() {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800207 int sock = TEMP_FAILURE_RETRY(socket_local_client(
208 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700209 if (sock < 0) return -errno;
210
211 static const char reinitStr[] = "reinit";
212 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
213 if (ret < 0) return -errno;
214
215 struct pollfd p;
216 memset(&p, 0, sizeof(p));
217 p.fd = sock;
218 p.events = POLLIN;
219 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
220 if (ret < 0) return -errno;
221 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
222
223 static const char success[] = "success";
224 char buffer[sizeof(success) - 1];
225 memset(buffer, 0, sizeof(buffer));
226 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
227 if (ret < 0) return -errno;
228
229 return strncmp(buffer, success, sizeof(success) - 1) != 0;
230}
231
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700232// Foreground waits for exit of the main persistent threads
233// that are started here. The threads are created to manage
234// UNIX domain client sockets for writing, reading and
235// controlling the user space logger, and for any additional
236// logging plugins like auditd and restart control. Additional
237// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800238int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900239 // logd is written under the assumption that the timezone is UTC.
240 // If TZ is not set, persist.sys.timezone is looked up in some time utility
241 // libc functions, including mktime. It confuses the logd time handling,
242 // so here explicitly set TZ to UTC, which overrides the property.
243 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700244 // issue reinit command. KISS argument parsing.
245 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700246 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700247 }
248
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700249 static const char dev_kmsg[] = "/dev/kmsg";
250 fdDmesg = android_get_control_file(dev_kmsg);
251 if (fdDmesg < 0) {
252 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
253 }
254
255 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800256 bool klogd = __android_logger_property_get_bool(
Siarhei Vishniakoue8ed36b2017-12-28 14:13:22 -0800257 "ro.logd.kernel",
258 BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700259 if (klogd) {
260 static const char proc_kmsg[] = "/proc/kmsg";
261 fdPmesg = android_get_control_file(proc_kmsg);
262 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800263 fdPmesg = TEMP_FAILURE_RETRY(
264 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700265 }
266 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
267 }
268
Tom Cherry0b2a0112019-06-06 13:41:20 -0700269 bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
270 if (drop_privs(klogd, auditd) != 0) {
271 return EXIT_FAILURE;
272 }
273
Tom Cherry1a12ae32020-05-01 16:13:18 -0700274 // A cache of event log tags
275 LogTags log_tags;
Tom Cherry68630a02020-05-11 16:29:29 -0700276
Tom Cherry5a3db392020-05-01 17:03:20 -0700277 // Pruning configuration.
278 PruneList prune_list;
Tom Cherry68630a02020-05-11 16:29:29 -0700279
Tom Cherry64e90162020-05-07 14:44:43 -0700280 // Partial (required for chatty) or full logging statistics.
281 bool enable_full_log_statistics = __android_logger_property_get_bool(
282 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
283 BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
284 LogStatistics log_statistics(enable_full_log_statistics);
Tom Cherry1a12ae32020-05-01 16:13:18 -0700285
Mark Salyzyn0175b072014-02-26 09:50:16 -0800286 // Serves the purpose of managing the last logs times read on a
287 // socket connection, and as a reader lock on a range of log
288 // entries.
Tom Cherry68630a02020-05-11 16:29:29 -0700289 LogReaderList reader_list;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800290
291 // LogBuffer is the object which is responsible for holding all
292 // log entries.
Tom Cherryd5b38382020-05-12 13:16:41 -0700293 LogBuffer* logBuf = new ChattyLogBuffer(&reader_list, &log_tags, &prune_list, &log_statistics);
Mark Salyzyne457b742014-02-19 17:18:31 -0800294
Mark Salyzyn0175b072014-02-26 09:50:16 -0800295 // LogReader listens on /dev/socket/logdr. When a client
296 // connects, log entries in the LogBuffer are written to the client.
Tom Cherry68630a02020-05-11 16:29:29 -0700297 LogReader* reader = new LogReader(logBuf, &reader_list);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800298 if (reader->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700299 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800300 }
301
302 // LogListener listens on /dev/socket/logdw for client
303 // initiated log messages. New log entries are added to LogBuffer
304 // and LogReader is notified to send updates to connected clients.
Tom Cherry68630a02020-05-11 16:29:29 -0700305 LogListener* swl = new LogListener(logBuf);
Tom Cherryc64dd8e2020-05-06 12:04:09 -0700306 if (!swl->StartListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700307 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800308 }
309
310 // Command listener listens on /dev/socket/logd for incoming logd
311 // administrative commands.
Tom Cherry64e90162020-05-07 14:44:43 -0700312 CommandListener* cl = new CommandListener(logBuf, &log_tags, &prune_list, &log_statistics);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800313 if (cl->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700314 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800315 }
316
William Roberts29d238d2013-02-08 09:45:26 +0900317 // LogAudit listens on NETLINK_AUDIT socket for selinux
318 // initiated log messages. New log entries are added to LogBuffer
319 // and LogReader is notified to send updates to connected clients.
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700320 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000321 if (auditd) {
Tom Cherry68630a02020-05-11 16:29:29 -0700322 int dmesg_fd = __android_logger_property_get_bool("ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
323 ? fdDmesg
324 : -1;
325 al = new LogAudit(logBuf, dmesg_fd, &log_statistics);
Sami Tolvanena742d102016-06-14 18:04:43 +0000326 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700327
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700328 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700329 if (klogd) {
Tom Cherry68630a02020-05-11 16:29:29 -0700330 kl = new LogKlog(logBuf, fdDmesg, fdPmesg, al != nullptr, &log_statistics);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700331 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700332
Sami Tolvanena742d102016-06-14 18:04:43 +0000333 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700334
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700335 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700336 if (kl && kl->startListener()) {
337 delete kl;
338 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700339
Sami Tolvanena742d102016-06-14 18:04:43 +0000340 if (al && al->startListener()) {
341 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900342 }
343
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700344 TEMP_FAILURE_RETRY(pause());
345
Elliott Hughescef62b42018-06-13 10:33:45 -0700346 return EXIT_SUCCESS;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800347}