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