2 * Copyright (c) 2014-2015 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 src/service/sockets/SocketManager.cpp
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19 * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
21 * @brief This file implements socket layer manager for cynara
28 #include <sys/select.h>
29 #include <sys/signalfd.h>
30 #include <sys/socket.h>
32 #include <sys/types.h>
36 #ifdef BUILD_WITH_SYSTEMD
37 #include <systemd/sd-daemon.h>
42 #include <config/PathConfig.h>
43 #include <exceptions/DescriptorNotExistsException.h>
44 #include <exceptions/InitException.h>
45 #include <exceptions/UnexpectedErrorException.h>
47 #include <logic/Logic.h>
48 #include <main/Cynara.h>
49 #include <protocol/ProtocolAdmin.h>
50 #include <protocol/ProtocolAgent.h>
51 #include <protocol/ProtocolClient.h>
52 #include <protocol/ProtocolSignal.h>
53 #include <request/pointers.h>
54 #include <request/RequestContext.h>
57 #include "SocketManager.h"
61 SocketManager::SocketManager() : m_working(false), m_maxDesc(-1) {
66 SocketManager::~SocketManager() {
69 void SocketManager::run(void) {
74 void SocketManager::init(void) {
75 LOGI("SocketManger init start");
76 const mode_t clientSocketUMask(0);
77 const mode_t adminSocketUMask(0077);
78 const mode_t agentSocketUMask(0);
80 createDomainSocket(std::make_shared<ProtocolClient>(), PathConfig::SocketPath::client,
81 clientSocketUMask, true);
82 createDomainSocket(std::make_shared<ProtocolAdmin>(), PathConfig::SocketPath::admin,
83 adminSocketUMask, false);
84 createDomainSocket(std::make_shared<ProtocolAgent>(), PathConfig::SocketPath::agent,
85 agentSocketUMask, false);
86 createSignalSocket(std::make_shared<ProtocolSignal>());
87 LOGI("SocketManger init done");
90 void SocketManager::mainLoop(void) {
91 LOGI("SocketManger mainLoop start");
94 fd_set readSet = m_readSet;
95 fd_set writeSet = m_writeSet;
97 int ret = select(m_maxDesc + 1, &readSet, &writeSet, nullptr, nullptr);
105 throw UnexpectedErrorException(err, strerror(err));
107 } else if (ret > 0) {
108 for (int i = 0; i < m_maxDesc + 1 && ret; ++i) {
109 if (FD_ISSET(i, &readSet)) {
113 if (FD_ISSET(i, &writeSet)) {
119 for (int i = 0; i < m_maxDesc + 1; ++i) {
120 if (m_fds[i].isUsed() && m_fds[i].hasDataToWrite())
125 LOGI("SocketManger mainLoop done");
128 void SocketManager::mainLoopStop(void) {
132 void SocketManager::readyForRead(int fd) {
133 LOGD("SocketManger readyForRead on fd [%d] start", fd);
134 auto &desc = m_fds[fd];
135 if (desc.isListen()) {
140 RawBuffer readBuffer(DEFAULT_BUFFER_SIZE);
141 ssize_t size = read(fd, readBuffer.data(), DEFAULT_BUFFER_SIZE);
144 LOGD("read [%zd] bytes", size);
145 readBuffer.resize(size);
146 if (handleRead(fd, readBuffer)) {
147 LOGD("SocketManger readyForRead on fd [%d] successfully done", fd);
150 LOGI("interpreting buffer read from [%d] failed", fd);
151 } else if (size < 0) {
155 #if EWOULDBLOCK != EAGAIN
161 LOGW("While reading from [%d] socket, error [%d]:<%s>",
162 fd, err, strerror(err));
165 LOGN("Socket [%d] closed on other end", fd);
168 LOGD("SocketManger readyForRead on fd [%d] done", fd);
171 void SocketManager::readyForWrite(int fd) {
172 LOGD("SocketManger readyForWrite on fd [%d] start", fd);
173 auto &desc = m_fds[fd];
174 auto &buffer = desc.prepareWriteBuffer();
175 size_t size = buffer.size();
176 ssize_t result = write(fd, buffer.data(), size);
182 // select will trigger write once again, nothing to do
186 LOGD("Error during write to fd [%d]:<%s> ", fd, strerror(err));
190 return; // We do not want to propagate error to next layer
193 LOGD("written [%zd] bytes", result);
194 buffer.erase(buffer.begin(), buffer.begin() + result);
197 removeWriteSocket(fd);
198 LOGD("SocketManger readyForWrite on fd [%d] done", fd);
201 void SocketManager::readyForAccept(int fd) {
202 LOGD("SocketManger readyForAccept on fd [%d] start", fd);
203 struct sockaddr_un clientAddr;
204 unsigned int clientLen = sizeof(clientAddr);
205 int clientFd = accept4(fd, (struct sockaddr*) &clientAddr, &clientLen, SOCK_NONBLOCK);
206 if (clientFd == -1) {
208 LOGW("Error in accept on socket [%d]: <%s>", fd, strerror(err));
211 LOGD("Accept on sock [%d]. New client socket opened [%d]", fd, clientFd);
213 auto &desc = createDescriptor(clientFd, m_fds[fd].isClient());
214 desc.setListen(false);
215 desc.setProtocol(m_fds[fd].protocol()->clone());
216 addReadSocket(clientFd);
217 LOGD("SocketManger readyForAccept on fd [%d] done", fd);
220 void SocketManager::closeSocket(int fd) {
221 LOGD("SocketManger closeSocket fd [%d] start", fd);
222 Descriptor &desc = m_fds[fd];
223 requestTaker()->contextClosed(RequestContext(nullptr, desc.writeQueue()));
224 removeReadSocket(fd);
225 removeWriteSocket(fd);
228 LOGD("SocketManger closeSocket fd [%d] done", fd);
231 bool SocketManager::handleRead(int fd, const RawBuffer &readbuffer) {
232 LOGD("SocketManger handleRead on fd [%d] start", fd);
233 auto &desc = m_fds[fd];
234 desc.pushReadBuffer(readbuffer);
238 //try extract request from binary data received on socket
239 auto req = desc.extractRequest();
240 if (!req) // not enough data to build request yet
242 LOGD("request extracted");
245 RequestContext context(desc.responseTaker(), desc.writeQueue());
246 //pass request to request taker
247 req->execute(*requestTaker(), context);
249 } catch (const Exception &ex) {
250 LOGE("Error handling request <%s>. Closing socket", ex.what());
253 LOGD("SocketManger handleRead on fd [%d] done", fd);
257 void SocketManager::createDomainSocket(ProtocolPtr protocol, const std::string &path, mode_t mask,
260 #ifdef BUILD_WITH_SYSTEMD
261 fd = getSocketFromSystemD(path);
264 fd = createDomainSocketHelp(path, mask);
266 auto &desc = createDescriptor(fd, client);
267 desc.setListen(true);
268 desc.setProtocol(protocol);
271 LOGD("Domain socket: [%d] added.", fd);
274 int SocketManager::createDomainSocketHelp(const std::string &path, mode_t mask) {
277 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
279 LOGE("Error during UNIX socket creation: <%s>", strerror(err));
280 throw InitException();
284 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
286 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
289 LOGE("Error setting \"O_NONBLOCK\" on descriptor [%d] with fcntl: <%s>",
291 throw InitException();
294 sockaddr_un serverAddress;
295 memset(&serverAddress, 0, sizeof(serverAddress));
296 serverAddress.sun_family = AF_UNIX;
297 if (path.length() > sizeof(serverAddress.sun_path)) {
298 LOGE("Path for unix domain socket <%s> is [%zu] bytes long, while it should be maximum "
299 "[%zu] bytes long", path.c_str(), path.length(), sizeof(serverAddress));
300 throw InitException();
302 strcpy(serverAddress.sun_path, path.c_str());
303 unlink(serverAddress.sun_path);
305 mode_t originalUmask;
306 originalUmask = umask(mask);
308 if (bind(fd, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
311 LOGE("Error in bind socket descriptor [%d] to path <%s>: <%s>",
312 fd, path.c_str(), strerror(err));
313 throw InitException();
316 umask(originalUmask);
318 if (listen(fd, 5) == -1) {
321 LOGE("Error setting listen on file descriptor [%d], path <%s>: <%s>",
322 fd, path.c_str(), strerror(err));
323 throw InitException();
329 #ifdef BUILD_WITH_SYSTEMD
330 int SocketManager::getSocketFromSystemD(const std::string &path) {
331 int n = sd_listen_fds(0);
332 LOGI("sd_listen_fds returns: [%d]", n);
334 LOGE("Error in sd_listend_fds");
335 throw InitException();
338 for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; ++fd) {
339 if (sd_is_socket_unix(fd, SOCK_STREAM, 1, path.c_str(), 0) > 0) {
340 LOGI("Useable socket <%s> was passed by SystemD under descriptor [%d]",
345 LOGI("No useable sockets were passed by systemd.");
348 #endif // BUILD_WITH_SYSTEMD
350 void SocketManager::createSignalSocket(ProtocolPtr protocol) {
353 // Maybe someone will find useful some kind of registering signals with callbacks
354 // but for now I'm making it as simple as possible.
356 sigaddset(&mask, SIGTERM); // systemd terminates service sending this signal
358 if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
359 LOGE("sigprocmask failed: <%s>", strerror(errno));
363 int fd = signalfd(-1, &mask, SFD_NONBLOCK);
365 LOGE("Creating signal file descriptor failed: <%s>", strerror(errno));
369 auto &desc = createDescriptor(fd, false);
370 desc.setListen(false);
371 desc.setProtocol(protocol);
374 LOGD("Signal socket: [%d] added.", fd);
377 Descriptor &SocketManager::createDescriptor(int fd, bool client) {
378 if (fd > m_maxDesc) {
380 if (fd >= static_cast<int>(m_fds.size()))
381 m_fds.resize(fd + 20);
383 auto &desc = m_fds[fd];
385 desc.setClient(client);
389 void SocketManager::addReadSocket(int fd) {
390 FD_SET(fd, &m_readSet);
393 void SocketManager::removeReadSocket(int fd) {
394 FD_CLR(fd, &m_readSet);
397 void SocketManager::addWriteSocket(int fd) {
398 FD_SET(fd, &m_writeSet);
401 void SocketManager::removeWriteSocket(int fd) {
402 FD_CLR(fd, &m_writeSet);
405 RequestTakerPtr SocketManager::requestTaker(void) {
406 return std::static_pointer_cast<RequestTaker>(m_logic);
409 void SocketManager::disconnectAllClients(void) {
410 for(int i = 0; i <= m_maxDesc; ++i) {
411 auto &desc = m_fds[i];
412 if(desc.isUsed() && desc.isClient() && !desc.isListen())
417 } // namespace Cynara