更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_blackbox_common.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* ------------ includes ------------ */
32#include "los_blackbox_common.h"
33#ifdef LOSCFG_LIB_LIBC
34#include "stdlib.h"
35#include "unistd.h"
36#endif
37#ifdef LOSCFG_FS_VFS
38#include "fs/fs.h"
39#include "fs/mount.h"
40#endif
41#include "securec.h"
42#include "los_memory.h"
43
44/* ------------ local macroes ------------ */
45#ifdef LOSCFG_FS_VFS
46#define BBOX_DIR_MODE 0750
47#define BBOX_FILE_MODE 0640
48#endif
49
50/* ------------ local prototypes ------------ */
51/* ------------ local function declarations ------------ */
52/* ------------ global function declarations ------------ */
53/* ------------ local variables ------------ */
54static bool g_isLogPartReady = FALSE;
55
56/* ------------ function definitions ------------ */
57int FullWriteFile(const char *filePath, const char *buf, size_t bufSize, int isAppend)
58{
59#ifdef LOSCFG_FS_VFS
60 int fd;
61 int totalToWrite = (int)bufSize;
62 int totalWrite = 0;
63
64 if (filePath == NULL || buf == NULL || bufSize == 0) {
65 BBOX_PRINT_ERR("filePath: %p, buf: %p, bufSize: %lu!\n", filePath, buf, bufSize);
66 return -1;
67 }
68
69 if (!IsLogPartReady()) {
70 BBOX_PRINT_ERR("log path [%s] isn't ready to be written!\n", LOSCFG_BLACKBOX_LOG_ROOT_PATH);
71 return -1;
72 }
73 fd = open(filePath, O_CREAT | O_RDWR | (isAppend ? O_APPEND : O_TRUNC), BBOX_FILE_MODE);
74 if (fd < 0) {
75 BBOX_PRINT_ERR("Create file [%s] failed, fd: %d!\n", filePath, fd);
76 return -1;
77 }
78 while (totalToWrite > 0) {
79 int writeThisTime = write(fd, buf, totalToWrite);
80 if (writeThisTime < 0) {
81 BBOX_PRINT_ERR("Failed to write file [%s]!\n", filePath);
82 (void)close(fd);
83 return -1;
84 }
85 buf += writeThisTime;
86 totalToWrite -= writeThisTime;
87 totalWrite += writeThisTime;
88 }
89 (void)fsync(fd);
90 (void)close(fd);
91
92 return (totalWrite == (int)bufSize) ? 0 : -1;
93#else
94 (VOID)filePath;
95 (VOID)buf;
96 (VOID)bufSize;
97 (VOID)isAppend;
98 return -1;
99#endif
100}
101
102int SaveBasicErrorInfo(const char *filePath, const struct ErrorInfo *info)
103{
104 char *buf = NULL;
105
106 if (filePath == NULL || info == NULL) {
107 BBOX_PRINT_ERR("filePath: %p, event: %p!\n", filePath, info);
108 return -1;
109 }
110
111 buf = LOS_MemAlloc(m_aucSysMem1, ERROR_INFO_MAX_LEN);
112 if (buf == NULL) {
113 BBOX_PRINT_ERR("LOS_MemAlloc failed!\n");
114 return -1;
115 }
116 (void)memset_s(buf, ERROR_INFO_MAX_LEN, 0, ERROR_INFO_MAX_LEN);
117 if (snprintf_s(buf, ERROR_INFO_MAX_LEN, ERROR_INFO_MAX_LEN - 1,
118 ERROR_INFO_HEADER_FORMAT, info->event, info->module, info->errorDesc) != -1) {
119 *(buf + ERROR_INFO_MAX_LEN - 1) = '\0';
120 (void)FullWriteFile(filePath, buf, strlen(buf), 0);
121 } else {
122 BBOX_PRINT_ERR("buf is not enough or snprintf_s failed!\n");
123 }
124
126
127 return 0;
128}
129
130#ifdef LOSCFG_FS_VFS
131static int IsLogPartMounted(const char *devPoint, const char *mountPoint, struct statfs *statBuf, void *arg)
132{
133 (void)devPoint;
134 (void)statBuf;
135 (void)arg;
136 if (mountPoint != NULL && arg != NULL) {
137 if (strcmp(mountPoint, (char *)arg) == 0) {
138 g_isLogPartReady = TRUE;
139 }
140 }
141 return 0;
142}
143
145{
146 if (!g_isLogPartReady) {
147 (void)foreach_mountpoint((foreach_mountpoint_t)IsLogPartMounted, LOSCFG_BLACKBOX_LOG_PART_MOUNT_POINT);
148 }
149 return g_isLogPartReady;
150}
151#else
152bool IsLogPartReady(void)
153{
154 return TRUE;
155}
156#endif
157
158#ifdef LOSCFG_FS_VFS
159int CreateNewDir(const char *dirPath)
160{
161 int ret;
162
163 if (dirPath == NULL) {
164 BBOX_PRINT_ERR("dirPath is NULL!\n");
165 return -1;
166 }
167
168 ret = access(dirPath, 0);
169 if (ret == 0) {
170 return 0;
171 }
172 ret = mkdir(dirPath, BBOX_DIR_MODE);
173 if (ret != 0) {
174 BBOX_PRINT_ERR("mkdir [%s] failed!\n", dirPath);
175 return -1;
176 }
177
178 return 0;
179}
180
181int CreateLogDir(const char *dirPath)
182{
183 const char *temp = dirPath;
184 char curPath[PATH_MAX_LEN];
185 int idx = 0;
186
187 if (dirPath == NULL) {
188 BBOX_PRINT_ERR("dirPath is NULL!\n");
189 return -1;
190 }
191 if (*dirPath != '/') {
192 BBOX_PRINT_ERR("Invalid dirPath: %s\n", dirPath);
193 return -1;
194 }
195 (void)memset_s(curPath, sizeof(curPath), 0, sizeof(curPath));
196 curPath[idx++] = *dirPath++;
197 while (*dirPath != '\0' && idx < sizeof(curPath)) {
198 if (*dirPath == '/') {
199 if (CreateNewDir(curPath) != 0) {
200 return -1;
201 }
202 }
203 curPath[idx] = *dirPath;
204 dirPath++;
205 idx++;
206 }
207 if (*dirPath != '\0') {
208 BBOX_PRINT_ERR("dirPath [%s] is too long!\n", temp);
209 return -1;
210 }
211
212 return CreateNewDir(curPath);
213}
214#else
215int CreateLogDir(const char *dirPath)
216{
217 (void)dirPath;
218 return -1;
219}
220#endif
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
UINT8 * m_aucSysMem1
系统动态内存池地址的起始地址 @note_thinking 能否不要用 0,1来命名核心变量 ???
Definition: los_memory.c:108
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
bool IsLogPartReady(void)
int FullWriteFile(const char *filePath, const char *buf, size_t bufSize, int isAppend)
static int IsLogPartMounted(const char *devPoint, const char *mountPoint, struct statfs *statBuf, void *arg)
int SaveBasicErrorInfo(const char *filePath, const struct ErrorInfo *info)
int CreateNewDir(const char *dirPath)
static bool g_isLogPartReady
int CreateLogDir(const char *dirPath)
int(* foreach_mountpoint_t)(const char *devpoint, const char *mountpoint, struct statfs *statbuf, void *arg)
Definition: mount.h:96
int foreach_mountpoint(foreach_mountpoint_t handler, void *arg)
char errorDesc[ERROR_DESC_MAX_LEN]
Definition: los_blackbox.h:64
char module[MODULE_MAX_LEN]
Definition: los_blackbox.h:63
char event[EVENT_MAX_LEN]
Definition: los_blackbox.h:62
ARG_NUM_3 int
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
int access(const char *path, int amode)
Definition: vfs_other.c:293