blob: 3ddc1df6e5d6734059ee3846f8a66253322923e7 [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 Salyzyn11e55cb2015-03-10 16:45:17 -0700146static sem_t reinit;
147static bool reinit_running = false;
148static LogBuffer *logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700149
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700150static void *reinit_thread_start(void * /*obj*/) {
151 prctl(PR_SET_NAME, "logd.daemon");
152 set_sched_policy(0, SP_BACKGROUND);
153
154 setgid(AID_LOGD);
155 setuid(AID_LOGD);
156
157 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzynccbadc62015-03-12 12:25:35 -0700158 if (fdDmesg >= 0) {
159 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
160 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
161 ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
162 write(fdDmesg, reinit_message, sizeof(reinit_message));
163 }
164
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700165 // Anything that reads persist.<property>
166 if (logBuf) {
167 logBuf->init();
168 }
169 }
170
171 return NULL;
172}
173
174// Serves as a global method to trigger reinitialization
175// and as a function that can be provided to signal().
176void reinit_signal_handler(int /*signal*/) {
177 sem_post(&reinit);
178}
179
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700180// Foreground waits for exit of the main persistent threads
181// that are started here. The threads are created to manage
182// UNIX domain client sockets for writing, reading and
183// controlling the user space logger, and for any additional
184// logging plugins like auditd and restart control. Additional
185// transitory per-client threads are created for each reader.
186int main(int argc, char *argv[]) {
187 fdDmesg = open("/dev/kmsg", O_WRONLY);
188
189 // issue reinit command. KISS argument parsing.
190 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
191 int sock = TEMP_FAILURE_RETRY(
192 socket_local_client("logd",
193 ANDROID_SOCKET_NAMESPACE_RESERVED,
194 SOCK_STREAM));
195 if (sock < 0) {
196 return -errno;
197 }
198 static const char reinit[] = "reinit";
199 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
200 if (ret < 0) {
201 return -errno;
202 }
203 struct pollfd p;
204 memset(&p, 0, sizeof(p));
205 p.fd = sock;
206 p.events = POLLIN;
207 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100));
208 if (ret < 0) {
209 return -errno;
210 }
211 if ((ret == 0) || !(p.revents & POLLIN)) {
212 return -ETIME;
213 }
214 static const char success[] = "success";
215 char buffer[sizeof(success) - 1];
216 memset(buffer, 0, sizeof(buffer));
217 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
218 if (ret < 0) {
219 return -errno;
220 }
221 return strncmp(buffer, success, sizeof(success) - 1) != 0;
222 }
223
224 // Reinit Thread
225 sem_init(&reinit, 0, 0);
226 pthread_attr_t attr;
227 if (!pthread_attr_init(&attr)) {
228 struct sched_param param;
229
230 memset(&param, 0, sizeof(param));
231 pthread_attr_setschedparam(&attr, &param);
232 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
233 if (!pthread_attr_setdetachstate(&attr,
234 PTHREAD_CREATE_DETACHED)) {
235 pthread_t thread;
236 reinit_running = true;
237 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
238 reinit_running = false;
239 }
240 }
241 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700242 }
243
Mark Salyzyn0175b072014-02-26 09:50:16 -0800244 if (drop_privs() != 0) {
245 return -1;
246 }
247
248 // Serves the purpose of managing the last logs times read on a
249 // socket connection, and as a reader lock on a range of log
250 // entries.
251
252 LastLogTimes *times = new LastLogTimes();
253
254 // LogBuffer is the object which is responsible for holding all
255 // log entries.
256
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700257 logBuf = new LogBuffer(times);
258
259 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800260
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700261 {
262 char property[PROPERTY_VALUE_MAX];
263 property_get("ro.build.type", property, "");
264 if (property_get_bool("logd.statistics",
265 !!strcmp(property, "user")
266 && !property_get_bool("ro.config.low_ram", false))) {
267 logBuf->enableStatistics();
268 }
269 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800270
Mark Salyzyn0175b072014-02-26 09:50:16 -0800271 // LogReader listens on /dev/socket/logdr. When a client
272 // connects, log entries in the LogBuffer are written to the client.
273
274 LogReader *reader = new LogReader(logBuf);
275 if (reader->startListener()) {
276 exit(1);
277 }
278
279 // LogListener listens on /dev/socket/logdw for client
280 // initiated log messages. New log entries are added to LogBuffer
281 // and LogReader is notified to send updates to connected clients.
282
283 LogListener *swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800284 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
285 if (swl->startListener(300)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800286 exit(1);
287 }
288
289 // Command listener listens on /dev/socket/logd for incoming logd
290 // administrative commands.
291
292 CommandListener *cl = new CommandListener(logBuf, reader, swl);
293 if (cl->startListener()) {
294 exit(1);
295 }
296
William Roberts29d238d2013-02-08 09:45:26 +0900297 // LogAudit listens on NETLINK_AUDIT socket for selinux
298 // initiated log messages. New log entries are added to LogBuffer
299 // and LogReader is notified to send updates to connected clients.
300
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700301 bool auditd = property_get_bool("logd.auditd", true);
302
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700303 if (auditd) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700304 bool dmesg = property_get_bool("logd.auditd.dmesg", true);
305
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700306 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700307 LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700308
309 int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
310 if (len > 0) {
311 len++;
312 char buf[len];
313
314 int rc = klogctl(KLOG_READ_ALL, buf, len);
315
316 buf[len - 1] = '\0';
317
318 for(char *ptr, *tok = buf;
319 (rc >= 0) && ((tok = strtok_r(tok, "\r\n", &ptr)));
320 tok = NULL) {
321 rc = al->log(tok);
322 }
323 }
324
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700325 if (al->startListener()) {
326 delete al;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700327 }
William Roberts29d238d2013-02-08 09:45:26 +0900328 }
329
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700330 TEMP_FAILURE_RETRY(pause());
331
Mark Salyzyn0175b072014-02-26 09:50:16 -0800332 exit(0);
333}