Mark Salyzyn | 9472b4d | 2016-09-12 14:51:54 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #ifndef _LOGD_LOG_TAGS_H__ |
| 18 | #define _LOGD_LOG_TAGS_H__ |
| 19 | |
| 20 | #include <unordered_map> |
| 21 | #include <unordered_set> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <utils/RWLock.h> |
| 25 | |
| 26 | class LogTags { |
| 27 | // This lock protects all the unordered_map accesses below. It |
| 28 | // is a reader/writer lock so that contentions are kept to a |
| 29 | // minimum since writes are rare, even administratably when |
| 30 | // reads are extended. Resist the temptation to use the writer |
| 31 | // lock to protect anything outside the following unordered_maps |
| 32 | // as that would increase the reader contentions. Use a separate |
| 33 | // mutex to protect the other entities. |
| 34 | android::RWLock rwlock; |
| 35 | |
| 36 | // key is Name + "+" + Format |
| 37 | std::unordered_map<std::string, uint32_t> key2tag; |
| 38 | typedef std::unordered_map<std::string, uint32_t>::const_iterator key2tag_const_iterator; |
| 39 | |
| 40 | // Allows us to manage access permissions based on uid registrants |
| 41 | // Global entries are specifically erased. |
| 42 | typedef std::unordered_set<uid_t> uid_list; |
| 43 | std::unordered_map<uint32_t, uid_list> tag2uid; |
| 44 | typedef std::unordered_map<uint32_t, uid_list>::const_iterator tag2uid_const_iterator; |
| 45 | |
| 46 | std::unordered_map<uint32_t, std::string> tag2name; |
| 47 | typedef std::unordered_map<uint32_t, std::string>::const_iterator tag2name_const_iterator; |
| 48 | |
| 49 | std::unordered_map<uint32_t, std::string> tag2format; |
| 50 | typedef std::unordered_map<uint32_t, std::string>::const_iterator tag2format_const_iterator; |
| 51 | |
| 52 | static const size_t max_per_uid = 256; // Put a cap on the tags per uid |
| 53 | std::unordered_map<uid_t, size_t> uid2count; |
| 54 | typedef std::unordered_map<uid_t, size_t>::const_iterator uid2count_const_iterator; |
| 55 | |
| 56 | // Dynamic entries are assigned |
| 57 | std::unordered_map<uint32_t, size_t> tag2total; |
| 58 | typedef std::unordered_map<uint32_t, size_t>::const_iterator tag2total_const_iterator; |
| 59 | |
| 60 | // emplace unique tag |
| 61 | uint32_t nameToTag(uid_t uid, const char* name, const char* format); |
| 62 | // find unique or associated tag |
| 63 | uint32_t nameToTag_locked(const std::string& name, const char* format, bool &unique); |
| 64 | |
| 65 | // Record expected file watermarks to detect corruption. |
| 66 | std::unordered_map<std::string, size_t> file2watermark; |
| 67 | typedef std::unordered_map<std::string, size_t>::const_iterator file2watermark_const_iterator; |
| 68 | |
| 69 | void ReadPersistEventLogTags(); |
| 70 | |
| 71 | // format helpers |
| 72 | // format a single entry, does not need object data |
| 73 | static std::string formatEntry(uint32_t tag, uid_t uid, |
| 74 | const char* name, const char* format); |
| 75 | // caller locks, database lookup, authenticate against uid |
| 76 | std::string formatEntry_locked(uint32_t tag, uid_t uid, |
| 77 | bool authenticate = true); |
| 78 | |
| 79 | bool RebuildFileEventLogTags(const char* filename, bool warn = true); |
| 80 | |
| 81 | void AddEventLogTags(uint32_t tag, uid_t uid, |
| 82 | const std::string& Name, const std::string& Format, |
| 83 | const char* source = NULL, bool warn = false); |
| 84 | |
| 85 | void WriteDynamicEventLogTags(uint32_t tag, uid_t uid); |
| 86 | void WriteDebugEventLogTags(uint32_t tag, uid_t uid); |
| 87 | // push tag details to persistent storage |
| 88 | void WritePersistEventLogTags(uint32_t tag, |
| 89 | uid_t uid = AID_ROOT, |
| 90 | const char* source = NULL); |
| 91 | |
| 92 | static const uint32_t emptyTag = uint32_t(-1); |
| 93 | |
| 94 | public: |
| 95 | |
| 96 | static const char system_event_log_tags[]; |
| 97 | static const char dynamic_event_log_tags[]; |
| 98 | // Only for userdebug and eng |
| 99 | static const char debug_event_log_tags[]; |
| 100 | |
| 101 | LogTags(); |
| 102 | |
| 103 | void WritePmsgEventLogTags(uint32_t tag, uid_t uid = AID_ROOT); |
| 104 | void ReadFileEventLogTags(const char* filename, bool warn = true); |
| 105 | |
| 106 | // reverse lookup from tag |
| 107 | const char* tagToName(uint32_t tag) const; |
| 108 | const char* tagToFormat(uint32_t tag) const; |
| 109 | // find associated tag |
| 110 | uint32_t nameToTag(const char* name) const; |
| 111 | |
| 112 | // emplace tag if necessary, provide event-log-tag formated output in string |
| 113 | std::string formatGetEventTag(uid_t uid, |
| 114 | const char* name, |
| 115 | const char* format); |
| 116 | }; |
| 117 | |
| 118 | #endif // _LOGD_LOG_TAGS_H__ |