Skip to content

Commit 5e32c02

Browse files
lauxinwlijinxia
authored andcommitted
tools:acrn-crashlog: Enhance some functions
The changes include: 1. modify the outparam only in successful cases. 2. return -1 instead of a errno-style value if error happens. 3. check return value of strrchr. Tracked-On: #971 Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com> Acked-by: Chen Gang <gang.c.chen@intel.com>
1 parent 10f0bb0 commit 5e32c02

File tree

4 files changed

+63
-44
lines changed

4 files changed

+63
-44
lines changed

tools/acrn-crashlog/acrnprobe/sender.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ static void telemd_get_log(struct log_t *log, void *data)
224224
count = ac_scandir(d->srcdir, &filelist, filter_filename_substr,
225225
log->name, NULL);
226226
if (count < 0) {
227-
LOGE("search (%s) in dir (%s) failed, error (%s)\n", log->name,
228-
d->srcdir, strerror(count));
227+
LOGE("search (%s) in dir (%s) failed\n", log->name, d->srcdir);
229228
return;
230229
}
231230
if (!count) {
@@ -289,8 +288,7 @@ static void crashlog_get_log(struct log_t *log, void *data)
289288
const int count = config_fmt_to_files(log->path, &files);
290289

291290
if (count < 0) {
292-
LOGE("parse config format (%s) failed, error (%s)\n",
293-
log->path, strerror(count));
291+
LOGE("parse config format (%s) failed\n", log->path);
294292
return;
295293
}
296294
if (!count) {
@@ -597,9 +595,8 @@ static int telemd_new_vmevent(const char *line_to_sync,
597595
res = find_file(crashlog->outdir, log + strlen("/logs/"),
598596
2, &vmlogpath, 1);
599597
if (res < 0) {
600-
LOGE("find (%s) in (%s) failed, strerror (%s)\n",
601-
log + strlen("/logs/"), crashlog->outdir,
602-
strerror(-res));
598+
LOGE("find (%s) in (%s) failed\n",
599+
log + strlen("/logs/"), crashlog->outdir);
603600
return VMEVT_DEFER;
604601
}
605602
}

tools/acrn-crashlog/common/fsutils.c

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,7 @@ int file_read_key_value_r(const char *path, const char *key,
928928
* @param farg The second arg of filter.
929929
* @param compar See scandir.
930930
*
931-
* @return the count of scanned files if successful, or a negative
932-
* errno-style value if not.
931+
* @return the count of scanned files on success, or -1 on error.
933932
*/
934933
int ac_scandir(const char *dirp, struct dirent ***namelist,
935934
int (*filter)(const struct dirent *, const void *),
@@ -941,18 +940,21 @@ int ac_scandir(const char *dirp, struct dirent ***namelist,
941940
int count = 0;
942941
int index = 0;
943942
struct dirent **_filelist;
943+
struct dirent **_outlist;
944944

945945
if (!dirp || !namelist)
946-
return -EINVAL;
946+
return -1;
947947

948948
const int res = scandir(dirp, &_filelist, NULL, compar);
949949

950950
if (!filter) {
951951
*namelist = _filelist;
952952
return res;
953953
}
954-
if (res == -1)
955-
return -errno;
954+
if (res == -1) {
955+
LOGE("failed to scandir, error (%s)\n", strerror(errno));
956+
return -1;
957+
}
956958

957959
/* overwrite filter */
958960
/* calculate the matched files, free unneeded files and mark them */
@@ -972,25 +974,28 @@ int ac_scandir(const char *dirp, struct dirent ***namelist,
972974
}
973975

974976
/* construct the out array */
975-
*namelist = malloc(count * sizeof(struct dirent *));
976-
if (!(*namelist))
977+
_outlist = malloc(count * sizeof(struct dirent *));
978+
if (!_outlist) {
979+
LOGE("failed to malloc\n");
977980
goto e_free;
981+
}
978982

979983
for (i = 0; i < res; i++) {
980984
if (_filelist[i])
981-
(*namelist)[index++] = _filelist[i];
985+
_outlist[index++] = _filelist[i];
982986
}
983987

984988
free(_filelist);
985989

990+
*namelist = _outlist;
986991
return count;
987992

988993
e_free:
989994
for (i = 0; i < res; i++)
990995
if (_filelist[i])
991996
free(_filelist[i]);
992997
free(_filelist);
993-
return -errno;
998+
return -1;
994999
}
9951000

9961001
/* filters return zero if the match is successful */
@@ -1023,7 +1028,7 @@ int dir_contains(const char *dir, const char *filename, const int exact)
10231028
struct dirent **filelist;
10241029

10251030
if (!dir || !filename)
1026-
return -EINVAL;
1031+
return -1;
10271032

10281033
if (exact)
10291034
ret = ac_scandir(dir, &filelist, filter_filename_exactly,
@@ -1150,8 +1155,7 @@ static void expand_dir(char *_dirs[], int *count, int depth, int max_depth)
11501155
* @param path[out] Searched file path in given dir.
11511156
* @param limit The number of files uplayer want to get.
11521157
*
1153-
* @return the count of searched files if successful, or a negative
1154-
* errno-style value if not.
1158+
* @return the count of searched files on success, or -1 on error.
11551159
*/
11561160
int find_file(char *dir, char *target_file, int depth, char *path[], int limit)
11571161
{
@@ -1161,12 +1165,12 @@ int find_file(char *dir, char *target_file, int depth, char *path[], int limit)
11611165
int dirs;
11621166

11631167
if (depth < 1 || !dir || !target_file || !path || limit <= 0)
1164-
return -EINVAL;
1168+
return -1;
11651169

11661170
ret = asprintf(&_dirs[0], "%s", dir);
11671171
if (ret < 0) {
11681172
LOGE("compute string failed, out of memory\n");
1169-
return -ENOMEM;
1173+
return -1;
11701174
}
11711175
dirs = 1;
11721176

@@ -1182,12 +1186,13 @@ int find_file(char *dir, char *target_file, int depth, char *path[], int limit)
11821186
_dirs[i], target_file);
11831187
if (ret < 0) {
11841188
LOGE("compute string failed, out of memory\n");
1185-
ret = -ENOMEM;
1189+
ret = -1;
11861190
goto fail;
11871191
}
11881192
} else if (ret < 0) {
1189-
LOGE("dir_contains failed, error (%s)\n",
1190-
strerror(-ret));
1193+
LOGE("dir_contains failed\n");
1194+
ret = -1;
1195+
goto fail;
11911196
}
11921197
}
11931198

@@ -1280,8 +1285,7 @@ int is_ac_filefmt(const char *file_fmt)
12801285
* @param file_fmt A string pointer of a file format.
12811286
* @param out Files were found.
12821287
*
1283-
* @return the count of searched files if successful, or a negative
1284-
* errno-style value if not.
1288+
* @return the count of searched files on success, or -1 on error.
12851289
*/
12861290
int config_fmt_to_files(const char *file_fmt, char ***out)
12871291
{
@@ -1298,11 +1302,13 @@ int config_fmt_to_files(const char *file_fmt, char ***out)
12981302
char **out_array;
12991303

13001304
if (!file_fmt || !out)
1301-
return -EINVAL;
1305+
return -1;
13021306

13031307
dir = strdup(file_fmt);
1304-
if (!dir)
1305-
return -ENOMEM;
1308+
if (!dir) {
1309+
LOGE("failed to strdup\n");
1310+
return -1;
1311+
}
13061312

13071313
if (!is_ac_filefmt(file_fmt)) {
13081314
/* It's an regular file as default */
@@ -1320,37 +1326,58 @@ int config_fmt_to_files(const char *file_fmt, char ***out)
13201326
/* get dir and file prefix from format */
13211327
p = strrchr(dir, '/');
13221328
if (!p) {
1323-
ret = -EINVAL;
1329+
LOGE("only support abs path, dir (%s)\n", dir);
1330+
ret = -1;
13241331
goto free_dir;
13251332
}
13261333
*p = '\0';
13271334
file_prefix = p + 1;
1328-
*strrchr(file_prefix, '[') = '\0';
1335+
p = strrchr(file_prefix, '[');
1336+
if (p) {
1337+
*p = '\0';
1338+
} else {
1339+
ret = -1;
1340+
LOGE("unsupported formats (%s)\n", dir);
1341+
goto free_dir;
1342+
}
1343+
13291344

13301345
if (!directory_exists(dir)) {
13311346
ret = 0;
13321347
goto free_dir;
13331348
}
13341349
/* get format type */
13351350
subfix = strrchr(file_fmt, '[');
1351+
if (!subfix) {
1352+
ret = -1;
1353+
LOGE("unsupported formats (%s)\n", dir);
1354+
goto free_dir;
1355+
}
13361356
res = sscanf(subfix, "[%2[01-*]]", type);
13371357
if (res != 1) {
1338-
ret = -EINVAL;
1358+
ret = -1;
1359+
LOGE("unsupported formats (%s)\n", dir);
13391360
goto free_dir;
13401361
}
13411362

13421363
/* get all files which start with prefix */
13431364
count = ac_scandir(dir, &filelist, filter_filename_startswith,
13441365
file_prefix, alphasort);
1345-
if (count <= 0) {
1346-
ret = count;
1366+
if (count < 0) {
1367+
ret = -1;
1368+
LOGE("failed to ac_scandir\n");
1369+
goto free_dir;
1370+
}
1371+
if (!count) {
1372+
ret = 0;
13471373
goto free_dir;
13481374
}
13491375

13501376
/* construct output */
13511377
out_array = (char **)malloc(count * sizeof(char *));
13521378
if (!out_array) {
1353-
ret = -errno;
1379+
ret = -1;
1380+
LOGE("failed to malloc\n");
13541381
goto free_filelist;
13551382
}
13561383

tools/acrn-crashlog/common/log_sys.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ void do_log(const int level,
1717
va_list args;
1818
char *fmt;
1919
char log[MAX_LOG_LEN];
20-
char *msg_log;
2120
int n = 0;
22-
int msg_len = 0;
2321
#ifdef DEBUG_ACRN_CRASHLOG
2422
const char header_fmt[] = "<%-20s%5d>: ";
2523
#endif
@@ -42,10 +40,8 @@ void do_log(const int level,
4240
if (n < 0 || (size_t)n >= sizeof(log))
4341
n = 0;
4442
#endif
45-
msg_log = log + n;
46-
msg_len = sizeof(log) - n;
4743
/* msg */
48-
vsnprintf(msg_log, msg_len, fmt, args);
44+
vsnprintf(log + n, sizeof(log) - (size_t)n, fmt, args);
4945
log[sizeof(log) - 1] = 0;
5046
va_end(args);
5147

tools/acrn-crashlog/usercrash/protocol.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ static int socket_make_sockaddr_un(const char *name,
4040
socket_len = strlen(RESERVED_SOCKET_PREFIX);
4141
if (socket_len >= SUN_PATH_MAX)
4242
return -1;
43-
strcpy(p_addr->sun_path, RESERVED_SOCKET_PREFIX);
43+
strncpy(p_addr->sun_path, RESERVED_SOCKET_PREFIX, socket_len + 1);
4444
name_len = strlen(name);
4545
if (name_len >= (SUN_PATH_MAX - socket_len))
4646
return -1;
47-
strncat(p_addr->sun_path, name, SUN_PATH_MAX - socket_len);
47+
strncat(p_addr->sun_path, name, name_len);
4848

4949
p_addr->sun_family = AF_LOCAL;
5050
*alen = name_len + socket_len +
@@ -111,8 +111,7 @@ static int socket_bind(int fd, const char *name)
111111
name_len = strlen(name);
112112
if (name_len >= SUN_PATH_MAX)
113113
return -1;
114-
strncpy(addr.sun_path, name, SUN_PATH_MAX);
115-
addr.sun_path[SUN_PATH_MAX - 1] = '\0';
114+
strncpy(addr.sun_path, name, name_len + 1);
116115
unlink(addr.sun_path);
117116
alen = strlen(addr.sun_path) + sizeof(addr.sun_family);
118117

0 commit comments

Comments
 (0)