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 | |
William Roberts | aeca97b | 2015-07-31 13:10:36 -0700 | [diff] [blame] | 36 | #include <cstdbool> |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 37 | #include <memory> |
| 38 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 39 | #include <cutils/properties.h> |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 40 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 41 | #include <cutils/sockets.h> |
Mark Salyzyn | ff32f3c | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 42 | #include <log/event_tag_map.h> |
William Roberts | aeca97b | 2015-07-31 13:10:36 -0700 | [diff] [blame] | 43 | #include <packagelistparser/packagelistparser.h> |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 45 | #include <utils/threads.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 46 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 47 | #include "CommandListener.h" |
| 48 | #include "LogBuffer.h" |
| 49 | #include "LogListener.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 50 | #include "LogAudit.h" |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 51 | #include "LogKlog.h" |
Mark Salyzyn | 5ac5c6b | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 52 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 53 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 54 | #define KMSG_PRIORITY(PRI) \ |
| 55 | '<', \ |
| 56 | '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \ |
| 57 | '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \ |
| 58 | '>' |
| 59 | |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 60 | // |
| 61 | // The service is designed to be run by init, it does not respond well |
| 62 | // to starting up manually. When starting up manually the sockets will |
| 63 | // fail to open typically for one of the following reasons: |
| 64 | // EADDRINUSE if logger is running. |
| 65 | // EACCESS if started without precautions (below) |
| 66 | // |
| 67 | // Here is a cookbook procedure for starting up logd manually assuming |
| 68 | // init is out of the way, pedantically all permissions and selinux |
| 69 | // security is put back in place: |
| 70 | // |
| 71 | // setenforce 0 |
| 72 | // rm /dev/socket/logd* |
| 73 | // chmod 777 /dev/socket |
| 74 | // # here is where you would attach the debugger or valgrind for example |
| 75 | // runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 & |
| 76 | // sleep 1 |
| 77 | // chmod 755 /dev/socket |
| 78 | // chown logd.logd /dev/socket/logd* |
| 79 | // restorecon /dev/socket/logd* |
| 80 | // setenforce 1 |
| 81 | // |
| 82 | // If minimalism prevails, typical for debugging and security is not a concern: |
| 83 | // |
| 84 | // setenforce 0 |
| 85 | // chmod 777 /dev/socket |
| 86 | // logd |
| 87 | // |
| 88 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 89 | static int drop_privs() { |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 90 | struct sched_param param; |
| 91 | memset(¶m, 0, sizeof(param)); |
| 92 | |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 93 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 94 | return -1; |
| 95 | } |
| 96 | |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 97 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 98 | return -1; |
| 99 | } |
| 100 | |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 101 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 102 | return -1; |
| 103 | } |
| 104 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 105 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 106 | return -1; |
| 107 | } |
| 108 | |
Nick Kralevich | c39ba5a | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 109 | gid_t groups[] = { AID_READPROC }; |
| 110 | |
| 111 | if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) == -1) { |
Jeff Vander Stoep | 3f62a02 | 2015-07-23 15:18:36 -0700 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 115 | if (setgid(AID_LOGD) != 0) { |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | if (setuid(AID_LOGD) != 0) { |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | struct __user_cap_header_struct capheader; |
| 124 | struct __user_cap_data_struct capdata[2]; |
| 125 | memset(&capheader, 0, sizeof(capheader)); |
| 126 | memset(&capdata, 0, sizeof(capdata)); |
| 127 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 128 | capheader.pid = 0; |
| 129 | |
| 130 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 131 | capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL); |
| 132 | |
| 133 | capdata[0].effective = capdata[0].permitted; |
| 134 | capdata[1].effective = capdata[1].permitted; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 135 | capdata[0].inheritable = 0; |
| 136 | capdata[1].inheritable = 0; |
| 137 | |
| 138 | if (capset(&capheader, &capdata[0]) < 0) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 145 | // Property helper |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 146 | static bool check_flag(const char *prop, const char *flag) { |
| 147 | const char *cp = strcasestr(prop, flag); |
| 148 | if (!cp) { |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 151 | // We only will document comma (,) |
| 152 | static const char sep[] = ",:;|+ \t\f"; |
| 153 | if ((cp != prop) && !strchr(sep, cp[-1])) { |
| 154 | return false; |
| 155 | } |
| 156 | cp += strlen(flag); |
| 157 | return !*cp || !!strchr(sep, *cp); |
| 158 | } |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 159 | |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 160 | bool property_get_bool(const char *key, int flag) { |
| 161 | char def[PROPERTY_VALUE_MAX]; |
| 162 | char property[PROPERTY_VALUE_MAX]; |
| 163 | def[0] = '\0'; |
| 164 | if (flag & BOOL_DEFAULT_FLAG_PERSIST) { |
| 165 | char newkey[PROPERTY_KEY_MAX]; |
| 166 | snprintf(newkey, sizeof(newkey), "ro.%s", key); |
| 167 | property_get(newkey, property, ""); |
Mark Salyzyn | 07522c6 | 2016-03-02 07:51:48 -0800 | [diff] [blame] | 168 | // persist properties set by /data require inoculation with |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 169 | // logd-reinit. They may be set in init.rc early and function, but |
| 170 | // otherwise are defunct unless reset. Do not rely on persist |
| 171 | // properties for startup-only keys unless you are willing to restart |
| 172 | // logd daemon (not advised). |
| 173 | snprintf(newkey, sizeof(newkey), "persist.%s", key); |
| 174 | property_get(newkey, def, property); |
| 175 | } |
| 176 | |
| 177 | property_get(key, property, def); |
| 178 | |
| 179 | if (check_flag(property, "true")) { |
| 180 | return true; |
| 181 | } |
| 182 | if (check_flag(property, "false")) { |
| 183 | return false; |
| 184 | } |
| 185 | if (check_flag(property, "eng")) { |
| 186 | flag |= BOOL_DEFAULT_FLAG_ENG; |
| 187 | } |
| 188 | // this is really a "not" flag |
| 189 | if (check_flag(property, "svelte")) { |
| 190 | flag |= BOOL_DEFAULT_FLAG_SVELTE; |
| 191 | } |
| 192 | |
| 193 | // Sanity Check |
| 194 | if (flag & (BOOL_DEFAULT_FLAG_SVELTE | BOOL_DEFAULT_FLAG_ENG)) { |
| 195 | flag &= ~BOOL_DEFAULT_FLAG_TRUE_FALSE; |
| 196 | flag |= BOOL_DEFAULT_TRUE; |
| 197 | } |
| 198 | |
| 199 | if ((flag & BOOL_DEFAULT_FLAG_SVELTE) |
| 200 | && property_get_bool("ro.config.low_ram", |
| 201 | BOOL_DEFAULT_FALSE)) { |
| 202 | return false; |
| 203 | } |
| 204 | if (flag & BOOL_DEFAULT_FLAG_ENG) { |
Mark Salyzyn | d1f41d6 | 2016-02-09 15:49:29 -0800 | [diff] [blame] | 205 | property_get("ro.debuggable", property, ""); |
| 206 | if (strcmp(property, "1")) { |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return (flag & BOOL_DEFAULT_FLAG_TRUE_FALSE) != BOOL_DEFAULT_FALSE; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 214 | static int fdDmesg = -1; |
Mark Salyzyn | d048f11 | 2016-02-08 10:28:12 -0800 | [diff] [blame] | 215 | void inline android::prdebug(const char *fmt, ...) { |
| 216 | if (fdDmesg < 0) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | static const char message[] = { |
| 221 | KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' ' |
| 222 | }; |
| 223 | char buffer[256]; |
| 224 | memcpy(buffer, message, sizeof(message)); |
| 225 | |
| 226 | va_list ap; |
| 227 | va_start(ap, fmt); |
| 228 | int n = vsnprintf(buffer + sizeof(message), |
| 229 | sizeof(buffer) - sizeof(message), fmt, ap); |
| 230 | va_end(ap); |
| 231 | if (n > 0) { |
| 232 | buffer[sizeof(buffer) - 1] = '\0'; |
| 233 | if (!strchr(buffer, '\n')) { |
| 234 | buffer[sizeof(buffer) - 2] = '\0'; |
| 235 | strlcat(buffer, "\n", sizeof(buffer)); |
| 236 | } |
| 237 | write(fdDmesg, buffer, strlen(buffer)); |
| 238 | } |
| 239 | } |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 240 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 241 | static sem_t uidName; |
| 242 | static uid_t uid; |
| 243 | static char *name; |
| 244 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 245 | static sem_t reinit; |
| 246 | static bool reinit_running = false; |
| 247 | static LogBuffer *logBuf = NULL; |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 248 | |
William Roberts | aeca97b | 2015-07-31 13:10:36 -0700 | [diff] [blame] | 249 | static bool package_list_parser_cb(pkg_info *info, void * /* userdata */) { |
| 250 | |
| 251 | bool rc = true; |
| 252 | if (info->uid == uid) { |
| 253 | name = strdup(info->name); |
| 254 | // false to stop processing |
| 255 | rc = false; |
| 256 | } |
| 257 | |
| 258 | packagelist_free(info); |
| 259 | return rc; |
| 260 | } |
| 261 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 262 | static void *reinit_thread_start(void * /*obj*/) { |
| 263 | prctl(PR_SET_NAME, "logd.daemon"); |
| 264 | set_sched_policy(0, SP_BACKGROUND); |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 265 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 266 | |
Mark Salyzyn | 07522c6 | 2016-03-02 07:51:48 -0800 | [diff] [blame] | 267 | // If we are AID_ROOT, we should drop to AID_SYSTEM, if we are anything |
| 268 | // else, we have even lesser privileges and accept our fate. Not worth |
| 269 | // checking for error returns setting this thread's privileges. |
| 270 | (void)setgid(AID_SYSTEM); |
| 271 | (void)setuid(AID_SYSTEM); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 272 | |
| 273 | while (reinit_running && !sem_wait(&reinit) && reinit_running) { |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 274 | |
| 275 | // uidToName Privileged Worker |
| 276 | if (uid) { |
| 277 | name = NULL; |
| 278 | |
William Roberts | aeca97b | 2015-07-31 13:10:36 -0700 | [diff] [blame] | 279 | packagelist_parse(package_list_parser_cb, NULL); |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 280 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 281 | uid = 0; |
| 282 | sem_post(&uidName); |
| 283 | continue; |
| 284 | } |
| 285 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 286 | if (fdDmesg >= 0) { |
| 287 | static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), |
| 288 | 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':', |
| 289 | ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' }; |
| 290 | write(fdDmesg, reinit_message, sizeof(reinit_message)); |
| 291 | } |
| 292 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 293 | // Anything that reads persist.<property> |
| 294 | if (logBuf) { |
| 295 | logBuf->init(); |
Mark Salyzyn | 932f7ac | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 296 | logBuf->initPrune(NULL); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 303 | static sem_t sem_name; |
| 304 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 305 | char *android::uidToName(uid_t u) { |
| 306 | if (!u || !reinit_running) { |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 310 | sem_wait(&sem_name); |
| 311 | |
| 312 | // Not multi-thread safe, we use sem_name to protect |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 313 | uid = u; |
| 314 | |
| 315 | name = NULL; |
| 316 | sem_post(&reinit); |
| 317 | sem_wait(&uidName); |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 318 | char *ret = name; |
| 319 | |
| 320 | sem_post(&sem_name); |
| 321 | |
| 322 | return ret; |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 325 | // Serves as a global method to trigger reinitialization |
| 326 | // and as a function that can be provided to signal(). |
| 327 | void reinit_signal_handler(int /*signal*/) { |
| 328 | sem_post(&reinit); |
| 329 | } |
| 330 | |
Mark Salyzyn | ff32f3c | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 331 | // tagToName converts an events tag into a name |
| 332 | const char *android::tagToName(uint32_t tag) { |
| 333 | static const EventTagMap *map; |
| 334 | |
| 335 | if (!map) { |
| 336 | sem_wait(&sem_name); |
| 337 | if (!map) { |
| 338 | map = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 339 | } |
| 340 | sem_post(&sem_name); |
| 341 | if (!map) { |
| 342 | return NULL; |
| 343 | } |
| 344 | } |
| 345 | return android_lookupEventTag(map, tag); |
| 346 | } |
| 347 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 348 | static void readDmesg(LogAudit *al, LogKlog *kl) { |
| 349 | if (!al && !kl) { |
| 350 | return; |
| 351 | } |
| 352 | |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 353 | int rc = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 354 | if (rc <= 0) { |
| 355 | return; |
| 356 | } |
| 357 | |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 358 | size_t len = rc + 1024; // Margin for additional input race or trailing nul |
| 359 | std::unique_ptr<char []> buf(new char[len]); |
| 360 | |
| 361 | rc = klogctl(KLOG_READ_ALL, buf.get(), len); |
| 362 | if (rc <= 0) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | if ((size_t)rc < len) { |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 367 | len = rc + 1; |
| 368 | } |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 369 | buf[--len] = '\0'; |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 370 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 371 | if (kl && kl->isMonotonic()) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 372 | kl->synchronize(buf.get(), len); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 375 | size_t sublen; |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 376 | for (char *ptr = NULL, *tok = buf.get(); |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 377 | (rc >= 0) && ((tok = log_strntok_r(tok, &len, &ptr, &sublen))); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 378 | tok = NULL) { |
| 379 | if (al) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 380 | rc = al->log(tok, sublen); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 381 | } |
| 382 | if (kl) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 383 | rc = kl->log(tok, sublen); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 388 | // Foreground waits for exit of the main persistent threads |
| 389 | // that are started here. The threads are created to manage |
| 390 | // UNIX domain client sockets for writing, reading and |
| 391 | // controlling the user space logger, and for any additional |
| 392 | // logging plugins like auditd and restart control. Additional |
| 393 | // transitory per-client threads are created for each reader. |
| 394 | int main(int argc, char *argv[]) { |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 395 | int fdPmesg = -1; |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 396 | bool klogd = property_get_bool("logd.kernel", |
| 397 | BOOL_DEFAULT_TRUE | |
| 398 | BOOL_DEFAULT_FLAG_PERSIST | |
| 399 | BOOL_DEFAULT_FLAG_ENG | |
| 400 | BOOL_DEFAULT_FLAG_SVELTE); |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 401 | if (klogd) { |
| 402 | fdPmesg = open("/proc/kmsg", O_RDONLY | O_NDELAY); |
| 403 | } |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 404 | fdDmesg = open("/dev/kmsg", O_WRONLY); |
| 405 | |
| 406 | // issue reinit command. KISS argument parsing. |
| 407 | if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) { |
| 408 | int sock = TEMP_FAILURE_RETRY( |
| 409 | socket_local_client("logd", |
| 410 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 411 | SOCK_STREAM)); |
| 412 | if (sock < 0) { |
| 413 | return -errno; |
| 414 | } |
| 415 | static const char reinit[] = "reinit"; |
| 416 | ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit))); |
| 417 | if (ret < 0) { |
| 418 | return -errno; |
| 419 | } |
| 420 | struct pollfd p; |
| 421 | memset(&p, 0, sizeof(p)); |
| 422 | p.fd = sock; |
| 423 | p.events = POLLIN; |
Mark Salyzyn | f011a33 | 2015-12-10 11:27:03 -0800 | [diff] [blame] | 424 | ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000)); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 425 | if (ret < 0) { |
| 426 | return -errno; |
| 427 | } |
| 428 | if ((ret == 0) || !(p.revents & POLLIN)) { |
| 429 | return -ETIME; |
| 430 | } |
| 431 | static const char success[] = "success"; |
| 432 | char buffer[sizeof(success) - 1]; |
| 433 | memset(buffer, 0, sizeof(buffer)); |
| 434 | ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); |
| 435 | if (ret < 0) { |
| 436 | return -errno; |
| 437 | } |
| 438 | return strncmp(buffer, success, sizeof(success) - 1) != 0; |
| 439 | } |
| 440 | |
| 441 | // Reinit Thread |
| 442 | sem_init(&reinit, 0, 0); |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 443 | sem_init(&uidName, 0, 0); |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 444 | sem_init(&sem_name, 0, 1); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 445 | pthread_attr_t attr; |
| 446 | if (!pthread_attr_init(&attr)) { |
| 447 | struct sched_param param; |
| 448 | |
| 449 | memset(¶m, 0, sizeof(param)); |
| 450 | pthread_attr_setschedparam(&attr, ¶m); |
| 451 | pthread_attr_setschedpolicy(&attr, SCHED_BATCH); |
| 452 | if (!pthread_attr_setdetachstate(&attr, |
| 453 | PTHREAD_CREATE_DETACHED)) { |
| 454 | pthread_t thread; |
| 455 | reinit_running = true; |
| 456 | if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) { |
| 457 | reinit_running = false; |
| 458 | } |
| 459 | } |
| 460 | pthread_attr_destroy(&attr); |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 463 | if (drop_privs() != 0) { |
| 464 | return -1; |
| 465 | } |
| 466 | |
| 467 | // Serves the purpose of managing the last logs times read on a |
| 468 | // socket connection, and as a reader lock on a range of log |
| 469 | // entries. |
| 470 | |
| 471 | LastLogTimes *times = new LastLogTimes(); |
| 472 | |
| 473 | // LogBuffer is the object which is responsible for holding all |
| 474 | // log entries. |
| 475 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 476 | logBuf = new LogBuffer(times); |
| 477 | |
| 478 | signal(SIGHUP, reinit_signal_handler); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 479 | |
Mark Salyzyn | 9c66a58 | 2015-12-14 16:40:12 -0800 | [diff] [blame] | 480 | if (property_get_bool("logd.statistics", |
| 481 | BOOL_DEFAULT_TRUE | |
| 482 | BOOL_DEFAULT_FLAG_PERSIST | |
| 483 | BOOL_DEFAULT_FLAG_ENG | |
| 484 | BOOL_DEFAULT_FLAG_SVELTE)) { |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 485 | logBuf->enableStatistics(); |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 486 | } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 487 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 488 | // LogReader listens on /dev/socket/logdr. When a client |
| 489 | // connects, log entries in the LogBuffer are written to the client. |
| 490 | |
| 491 | LogReader *reader = new LogReader(logBuf); |
| 492 | if (reader->startListener()) { |
| 493 | exit(1); |
| 494 | } |
| 495 | |
| 496 | // LogListener listens on /dev/socket/logdw for client |
| 497 | // initiated log messages. New log entries are added to LogBuffer |
| 498 | // and LogReader is notified to send updates to connected clients. |
| 499 | |
| 500 | LogListener *swl = new LogListener(logBuf, reader); |
Mark Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 501 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
Mark Salyzyn | 39944c8 | 2015-09-08 11:24:07 -0700 | [diff] [blame] | 502 | if (swl->startListener(600)) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 503 | exit(1); |
| 504 | } |
| 505 | |
| 506 | // Command listener listens on /dev/socket/logd for incoming logd |
| 507 | // administrative commands. |
| 508 | |
| 509 | CommandListener *cl = new CommandListener(logBuf, reader, swl); |
| 510 | if (cl->startListener()) { |
| 511 | exit(1); |
| 512 | } |
| 513 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 514 | // LogAudit listens on NETLINK_AUDIT socket for selinux |
| 515 | // initiated log messages. New log entries are added to LogBuffer |
| 516 | // and LogReader is notified to send updates to connected clients. |
| 517 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 518 | bool auditd = property_get_bool("logd.auditd", |
| 519 | BOOL_DEFAULT_TRUE | |
| 520 | BOOL_DEFAULT_FLAG_PERSIST); |
| 521 | LogAudit *al = NULL; |
| 522 | if (auditd) { |
| 523 | al = new LogAudit(logBuf, reader, |
| 524 | property_get_bool("logd.auditd.dmesg", |
| 525 | BOOL_DEFAULT_TRUE | |
| 526 | BOOL_DEFAULT_FLAG_PERSIST) |
| 527 | ? fdDmesg |
| 528 | : -1); |
| 529 | } |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 530 | |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 531 | LogKlog *kl = NULL; |
| 532 | if (klogd) { |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 533 | kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL); |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 534 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 535 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 536 | readDmesg(al, kl); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 537 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 538 | // failure is an option ... messages are in dmesg (required by standard) |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 539 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 540 | if (kl && kl->startListener()) { |
| 541 | delete kl; |
| 542 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 543 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 544 | if (al && al->startListener()) { |
| 545 | delete al; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 546 | } |
| 547 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 548 | TEMP_FAILURE_RETRY(pause()); |
| 549 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 550 | exit(0); |
| 551 | } |