add list command 61/54161/1 accepted/tizen/mobile/20151214.055728 accepted/tizen/tv/20151214.055748 accepted/tizen/wearable/20151214.055814 submit/tizen/20151214.010311
authorSooChan Lim <sc1.lim@samsung.com>
Mon, 14 Dec 2015 00:51:26 +0000 (09:51 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Mon, 14 Dec 2015 00:51:26 +0000 (09:51 +0900)
The list command shows the list of the wayland-tbm clients.

Change-Id: Iec64ba0c9d5ce7b0f9f7a6ad622fa184b4fa7b28

src/Makefile.am
src/wayland-tbm-int.h
src/wayland-tbm-server.c
src/wayland-tbm-util.c [new file with mode: 0755]
tool/wayland-tbm-monitor.c

index 569b44b..d32ac41 100644 (file)
@@ -24,7 +24,8 @@ libwayland_tbm_server_la_LIBADD = \
        $(WL_TBM_SERVER_LIBS)
 libwayland_tbm_server_la_SOURCES = \
        wayland-tbm-client.c \
-       wayland-tbm-server.c
+       wayland-tbm-server.c \
+       wayland-tbm-util.c
 
 nodist_libwayland_tbm_server_la_SOURCES =              \
        wayland-tbm-server-protocol.h   \
@@ -40,7 +41,8 @@ libwayland_tbm_client_la_LIBADD = \
        $(WL_TBM_COMMON_LIBS) \
        $(WL_TBM_CLIENT_LIBS)
 libwayland_tbm_client_la_SOURCES = \
-       wayland-tbm-client.c
+       wayland-tbm-client.c \
+       wayland-tbm-util.c
 
 nodist_libwayland_tbm_client_la_SOURCES =              \
        wayland-tbm-client-protocol.h   \
index bc423f7..56205e3 100755 (executable)
@@ -36,7 +36,10 @@ extern "C" {
 
 #include "wayland-tbm-client.h"
 
-#define WL_TBM_LOG(...)  fprintf (stderr, __VA_ARGS__)
+#define WL_TBM_DEBUG(fmt, ...)   fprintf (stderr, "[WL_TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__)
+#define WL_TBM_C_LOG(fmt, ...)   fprintf (stderr, "[WL_TBM_C(%d):%s] " fmt, getpid(), __func__, ##__VA_ARGS__)
+#define WL_TBM_S_LOG(fmt, ...)   fprintf (stderr, "[WL_TBM_S(%d):%s] " fmt, getpid(), __func__, ##__VA_ARGS__)
+#define WL_TBM_LOG(fmt, ...)     fprintf (stderr, "[WL_TBM(%d)] " fmt, getpid(), ##__VA_ARGS__)
 
 /* check condition */
 #define WL_TBM_RETURN_IF_FAIL(cond) {\
@@ -60,6 +63,7 @@ extern "C" {
 
 typedef enum
 {
+       WL_TBM_MONITOR_COMMAND_LIST,
     WL_TBM_MONITOR_COMMAND_SHOW,
     WL_TBM_MONITOR_COMMAND_TRACE,
 } WL_TBM_MONITOR_COMMAND;
@@ -91,6 +95,9 @@ int32_t      _wayland_tbm_client_get_embedded_auth_fd(struct wayland_tbm_client
 uint32_t     _wayland_tbm_client_get_embedded_capability(struct wayland_tbm_client *tbm_client);
 const char  *_wayland_tbm_client_get_embedded_device_name(struct wayland_tbm_client *tbm_client);
 
+void    _wayland_tbm_util_get_appname_brief(char *brief);
+void    _wayland_tbm_util_get_appname_from_pid(long pid, char *str);
+
 #ifdef  __cplusplus
 }
 #endif
index b7566eb..19e906d 100755 (executable)
@@ -80,6 +80,7 @@ struct wl_tbm_buffer {
 struct wayland_tbm_client_resource {
     struct wl_resource *resource;
        pid_t pid;
+    char *app_name;
        struct wl_list link;
 };
 
@@ -262,12 +263,39 @@ _wayland_tbm_server_impl_request_tbm_monitor(struct wl_client *client,
 {
     struct wayland_tbm_server *tbm_srv = wl_resource_get_user_data(resource);
        struct wayland_tbm_client_resource *c_res = NULL, *tmp_res;
+    int i = 0;
 
 #ifdef WL_TBM_SERVER_DEBUG
    WL_TBM_LOG("[%s]: command=%d, trace_command=%d, target=%d, pid=%d.\n", __func__,
        command, trace_command, target, pid);
 #endif
 
+       if (command == WL_TBM_MONITOR_COMMAND_LIST) {
+               WL_TBM_DEBUG("==================  app list       =======================\n");
+               WL_TBM_DEBUG("no pid  app_name\n");
+
+               if (!wl_list_empty(&tbm_srv->client_resource_list)) {
+                       wl_list_for_each_safe(c_res, tmp_res, &tbm_srv->client_resource_list, link) {
+                /* skip the requestor (wayland-tbm-monitor */
+                if (c_res->resource == resource)
+                    continue;
+
+                if (!c_res->app_name) {
+                    c_res->app_name = (char*) calloc(1, 255*sizeof(char));
+
+                    _wayland_tbm_util_get_appname_from_pid(c_res->pid, c_res->app_name);
+                    _wayland_tbm_util_get_appname_brief(c_res->app_name);
+                }
+
+                WL_TBM_DEBUG("%-3d%-5d%s\n", ++i, c_res->pid, c_res->app_name);
+                       }
+               }
+
+               WL_TBM_DEBUG("======================================================\n");
+
+               return;
+       }
+
     if (target == WL_TBM_MONITOR_TARGET_CLIENT) {
         if (pid < 1) {
                        wl_resource_post_error(resource, WL_TBM_ERROR_INVALID_FORMAT, "invalid format");
@@ -479,9 +507,13 @@ _wayland_tbm_server_destroy_resource (struct wl_resource *resource)
        if (!wl_list_empty(&tbm_srv->client_resource_list)) {
                wl_list_for_each_safe(c_res, tmp_res, &tbm_srv->client_resource_list, link) {
                        if (c_res->resource == resource) {
+#ifdef WL_TBM_SERVER_DEBUG
                                WL_TBM_LOG("[%s]: resource,%p pid,%d \n", __func__, c_res->resource, c_res->pid);
-
+#endif
                                wl_list_remove(&c_res->link);
+                if (c_res->app_name)
+                    free(c_res->app_name);
+                free(c_res);
                                break;
             }
                }
@@ -516,7 +548,9 @@ _wayland_tbm_server_bind_cb(struct wl_client *client, void *data, uint32_t versi
     tbm_srv = wl_resource_get_user_data(resource);
        wl_client_get_credentials(client, &pid, &uid, &gid);
 
+#ifdef WL_TBM_SERVER_DEBUG
     WL_TBM_LOG("[%s]: resource,%p pid,%d \n", __func__, resource, pid);
+#endif
 
     c_res = calloc (1, sizeof(struct wayland_tbm_client_resource));
     c_res->pid = pid;
diff --git a/src/wayland-tbm-util.c b/src/wayland-tbm-util.c
new file mode 100755 (executable)
index 0000000..7a7d32f
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+Copyright (C) 2015 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact:
+      SooChan Lim <sc1.lim@samsung.com>,
+      Sangjin Lee <lsj119@samsung.com>,
+      Boram Park <boram1288.park@samsung.com>,
+      Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice (including the next
+paragraph) shall be included in all copies or substantial portions of the
+Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "wayland-tbm-int.h"
+
+void
+_wayland_tbm_util_get_appname_brief(char *brief)
+{
+    char delim[] = "/";
+    char *token = NULL;
+    char temp[255] = {0,};
+    char *saveptr = NULL;
+
+    token = strtok_r(brief, delim, &saveptr);
+
+    while (token != NULL) {
+        memset(temp, 0x00, 255*sizeof(char));
+        strncpy(temp, token, 254*sizeof(char));
+        token = strtok_r(NULL, delim, &saveptr);
+    }
+
+    snprintf(brief, sizeof(temp), "%s", temp);
+}
+
+void
+_wayland_tbm_util_get_appname_from_pid(long pid, char *str)
+{
+    FILE* fp;
+    int len;
+    long app_pid = pid;
+    char fn_cmdline[255] = {0,};
+    char cmdline[255] = {0,};
+
+    snprintf(fn_cmdline, sizeof(fn_cmdline), "/proc/%ld/cmdline",app_pid);
+
+    fp = fopen(fn_cmdline, "r");
+    if(fp == 0) {
+        fprintf(stderr,"cannot file open /proc/%ld/cmdline", app_pid);
+        return;
+    }
+
+    if (!fgets(cmdline, 255, fp)) {
+        fprintf(stderr, "fail to get appname for pid(%ld)\n", app_pid);
+        fclose(fp);
+        return;
+    }
+    fclose(fp);
+
+    len = strlen(cmdline);
+    if(len < 1)
+        memset(cmdline, 0x00,255);
+    else
+        cmdline[len] = 0;
+
+    snprintf(str, sizeof(cmdline), "%s", cmdline);
+}
+
index 10f4553..9aa7df2 100755 (executable)
@@ -126,9 +126,18 @@ _wl_tbm_show_usage()
 }
 
 static void
+_wl_tbm_list_usage()
+{
+       WL_TBM_LOG("  list : list the wayland-tbm client\n");
+       WL_TBM_LOG("   list usage : wayland-tbm-monitor list\n");
+       WL_TBM_LOG("\n");
+}
+
+static void
 _wl_tbm_usage()
 {
        WL_TBM_LOG("wayland-tbm-monitor : show/trace the infomation of tbm_bo in clietns and in server.\n");
+    _wl_tbm_list_usage();
     _wl_tbm_show_usage();
     _wl_tbm_trace_usage();
        WL_TBM_LOG("\n");
@@ -165,7 +174,9 @@ _wl_tbm_monitor_process_options(struct wayland_tbm_monitor *tbm_monitor, int arg
         return 0;
     }
 
-    if (!strncmp(argv[1], "show", 4)) {
+    if (!strncmp(argv[1], "list", 4)) {
+        tbm_monitor->options.command = WL_TBM_MONITOR_COMMAND_LIST;
+    } else if (!strncmp(argv[1], "show", 4)) {
         if (argc < 3) {
                        _wl_tbm_show_usage();
                        return 0;
@@ -197,6 +208,7 @@ _wl_tbm_monitor_process_options(struct wayland_tbm_monitor *tbm_monitor, int arg
 
                        tbm_monitor->options.trace_command = WL_TBM_MONITOR_TRACE_COMMAND_REGISTER;
                        if (_wl_tbm_select_target_option(tbm_monitor, argc, argv, 3)){
+                               WL_TBM_LOG("error: no pid. please type the target(client [pid]/server/all).\n");
                                _wl_tbm_trace_usage();
                                return 0;
                        }
@@ -209,6 +221,7 @@ _wl_tbm_monitor_process_options(struct wayland_tbm_monitor *tbm_monitor, int arg
 
                        tbm_monitor->options.trace_command = WL_TBM_MONITOR_TRACE_COMMAND_UNREGISTER;
                        if (_wl_tbm_select_target_option(tbm_monitor, argc, argv, 3)){
+                               WL_TBM_LOG("error: no pid. please type the target(client [pid]/server/all).\n");
                                _wl_tbm_trace_usage();
                                return 0;
                        }
@@ -241,7 +254,9 @@ main(int argc, char* argv[])
    WL_TBM_RETURN_VAL_IF_FAIL(tbm_monitor != NULL, -1);
 
    ret = _wl_tbm_monitor_process_options(tbm_monitor, argc, argv);
-   WL_TBM_GOTO_IF_FAIL(ret != 0, finish);
+   if (!ret) {
+       goto finish;
+   }
 
    tbm_monitor->dpy = wl_display_connect(NULL);
    WL_TBM_GOTO_IF_FAIL(tbm_monitor->dpy != NULL, finish);