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 IMPLEMENT_SAFE_SINGLETON(CKM::Log::LogSystem);
44 const int POLL_TIMEOUT = 8000;
46 void centKeyClientEnableLogSystem(void) {
47 CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM_CLIENT");
50 int waitForSocket(int sock, int event, int timeout) {
54 desc[0].events = event;
56 while((-1 == (retval = poll(desc, 1, timeout))) && (errno == EINTR)) {
62 LogDebug("Poll timeout");
63 } else if (-1 == retval) {
65 LogError("Error in poll: " << strerror(err));
81 int Connect(char const * const interface) {
82 sockaddr_un clientAddr;
85 if (m_sock != -1) // guard
88 m_sock = socket(AF_UNIX, SOCK_STREAM, 0);
91 LogError("Error creating socket: " << strerror(err));
92 return CKM_API_ERROR_SOCKET;
95 if ((flags = fcntl(m_sock, F_GETFL, 0)) < 0 ||
96 fcntl(m_sock, F_SETFL, flags | O_NONBLOCK) < 0)
99 LogError("Error in fcntl: " << strerror(err));
100 return CKM_API_ERROR_SOCKET;
103 memset(&clientAddr, 0, sizeof(clientAddr));
105 clientAddr.sun_family = AF_UNIX;
107 if (strlen(interface) >= sizeof(clientAddr.sun_path)) {
108 LogError("Error: interface name " << interface << "is too long. Max len is:" << sizeof(clientAddr.sun_path));
109 return CKM_API_ERROR_SOCKET;
112 strcpy(clientAddr.sun_path, interface);
114 LogDebug("ClientAddr.sun_path = " << interface);
116 int retval = TEMP_FAILURE_RETRY(connect(m_sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
117 if ((retval == -1) && (errno == EINPROGRESS)) {
118 if (0 >= waitForSocket(m_sock, POLLOUT, POLL_TIMEOUT)) {
119 LogError("Error in waitForSocket.");
120 return CKM_API_ERROR_SOCKET;
123 size_t len = sizeof(error);
124 retval = getsockopt(m_sock, SOL_SOCKET, SO_ERROR, &error, &len);
128 LogError("Error in getsockopt: " << strerror(err));
129 return CKM_API_ERROR_SOCKET;
132 if (error == EACCES) {
133 LogError("Access denied");
134 return CKM_API_ERROR_ACCESS_DENIED;
138 LogError("Error in connect: " << strerror(error));
139 return CKM_API_ERROR_SOCKET;
142 return CKM_API_SUCCESS;
147 LogError("Error connecting socket: " << strerror(err));
149 return CKM_API_ERROR_ACCESS_DENIED;
150 return CKM_API_ERROR_SOCKET;
153 return CKM_API_SUCCESS;
164 } // namespace anonymous
169 int sendToServer(char const * const interface, const SafeBuffer &send, MessageBuffer &recv) {
175 if (CKM_API_SUCCESS != (ret = sock.Connect(interface))) {
176 LogError("Error in SockRAII");
180 while ((send.size() - done) > 0) {
181 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
182 LogError("Error in poll(POLLOUT)");
183 return CKM_API_ERROR_SOCKET;
185 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
188 LogError("Error in write: " << strerror(err));
189 return CKM_API_ERROR_SOCKET;
195 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
196 LogError("Error in poll(POLLIN)");
197 return CKM_API_ERROR_SOCKET;
199 ssize_t temp = TEMP_FAILURE_RETRY(read(sock.Get(), buffer, 2048));
202 LogError("Error in read: " << strerror(err));
203 return CKM_API_ERROR_SOCKET;
207 LogError("Read return 0/Connection closed by server(?)");
208 return CKM_API_ERROR_SOCKET;
211 SafeBuffer raw(buffer, buffer+temp);
213 } while(!recv.Ready());
214 return CKM_API_SUCCESS;
217 int try_catch(const std::function<int()>& func)
221 } catch (MessageBuffer::Exception::Base &e) {
222 LogError("CKM::MessageBuffer::Exception " << e.DumpToString());
223 } catch (std::exception &e) {
224 LogError("STD exception " << e.what());
226 LogError("Unknown exception occured");
228 return CKM_API_ERROR_UNKNOWN;
233 static void init_lib(void) __attribute__ ((constructor));
234 static void init_lib(void)
236 centKeyClientEnableLogSystem();
239 static void fini_lib(void) __attribute__ ((destructor));
240 static void fini_lib(void)