1 /*
2 * Copyright (C) 2012-2015 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 #pragma once
18
19 #include <sys/cdefs.h>
20 #include <sys/types.h>
21
22 #include <private/android_logger.h>
23 #include <sysutils/SocketClient.h>
24 #include <utils/FastStrcmp.h>
25
26 // Hijack this header as a common include file used by most all sources
27 // to report some utilities defined here and there.
28
29 namespace android {
30
31 // Furnished in main.cpp. Caller must own and free returned value
32 char* uidToName(uid_t uid);
33
34 // Caller must own and free returned value
35 char* pidToName(pid_t pid);
36 char* tidToName(pid_t tid);
37
38 // Furnished in LogTags.cpp. Thread safe.
39 const char* tagToName(uint32_t tag);
40
41 // Furnished by LogKlog.cpp
42 char* log_strntok_r(char* s, ssize_t& len, char*& saveptr, ssize_t& sublen);
43
44 // needle should reference a string longer than 1 character
strnstr(const char * s,ssize_t len,const char * needle)45 static inline const char* strnstr(const char* s, ssize_t len,
46 const char* needle) {
47 if (len <= 0) return nullptr;
48
49 const char c = *needle++;
50 const size_t needleLen = strlen(needle);
51 do {
52 do {
53 if (len <= (ssize_t)needleLen) return nullptr;
54 --len;
55 } while (*s++ != c);
56 } while (fastcmp<memcmp>(s, needle, needleLen));
57 s--;
58 return s;
59 }
60 }
61
62 // Returns true if the log buffer is meant for binary logs.
IsBinary(log_id_t log_id)63 static inline bool IsBinary(log_id_t log_id) {
64 return log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS || log_id == LOG_ID_SECURITY;
65 }
66
67 // Returns the numeric log tag for binary log messages.
MsgToTag(const char * msg,uint16_t msg_len)68 static inline uint32_t MsgToTag(const char* msg, uint16_t msg_len) {
69 if (msg_len < sizeof(android_event_header_t)) {
70 return 0;
71 }
72
73 return reinterpret_cast<const android_event_header_t*>(msg)->tag;
74 }
75
worstUidEnabledForLogid(log_id_t id)76 static inline bool worstUidEnabledForLogid(log_id_t id) {
77 return (id == LOG_ID_MAIN) || (id == LOG_ID_SYSTEM) ||
78 (id == LOG_ID_RADIO) || (id == LOG_ID_EVENTS);
79 }
80