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>
30 #include <sys/smack.h>
31 #include <sys/xattr.h>
32 #include <linux/xattr.h>
35 #include <dpl/log/log.h>
36 #include <dpl/serialization.h>
37 #include <dpl/singleton.h>
38 #include <dpl/singleton_safe_impl.h>
40 #include <message-buffer.h>
42 #include <protocols.h>
44 IMPLEMENT_SAFE_SINGLETON(SecurityManager::Log::LogSystem);
48 const int POLL_TIMEOUT = -1;
50 void securityClientEnableLogSystem(void) {
51 SecurityManager::Singleton<SecurityManager::Log::LogSystem>::Instance().SetTag("SECURITY_MANAGER_CLIENT");
54 int waitForSocket(int sock, int event, int timeout) {
58 desc[0].events = event;
60 while((-1 == (retval = poll(desc, 1, timeout))) && (errno == EINTR)) {
66 LogDebug("Poll timeout");
67 } else if (-1 == retval) {
69 LogError("Error in poll: " << strerror(err));
85 int Connect(char const * const interface) {
86 sockaddr_un clientAddr;
89 if (m_sock != -1) // guard
92 m_sock = socket(AF_UNIX, SOCK_STREAM, 0);
95 LogError("Error creating socket: " << strerror(err));
96 return SECURITY_MANAGER_API_ERROR_SOCKET;
99 if ((flags = fcntl(m_sock, F_GETFL, 0)) < 0 ||
100 fcntl(m_sock, F_SETFL, flags | O_NONBLOCK) < 0)
103 LogError("Error in fcntl: " << strerror(err));
104 return SECURITY_MANAGER_API_ERROR_SOCKET;
107 memset(&clientAddr, 0, sizeof(clientAddr));
109 clientAddr.sun_family = AF_UNIX;
111 if (strlen(interface) >= sizeof(clientAddr.sun_path)) {
112 LogError("Error: interface name " << interface << "is too long. Max len is:" << sizeof(clientAddr.sun_path));
113 return SECURITY_MANAGER_API_ERROR_NO_SUCH_SERVICE;
116 strcpy(clientAddr.sun_path, interface);
118 LogDebug("ClientAddr.sun_path = " << interface);
120 int retval = TEMP_FAILURE_RETRY(connect(m_sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
121 if ((retval == -1) && (errno == EINPROGRESS)) {
122 if (0 >= waitForSocket(m_sock, POLLOUT, POLL_TIMEOUT)) {
123 LogError("Error in waitForSocket.");
124 return SECURITY_MANAGER_API_ERROR_SOCKET;
127 socklen_t len = sizeof(error);
128 retval = getsockopt(m_sock, SOL_SOCKET, SO_ERROR, &error, &len);
132 LogError("Error in getsockopt: " << strerror(err));
133 return SECURITY_MANAGER_API_ERROR_SOCKET;
136 if (error == EACCES) {
137 LogError("Access denied");
138 return SECURITY_MANAGER_API_ERROR_ACCESS_DENIED;
142 LogError("Error in connect: " << strerror(error));
143 return SECURITY_MANAGER_API_ERROR_SOCKET;
146 return SECURITY_MANAGER_API_SUCCESS;
151 LogError("Error connecting socket: " << strerror(err));
153 return SECURITY_MANAGER_API_ERROR_ACCESS_DENIED;
155 return SECURITY_MANAGER_API_ERROR_NO_SUCH_SERVICE;
156 return SECURITY_MANAGER_API_ERROR_SOCKET;
159 return SECURITY_MANAGER_API_SUCCESS;
170 } // namespace anonymous
172 namespace SecurityManager {
174 int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv) {
180 if (SECURITY_MANAGER_API_SUCCESS != (ret = sock.Connect(interface))) {
181 LogError("Error in SockRAII");
185 while ((send.size() - done) > 0) {
186 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
187 LogError("Error in poll(POLLOUT)");
188 return SECURITY_MANAGER_API_ERROR_SOCKET;
190 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
193 LogError("Error in write: " << strerror(err));
194 return SECURITY_MANAGER_API_ERROR_SOCKET;
200 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
201 LogError("Error in poll(POLLIN)");
202 return SECURITY_MANAGER_API_ERROR_SOCKET;
204 ssize_t temp = TEMP_FAILURE_RETRY(read(sock.Get(), buffer, 2048));
207 LogError("Error in read: " << strerror(err));
208 return SECURITY_MANAGER_API_ERROR_SOCKET;
212 LogError("Read return 0/Connection closed by server(?)");
213 return SECURITY_MANAGER_API_ERROR_SOCKET;
216 RawBuffer raw(buffer, buffer+temp);
218 } while(!recv.Ready());
219 return SECURITY_MANAGER_API_SUCCESS;
222 int sendToServerAncData(char const * const interface, const RawBuffer &send, struct msghdr &hdr) {
227 if (SECURITY_MANAGER_API_SUCCESS != (ret = sock.Connect(interface))) {
228 LogError("Error in SockRAII");
232 while ((send.size() - done) > 0) {
233 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
234 LogError("Error in poll(POLLOUT)");
235 return SECURITY_MANAGER_API_ERROR_SOCKET;
237 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
240 LogError("Error in write: " << strerror(err));
241 return SECURITY_MANAGER_API_ERROR_SOCKET;
246 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
247 LogError("Error in poll(POLLIN)");
248 return SECURITY_MANAGER_API_ERROR_SOCKET;
251 ssize_t temp = TEMP_FAILURE_RETRY(recvmsg(sock.Get(), &hdr, MSG_CMSG_CLOEXEC));
255 LogError("Error in recvmsg(): " << strerror(err) << " errno: " << err);
256 return SECURITY_MANAGER_API_ERROR_SOCKET;
260 LogError("Read return 0/Connection closed by server(?)");
261 return SECURITY_MANAGER_API_ERROR_SOCKET;
264 return SECURITY_MANAGER_API_SUCCESS;
267 int try_catch(const std::function<int()>& func)
271 } catch (MessageBuffer::Exception::Base &e) {
272 LogError("SecurityManager::MessageBuffer::Exception " << e.DumpToString());
273 } catch (std::exception &e) {
274 LogError("STD exception " << e.what());
276 LogError("Unknown exception occured");
278 return SECURITY_MANAGER_API_ERROR_UNKNOWN;
281 } // namespace SecurityMANAGER
283 static void init_lib(void) __attribute__ ((constructor));
284 static void init_lib(void)
286 securityClientEnableLogSystem();
289 static void fini_lib(void) __attribute__ ((destructor));
290 static void fini_lib(void)