1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "It_container_test.h"
31 using namespace std;
32
33 static const char *containerType = "ipc";
34
35 struct shared_use_st {
36 char test[SHM_TEST_DATA_SIZE];
37 };
38
childFunc1(void *arg)39 static int childFunc1(void *arg)
40 {
41 struct shared_use_st *shared = NULL;
42 const char testBuf[] = "child test shm";
43 int ret;
44 (void)arg;
45
46 int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
47 if (shmid == -1) {
48 return EXIT_CODE_ERRNO_1;
49 }
50
51 void *shm = shmat(shmid, 0, 0);
52 if (shm == reinterpret_cast<void *>(-1)) {
53 shmctl(shmid, IPC_RMID, 0);
54 return EXIT_CODE_ERRNO_2;
55 }
56 shared = (struct shared_use_st *)shm;
57 ret = strncmp(shared->test, testBuf, strlen(testBuf));
58 if (ret != 0) {
59 shmdt(shm);
60 shmctl(shmid, IPC_RMID, 0);
61 return EXIT_CODE_ERRNO_3;
62 }
63 ret = shmdt(shm);
64 if (ret == -1) {
65 shmctl(shmid, IPC_RMID, 0);
66 return EXIT_CODE_ERRNO_4;
67 }
68 return 0;
69 }
70
childFunc(void *arg)71 static int childFunc(void *arg)
72 {
73 const char testBuf[] = "parent test shm";
74 const char testBuf1[] = "child test shm";
75 int ret, status, pid, exitCode;
76 (void)arg;
77 char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
78 if (stack == nullptr) {
79 return EXIT_CODE_ERRNO_17;
80 }
81 char *stackTop = stack + STACK_SIZE;
82 auto linkBuffer = ReadlinkContainer(getpid(), containerType);
83
84 int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
85 if (shmid == -1) {
86 return EXIT_CODE_ERRNO_1;
87 }
88
89 void *shm = shmat(shmid, 0, 0);
90 if (shm == reinterpret_cast<void *>(-1)) {
91 shmctl(shmid, IPC_RMID, 0);
92 return EXIT_CODE_ERRNO_2;
93 }
94
95 struct shared_use_st *shared = (struct shared_use_st *)shm;
96 ret = strncmp(shared->test, testBuf, strlen(testBuf));
97 if (ret == 0) {
98 ret = EXIT_CODE_ERRNO_3;
99 goto EXIT;
100 }
101
102 ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf1, sizeof(testBuf1));
103 if (ret != 0) {
104 ret = EXIT_CODE_ERRNO_4;
105 goto EXIT;
106 }
107
108 pid = clone(childFunc1, stackTop, SIGCHLD, &arg);
109 if (pid == -1) {
110 ret = EXIT_CODE_ERRNO_5;
111 goto EXIT;
112 }
113
114 ret = waitpid(pid, &status, 0);
115 if (ret != pid) {
116 ret = EXIT_CODE_ERRNO_6;
117 goto EXIT;
118 }
119
120 ret = WIFEXITED(status);
121 if (ret == 0) {
122 ret = EXIT_CODE_ERRNO_7;
123 goto EXIT;
124 }
125
126 exitCode = WEXITSTATUS(status);
127 if (exitCode != 0) {
128 ret = EXIT_CODE_ERRNO_8;
129 goto EXIT;
130 }
131
132 ret = shmdt(shm);
133 if (ret == -1) {
134 shmctl(shmid, IPC_RMID, 0);
135 return EXIT_CODE_ERRNO_9;
136 }
137
138 ret = shmctl(shmid, IPC_RMID, 0);
139 if (ret == -1) {
140 return EXIT_CODE_ERRNO_10;
141 }
142
143 return 0;
144 EXIT:
145 shmdt(shm);
146 shmctl(shmid, IPC_RMID, 0);
147 return ret;
148 }
149
ItIpcContainer004(void)150 void ItIpcContainer004(void)
151 {
152 const char testBuf[] = "parent test shm";
153 int pid, exitCode, status, ret;
154 void *shm = NULL;
155 struct shmid_ds ds = {};
156 struct shminfo info = {};
157
158 int arg = CHILD_FUNC_ARG;
159 char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
160 ASSERT_NE(stack, nullptr);
161 char *stackTop = stack + STACK_SIZE;
162
163 int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
164 ShmFinalizer ShmFinalizer(shm, shmid);
165 ASSERT_NE(shmid, -1);
166
167 shm = shmat(shmid, 0, 0);
168 ASSERT_NE((int)shm, -1);
169
170 struct shared_use_st *shared = (struct shared_use_st *)shm;
171 ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
172 ASSERT_EQ(ret, 0);
173
174 pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
175 ASSERT_NE(pid, -1);
176
177 ret = waitpid(pid, &status, 0);
178 ASSERT_EQ(ret, pid);
179
180 ret = WIFEXITED(status);
181 ASSERT_NE(ret, 0);
182
183 exitCode = WEXITSTATUS(status);
184 ASSERT_EQ(exitCode, 0);
185
186 ret = shmctl(shmid, IPC_STAT, &ds);
187 ASSERT_EQ(ret, 0);
188 ASSERT_EQ(ds.shm_segsz, PAGE_SIZE);
189 ASSERT_EQ(ds.shm_nattch, 1);
190 ASSERT_EQ(ds.shm_cpid, getpid());
191 ASSERT_EQ(ds.shm_lpid, getpid());
192 ASSERT_EQ(ds.shm_perm.uid, getuid());
193
194 ret = shmctl(shmid, SHM_STAT, &ds);
195 ASSERT_NE(ret, -1);
196 ASSERT_NE(ret, 0);
197
198 ds.shm_perm.uid = getuid();
199 ds.shm_perm.gid = getgid();
200 ds.shm_perm.mode = 0;
201 ret = shmctl(shmid, IPC_SET, &ds);
202 ASSERT_EQ(ret, 0);
203
204 ret = shmctl(shmid, IPC_INFO, (struct shmid_ds *)&info);
205 ASSERT_EQ(ret, 192); /* 192: test value */
206 ASSERT_EQ(info.shmmax, 0x1000000); /* 0x1000000: Shared memory information */
207 ASSERT_EQ(info.shmmin, 1); /* 1: Shared memory information */
208 ASSERT_EQ(info.shmmni, 192); /* 192: Shared memory information */
209 ASSERT_EQ(info.shmseg, 128); /* 128: Shared memory information */
210 ASSERT_EQ(info.shmall, 0x1000); /* 0x1000: Shared memory information */
211
212 ret = shmdt(shm);
213 ASSERT_NE(ret, -1);
214
215 ret = shmctl(shmid, IPC_RMID, NULL);
216 ASSERT_EQ(ret, 0);
217 }
218