Added On-demand launch feature using the socket_activation 52/163552/3
authorWonkeun Oh <wonkeun.oh@samsung.com>
Mon, 10 Jul 2017 07:22:16 +0000 (16:22 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Tue, 12 Dec 2017 05:57:37 +0000 (14:57 +0900)
Change-Id: I0961165568f1d75131ce918af30cde6f7ec7088b

configure.ac
ism/extras/wayland_immodule/wayland_imcontext.c
ism/src/Makefile.am
ism/src/scim_socket.cpp
ism/src/scim_socket.h
packaging/isf.spec

index 515406e..58313fa 100644 (file)
@@ -368,6 +368,11 @@ AC_ARG_ENABLE(include-libwebsocket,
               enable_libwebsocket=no,
               enable_libwebsocket=yes)
 
+# Check systemd library
+PKG_CHECK_MODULES(SYSTEMD, [libsystemd],
+                  [ISF_HAS_SYSTEMD=yes],
+                  [ISF_HAS_SYSTEMD=no])
+
 if test "$ISF_HAS_VCONF" = "yes"; then
   AC_DEFINE(HAVE_VCONF,1,[Have VConf functions.])
 fi
index 5babf4a..95289cb 100644 (file)
 #ifdef HAVE_VCONF
 #include <vconf.h>
 #endif
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/time.h>
+#include <fcntl.h>
+
 #include "isf_debug.h"
 #include "wayland_imcontext.h"
 #include "tizen_profile.h"
@@ -56,6 +64,7 @@
 
 #define VCONFKEY_AUTOPERIOD_ALLOW_BOOL  "file/private/isf/autoperiod_allow"
 #define VCONFKEY_AUTOCAPITAL_ALLOW_BOOL "file/private/isf/autocapital_allow"
+#define SOCK_PATH "/run/.isf/scim-panel-socket"
 
 typedef enum {
     INPUT_LANG_URDU,
@@ -2506,6 +2515,56 @@ keyboard_mode_changed_cb (keynode_t *key, void* data)
 }
 #endif
 
+#ifdef SOCKET_ACTIVATION
+static int activate_socket () {
+    int s;
+    struct sockaddr_un svr;
+    int len;
+    int r;
+    int cnt = 5;
+    int flag;
+
+    LOGD("socket_activate start");
+    s = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (s == -1) {
+        LOGW("socket error");
+        return -1;
+    }
+
+    flag = fcntl(s, F_GETFL, NULL);
+    flag |= O_NONBLOCK;
+    fcntl(s, F_SETFL, flag);
+
+    svr.sun_family = AF_UNIX;
+    strcpy(svr.sun_path, SOCK_PATH);
+    len = sizeof(svr);
+
+    r = connect(s, (struct sockaddr *)&svr, len);
+    if (r == -1) {
+        LOGD("connect error");
+        close(s);
+        return -1;
+    }
+
+    while (cnt > 0) {
+        struct timeval s1;
+
+        gettimeofday(&s1, NULL);
+        LOGD("%d %06d\n", (int)s1.tv_sec, (int)s1.tv_usec);
+
+        r = send(s, (const void *)&s1, sizeof(s1), 0);
+        if (r == -1) {
+            LOGW("send error");
+            break;
+        }
+        cnt--;
+    }
+
+    close(s);
+    return 0;
+}
+#endif
+
 void wayland_im_initialize ()
 {
     register_key_handler ();
@@ -2706,6 +2765,10 @@ wayland_im_context_focus_in(Ecore_IMF_Context *ctx)
           ecore_imf_context_input_panel_enabled_get(ctx),
           ecore_imf_context_input_panel_show_on_demand_get (ctx));
 
+#ifdef SOCKET_ACTIVATION
+    activate_socket ();
+#endif
+
     if (!set_focus(ctx)) {
         LOGW("ctx : %p. Fail to set focus!", ctx);
         return;
index 3356d47..c1d7afd 100644 (file)
@@ -179,7 +179,8 @@ libscim@SCIM_EPOCH@_la_CXXFLAGS = @EVAS_CFLAGS@ \
                          @TZPLATFORM_CONFIG_CFLAGS@ \
                          @PKGMGR_INFO_CFLAGS@ \
                          @VCONF_CFLAGS@ \
-                         @GLIB_CFLAGS@
+                         @GLIB_CFLAGS@ \
+                         @SYSTEMD_CFLAGS@
 
 libscim@SCIM_EPOCH@_la_LDFLAGS  = -version-info $(SCIM_CURRENT):$(SCIM_REVISION):$(SCIM_AGE) \
                          -export-dynamic \
@@ -196,6 +197,7 @@ libscim@SCIM_EPOCH@_la_LDFLAGS  = -version-info $(SCIM_CURRENT):$(SCIM_REVISION)
                          @PKGMGR_INFO_LIBS@ \
                          @VCONF_LIBS@ \
                          @GLIB_LIBS@ \
+                         @SYSTEMD_LIBS@ \
                          -lstdc++
 
 libscim@SCIM_EPOCH@_la_LIBADD  = libltdlc.la libprofile.la
index 3abae3a..8b12e2d 100644 (file)
@@ -60,6 +60,7 @@
 
 #include "scim_private.h"
 #include "scim.h"
+#include <systemd/sd-daemon.h>
 
 #ifdef LOG_TAG
 # undef LOG_TAG
@@ -741,6 +742,34 @@ public:
         return ret >= 0;
     }
 
+    bool sd_socket (SocketFamily family, const SocketAddress &addr) {
+        int ret, n;
+        char pid[10];
+
+        snprintf(pid, sizeof(pid), "%d", getpid());
+        setenv("LISTEN_PID", pid, 1);
+        n = sd_listen_fds (0);
+        if (n > 1) {
+            SCIM_DEBUG_SOCKET (1) << "Socket: Too many file descriptors received.\n";
+            return false;
+        } else if (n == 1) {
+            m_no_close = false;
+            m_binded = true;
+            m_err = 0;
+            m_family = family;
+            m_address = addr;
+            ret = SD_LISTEN_FDS_START + 0;
+            int flag = fcntl (ret, F_GETFD, 0);
+            fcntl (ret, F_SETFD, flag|FD_CLOEXEC);
+            m_id = ret;
+        } else {
+            LOGW("No sd socket!!");
+            SCIM_DEBUG_SOCKET (1) << "%d returned.\n" << n;
+            return false;
+        }
+        return true;
+    }
+
     void close () {
         if (m_id < 0) return;
 
@@ -923,6 +952,12 @@ Socket::create (SocketFamily family)
     return m_impl->create (family);
 }
 
+bool
+Socket::sd_socket (SocketFamily family, const SocketAddress &addr)
+{
+    return m_impl->sd_socket (family, addr);
+}
+
 void
 Socket::close ()
 {
@@ -994,6 +1029,15 @@ SocketServer::create (const SocketAddress &address)
                 FD_SET (m_impl->max_fd, &(m_impl->active_fds));
                 m_impl->err = 0;
                 return true;
+#ifdef SOCKET_ACTIVATION
+            } else if (Socket::sd_socket (family, address)) {
+                m_impl->created = true;
+                m_impl->max_fd = Socket::get_id ();
+                FD_ZERO (&(m_impl->active_fds));
+                FD_SET (m_impl->max_fd, &(m_impl->active_fds));
+                m_impl->err = 0;
+                return true;
+#endif
             }
             m_impl->err = Socket::get_error_number ();
             Socket::close ();
index 3cb9250..171cbd2 100644 (file)
@@ -328,6 +328,11 @@ protected:
     bool create (SocketFamily family);
 
     /**
+     * @brief Create an activation socket.
+     */
+    bool sd_socket (SocketFamily family, const SocketAddress &addr);
+
+    /**
      * @brief Close the socket.
      */
     void close ();
index e71defc..94ff73a 100644 (file)
@@ -44,6 +44,7 @@ BuildRequires:  pkgconfig(cynara-creds-socket)
 BuildRequires:  pkgconfig(cynara-session)
 BuildRequires:  pkgconfig(capi-appfw-package-manager)
 BuildRequires:  pkgconfig(capi-system-info)
+BuildRequires:  pkgconfig(libsystemd)
 BuildRequires:  pkgconfig(notification)
 Requires(postun): /sbin/ldconfig
 Requires:      org.tizen.isf-kbd-mode-changer