Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 20 | #include <poll.h> |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 21 | #include <sched.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 22 | #include <semaphore.h> |
| 23 | #include <signal.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/capability.h> |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 28 | #include <sys/klog.h> |
Elliott Hughes | e5a0f20 | 2014-07-18 17:39:41 -0700 | [diff] [blame] | 29 | #include <sys/prctl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 32 | #include <unistd.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 35 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 36 | #include <cutils/sockets.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 37 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 38 | #include "private/android_filesystem_config.h" |
| 39 | #include "CommandListener.h" |
| 40 | #include "LogBuffer.h" |
| 41 | #include "LogListener.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 42 | #include "LogAudit.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 43 | |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 44 | // |
| 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 73 | static int drop_privs() { |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 74 | struct sched_param param; |
| 75 | memset(¶m, 0, sizeof(param)); |
| 76 | |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 77 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 78 | return -1; |
| 79 | } |
| 80 | |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 81 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 82 | return -1; |
| 83 | } |
| 84 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 85 | 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 Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 105 | 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 109 | 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 Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 119 | // Property helper |
| 120 | static 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 134 | static sem_t reinit; |
| 135 | static bool reinit_running = false; |
| 136 | static LogBuffer *logBuf = NULL; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 137 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 138 | static 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(). |
| 157 | void 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); |
| 164 | static 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. |
| 172 | int 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(¶m, 0, sizeof(param)); |
| 217 | pthread_attr_setschedparam(&attr, ¶m); |
| 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 Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 230 | 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 243 | logBuf = new LogBuffer(times); |
| 244 | |
| 245 | signal(SIGHUP, reinit_signal_handler); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 246 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 247 | { |
| 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 Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 256 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 257 | // 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 Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 270 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 271 | if (swl->startListener(300)) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 272 | 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 Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 283 | // 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 287 | bool auditd = property_get_bool("logd.auditd", true); |
| 288 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 289 | if (auditd) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 290 | bool dmesg = property_get_bool("logd.auditd.dmesg", true); |
| 291 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 292 | // failure is an option ... messages are in dmesg (required by standard) |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 293 | LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 294 | |
| 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 Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 311 | if (al->startListener()) { |
| 312 | delete al; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 313 | } |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 314 | } |
| 315 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 316 | TEMP_FAILURE_RETRY(pause()); |
| 317 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 318 | exit(0); |
| 319 | } |