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 | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 224 | static LogBuffer* logBuf = NULL; |
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 | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 243 | cap_t caps = cap_init(); |
| 244 | (void)cap_clear(caps); |
| 245 | (void)cap_set_proc(caps); |
| 246 | (void)cap_free(caps); |
| 247 | |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 248 | // If we are AID_ROOT, we should drop to AID_LOGD+AID_SYSTEM, if we are |
| 249 | // anything else, we have even lesser privileges and accept our fate. Not |
| 250 | // worth checking for error returns setting this thread's privileges. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 251 | (void)setgid(AID_SYSTEM); // readonly access to /data/system/packages.list |
| 252 | (void)setuid(AID_LOGD); // access to everything logd, eg /data/misc/logd |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 253 | |
| 254 | while (reinit_running && !sem_wait(&reinit) && reinit_running) { |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 255 | // uidToName Privileged Worker |
| 256 | if (uid) { |
| 257 | name = NULL; |
| 258 | |
William Roberts | aeca97b | 2015-07-31 13:10:36 -0700 | [diff] [blame] | 259 | packagelist_parse(package_list_parser_cb, NULL); |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 260 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 261 | uid = 0; |
| 262 | sem_post(&uidName); |
| 263 | continue; |
| 264 | } |
| 265 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 266 | if (fdDmesg >= 0) { |
| 267 | static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 268 | 'l', |
| 269 | 'o', |
| 270 | 'g', |
| 271 | 'd', |
| 272 | '.', |
| 273 | 'd', |
| 274 | 'a', |
| 275 | 'e', |
| 276 | 'm', |
| 277 | 'o', |
| 278 | 'n', |
| 279 | ':', |
| 280 | ' ', |
| 281 | 'r', |
| 282 | 'e', |
| 283 | 'i', |
| 284 | 'n', |
| 285 | 'i', |
| 286 | 't', |
| 287 | '\n' }; |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 288 | write(fdDmesg, reinit_message, sizeof(reinit_message)); |
| 289 | } |
| 290 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 291 | // Anything that reads persist.<property> |
| 292 | if (logBuf) { |
| 293 | logBuf->init(); |
Mark Salyzyn | 932f7ac | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 294 | logBuf->initPrune(NULL); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 295 | } |
Mark Salyzyn | 61e9ce6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 296 | android::ReReadEventLogTags(); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return NULL; |
| 300 | } |
| 301 | |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 302 | static sem_t sem_name; |
| 303 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 304 | char* android::uidToName(uid_t u) { |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 305 | if (!u || !reinit_running) { |
| 306 | return NULL; |
| 307 | } |
| 308 | |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 309 | sem_wait(&sem_name); |
| 310 | |
| 311 | // Not multi-thread safe, we use sem_name to protect |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 312 | uid = u; |
| 313 | |
| 314 | name = NULL; |
| 315 | sem_post(&reinit); |
| 316 | sem_wait(&uidName); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 317 | char* ret = name; |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 318 | |
| 319 | sem_post(&sem_name); |
| 320 | |
| 321 | return ret; |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 324 | // Serves as a global method to trigger reinitialization |
| 325 | // and as a function that can be provided to signal(). |
| 326 | void reinit_signal_handler(int /*signal*/) { |
| 327 | sem_post(&reinit); |
| 328 | } |
| 329 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 330 | static void readDmesg(LogAudit* al, LogKlog* kl) { |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 331 | if (!al && !kl) { |
| 332 | return; |
| 333 | } |
| 334 | |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 335 | int rc = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 336 | if (rc <= 0) { |
| 337 | return; |
| 338 | } |
| 339 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 340 | size_t len = rc + 1024; // Margin for additional input race or trailing nul |
| 341 | std::unique_ptr<char[]> buf(new char[len]); |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 342 | |
| 343 | rc = klogctl(KLOG_READ_ALL, buf.get(), len); |
| 344 | if (rc <= 0) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | if ((size_t)rc < len) { |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 349 | len = rc + 1; |
| 350 | } |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 351 | buf[--len] = '\0'; |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 352 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 353 | if (kl && kl->isMonotonic()) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 354 | kl->synchronize(buf.get(), len); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 357 | size_t sublen; |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 358 | for (char *ptr = NULL, *tok = buf.get(); |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 359 | (rc >= 0) && ((tok = log_strntok_r(tok, &len, &ptr, &sublen))); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 360 | tok = NULL) { |
| 361 | if (al) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 362 | rc = al->log(tok, sublen); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 363 | } |
| 364 | if (kl) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 365 | rc = kl->log(tok, sublen); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Mark Salyzyn | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 370 | static int issueReinit() { |
| 371 | cap_t caps = cap_init(); |
| 372 | (void)cap_clear(caps); |
| 373 | (void)cap_set_proc(caps); |
| 374 | (void)cap_free(caps); |
| 375 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 376 | int sock = TEMP_FAILURE_RETRY(socket_local_client( |
| 377 | "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM)); |
Mark Salyzyn | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 378 | if (sock < 0) return -errno; |
| 379 | |
| 380 | static const char reinitStr[] = "reinit"; |
| 381 | ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr))); |
| 382 | if (ret < 0) return -errno; |
| 383 | |
| 384 | struct pollfd p; |
| 385 | memset(&p, 0, sizeof(p)); |
| 386 | p.fd = sock; |
| 387 | p.events = POLLIN; |
| 388 | ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000)); |
| 389 | if (ret < 0) return -errno; |
| 390 | if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME; |
| 391 | |
| 392 | static const char success[] = "success"; |
| 393 | char buffer[sizeof(success) - 1]; |
| 394 | memset(buffer, 0, sizeof(buffer)); |
| 395 | ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); |
| 396 | if (ret < 0) return -errno; |
| 397 | |
| 398 | return strncmp(buffer, success, sizeof(success) - 1) != 0; |
| 399 | } |
| 400 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 401 | // Foreground waits for exit of the main persistent threads |
| 402 | // that are started here. The threads are created to manage |
| 403 | // UNIX domain client sockets for writing, reading and |
| 404 | // controlling the user space logger, and for any additional |
| 405 | // logging plugins like auditd and restart control. Additional |
| 406 | // transitory per-client threads are created for each reader. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 407 | int main(int argc, char* argv[]) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 408 | // issue reinit command. KISS argument parsing. |
| 409 | if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) { |
Mark Salyzyn | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 410 | return issueReinit(); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 413 | static const char dev_kmsg[] = "/dev/kmsg"; |
| 414 | fdDmesg = android_get_control_file(dev_kmsg); |
| 415 | if (fdDmesg < 0) { |
| 416 | fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC)); |
| 417 | } |
| 418 | |
| 419 | int fdPmesg = -1; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 420 | bool klogd = __android_logger_property_get_bool( |
| 421 | "logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST | |
| 422 | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE); |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 423 | if (klogd) { |
| 424 | static const char proc_kmsg[] = "/proc/kmsg"; |
| 425 | fdPmesg = android_get_control_file(proc_kmsg); |
| 426 | if (fdPmesg < 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 427 | fdPmesg = TEMP_FAILURE_RETRY( |
| 428 | open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC)); |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 429 | } |
| 430 | if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg); |
| 431 | } |
| 432 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 433 | // Reinit Thread |
| 434 | sem_init(&reinit, 0, 0); |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 435 | sem_init(&uidName, 0, 0); |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 436 | sem_init(&sem_name, 0, 1); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 437 | pthread_attr_t attr; |
| 438 | if (!pthread_attr_init(&attr)) { |
| 439 | struct sched_param param; |
| 440 | |
| 441 | memset(¶m, 0, sizeof(param)); |
| 442 | pthread_attr_setschedparam(&attr, ¶m); |
| 443 | pthread_attr_setschedpolicy(&attr, SCHED_BATCH); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 444 | if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 445 | pthread_t thread; |
| 446 | reinit_running = true; |
| 447 | if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) { |
| 448 | reinit_running = false; |
| 449 | } |
| 450 | } |
| 451 | pthread_attr_destroy(&attr); |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 454 | bool auditd = |
| 455 | __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE); |
Mark Salyzyn | d2b3291 | 2016-10-28 15:11:46 -0700 | [diff] [blame] | 456 | if (drop_privs(klogd, auditd) != 0) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | // Serves the purpose of managing the last logs times read on a |
| 461 | // socket connection, and as a reader lock on a range of log |
| 462 | // entries. |
| 463 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 464 | LastLogTimes* times = new LastLogTimes(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 465 | |
| 466 | // LogBuffer is the object which is responsible for holding all |
| 467 | // log entries. |
| 468 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 469 | logBuf = new LogBuffer(times); |
| 470 | |
| 471 | signal(SIGHUP, reinit_signal_handler); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 472 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 473 | if (__android_logger_property_get_bool( |
| 474 | "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST | |
| 475 | BOOL_DEFAULT_FLAG_ENG | |
| 476 | BOOL_DEFAULT_FLAG_SVELTE)) { |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 477 | logBuf->enableStatistics(); |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 478 | } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 479 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 480 | // LogReader listens on /dev/socket/logdr. When a client |
| 481 | // connects, log entries in the LogBuffer are written to the client. |
| 482 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 483 | LogReader* reader = new LogReader(logBuf); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 484 | if (reader->startListener()) { |
| 485 | exit(1); |
| 486 | } |
| 487 | |
| 488 | // LogListener listens on /dev/socket/logdw for client |
| 489 | // initiated log messages. New log entries are added to LogBuffer |
| 490 | // and LogReader is notified to send updates to connected clients. |
| 491 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 492 | LogListener* swl = new LogListener(logBuf, reader); |
Mark Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 493 | // 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] | 494 | if (swl->startListener(600)) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 495 | exit(1); |
| 496 | } |
| 497 | |
| 498 | // Command listener listens on /dev/socket/logd for incoming logd |
| 499 | // administrative commands. |
| 500 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 501 | CommandListener* cl = new CommandListener(logBuf, reader, swl); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 502 | if (cl->startListener()) { |
| 503 | exit(1); |
| 504 | } |
| 505 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 506 | // LogAudit listens on NETLINK_AUDIT socket for selinux |
| 507 | // initiated log messages. New log entries are added to LogBuffer |
| 508 | // and LogReader is notified to send updates to connected clients. |
| 509 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 510 | LogAudit* al = NULL; |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 511 | if (auditd) { |
| 512 | al = new LogAudit(logBuf, reader, |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 513 | __android_logger_property_get_bool( |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 514 | "ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE) |
| 515 | ? fdDmesg |
| 516 | : -1); |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 517 | } |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 518 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 519 | LogKlog* kl = NULL; |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 520 | if (klogd) { |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 521 | kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL); |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 522 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 523 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 524 | readDmesg(al, kl); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 525 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 526 | // failure is an option ... messages are in dmesg (required by standard) |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 527 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 528 | if (kl && kl->startListener()) { |
| 529 | delete kl; |
| 530 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 531 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 532 | if (al && al->startListener()) { |
| 533 | delete al; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 534 | } |
| 535 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 536 | TEMP_FAILURE_RETRY(pause()); |
| 537 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 538 | exit(0); |
| 539 | } |