Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / message_pipe.cpp
1 /*
2  * Copyright (c) 2016 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        message_pipe.cpp
18  * @author      Zofia Abramowska
19  * @version     1.0
20  * @brief       Simple wrapper for message pipe
21  */
22
23 #include "message_pipe.h"
24
25 static void closeFd(int *fd) {
26     if (*fd > -1) {
27         close(*fd);
28         *fd = -1;
29     }
30 }
31
32 MessagePipe::MessagePipe() {
33     auto ret = pipe(m_pipeCP);
34     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
35
36     ret = pipe(m_pipePC);
37     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
38 }
39
40 MessagePipe::~MessagePipe() {
41     closeFd(m_pipeCP + 0);
42     closeFd(m_pipeCP + 1);
43     closeFd(m_pipePC + 0);
44     closeFd(m_pipePC + 1);
45 }
46
47 void MessagePipe::claimParentEp() {
48     if (m_epClaimed)
49         return;
50
51     m_readEp = m_pipeCP[0];
52     closeFd(m_pipeCP + 1);
53
54     m_writeEp = m_pipePC[1];
55     closeFd(m_pipePC + 0);
56
57     m_epClaimed = true;
58 }
59
60 void MessagePipe::claimChildEp() {
61     if (m_epClaimed)
62         return;
63
64     m_readEp = m_pipePC[0];
65     closeFd(m_pipePC + 1);
66
67     m_writeEp = m_pipeCP[1];
68     closeFd(m_pipeCP + 0);
69
70     m_epClaimed = true;
71 }
72