blob: 33345067c43473d0960175348efc974a27a8d430 [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>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070020#include <poll.h>
Mark Salyzyn882f8562013-12-26 15:13:36 -080021#include <sched.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070022#include <semaphore.h>
23#include <signal.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/capability.h>
Mark Salyzyneb06de72014-10-13 09:59:37 -070028#include <sys/klog.h>
Elliott Hughese5a0f202014-07-18 17:39:41 -070029#include <sys/prctl.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070030#include <sys/resource.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080031#include <sys/stat.h>
32#include <sys/types.h>
Mark Salyzynccbadc62015-03-12 12:25:35 -070033#include <syslog.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080034#include <unistd.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
William Robertsaeca97b2015-07-31 13:10:36 -070036#include <cstdbool>
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 Salyzyn56ba4b52015-01-30 15:19:48 -080042#include <cutils/sched_policy.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070043#include <cutils/sockets.h>
Mark Salyzynff32f3c2015-04-13 14:24:45 -070044#include <log/event_tag_map.h>
William Robertsaeca97b2015-07-31 13:10:36 -070045#include <packagelistparser/packagelistparser.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070046#include <private/android_filesystem_config.h>
Mark Salyzyn5740a462016-03-28 15:42:08 -070047#include <private/android_logger.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"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070055#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080056
Mark Salyzyn501c3732017-03-10 14:31:54 -080057#define KMSG_PRIORITY(PRI) \
58 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
59 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070060
Mark Salyzyndfc47e82014-03-24 10:26:47 -070061//
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040062// The service is designed to be run by init, it does not respond well
Mark Salyzyndfc47e82014-03-24 10:26:47 -070063// to starting up manually. When starting up manually the sockets will
64// fail to open typically for one of the following reasons:
65// EADDRINUSE if logger is running.
66// EACCESS if started without precautions (below)
67//
68// Here is a cookbook procedure for starting up logd manually assuming
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040069// init is out of the way, pedantically all permissions and SELinux
Mark Salyzyndfc47e82014-03-24 10:26:47 -070070// security is put back in place:
71//
72// setenforce 0
73// rm /dev/socket/logd*
74// chmod 777 /dev/socket
75// # here is where you would attach the debugger or valgrind for example
76// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
77// sleep 1
78// chmod 755 /dev/socket
79// chown logd.logd /dev/socket/logd*
80// restorecon /dev/socket/logd*
81// setenforce 1
82//
83// If minimalism prevails, typical for debugging and security is not a concern:
84//
85// setenforce 0
86// chmod 777 /dev/socket
87// logd
88//
89
Mark Salyzynd2b32912016-10-28 15:11:46 -070090static int drop_privs(bool klogd, bool auditd) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070091 // Tricky, if ro.build.type is "eng" then this is true because of the
92 // side effect that ro.debuggable == 1 as well, else it is false.
Mark Salyzyn501c3732017-03-10 14:31:54 -080093 bool eng =
94 __android_logger_property_get_bool("ro.build.type", BOOL_DEFAULT_FALSE);
Mark Salyzyn107e29a2016-10-28 15:51:03 -070095
Mark Salyzyn882f8562013-12-26 15:13:36 -080096 struct sched_param param;
97 memset(&param, 0, sizeof(param));
98
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080099 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700100 android::prdebug("failed to set background scheduling policy");
101 if (!eng) return -1;
Mark Salyzyn56ba4b52015-01-30 15:19:48 -0800102 }
103
Mark Salyzyn501c3732017-03-10 14:31:54 -0800104 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700105 android::prdebug("failed to set batch scheduler");
106 if (!eng) return -1;
Mark Salyzyn882f8562013-12-26 15:13:36 -0800107 }
108
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700109 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700110 android::prdebug("failed to set background cgroup");
111 if (!eng) return -1;
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700112 }
113
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700114 if (!eng && (prctl(PR_SET_DUMPABLE, 0) < 0)) {
Mark Salyzyn6a70ded2016-10-28 14:49:53 -0700115 android::prdebug("failed to clear PR_SET_DUMPABLE");
116 return -1;
117 }
118
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700119 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
120 android::prdebug("failed to set PR_SET_KEEPCAPS");
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700121 if (!eng) return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700122 }
123
Mark Salyzyn501c3732017-03-10 14:31:54 -0800124 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(),
125 cap_free);
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700126 if (cap_clear(caps.get()) < 0) return -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800127 cap_value_t cap_value[] = { CAP_SETGID, // must be first for below
128 klogd ? CAP_SYSLOG : CAP_SETGID,
129 auditd ? CAP_AUDIT_CONTROL : CAP_SETGID };
130 if (cap_set_flag(caps.get(), CAP_PERMITTED, arraysize(cap_value), cap_value,
131 CAP_SET) < 0) {
132 return -1;
133 }
134 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, arraysize(cap_value), cap_value,
135 CAP_SET) < 0) {
136 return -1;
137 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700138 if (cap_set_proc(caps.get()) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139 android::prdebug(
140 "failed to set CAP_SETGID, CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)",
141 errno);
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700142 if (!eng) return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700143 }
144
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800145 gid_t groups[] = { AID_READPROC };
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700146
147 if (setgroups(arraysize(groups), groups) == -1) {
148 android::prdebug("failed to set AID_READPROC groups");
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700149 if (!eng) return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700150 }
151
152 if (setgid(AID_LOGD) != 0) {
153 android::prdebug("failed to set AID_LOGD gid");
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700154 if (!eng) return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700155 }
156
157 if (setuid(AID_LOGD) != 0) {
158 android::prdebug("failed to set AID_LOGD uid");
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700159 if (!eng) return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700160 }
161
Mark Salyzyn501c3732017-03-10 14:31:54 -0800162 if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, cap_value, CAP_CLEAR) < 0) {
163 return -1;
164 }
165 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, cap_value, CAP_CLEAR) < 0) {
166 return -1;
167 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700168 if (cap_set_proc(caps.get()) < 0) {
169 android::prdebug("failed to clear CAP_SETGID (%d)", errno);
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700170 if (!eng) return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700171 }
172
Mark Salyzyn0175b072014-02-26 09:50:16 -0800173 return 0;
174}
175
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700176// Property helper
Mark Salyzyn501c3732017-03-10 14:31:54 -0800177static bool check_flag(const char* prop, const char* flag) {
178 const char* cp = strcasestr(prop, flag);
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800179 if (!cp) {
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700180 return false;
181 }
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800182 // We only will document comma (,)
183 static const char sep[] = ",:;|+ \t\f";
184 if ((cp != prop) && !strchr(sep, cp[-1])) {
185 return false;
186 }
187 cp += strlen(flag);
188 return !*cp || !!strchr(sep, *cp);
189}
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700190
Mark Salyzynccbadc62015-03-12 12:25:35 -0700191static int fdDmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800192void android::prdebug(const char* fmt, ...) {
Mark Salyzynd048f112016-02-08 10:28:12 -0800193 if (fdDmesg < 0) {
194 return;
195 }
196
197 static const char message[] = {
198 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
199 };
200 char buffer[256];
201 memcpy(buffer, message, sizeof(message));
202
203 va_list ap;
204 va_start(ap, fmt);
205 int n = vsnprintf(buffer + sizeof(message),
206 sizeof(buffer) - sizeof(message), fmt, ap);
207 va_end(ap);
208 if (n > 0) {
209 buffer[sizeof(buffer) - 1] = '\0';
210 if (!strchr(buffer, '\n')) {
211 buffer[sizeof(buffer) - 2] = '\0';
212 strlcat(buffer, "\n", sizeof(buffer));
213 }
214 write(fdDmesg, buffer, strlen(buffer));
215 }
216}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700217
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700218static sem_t uidName;
219static uid_t uid;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800220static char* name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700221
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700222static sem_t reinit;
223static bool reinit_running = false;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800224static LogBuffer* logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700225
Mark Salyzyn501c3732017-03-10 14:31:54 -0800226static bool package_list_parser_cb(pkg_info* info, void* /* userdata */) {
William Robertsaeca97b2015-07-31 13:10:36 -0700227 bool rc = true;
228 if (info->uid == uid) {
229 name = strdup(info->name);
230 // false to stop processing
231 rc = false;
232 }
233
234 packagelist_free(info);
235 return rc;
236}
237
Mark Salyzyn501c3732017-03-10 14:31:54 -0800238static void* reinit_thread_start(void* /*obj*/) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700239 prctl(PR_SET_NAME, "logd.daemon");
240 set_sched_policy(0, SP_BACKGROUND);
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700241 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700242
Mark Salyzynd8f01802016-10-31 13:49:44 -0700243 cap_t caps = cap_init();
244 (void)cap_clear(caps);
245 (void)cap_set_proc(caps);
246 (void)cap_free(caps);
247
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700248 // If we are AID_ROOT, we should drop to AID_LOGD+AID_SYSTEM, if we are
249 // anything else, we have even lesser privileges and accept our fate. Not
250 // worth checking for error returns setting this thread's privileges.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800251 (void)setgid(AID_SYSTEM); // readonly access to /data/system/packages.list
252 (void)setuid(AID_LOGD); // access to everything logd, eg /data/misc/logd
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700253
254 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700255 // uidToName Privileged Worker
256 if (uid) {
257 name = NULL;
258
William Robertsaeca97b2015-07-31 13:10:36 -0700259 packagelist_parse(package_list_parser_cb, NULL);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700260
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700261 uid = 0;
262 sem_post(&uidName);
263 continue;
264 }
265
Mark Salyzynccbadc62015-03-12 12:25:35 -0700266 if (fdDmesg >= 0) {
267 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn501c3732017-03-10 14:31:54 -0800268 'l',
269 'o',
270 'g',
271 'd',
272 '.',
273 'd',
274 'a',
275 'e',
276 'm',
277 'o',
278 'n',
279 ':',
280 ' ',
281 'r',
282 'e',
283 'i',
284 'n',
285 'i',
286 't',
287 '\n' };
Mark Salyzynccbadc62015-03-12 12:25:35 -0700288 write(fdDmesg, reinit_message, sizeof(reinit_message));
289 }
290
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700291 // Anything that reads persist.<property>
292 if (logBuf) {
293 logBuf->init();
Mark Salyzyn932f7ac2015-08-28 08:02:59 -0700294 logBuf->initPrune(NULL);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700295 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700296 android::ReReadEventLogTags();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700297 }
298
299 return NULL;
300}
301
Mark Salyzyn95108f12015-04-20 07:26:27 -0700302static sem_t sem_name;
303
Mark Salyzyn501c3732017-03-10 14:31:54 -0800304char* android::uidToName(uid_t u) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700305 if (!u || !reinit_running) {
306 return NULL;
307 }
308
Mark Salyzyn95108f12015-04-20 07:26:27 -0700309 sem_wait(&sem_name);
310
311 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700312 uid = u;
313
314 name = NULL;
315 sem_post(&reinit);
316 sem_wait(&uidName);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800317 char* ret = name;
Mark Salyzyn95108f12015-04-20 07:26:27 -0700318
319 sem_post(&sem_name);
320
321 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700322}
323
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700324// Serves as a global method to trigger reinitialization
325// and as a function that can be provided to signal().
326void reinit_signal_handler(int /*signal*/) {
327 sem_post(&reinit);
328}
329
Mark Salyzyn501c3732017-03-10 14:31:54 -0800330static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700331 if (!al && !kl) {
332 return;
333 }
334
Mark Salyzynea1a2412015-09-02 07:39:53 -0700335 int rc = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700336 if (rc <= 0) {
337 return;
338 }
339
Mark Salyzyn501c3732017-03-10 14:31:54 -0800340 size_t len = rc + 1024; // Margin for additional input race or trailing nul
341 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700342
343 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
344 if (rc <= 0) {
345 return;
346 }
347
348 if ((size_t)rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700349 len = rc + 1;
350 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700351 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700352
Mark Salyzynb6bee332015-09-08 08:56:32 -0700353 if (kl && kl->isMonotonic()) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700354 kl->synchronize(buf.get(), len);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700355 }
356
Mark Salyzynea1a2412015-09-02 07:39:53 -0700357 size_t sublen;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700358 for (char *ptr = NULL, *tok = buf.get();
Mark Salyzynea1a2412015-09-02 07:39:53 -0700359 (rc >= 0) && ((tok = log_strntok_r(tok, &len, &ptr, &sublen)));
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700360 tok = NULL) {
361 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700362 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700363 }
364 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700365 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700366 }
367 }
368}
369
Mark Salyzynd8f01802016-10-31 13:49:44 -0700370static int issueReinit() {
371 cap_t caps = cap_init();
372 (void)cap_clear(caps);
373 (void)cap_set_proc(caps);
374 (void)cap_free(caps);
375
Mark Salyzyn501c3732017-03-10 14:31:54 -0800376 int sock = TEMP_FAILURE_RETRY(socket_local_client(
377 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700378 if (sock < 0) return -errno;
379
380 static const char reinitStr[] = "reinit";
381 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
382 if (ret < 0) return -errno;
383
384 struct pollfd p;
385 memset(&p, 0, sizeof(p));
386 p.fd = sock;
387 p.events = POLLIN;
388 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
389 if (ret < 0) return -errno;
390 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
391
392 static const char success[] = "success";
393 char buffer[sizeof(success) - 1];
394 memset(buffer, 0, sizeof(buffer));
395 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
396 if (ret < 0) return -errno;
397
398 return strncmp(buffer, success, sizeof(success) - 1) != 0;
399}
400
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700401// Foreground waits for exit of the main persistent threads
402// that are started here. The threads are created to manage
403// UNIX domain client sockets for writing, reading and
404// controlling the user space logger, and for any additional
405// logging plugins like auditd and restart control. Additional
406// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800407int main(int argc, char* argv[]) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700408 // issue reinit command. KISS argument parsing.
409 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700410 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700411 }
412
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700413 static const char dev_kmsg[] = "/dev/kmsg";
414 fdDmesg = android_get_control_file(dev_kmsg);
415 if (fdDmesg < 0) {
416 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
417 }
418
419 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800420 bool klogd = __android_logger_property_get_bool(
421 "logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
422 BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700423 if (klogd) {
424 static const char proc_kmsg[] = "/proc/kmsg";
425 fdPmesg = android_get_control_file(proc_kmsg);
426 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800427 fdPmesg = TEMP_FAILURE_RETRY(
428 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700429 }
430 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
431 }
432
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700433 // Reinit Thread
434 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700435 sem_init(&uidName, 0, 0);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700436 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700437 pthread_attr_t attr;
438 if (!pthread_attr_init(&attr)) {
439 struct sched_param param;
440
441 memset(&param, 0, sizeof(param));
442 pthread_attr_setschedparam(&attr, &param);
443 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800444 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700445 pthread_t thread;
446 reinit_running = true;
447 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
448 reinit_running = false;
449 }
450 }
451 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700452 }
453
Mark Salyzyn501c3732017-03-10 14:31:54 -0800454 bool auditd =
455 __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
Mark Salyzynd2b32912016-10-28 15:11:46 -0700456 if (drop_privs(klogd, auditd) != 0) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800457 return -1;
458 }
459
460 // Serves the purpose of managing the last logs times read on a
461 // socket connection, and as a reader lock on a range of log
462 // entries.
463
Mark Salyzyn501c3732017-03-10 14:31:54 -0800464 LastLogTimes* times = new LastLogTimes();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800465
466 // LogBuffer is the object which is responsible for holding all
467 // log entries.
468
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700469 logBuf = new LogBuffer(times);
470
471 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800472
Mark Salyzyn501c3732017-03-10 14:31:54 -0800473 if (__android_logger_property_get_bool(
474 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
475 BOOL_DEFAULT_FLAG_ENG |
476 BOOL_DEFAULT_FLAG_SVELTE)) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700477 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700478 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800479
Mark Salyzyn0175b072014-02-26 09:50:16 -0800480 // LogReader listens on /dev/socket/logdr. When a client
481 // connects, log entries in the LogBuffer are written to the client.
482
Mark Salyzyn501c3732017-03-10 14:31:54 -0800483 LogReader* reader = new LogReader(logBuf);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800484 if (reader->startListener()) {
485 exit(1);
486 }
487
488 // LogListener listens on /dev/socket/logdw for client
489 // initiated log messages. New log entries are added to LogBuffer
490 // and LogReader is notified to send updates to connected clients.
491
Mark Salyzyn501c3732017-03-10 14:31:54 -0800492 LogListener* swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800493 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700494 if (swl->startListener(600)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800495 exit(1);
496 }
497
498 // Command listener listens on /dev/socket/logd for incoming logd
499 // administrative commands.
500
Mark Salyzyn501c3732017-03-10 14:31:54 -0800501 CommandListener* cl = new CommandListener(logBuf, reader, swl);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800502 if (cl->startListener()) {
503 exit(1);
504 }
505
William Roberts29d238d2013-02-08 09:45:26 +0900506 // LogAudit listens on NETLINK_AUDIT socket for selinux
507 // initiated log messages. New log entries are added to LogBuffer
508 // and LogReader is notified to send updates to connected clients.
509
Mark Salyzyn501c3732017-03-10 14:31:54 -0800510 LogAudit* al = NULL;
Sami Tolvanena742d102016-06-14 18:04:43 +0000511 if (auditd) {
512 al = new LogAudit(logBuf, reader,
Mark Salyzynf10e2732016-09-27 13:08:23 -0700513 __android_logger_property_get_bool(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800514 "ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
515 ? fdDmesg
516 : -1);
Sami Tolvanena742d102016-06-14 18:04:43 +0000517 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700518
Mark Salyzyn501c3732017-03-10 14:31:54 -0800519 LogKlog* kl = NULL;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700520 if (klogd) {
Sami Tolvanena742d102016-06-14 18:04:43 +0000521 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700522 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700523
Sami Tolvanena742d102016-06-14 18:04:43 +0000524 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700525
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700526 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700527
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700528 if (kl && kl->startListener()) {
529 delete kl;
530 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700531
Sami Tolvanena742d102016-06-14 18:04:43 +0000532 if (al && al->startListener()) {
533 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900534 }
535
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700536 TEMP_FAILURE_RETRY(pause());
537
Mark Salyzyn0175b072014-02-26 09:50:16 -0800538 exit(0);
539}