From 5b3fb2a01a8b9ec423b53e46d3f37e924455b02f Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Wed, 3 Sep 2014 08:26:39 +0200 Subject: [PATCH 1/1] Add UDSHelpers (Helpers for Unix Domain Sockets) Change-Id: Id4c0429c325bc828d7c928e1c12d2ba9ec2b6a9f --- src/common/CMakeLists.txt | 1 + src/common/uds.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++ src/common/uds.h | 37 +++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/common/uds.cpp create mode 100644 src/common/uds.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 8c35483..4ff6f8c 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -22,6 +22,7 @@ SET(COMMON_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/common/db_sqlite.cpp ${PROJECT_SOURCE_DIR}/src/common/fs_label_manager.cpp ${PROJECT_SOURCE_DIR}/src/common/passwd_access.cpp + ${PROJECT_SOURCE_DIR}/src/common/uds.cpp ) #system and local includes diff --git a/src/common/uds.cpp b/src/common/uds.cpp new file mode 100644 index 0000000..7523539 --- /dev/null +++ b/src/common/uds.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file uds.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helpers for Unix Domain Sockets + */ + +#include +#include +#include +#include + +#include +#include + +#include "uds.h" + +namespace UDSHelpers { + +int createServer(const struct sockaddr_un *sockaddr) { + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + RUNNER_ASSERT_ERRNO_MSG(sock >= 0, "socket failed"); + + SockUniquePtr sockPtr(&sock); + + int bindResult = bind(sock, (const struct sockaddr*) sockaddr, sizeof(struct sockaddr_un)); + RUNNER_ASSERT_ERRNO_MSG(bindResult == 0, "bind failed"); + + int listenResult = listen(sock, 1); + RUNNER_ASSERT_ERRNO_MSG(listenResult == 0, "listen failed"); + + sockPtr.release(); + return sock; +} + +int createClient(const struct sockaddr_un *sockaddr) { + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + RUNNER_ASSERT_ERRNO_MSG(sock >= 0, "socket failed"); + + SockUniquePtr sockPtr(&sock); + + int connectResult = TEMP_FAILURE_RETRY( + connect(sock, (const struct sockaddr*) sockaddr, sizeof(struct sockaddr_un))); + RUNNER_ASSERT_ERRNO_MSG(connectResult == 0, "connect failed"); + + sockPtr.release(); + return sock; +} + +int acceptClient(int sock) { + int clientSock = TEMP_FAILURE_RETRY(accept(sock, NULL, NULL)); + RUNNER_ASSERT_ERRNO_MSG(clientSock >= 0, "accept failed"); + return clientSock; +} + +void waitForDisconnect(int sock) { + const nfds_t fdCount = 1; + const int timeout = -1; // no timeout + + struct pollfd pfd { sock, POLLRDHUP, 0 }; + int ret = TEMP_FAILURE_RETRY(poll(&pfd, fdCount, timeout)); + RUNNER_ASSERT_ERRNO_MSG(ret >= 0, "poll failed"); +} + +struct sockaddr_un makeAbstractAddress(const std::string &path) { + struct sockaddr_un sockaddr; + RUNNER_ASSERT_MSG(path.size() <= sizeof(sockaddr.sun_path) - 1, "Socket path too long"); + memset(&sockaddr, 0, sizeof(struct sockaddr_un)); + sockaddr.sun_family = AF_UNIX; + // Leave '\0' as a first character of path + memcpy(sockaddr.sun_path + 1, path.c_str(), path.size()); + return sockaddr; +} + +} // namespace UDSHelpers diff --git a/src/common/uds.h b/src/common/uds.h new file mode 100644 index 0000000..7af0631 --- /dev/null +++ b/src/common/uds.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file uds.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helpers for Unix Domain Sockets + */ + +#ifndef TESTS_COMMON_UDS_H_ +#define TESTS_COMMON_UDS_H_ + +#include +#include + +namespace UDSHelpers { + int createServer(const struct sockaddr_un *sockaddr); + int createClient(const struct sockaddr_un *sockaddr); + int acceptClient(int sock); + void waitForDisconnect(int sock); + struct sockaddr_un makeAbstractAddress(const std::string &path); +}; + +#endif // TESTS_COMMON_UDS_H_ -- 2.7.4