sensord: change to systemd socket 59/59959/2
authorkibak.yoon <kibak.yoon@samsung.com>
Sat, 20 Feb 2016 09:54:42 +0000 (18:54 +0900)
committerkibak.yoon <kibak.yoon@samsung.com>
Sat, 20 Feb 2016 11:03:14 +0000 (20:03 +0900)
- use systemd socket for event socket

Change-Id: Ib18c94092aa9cd93b097ee270a40220d17d7e0ab
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
packaging/sensord.spec
src/server/sensor_event_dispatcher.cpp
src/server/sensor_event_dispatcher.h
src/server/server.cpp
src/server/server.h
src/shared/csocket.cpp
systemd/CMakeLists.txt
systemd/sensord.service
systemd/sensord_command.socket [moved from systemd/sensord.socket with 80% similarity]
systemd/sensord_event.socket [new file with mode: 0644]

index 92c5c11..7b93195 100644 (file)
@@ -90,7 +90,8 @@ rm -rf %{buildroot}
 %make_install
 
 %install_service multi-user.target.wants sensord.service
-%install_service sockets.target.wants sensord.socket
+%install_service sockets.target.wants sensord_event.socket
+%install_service sockets.target.wants sensord_command.socket
 
 %post
 systemctl daemon-reload
@@ -110,9 +111,11 @@ ln -sf %{_libdir}/libsensor.so.%{version} %{_libdir}/libsensor.so.1
 %manifest sensord.manifest
 %{_bindir}/sensord
 %{_unitdir}/sensord.service
-%{_unitdir}/sensord.socket
+%{_unitdir}/sensord_command.socket
+%{_unitdir}/sensord_event.socket
 %{_unitdir}/multi-user.target.wants/sensord.service
-%{_unitdir}/sockets.target.wants/sensord.socket
+%{_unitdir}/sockets.target.wants/sensord_command.socket
+%{_unitdir}/sockets.target.wants/sensord_event.socket
 %license LICENSE.APLv2
 
 %files -n libsensord
index 6333fb1..d2deaa2 100644 (file)
@@ -42,30 +42,8 @@ sensor_event_dispatcher& sensor_event_dispatcher::get_instance()
        return inst;
 }
 
-
 bool sensor_event_dispatcher::run(void)
 {
-       _I("Starting Event Dispatcher\n");
-
-       if (!m_accept_socket.create(SOCK_SEQPACKET)) {
-               _E("Listener Socket Creation failed in Server \n");
-               return false;
-       }
-
-       if(!m_accept_socket.bind(EVENT_CHANNEL_PATH)) {
-               _E("Listener Socket Binding failed in Server \n");
-               m_accept_socket.close();
-               return false;
-       }
-
-       if(!m_accept_socket.listen(MAX_PENDING_CONNECTION)) {
-               _E("Socket Listen failed in Server \n");
-               return false;
-       }
-
-       thread accepter(&sensor_event_dispatcher::accept_connections, this);
-       accepter.detach();
-
        thread dispatcher(&sensor_event_dispatcher::dispatch_event, this);
        dispatcher.detach();
 
@@ -87,8 +65,6 @@ void sensor_event_dispatcher::accept_event_channel(csocket client_socket)
 
        client_socket.set_transfer_mode();
 
-       AUTOLOCK(m_mutex);
-
        if(!get_client_info_manager().set_event_socket(client_id, client_socket)) {
                _E("Failed to store event socket[%d] for %s", client_socket.get_socket_fd(),
                        client_info_manager.get_client_info(client_id));
@@ -108,23 +84,10 @@ void sensor_event_dispatcher::accept_event_channel(csocket client_socket)
        }
 }
 
-void sensor_event_dispatcher::accept_connections(void)
+void sensor_event_dispatcher::accept_event_connections(csocket client_socket)
 {
-       _I("Event channel acceptor is started.\n");
-
-       while (true) {
-               csocket client_socket;
-
-               if (!m_accept_socket.accept(client_socket)) {
-                       _E("Accepting socket failed in Server \n");
-                       continue;
-               }
-
-               _I("New client connected (socket_fd : %d)\n", client_socket.get_socket_fd());
-
-               thread event_channel_creator(&sensor_event_dispatcher::accept_event_channel, this, client_socket);
-               event_channel_creator.detach();
-       }
+       thread event_channel_creator(&sensor_event_dispatcher::accept_event_channel, this, client_socket);
+       event_channel_creator.detach();
 }
 
 void sensor_event_dispatcher::dispatch_event(void)
index befa90e..bc23f5c 100644 (file)
@@ -47,7 +47,6 @@ private:
        sensor_event_dispatcher(sensor_event_dispatcher const&) {};
        sensor_event_dispatcher& operator=(sensor_event_dispatcher const&);
 
-       void accept_connections(void);
        void accept_event_channel(csocket client_socket);
 
        void dispatch_event(void);
@@ -65,7 +64,10 @@ private:
        void sort_sensor_events(std::vector<void *> &events);
 public:
        static sensor_event_dispatcher& get_instance();
