avoid dereference null, fix memory leak
[platform/core/uifw/libtdm.git] / src / tdm_monitor_server.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40
41 #include "tdm.h"
42 #include "tdm_private.h"
43 #include "tdm_helper.h"
44 #include "tdm_log.h"
45
46 #define TDM_DBG_SERVER_ARGS_MAX         32
47
48 static void _tdm_monitor_server_usage(char *app_name, char *reply, int *len);
49
50 static void
51 _tdm_monitor_server_query(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
52 {
53         tdm_helper_get_display_information(dpy, reply, len);
54 }
55
56 static void
57 _tdm_monitor_server_dpms(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
58 {
59         tdm_output *output;
60         int output_idx, dpms_value;
61         char *arg;
62         char *end;
63         tdm_error ret;
64
65         if (argc < 3) {
66                 _tdm_monitor_server_usage(argv[0], reply, len);
67                 return;
68         }
69
70         arg = argv[2];
71         output_idx = strtol(arg, &end, 10);
72         if (*end != ':') {
73                 TDM_SNPRINTF(reply, len, "failed: no onoff value\n");
74                 return;
75         }
76
77         arg = end + 1;
78         dpms_value = strtol(arg, &end, 10);
79
80         output = tdm_display_get_output(dpy, output_idx, &ret);
81         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
82
83         ret = tdm_output_set_dpms(output, dpms_value);
84         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
85
86         TDM_SNPRINTF(reply, len, "done: DPMS %s\n", tdm_dpms_str(dpms_value));
87 }
88
89 static void
90 _tdm_monitor_server_debug(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
91 {
92         int level;
93         char *arg;
94         char *end;
95
96         if (argc < 3) {
97                 _tdm_monitor_server_usage(argv[0], reply, len);
98                 return;
99         }
100
101         arg = argv[2];
102         level = strtol(arg, &end, 10);
103
104         tdm_log_set_debug_level(level);
105         TDM_SNPRINTF(reply, len, "debug level: %d\n", level);
106
107         if (*end == '@') {
108                 char *arg = end + 1;
109
110                 tdm_display_enable_debug_module((const char *)arg);
111
112                 TDM_SNPRINTF(reply, len, "debugging... '%s'\n", arg);
113         }
114 }
115
116 static void
117 _tdm_monitor_server_log_path(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
118 {
119         static int old_stdout = -1;
120         char fd_name[TDM_PATH_LEN];
121         char *path;
122
123         if (argc < 3) {
124                 _tdm_monitor_server_usage(argv[0], reply, len);
125                 return;
126         }
127
128         if (old_stdout == -1)
129                 old_stdout = dup(STDOUT_FILENO);
130
131         path = argv[2];
132         TDM_DBG_RETURN_IF_FAIL(path != NULL);
133
134         tdm_log_enable_dlog(0);
135
136         if (!strncmp(path, "dlog", 4)) {
137                 tdm_log_enable_dlog(1);
138                 goto done;
139         } else if (!strncmp(path, "console", 7))
140                 snprintf(fd_name, TDM_PATH_LEN, "/proc/%d/fd/1", pid);
141         else {
142                 if (path[0] == '/')
143                         snprintf(fd_name, TDM_PATH_LEN, "%s", path);
144                 else {
145                         if (cwd)
146                                 snprintf(fd_name, TDM_PATH_LEN, "%s/%s", cwd, path);
147                         else
148                                 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
149                 }
150                 tdm_log_enable_color(0);
151         }
152
153         if (tdm_display_enable_path((const char*)fd_name) != TDM_ERROR_NONE) {
154                 TDM_SNPRINTF(reply, len, "failed: '%s'\n", path);
155                 return;
156         }
157
158 done:
159         TDM_SNPRINTF(reply, len, "log path: '%s'\n", path);
160 }
161
162 static void
163 _tdm_monitor_server_prop(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
164 {
165         tdm_output *output;
166         tdm_output *layer = NULL;
167         int output_idx, layer_idx = -1;
168         int cnt, i, done = 0;
169         tdm_value value;
170         char temp[TDM_PATH_LEN];
171         char *prop_name;
172         char *arg;
173         char *end;
174         tdm_error ret;
175         const tdm_prop *props;
176
177         if (argc < 3) {
178                 _tdm_monitor_server_usage(argv[0], reply, len);
179                 return;
180         }
181
182         snprintf(temp, TDM_PATH_LEN, "%s", argv[2]);
183         arg = temp;
184
185         output_idx = strtol(arg, &end, 10);
186         if (*end == ',') {
187                 arg = end + 1;
188                 layer_idx = strtol(arg, &end, 10);
189         }
190
191         if (*end != ':') {
192                 TDM_SNPRINTF(reply, len, "failed: no prop_name\n");
193                 return;
194         }
195
196         arg = end + 1;
197         prop_name = strtok_r(arg, ",", &end);
198         if (!prop_name) {
199                 TDM_SNPRINTF(reply, len, "failed: get prop_name by strtok_r\n");
200                 return;
201         }
202
203         if (*end == '\0') {
204                 TDM_SNPRINTF(reply, len, "failed: no value\n");
205                 return;
206         }
207
208         arg = strtok_r(NULL, TDM_DELIM, &end);
209         if (!arg) {
210                 TDM_SNPRINTF(reply, len, "failed: get arg by strtok_r\n");
211                 return;
212         }
213         value.u32 = strtol(arg, &end, 10);
214
215         output = tdm_display_get_output(dpy, output_idx, &ret);
216         TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && output != NULL);
217
218         if (layer_idx != -1) {
219                 layer = tdm_output_get_layer(output, layer_idx, &ret);
220                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE && layer != NULL);
221         }
222
223         if (layer) {
224                 ret = tdm_layer_get_available_properties(layer, &props, &cnt);
225                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
226
227                 for (i = 0; i < cnt; i++) {
228                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
229                                 ret = tdm_layer_set_property(layer, props[i].id, value);
230                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
231                                 done = 1;
232                                 break;
233                         }
234                 }
235         } else {
236                 ret = tdm_output_get_available_properties(output, &props, &cnt);
237                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
238
239                 for (i = 0; i < cnt; i++) {
240                         if (!strncmp(props[i].name, prop_name, TDM_NAME_LEN)) {
241                                 ret = tdm_output_set_property(output, props[i].id, value);
242                                 TDM_DBG_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
243                                 done = 1;
244                                 break;
245                         }
246                 }
247         }
248
249         if (done)
250                 TDM_SNPRINTF(reply, len, "done: %s:%d \n", prop_name, value.u32);
251         else
252                 TDM_SNPRINTF(reply, len, "no '%s' propperty \n", prop_name);
253 }
254
255 static void
256 _tdm_monitor_server_dump(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy)
257 {
258         if (argc < 3) {
259                 _tdm_monitor_server_usage(argv[0], reply, len);
260                 return;
261         }
262
263         tdm_display_enable_dump(dpy, (const char*)argv[2], reply, len);
264 }
265
266 static struct {
267         const char *opt;
268         void (*func)(unsigned int pid, char *cwd, int argc, char *argv[], char *reply, int *len, tdm_display *dpy);
269         const char *desc;
270         const char *arg;
271         const char *ex;
272 } option_proc[] = {
273         {
274                 "info", _tdm_monitor_server_query,
275                 "show tdm output, layer information", NULL, NULL
276         },
277         {
278                 "dpms", _tdm_monitor_server_dpms,
279                 "set output dpms", "<output_idx>:<dpms>", "0:3 or 0:0"
280         },
281         {
282                 "debug", _tdm_monitor_server_debug,
283                 "set the debug level and modules(module: none, mutex, buffer, thread, vblank)",
284                 "<level>[@<module1>[,<module2>]]",
285                 NULL
286         },
287         {
288                 "log_path", _tdm_monitor_server_log_path,
289                 "set the log path (console,dlog,filepath)",
290                 "<path>",
291                 "console"
292         },
293         {
294                 "prop", _tdm_monitor_server_prop,
295                 "set the property of a output or a layer",
296                 "<output_idx>[,<layer_idx>]:<prop_name>,<value>",
297                 NULL
298         },
299         {
300                 "dump", _tdm_monitor_server_dump,
301                 "dump buffers (type: none, layer, pp, capture, current)\n"
302                 "\t\t  layer, pp, capture - start to dump buffers of layer, pp, capture\n"
303                 "\t\t  none               - stop to dump buffers\n"
304                 "\t\t  current            - dump the current buffer of all layers",
305                 "<object_type1>[,<object_type2>[,...]]@[<directory_path>]",
306                 NULL
307         },
308 };
309
310 static void
311 _tdm_monitor_server_usage(char *app_name, char *reply, int *len)
312 {
313         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
314         int i;
315
316         TDM_SNPRINTF(reply, len, "usage: %s \n\n", app_name);
317
318         for (i = 0; i < opt_size; i++) {
319                 TDM_SNPRINTF(reply, len, "\t-%s\t%s\n", option_proc[i].opt, option_proc[i].desc);
320                 if (option_proc[i].arg)
321                         TDM_SNPRINTF(reply, len, "\t\t  %s\n", option_proc[i].arg);
322                 if (option_proc[i].ex)
323                         TDM_SNPRINTF(reply, len, "\t\t  ex) %s\n", option_proc[i].ex);
324                 TDM_SNPRINTF(reply, len, "\n");
325         }
326 }
327
328 static void
329 _tdm_monitor_server_command(unsigned int pid, char *cwd, tdm_display *dpy, int argc, char *argv[], char *reply, int *len)
330 {
331         int opt_size = sizeof(option_proc) / sizeof(option_proc[0]);
332         int i;
333
334         if (argc < 2) {
335                 _tdm_monitor_server_usage(argv[0], reply, len);
336                 return;
337         }
338
339         for (i = 0; i < opt_size; i++) {
340                 if (argv[1][0] == '-' && !strncmp(argv[1] + 1, option_proc[i].opt, 32)) {
341                         if (option_proc[i].func) {
342                                 option_proc[i].func(pid, cwd, argc, argv, reply, len, dpy);
343                                 return;
344                         } else {
345                                 TDM_SNPRINTF(reply, len, "'%s' not implemented.\n", argv[1]);
346                                 return;
347                         }
348                 }
349         }
350
351         _tdm_monitor_server_usage(argv[0], reply, len);
352         return;
353 }
354
355
356 INTERN void
357 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len)
358 {
359         unsigned int pid;
360         char cwd[1024];
361         int argc = 0;
362         char *argv[TDM_DBG_SERVER_ARGS_MAX] = {0,};
363         char temp[1024];
364         char *arg;
365         char *end = NULL, *e;
366
367         snprintf(temp, sizeof(temp), "%s", options);
368
369         arg = strtok_r(temp, " ", &end);
370         if (!arg) {
371                 TDM_SNPRINTF(reply, len, "no pid for tdm-monitor");
372                 return;
373         }
374         pid = strtol(arg, &e, 10);
375
376         arg = strtok_r(NULL, " ", &end);
377         if (!arg) {
378                 TDM_SNPRINTF(reply, len, "no cwd for tdm-monitor");
379                 return;
380         }
381         snprintf(cwd, sizeof(cwd), "%s", arg);
382
383         TDM_DBG("pid(%d) cwd(%s)", pid, cwd);
384
385         argv[argc] = strtok_r(NULL, " ", &end);
386         while (argv[argc]) {
387                 argc++;
388                 if (argc == TDM_DBG_SERVER_ARGS_MAX) {
389                         TDM_SNPRINTF(reply, len, "too many arguments for tdm-monitor");
390                         break;
391                 }
392                 argv[argc] = strtok_r(NULL, " ", &end);
393         }
394
395         _tdm_monitor_server_command(pid, cwd, dpy, argc, argv, reply, len);
396 }