blob: 203318d91af566f65654f48e36e63a7b425ecef0 [file] [log] [blame]
Mark Salyzyn61e9ce62016-09-12 14:51:54 -07001/*
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
Mark Salyzyn501c3732017-03-10 14:31:54 -080020#include <string>
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070021#include <unordered_map>
22#include <unordered_set>
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070023
24#include <utils/RWLock.h>
25
26class 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;
Mark Salyzyn501c3732017-03-10 14:31:54 -080038 typedef std::unordered_map<std::string, uint32_t>::const_iterator
39 key2tag_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070040
41 // Allows us to manage access permissions based on uid registrants
42 // Global entries are specifically erased.
43 typedef std::unordered_set<uid_t> uid_list;
44 std::unordered_map<uint32_t, uid_list> tag2uid;
Mark Salyzyn501c3732017-03-10 14:31:54 -080045 typedef std::unordered_map<uint32_t, uid_list>::const_iterator
46 tag2uid_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070047
48 std::unordered_map<uint32_t, std::string> tag2name;
Mark Salyzyn501c3732017-03-10 14:31:54 -080049 typedef std::unordered_map<uint32_t, std::string>::const_iterator
50 tag2name_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070051
52 std::unordered_map<uint32_t, std::string> tag2format;
Mark Salyzyn501c3732017-03-10 14:31:54 -080053 typedef std::unordered_map<uint32_t, std::string>::const_iterator
54 tag2format_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070055
Mark Salyzyn501c3732017-03-10 14:31:54 -080056 static const size_t max_per_uid = 256; // Put a cap on the tags per uid
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070057 std::unordered_map<uid_t, size_t> uid2count;
Mark Salyzyn501c3732017-03-10 14:31:54 -080058 typedef std::unordered_map<uid_t, size_t>::const_iterator
59 uid2count_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070060
61 // Dynamic entries are assigned
62 std::unordered_map<uint32_t, size_t> tag2total;
Mark Salyzyn501c3732017-03-10 14:31:54 -080063 typedef std::unordered_map<uint32_t, size_t>::const_iterator
64 tag2total_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070065
66 // emplace unique tag
67 uint32_t nameToTag(uid_t uid, const char* name, const char* format);
68 // find unique or associated tag
Mark Salyzyn501c3732017-03-10 14:31:54 -080069 uint32_t nameToTag_locked(const std::string& name, const char* format,
70 bool& unique);
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070071
72 // Record expected file watermarks to detect corruption.
73 std::unordered_map<std::string, size_t> file2watermark;
Mark Salyzyn501c3732017-03-10 14:31:54 -080074 typedef std::unordered_map<std::string, size_t>::const_iterator
75 file2watermark_const_iterator;
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070076
77 void ReadPersistEventLogTags();
78
79 // format helpers
80 // format a single entry, does not need object data
Mark Salyzyn501c3732017-03-10 14:31:54 -080081 static std::string formatEntry(uint32_t tag, uid_t uid, const char* name,
82 const char* format);
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070083 // caller locks, database lookup, authenticate against uid
84 std::string formatEntry_locked(uint32_t tag, uid_t uid,
85 bool authenticate = true);
86
87 bool RebuildFileEventLogTags(const char* filename, bool warn = true);
88
Mark Salyzyn501c3732017-03-10 14:31:54 -080089 void AddEventLogTags(uint32_t tag, uid_t uid, const std::string& Name,
90 const std::string& Format, const char* source = NULL,
91 bool warn = false);
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070092
93 void WriteDynamicEventLogTags(uint32_t tag, uid_t uid);
94 void WriteDebugEventLogTags(uint32_t tag, uid_t uid);
95 // push tag details to persistent storage
Mark Salyzyn501c3732017-03-10 14:31:54 -080096 void WritePersistEventLogTags(uint32_t tag, uid_t uid = AID_ROOT,
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070097 const char* source = NULL);
98
99 static const uint32_t emptyTag = uint32_t(-1);
100
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101 public:
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700102 static const char system_event_log_tags[];
103 static const char dynamic_event_log_tags[];
104 // Only for userdebug and eng
105 static const char debug_event_log_tags[];
106
107 LogTags();
108
109 void WritePmsgEventLogTags(uint32_t tag, uid_t uid = AID_ROOT);
110 void ReadFileEventLogTags(const char* filename, bool warn = true);
111
112 // reverse lookup from tag
113 const char* tagToName(uint32_t tag) const;
114 const char* tagToFormat(uint32_t tag) const;
Mark Salyzyn407537f2017-02-21 16:19:08 -0800115 std::string formatEntry(uint32_t tag, uid_t uid);
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700116 // find associated tag
117 uint32_t nameToTag(const char* name) const;
118
119 // emplace tag if necessary, provide event-log-tag formated output in string
Mark Salyzyn501c3732017-03-10 14:31:54 -0800120 std::string formatGetEventTag(uid_t uid, const char* name,
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700121 const char* format);
122};
123
Mark Salyzyn501c3732017-03-10 14:31:54 -0800124#endif // _LOGD_LOG_TAGS_H__