From: Aleksander Zdyb Date: Thu, 18 Dec 2014 10:31:02 +0000 (+0100) Subject: Add SynchronizationPipe X-Git-Tag: security-manager_5.5_testing~138 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Ftest%2Fsecurity-tests.git;a=commitdiff_plain;h=04a6c6b43647847ab973a2c8a33a2a6b929a9050 Add SynchronizationPipe A crippled abstraction of widely praised, but often misused communication mechanism. Change-Id: I2bd4b63dac76711ce22331c0bc666d4cd6021240 --- diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 4ff6f8c..fc42023 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -23,6 +23,7 @@ SET(COMMON_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/common/fs_label_manager.cpp ${PROJECT_SOURCE_DIR}/src/common/passwd_access.cpp ${PROJECT_SOURCE_DIR}/src/common/uds.cpp + ${PROJECT_SOURCE_DIR}/src/common/synchronization_pipe.cpp ) #system and local includes diff --git a/src/common/synchronization_pipe.cpp b/src/common/synchronization_pipe.cpp new file mode 100644 index 0000000..dcf9d30 --- /dev/null +++ b/src/common/synchronization_pipe.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 synchronization_pipe.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief A crippled abstraction of widely praised, but often misused communication mechanism + */ + +#include +#include + +#include + +#include "synchronization_pipe.h" + +static void closeFd(int *fd) { + if (*fd > -1) { + close(*fd); + *fd = -1; + } +} + +SynchronizationPipe::SynchronizationPipe() { + auto ret = pipe(m_pipeCP); + RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed"); + + ret = pipe(m_pipePC); + RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed"); +} + +SynchronizationPipe::~SynchronizationPipe() { + closeFd(m_pipeCP + 0); + closeFd(m_pipeCP + 1); + closeFd(m_pipePC + 0); + closeFd(m_pipePC + 1); +} + +void SynchronizationPipe::claimParentEp() { + if (m_epClaimed) + return; + + m_readEp = m_pipeCP[0]; + closeFd(m_pipeCP + 1); + + m_writeEp = m_pipePC[1]; + closeFd(m_pipePC + 0); + + m_epClaimed = true; +} + +void SynchronizationPipe::claimChildEp() { + if (m_epClaimed) + return; + + m_readEp = m_pipePC[0]; + closeFd(m_pipePC + 1); + + m_writeEp = m_pipeCP[1]; + closeFd(m_pipeCP + 0); + + m_epClaimed = true; +} + +void SynchronizationPipe::post() { + RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed"); + auto ret = TEMP_FAILURE_RETRY(write(m_writeEp, "#", 1)); + RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Write failed ret = " << ret); +} + +void SynchronizationPipe::wait() { + RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed"); + + char buf; + auto ret = TEMP_FAILURE_RETRY(read(m_readEp, &buf, 1)); + RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Read failed ret = " << ret); +} diff --git a/src/common/synchronization_pipe.h b/src/common/synchronization_pipe.h new file mode 100644 index 0000000..e072ca2 --- /dev/null +++ b/src/common/synchronization_pipe.h @@ -0,0 +1,45 @@ +/* + * 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 synchronization_pipe.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief A crippled abstraction of widely praised, but often misused communication mechanism + */ + +#ifndef TESTS_COMMON_SYNCHRONIZATION_PIPE_H_ +#define TESTS_COMMON_SYNCHRONIZATION_PIPE_H_ + +class SynchronizationPipe { +public: + SynchronizationPipe(); + ~SynchronizationPipe(); + + void claimParentEp(); + void claimChildEp(); + + void post(); + void wait(); + +private: + int m_pipeCP[2]; // Child -> Parent + int m_pipePC[2]; // Parent -> Child + int m_readEp = -1; + int m_writeEp = -1; + bool m_epClaimed = false; +}; + +#endif // TESTS_COMMON_SYNCHRONIZATION_PIPE_H_