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 "it_test_signal.h"
32 #include "signal.h"
33 #include "fcntl.h"
34 #include "sys/shm.h"
35
TestPipeMultiProcess()36 static int TestPipeMultiProcess()
37 {
38 int pipefd[2]; // 2, array subscript
39 pid_t pid;
40 int retValue = -1;
41 retValue = pipe(pipefd);
42 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
43
44 int *readFd = &pipefd[0];
45 int *writeFd = &pipefd[1];
46 char readbuffer[100] = {0};
47 int status, ret;
48 int totalNum = 3;
49 int *sharedflag = NULL;
50 int shmid;
51
52 int flag = fcntl(*readFd, F_GETFL);
53 fcntl(*readFd, F_SETFL, flag | O_NONBLOCK);
54 shmid = shmget(static_cast<key_t>(IPC_PRIVATE), sizeof(int), 0666 | IPC_CREAT); // 0666 the authority of the shm
55 ICUNIT_ASSERT_NOT_EQUAL(shmid, -1, shmid);
56 sharedflag = (int *)shmat(shmid, NULL, 0);
57 *sharedflag = 0;
58
59 pid = fork();
60 if (pid == -1) {
61 printf("Fork Error!\n");
62 return -1;
63 } else if (pid == 0) {
64 sharedflag = (int *)shmat(shmid, NULL, 0);
65 close(pipefd[0]);
66 for (int i = 0; i < totalNum; i++) {
67 errno = 0;
68 char sentence1[15] = "Hello World";
69 char a[2] = {0};
70 (void)sprintf_s(a, sizeof(a), "%d", i);
71 (void)strcat_s(sentence1, 15, a); // 15, sizeof sentence1
72 int ret = write(*writeFd, sentence1, strlen(sentence1) + 1);
73 ICUNIT_ASSERT_EQUAL(ret, strlen(sentence1) + 1, ret);
74 usleep(10000); // 10000, Used to calculate the delay time.
75 }
76 *sharedflag = 1;
77 ret = close(pipefd[1]);
78 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
79 exit(0);
80 } else {
81 close(pipefd[1]);
82 // waitting for the sub process has written the sentence first
83 while (*sharedflag != 1) {
84 usleep(1);
85 }
86 for (int i = 0; i < totalNum; i++) {
87 printf("read\n");
88 char sentence1[15] = "Hello World";
89 char a[2] = {0};
90 (void)sprintf_s(a, sizeof(a), "%d", i);
91 (void)strcat_s(sentence1, 15, a); // 15, sizeof sentence1
92 (void)memset_s(readbuffer, sizeof(readbuffer), 0, sizeof(readbuffer));
93 retValue = read(*readFd, readbuffer, strlen(sentence1) + 1);
94 printf("Receive %d bytes data : %s, errno : %d\n", retValue, readbuffer, errno);
95 ICUNIT_ASSERT_SIZE_STRING_EQUAL(readbuffer, sentence1, strlen(sentence1), errno);
96 }
97 }
98 ret = waitpid(pid, &status, 0);
99 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
100 ret = close(pipefd[0]);
101 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
102
103 return 0;
104 }
105
106
ItPosixPipe002(void)107 void ItPosixPipe002(void)
108 {
109 TEST_ADD_CASE(__FUNCTION__, TestPipeMultiProcess, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
110 }
111