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> |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 30 | #include <sys/resource.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 33 | #include <syslog.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 34 | #include <unistd.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 36 | #include <memory> |
| 37 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 39 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 40 | #include <cutils/sockets.h> |
Mark Salyzyn | ff32f3c | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 41 | #include <log/event_tag_map.h> |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 42 | #include <private/android_filesystem_config.h> |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 43 | #include <utils/threads.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 44 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | #include "CommandListener.h" |
| 46 | #include "LogBuffer.h" |
| 47 | #include "LogListener.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 48 | #include "LogAudit.h" |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 49 | #include "LogKlog.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 50 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 51 | #define KMSG_PRIORITY(PRI) \ |
| 52 | '<', \ |
| 53 | '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \ |
| 54 | '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \ |
| 55 | '>' |
| 56 | |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 57 | // |
| 58 | // The service is designed to be run by init, it does not respond well |
| 59 | // to starting up manually. When starting up manually the sockets will |
| 60 | // fail to open typically for one of the following reasons: |
| 61 | // EADDRINUSE if logger is running. |
| 62 | // EACCESS if started without precautions (below) |
| 63 | // |
| 64 | // Here is a cookbook procedure for starting up logd manually assuming |
| 65 | // init is out of the way, pedantically all permissions and selinux |
| 66 | // security is put back in place: |
| 67 | // |
| 68 | // setenforce 0 |
| 69 | // rm /dev/socket/logd* |
| 70 | // chmod 777 /dev/socket |
| 71 | // # here is where you would attach the debugger or valgrind for example |
| 72 | // runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 & |
| 73 | // sleep 1 |
| 74 | // chmod 755 /dev/socket |
| 75 | // chown logd.logd /dev/socket/logd* |
| 76 | // restorecon /dev/socket/logd* |
| 77 | // setenforce 1 |
| 78 | // |
| 79 | // If minimalism prevails, typical for debugging and security is not a concern: |
| 80 | // |
| 81 | // setenforce 0 |
| 82 | // chmod 777 /dev/socket |
| 83 | // logd |
| 84 | // |
| 85 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 86 | static int drop_privs() { |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 87 | struct sched_param param; |
| 88 | memset(¶m, 0, sizeof(param)); |
| 89 | |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 90 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 91 | return -1; |
| 92 | } |
| 93 | |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 94 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 98 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 99 | return -1; |
| 100 | } |
| 101 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 102 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 103 | return -1; |
| 104 | } |
| 105 | |
Jeff Vander Stoep | 3f62a02 | 2015-07-23 15:18:36 -0700 | [diff] [blame] | 106 | if (setgroups(0, NULL) == -1) { |
| 107 | return -1; |
| 108 | } |
| 109 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 110 | if (setgid(AID_LOGD) != 0) { |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | if (setuid(AID_LOGD) != 0) { |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | struct __user_cap_header_struct capheader; |
| 119 | struct __user_cap_data_struct capdata[2]; |
| 120 | memset(&capheader, 0, sizeof(capheader)); |
| 121 | memset(&capdata, 0, sizeof(capdata)); |
| 122 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 123 | capheader.pid = 0; |
| 124 | |
| 125 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 126 | capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL); |
| 127 | |
| 128 | capdata[0].effective = capdata[0].permitted; |
| 129 | capdata[1].effective = capdata[1].permitted; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 130 | capdata[0].inheritable = 0; |
| 131 | capdata[1].inheritable = 0; |
| 132 | |
| 133 | if (capset(&capheader, &capdata[0]) < 0) { |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 140 | // Property helper |
| 141 | static bool property_get_bool(const char *key, bool def) { |
| 142 | char property[PROPERTY_VALUE_MAX]; |
| 143 | property_get(key, property, ""); |
| 144 | |
| 145 | if (!strcasecmp(property, "true")) { |
| 146 | return true; |
| 147 | } |
| 148 | if (!strcasecmp(property, "false")) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | return def; |
| 153 | } |
| 154 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 155 | // Remove the static, and use this variable |
| 156 | // globally for debugging if necessary. eg: |
| 157 | // write(fdDmesg, "I am here\n", 10); |
| 158 | static int fdDmesg = -1; |
| 159 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 160 | static sem_t uidName; |
| 161 | static uid_t uid; |
| 162 | static char *name; |
| 163 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 164 | static sem_t reinit; |
| 165 | static bool reinit_running = false; |
| 166 | static LogBuffer *logBuf = NULL; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 167 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 168 | static void *reinit_thread_start(void * /*obj*/) { |
| 169 | prctl(PR_SET_NAME, "logd.daemon"); |
| 170 | set_sched_policy(0, SP_BACKGROUND); |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 171 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 172 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 173 | setgid(AID_SYSTEM); |
| 174 | setuid(AID_SYSTEM); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 175 | |
| 176 | while (reinit_running && !sem_wait(&reinit) && reinit_running) { |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 177 | |
| 178 | // uidToName Privileged Worker |
| 179 | if (uid) { |
| 180 | name = NULL; |
| 181 | |
| 182 | FILE *fp = fopen("/data/system/packages.list", "r"); |
| 183 | if (fp) { |
| 184 | // This simple parser is sensitive to format changes in |
| 185 | // frameworks/base/services/core/java/com/android/server/pm/Settings.java |
| 186 | // A dependency note has been added to that file to correct |
| 187 | // this parser. |
| 188 | |
| 189 | char *buffer = NULL; |
| 190 | size_t len; |
| 191 | while (getline(&buffer, &len, fp) > 0) { |
| 192 | char *userId = strchr(buffer, ' '); |
| 193 | if (!userId) { |
| 194 | continue; |
| 195 | } |
| 196 | *userId = '\0'; |
| 197 | unsigned long value = strtoul(userId + 1, NULL, 10); |
| 198 | if (value != uid) { |
| 199 | continue; |
| 200 | } |
| 201 | name = strdup(buffer); |
| 202 | break; |
| 203 | } |
| 204 | free(buffer); |
| 205 | fclose(fp); |
| 206 | } |
| 207 | uid = 0; |
| 208 | sem_post(&uidName); |
| 209 | continue; |
| 210 | } |
| 211 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 212 | if (fdDmesg >= 0) { |
| 213 | static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), |
| 214 | 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':', |
| 215 | ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' }; |
| 216 | write(fdDmesg, reinit_message, sizeof(reinit_message)); |
| 217 | } |
| 218 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 219 | // Anything that reads persist.<property> |
| 220 | if (logBuf) { |
| 221 | logBuf->init(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 228 | static sem_t sem_name; |
| 229 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 230 | char *android::uidToName(uid_t u) { |
| 231 | if (!u || !reinit_running) { |
| 232 | return NULL; |
| 233 | } |
| 234 | |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 235 | sem_wait(&sem_name); |
| 236 | |
| 237 | // Not multi-thread safe, we use sem_name to protect |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 238 | uid = u; |
| 239 | |
| 240 | name = NULL; |
| 241 | sem_post(&reinit); |
| 242 | sem_wait(&uidName); |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 243 | char *ret = name; |
| 244 | |
| 245 | sem_post(&sem_name); |
| 246 | |
| 247 | return ret; |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 250 | // Serves as a global method to trigger reinitialization |
| 251 | // and as a function that can be provided to signal(). |
| 252 | void reinit_signal_handler(int /*signal*/) { |
| 253 | sem_post(&reinit); |
| 254 | } |
| 255 | |
Mark Salyzyn | ff32f3c | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 256 | // tagToName converts an events tag into a name |
| 257 | const char *android::tagToName(uint32_t tag) { |
| 258 | static const EventTagMap *map; |
| 259 | |
| 260 | if (!map) { |
| 261 | sem_wait(&sem_name); |
| 262 | if (!map) { |
| 263 | map = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 264 | } |
| 265 | sem_post(&sem_name); |
| 266 | if (!map) { |
| 267 | return NULL; |
| 268 | } |
| 269 | } |
| 270 | return android_lookupEventTag(map, tag); |
| 271 | } |
| 272 | |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 273 | static bool property_get_bool_svelte(const char *key) { |
| 274 | bool not_user; |
| 275 | { |
| 276 | char property[PROPERTY_VALUE_MAX]; |
| 277 | property_get("ro.build.type", property, ""); |
| 278 | not_user = !!strcmp(property, "user"); |
| 279 | } |
| 280 | return property_get_bool(key, not_user |
| 281 | && !property_get_bool("ro.config.low_ram", false)); |
| 282 | } |
| 283 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 284 | static void readDmesg(LogAudit *al, LogKlog *kl) { |
| 285 | if (!al && !kl) { |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
| 290 | if (len <= 0) { |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | len += 1024; // Margin for additional input race or trailing nul |
| 295 | std::unique_ptr<char []> buf(new char[len]); |
| 296 | |
| 297 | int rc = klogctl(KLOG_READ_ALL, buf.get(), len); |
| 298 | if (rc <= 0) { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (rc < len) { |
| 303 | len = rc + 1; |
| 304 | } |
| 305 | buf[len - 1] = '\0'; |
| 306 | |
| 307 | if (kl) { |
| 308 | kl->synchronize(buf.get()); |
| 309 | } |
| 310 | |
| 311 | for (char *ptr = NULL, *tok = buf.get(); |
| 312 | (rc >= 0) && ((tok = log_strtok_r(tok, &ptr))); |
| 313 | tok = NULL) { |
| 314 | if (al) { |
| 315 | rc = al->log(tok); |
| 316 | } |
| 317 | if (kl) { |
| 318 | rc = kl->log(tok); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 323 | // Foreground waits for exit of the main persistent threads |
| 324 | // that are started here. The threads are created to manage |
| 325 | // UNIX domain client sockets for writing, reading and |
| 326 | // controlling the user space logger, and for any additional |
| 327 | // logging plugins like auditd and restart control. Additional |
| 328 | // transitory per-client threads are created for each reader. |
| 329 | int main(int argc, char *argv[]) { |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 330 | int fdPmesg = -1; |
| 331 | bool klogd = property_get_bool_svelte("logd.klogd"); |
| 332 | if (klogd) { |
| 333 | fdPmesg = open("/proc/kmsg", O_RDONLY | O_NDELAY); |
| 334 | } |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 335 | fdDmesg = open("/dev/kmsg", O_WRONLY); |
| 336 | |
| 337 | // issue reinit command. KISS argument parsing. |
| 338 | if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) { |
| 339 | int sock = TEMP_FAILURE_RETRY( |
| 340 | socket_local_client("logd", |
| 341 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 342 | SOCK_STREAM)); |
| 343 | if (sock < 0) { |
| 344 | return -errno; |
| 345 | } |
| 346 | static const char reinit[] = "reinit"; |
| 347 | ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit))); |
| 348 | if (ret < 0) { |
| 349 | return -errno; |
| 350 | } |
| 351 | struct pollfd p; |
| 352 | memset(&p, 0, sizeof(p)); |
| 353 | p.fd = sock; |
| 354 | p.events = POLLIN; |
| 355 | ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100)); |
| 356 | if (ret < 0) { |
| 357 | return -errno; |
| 358 | } |
| 359 | if ((ret == 0) || !(p.revents & POLLIN)) { |
| 360 | return -ETIME; |
| 361 | } |
| 362 | static const char success[] = "success"; |
| 363 | char buffer[sizeof(success) - 1]; |
| 364 | memset(buffer, 0, sizeof(buffer)); |
| 365 | ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); |
| 366 | if (ret < 0) { |
| 367 | return -errno; |
| 368 | } |
| 369 | return strncmp(buffer, success, sizeof(success) - 1) != 0; |
| 370 | } |
| 371 | |
| 372 | // Reinit Thread |
| 373 | sem_init(&reinit, 0, 0); |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 374 | sem_init(&uidName, 0, 0); |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 375 | sem_init(&sem_name, 0, 1); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 376 | pthread_attr_t attr; |
| 377 | if (!pthread_attr_init(&attr)) { |
| 378 | struct sched_param param; |
| 379 | |
| 380 | memset(¶m, 0, sizeof(param)); |
| 381 | pthread_attr_setschedparam(&attr, ¶m); |
| 382 | pthread_attr_setschedpolicy(&attr, SCHED_BATCH); |
| 383 | if (!pthread_attr_setdetachstate(&attr, |
| 384 | PTHREAD_CREATE_DETACHED)) { |
| 385 | pthread_t thread; |
| 386 | reinit_running = true; |
| 387 | if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) { |
| 388 | reinit_running = false; |
| 389 | } |
| 390 | } |
| 391 | pthread_attr_destroy(&attr); |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 394 | if (drop_privs() != 0) { |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | // Serves the purpose of managing the last logs times read on a |
| 399 | // socket connection, and as a reader lock on a range of log |
| 400 | // entries. |
| 401 | |
| 402 | LastLogTimes *times = new LastLogTimes(); |
| 403 | |
| 404 | // LogBuffer is the object which is responsible for holding all |
| 405 | // log entries. |
| 406 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 407 | logBuf = new LogBuffer(times); |
| 408 | |
| 409 | signal(SIGHUP, reinit_signal_handler); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 410 | |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 411 | if (property_get_bool_svelte("logd.statistics")) { |
| 412 | logBuf->enableStatistics(); |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 413 | } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 414 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 415 | // LogReader listens on /dev/socket/logdr. When a client |
| 416 | // connects, log entries in the LogBuffer are written to the client. |
| 417 | |
| 418 | LogReader *reader = new LogReader(logBuf); |
| 419 | if (reader->startListener()) { |
| 420 | exit(1); |
| 421 | } |
| 422 | |
| 423 | // LogListener listens on /dev/socket/logdw for client |
| 424 | // initiated log messages. New log entries are added to LogBuffer |
| 425 | // and LogReader is notified to send updates to connected clients. |
| 426 | |
| 427 | LogListener *swl = new LogListener(logBuf, reader); |
Mark Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 428 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 429 | if (swl->startListener(300)) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 430 | exit(1); |
| 431 | } |
| 432 | |
| 433 | // Command listener listens on /dev/socket/logd for incoming logd |
| 434 | // administrative commands. |
| 435 | |
| 436 | CommandListener *cl = new CommandListener(logBuf, reader, swl); |
| 437 | if (cl->startListener()) { |
| 438 | exit(1); |
| 439 | } |
| 440 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 441 | // LogAudit listens on NETLINK_AUDIT socket for selinux |
| 442 | // initiated log messages. New log entries are added to LogBuffer |
| 443 | // and LogReader is notified to send updates to connected clients. |
| 444 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 445 | bool auditd = property_get_bool("logd.auditd", true); |
| 446 | |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 447 | LogAudit *al = NULL; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 448 | if (auditd) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 449 | bool dmesg = property_get_bool("logd.auditd.dmesg", true); |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 450 | al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1); |
| 451 | } |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 452 | |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 453 | LogKlog *kl = NULL; |
| 454 | if (klogd) { |
| 455 | kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL); |
| 456 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 457 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 458 | readDmesg(al, kl); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 459 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 460 | // failure is an option ... messages are in dmesg (required by standard) |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 461 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 462 | if (kl && kl->startListener()) { |
| 463 | delete kl; |
| 464 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 465 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 466 | if (al && al->startListener()) { |
| 467 | delete al; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 468 | } |
| 469 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 470 | TEMP_FAILURE_RETRY(pause()); |
| 471 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 472 | exit(0); |
| 473 | } |