2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file client-common.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19 * @author Zofia Abramowska (z.abramowska@samsung.com)
21 * @brief This file is implementation of client-common functions.
26 #include <sys/types.h>
27 #include <sys/socket.h>
31 #include <dpl/log/log.h>
32 #include <dpl/serialization.h>
33 #include <dpl/singleton.h>
34 #include <dpl/singleton_safe_impl.h>
36 #include <message-buffer.h>
38 #include <ckm/ckm-error.h>
40 #include <client-common.h>
42 IMPLEMENT_SAFE_SINGLETON(CKM::Log::LogSystem);
46 const int POLL_TIMEOUT = 8000;
48 void centKeyClientEnableLogSystem(void) {
49 CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM_CLIENT");
52 int waitForSocket(int sock, int event, int timeout) {
56 desc[0].events = event;
58 while((-1 == (retval = poll(desc, 1, timeout))) && (errno == EINTR)) {
64 LogDebug("Poll timeout");
65 } else if (-1 == retval) {
67 LogError("Error in poll: " << strerror(err));
72 } // namespace anonymous
77 int connectSocket(int& sock, char const * const interface) {
78 sockaddr_un clientAddr;
81 if (sock != -1) // guard
84 sock = socket(AF_UNIX, SOCK_STREAM, 0);
87 LogError("Error creating socket: " << strerror(err));
88 return CKM_API_ERROR_SOCKET;
91 if ((flags = fcntl(sock, F_GETFL, 0)) < 0 ||
92 fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
95 LogError("Error in fcntl: " << strerror(err));
96 return CKM_API_ERROR_SOCKET;
99 memset(&clientAddr, 0, sizeof(clientAddr));
101 clientAddr.sun_family = AF_UNIX;
103 if (strlen(interface) >= sizeof(clientAddr.sun_path)) {
104 LogError("Error: interface name " << interface << "is too long. Max len is:" <<
105 sizeof(clientAddr.sun_path));
106 return CKM_API_ERROR_SOCKET;
109 strcpy(clientAddr.sun_path, interface);
111 LogDebug("ClientAddr.sun_path = " << interface);
113 int retval = TEMP_FAILURE_RETRY(
114 connect(sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
115 if ((retval == -1) && (errno == EINPROGRESS)) {
116 if (0 >= waitForSocket(sock, POLLOUT, POLL_TIMEOUT)) {
117 LogError("Error in waitForSocket.");
118 return CKM_API_ERROR_SOCKET;
121 socklen_t len = sizeof(error);
122 retval = getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len);
126 LogError("Error in getsockopt: " << strerror(err));
127 return CKM_API_ERROR_SOCKET;
130 if (error == EACCES) {
131 LogError("Access denied");
132 return CKM_API_ERROR_ACCESS_DENIED;
136 LogError("Error in connect: " << strerror(error));
137 return CKM_API_ERROR_SOCKET;
140 return CKM_API_SUCCESS;
145 LogError("Error connecting socket: " << strerror(err));
147 return CKM_API_ERROR_ACCESS_DENIED;
148 return CKM_API_ERROR_SOCKET;
151 return CKM_API_SUCCESS;
154 int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv) {
160 if (CKM_API_SUCCESS != (ret = sock.Connect(interface))) {
161 LogError("Error in SockRAII");
165 while ((send.size() - done) > 0) {
166 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
167 LogError("Error in poll(POLLOUT)");
168 return CKM_API_ERROR_SOCKET;
170 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
173 LogError("Error in write: " << strerror(err));
174 return CKM_API_ERROR_SOCKET;
180 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
181 LogError("Error in poll(POLLIN)");
182 return CKM_API_ERROR_SOCKET;
184 ssize_t temp = TEMP_FAILURE_RETRY(read(sock.Get(), buffer, 2048));
187 LogError("Error in read: " << strerror(err));
188 return CKM_API_ERROR_SOCKET;
192 LogError("Read return 0/Connection closed by server(?)");
193 return CKM_API_ERROR_SOCKET;
196 RawBuffer raw(buffer, buffer+temp);
198 } while(!recv.Ready());
199 return CKM_API_SUCCESS;
202 int try_catch(const std::function<int()>& func)
206 } catch (MessageBuffer::Exception::Base &e) {
207 LogError("CKM::MessageBuffer::Exception " << e.DumpToString());
208 } catch (std::exception &e) {
209 LogError("STD exception " << e.what());
211 LogError("Unknown exception occured");
213 return CKM_API_ERROR_UNKNOWN;
216 void try_catch_async(const std::function<void()>& func, const std::function<void(int)>& error)
220 } catch (const MessageBuffer::Exception::Base& e) {
221 LogError("CKM::MessageBuffer::Exception " << e.DumpToString());
222 error(CKM_API_ERROR_BAD_REQUEST);
223 } catch (const std::exception& e) {
224 LogError("STD exception " << e.what());
225 error(CKM_API_ERROR_UNKNOWN);
227 LogError("Unknown exception occured");
228 error(CKM_API_ERROR_UNKNOWN);
234 static void init_lib(void) __attribute__ ((constructor));
235 static void init_lib(void)
237 centKeyClientEnableLogSystem();
240 static void fini_lib(void) __attribute__ ((destructor));
241 static void fini_lib(void)