+
        bool run(void);
+       void accept_event_connections(csocket client_socket);
+
        void request_last_event(int client_id, sensor_id_t sensor_id);
 
        bool add_active_virtual_sensor(virtual_sensor *sensor);
index 39900bc..52671ce 100644 (file)
@@ -26,6 +26,8 @@
 #include <sys/epoll.h>
 #include <sensor_event_poller.h>
 
+#define SYSTEMD_SOCKET_MAX 2
+
 using std::thread;
 
 server::server()
@@ -42,32 +44,43 @@ server::~server()
 int server::get_systemd_socket(const char *name)
 {
        int type = SOCK_STREAM;
-       const int listening = 1;
+       int listening = 1;
        size_t length = 0;
        int fd = -1;
+       int fd_env = 1;
+       int fd_index;
+
+       if (!strcmp(name, EVENT_CHANNEL_PATH)) {
+               type = SOCK_SEQPACKET;
+               listening = -1;
+               fd_env = 0;
+       }
 
-       if (sd_listen_fds(1) != 1)
+       if (sd_listen_fds(fd_env) < 0) {
+               ERR("Failed to listen fds from systemd");
                return -1;
+       }
 
-       fd = SD_LISTEN_FDS_START + 0;
+       for (fd_index = 0; fd_index < SYSTEMD_SOCKET_MAX; ++fd_index) {
+               fd = SD_LISTEN_FDS_START + fd_index;
 
-       if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
-               return fd;
+               if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
+                       return fd;
+       }
 
        return -1;
 }
 
-void server::accept_client(void)
+void server::accept_command_channel(void)
 {
        command_worker *cmd_worker;
-
-       _I("Client acceptor is started");
+       _I("Command channel acceptor is started");
 
        while (true) {
                csocket client_command_socket;
 
-               if (!m_client_accep_socket.accept(client_command_socket)) {
-                       _E("Failed to accept connection request from a client");
+               if (!m_command_channel_accept_socket.accept(client_command_socket)) {
+                       _E("Failed to accept command channel from a client");
                        continue;
                }
 
@@ -80,11 +93,29 @@ void server::accept_client(void)
                        continue;
                }
 
-               if(!cmd_worker->start())
+               if (!cmd_worker->start())
                        delete cmd_worker;
        }
 }
 
