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>
41 #include <smack-common.h>
43 #include <protocols.h>
45 IMPLEMENT_SAFE_SINGLETON(SecurityManager::Log::LogSystem);
49 const int POLL_TIMEOUT = 2000;
51 void securityClientEnableLogSystem(void) {
52 SecurityManager::Singleton<SecurityManager::Log::LogSystem>::Instance().SetTag("SECURITY_MANAGER_CLIENT");
55 int waitForSocket(int sock, int event, int timeout) {
59 desc[0].events = event;
61 while((-1 == (retval = poll(desc, 1, timeout))) && (errno == EINTR)) {
67 LogDebug("Poll timeout");
68 } else if (-1 == retval) {
70 LogError("Error in poll: " << strerror(err));
86 int Connect(char const * const interface) {
87 sockaddr_un clientAddr;
90 if (m_sock != -1) // guard
93 m_sock = socket(AF_UNIX, SOCK_STREAM, 0);
96 LogError("Error creating socket: " << strerror(err));
97 return SECURITY_MANAGER_API_ERROR_SOCKET;
100 if ((flags = fcntl(m_sock, F_GETFL, 0)) < 0 ||
101 fcntl(m_sock, F_SETFL, flags | O_NONBLOCK) < 0)
104 LogError("Error in fcntl: " << strerror(err));
105 return SECURITY_MANAGER_API_ERROR_SOCKET;
108 memset(&clientAddr, 0, sizeof(clientAddr));
110 clientAddr.sun_family = AF_UNIX;
112 if (strlen(interface) >= sizeof(clientAddr.sun_path)) {
113 LogError("Error: interface name " << interface << "is too long. Max len is:" << sizeof(clientAddr.sun_path));
114 return SECURITY_MANAGER_API_ERROR_NO_SUCH_SERVICE;
117 strcpy(clientAddr.sun_path, interface);
119 LogDebug("ClientAddr.sun_path = " << interface);
121 int retval = TEMP_FAILURE_RETRY(connect(m_sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
122 if ((retval == -1) && (errno == EINPROGRESS)) {
123 if (0 >= waitForSocket(m_sock, POLLOUT, POLL_TIMEOUT)) {
124 LogError("Error in waitForSocket.");
125 return SECURITY_MANAGER_API_ERROR_SOCKET;
128 socklen_t len = sizeof(error);
129 retval = getsockopt(m_sock, SOL_SOCKET, SO_ERROR, &error, &len);
133 LogError("Error in getsockopt: " << strerror(err));
134 return SECURITY_MANAGER_API_ERROR_SOCKET;
137 if (error == EACCES) {
138 LogError("Access denied");
139 return SECURITY_MANAGER_API_ERROR_ACCESS_DENIED;
143 LogError("Error in connect: " << strerror(error));
144 return SECURITY_MANAGER_API_ERROR_SOCKET;
147 return SECURITY_MANAGER_API_SUCCESS;
152 LogError("Error connecting socket: " << strerror(err));
154 return SECURITY_MANAGER_API_ERROR_ACCESS_DENIED;
156 return SECURITY_MANAGER_API_ERROR_NO_SUCH_SERVICE;
157 return SECURITY_MANAGER_API_ERROR_SOCKET;
160 return SECURITY_MANAGER_API_SUCCESS;
171 } // namespace anonymous
173 namespace SecurityManager {
175 int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv) {
181 if (SECURITY_MANAGER_API_SUCCESS != (ret = sock.Connect(interface))) {
182 LogError("Error in SockRAII");
186 while ((send.size() - done) > 0) {
187 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
188 LogError("Error in poll(POLLOUT)");
189 return SECURITY_MANAGER_API_ERROR_SOCKET;
191 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
194 LogError("Error in write: " << strerror(err));
195 return SECURITY_MANAGER_API_ERROR_SOCKET;
201 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
202 LogError("Error in poll(POLLIN)");
203 return SECURITY_MANAGER_API_ERROR_SOCKET;
205 ssize_t temp = TEMP_FAILURE_RETRY(read(sock.Get(), buffer, 2048));
208 LogError("Error in read: " << strerror(err));
209 return SECURITY_MANAGER_API_ERROR_SOCKET;
213 LogError("Read return 0/Connection closed by server(?)");
214 return SECURITY_MANAGER_API_ERROR_SOCKET;
217 RawBuffer raw(buffer, buffer+temp);
219 } while(!recv.Ready());
220 return SECURITY_MANAGER_API_SUCCESS;
223 int sendToServerAncData(char const * const interface, const RawBuffer &send, struct msghdr &hdr) {
228 if (SECURITY_MANAGER_API_SUCCESS != (ret = sock.Connect(interface))) {
229 LogError("Error in SockRAII");
233 while ((send.size() - done) > 0) {
234 if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
235 LogError("Error in poll(POLLOUT)");
236 return SECURITY_MANAGER_API_ERROR_SOCKET;
238 ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
241 LogError("Error in write: " << strerror(err));
242 return SECURITY_MANAGER_API_ERROR_SOCKET;
247 if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
248 LogError("Error in poll(POLLIN)");
249 return SECURITY_MANAGER_API_ERROR_SOCKET;
252 ssize_t temp = TEMP_FAILURE_RETRY(recvmsg(sock.Get(), &hdr, MSG_CMSG_CLOEXEC));
256 LogError("Error in recvmsg(): " << strerror(err) << " errno: " << err);
257 return SECURITY_MANAGER_API_ERROR_SOCKET;
261 LogError("Read return 0/Connection closed by server(?)");
262 return SECURITY_MANAGER_API_ERROR_SOCKET;
265 return SECURITY_MANAGER_API_SUCCESS;
268 int try_catch(const std::function<int()>& func)
272 } catch (MessageBuffer::Exception::Base &e) {
273 LogError("SecurityManager::MessageBuffer::Exception " << e.DumpToString());
274 } catch (std::exception &e) {
275 LogError("STD exception " << e.what());
277 LogError("Unknown exception occured");
279 return SECURITY_MANAGER_API_ERROR_UNKNOWN;
282 } // namespace SecurityMANAGER
284 static void init_lib(void) __attribute__ ((constructor));
285 static void init_lib(void)
287 securityClientEnableLogSystem();
290 static void fini_lib(void) __attribute__ ((destructor));
291 static void fini_lib(void)