1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define _GNU_SOURCE
17
18 #include <hilog_adapter.h>
19
20 #include <errno.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <sys/uio.h>
27 #include <sys/un.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "hilog_common.h"
32 #ifdef OHOS_ENABLE_PARAMETER
33 #include "sys_param.h"
34 #endif
35 #include "vsnprintf_s_p.h"
36
37 #define LOG_LEN 3
38 #define ERROR_FD 2
39 #ifdef OHOS_ENABLE_PARAMETER
40 #define SYSPARAM_LENGTH 32
41 #endif
42
43 const int SOCKET_TYPE = SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
44 const int INVALID_SOCKET = -1;
45 const struct sockaddr_un SOCKET_ADDR = {AF_UNIX, SOCKET_FILE_DIR INPUT_SOCKET_NAME};
46
47 static bool musl_log_enable = false;
48
49 #ifdef OHOS_ENABLE_PARAMETER
50 static const char *param_name = "musl.log.enable";
51 #endif
52
SendMessage(HilogMsg * header,const char * tag,uint16_t tagLen,const char * fmt,uint16_t fmtLen)53 static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, uint16_t fmtLen)
54 {
55 int socketFd = TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCKET_TYPE, 0));
56 if (socketFd < 0) {
57 dprintf(ERROR_FD, "HiLogAdapter: Can't create socket! Errno: %d\n", errno);
58 return socketFd;
59 }
60
61 long int result =
62 TEMP_FAILURE_RETRY(connect(socketFd, (const struct sockaddr *)(&SOCKET_ADDR), sizeof(SOCKET_ADDR)));
63 if (result < 0) {
64 dprintf(ERROR_FD, "HiLogAdapter: Can't connect to server. Errno: %d\n", errno);
65 if (socketFd >= 0) {
66 close(socketFd);
67 }
68 return result;
69 }
70 struct timespec ts = {0};
71 (void)clock_gettime(CLOCK_REALTIME, &ts);
72 struct timespec ts_mono = {0};
73 (void)clock_gettime(CLOCK_MONOTONIC, &ts_mono);
74 header->tv_sec = (uint32_t)(ts.tv_sec);
75 header->tv_nsec = (uint32_t)(ts.tv_nsec);
76 header->mono_sec = (uint32_t)(ts_mono.tv_sec);
77 header->len = sizeof(HilogMsg) + tagLen + fmtLen;
78 header->tag_len = tagLen;
79
80 struct iovec vec[LOG_LEN] = {0};
81 vec[0].iov_base = header; // 0 : index of hos log header
82 vec[0].iov_len = sizeof(HilogMsg); // 0 : index of hos log header
83 vec[1].iov_base = (void *)((char *)(tag)); // 1 : index of log tag
84 vec[1].iov_len = tagLen; // 1 : index of log tag
85 vec[2].iov_base = (void *)((char *)(fmt)); // 2 : index of log content
86 vec[2].iov_len = fmtLen; // 2 : index of log content
87 int ret = TEMP_FAILURE_RETRY(writev(socketFd, vec, LOG_LEN));
88 if (socketFd >= 0) {
89 close(socketFd);
90 }
91 return ret;
92 }
93
94 HILOG_LOCAL_API
HiLogAdapterPrintArgs(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * fmt,va_list ap)95 int HiLogAdapterPrintArgs(
96 const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap)
97 {
98 char buf[MAX_LOG_LEN] = {0};
99
100 vsnprintfp_s(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt, ap);
101
102 size_t tagLen = strnlen(tag, MAX_TAG_LEN - 1);
103 size_t logLen = strnlen(buf, MAX_LOG_LEN - 1);
104 HilogMsg header = {0};
105 header.type = type;
106 header.level = level;
107 #ifndef __RECV_MSG_WITH_UCRED_
108 header.pid = getpid();
109 #endif
110 header.tid = (uint32_t)(gettid());
111 header.domain = domain;
112
113 return SendMessage(&header, tag, tagLen + 1, buf, logLen + 1);
114 }
115
116 HILOG_LOCAL_API
HiLogAdapterPrint(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)117 int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
118 {
119 if (!HiLogAdapterIsLoggable(domain, tag, level)) {
120 return -1;
121 }
122
123 int ret;
124 va_list ap;
125 va_start(ap, fmt);
126 ret = HiLogAdapterPrintArgs(type, level, domain, tag, fmt, ap);
127 va_end(ap);
128 return ret;
129 }
130
is_musl_log_enable()131 bool is_musl_log_enable()
132 {
133 if (getpid() == 1) {
134 return false;
135 }
136 return musl_log_enable;
137 }
138
HiLogAdapterIsLoggable(unsigned int domain,const char * tag,LogLevel level)139 bool HiLogAdapterIsLoggable(unsigned int domain, const char *tag, LogLevel level)
140 {
141 if ((level == LOG_ERROR) || (level == LOG_FATAL)) {
142 return true;
143 }
144
145 if (!is_musl_log_enable()) {
146 return false;
147 }
148
149 if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == NULL) {
150 return false;
151 }
152 return true;
153 }
154
155 #ifdef OHOS_ENABLE_PARAMETER
get_bool_sysparam(CachedHandle cachedhandle)156 bool get_bool_sysparam(CachedHandle cachedhandle)
157 {
158 char *param_value = CachedParameterGet(cachedhandle);
159 if (param_value != NULL) {
160 if (strcmp(param_value, "true") == 0) {
161 return true;
162 }
163 }
164 return false;
165 }
166 #endif
167
musl_log_reset()168 void musl_log_reset()
169 {
170 #if (defined(OHOS_ENABLE_PARAMETER))
171 static CachedHandle musl_log_Handle = NULL;
172 if (musl_log_Handle == NULL) {
173 musl_log_Handle = CachedParameterCreate(param_name, "false");
174 }
175 musl_log_enable = get_bool_sysparam(musl_log_Handle);
176 #elif (defined(ENABLE_MUSL_LOG))
177 musl_log_enable = true;
178 #endif
179 }
180