blob: 237c7c1cf279747ed503b79ea513dda8eca19ba5 [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 Salyzyne3aeeee2015-03-17 07:56:32 -070038#include <private/android_filesystem_config.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080039
Mark Salyzyn0175b072014-02-26 09:50:16 -080040#include "CommandListener.h"
41#include "LogBuffer.h"
42#include "LogListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090043#include "LogAudit.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080044
Mark Salyzynccbadc62015-03-12 12:25:35 -070045#define KMSG_PRIORITY(PRI) \
46 '<', \
47 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
48 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
49 '>'
50
Mark Salyzyndfc47e82014-03-24 10:26:47 -070051//
52// The service is designed to be run by init, it does not respond well
53// to starting up manually. When starting up manually the sockets will
54// fail to open typically for one of the following reasons:
55// EADDRINUSE if logger is running.
56// EACCESS if started without precautions (below)
57//
58// Here is a cookbook procedure for starting up logd manually assuming
59// init is out of the way, pedantically all permissions and selinux
60// security is put back in place:
61//
62// setenforce 0
63// rm /dev/socket/logd*
64// chmod 777 /dev/socket
65// # here is where you would attach the debugger or valgrind for example
66// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
67// sleep 1
68// chmod 755 /dev/socket
69// chown logd.logd /dev/socket/logd*
70// restorecon /dev/socket/logd*
71// setenforce 1
72//
73// If minimalism prevails, typical for debugging and security is not a concern:
74//
75// setenforce 0
76// chmod 777 /dev/socket
77// logd
78//
79
Mark Salyzyn0175b072014-02-26 09:50:16 -080080static int drop_privs() {
Mark Salyzyn882f8562013-12-26 15:13:36 -080081 struct sched_param param;
82 memset(&param, 0, sizeof(param));
83
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080084 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
85 return -1;
86 }
87
Mark Salyzyn882f8562013-12-26 15:13:36 -080088 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
89 return -1;
90 }
91
Mark Salyzyn0175b072014-02-26 09:50:16 -080092 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
93 return -1;
94 }
95
96 if (setgid(AID_LOGD) != 0) {
97 return -1;
98 }
99
100 if (setuid(AID_LOGD) != 0) {
101 return -1;
102 }
103
104 struct __user_cap_header_struct capheader;
105 struct __user_cap_data_struct capdata[2];
106 memset(&capheader, 0, sizeof(capheader));
107 memset(&capdata, 0, sizeof(capdata));
108 capheader.version = _LINUX_CAPABILITY_VERSION_3;
109 capheader.pid = 0;
110
111 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
William Roberts29d238d2013-02-08 09:45:26 +0900112 capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL);
113
114 capdata[0].effective = capdata[0].permitted;
115 capdata[1].effective = capdata[1].permitted;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116 capdata[0].inheritable = 0;
117 capdata[1].inheritable = 0;
118
119 if (capset(&capheader, &capdata[0]) < 0) {
120 return -1;
121 }
122
123 return 0;
124}
125
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700126// Property helper
127static bool property_get_bool(const char *key, bool def) {
128 char property[PROPERTY_VALUE_MAX];
129 property_get(key, property, "");
130
131 if (!strcasecmp(property, "true")) {
132 return true;
133 }
134 if (!strcasecmp(property, "false")) {
135 return false;
136 }
137
138 return def;
139}
140
Mark Salyzynccbadc62015-03-12 12:25:35 -0700141// Remove the static, and use this variable
142// globally for debugging if necessary. eg:
143// write(fdDmesg, "I am here\n", 10);
144static int fdDmesg = -1;
145
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700146static sem_t uidName;
147static uid_t uid;
148static char *name;
149
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700150static sem_t reinit;
151static bool reinit_running = false;
152static LogBuffer *logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700153
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700154static void *reinit_thread_start(void * /*obj*/) {
155 prctl(PR_SET_NAME, "logd.daemon");
156 set_sched_policy(0, SP_BACKGROUND);
157
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700158 setgid(AID_SYSTEM);
159 setuid(AID_SYSTEM);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700160
161 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700162
163 // uidToName Privileged Worker
164 if (uid) {
165 name = NULL;
166
167 FILE *fp = fopen("/data/system/packages.list", "r");
168 if (fp) {
169 // This simple parser is sensitive to format changes in
170 // frameworks/base/services/core/java/com/android/server/pm/Settings.java
171 // A dependency note has been added to that file to correct
172 // this parser.
173
174 char *buffer = NULL;
175 size_t len;
176 while (getline(&buffer, &len, fp) > 0) {
177 char *userId = strchr(buffer, ' ');
178 if (!userId) {
179 continue;
180 }
181 *userId = '\0';
182 unsigned long value = strtoul(userId + 1, NULL, 10);
183 if (value != uid) {
184 continue;
185 }
186 name = strdup(buffer);
187 break;
188 }
189 free(buffer);
190 fclose(fp);
191 }
192 uid = 0;
193 sem_post(&uidName);
194 continue;
195 }
196
Mark Salyzynccbadc62015-03-12 12:25:35 -0700197 if (fdDmesg >= 0) {
198 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
199 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
200 ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
201 write(fdDmesg, reinit_message, sizeof(reinit_message));
202 }
203
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700204 // Anything that reads persist.<property>
205 if (logBuf) {
206 logBuf->init();
207 }
208 }
209
210 return NULL;
211}
212
Mark Salyzyn95108f12015-04-20 07:26:27 -0700213static sem_t sem_name;
214
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700215char *android::uidToName(uid_t u) {
216 if (!u || !reinit_running) {
217 return NULL;
218 }
219
Mark Salyzyn95108f12015-04-20 07:26:27 -0700220 sem_wait(&sem_name);
221
222 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700223 uid = u;
224
225 name = NULL;
226 sem_post(&reinit);
227 sem_wait(&uidName);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700228 char *ret = name;
229
230 sem_post(&sem_name);
231
232 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700233}
234
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700235// Serves as a global method to trigger reinitialization
236// and as a function that can be provided to signal().
237void reinit_signal_handler(int /*signal*/) {
238 sem_post(&reinit);
239}
240
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700241// Foreground waits for exit of the main persistent threads
242// that are started here. The threads are created to manage
243// UNIX domain client sockets for writing, reading and
244// controlling the user space logger, and for any additional
245// logging plugins like auditd and restart control. Additional
246// transitory per-client threads are created for each reader.
247int main(int argc, char *argv[]) {
248 fdDmesg = open("/dev/kmsg", O_WRONLY);
249
250 // issue reinit command. KISS argument parsing.
251 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
252 int sock = TEMP_FAILURE_RETRY(
253 socket_local_client("logd",
254 ANDROID_SOCKET_NAMESPACE_RESERVED,
255 SOCK_STREAM));
256 if (sock < 0) {
257 return -errno;
258 }
259 static const char reinit[] = "reinit";
260 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
261 if (ret < 0) {
262 return -errno;
263 }
264 struct pollfd p;
265 memset(&p, 0, sizeof(p));
266 p.fd = sock;
267 p.events = POLLIN;
268 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100));
269 if (ret < 0) {
270 return -errno;
271 }
272 if ((ret == 0) || !(p.revents & POLLIN)) {
273 return -ETIME;
274 }
275 static const char success[] = "success";
276 char buffer[sizeof(success) - 1];
277 memset(buffer, 0, sizeof(buffer));
278 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
279 if (ret < 0) {
280 return -errno;
281 }
282 return strncmp(buffer, success, sizeof(success) - 1) != 0;
283 }
284
285 // Reinit Thread
286 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700287 sem_init(&uidName, 0, 0);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700288 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700289 pthread_attr_t attr;
290 if (!pthread_attr_init(&attr)) {
291 struct sched_param param;
292
293 memset(&param, 0, sizeof(param));
294 pthread_attr_setschedparam(&attr, &param);
295 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
296 if (!pthread_attr_setdetachstate(&attr,
297 PTHREAD_CREATE_DETACHED)) {
298 pthread_t thread;
299 reinit_running = true;
300 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
301 reinit_running = false;
302 }
303 }
304 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700305 }
306
Mark Salyzyn0175b072014-02-26 09:50:16 -0800307 if (drop_privs() != 0) {
308 return -1;
309 }
310
311 // Serves the purpose of managing the last logs times read on a
312 // socket connection, and as a reader lock on a range of log
313 // entries.
314
315 LastLogTimes *times = new LastLogTimes();
316
317 // LogBuffer is the object which is responsible for holding all
318 // log entries.
319
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700320 logBuf = new LogBuffer(times);
321
322 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800323
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700324 {
325 char property[PROPERTY_VALUE_MAX];
326 property_get("ro.build.type", property, "");
327 if (property_get_bool("logd.statistics",
328 !!strcmp(property, "user")
329 && !property_get_bool("ro.config.low_ram", false))) {
330 logBuf->enableStatistics();
331 }
332 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800333
Mark Salyzyn0175b072014-02-26 09:50:16 -0800334 // LogReader listens on /dev/socket/logdr. When a client
335 // connects, log entries in the LogBuffer are written to the client.
336
337 LogReader *reader = new LogReader(logBuf);
338 if (reader->startListener()) {
339 exit(1);
340 }
341
342 // LogListener listens on /dev/socket/logdw for client
343 // initiated log messages. New log entries are added to LogBuffer
344 // and LogReader is notified to send updates to connected clients.
345
346 LogListener *swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800347 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
348 if (swl->startListener(300)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800349 exit(1);
350 }
351
352 // Command listener listens on /dev/socket/logd for incoming logd
353 // administrative commands.
354
355 CommandListener *cl = new CommandListener(logBuf, reader, swl);
356 if (cl->startListener()) {
357 exit(1);
358 }
359
William Roberts29d238d2013-02-08 09:45:26 +0900360 // LogAudit listens on NETLINK_AUDIT socket for selinux
361 // initiated log messages. New log entries are added to LogBuffer
362 // and LogReader is notified to send updates to connected clients.
363
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700364 bool auditd = property_get_bool("logd.auditd", true);
365
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700366 if (auditd) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700367 bool dmesg = property_get_bool("logd.auditd.dmesg", true);
368
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700369 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700370 LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700371
372 int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
373 if (len > 0) {
374 len++;
375 char buf[len];
376
377 int rc = klogctl(KLOG_READ_ALL, buf, len);
378
Mark Salyzyn202e1532015-02-09 08:21:05 -0800379 if (rc >= 0) {
380 buf[len - 1] = '\0';
Mark Salyzyneb06de72014-10-13 09:59:37 -0700381
Mark Salyzyn202e1532015-02-09 08:21:05 -0800382 for (char *ptr, *tok = buf; (tok = strtok_r(tok, "\r\n", &ptr)); tok = NULL) {
383 al->log(tok);
384 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700385 }
386 }
387
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700388 if (al->startListener()) {
389 delete al;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700390 }
William Roberts29d238d2013-02-08 09:45:26 +0900391 }
392
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700393 TEMP_FAILURE_RETRY(pause());
394
Mark Salyzyn0175b072014-02-26 09:50:16 -0800395 exit(0);
396}