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