developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2023 MediaTek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: Alvin Kuo <Alvin.Kuo@mediatek.com> |
| 6 | */ |
| 7 | |
| 8 | #include <limits.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <stdint.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | #include <stdio.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <time.h> |
| 17 | #include <poll.h> |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | |
| 22 | #include "dump.h" |
| 23 | |
| 24 | static int time_to_str(time_t *time_sec, char *time_str, unsigned int time_str_size) |
| 25 | { |
| 26 | struct tm *ptm; |
| 27 | int ret; |
| 28 | |
| 29 | ptm = gmtime(time_sec); |
| 30 | if (!ptm) |
| 31 | return -1; |
| 32 | |
| 33 | ret = strftime(time_str, time_str_size, "%Y%m%d%H%M%S", ptm); |
| 34 | if (!ret) |
| 35 | return -2; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int save_dump_data(char *dump_root_dir, |
| 41 | struct dump_data_header *dd_hdr, |
| 42 | char *dd) |
| 43 | { |
| 44 | size_t dump_file_size = dd_hdr->info.size + sizeof(struct dump_info); |
| 45 | char dump_time_str[32]; |
| 46 | struct stat st = { 0 }; |
| 47 | char *dump_file = NULL; |
| 48 | char *dump_dir = NULL; |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 49 | size_t path_len; |
| 50 | int ret; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 51 | int fd; |
| 52 | |
| 53 | ret = time_to_str((time_t *)&dd_hdr->info.dump_time_sec, |
| 54 | dump_time_str, sizeof(dump_time_str)); |
| 55 | if (ret < 0) { |
| 56 | fprintf(stderr, |
| 57 | DUMP_LOG_FMT("time_to_str(%lu) fail(%d)\n"), |
| 58 | dd_hdr->info.dump_time_sec, ret); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 59 | return ret; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 60 | } |
| 61 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 62 | /* create the dump directory */ |
| 63 | path_len = strlen(dump_root_dir) + 1 + strlen(dump_time_str) + 1; |
| 64 | dump_dir = malloc(path_len); |
| 65 | if (!dump_dir) |
| 66 | return -ENOMEM; |
| 67 | |
| 68 | ret = snprintf(dump_dir, path_len, "%s/%s", dump_root_dir, dump_time_str); |
| 69 | if (ret < 0) |
| 70 | goto free_dump_dir; |
| 71 | |
| 72 | ret = mkdir(dump_dir, 0775); |
| 73 | if (ret && errno != EEXIST) { |
| 74 | fprintf(stderr, |
| 75 | DUMP_LOG_FMT("mkdir(%s) fail(%s)\n"), |
| 76 | dump_dir, strerror(errno)); |
| 77 | goto free_dump_dir; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 78 | } |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 79 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 80 | /* TODO: only keep latest three dump directories */ |
| 81 | |
| 82 | /* create the dump file */ |
| 83 | path_len = strlen(dump_dir) + 1 + strlen(dd_hdr->info.name) + 1; |
| 84 | dump_file = malloc(path_len); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 85 | if (!dump_file) { |
| 86 | ret = -ENOMEM; |
| 87 | goto free_dump_dir; |
| 88 | } |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 89 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 90 | ret = snprintf(dump_file, path_len, "%s/%s", dump_dir, dd_hdr->info.name); |
| 91 | if (ret < 0) |
| 92 | goto free_dump_file; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 93 | |
developer | 8762a19 | 2023-09-04 09:16:22 +0800 | [diff] [blame] | 94 | fd = open(dump_file, O_WRONLY | O_CREAT, 0664); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 95 | if (fd < 0) { |
| 96 | fprintf(stderr, |
| 97 | DUMP_LOG_FMT("open(%s) fail(%s)\n"), |
| 98 | dump_file, strerror(errno)); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 99 | ret = fd; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 100 | goto free_dump_file; |
| 101 | } |
| 102 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 103 | /* write the dump information at the begining of dump file */ |
| 104 | ret = lseek(fd, 0, SEEK_SET); |
| 105 | if (ret < 0) { |
| 106 | fprintf(stderr, |
| 107 | DUMP_LOG_FMT("lseek fail(%s)\n"), |
| 108 | strerror(errno)); |
| 109 | goto close_dump_file; |
| 110 | } |
| 111 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 112 | write(fd, &dd_hdr->info, sizeof(struct dump_info)); |
| 113 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 114 | /* write the dump data start from dump information plus data offset */ |
| 115 | ret = lseek(fd, dd_hdr->data_offset, SEEK_CUR); |
| 116 | if (ret < 0) { |
| 117 | fprintf(stderr, |
| 118 | DUMP_LOG_FMT("lseek fail(%s)\n"), |
| 119 | strerror(errno)); |
| 120 | goto close_dump_file; |
| 121 | } |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 122 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 123 | write(fd, dd, dd_hdr->data_len); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 124 | |
| 125 | if (dd_hdr->last_frag) { |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 126 | ret = stat(dump_file, &st); |
| 127 | if (ret < 0) { |
| 128 | fprintf(stderr, |
| 129 | DUMP_LOG_FMT("stat(%s) fail(%s)\n"), |
| 130 | dump_file, strerror(errno)); |
| 131 | goto close_dump_file; |
| 132 | } |
| 133 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 134 | if ((size_t)st.st_size != dump_file_size) { |
| 135 | fprintf(stderr, |
| 136 | DUMP_LOG_FMT("file(%s) size %zu != %zu\n"), |
| 137 | dump_file, st.st_size, dump_file_size); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 138 | ret = -EINVAL; |
| 139 | goto close_dump_file; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 143 | ret = 0; |
| 144 | |
| 145 | close_dump_file: |
| 146 | close(fd); |
| 147 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 148 | free_dump_file: |
| 149 | free(dump_file); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 150 | |
| 151 | free_dump_dir: |
| 152 | free(dump_dir); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 153 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int read_retry(int fd, void *buf, int len) |
| 158 | { |
| 159 | int out_len = 0; |
| 160 | int ret; |
| 161 | |
| 162 | while (len > 0) { |
| 163 | ret = read(fd, buf, len); |
| 164 | if (ret < 0) { |
| 165 | if (errno == EINTR || errno == EAGAIN) |
| 166 | continue; |
| 167 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 168 | return ret; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (!ret) |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 172 | break; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 173 | |
| 174 | out_len += ret; |
| 175 | len -= ret; |
| 176 | buf += ret; |
| 177 | } |
| 178 | |
| 179 | return out_len; |
| 180 | } |
| 181 | |
| 182 | static int mkdir_p(char *path, mode_t mode) |
| 183 | { |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 184 | size_t path_len; |
| 185 | char *cpy_path; |
| 186 | char *cur_path; |
| 187 | char *tmp_path; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 188 | char *dir; |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 189 | int ret; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 190 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 191 | path_len = strlen(path) + 1; |
| 192 | if (path_len == 0) |
| 193 | return -EINVAL; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 194 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 195 | cpy_path = malloc(path_len); |
| 196 | if (!cpy_path) |
| 197 | return -ENOMEM; |
| 198 | strncpy(cpy_path, path, path_len); |
| 199 | |
| 200 | cur_path = calloc(1, path_len); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 201 | if (!cur_path) { |
| 202 | ret = -ENOMEM; |
| 203 | goto free_cpy_path; |
| 204 | } |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 205 | |
| 206 | tmp_path = malloc(path_len); |
| 207 | if (!tmp_path) { |
| 208 | ret = -ENOMEM; |
| 209 | goto free_cur_path; |
| 210 | } |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 211 | |
| 212 | for (dir = strtok(cpy_path, "/"); |
| 213 | dir != NULL; |
| 214 | dir = strtok(NULL, "/")) { |
| 215 | /* keep current path */ |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 216 | strncpy(tmp_path, cur_path, path_len); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 217 | |
| 218 | /* append directory in current path */ |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 219 | ret = snprintf(cur_path, path_len, "%s/%s", tmp_path, dir); |
| 220 | if (ret < 0) { |
| 221 | fprintf(stderr, |
| 222 | DUMP_LOG_FMT("append dir(%s) in cur_path(%s) fail(%d)\n"), |
| 223 | dir, cur_path, ret); |
| 224 | goto free_tmp_path; |
| 225 | } |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 226 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 227 | ret = mkdir(cur_path, mode); |
| 228 | if (ret && errno != EEXIST) { |
| 229 | fprintf(stderr, |
| 230 | DUMP_LOG_FMT("mkdir(%s) fail(%s)\n"), |
| 231 | cur_path, strerror(errno)); |
| 232 | goto free_tmp_path; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 236 | ret = 0; |
| 237 | |
| 238 | free_tmp_path: |
| 239 | free(tmp_path); |
| 240 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 241 | free_cur_path: |
| 242 | free(cur_path); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 243 | |
| 244 | free_cpy_path: |
| 245 | free(cpy_path); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 246 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | int tops_save_dump_data(char *dump_root_dir) |
| 251 | { |
| 252 | struct stat st = { 0 }; |
| 253 | int ret = 0; |
| 254 | int fd; |
| 255 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 256 | if (!dump_root_dir) |
| 257 | return -EINVAL; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 258 | |
| 259 | /* reserve 256 bytes for saving name of dump directory and dump file */ |
| 260 | if (strlen(dump_root_dir) > (PATH_MAX - 256)) { |
| 261 | fprintf(stderr, |
| 262 | DUMP_LOG_FMT("dump_root_dir(%s) length %zu > %u\n"), |
| 263 | dump_root_dir, strlen(dump_root_dir), PATH_MAX - 256); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 264 | return -EINVAL; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 265 | } |
| 266 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 267 | ret = mkdir_p(dump_root_dir, 0775); |
| 268 | if (ret < 0) { |
| 269 | fprintf(stderr, |
| 270 | DUMP_LOG_FMT("mkdir_p(%s) fail(%d)\n"), |
| 271 | dump_root_dir, ret); |
| 272 | return ret; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | fd = open(DUMP_DATA_PATH, O_RDONLY); |
| 276 | if (fd < 0) { |
| 277 | fprintf(stderr, |
| 278 | DUMP_LOG_FMT("open(%s) fail(%s)\n"), |
| 279 | DUMP_DATA_PATH, strerror(errno)); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 280 | return fd; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | while (1) { |
| 284 | char dd[RELAY_DUMP_SUBBUF_SIZE - sizeof(struct dump_data_header)]; |
| 285 | struct dump_data_header dd_hdr; |
| 286 | struct pollfd pfd = { |
| 287 | .fd = fd, |
| 288 | .events = POLLIN | POLLHUP | POLLERR, |
| 289 | }; |
| 290 | |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 291 | ret = poll(&pfd, 1, -1); |
| 292 | if (ret < 0) { |
| 293 | fprintf(stderr, |
| 294 | DUMP_LOG_FMT("poll fail(%s)\n"), |
| 295 | strerror(errno)); |
| 296 | break; |
| 297 | } |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 298 | |
| 299 | ret = read_retry(fd, &dd_hdr, sizeof(struct dump_data_header)); |
| 300 | if (ret < 0) { |
| 301 | fprintf(stderr, |
| 302 | DUMP_LOG_FMT("read dd_hdr fail(%d)\n"), ret); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | |
| 306 | if (!ret) |
| 307 | continue; |
| 308 | |
| 309 | if (dd_hdr.data_len == 0) { |
| 310 | fprintf(stderr, |
| 311 | DUMP_LOG_FMT("read empty data\n")); |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | if (dd_hdr.data_len > sizeof(dd)) { |
| 316 | fprintf(stderr, |
| 317 | DUMP_LOG_FMT("data length %u > %lu\n"), |
| 318 | dd_hdr.data_len, sizeof(dd)); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 319 | ret = -ENOMEM; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | |
| 323 | ret = read_retry(fd, dd, dd_hdr.data_len); |
| 324 | if (ret < 0) { |
| 325 | fprintf(stderr, |
| 326 | DUMP_LOG_FMT("read dd fail(%d)\n"), ret); |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | |
| 330 | if ((uint32_t)ret != dd_hdr.data_len) { |
| 331 | fprintf(stderr, |
| 332 | DUMP_LOG_FMT("read dd length %u != %u\n"), |
| 333 | (uint32_t)ret, dd_hdr.data_len); |
developer | f6c5ced | 2023-10-03 16:23:42 +0800 | [diff] [blame] | 334 | ret = -EAGAIN; |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | |
| 338 | ret = save_dump_data(dump_root_dir, &dd_hdr, dd); |
| 339 | if (ret) { |
| 340 | fprintf(stderr, |
| 341 | DUMP_LOG_FMT("save_dump_data(%s) fail(%d)\n"), |
| 342 | dump_root_dir, ret); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | close(fd); |
| 348 | |
developer | e5e687d | 2023-08-08 16:05:33 +0800 | [diff] [blame] | 349 | return ret; |
| 350 | } |