Implementation of fork-exec 94/243594/4 accepted/tizen/unified/20200911.143457 submit/tizen/20200909.080622
authorManish Toshan Rathod/Tizen Platform /SRI-Bangalore/Staff Engineer/Samsung Electronics <manish.r@samsung.com>
Tue, 8 Sep 2020 10:15:33 +0000 (15:45 +0530)
committerManish Toshan Rathod/Tizen Platform /SRI-Bangalore/Staff Engineer/Samsung Electronics <manish.r@samsung.com>
Tue, 8 Sep 2020 10:56:39 +0000 (16:26 +0530)
Change-Id: I4d40c8c810c5d2a7717c11bd817502879b93109b
Signed-off-by: Manish Toshan Rathod/Tizen Platform /SRI-Bangalore/Staff Engineer/Samsung Electronics <manish.r@samsung.com>
CMakeLists.txt
packaging/sdbd.spec
src/file_sync_service.h
src/sdb.c
src/sdbd.c [new file with mode: 0644]
src/sdbd_service.c [new file with mode: 0644]
src/services.c
src/sysdeps.h

index 5d376e1..bac4df9 100755 (executable)
@@ -58,7 +58,44 @@ SET(SDBD_SRCS
         src/plugin.c
         src/plugin_encrypt.c
         src/descs_strings.c
+        src/sdbd.c
 )
+
+SET(SDBD_SERVICE_SRCS
+        src/sdb.c
+        src/fdevent.c
+        src/transport.c
+        src/transport_local.c
+        src/transport_usb.c
+        src/sockets.c
+        src/file_sync_service.c
+        src/usb_linux_client.c
+        src/utils.c
+        src/socket_inaddr_any_server.c
+        src/socket_local_client.c
+        src/socket_local_server.c
+        src/socket_loopback_client.c
+        src/socket_loopback_server.c
+        src/socket_network_client.c
+        src/sdktools.c
+        src/strutils.c
+        src/commandline_sdbd.c
+        src/usb_linux_client.c
+        src/usb_funcfs_client.c
+        src/default_plugin_auth.c
+        src/default_plugin_basic.c
+        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
+        src/descs_strings.c
+        src/services.c
+        src/sdbd_service.c
+)
+
 SET(SDBD_SUBS
         src/subprocess.c
 )
@@ -111,7 +148,9 @@ find_package(Threads REQUIRED)
 
 ADD_EXECUTABLE(sdbd ${SDBD_SRCS})
 ADD_EXECUTABLE(sdbd-user ${SDBD_SUBS})
+ADD_EXECUTABLE(sdbd-service ${SDBD_SERVICE_SRCS})
 TARGET_LINK_LIBRARIES(sdbd -pie -lsmack -lresolv -ldl ${CMAKE_THREAD_LIBS_INIT} ${pkgs_LDFLAGS} ${SYSTEMD_LDFLAGS})
