blob: 2d799bf7af6fbc6c40cc5018325222d6b3ee4bc7 [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 Salyzyne457b742014-02-19 17:18:31 -080032#include <unistd.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzyne457b742014-02-19 17:18:31 -080034#include <cutils/properties.h>
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080035#include <cutils/sched_policy.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070036#include <cutils/sockets.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080037
Mark Salyzyn0175b072014-02-26 09:50:16 -080038#include "private/android_filesystem_config.h"
39#include "CommandListener.h"
40#include "LogBuffer.h"
41#include "LogListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090042#include "LogAudit.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080043
Mark Salyzyndfc47e82014-03-24 10:26:47 -070044//
45// The service is designed to be run by init, it does not respond well
46// to starting up manually. When starting up manually the sockets will
47// fail to open typically for one of the following reasons:
48// EADDRINUSE if logger is running.
49// EACCESS if started without precautions (below)
50//
51// Here is a cookbook procedure for starting up logd manually assuming
52// init is out of the way, pedantically all permissions and selinux
53// security is put back in place:
54//
55// setenforce 0
56// rm /dev/socket/logd*
57// chmod 777 /dev/socket
58// # here is where you would attach the debugger or valgrind for example
59// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
60// sleep 1
61// chmod 755 /dev/socket
62// chown logd.logd /dev/socket/logd*
63// restorecon /dev/socket/logd*
64// setenforce 1
65//
66// If minimalism prevails, typical for debugging and security is not a concern:
67//
68// setenforce 0
69// chmod 777 /dev/socket
70// logd
71//
72
Mark Salyzyn0175b072014-02-26 09:50:16 -080073static int drop_privs() {
Mark Salyzyn882f8562013-12-26 15:13:36 -080074 struct sched_param param;
75 memset(&param, 0, sizeof(param));
76
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080077 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
78 return -1;
79 }
80
Mark Salyzyn882f8562013-12-26 15:13:36 -080081 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
82 return -1;
83 }
84
Mark Salyzyn0175b072014-02-26 09:50:16 -080085 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
86 return -1;
87 }
88
89 if (setgid(AID_LOGD) != 0) {
90 return -1;
91 }
92
93 if (setuid(AID_LOGD) != 0) {
94 return -1;
95 }
96
97 struct __user_cap_header_struct capheader;
98 struct __user_cap_data_struct capdata[2];
99 memset(&capheader, 0, sizeof(capheader));
100 memset(&capdata, 0, sizeof(capdata));
101 capheader.version = _LINUX_CAPABILITY_VERSION_3;
102 capheader.pid = 0;
103
104 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
William Roberts29d238d2013-02-08 09:45:26 +0900105 capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL);
106
107 capdata[0].effective = capdata[0].permitted;
108 capdata[1].effective = capdata[1].permitted;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800109 capdata[0].inheritable = 0;
110 capdata[1].inheritable = 0;
111
112 if (capset(&capheader, &capdata[0]) < 0) {
113 return -1;
114 }
115
116 return 0;
117}
118
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700119// Property helper
120static bool property_get_bool(const char *key, bool def) {
121 char property[PROPERTY_VALUE_MAX];
122 property_get(key, property, "");
123
124 if (!strcasecmp(property, "true")) {
125 return true;
126 }
127 if (!strcasecmp(property, "false")) {
128 return false;
129 }
130
131 return def;
132}
133
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700134static sem_t reinit;
135static bool reinit_running = false;
136static LogBuffer *logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700137
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700138static void *reinit_thread_start(void * /*obj*/) {
139 prctl(PR_SET_NAME, "logd.daemon");
140 set_sched_policy(0, SP_BACKGROUND);
141
142 setgid(AID_LOGD);
143 setuid(AID_LOGD);
144
145 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
146 // Anything that reads persist.<property>
147 if (logBuf) {
148 logBuf->init();
149 }
150 }
151
152 return NULL;
153}
154
155// Serves as a global method to trigger reinitialization
156// and as a function that can be provided to signal().
157void reinit_signal_handler(int /*signal*/) {
158 sem_post(&reinit);
159}
160
161// Remove the static, and use this variable
162// globally for debugging if necessary. eg:
163// write(fdDmesg, "I am here\n", 10);
164static int fdDmesg = -1;
165
166// Foreground waits for exit of the main persistent threads
167// that are started here. The threads are created to manage
168// UNIX domain client sockets for writing, reading and
169// controlling the user space logger, and for any additional
170// logging plugins like auditd and restart control. Additional
171// transitory per-client threads are created for each reader.
172int main(int argc, char *argv[]) {
173 fdDmesg = open("/dev/kmsg", O_WRONLY);
174
175 // issue reinit command. KISS argument parsing.
176 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
177 int sock = TEMP_FAILURE_RETRY(
178 socket_local_client("logd",
179 ANDROID_SOCKET_NAMESPACE_RESERVED,
180 SOCK_STREAM));
181 if (sock < 0) {
182 return -errno;
183 }
184 static const char reinit[] = "reinit";
185 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
186 if (ret < 0) {
187 return -errno;
188 }
189 struct pollfd p;
190 memset(&p, 0, sizeof(p));
191 p.fd = sock;
192 p.events = POLLIN;
193 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100));
194 if (ret < 0) {
195 return -errno;
196 }
197 if ((ret == 0) || !(p.revents & POLLIN)) {
198 return -ETIME;
199 }
200 static const char success[] = "success";
201 char buffer[sizeof(success) - 1];
202 memset(buffer, 0, sizeof(buffer));
203 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
204 if (ret < 0) {
205 return -errno;
206 }
207 return strncmp(buffer, success, sizeof(success) - 1) != 0;
208 }
209
210 // Reinit Thread
211 sem_init(&reinit, 0, 0);
212 pthread_attr_t attr;
213 if (!pthread_attr_init(&attr)) {
214 struct sched_param param;
215
216 memset(&param, 0, sizeof(param));
217 pthread_attr_setschedparam(&attr, &param);
218 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
219 if (!pthread_attr_setdetachstate(&attr,
220 PTHREAD_CREATE_DETACHED)) {
221 pthread_t thread;
222 reinit_running = true;
223 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
224 reinit_running = false;
225 }
226 }
227 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700228 }
229
Mark Salyzyn0175b072014-02-26 09:50:16 -0800230 if (drop_privs() != 0) {
231 return -1;
232 }
233
234 // Serves the purpose of managing the last logs times read on a
235 // socket connection, and as a reader lock on a range of log
236 // entries.
237
238 LastLogTimes *times = new LastLogTimes();
239
240 // LogBuffer is the object which is responsible for holding all
241 // log entries.
242
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700243 logBuf = new LogBuffer(times);
244
245 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800246
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700247 {
248 char property[PROPERTY_VALUE_MAX];
249 property_get("ro.build.type", property, "");
250 if (property_get_bool("logd.statistics",
251 !!strcmp(property, "user")
252 && !property_get_bool("ro.config.low_ram", false))) {
253 logBuf->enableStatistics();
254 }
255 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800256
Mark Salyzyn0175b072014-02-26 09:50:16 -0800257 // LogReader listens on /dev/socket/logdr. When a client
258 // connects, log entries in the LogBuffer are written to the client.
259
260 LogReader *reader = new LogReader(logBuf);
261 if (reader->startListener()) {
262 exit(1);
263 }
264
265 // LogListener listens on /dev/socket/logdw for client
266 // initiated log messages. New log entries are added to LogBuffer
267 // and LogReader is notified to send updates to connected clients.
268
269 LogListener *swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800270 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
271 if (swl->startListener(300)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800272 exit(1);
273 }
274
275 // Command listener listens on /dev/socket/logd for incoming logd
276 // administrative commands.
277
278 CommandListener *cl = new CommandListener(logBuf, reader, swl);
279 if (cl->startListener()) {
280 exit(1);
281 }
282
William Roberts29d238d2013-02-08 09:45:26 +0900283 // LogAudit listens on NETLINK_AUDIT socket for selinux
284 // initiated log messages. New log entries are added to LogBuffer
285 // and LogReader is notified to send updates to connected clients.
286
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700287 bool auditd = property_get_bool("logd.auditd", true);
288
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700289 if (auditd) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700290 bool dmesg = property_get_bool("logd.auditd.dmesg", true);
291
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700292 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700293 LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700294
295 int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
296 if (len > 0) {
297 len++;
298 char buf[len];
299
300 int rc = klogctl(KLOG_READ_ALL, buf, len);
301
302 buf[len - 1] = '\0';
303
304 for(char *ptr, *tok = buf;
305 (rc >= 0) && ((tok = strtok_r(tok, "\r\n", &ptr)));
306 tok = NULL) {
307 rc = al->log(tok);
308 }
309 }
310
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700311 if (al->startListener()) {
312 delete al;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700313 }
William Roberts29d238d2013-02-08 09:45:26 +0900314 }
315
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700316 TEMP_FAILURE_RETRY(pause());
317
Mark Salyzyn0175b072014-02-26 09:50:16 -0800318 exit(0);
319}