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 | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 32 | #include <syslog.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 33 | #include <unistd.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 35 | #include <cutils/properties.h> |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 36 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 38 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 39 | #include "private/android_filesystem_config.h" |
| 40 | #include "CommandListener.h" |
| 41 | #include "LogBuffer.h" |
| 42 | #include "LogListener.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 43 | #include "LogAudit.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 44 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 45 | #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 Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 51 | // |
| 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 80 | static int drop_privs() { |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 81 | struct sched_param param; |
| 82 | memset(¶m, 0, sizeof(param)); |
| 83 | |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 84 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 85 | return -1; |
| 86 | } |
| 87 | |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 88 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 89 | return -1; |
| 90 | } |
| 91 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 92 | 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 Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 112 | 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 116 | 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 Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 126 | // Property helper |
| 127 | static 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 Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 141 | // Remove the static, and use this variable |
| 142 | // globally for debugging if necessary. eg: |
| 143 | // write(fdDmesg, "I am here\n", 10); |
| 144 | static int fdDmesg = -1; |
| 145 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 146 | static sem_t reinit; |
| 147 | static bool reinit_running = false; |
| 148 | static LogBuffer *logBuf = NULL; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 149 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 150 | static 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 Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 158 | 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 165 | // 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(). |
| 176 | void reinit_signal_handler(int /*signal*/) { |
| 177 | sem_post(&reinit); |
| 178 | } |
| 179 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 180 | // 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. |
| 186 | int 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(¶m, 0, sizeof(param)); |
| 231 | pthread_attr_setschedparam(&attr, ¶m); |
| 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 Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 244 | 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 257 | logBuf = new LogBuffer(times); |
| 258 | |
| 259 | signal(SIGHUP, reinit_signal_handler); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 260 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 261 | { |
| 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 Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 270 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 271 | // 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 Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 284 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 285 | if (swl->startListener(300)) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 286 | 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 Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 297 | // 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 Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 301 | bool auditd = property_get_bool("logd.auditd", true); |
| 302 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 303 | if (auditd) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 304 | bool dmesg = property_get_bool("logd.auditd.dmesg", true); |
| 305 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 306 | // failure is an option ... messages are in dmesg (required by standard) |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 307 | LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 308 | |
| 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 Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 325 | if (al->startListener()) { |
| 326 | delete al; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 327 | } |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 328 | } |
| 329 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 330 | TEMP_FAILURE_RETRY(pause()); |
| 331 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 332 | exit(0); |
| 333 | } |