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 #ifndef _LOGD_LOG_UTILS_H__
18 #define _LOGD_LOG_UTILS_H__
19 
20 #include <sys/cdefs.h>
21 #include <sys/types.h>
22 
23 #include <private/android_logger.h>
24 #include <sysutils/SocketClient.h>
25 #include <utils/FastStrcmp.h>
26 
27 // Hijack this header as a common include file used by most all sources
28 // to report some utilities defined here and there.
29 
30 namespace android {
31 
32 // Furnished in main.cpp. Caller must own and free returned value
33 char* uidToName(uid_t uid);
34 void prdebug(const char* fmt, ...) __printflike(1, 2);
35 
36 // Furnished in LogStatistics.cpp.
37 size_t sizesTotal();
38 // Caller must own and free returned value
39 char* pidToName(pid_t pid);
40 char* tidToName(pid_t tid);
41 
42 // Furnished in LogTags.cpp. Thread safe.
43 const char* tagToName(uint32_t tag);
44 void ReReadEventLogTags();
45 
46 // Furnished by LogKlog.cpp
47 char* log_strntok_r(char* s, ssize_t& len, char*& saveptr, ssize_t& sublen);
48 
49 // needle should reference a string longer than 1 character
strnstr(const char * s,ssize_t len,const char * needle)50 static inline const char* strnstr(const char* s, ssize_t len,
51                                   const char* needle) {
52     if (len <= 0) return nullptr;
53 
54     const char c = *needle++;
55     const size_t needleLen = strlen(needle);
56     do {
57         do {
58             if (len <= (ssize_t)needleLen) return nullptr;
59             --len;
60         } while (*s++ != c);
61     } while (fastcmp<memcmp>(s, needle, needleLen));
62     s--;
63     return s;
64 }
65 }
66 
67 // Furnished in LogCommand.cpp
68 bool clientHasLogCredentials(uid_t uid, gid_t gid, pid_t pid);
69 bool clientHasLogCredentials(SocketClient* cli);
70 
worstUidEnabledForLogid(log_id_t id)71 static inline bool worstUidEnabledForLogid(log_id_t id) {
72     return (id == LOG_ID_MAIN) || (id == LOG_ID_SYSTEM) ||
73            (id == LOG_ID_RADIO) || (id == LOG_ID_EVENTS);
74 }
75 
76 #endif  // _LOGD_LOG_UTILS_H__
77