+TARGET_LINK_LIBRARIES(sdbd-service -pie -lsmack -lresolv -ldl ${CMAKE_THREAD_LIBS_INIT} ${pkgs_LDFLAGS} ${SYSTEMD_LDFLAGS})
 TARGET_LINK_LIBRARIES(sdbd-user -pie ${CMAKE_THREAD_LIBS_INIT} ${pkgs_LDFLAGS})
 
 set_property(
@@ -130,6 +169,23 @@ set_property(
         HAVE_FORKEXEC
 )
 
+set_property(
+        TARGET sdbd-service
+        PROPERTY COMPILE_DEFINITIONS
+        SDB_HOST=0
+        _DROP_PRIVILEGE
+        _FILE_OFFSET_BITS=64
+)
+
+set_property(
+        TARGET sdbd-service
+        APPEND PROPERTY COMPILE_DEFINITIONS
+        _XOPEN_SOURCE
+        _GNU_SOURCE
+        HAVE_FORKEXEC
+)
+
+
 if(USE_FUNCTION_FS)
         set_property(
                 TARGET sdbd
@@ -138,7 +194,7 @@ if(USE_FUNCTION_FS)
         )
 endif()
 
-install(TARGETS sdbd sdbd-user DESTINATION /usr/sbin)
+install(TARGETS sdbd sdbd-user sdbd-service DESTINATION /usr/sbin)
 install(FILES script/sdbd DESTINATION /etc/init.d)
 
 # Extract descriptors and strings for systemd socket activation
index 8e4635f..a9ad652 100644 (file)
@@ -136,6 +136,7 @@ chsmack -e "User::Shell" /sbin/sdbd-user
 %defattr(-,root,root,-)
 %{_prefix}/sbin/sdbd
 %{_prefix}/sbin/sdbd-user
+%{_prefix}/sbin/sdbd-service
 %{_prefix}/sbin/sdk_launch
 %attr(0755, root, root) %{_sysconfdir}/init.d/sdbd
 %{_unitdir}/sdbd.service
index 089a212..3b94055 100644 (file)
@@ -86,4 +86,6 @@ int do_sync_pull(const char *rpath, const char *lpath);
 
 #define SYNC_DATA_MAX (64*1024)
 
+#define EXEC_AFTER_FORK 1 //with this enabled sdbd-service will be used for file sync services
+
 #endif
index 2732b81..504382c 100755 (executable)
--- a/src/sdb.c
+++ b/src/sdb.c
@@ -1,4 +1,4 @@
-/*
+/*
  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
@@ -50,7 +50,6 @@
 #endif
 
 #include <linux/prctl.h>
-#define SDB_PIDPATH "/tmp/.sdbd.pid"
 #include <system_info.h>
 #include <vconf.h>
 #include <glib.h>
@@ -108,6 +107,8 @@ struct group_info g_default_groups[] = {
 int is_init_sdk_userinfo = 0;
 int is_pwlocked = 0;    // 0 if unlocked, 1 otherwise
 
+int recovery_mode = 0;
+
 SdbdCommandlineArgs sdbd_commandline_args;
 
 static int is_support_usbproto();
@@ -194,13 +195,6 @@ int is_container_enabled(void) {
     }
 }
 
-void handle_sig_term(int sig) {
-#ifdef SDB_PIDPATH
-    if (access(SDB_PIDPATH, F_OK) == 0)
-        sdb_unlink(SDB_PIDPATH);
-#endif
-    exit(0);
-}
 
 static const char *sdb_device_banner = "device";
 
@@ -2161,6 +2155,12 @@ int sdb_main(int server_port)
     return 0;
 }
 
+void sdb_main_service()
+{
+    init_capabilities();
+    init_sdk_requirements();
+}
+
 int copy_packet(apacket* dest, apacket* src) {
 
     if(dest == NULL) {
@@ -2254,43 +2254,4 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r
         return 0;
     }
     return -1;
-}
-
-int recovery_mode = 0;
-
-int main(int argc, char **argv)
-{
-    /* If sdbd runs inside the emulator this will enable sdb tracing via
-     * sdb-debug qemud service in the emulator. */
-#if 0 /* tizen specific */
-    sdb_qemu_trace_init();
-    if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
-        sdb_device_banner = "recovery";
-        recovery_mode = 1;
-    }
-#endif
-
-    apply_sdbd_commandline_defaults(&sdbd_commandline_args);
-    int parse_ret = parse_sdbd_commandline(&sdbd_commandline_args, argc, argv);
-
-    // TODO: Add detailed error messages
-    // TODO: Add individual messages for help and usage
-    if(parse_ret != SDBD_COMMANDLINE_SUCCESS) {
-        if (parse_ret == SDBD_COMMANDLINE_HELP
-                || parse_ret == SDBD_COMMANDLINE_USAGE) {
-            // User requested help or usage
-            print_sdbd_usage_message(stdout);
-            return EXIT_SUCCESS;
-        }
-
-        // Print usage message because of invalid options
-        print_sdbd_usage_message(stderr);
-        return EXIT_FAILURE;
-    }
-
-    I("Handling main()\n");
-
-    //sdbd will never die on emulator!
-    signal(SIGTERM, handle_sig_term); /* tizen specific */
-    return sdb_main(DEFAULT_SDB_PORT);
-}
+}
\ No newline at end of file
diff --git a/src/sdbd.c b/src/sdbd.c
new file mode 100644 (file)
index 0000000..826da15
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd
+ *
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include "sdb.h"
+#include "sysdeps.h"
+#include "commandline_sdbd.h"
+#define LOG_TAG "SDBD_TRACE_SDB"
+#include "log.h"
+
+extern SdbdCommandlineArgs sdbd_commandline_args;
+
+#define SDB_PIDPATH "/tmp/.sdbd.pid"
+
+void handle_sig_term(int sig) {
+#ifdef SDB_PIDPATH
+    if (access(SDB_PIDPATH, F_OK) == 0)
+        sdb_unlink(SDB_PIDPATH);
+#endif
+    exit(0);
+}
+
+int main(int argc, char **argv)
+{
+    /* If sdbd runs inside the emulator this will enable sdb tracing via
+     * sdb-debug qemud service in the emulator. */
+#if 0 /* tizen specific */
+    sdb_qemu_trace_init();
+    if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
+        sdb_device_banner = "recovery";
+        recovery_mode = 1;
+    }
+#endif
+
+    apply_sdbd_commandline_defaults(&sdbd_commandline_args);
+    int parse_ret = parse_sdbd_commandline(&sdbd_commandline_args, argc, argv);
+
+    // TODO: Add detailed error messages
+    // TODO: Add individual messages for help and usage
+    if(parse_ret != SDBD_COMMANDLINE_SUCCESS) {
+        if (parse_ret == SDBD_COMMANDLINE_HELP
+                || parse_ret == SDBD_COMMANDLINE_USAGE) {
+            // User requested help or usage
+            print_sdbd_usage_message(stdout);
+            return EXIT_SUCCESS;
+        }
+
+        // Print usage message because of invalid options
+        print_sdbd_usage_message(stderr);
+        return EXIT_FAILURE;
+    }
+
+    I("Handling main()\n");
+
+    //sdbd will never die on emulator!
+    signal(SIGTERM, handle_sig_term); /* tizen specific */
+    return sdb_main(DEFAULT_SDB_PORT);
+}
diff --git a/src/sdbd_service.c b/src/sdbd_service.c
new file mode 100644 (file)
index 0000000..437ecf9
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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_SDB
+#define LOG_TAG "SDBD_TRACE_SDB"
+#include "log.h"
+#include "sdb.h"
+#include "file_sync_service.h"
+
+extern void sdb_main_service();
+
+int main(int argc, char* argv[]) {
+    sdb_main_service();
+    int fd=atoi(argv[1]);
+    D("SERVICE In child process fd=%d",fd);
+    const char* cookie = argv[2];
+    D("SERVICE In child process cookie=%s",cookie);
+    rootshell_mode = atoi(argv[3]);
+    D("SERVICE In child process rootshell_mode=%d",rootshell_mode);
+    file_sync_service(fd, (void*)cookie);
+    D("SERVICE after file sync");
+    exit(0);
+}
index 6beb905..bba01a0 100755 (executable)
@@ -855,18 +855,46 @@ static int create_sync_subprocess(void (*func)(int, void *), void* cookie) {
     sdb_thread_t t;
     int s[2];
 
+#ifdef EXEC_AFTER_FORK
+    if(sdb_socketpair_without_execclose(s)) {
+        E("cannot create service socket pair\n");
+        return -1;
+    }
+#else
     if(sdb_socketpair(s)) {
         E("cannot create service socket pair\n");
         return -1;
     }
+#endif
 
+    D("SERVICE create_sync_subprocess cookie=%s\n",(char*)cookie);
     pid_t pid = fork();
 
     if (pid == 0) {
         sdb_close(s[0]);
+#ifdef EXEC_AFTER_FORK
+        // exec the new process to do file sync
+        char *fname="/usr/sbin/sdbd-service";
+        char *arg[5];
+        char targ[10];
+        char smarg[10];
+        sprintf(targ,"%d",s[1]);
+        sprintf(smarg, "%d",rootshell_mode);
+        D("SERVICE fd to be used by child process %d",s[1]);
+        D("SERVICE rootmode to be used by child process %s",smarg);
+        arg[0]=fname;
+        arg[1]=targ;
+        arg[2]= strdup((char*)cookie);
+        D("SERVICE cookie to be used by child process %s",arg[2]);
+        arg[3]=smarg;
+        arg[4]=NULL;
+        execvp(fname,arg);
+#else
         func(s[1], cookie);
         exit(-1);
+#endif
     } else if (pid > 0) {
+        D("SERVICE fd passed by parent process %d",s[1]);
         sdb_close(s[1]);
         // FIXME: do not wait child process hear
         //waitpid(pid, &ret, 0);
index 4e1bf1e..d7840fb 100644 (file)
@@ -498,6 +498,17 @@ static __inline__ int  sdb_socketpair( int  sv[2] )
     return 0;
 }
 
+static __inline__ int  sdb_socketpair_without_execclose( int  sv[2] )
+{
+    int  rc;
+
+    rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
+    if (rc < 0) {
+        return -1;
+    }
+    return 0;
+}
+
 #undef   socketpair
 #define  socketpair   ___xxx_socketpair