blob: c8183f0e76b5c1f12a3c996f1f4c105e9cdde78f [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 Salyzyn0484b3b2016-08-11 08:02:06 -0700224static LogBuffer* logBuf = nullptr;
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 Salyzynd392ac12017-04-27 14:46:31 -0700243 // We should drop to AID_LOGD, if we are anything else, we have
244 // even lesser privileges and accept our fate.
245 gid_t groups[] = {
246 AID_SYSTEM, // search access to /data/system path
247 AID_PACKAGE_INFO, // readonly access to /data/system/packages.list
248 };
249 if (setgroups(arraysize(groups), groups) == -1) {
250 android::prdebug(
251 "logd.daemon: failed to set AID_SYSTEM AID_PACKAGE_INFO groups");
252 }
253 if (setgid(AID_LOGD) != 0) {
254 android::prdebug("logd.daemon: failed to set AID_LOGD gid");
255 }
256 if (setuid(AID_LOGD) != 0) {
257 android::prdebug("logd.daemon: failed to set AID_LOGD uid");
258 }
259
Mark Salyzynd8f01802016-10-31 13:49:44 -0700260 cap_t caps = cap_init();
261 (void)cap_clear(caps);
262 (void)cap_set_proc(caps);
263 (void)cap_free(caps);
264
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700265 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700266 // uidToName Privileged Worker
267 if (uid) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700268 name = nullptr;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700269
Mark Salyzynd392ac12017-04-27 14:46:31 -0700270 // if we got the perms wrong above, this would spam if we reported
271 // problems with acquisition of an uid name from the packages.
272 (void)packagelist_parse(package_list_parser_cb, nullptr);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700273
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700274 uid = 0;
275 sem_post(&uidName);
276 continue;
277 }
278
Mark Salyzynccbadc62015-03-12 12:25:35 -0700279 if (fdDmesg >= 0) {
280 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn501c3732017-03-10 14:31:54 -0800281 'l',
282 'o',
283 'g',
284 'd',
285 '.',
286 'd',
287 'a',
288 'e',
289 'm',
290 'o',
291 'n',
292 ':',
293 ' ',
294 'r',
295 'e',
296 'i',
297 'n',
298 'i',
299 't',
300 '\n' };
Mark Salyzynccbadc62015-03-12 12:25:35 -0700301 write(fdDmesg, reinit_message, sizeof(reinit_message));
302 }
303
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700304 // Anything that reads persist.<property>
305 if (logBuf) {
306 logBuf->init();
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700307 logBuf->initPrune(nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700308 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700309 android::ReReadEventLogTags();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700310 }
311
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700312 return nullptr;
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700313}
314
Mark Salyzyn95108f12015-04-20 07:26:27 -0700315static sem_t sem_name;
316
Mark Salyzyn501c3732017-03-10 14:31:54 -0800317char* android::uidToName(uid_t u) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700318 if (!u || !reinit_running) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700319 return nullptr;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700320 }
321
Mark Salyzyn95108f12015-04-20 07:26:27 -0700322 sem_wait(&sem_name);
323
324 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700325 uid = u;
326
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700327 name = nullptr;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700328 sem_post(&reinit);
329 sem_wait(&uidName);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800330 char* ret = name;
Mark Salyzyn95108f12015-04-20 07:26:27 -0700331
332 sem_post(&sem_name);
333
334 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700335}
336
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700337// Serves as a global method to trigger reinitialization
338// and as a function that can be provided to signal().
339void reinit_signal_handler(int /*signal*/) {
340 sem_post(&reinit);
341}
342
Mark Salyzyn501c3732017-03-10 14:31:54 -0800343static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700344 if (!al && !kl) {
345 return;
346 }
347
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700348 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700349 if (rc <= 0) {
350 return;
351 }
352
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700353 // Margin for additional input race or trailing nul
354 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800355 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700356
357 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
358 if (rc <= 0) {
359 return;
360 }
361
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700362 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700363 len = rc + 1;
364 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700365 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700366
Mark Salyzynb6bee332015-09-08 08:56:32 -0700367 if (kl && kl->isMonotonic()) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700368 kl->synchronize(buf.get(), len);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700369 }
370
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700371 ssize_t sublen;
372 for (char *ptr = nullptr, *tok = buf.get();
373 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
374 tok = nullptr) {
375 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700376 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700377 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700378 }
379 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700380 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700381 }
382 }
383}
384
Mark Salyzynd8f01802016-10-31 13:49:44 -0700385static int issueReinit() {
386 cap_t caps = cap_init();
387 (void)cap_clear(caps);
388 (void)cap_set_proc(caps);
389 (void)cap_free(caps);
390
Mark Salyzyn501c3732017-03-10 14:31:54 -0800391 int sock = TEMP_FAILURE_RETRY(socket_local_client(
392 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700393 if (sock < 0) return -errno;
394
395 static const char reinitStr[] = "reinit";
396 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
397 if (ret < 0) return -errno;
398
399 struct pollfd p;
400 memset(&p, 0, sizeof(p));
401 p.fd = sock;
402 p.events = POLLIN;
403 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
404 if (ret < 0) return -errno;
405 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
406
407 static const char success[] = "success";
408 char buffer[sizeof(success) - 1];
409 memset(buffer, 0, sizeof(buffer));
410 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
411 if (ret < 0) return -errno;
412
413 return strncmp(buffer, success, sizeof(success) - 1) != 0;
414}
415
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700416// Foreground waits for exit of the main persistent threads
417// that are started here. The threads are created to manage
418// UNIX domain client sockets for writing, reading and
419// controlling the user space logger, and for any additional
420// logging plugins like auditd and restart control. Additional
421// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800422int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900423 // logd is written under the assumption that the timezone is UTC.
424 // If TZ is not set, persist.sys.timezone is looked up in some time utility
425 // libc functions, including mktime. It confuses the logd time handling,
426 // so here explicitly set TZ to UTC, which overrides the property.
427 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700428 // issue reinit command. KISS argument parsing.
429 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700430 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700431 }
432
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700433 static const char dev_kmsg[] = "/dev/kmsg";
434 fdDmesg = android_get_control_file(dev_kmsg);
435 if (fdDmesg < 0) {
436 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
437 }
438
439 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800440 bool klogd = __android_logger_property_get_bool(
441 "logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
442 BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700443 if (klogd) {
444 static const char proc_kmsg[] = "/proc/kmsg";
445 fdPmesg = android_get_control_file(proc_kmsg);
446 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800447 fdPmesg = TEMP_FAILURE_RETRY(
448 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700449 }
450 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
451 }
452
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700453 // Reinit Thread
454 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700455 sem_init(&uidName, 0, 0);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700456 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700457 pthread_attr_t attr;
458 if (!pthread_attr_init(&attr)) {
459 struct sched_param param;
460
461 memset(&param, 0, sizeof(param));
462 pthread_attr_setschedparam(&attr, &param);
463 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800464 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700465 pthread_t thread;
466 reinit_running = true;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700467 if (pthread_create(&thread, &attr, reinit_thread_start, nullptr)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700468 reinit_running = false;
469 }
470 }
471 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700472 }
473
Mark Salyzyn501c3732017-03-10 14:31:54 -0800474 bool auditd =
475 __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
Mark Salyzynd2b32912016-10-28 15:11:46 -0700476 if (drop_privs(klogd, auditd) != 0) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800477 return -1;
478 }
479
480 // Serves the purpose of managing the last logs times read on a
481 // socket connection, and as a reader lock on a range of log
482 // entries.
483
Mark Salyzyn501c3732017-03-10 14:31:54 -0800484 LastLogTimes* times = new LastLogTimes();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800485
486 // LogBuffer is the object which is responsible for holding all
487 // log entries.
488
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700489 logBuf = new LogBuffer(times);
490
491 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800492
Mark Salyzyn501c3732017-03-10 14:31:54 -0800493 if (__android_logger_property_get_bool(
494 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
495 BOOL_DEFAULT_FLAG_ENG |
496 BOOL_DEFAULT_FLAG_SVELTE)) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700497 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700498 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800499
Mark Salyzyn0175b072014-02-26 09:50:16 -0800500 // LogReader listens on /dev/socket/logdr. When a client
501 // connects, log entries in the LogBuffer are written to the client.
502
Mark Salyzyn501c3732017-03-10 14:31:54 -0800503 LogReader* reader = new LogReader(logBuf);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800504 if (reader->startListener()) {
505 exit(1);
506 }
507
508 // LogListener listens on /dev/socket/logdw for client
509 // initiated log messages. New log entries are added to LogBuffer
510 // and LogReader is notified to send updates to connected clients.
511
Mark Salyzyn501c3732017-03-10 14:31:54 -0800512 LogListener* swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800513 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700514 if (swl->startListener(600)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800515 exit(1);
516 }
517
518 // Command listener listens on /dev/socket/logd for incoming logd
519 // administrative commands.
520
Mark Salyzyn501c3732017-03-10 14:31:54 -0800521 CommandListener* cl = new CommandListener(logBuf, reader, swl);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800522 if (cl->startListener()) {
523 exit(1);
524 }
525
William Roberts29d238d2013-02-08 09:45:26 +0900526 // LogAudit listens on NETLINK_AUDIT socket for selinux
527 // initiated log messages. New log entries are added to LogBuffer
528 // and LogReader is notified to send updates to connected clients.
529
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700530 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000531 if (auditd) {
532 al = new LogAudit(logBuf, reader,
Mark Salyzynf10e2732016-09-27 13:08:23 -0700533 __android_logger_property_get_bool(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800534 "ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
535 ? fdDmesg
536 : -1);
Sami Tolvanena742d102016-06-14 18:04:43 +0000537 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700538
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700539 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700540 if (klogd) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700541 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != nullptr);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700542 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700543
Sami Tolvanena742d102016-06-14 18:04:43 +0000544 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700545
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700546 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700547
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700548 if (kl && kl->startListener()) {
549 delete kl;
550 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700551
Sami Tolvanena742d102016-06-14 18:04:43 +0000552 if (al && al->startListener()) {
553 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900554 }
555
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700556 TEMP_FAILURE_RETRY(pause());
557
Mark Salyzyn0175b072014-02-26 09:50:16 -0800558 exit(0);
559}