Bump up efl module version.
[platform/core/appfw/launchpad.git] / src / launchpad_debug.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <linux/limits.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <bundle_internal.h>
26
27 #include "launchpad_common.h"
28 #include "launchpad_debug.h"
29 #include "debugger_info.h"
30 #include "key.h"
31
32 #define DEBUGGER_INFO_PATH "/usr/share/aul"
33
34 static int debug_initialized;
35 static GList *debugger_info_list;
36 static debugger_info_h debugger_info;
37 static GList *debug_argv_list;
38 static GList *extra_argv_list;
39
40 int _debug_create_extra_argv(int *argc, char ***argv)
41 {
42         int new_argc;
43         char **new_argv;
44         const char *extra_argv;
45         GList *iter;
46         int i;
47
48         if (argc == NULL || argv == NULL) {
49                 _E("[DEBUG] Invalid parameter");
50                 return -1;
51         }
52
53         if (debugger_info == NULL)
54                 return 0;
55
56         new_argc = g_list_length(extra_argv_list);
57         if (new_argc == 0)
58                 return 0;
59
60         new_argv = (char **)calloc(new_argc, sizeof(char *));
61         if (new_argv == NULL) {
62                 _E("out of memory");
63                 return -1;
64         }
65
66         i = LOADER_ARG_PATH;
67         iter = g_list_first(extra_argv_list);
68         while (iter) {
69                 extra_argv = (const char *)iter->data;
70                 if (extra_argv)
71                         new_argv[i++] = strdup(extra_argv);
72
73                 iter = g_list_next(iter);
74         }
75
76         *argc = new_argc;
77         *argv = new_argv;
78         _D("[DEBUG] argc: %d, i: %d", *argc, i);
79
80         return 0;
81 }
82
83 int _debug_create_argv(int *argc, char ***argv, bool *attach)
84 {
85         int new_argc = 0;
86         char **new_argv;
87         const char *exe;
88         const char *debug_argv;
89         const char *attach_str;
90         GList *iter;
91         GList *list;
92         int i;
93
94         if (argc == NULL || argv == NULL || attach == NULL) {
95                 _E("[DEBUG] Invalid parameter");
96                 return -1;
97         }
98
99         if (debugger_info == NULL)
100                 return 0;
101
102         exe = _debugger_info_get_exe(debugger_info);
103         if (exe == NULL)
104                 return 0;
105
106         attach_str = _debugger_info_get_attach(debugger_info);
107         if (attach_str && strcasecmp(attach_str, "true") == 0) {
108                 *attach = true;
109                 new_argc++;
110         }
111
112         list = _debugger_info_get_default_opt_list(debugger_info);
113         new_argc += g_list_length(debug_argv_list) + g_list_length(list) + 1;
114         new_argv = (char **)calloc(new_argc, sizeof(char *));
115         if (new_argv == NULL) {
116                 _E("out of memory");
117                 return -1;
118         }
119
120         i = LOADER_ARG_PATH;
121         new_argv[i++] = strdup(exe);
122
123         iter = g_list_first(list);
124         while (iter) {
125                 debug_argv = (const char *)iter->data;
126                 if (debug_argv)
127                         new_argv[i++] = strdup(debug_argv);
128
129                 iter = g_list_next(iter);
130         }
131
132         iter = g_list_first(debug_argv_list);
133         while (iter) {
134                 debug_argv = (const char *)iter->data;
135                 if (debug_argv)
136                         new_argv[i++] = strdup(debug_argv);
137
138                 iter = g_list_next(iter);
139         }
140
141         *argc = new_argc;
142         *argv = new_argv;
143         _D("[DEBUG] argc: %d, argv[0]: %s",
144                         new_argc, new_argv[LOADER_ARG_PATH]);
145
146         return 0;
147 }
148
149 void _debug_destroy_argv(int argc, char **argv)
150 {
151         int i;
152
153         if (argv == NULL)
154                 return;
155
156         for (i = 0; i < argc; i++)
157                 free(argv[i]);
158         free(argv);
159 }
160
161 int _debug_get_caller_pid(bundle *kb)
162 {
163         const char *pid_str;
164         int pid;
165
166         pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
167         if (pid_str == NULL)
168                 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
169
170         if (pid_str == NULL)
171                 return -1;
172
173         pid = atoi(pid_str);
174         if (pid <= 1)
175                 return -1;
176
177         return pid;
178 }
179
180 static int __redirect_std_fds(bundle *kb)
181 {
182         char path[PATH_MAX];
183         char err_buf[1024];
184         int fd;
185         int caller_pid;
186
187         if (kb == NULL) {
188                 _E("[DEBUG] Invalid parameter");
189                 return -1;
190         }
191
192         caller_pid = _debug_get_caller_pid(kb);
193         if (caller_pid < 0) {
194                 _E("[DEBUG] Failed to get caller pid");
195                 return -1;
196         }
197
198         /* stdin */
199         snprintf(path, sizeof(path), "/proc/%d/fd/0", caller_pid);
200         fd = open(path, O_RDONLY);
201         if (fd < 0) {
202                 _E("[DEBUG] Failed to open %s [%s]", path,
203                                 strerror_r(errno, err_buf, sizeof(err_buf)));
204                 return -1;
205         }
206         dup2(fd, 0);
207         close(fd);
208
209         /* stdout */
210         snprintf(path, sizeof(path), "/proc/%d/fd/1", caller_pid);
211         fd = open(path, O_WRONLY);
212         if (fd < 0) {
213                 _E("[DEBUG] Failed to open %s [%s]", path,
214                                 strerror_r(errno, err_buf, sizeof(err_buf)));
215                 return -1;
216         }
217         dup2(fd, 1);
218         close(fd);
219
220         /* stderr */
221         snprintf(path, sizeof(path), "/proc/%d/fd/2", caller_pid);
222         fd = open(path, O_WRONLY);
223         if (fd < 0) {
224                 _E("[DEBUG] Failed to open %s [%s]", path,
225                                 strerror_r(errno, err_buf, sizeof(err_buf)));
226                 return -1;
227         }
228         dup2(fd, 2);
229         close(fd);
230
231         return 0;
232 }
233
234 static void __add_extra_argv(gpointer data, gpointer user_data)
235 {
236         const char *key = (const char *)data;
237         bundle *kb = (bundle *)user_data;
238         const char *str;
239         const char **str_arr = NULL;
240         int len = 0;
241         int i;
242
243         if (key == NULL || kb == NULL)
244                 return;
245
246         _D("[DEBUG] key: %s", key);
247         if (bundle_get_type(kb, key) & BUNDLE_TYPE_ARRAY) {
248                 str_arr = bundle_get_str_array(kb, key, &len);
249         } else {
250                 str = bundle_get_val(kb, key);
251                 if (str) {
252                         str_arr = &str;
253                         len = 1;
254                 }
255         }
256
257         for (i = 0; i < len; i++) {
258                 if (str_arr[i] == NULL)
259                         break;
260
261                 extra_argv_list = g_list_append(extra_argv_list,
262                                 strdup(str_arr[i]));
263         }
264
265         if (str_arr)
266                 bundle_del(kb, key);
267 }
268
269 static void __add_debug_argv(gpointer data, gpointer user_data)
270 {
271         const char *key = (const char *)data;
272         bundle *kb = (bundle *)user_data;
273         const char *str;
274         const char **str_arr = NULL;
275         int len = 0;
276         int i;
277
278         if (key == NULL || kb == NULL)
279                 return;
280
281         _D("[DEBUG] key: %s", key);
282         if (bundle_get_type(kb, key) & BUNDLE_TYPE_ARRAY) {
283                 str_arr = bundle_get_str_array(kb, key, &len);
284         } else {
285                 str = bundle_get_val(kb, key);
286                 if (str) {
287                         str_arr = &str;
288                         len = 1;
289                 }
290         }
291
292         for (i = 0; i < len; i++) {
293                 if (str_arr[i] == NULL)
294                         break;
295
296                 debug_argv_list = g_list_append(debug_argv_list,
297                                 strdup(str_arr[i]));
298         }
299
300         if (str_arr)
301                 bundle_del(kb, key);
302 }
303
304 static void __set_debug_env(gpointer data, gpointer user_data)
305 {
306         const char *key = (const char *)data;
307         bundle *kb = (bundle *)user_data;
308         const char *str;
309         const char **str_arr = NULL;
310         int len = 0;
311         int i;
312         char buf[LINE_MAX] = {0,};
313
314         if (key == NULL || kb == NULL)
315                 return;
316
317         _D("[DEBUG] key: %s", key);
318         if (bundle_get_type(kb, key) & BUNDLE_TYPE_ARRAY) {
319                 str_arr = bundle_get_str_array(kb, key, &len);
320         } else {
321                 str = bundle_get_val(kb, key);
322                 if (str) {
323                         str_arr = &str;
324                         len = 1;
325                 }
326         }
327
328         if (str_arr == NULL)
329                 return;
330
331         strncat(buf, str_arr[0], sizeof(buf) - strlen(buf) - 1);
332         for (i = 1; i < len; i++) {
333                 if (str_arr[i] == NULL)
334                         break;
335
336                 strncat(buf, ",", sizeof(buf) - strlen(buf) - 1);
337                 strncat(buf, str_arr[i], sizeof(buf) - strlen(buf) - 1);
338         }
339
340         bundle_del(kb, key);
341         _D("[DEBUG] name: %s, value: %s", key, buf);
342         setenv(key, buf, 1);
343 }
344
345 static void __remove_file(gpointer data, gpointer user_data)
346 {
347         const char *file = (const char *)data;
348
349         if (file == NULL)
350                 return;
351
352         _D("[DEBUG] file: %s", file);
353         if (access(file, F_OK) == 0) {
354                 if (remove(file) != 0)
355                         _W("[DEBUG] Failed to remove %s", file);
356         }
357 }
358
359 void _debug_prepare_debugger(bundle *kb)
360 {
361         const char *debugger;
362         GList *list;
363         int ret;
364
365         if (kb == NULL)
366                 return;
367
368         debugger = bundle_get_val(kb, AUL_K_SDK);
369         if (debugger == NULL)
370                 return;
371
372         ret = __redirect_std_fds(kb);
373         if (ret < 0)
374                 _E("[DEBUG] Failed to redirect standard fds");
375
376         _D("[DEBUG] debugger: %s", debugger);
377         debugger_info = _debugger_info_find(debugger_info_list, debugger);
378         if (debugger_info == NULL)
379                 return;
380
381         list = _debugger_info_get_unlink_list(debugger_info);
382         g_list_foreach(list, __remove_file, NULL);
383
384         list = _debugger_info_get_extra_env_list(debugger_info);
385         g_list_foreach(list, __set_debug_env, kb);
386
387         list = _debugger_info_get_extra_key_list(debugger_info);
388         g_list_foreach(list, __add_debug_argv, kb);
389
390         list = _debugger_info_get_last_extra_key_list(debugger_info);
391         g_list_foreach(list, __add_extra_argv, kb);
392 }
393
394 int _debug_init(void)
395 {
396         if (debug_initialized)
397                 return 0;
398
399         debugger_info_list = _debugger_info_load(DEBUGGER_INFO_PATH);
400         if (debugger_info_list == NULL)
401                 return -1;
402
403         debug_initialized = 1;
404
405         return 0;
406 }
407
408 void _debug_fini(void)
409 {
410         if (!debug_initialized)
411                 return;
412
413         _debugger_info_unload(debugger_info_list);
414 }