Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / synchronization_pipe.cpp
1 /*
2  * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd. All rights reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        synchronization_pipe.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       A crippled abstraction of widely praised, but often misused communication mechanism
21  */
22
23 #include <poll.h>
24 #include <stdexcept>
25 #include <unistd.h>
26
27 #include <dpl/test/test_runner.h>
28
29 #include "synchronization_pipe.h"
30
31 static void closeFd(int *fd) {
32     if (*fd > -1) {
33         close(*fd);
34         *fd = -1;
35     }
36 }
37
38 SynchronizationPipe::SynchronizationPipe() {
39     auto ret = pipe(m_pipeCP);
40     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
41
42     ret = pipe(m_pipePC);
43     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
44 }
45
46 SynchronizationPipe::~SynchronizationPipe() {
47     closeFd(m_pipeCP + 0);
48     closeFd(m_pipeCP + 1);
49     closeFd(m_pipePC + 0);
50     closeFd(m_pipePC + 1);
51 }
52
53 void SynchronizationPipe::claimParentEp() {
54     if (m_epClaimed)
55         return;
56
57     m_readEp = m_pipeCP[0];
58     closeFd(m_pipeCP + 1);
59
60     m_writeEp = m_pipePC[1];
61     closeFd(m_pipePC + 0);
62
63     m_epClaimed = true;
64 }
65
66 void SynchronizationPipe::claimChildEp() {
67     if (m_epClaimed)
68         return;
69
70     m_readEp = m_pipePC[0];
71     closeFd(m_pipePC + 1);
72
73     m_writeEp = m_pipeCP[1];
74     closeFd(m_pipeCP + 0);
75
76     m_epClaimed = true;
77 }
78
79 void SynchronizationPipe::post() {
80     post("#", 1);
81 }
82
83 void SynchronizationPipe::wait() {
84     char dummy;
85     wait(&dummy, 1);
86 }
87
88 void SynchronizationPipe::pollForWait() {
89     RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
90
91     pollfd fds[1];
92     fds->fd = m_readEp;
93     fds->events = POLLIN;
94     auto ret = TEMP_FAILURE_RETRY(poll(fds, 1, -1));
95     RUNNER_ASSERT_ERRNO(ret > 0);
96 }
97
98 void SynchronizationPipe::post(const void *data, size_t size) {
99     RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
100     auto ret = TEMP_FAILURE_RETRY(write(m_writeEp, data, size));
101     RUNNER_ASSERT_ERRNO_MSG(ret > 0 && size_t(ret) == size, "Write failed size = " << size << " ret = " << ret);
102 }
103
104 void SynchronizationPipe::wait(void *data, size_t size) {
105     RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
106
107     auto ret = TEMP_FAILURE_RETRY(read(m_readEp, data, size));
108     RUNNER_ASSERT_ERRNO_MSG(ret > 0 && size_t(ret) == size, "Read failed size = " << size << " ret = " << ret);
109 }