2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Rafal Krypa <r.krypa@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * @file client-common.cpp
20 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
22 * @brief This file is implementation of client-common functions.
27 #include <sys/types.h>
28 #include <sys/socket.h>
32 #include <dpl/log/log.h>
33 #include <dpl/serialization.h>
34 #include <dpl/singleton.h>
35 #include <dpl/singleton_safe_impl.h>
37 #include <message-buffer.h>
39 #include <security-manager.h>
41 IMPLEMENT_SAFE_SINGLETON(SecurityManager::Log::LogSystem);
45 const int POLL_TIMEOUT = 2000;
47 void securityClientEnableLogSystem(void) {
48 SecurityManager::Singleton<SecurityManager::Log::LogSystem>::Instance().SetTag("SECURITY_MANAGER_CLIENT");
51 int waitForSocket(int sock, int event, int timeout) {
55 desc[0].events = event;
57 while((-1 == (retval = poll(desc, 1, timeout))) && (errno == EINTR)) {
63 LogDebug("Poll timeout");
64 } else if (-1 == retval) {
66 LogError("Error in poll: " << strerror(err));
82 int Connect(char const * const interface) {
83 sockaddr_un clientAddr;
86 if (m_sock != -1) // guard
89 m_sock = socket(AF_UNIX, SOCK_STREAM, 0);
92 LogError("Error creating socket: " << strerror(err));
93 return SECURITY_MANAGER_API_ERROR_SOCKET;
96 if ((flags = fcntl(m_sock, F_GETFL, 0)) < 0 ||
97 fcntl(m_sock, F_SETFL, flags | O_NONBLOCK) < 0)
100 LogError("Error in fcntl: " << strerror(err));
101 return SECURITY_MANAGER_API_ERROR_SOCKET;
104 memset(&clientAddr, 0, sizeof(clientAddr));
106 clientAddr.sun_family = AF_UNIX;
108 if (strlen(interface) >= sizeof(clientAddr.sun_path)) {
109 LogError("Error: interface name " << interface << "is too long. Max len is:" << sizeof(clientAddr.sun_path));
110 return SECURITY_MANAGER_API_ERROR_NO_SUCH_SERVICE;
113 strcpy(clientAddr.sun_path, interface);
115 LogDebug("ClientAddr.sun_path = " << interface);
117 int retval = TEMP_FAILURE_RETRY(connect(m_sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
118 if ((retval == -1) && (errno == EINPROGRESS)) {
119 if (0 >= waitForSocket(m_sock, POLLIN, POLL_TIMEOUT)) {
120 LogError("Error in waitForSocket.");
121 return SECURITY_MANAGER_API_ERROR_SOCKET;
124 socklen_t len = sizeof(error);
125 retval = getsockopt(m_sock, SOL_SOCKET, SO_ERROR, &error, &len);
129 LogError("Error in getsockopt: " << strerror(err));
130 return SECURITY_MANAGER_API_ERROR_SOCKET;
133 if (error == EACCES) {
134 LogError("Access denied");
135 return SECURITY_MANAGER_API_ERROR_ACCESS_DENIED;
139 LogError("Error in connect: " << strerror(error));
140 return SECURITY_MANAGER_API_ERROR_SOCKET;
143 return SECURITY_MANAGER_API_SUCCESS;
148 LogError("Error connecting socket: " << strerror(err));
150 return SECURITY_MANAGER_API_ERROR_ACCESS_DENIED;
152 return SECURITY_MANAGER_API_ERROR_NO_SUCH_SERVICE;
153 return SECURITY_MANAGER_API_ERROR_SOCKET;
156 return SECURITY_MANAGER_API_SUCCESS;
167 } // namespace anonymous
169 namespace SecurityManager {
172 int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv) {
178 if (SECURITY_MANAGER_API_SUCCESS != (ret = sock.Connect(interface))) {
179 LogError("Error in SockRAII");
183 while ((send.size() - done) > 0) {
184 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
185 LogError("Error in poll(POLLOUT)");
186 return SECURITY_MANAGER_API_ERROR_SOCKET;
188 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
191 LogError("Error in write: " << strerror(err));
192 return SECURITY_MANAGER_API_ERROR_SOCKET;
198 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
199 LogError("Error in poll(POLLIN)");
200 return SECURITY_MANAGER_API_ERROR_SOCKET;
202 ssize_t temp = TEMP_FAILURE_RETRY(read(sock.Get(), buffer, 2048));
205 LogError("Error in read: " << strerror(err));
206 return SECURITY_MANAGER_API_ERROR_SOCKET;
210 LogError("Read return 0/Connection closed by server(?)");
211 return SECURITY_MANAGER_API_ERROR_SOCKET;
214 RawBuffer raw(buffer, buffer+temp);
216 } while(!recv.Ready());
217 return SECURITY_MANAGER_API_SUCCESS;
220 int sendToServerAncData(char const * const interface, const RawBuffer &send, struct msghdr &hdr) {
225 if (SECURITY_MANAGER_API_SUCCESS != (ret = sock.Connect(interface))) {
226 LogError("Error in SockRAII");
230 while ((send.size() - done) > 0) {
231 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
232 LogError("Error in poll(POLLOUT)");
233 return SECURITY_MANAGER_API_ERROR_SOCKET;
235 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
238 LogError("Error in write: " << strerror(err));
239 return SECURITY_MANAGER_API_ERROR_SOCKET;
244 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
245 LogError("Error in poll(POLLIN)");
246 return SECURITY_MANAGER_API_ERROR_SOCKET;
249 ssize_t temp = TEMP_FAILURE_RETRY(recvmsg(sock.Get(), &hdr, MSG_CMSG_CLOEXEC));
253 LogError("Error in recvmsg(): " << strerror(err) << " errno: " << err);
254 return SECURITY_MANAGER_API_ERROR_SOCKET;
258 LogError("Read return 0/Connection closed by server(?)");
259 return SECURITY_MANAGER_API_ERROR_SOCKET;
262 return SECURITY_MANAGER_API_SUCCESS;
265 int try_catch(const std::function<int()>& func)
269 } catch (MessageBuffer::Exception::Base &e) {
270 LogError("SecurityManager::MessageBuffer::Exception " << e.DumpToString());
271 } catch (std::exception &e) {
272 LogError("STD exception " << e.what());
274 LogError("Unknown exception occured");
276 return SECURITY_MANAGER_API_ERROR_UNKNOWN;
279 } // namespace SecurityMANAGER
281 static void init_lib(void) __attribute__ ((constructor));
282 static void init_lib(void)
284 securityClientEnableLogSystem();
287 static void fini_lib(void) __attribute__ ((destructor));
288 static void fini_lib(void)