blob: 8c38d9aa3ec6f0282c05719d0561d0dfa5e7a0f7 [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
Mark Salyzynd5600fd2015-06-12 14:59:42 -070036#include <memory>
37
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040038#include <android-base/macros.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080039#include <cutils/android_get_control_file.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080040#include <cutils/properties.h>
Suren Baghdasaryan1bef8c52019-01-23 20:43:03 -080041#include <cutils/sched_policy.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>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070047#include <utils/threads.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080048
Mark Salyzyn0175b072014-02-26 09:50:16 -080049#include "CommandListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090050#include "LogAudit.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080051#include "LogBuffer.h"
Mark Salyzyna1aacb72014-10-15 08:49:39 -070052#include "LogKlog.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080053#include "LogListener.h"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070054#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080055
Mark Salyzyn501c3732017-03-10 14:31:54 -080056#define KMSG_PRIORITY(PRI) \
57 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
58 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070059
Mark Salyzyndfc47e82014-03-24 10:26:47 -070060//
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040061// The service is designed to be run by init, it does not respond well
Mark Salyzyndfc47e82014-03-24 10:26:47 -070062// to starting up manually. When starting up manually the sockets will
63// fail to open typically for one of the following reasons:
64// EADDRINUSE if logger is running.
65// EACCESS if started without precautions (below)
66//
67// Here is a cookbook procedure for starting up logd manually assuming
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040068// init is out of the way, pedantically all permissions and SELinux
Mark Salyzyndfc47e82014-03-24 10:26:47 -070069// security is put back in place:
70//
71// setenforce 0
72// rm /dev/socket/logd*
73// chmod 777 /dev/socket
74// # here is where you would attach the debugger or valgrind for example
75// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
76// sleep 1
77// chmod 755 /dev/socket
78// chown logd.logd /dev/socket/logd*
79// restorecon /dev/socket/logd*
80// setenforce 1
81//
82// If minimalism prevails, typical for debugging and security is not a concern:
83//
84// setenforce 0
85// chmod 777 /dev/socket
86// logd
87//
88
Mark Salyzynd2b32912016-10-28 15:11:46 -070089static int drop_privs(bool klogd, bool auditd) {
Elliott Hughescef62b42018-06-13 10:33:45 -070090 sched_param param = {};
Mark Salyzyn882f8562013-12-26 15:13:36 -080091
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080092 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070093 android::prdebug("failed to set background scheduling policy");
Elliott Hughescef62b42018-06-13 10:33:45 -070094 return -1;
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080095 }
96
Mark Salyzyn501c3732017-03-10 14:31:54 -080097 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070098 android::prdebug("failed to set batch scheduler");
Elliott Hughescef62b42018-06-13 10:33:45 -070099 return -1;
Mark Salyzyn882f8562013-12-26 15:13:36 -0800100 }
101
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700102 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -0700103 android::prdebug("failed to set background cgroup");
Elliott Hughescef62b42018-06-13 10:33:45 -0700104 return -1;
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700105 }
106
Mark Salyzyn4b42ea52018-08-15 12:17:18 -0700107 if (!__android_logger_property_get_bool("ro.debuggable",
108 BOOL_DEFAULT_FALSE) &&
Elliott Hughescef62b42018-06-13 10:33:45 -0700109 prctl(PR_SET_DUMPABLE, 0) == -1) {
Mark Salyzyn6a70ded2016-10-28 14:49:53 -0700110 android::prdebug("failed to clear PR_SET_DUMPABLE");
111 return -1;
112 }
113
Mark Salyzyn501c3732017-03-10 14:31:54 -0800114 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(),
115 cap_free);
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700116 if (cap_clear(caps.get()) < 0) return -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117 cap_value_t cap_value[] = { CAP_SETGID, // must be first for below
118 klogd ? CAP_SYSLOG : CAP_SETGID,
119 auditd ? CAP_AUDIT_CONTROL : CAP_SETGID };
120 if (cap_set_flag(caps.get(), CAP_PERMITTED, arraysize(cap_value), cap_value,
121 CAP_SET) < 0) {
122 return -1;
123 }
124 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, arraysize(cap_value), cap_value,
125 CAP_SET) < 0) {
126 return -1;
127 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700128 if (cap_set_proc(caps.get()) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800129 android::prdebug(
130 "failed to set CAP_SETGID, CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)",
131 errno);
Elliott Hughescef62b42018-06-13 10:33:45 -0700132 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700133 }
134
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800135 gid_t groups[] = { AID_READPROC };
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700136
137 if (setgroups(arraysize(groups), groups) == -1) {
138 android::prdebug("failed to set AID_READPROC groups");
Elliott Hughescef62b42018-06-13 10:33:45 -0700139 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700140 }
141
142 if (setgid(AID_LOGD) != 0) {
143 android::prdebug("failed to set AID_LOGD gid");
Elliott Hughescef62b42018-06-13 10:33:45 -0700144 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700145 }
146
147 if (setuid(AID_LOGD) != 0) {
148 android::prdebug("failed to set AID_LOGD uid");
Elliott Hughescef62b42018-06-13 10:33:45 -0700149 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700150 }
151
Mark Salyzyn501c3732017-03-10 14:31:54 -0800152 if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, cap_value, CAP_CLEAR) < 0) {
153 return -1;
154 }
155 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, cap_value, CAP_CLEAR) < 0) {
156 return -1;
157 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700158 if (cap_set_proc(caps.get()) < 0) {
159 android::prdebug("failed to clear CAP_SETGID (%d)", errno);
Elliott Hughescef62b42018-06-13 10:33:45 -0700160 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700161 }
162
Mark Salyzyn0175b072014-02-26 09:50:16 -0800163 return 0;
164}
165
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700166// Property helper
Mark Salyzyn501c3732017-03-10 14:31:54 -0800167static bool check_flag(const char* prop, const char* flag) {
168 const char* cp = strcasestr(prop, flag);
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800169 if (!cp) {
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700170 return false;
171 }
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800172 // We only will document comma (,)
173 static const char sep[] = ",:;|+ \t\f";
174 if ((cp != prop) && !strchr(sep, cp[-1])) {
175 return false;
176 }
177 cp += strlen(flag);
178 return !*cp || !!strchr(sep, *cp);
179}
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700180
Mark Salyzynccbadc62015-03-12 12:25:35 -0700181static int fdDmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800182void android::prdebug(const char* fmt, ...) {
Mark Salyzynd048f112016-02-08 10:28:12 -0800183 if (fdDmesg < 0) {
184 return;
185 }
186
187 static const char message[] = {
188 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
189 };
190 char buffer[256];
191 memcpy(buffer, message, sizeof(message));
192
193 va_list ap;
194 va_start(ap, fmt);
195 int n = vsnprintf(buffer + sizeof(message),
196 sizeof(buffer) - sizeof(message), fmt, ap);
197 va_end(ap);
198 if (n > 0) {
199 buffer[sizeof(buffer) - 1] = '\0';
200 if (!strchr(buffer, '\n')) {
201 buffer[sizeof(buffer) - 2] = '\0';
202 strlcat(buffer, "\n", sizeof(buffer));
203 }
204 write(fdDmesg, buffer, strlen(buffer));
205 }
206}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700207
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700208static sem_t uidName;
209static uid_t uid;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800210static char* name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700211
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700212static sem_t reinit;
213static bool reinit_running = false;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700214static LogBuffer* logBuf = nullptr;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700215
Mark Salyzyn501c3732017-03-10 14:31:54 -0800216static bool package_list_parser_cb(pkg_info* info, void* /* userdata */) {
William Robertsaeca97b2015-07-31 13:10:36 -0700217 bool rc = true;
218 if (info->uid == uid) {
219 name = strdup(info->name);
220 // false to stop processing
221 rc = false;
222 }
223
224 packagelist_free(info);
225 return rc;
226}
227
Mark Salyzyn501c3732017-03-10 14:31:54 -0800228static void* reinit_thread_start(void* /*obj*/) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700229 prctl(PR_SET_NAME, "logd.daemon");
230 set_sched_policy(0, SP_BACKGROUND);
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700231 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700232
Mark Salyzynd392ac12017-04-27 14:46:31 -0700233 // We should drop to AID_LOGD, if we are anything else, we have
234 // even lesser privileges and accept our fate.
235 gid_t groups[] = {
236 AID_SYSTEM, // search access to /data/system path
237 AID_PACKAGE_INFO, // readonly access to /data/system/packages.list
238 };
239 if (setgroups(arraysize(groups), groups) == -1) {
240 android::prdebug(
241 "logd.daemon: failed to set AID_SYSTEM AID_PACKAGE_INFO groups");
242 }
243 if (setgid(AID_LOGD) != 0) {
244 android::prdebug("logd.daemon: failed to set AID_LOGD gid");
245 }
246 if (setuid(AID_LOGD) != 0) {
247 android::prdebug("logd.daemon: failed to set AID_LOGD uid");
248 }
249
Mark Salyzynd8f01802016-10-31 13:49:44 -0700250 cap_t caps = cap_init();
251 (void)cap_clear(caps);
252 (void)cap_set_proc(caps);
253 (void)cap_free(caps);
254
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700255 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700256 // uidToName Privileged Worker
257 if (uid) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700258 name = nullptr;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700259
Mark Salyzynd392ac12017-04-27 14:46:31 -0700260 // if we got the perms wrong above, this would spam if we reported
261 // problems with acquisition of an uid name from the packages.
262 (void)packagelist_parse(package_list_parser_cb, nullptr);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700263
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700264 uid = 0;
265 sem_post(&uidName);
266 continue;
267 }
268
Mark Salyzynccbadc62015-03-12 12:25:35 -0700269 if (fdDmesg >= 0) {
270 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn501c3732017-03-10 14:31:54 -0800271 'l',
272 'o',
273 'g',
274 'd',
275 '.',
276 'd',
277 'a',
278 'e',
279 'm',
280 'o',
281 'n',
282 ':',
283 ' ',
284 'r',
285 'e',
286 'i',
287 'n',
288 'i',
289 't',
290 '\n' };
Mark Salyzynccbadc62015-03-12 12:25:35 -0700291 write(fdDmesg, reinit_message, sizeof(reinit_message));
292 }
293
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700294 // Anything that reads persist.<property>
295 if (logBuf) {
296 logBuf->init();
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700297 logBuf->initPrune(nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700298 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700299 android::ReReadEventLogTags();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700300 }
301
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700302 return nullptr;
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700303}
304
Mark Salyzyn95108f12015-04-20 07:26:27 -0700305static sem_t sem_name;
306
Mark Salyzyn501c3732017-03-10 14:31:54 -0800307char* android::uidToName(uid_t u) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700308 if (!u || !reinit_running) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700309 return nullptr;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700310 }
311
Mark Salyzyn95108f12015-04-20 07:26:27 -0700312 sem_wait(&sem_name);
313
314 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700315 uid = u;
316
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700317 name = nullptr;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700318 sem_post(&reinit);
319 sem_wait(&uidName);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800320 char* ret = name;
Mark Salyzyn95108f12015-04-20 07:26:27 -0700321
322 sem_post(&sem_name);
323
324 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700325}
326
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700327// Serves as a global method to trigger reinitialization
328// and as a function that can be provided to signal().
329void reinit_signal_handler(int /*signal*/) {
330 sem_post(&reinit);
331}
332
Mark Salyzyn501c3732017-03-10 14:31:54 -0800333static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700334 if (!al && !kl) {
335 return;
336 }
337
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700338 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700339 if (rc <= 0) {
340 return;
341 }
342
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700343 // Margin for additional input race or trailing nul
344 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800345 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700346
347 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
348 if (rc <= 0) {
349 return;
350 }
351
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700352 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700353 len = rc + 1;
354 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700355 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700356
Mark Salyzynb6bee332015-09-08 08:56:32 -0700357 if (kl && kl->isMonotonic()) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700358 kl->synchronize(buf.get(), len);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700359 }
360
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700361 ssize_t sublen;
362 for (char *ptr = nullptr, *tok = buf.get();
363 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
364 tok = nullptr) {
365 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700366 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700367 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700368 }
369 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700370 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700371 }
372 }
373}
374
Mark Salyzynd8f01802016-10-31 13:49:44 -0700375static int issueReinit() {
376 cap_t caps = cap_init();
377 (void)cap_clear(caps);
378 (void)cap_set_proc(caps);
379 (void)cap_free(caps);
380
Mark Salyzyn501c3732017-03-10 14:31:54 -0800381 int sock = TEMP_FAILURE_RETRY(socket_local_client(
382 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700383 if (sock < 0) return -errno;
384
385 static const char reinitStr[] = "reinit";
386 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
387 if (ret < 0) return -errno;
388
389 struct pollfd p;
390 memset(&p, 0, sizeof(p));
391 p.fd = sock;
392 p.events = POLLIN;
393 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
394 if (ret < 0) return -errno;
395 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
396
397 static const char success[] = "success";
398 char buffer[sizeof(success) - 1];
399 memset(buffer, 0, sizeof(buffer));
400 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
401 if (ret < 0) return -errno;
402
403 return strncmp(buffer, success, sizeof(success) - 1) != 0;
404}
405
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700406// Foreground waits for exit of the main persistent threads
407// that are started here. The threads are created to manage
408// UNIX domain client sockets for writing, reading and
409// controlling the user space logger, and for any additional
410// logging plugins like auditd and restart control. Additional
411// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800412int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900413 // logd is written under the assumption that the timezone is UTC.
414 // If TZ is not set, persist.sys.timezone is looked up in some time utility
415 // libc functions, including mktime. It confuses the logd time handling,
416 // so here explicitly set TZ to UTC, which overrides the property.
417 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700418 // issue reinit command. KISS argument parsing.
419 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700420 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700421 }
422
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700423 static const char dev_kmsg[] = "/dev/kmsg";
424 fdDmesg = android_get_control_file(dev_kmsg);
425 if (fdDmesg < 0) {
426 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
427 }
428
429 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800430 bool klogd = __android_logger_property_get_bool(
Siarhei Vishniakoue8ed36b2017-12-28 14:13:22 -0800431 "ro.logd.kernel",
432 BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700433 if (klogd) {
434 static const char proc_kmsg[] = "/proc/kmsg";
435 fdPmesg = android_get_control_file(proc_kmsg);
436 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800437 fdPmesg = TEMP_FAILURE_RETRY(
438 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700439 }
440 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
441 }
442
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700443 // Reinit Thread
444 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700445 sem_init(&uidName, 0, 0);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700446 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700447 pthread_attr_t attr;
448 if (!pthread_attr_init(&attr)) {
449 struct sched_param param;
450
451 memset(&param, 0, sizeof(param));
452 pthread_attr_setschedparam(&attr, &param);
453 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800454 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700455 pthread_t thread;
456 reinit_running = true;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700457 if (pthread_create(&thread, &attr, reinit_thread_start, nullptr)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700458 reinit_running = false;
459 }
460 }
461 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700462 }
463
Mark Salyzyn501c3732017-03-10 14:31:54 -0800464 bool auditd =
465 __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
Mark Salyzynd2b32912016-10-28 15:11:46 -0700466 if (drop_privs(klogd, auditd) != 0) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700467 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800468 }
469
470 // Serves the purpose of managing the last logs times read on a
471 // socket connection, and as a reader lock on a range of log
472 // entries.
473
Mark Salyzyn501c3732017-03-10 14:31:54 -0800474 LastLogTimes* times = new LastLogTimes();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800475
476 // LogBuffer is the object which is responsible for holding all
477 // log entries.
478
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700479 logBuf = new LogBuffer(times);
480
481 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800482
Mark Salyzyn501c3732017-03-10 14:31:54 -0800483 if (__android_logger_property_get_bool(
484 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
485 BOOL_DEFAULT_FLAG_ENG |
486 BOOL_DEFAULT_FLAG_SVELTE)) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700487 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700488 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800489
Mark Salyzyn0175b072014-02-26 09:50:16 -0800490 // LogReader listens on /dev/socket/logdr. When a client
491 // connects, log entries in the LogBuffer are written to the client.
492
Mark Salyzyn501c3732017-03-10 14:31:54 -0800493 LogReader* reader = new LogReader(logBuf);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800494 if (reader->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700495 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800496 }
497
498 // LogListener listens on /dev/socket/logdw for client
499 // initiated log messages. New log entries are added to LogBuffer
500 // and LogReader is notified to send updates to connected clients.
501
Mark Salyzyn501c3732017-03-10 14:31:54 -0800502 LogListener* swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800503 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700504 if (swl->startListener(600)) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700505 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800506 }
507
508 // Command listener listens on /dev/socket/logd for incoming logd
509 // administrative commands.
510
Mark Salyzyn501c3732017-03-10 14:31:54 -0800511 CommandListener* cl = new CommandListener(logBuf, reader, swl);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800512 if (cl->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700513 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800514 }
515
William Roberts29d238d2013-02-08 09:45:26 +0900516 // LogAudit listens on NETLINK_AUDIT socket for selinux
517 // initiated log messages. New log entries are added to LogBuffer
518 // and LogReader is notified to send updates to connected clients.
519
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700520 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000521 if (auditd) {
522 al = new LogAudit(logBuf, reader,
Mark Salyzynf10e2732016-09-27 13:08:23 -0700523 __android_logger_property_get_bool(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800524 "ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
525 ? fdDmesg
526 : -1);
Sami Tolvanena742d102016-06-14 18:04:43 +0000527 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700528
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700529 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700530 if (klogd) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700531 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != nullptr);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700532 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700533
Sami Tolvanena742d102016-06-14 18:04:43 +0000534 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700535
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700536 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700537
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700538 if (kl && kl->startListener()) {
539 delete kl;
540 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700541
Sami Tolvanena742d102016-06-14 18:04:43 +0000542 if (al && al->startListener()) {
543 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900544 }
545
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700546 TEMP_FAILURE_RETRY(pause());
547
Elliott Hughescef62b42018-06-13 10:33:45 -0700548 return EXIT_SUCCESS;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800549}