socket connection return value chack added to avoid crash
[platform/core/system/sensord.git] / src / shared / ipc_client.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 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 "ipc_client.h"
21
22 #include "sensor_log.h"
23 #include "stream_socket.h"
24 #include "event_handler.h"
25 #include "channel_event_handler.h"
26
27 using namespace ipc;
28
29 ipc_client::ipc_client(const std::string &path)
30 {
31         m_path = path;
32 }
33
34 ipc_client::~ipc_client()
35 {
36 }
37
38 bool ipc_client::set_option(int option, int value)
39 {
40         return true;
41 }
42
43 bool ipc_client::set_option(const std::string &option, int value)
44 {
45         return true;
46 }
47
48 channel *ipc_client::connect(channel_handler *handler)
49 {
50         return connect(handler, NULL);
51 }
52
53 channel *ipc_client::connect(channel_handler *handler, event_loop *loop, bool bind)
54 {
55         socket *sock = NULL;
56         channel *ch = NULL;
57         channel_event_handler *ev_handler = NULL;
58         bool ret = false;
59         retvm_if(access(m_path.c_str(), F_OK), NULL,
60                         "Failed to access to %s", m_path.c_str());
61
62         sock = new(std::nothrow) stream_socket();
63         retvm_if(!sock, NULL, "Failed to allocate memory");
64
65         if (!sock->create(m_path)) {
66                 delete sock;
67                 return NULL;
68         }
69
70         ch = new(std::nothrow) channel(sock);
71         if (!ch) {
72                 delete sock;
73                 _E("Failed to allocate memory");
74                 return NULL;
75         }
76
77         ev_handler = new(std::nothrow) channel_event_handler(ch, handler);
78         if (!ev_handler) {
79                 delete ch;
80                 delete sock;
81                 _E("Faield to allocate memory");
82                 return NULL;
83         }
84
85         ret = ch->connect(ev_handler, loop);
86         if(ret == false) {
87                 delete ch;
88                 delete sock;
89                 delete ev_handler;
90                 _E("Faield to connect");
91                 return NULL;
92         }
93
94         if (loop && bind) {
95                 uint64_t id = loop->add_event(sock->get_fd(),
96                                 (EVENT_IN | EVENT_HUP | EVENT_NVAL), ev_handler);
97                 ch->set_event_id(id);
98         }
99
100         _I("Connected");
101         return ch;
102 }