Release 6.5.17: Fix resource leaks
[platform/core/system/crash-worker.git] / src / bugreport-service / diagnostics / diagnostics_dump.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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 #include <alloca.h>
17 #include <assert.h>
18 #include <diagnostics.h>
19 #include <dirent.h>
20 #include <getopt.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "diagnostics/diagnostics.h"
32 #include "diagnostics/diagnostics_dump.h"
33 #include "shared/log.h"
34 #include "shared/util.h"
35
36 enum BUGREPORT_REPORT_TYPE { BR_UNKNOWN, BR_BUGREPORT, BR_CRASHINFO };
37
38 struct report_file {
39         char *file_name;
40         char *file_path;
41         struct timespec ctime;
42 };
43
44 static bool get_reports_list(const char *path, struct report_file **list, size_t *list_count)
45 {
46         assert(list);
47         assert(list_count);
48
49         DIR *dp;
50         struct dirent *ep;
51         dp = opendir(path);
52         if (dp == NULL) {
53                 _E("Open directory %s error: %m", path);
54                 return false;
55         }
56
57         char file_path[PATH_MAX];
58         bool ret = false;
59
60         while ((ep = readdir(dp))) {
61                 if (ep->d_type != DT_REG)
62                         continue;
63                 struct stat st;
64                 int r = snprintf(file_path, sizeof file_path, "%s/%s", path, ep->d_name);
65                 if (r == -1 || r >= sizeof file_path) {
66                         _E("Error while preparing report path: %m");
67                         goto out;
68                 }
69                 if (stat(file_path, &st) != 0) {
70                         _E("Get stats about %s error: %m", path);
71                         goto out;
72                 }
73                 struct tm t;
74                 if (localtime_r(&(st.st_ctim.tv_sec), &t) == NULL) {
75                         _E("localtime_r error: %m");
76                         goto out;
77                 }
78
79                 struct report_file *tmp_list = realloc(*list, sizeof(struct report_file)*(*list_count + 1));
80                 char *fname = strdup(ep->d_name);
81                 char *fpath = strdup(file_path);
82                 if (tmp_list == NULL || fname == NULL || fpath == NULL) {
83                         _E("Out of memory");
84                         free(fname);
85                         free(fpath);
86                         free(tmp_list);
87                         goto out;
88                 }
89
90                 *list = tmp_list;
91                 (*list)[*list_count].file_name = fname;
92                 (*list)[*list_count].file_path = fpath;
93                 (*list)[*list_count].ctime = st.st_ctim;
94                 (*list_count)++;
95         }
96
97         ret = true;
98 out:
99         closedir(dp);
100         return ret;
101 }
102
103 static void free_record(struct report_file *record)
104 {
105         free(record->file_name);
106         free(record->file_path);
107 }
108
109 static void filter_time(struct report_file *list, size_t *list_count, long time_from, long time_to)
110 {
111         size_t dest = 0;
112         size_t n_list_count = *list_count;
113
114         struct timespec current_ts;
115         clock_gettime(CLOCK_REALTIME, &current_ts);
116
117         for (int i = 0; i < *list_count; i++) {
118                 if (list[i].ctime.tv_sec >= time_from && list[i].ctime.tv_sec < time_to) {
119                         if (dest != i)
120                                 list[dest] = list[i];
121                         dest++;
122                 } else {
123                         free_record(&list[i]);
124                         n_list_count--;
125                 }
126         }
127         *list_count = n_list_count;
128 }
129
130 static int ctime_compare(const void *a, const void *b)
131 {
132         return ((struct report_file*)a)->ctime.tv_sec - ((struct report_file*)b)->ctime.tv_sec;
133 }
134
135 static struct report_file* get_reports(long time_from, long time_to, size_t *count)
136 {
137         struct report_file *list = NULL;
138         *count = 0;
139
140         const char *dirs[] = {
141                 CRASH_ROOT_PATH CRASH_PATH_SUBDIR,
142                 CRASH_ROOT_PATH LIVE_PATH_SUBDIR
143         };
144
145         for (int i = 0; i < ARRAY_SIZE(dirs); i++) {
146                 if (file_exists(dirs[i]) && !get_reports_list(dirs[i], &list, count)) {
147                         _E("Can not read reports from: %s", dirs[i]);
148                         return NULL;
149                 }
150         }
151
152         filter_time(list, count, time_from, time_to);
153
154         qsort(list, *count, sizeof(struct report_file), ctime_compare);
155         return list;
156 }
157
158 static void write_bugreport_log_file(int out_fd, const char *report_path)
159 {
160         int in_fd = diagnostics_report_get_file(report_path, LOG_FILE);
161         if (in_fd < 0) {
162                 _D("No LOG_FILE report in %s file", report_path);
163                 return;
164         }
165
166         if (dprintf(out_fd, "Begin systemtate-log\n") == -1) {
167                 _E("Write error: %m");
168                 goto out;
169         }
170
171         if (copy_bytes(out_fd, in_fd, NULL) == -1) {
172                 _E("Copy data error");
173                 goto out;
174         }
175
176 out:
177         close(in_fd);
178         if (dprintf(out_fd, "End systemtate-log\n") == -1)
179                 _E("Write error: %m");
180 }
181
182 static bool write_bugreport(int fd, long time_from, long time_to)
183 {
184         size_t list_count = 0;
185         bool result = true;
186
187         struct report_file *list = get_reports(time_from, time_to, &list_count);
188         if (list == NULL) {
189                 dprintf(fd, "Internal error.\n");
190                 return false;
191         }
192
193         for (int i = 0; i < list_count; i++) {
194                 if (dprintf(fd, "%sBegin bugreport <%s>\n", (i > 0) ? "\n" : "", list[i].file_path) == -1) {
195                         _E("Write error: %m");
196                         result = false;
197                         goto out;
198                 }
199
200                 write_bugreport_log_file(fd, list[i].file_path);
201
202                 if (dprintf(fd, "End bugreport <%s>\n", list[i].file_path) == -1) {
203                         _E("Write error: %m");
204                         result = false;
205                         goto out;
206                 }
207         }
208
209 out:
210         for (int i = 0; i < list_count; i++)
211                 free_record(&list[i]);
212         free(list);
213         return result;
214 }
215
216 static bool write_crash_info(int fd, long time_from, long time_to)
217 {
218         bool result = true;
219         size_t list_count = 0;
220
221         struct report_file *list = get_reports(time_from, time_to, &list_count);
222         if (list == NULL) {
223                 dprintf(fd, "Internal error.\n");
224                 return false;
225         }
226
227         for (int i = 0; i < list_count; i++) {
228                 int nfd = diagnostics_report_get_file(list[i].file_path, INFO_FILE);
229                 if (nfd <= 0) {
230                         _I("No INFO_FILE in %s file", list[i].file_path);
231                         dprintf(nfd, "No INFO_FILE in %s\n",  list[i].file_path);
232                         continue;
233                 }
234
235                 if (dprintf(fd, "%sBegin crash-info <%s>\n", (i > 0) ? "\n" : "", list[i].file_path) == -1) {
236                         _E("Write error: %m");
237                         result = false;
238                         goto out;
239                 }
240
241                 if (copy_bytes(fd, nfd, NULL) == -1) {
242                         _E("Copy data error");
243                         close(nfd);
244                         result = false;
245                         goto out;
246                 }
247
248                 close(nfd);
249
250                 if (dprintf(fd, "End crash-info <%s>\n", list[i].file_path) == -1) {
251                         _E("Write error: %m");
252                         result = false;
253                         goto out;
254                 }
255         }
256
257 out:
258         for (int i = 0; i < list_count; i++)
259                 free_record(&list[i]);
260         free(list);
261         return result;
262 }
263
264 struct diagnostics_call_options {
265         enum BUGREPORT_REPORT_TYPE report_type;
266         long from, to;
267         bool last_set, from_set, to_set;
268 };
269
270 static bool diagnostics_call_parse_options(int out_fd, char **params, int params_size, struct diagnostics_call_options *dco)
271 {
272         struct timespec cur_time;
273         clock_gettime(CLOCK_REALTIME, &cur_time);
274         dco->report_type = BR_BUGREPORT;
275         dco->from = 0;
276         dco->to = cur_time.tv_sec;
277         dco->last_set = false;
278         dco->from_set = false;
279         dco->to_set = false;
280
281         enum PARAM_NAME { PN_TYPE = 1, PN_LAST, PN_TO, PN_FROM};
282
283         struct option long_options[] = {
284                 {"type", required_argument, NULL, PN_TYPE},
285                 {"last", required_argument, NULL, PN_LAST},
286                 {"to", required_argument, NULL, PN_TO},
287                 {"from", required_argument, NULL, PN_FROM},
288                 {0, 0, 0, 0},
289         };
290
291         int opt;
292         optind = 0;
293
294         char **nparams = alloca((params_size+1)*sizeof(char*));
295         memcpy(&nparams[1], params, params_size*sizeof(char*));
296         nparams[0] = "n";
297         while ((opt = getopt_long_only(params_size+1, nparams, "", long_options, NULL)) != -1) {
298                 switch(opt) {
299                 case PN_TYPE:
300                         if (strcmp(optarg, "bugreport") == 0) {
301                                 dco->report_type = BR_BUGREPORT;
302                         } else if (strcmp(optarg, "crash-info") == 0) {
303                                 dco->report_type = BR_CRASHINFO;
304                         } else {
305                                 _E("Incorrect report type: %s", optarg);
306                                 dprintf(out_fd, "Incorrect report type: %s\n", optarg);
307                                 return false;
308                         }
309                         break;
310                 case PN_LAST:
311                         dco->last_set = true;
312                         dco->from = cur_time.tv_sec - strtol(optarg, NULL, 10);
313                         break;
314                 case PN_FROM:
315                         dco->from_set = true;
316                         dco->from = strtol(optarg, NULL, 10);
317                         break;
318                 case PN_TO:
319                         dco->to_set = true;
320                         dco->to = strtol(optarg, NULL, 10);
321                         break;
322                 default:
323                         _E("Incorrect option: %s", (optind > 0 && optind + 1 < params_size) ? params[optind-1] : "<unknown>");
324                         dprintf(out_fd, "Incorrect option: %s\n", (optind > 0 && optind + 1 < params_size) ? params[optind-1] : "<unknown>");
325                         return false;
326                 }
327         }
328
329         return true;
330 }
331
332 static void diagnostics_callback(diagnostics_data_h data, char **params, int params_size, diagnostics_ctx_h ctx, void *user_data)
333 {
334         int fd;
335         int ret = diagnostics_data_get_fd(data, &fd);
336         if (ret < 0) {
337                 _E("Get data file descriptor error: %d", ret);
338                 return;
339         }
340         struct diagnostics_call_options dco;
341
342         if (!diagnostics_call_parse_options(fd, params, params_size, &dco))
343                 return;
344
345         if ((dco.last_set && (dco.from_set || dco.to_set)) || (dco.to_set && !dco.from_set)) {
346                 _E("Incorrect parameters set");
347                 dprintf(fd, "Incorrect parameters set.\n");
348                 return;
349         }
350
351         switch(dco.report_type) {
352         case BR_CRASHINFO:
353                 write_crash_info(fd, dco.from, dco.to);
354                 break;
355         case BR_BUGREPORT:
356                 write_bugreport(fd, dco.from, dco.to);
357                 break;
358         default:
359                 _E("Unknown report type\n");
360         }
361 }
362
363 bool diagnostics_init()
364 {
365         diagnostics_set_client_id("org.tizen.bugreport-service");
366         if (diagnostics_set_data_request_cb(&diagnostics_callback, NULL) != DIAGNOSTICS_ERROR_NONE) {
367                 _E("Unable to register diagnostics callback");
368                 return false;
369         }
370
371         return true;
372 }
373