Added support for shell command to be handled by plugin. 43/186543/4 accepted/tizen/unified/20180824.162454 submit/tizen/20180824.082919
authoradhavan.m <adhavan.m@samsung.com>
Fri, 10 Aug 2018 07:07:14 +0000 (12:37 +0530)
committerManish Rathod <manish.r@samsung.com>
Fri, 24 Aug 2018 08:02:25 +0000 (08:02 +0000)
Change-Id: I2aeb9bd570d630d96650851ff3b12a424f077bca
Signed-off-by: adhavan.m <adhavan.m@samsung.com>
CMakeLists.txt
src/default_plugin.h
src/default_plugin_basic.c
src/default_plugin_main.c
src/default_plugin_shellcmd.c [new file with mode: 0755]
src/plugin.c
src/sdbd_plugin.h
src/services.c

index 7793229..5d376e1 100755 (executable)
@@ -53,6 +53,7 @@ SET(SDBD_SRCS
         src/default_plugin_main.c
         src/default_plugin_event.c
         src/default_plugin_appcmd.c
+        src/default_plugin_shellcmd.c
         src/hashtable.c
         src/plugin.c
         src/plugin_encrypt.c
index 9420d7c..f589196 100644 (file)
@@ -34,6 +34,7 @@ int auth_get_key_file_paths ( parameters* in, parameters* out );
 
 int confirm_public_key ( parameters* in, int out_fd );
 int appcmd_service ( parameters* in, int out_fd );
+int shellcmd_service ( parameters* in, int out_fd );
 
 void create_pwlock_thread();
 
index 80f82c2..4d4fdb5 100644 (file)
@@ -302,3 +302,36 @@ int verify_push ( parameters* in, parameters* out )
 
     return PLUGIN_CMD_SUCCESS;
 }
+
+int verify_handle_by_plugin ( parameters* in, parameters* out )
+{
+    if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
+            || in->array_of_parameter[0].type != type_string ) {
+        D ( "Invalid argument\n" );
+        return PLUGIN_CMD_FAIL;
+    }
+
+    if ( out == NULL ) {
+        D ( "Invalid argument\n" );
+        return PLUGIN_CMD_FAIL;
+    }
+
+    D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data );
+
+    out->number_of_parameter = 1;
+    out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
+    if (out->array_of_parameter == NULL) {
+        D("failed to allocate memory for the parameter\n");
+        return PLUGIN_CMD_FAIL;
+    }
+    if(!strncmp(in->array_of_parameter[0].v_string.data, "sample-echo", 11)) {
+       out->array_of_parameter[0].type = type_int32;
+       out->array_of_parameter[0].v_int32 = PLUGIN_RET_HANDLE;
+    }
+    else {
+       out->array_of_parameter[0].type = type_int32;
+       out->array_of_parameter[0].v_int32 = PLUGIN_RET_NOT_HANDLE;
+    }
+
+    return PLUGIN_CMD_SUCCESS;
+}
index cc1abf4..f6f1f71 100644 (file)
@@ -62,6 +62,8 @@ int default_plugin_sync_proc ( int cmd, parameters* in, parameters* out )
         ret = get_shell_env ( in, out );
     } else if ( cmd == PLUGIN_SYNC_CMD_VERITY_PUSH ) {
         ret = verify_push ( in, out );
+    } else if ( cmd == PLUGIN_SYNC_CMD_HANDLE_BY_PLUGIN ) {
+        ret = verify_handle_by_plugin ( in, out );
     } else {
         ret = PLUGIN_CMD_NOT_SUPPORT;
     }
@@ -77,6 +79,8 @@ int default_plugin_async_proc ( int cmd, parameters* in, int out_fd )
         ret = confirm_public_key ( in, out_fd );
     } else if ( cmd == PLUGIN_ASYNC_CMD_APPCMD_SERVICE ) {
         ret = appcmd_service ( in, out_fd );
+    } else if ( cmd == PLUGIN_ASYNC_CMD_HANDLEBYPLUGIN_SERVICE ) {
+        ret = shellcmd_service ( in, out_fd );
     } else {
         ret = PLUGIN_CMD_NOT_SUPPORT;
     }
diff --git a/src/default_plugin_shellcmd.c b/src/default_plugin_shellcmd.c
new file mode 100755 (executable)
index 0000000..7e5bf2b
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define TRACE_TAG  TRACE_SHELLCMD
+#define LOG_TAG "SDBD_TRACE_SHELLCMD"
+
+#include "log.h"
+#include "parameter.h"
+#include "sdbd_plugin.h"
+
+#define SHELLCMD_RESULT_BUFSIZE   (4096)
+
+
+int shellcmd_service( parameters* in, int out_fd ) {
+    char result_buf[SHELLCMD_RESULT_BUFSIZE] = {0,};
+    char* command = NULL;
+
+    if (in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
+            || in->array_of_parameter[0].type != type_string) {
+        D ( "Invalid argument\n" );
+        return PLUGIN_CMD_FAIL;
+    }
+
+    command = in->array_of_parameter[0].v_string.data;
+    D("command=%s(FD:%d)\n", command, out_fd);
+
+    if(!strncmp(command, "sample-echo", 11)) {
+       D("command arg: %s\n", command+12);
+       snprintf(result_buf, sizeof(result_buf), "%s\n", command+12);
+       writex(out_fd, result_buf, strlen(result_buf)+1);
+       return PLUGIN_CMD_SUCCESS;
+    }
+
+    return PLUGIN_CMD_FAIL;
+}
index 96956b7..73d095e 100644 (file)
@@ -404,6 +404,75 @@ int request_lock_state_to_plugin ( int lock_type )
     return result;
 }
 
