blob: 7b8e94e667de3571af0129aad45662351d05bbd1 [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 Salyzyn0175b072014-02-26 09:50:16 -080045
Mark Salyzynccbadc62015-03-12 12:25:35 -070046#define KMSG_PRIORITY(PRI) \
47 '<', \
48 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
49 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
50 '>'
51
Mark Salyzyndfc47e82014-03-24 10:26:47 -070052//
53// The service is designed to be run by init, it does not respond well
54// to starting up manually. When starting up manually the sockets will
55// fail to open typically for one of the following reasons:
56// EADDRINUSE if logger is running.
57// EACCESS if started without precautions (below)
58//
59// Here is a cookbook procedure for starting up logd manually assuming
60// init is out of the way, pedantically all permissions and selinux
61// security is put back in place:
62//
63// setenforce 0
64// rm /dev/socket/logd*
65// chmod 777 /dev/socket
66// # here is where you would attach the debugger or valgrind for example
67// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
68// sleep 1
69// chmod 755 /dev/socket
70// chown logd.logd /dev/socket/logd*
71// restorecon /dev/socket/logd*
72// setenforce 1
73//
74// If minimalism prevails, typical for debugging and security is not a concern:
75//
76// setenforce 0
77// chmod 777 /dev/socket
78// logd
79//
80
Mark Salyzyn0175b072014-02-26 09:50:16 -080081static int drop_privs() {
Mark Salyzyn882f8562013-12-26 15:13:36 -080082 struct sched_param param;
83 memset(&param, 0, sizeof(param));
84
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080085 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
86 return -1;
87 }
88
Mark Salyzyn882f8562013-12-26 15:13:36 -080089 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
90 return -1;
91 }
92
Mark Salyzyn0175b072014-02-26 09:50:16 -080093 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
94 return -1;
95 }
96
97 if (setgid(AID_LOGD) != 0) {
98 return -1;
99 }
100
101 if (setuid(AID_LOGD) != 0) {
102 return -1;
103 }
104
105 struct __user_cap_header_struct capheader;
106 struct __user_cap_data_struct capdata[2];
107 memset(&capheader, 0, sizeof(capheader));
108 memset(&capdata, 0, sizeof(capdata));
109 capheader.version = _LINUX_CAPABILITY_VERSION_3;
110 capheader.pid = 0;
111
112 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
William Roberts29d238d2013-02-08 09:45:26 +0900113 capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL);
114
115 capdata[0].effective = capdata[0].permitted;
116 capdata[1].effective = capdata[1].permitted;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800117 capdata[0].inheritable = 0;
118 capdata[1].inheritable = 0;
119
120 if (capset(&capheader, &capdata[0]) < 0) {
121 return -1;
122 }
123
124 return 0;
125}
126
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700127// Property helper
128static bool property_get_bool(const char *key, bool def) {
129 char property[PROPERTY_VALUE_MAX];
130 property_get(key, property, "");
131
132 if (!strcasecmp(property, "true")) {
133 return true;
134 }
135 if (!strcasecmp(property, "false")) {
136 return false;
137 }
138
139 return def;
140}
141
Mark Salyzynccbadc62015-03-12 12:25:35 -0700142// Remove the static, and use this variable
143// globally for debugging if necessary. eg:
144// write(fdDmesg, "I am here\n", 10);
145static int fdDmesg = -1;
146
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700147static sem_t uidName;
148static uid_t uid;
149static char *name;
150
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700151static sem_t reinit;
152static bool reinit_running = false;
153static LogBuffer *logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700154
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700155static void *reinit_thread_start(void * /*obj*/) {
156 prctl(PR_SET_NAME, "logd.daemon");
157 set_sched_policy(0, SP_BACKGROUND);
158
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700159 setgid(AID_SYSTEM);
160 setuid(AID_SYSTEM);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700161
162 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700163
164 // uidToName Privileged Worker
165 if (uid) {
166 name = NULL;
167
168 FILE *fp = fopen("/data/system/packages.list", "r");
169 if (fp) {
170 // This simple parser is sensitive to format changes in
171 // frameworks/base/services/core/java/com/android/server/pm/Settings.java
172 // A dependency note has been added to that file to correct
173 // this parser.
174
175 char *buffer = NULL;
176 size_t len;
177 while (getline(&buffer, &len, fp) > 0) {
178 char *userId = strchr(buffer, ' ');
179 if (!userId) {
180 continue;
181 }
182 *userId = '\0';
183 unsigned long value = strtoul(userId + 1, NULL, 10);
184 if (value != uid) {
185 continue;
186 }
187 name = strdup(buffer);
188 break;
189 }
190 free(buffer);
191 fclose(fp);
192 }
193 uid = 0;
194 sem_post(&uidName);
195 continue;
196 }
197
Mark Salyzynccbadc62015-03-12 12:25:35 -0700198 if (fdDmesg >= 0) {
199 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
200 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
201 ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
202 write(fdDmesg, reinit_message, sizeof(reinit_message));
203 }
204
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700205 // Anything that reads persist.<property>
206 if (logBuf) {
207 logBuf->init();
208 }
209 }
210
211 return NULL;
212}
213
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700214static sem_t sem_name;
215
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700216char *android::uidToName(uid_t u) {
217 if (!u || !reinit_running) {
218 return NULL;
219 }
220
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700221 sem_wait(&sem_name);
222
223 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700224 uid = u;
225
226 name = NULL;
227 sem_post(&reinit);
228 sem_wait(&uidName);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700229 char *ret = name;
230
231 sem_post(&sem_name);
232
233 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700234}
235
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700236// Serves as a global method to trigger reinitialization
237// and as a function that can be provided to signal().
238void reinit_signal_handler(int /*signal*/) {
239 sem_post(&reinit);
240}
241
Mark Salyzyn344bff42015-04-13 14:24:45 -0700242// tagToName converts an events tag into a name
243const char *android::tagToName(uint32_t tag) {
244 static const EventTagMap *map;
245
246 if (!map) {
247 sem_wait(&sem_name);
248 if (!map) {
249 map = android_openEventTagMap(EVENT_TAG_MAP_FILE);
250 }
251 sem_post(&sem_name);
252 if (!map) {
253 return NULL;
254 }
255 }
256 return android_lookupEventTag(map, tag);
257}
258
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700259// Foreground waits for exit of the main persistent threads
260// that are started here. The threads are created to manage
261// UNIX domain client sockets for writing, reading and
262// controlling the user space logger, and for any additional
263// logging plugins like auditd and restart control. Additional
264// transitory per-client threads are created for each reader.
265int main(int argc, char *argv[]) {
266 fdDmesg = open("/dev/kmsg", O_WRONLY);
267
268 // issue reinit command. KISS argument parsing.
269 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
270 int sock = TEMP_FAILURE_RETRY(
271 socket_local_client("logd",
272 ANDROID_SOCKET_NAMESPACE_RESERVED,
273 SOCK_STREAM));
274 if (sock < 0) {
275 return -errno;
276 }
277 static const char reinit[] = "reinit";
278 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
279 if (ret < 0) {
280 return -errno;
281 }
282 struct pollfd p;
283 memset(&p, 0, sizeof(p));
284 p.fd = sock;
285 p.events = POLLIN;
286 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100));
287 if (ret < 0) {
288 return -errno;
289 }
290 if ((ret == 0) || !(p.revents & POLLIN)) {
291 return -ETIME;
292 }
293 static const char success[] = "success";
294 char buffer[sizeof(success) - 1];
295 memset(buffer, 0, sizeof(buffer));
296 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
297 if (ret < 0) {
298 return -errno;
299 }
300 return strncmp(buffer, success, sizeof(success) - 1) != 0;
301 }
302
303 // Reinit Thread
304 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700305 sem_init(&uidName, 0, 0);
Mark Salyzyn21fb7e02015-04-20 07:26:27 -0700306 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700307 pthread_attr_t attr;
308 if (!pthread_attr_init(&attr)) {
309 struct sched_param param;
310
311 memset(&param, 0, sizeof(param));
312 pthread_attr_setschedparam(&attr, &param);
313 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
314 if (!pthread_attr_setdetachstate(&attr,
315 PTHREAD_CREATE_DETACHED)) {
316 pthread_t thread;
317 reinit_running = true;
318 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
319 reinit_running = false;
320 }
321 }
322 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700323 }
324
Mark Salyzyn0175b072014-02-26 09:50:16 -0800325 if (drop_privs() != 0) {
326 return -1;
327 }
328
329 // Serves the purpose of managing the last logs times read on a
330 // socket connection, and as a reader lock on a range of log
331 // entries.
332
333 LastLogTimes *times = new LastLogTimes();
334
335 // LogBuffer is the object which is responsible for holding all
336 // log entries.
337
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700338 logBuf = new LogBuffer(times);
339
340 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800341
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700342 {
343 char property[PROPERTY_VALUE_MAX];
344 property_get("ro.build.type", property, "");
345 if (property_get_bool("logd.statistics",
346 !!strcmp(property, "user")
347 && !property_get_bool("ro.config.low_ram", false))) {
348 logBuf->enableStatistics();
349 }
350 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800351
Mark Salyzyn0175b072014-02-26 09:50:16 -0800352 // LogReader listens on /dev/socket/logdr. When a client
353 // connects, log entries in the LogBuffer are written to the client.
354
355 LogReader *reader = new LogReader(logBuf);
356 if (reader->startListener()) {
357 exit(1);
358 }
359
360 // LogListener listens on /dev/socket/logdw for client
361 // initiated log messages. New log entries are added to LogBuffer
362 // and LogReader is notified to send updates to connected clients.
363
364 LogListener *swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800365 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
366 if (swl->startListener(300)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800367 exit(1);
368 }
369
370 // Command listener listens on /dev/socket/logd for incoming logd
371 // administrative commands.
372
373 CommandListener *cl = new CommandListener(logBuf, reader, swl);
374 if (cl->startListener()) {
375 exit(1);
376 }
377
William Roberts29d238d2013-02-08 09:45:26 +0900378 // LogAudit listens on NETLINK_AUDIT socket for selinux
379 // initiated log messages. New log entries are added to LogBuffer
380 // and LogReader is notified to send updates to connected clients.
381
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700382 bool auditd = property_get_bool("logd.auditd", true);
383
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700384 if (auditd) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700385 bool dmesg = property_get_bool("logd.auditd.dmesg", true);
386
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700387 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700388 LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700389
390 int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
391 if (len > 0) {
392 len++;
393 char buf[len];
394
395 int rc = klogctl(KLOG_READ_ALL, buf, len);
396
Mark Salyzyn202e1532015-02-09 08:21:05 -0800397 if (rc >= 0) {
398 buf[len - 1] = '\0';
Mark Salyzyneb06de72014-10-13 09:59:37 -0700399
Mark Salyzyn202e1532015-02-09 08:21:05 -0800400 for (char *ptr, *tok = buf; (tok = strtok_r(tok, "\r\n", &ptr)); tok = NULL) {
401 al->log(tok);
402 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700403 }
404 }
405
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700406 if (al->startListener()) {
407 delete al;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700408 }
William Roberts29d238d2013-02-08 09:45:26 +0900409 }
410
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700411 TEMP_FAILURE_RETRY(pause());
412
Mark Salyzyn0175b072014-02-26 09:50:16 -0800413 exit(0);
414}