sensord: change access permissions about all files
[platform/core/system/sensord.git] / src / server / server.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <systemd/sd-daemon.h>
21 #include <server.h>
22 #include <sensor_plugin_loader.h>
23 #include <command_worker.h>
24 #include <thread>
25 #include <sys/epoll.h>
26 #include <sensor_event_poller.h>
27
28 using std::thread;
29
30 server::server()
31 : m_mainloop(NULL)
32 {
33
34 }
35
36 server::~server()
37 {
38         stop();
39 }
40
41 int server::get_systemd_socket(const char *name)
42 {
43         int type = SOCK_STREAM;
44         const int listening = 1;
45         size_t length = 0;
46         int fd = -1;
47
48         if (sd_listen_fds(1) != 1)
49                 return -1;
50
51         fd = SD_LISTEN_FDS_START + 0;
52
53         if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
54                 return fd;
55
56         return -1;
57 }
58
59 void server::accept_client(void)
60 {
61         command_worker *cmd_worker;
62
63         INFO("Client acceptor is started");
64
65         while (true) {
66                 csocket client_command_socket;
67
68                 if (!m_client_accep_socket.accept(client_command_socket)) {
69                         ERR("Failed to accept connection request from a client");
70                         continue;
71                 }
72
73                 DBG("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
74
75                 cmd_worker = new(std::nothrow) command_worker(client_command_socket);
76
77                 if (!cmd_worker) {
78                         ERR("Failed to allocate memory");
79                         continue;
80                 }
81
82                 if(!cmd_worker->start())
83                         delete cmd_worker;
84         }
85 }
86
87 void server::poll_event(void)
88 {
89         INFO("Event poller is started");
90
91         sensor_event_poller poller;
92
93         if (!poller.poll()) {
94                 ERR("Failed to poll event");
95                 return;
96         }
97 }
98
99 void server::run(void)
100 {
101         int sock_fd = -1;
102         const int MAX_PENDING_CONNECTION = 5;
103
104         m_mainloop = g_main_loop_new(NULL, false);
105
106         sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
107
108         if (sock_fd >= 0) {
109                 INFO("Succeeded to get systemd socket(%d)", sock_fd);
110                 m_client_accep_socket = csocket(sock_fd);
111         } else {
112                 ERR("Failed to get systemd socket, create it by myself!");
113                 if (!m_client_accep_socket.create(SOCK_STREAM)) {
114                         ERR("Failed to create command channel");
115                         return;
116                 }
117
118                 if(!m_client_accep_socket.bind(COMMAND_CHANNEL_PATH)) {
119                         ERR("Failed to bind command channel");
120                         m_client_accep_socket.close();
121                         return;
122                 }
123
124                 if(!m_client_accep_socket.listen(MAX_PENDING_CONNECTION)) {
125                         ERR("Failed to listen command channel");
126                         return;
127                 }
128         }
129
130         sensor_event_dispatcher::get_instance().run();
131
132         thread client_accepter(&server::accept_client, this);
133         client_accepter.detach();
134
135         thread event_poller(&server::poll_event, this);
136         event_poller.detach();
137
138         sd_notify(0, "READY=1");
139
140         g_main_loop_run(m_mainloop);
141         g_main_loop_unref(m_mainloop);
142
143         return;
144 }
145
146 void server::stop(void)
147 {
148         if(m_mainloop)
149                 g_main_loop_quit(m_mainloop);
150
151         m_client_accep_socket.close();
152 }
153
154 server& server::get_instance()
155 {
156         static server inst;
157         return inst;
158 }