+void server::accept_event_channel(void)
+{
+       _I("Event channel acceptor is started");
+
+       while (true) {
+               csocket client_event_socket;
+
+               if (!m_event_channel_accept_socket.accept(client_event_socket)) {
+                       _E("Failed to accept event channel from a client");
+                       continue;
+               }
+
+               _D("New client(socket_fd : %d) connected", client_event_socket.get_socket_fd());
+
+               sensor_event_dispatcher::get_instance().accept_event_connections(client_event_socket);
+       }
+}
+
 void server::poll_event(void)
 {
        _I("Event poller is started");
@@ -99,39 +130,18 @@ void server::poll_event(void)
 
 void server::run(void)
 {
-       int sock_fd = -1;
-       const int MAX_PENDING_CONNECTION = 5;
-
        m_mainloop = g_main_loop_new(NULL, false);
 
-       sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
-
-       if (sock_fd >= 0) {
-               _I("Succeeded to get systemd socket(%d)", sock_fd);
-               m_client_accep_socket = csocket(sock_fd);
-       } else {
-               _E("Failed to get systemd socket, create it by myself!");
-               if (!m_client_accep_socket.create(SOCK_STREAM)) {
-                       _E("Failed to create command channel");
-                       return;
-               }
-
-               if(!m_client_accep_socket.bind(COMMAND_CHANNEL_PATH)) {
-                       _E("Failed to bind command channel");
-                       m_client_accep_socket.close();
-                       return;
-               }
+       sensor_event_dispatcher::get_instance().run();
 
-               if(!m_client_accep_socket.listen(MAX_PENDING_CONNECTION)) {
-                       _E("Failed to listen command channel");
-                       return;
-               }
-       }
+       listen_command_channel();
+       listen_event_channel();
 
-       sensor_event_dispatcher::get_instance().run();
+       thread event_channel_accepter(&server::accept_event_channel, this);
+       event_channel_accepter.detach();
 
-       thread client_accepter(&server::accept_client, this);
-       client_accepter.detach();
+       thread command_channel_accepter(&server::accept_command_channel, this);
+       command_channel_accepter.detach();
 
        thread event_poller(&server::poll_event, this);
        event_poller.detach();
@@ -144,12 +154,81 @@ void server::run(void)
        return;
 }
 
+bool server::listen_command_channel(void)
+{
+       int sock_fd = -1;
+       const int MAX_PENDING_CONNECTION = 10;
+
+       sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
+
+       if (sock_fd >= 0) {
+               INFO("Succeeded to get systemd socket(%d)", sock_fd);
+               m_command_channel_accept_socket = csocket(sock_fd);
+               return true;
+       }
+
+       INFO("Failed to get systemd socket, create it by myself!");
+       if (!m_command_channel_accept_socket.create(SOCK_STREAM)) {
+               ERR("Failed to create command channel");
+               return false;
+       }
+
+       if (!m_command_channel_accept_socket.bind(COMMAND_CHANNEL_PATH)) {
+               ERR("Failed to bind command channel");
+               m_command_channel_accept_socket.close();
+               return false;
+       }
+
+       if (!m_command_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
+               ERR("Failed to listen command channel");
+               return false;
+       }
+
+       return true;
+}
+
+bool server::listen_event_channel(void)
+{
+       int sock_fd = -1;
+       const int MAX_PENDING_CONNECTION = 32;
+
+       sock_fd = get_systemd_socket(EVENT_CHANNEL_PATH);
+
+       if (sock_fd >= 0) {
+               INFO("Succeeded to get systemd socket(%d)", sock_fd);
+               m_event_channel_accept_socket = csocket(sock_fd);
+               return true;
+       }
+
+       INFO("Failed to get systemd socket, create it by myself!");
+
+       if (!m_event_channel_accept_socket.create(SOCK_SEQPACKET)) {
+               ERR("Failed to create event channel");
+               return false;
+       }
+
+       if (!m_event_channel_accept_socket.bind(EVENT_CHANNEL_PATH)) {
+               ERR("Failed to bind event channel");
+               m_event_channel_accept_socket.close();
+               return false;
+       }
+
+       if (!m_event_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
+               ERR("Failed to listen event channel");
+               m_event_channel_accept_socket.close();
+               return false;
+       }
+
+       return true;
+}
+
 void server::stop(void)
 {
-       if(m_mainloop)
+       if (m_mainloop)
                g_main_loop_quit(m_mainloop);
 
-       m_client_accep_socket.close();
+       m_command_channel_accept_socket.close();
+       m_event_channel_accept_socket.close();
 }
 
 server& server::get_instance()
index 53549f6..8c69a7a 100644 (file)
 class server {
 private:
        GMainLoop *m_mainloop;
-       csocket m_client_accep_socket;
+       csocket m_command_channel_accept_socket;
+       csocket m_event_channel_accept_socket;
 
        server();
        ~server();
 
        void poll_event(void);
        void accept_client(void);
+
+       bool listen_command_channel(void);
+       bool listen_event_channel(void);
+
+       void accept_command_channel(void);
+       void accept_event_channel(void);
+
        int get_systemd_socket(const char *name);
 public:
        void run(void);
index a58b22a..b31cb0e 100644 (file)
@@ -88,22 +88,6 @@ bool csocket::bind (const char *sock_path)
                return false;
        }
 
-       if((fsetxattr(m_sock_fd, "security.SMACK64IPOUT", "@", 2, 0)) < 0) {
-               if(errno != EOPNOTSUPP) {
-                       close();
-                       _E("security.SMACK64IPOUT error = [%d][%s]\n", errno, strerror(errno) );
-                       return false;
-               }
-       }
-
-       if((fsetxattr(m_sock_fd, "security.SMACK64IPIN", "*", 2, 0)) < 0) {
-               if(errno != EOPNOTSUPP) {
-                       close();
-                       _E("security.SMACK64IPIN error  = [%d][%s]\n", errno, strerror(errno) );
-                       return false;
-               }
-       }
-
        if (!access(sock_path, F_OK)) {
                unlink(sock_path);
        }
index 233e8ba..64fbd69 100644 (file)
@@ -1,5 +1,6 @@
 INSTALL(FILES
        ${CMAKE_CURRENT_SOURCE_DIR}/sensord.service
-       ${CMAKE_CURRENT_SOURCE_DIR}/sensord.socket
+       ${CMAKE_CURRENT_SOURCE_DIR}/sensord_command.socket
+       ${CMAKE_CURRENT_SOURCE_DIR}/sensord_event.socket
        DESTINATION lib/systemd/system)
 
index d3ab961..26821a8 100644 (file)
@@ -6,7 +6,9 @@ Type=notify
 ExecStart=/usr/bin/sensord
 Restart=always
 RestartSec=0
-MemoryLimit=10M
+MemoryLimit=20M
+Sockets=sensord_command.socket
+Sockets=sensord_event.socket
 
 [Install]
 WantedBy=multi-user.target
similarity index 80%
rename from systemd/sensord.socket
rename to systemd/sensord_command.socket
index b8bd460..4af2f60 100644 (file)
@@ -1,5 +1,5 @@
 [Unit]
-Description=Sensor daemon socket
+Description=Sensor command socket
 
 [Socket]
 ListenStream=/tmp/sensord_command_socket
diff --git a/systemd/sensord_event.socket b/systemd/sensord_event.socket
new file mode 100644 (file)
index 0000000..b6641a1
--- /dev/null
@@ -0,0 +1,11 @@
+[Unit]
+Description=Sensor event socket
+
+[Socket]
+ListenSequentialPacket=/tmp/sensord_event_socket
+SocketMode=0777
+PassCredentials=yes
+Accept=false
+SmackLabelIPIn=*
+SmackLabelIPOut=@
+Service=sensord.service