blob: 6db819e31eb8422f9bbf173f21535e49b4cb46d3 [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>
Mark Salyzyn0175b072014-02-26 09:50:16 -080030#include <sys/stat.h>
31#include <sys/types.h>
Mark Salyzynccbadc62015-03-12 12:25:35 -070032#include <syslog.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080033#include <unistd.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Mark Salyzyne457b742014-02-19 17:18:31 -080035#include <cutils/properties.h>
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080036#include <cutils/sched_policy.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070037#include <cutils/sockets.h>
Mark Salyzyn344bff42015-04-13 14:24:45 -070038#include <log/event_tag_map.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070039#include <private/android_filesystem_config.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080040
Mark Salyzyn0175b072014-02-26 09:50:16 -080041#include "CommandListener.h"
42#include "LogBuffer.h"
43#include "LogListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090044#include "LogAudit.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070045#include "LogKlog.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080046
Mark Salyzynccbadc62015-03-12 12:25:35 -070047#define KMSG_PRIORITY(PRI) \
48 '<', \
49 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
50 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
51 '>'
52
Mark Salyzyndfc47e82014-03-24 10:26:47 -070053//
54// The service is designed to be run by init, it does not respond well
55// to starting up manually. When starting up manually the sockets will
56// fail to open typically for one of the following reasons:
57// EADDRINUSE if logger is running.
58// EACCESS if started without precautions (below)
59//
60// Here is a cookbook procedure for starting up logd manually assuming
61// init is out of the way, pedantically all permissions and selinux
62// security is put back in place:
63//
64// setenforce 0
65// rm /dev/socket/logd*
66// chmod 777 /dev/socket
67// # here is where you would attach the debugger or valgrind for example
68// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
69// sleep 1
70// chmod 755 /dev/socket
71// chown logd.logd /dev/socket/logd*
72// restorecon /dev/socket/logd*
73// setenforce 1
74//
75// If minimalism prevails, typical for debugging and security is not a concern:
76//
77// setenforce 0
78// chmod 777 /dev/socket
79// logd
80//
81
Mark Salyzyn0175b072014-02-26 09:50:16 -080082static int drop_privs() {
Mark Salyzyn882f8562013-12-26 15:13:36 -080083 struct sched_param param;
84 memset(&param, 0, sizeof(param));
85
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080086 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
87 return -1;
88 }
89
Mark Salyzyn882f8562013-12-26 15:13:36 -080090 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
91 return -1;
92 }
93
Mark Salyzyn0175b072014-02-26 09:50:16 -080094 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
95 return -1;
96 }
97
98 if (setgid(AID_LOGD) != 0) {
99 return -1;
100 }
101
102 if (setuid(AID_LOGD) != 0) {
103 return -1;
104 }
105
106 struct __user_cap_header_struct capheader;
107 struct __user_cap_data_struct capdata[2];
108 memset(&capheader, 0, sizeof(capheader));
109 memset(&capdata, 0, sizeof(capdata));
110 capheader.version = _LINUX_CAPABILITY_VERSION_3;
111 capheader.pid = 0;
112
113 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
William Roberts29d238d2013-02-08 09:45:26 +0900114 capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL);
115
116 capdata[0].effective = capdata[0].permitted;
117 capdata[1].effective = capdata[1].permitted;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800118 capdata[0].inheritable = 0;
119 capdata[1].inheritable = 0;
120
121 if (capset(&capheader, &capdata[0]) < 0) {
122 return -1;
123 }
124
125 return 0;
126}
127
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700128// Property helper
129static bool property_get_bool(const char *key, bool def) {
130 char property[PROPERTY_VALUE_MAX];
131 property_get(key, property, "");
132
133 if (!strcasecmp(property, "true")) {
134 return true;
135 }
136 if (!strcasecmp(property, "false")) {
137 return false;
138 }
139
140 return def;
141}
142
Mark Salyzynccbadc62015-03-12 12:25:35 -0700143// Remove the static, and use this variable
144// globally for debugging if necessary. eg:
145// write(fdDmesg, "I am here\n", 10);
146static int fdDmesg = -1;
147
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700148static sem_t uidName;
149static uid_t uid;
150static char *name;
151
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700152static sem_t reinit;
153static bool reinit_running = false;
154static LogBuffer *logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700155
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700156static void *reinit_thread_start(void * /*obj*/) {
157 prctl(PR_SET_NAME, "logd.daemon");
158 set_sched_policy(0, SP_BACKGROUND);
159
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700160 setgid(AID_SYSTEM);
161 setuid(AID_SYSTEM);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700162
163 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700164
165 // uidToName Privileged Worker
166 if (uid) {
167 name = NULL;
168
169 FILE *fp = fopen("/data/system/packages.list", "r");
170 if (fp) {
171 // This simple parser is sensitive to format changes in
172 // frameworks/base/services/core/java/com/android/server/pm/Settings.java
173 // A dependency note has been added to that file to correct
174 // this parser.
175
176 char *buffer = NULL;
177 size_t len;
178 while (getline(&buffer, &len, fp) > 0) {
179 char *userId = strchr(buffer, ' ');
180 if (!userId) {
181 continue;
182 }
183 *userId = '\0';
184 unsigned long value = strtoul(userId + 1, NULL, 10);
185 if (value != uid) {
186 continue;
187 }
188 name = strdup(buffer);
189 break;
190 }
191 free(buffer);
192 fclose(fp);
193 }
194 uid = 0;
195 sem_post(&uidName);
196 continue;
197 }
198
Mark Salyzynccbadc62015-03-12 12:25:35 -0700199 if (fdDmesg >= 0) {
200 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
201 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
202 ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
203 write(fdDmesg, reinit_message, sizeof(reinit_message));
204 }
205
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700206 // Anything that reads persist.<property>
207 if (logBuf) {
208 logBuf->init();
209 }
210 }
211
212 return NULL;
213}
214
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700215static sem_t sem_name;
216
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700217char *android::uidToName(uid_t u) {
218 if (!u || !reinit_running) {
219 return NULL;
220 }
221
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700222 sem_wait(&sem_name);
223
224 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700225 uid = u;
226
227 name = NULL;
228 sem_post(&reinit);
229 sem_wait(&uidName);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700230 char *ret = name;
231
232 sem_post(&sem_name);
233
234 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700235}
236
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700237// Serves as a global method to trigger reinitialization
238// and as a function that can be provided to signal().
239void reinit_signal_handler(int /*signal*/) {
240 sem_post(&reinit);
241}
242
Mark Salyzyn344bff42015-04-13 14:24:45 -0700243// tagToName converts an events tag into a name
244const char *android::tagToName(uint32_t tag) {
245 static const EventTagMap *map;
246
247 if (!map) {
248 sem_wait(&sem_name);
249 if (!map) {
250 map = android_openEventTagMap(EVENT_TAG_MAP_FILE);
251 }
252 sem_post(&sem_name);
253 if (!map) {
254 return NULL;
255 }
256 }
257 return android_lookupEventTag(map, tag);
258}
259
Mark Salyzynae4d9282014-10-15 08:49:39 -0700260static bool property_get_bool_svelte(const char *key) {
261 bool not_user;
262 {
263 char property[PROPERTY_VALUE_MAX];
264 property_get("ro.build.type", property, "");
265 not_user = !!strcmp(property, "user");
266 }
267 return property_get_bool(key, not_user
268 && !property_get_bool("ro.config.low_ram", false));
269}
270
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700271// Foreground waits for exit of the main persistent threads
272// that are started here. The threads are created to manage
273// UNIX domain client sockets for writing, reading and
274// controlling the user space logger, and for any additional
275// logging plugins like auditd and restart control. Additional
276// transitory per-client threads are created for each reader.
277int main(int argc, char *argv[]) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700278 int fdPmesg = -1;
279 bool klogd = property_get_bool_svelte("logd.klogd");
280 if (klogd) {
281 fdPmesg = open("/proc/kmsg", O_RDONLY | O_NDELAY);
282 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700283 fdDmesg = open("/dev/kmsg", O_WRONLY);
284
285 // issue reinit command. KISS argument parsing.
286 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
287 int sock = TEMP_FAILURE_RETRY(
288 socket_local_client("logd",
289 ANDROID_SOCKET_NAMESPACE_RESERVED,
290 SOCK_STREAM));
291 if (sock < 0) {
292 return -errno;
293 }
294 static const char reinit[] = "reinit";
295 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
296 if (ret < 0) {
297 return -errno;
298 }
299 struct pollfd p;
300 memset(&p, 0, sizeof(p));
301 p.fd = sock;
302 p.events = POLLIN;
303 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100));
304 if (ret < 0) {
305 return -errno;
306 }
307 if ((ret == 0) || !(p.revents & POLLIN)) {
308 return -ETIME;
309 }
310 static const char success[] = "success";
311 char buffer[sizeof(success) - 1];
312 memset(buffer, 0, sizeof(buffer));
313 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
314 if (ret < 0) {
315 return -errno;
316 }
317 return strncmp(buffer, success, sizeof(success) - 1) != 0;
318 }
319
320 // Reinit Thread
321 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700322 sem_init(&uidName, 0, 0);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700323 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700324 pthread_attr_t attr;
325 if (!pthread_attr_init(&attr)) {
326 struct sched_param param;
327
328 memset(&param, 0, sizeof(param));
329 pthread_attr_setschedparam(&attr, &param);
330 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
331 if (!pthread_attr_setdetachstate(&attr,
332 PTHREAD_CREATE_DETACHED)) {
333 pthread_t thread;
334 reinit_running = true;
335 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
336 reinit_running = false;
337 }
338 }
339 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700340 }
341
Mark Salyzyn0175b072014-02-26 09:50:16 -0800342 if (drop_privs() != 0) {
343 return -1;
344 }
345
346 // Serves the purpose of managing the last logs times read on a
347 // socket connection, and as a reader lock on a range of log
348 // entries.
349
350 LastLogTimes *times = new LastLogTimes();
351
352 // LogBuffer is the object which is responsible for holding all
353 // log entries.
354
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700355 logBuf = new LogBuffer(times);
356
357 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800358
Mark Salyzynae4d9282014-10-15 08:49:39 -0700359 if (property_get_bool_svelte("logd.statistics")) {
360 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700361 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800362
Mark Salyzyn0175b072014-02-26 09:50:16 -0800363 // LogReader listens on /dev/socket/logdr. When a client
364 // connects, log entries in the LogBuffer are written to the client.
365
366 LogReader *reader = new LogReader(logBuf);
367 if (reader->startListener()) {
368 exit(1);
369 }
370
371 // LogListener listens on /dev/socket/logdw for client
372 // initiated log messages. New log entries are added to LogBuffer
373 // and LogReader is notified to send updates to connected clients.
374
375 LogListener *swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800376 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
377 if (swl->startListener(300)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800378 exit(1);
379 }
380
381 // Command listener listens on /dev/socket/logd for incoming logd
382 // administrative commands.
383
384 CommandListener *cl = new CommandListener(logBuf, reader, swl);
385 if (cl->startListener()) {
386 exit(1);
387 }
388
William Roberts29d238d2013-02-08 09:45:26 +0900389 // LogAudit listens on NETLINK_AUDIT socket for selinux
390 // initiated log messages. New log entries are added to LogBuffer
391 // and LogReader is notified to send updates to connected clients.
392
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700393 bool auditd = property_get_bool("logd.auditd", true);
394
Mark Salyzynae4d9282014-10-15 08:49:39 -0700395 LogAudit *al = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700396 if (auditd) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700397 bool dmesg = property_get_bool("logd.auditd.dmesg", true);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700398 al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1);
399 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700400
Mark Salyzynae4d9282014-10-15 08:49:39 -0700401 LogKlog *kl = NULL;
402 if (klogd) {
403 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL);
404 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700405
Mark Salyzynae4d9282014-10-15 08:49:39 -0700406 if (al || kl) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700407 int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
408 if (len > 0) {
409 len++;
410 char buf[len];
411
412 int rc = klogctl(KLOG_READ_ALL, buf, len);
413
Mark Salyzynae4d9282014-10-15 08:49:39 -0700414 buf[len - 1] = '\0';
Mark Salyzyneb06de72014-10-13 09:59:37 -0700415
Mark Salyzynae4d9282014-10-15 08:49:39 -0700416 if ((rc >= 0) && kl) {
417 kl->synchronize(buf);
418 }
419
420 for (char *ptr, *tok = buf;
421 (rc >= 0) && ((tok = strtok_r(tok, "\r\n", &ptr)));
422 tok = NULL) {
423 if (al) {
424 rc = al->log(tok);
425 }
426 if (kl) {
427 rc = kl->log(tok);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800428 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700429 }
430 }
431
Mark Salyzynae4d9282014-10-15 08:49:39 -0700432 // failure is an option ... messages are in dmesg (required by standard)
433
434 if (kl && kl->startListener()) {
435 delete kl;
436 }
437
438 if (al && al->startListener()) {
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700439 delete al;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700440 }
William Roberts29d238d2013-02-08 09:45:26 +0900441 }
442
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700443 TEMP_FAILURE_RETRY(pause());
444
Mark Salyzyn0175b072014-02-26 09:50:16 -0800445 exit(0);
446}