• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "sys/shm.h"
32 #include "it_test_signal.h"
33 #include "signal.h"
34 
SigPrint(int signum)35 static void SigPrint(int signum)
36 {
37     printf("Pipe break\n");
38 }
39 
PipecommonWrite()40 static int PipecommonWrite()
41 {
42     int pipefd[2]; // 2, array subscript
43     pid_t pid;
44     int retValue = -1;
45     int *sharedflag = NULL;
46     int shmid;
47     int *readFd = &pipefd[0];
48     int *writeFd = &pipefd[1];
49     char sentence[] = "Hello World";
50     char readbuffer[100];
51     int status, ret;
52 
53     retValue = pipe(pipefd);
54     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
55     if (signal(SIGPIPE, SigPrint) == SIG_ERR) {
56         printf("signal error\n");
57     }
58 
59     shmid = shmget(static_cast<key_t>(IPC_PRIVATE), sizeof(int), 0666 | IPC_CREAT); // 0666 the authority of the shm
60     ICUNIT_ASSERT_NOT_EQUAL(shmid, -1, shmid);
61     sharedflag = (int *)shmat(shmid, NULL, 0);
62     *sharedflag = 0;
63 
64     pid = fork();
65     if (pid == -1) {
66         printf("Fork Error!\n");
67         return -1;
68     } else if (pid == 0) {
69         sharedflag = (int *)shmat(shmid, NULL, 0);
70         close(*readFd);
71         retValue = write(*writeFd, sentence, strlen(sentence) + 1);
72         ICUNIT_ASSERT_EQUAL(retValue, strlen(sentence) + 1, retValue);
73         *sharedflag = 1;
74         // 2 waitting for the father process close the pipe's read port
75         while (*sharedflag != 2) {
76             usleep(1);
77         }
78         retValue = write(*writeFd, sentence, strlen(sentence) + 1);
79         ICUNIT_ASSERT_EQUAL(retValue, -1, retValue);
80         ICUNIT_ASSERT_EQUAL(errno, EPIPE, errno);
81         exit(0);
82     } else {
83         close(*writeFd);
84         // 1 waitting for the sub process has written the sentence first
85         while (*sharedflag != 1) {
86             usleep(1);
87         }
88         close(*readFd);
89         // 2 father process close the pipe's read port
90         *sharedflag = 2;
91         ret = waitpid(pid, &status, 0);
92         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
93         ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
94     }
95     return 0;
96 }
97 
ItPosixPipe005(void)98 void ItPosixPipe005(void)
99 {
100     TEST_ADD_CASE(__FUNCTION__, PipecommonWrite, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
101 }
102