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