+// return 1 if shell command handled by plugin (handle)
+// return 0 if shell command not handled by plugin (not handle)
+int request_handlecmd_to_plugin ( int cmd, const char* in_buf )
+{
+    int success = 0;
+    int ret;
+    parameters in, out;
+
+    if ( in_buf != NULL ) {
+        in.number_of_parameter = 1;
+        in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
+        if (in.array_of_parameter == NULL) {
+            D("failed to allocate memory for the parameter\n");
+            return success;
+        }
+        in.array_of_parameter[0].type = type_string;
+        in.array_of_parameter[0].v_string.length = strlen ( in_buf );
+        in.array_of_parameter[0].v_string.data = strdup ( in_buf );
+    } else {
+        in.number_of_parameter = 0;
+        in.array_of_parameter = NULL;
+    }
+
+    D ("requested command handling : %d, %s\n", cmd, in_buf);
+
+    ret = request_sync_cmd ( cmd, &in, &out );
+    if ( ret == PLUGIN_CMD_SUCCESS ) {
+        success = ( out.array_of_parameter[0].v_int32 == PLUGIN_RET_HANDLE ) ? 1 : 0;
+        release_parameters ( &out );
+
+        D ("request command handling success : %d\n", success);
+    }
+
+    release_parameters ( &in );
+    return success;
+}
+
+int request_shellcmd_to_plugin ( const char* in_buf )
+{
+    parameters* in;
+    int fd;
+
+    in = (parameters*)malloc(sizeof(parameters));
+    if (in == NULL) {
+        E("failed to allocate memory for the parameters\n");
+        return -1;
+    }
+
+    if ( in_buf != NULL ) {
+        in->number_of_parameter = 1;
+        in->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
+        if (in->array_of_parameter == NULL) {
+            free(in);
+            E("failed to allocate memory for the parameter\n");
+            return -1;
+        }
+        in->array_of_parameter[0].type = type_string;
+        in->array_of_parameter[0].v_string.length = strlen ( in_buf );
+        in->array_of_parameter[0].v_string.data = strdup ( in_buf );
+    } else {
+        in->number_of_parameter = 0;
+        in->array_of_parameter = NULL;
+    }
+
+    fd = create_async_proc_thread( PLUGIN_ASYNC_CMD_HANDLEBYPLUGIN_SERVICE, in );
+
+    return fd;
+}
+
 // return nonnegative integer that is a socket descriptor for communication
 //        with async proc thread if success to create async proc thread
 // return -1 if failed to create async proc thread
index e31eb31..64c25db 100644 (file)
@@ -36,6 +36,7 @@
 #define PLUGIN_SYNC_CMD_GET_SHELL_ENV               1009
 #define PLUGIN_SYNC_CMD_VERITY_PUSH                 1010
 #define PLUGIN_SYNC_CMD_VERIFY_PEERIPV6             1011
+#define PLUGIN_SYNC_CMD_HANDLE_BY_PLUGIN            1012
 
 #define PLUGIN_SYNC_CMD_SEC_INIT                    1100
 #define PLUGIN_SYNC_CMD_SEC_DEINIT                  1101
@@ -49,6 +50,7 @@
 // asynchronous command
 #define PLUGIN_ASYNC_CMD_AUTH_CONFIRM_PUBLIC        2000
 #define PLUGIN_ASYNC_CMD_APPCMD_SERVICE             2001
+#define PLUGIN_ASYNC_CMD_HANDLEBYPLUGIN_SERVICE     2002
 
 // event
 #define PLUGIN_EVENT_PWLOCK                         3000
@@ -101,6 +103,8 @@ typedef enum _priority {
 #define PLUGIN_RET_OFF                  11
 #define PLUGIN_RET_VALID                12
 #define PLUGIN_RET_INVALID              13
+#define PLUGIN_RET_HANDLE               14
+#define PLUGIN_RET_NOT_HANDLE           15
 #define PLUGIN_CMD_SUCCESS              101
 #define PLUGIN_CMD_FAIL                 102
 #define PLUGIN_CMD_NOT_SUPPORT          103
index 4f5634b..7f61a0b 100644 (file)
@@ -1266,7 +1266,13 @@ int service_to_fd(const char *name)
         ret = create_service_thread(log_service, get_log_file_path(name + 4));
     }*/ else if(!HOST && !strncmp(name, "shell:", 6)) {
         if(name[6]) {
-            ret = create_subproc_thread(name + 6, 0, 0);
+               if (request_handlecmd_to_plugin(PLUGIN_SYNC_CMD_HANDLE_BY_PLUGIN, name+6)) {
+                       D("This shell command is handled by plugin. (%s)\n", name+6);
+                       ret = request_shellcmd_to_plugin(name+6);
+               }
+               else {
+                       ret = create_subproc_thread(name + 6, 0, 0);
+               }
         } else {
             ret = create_subproc_thread(NULL, 0, 0);
         }