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