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_shm.h"
32 #include "sys/types.h"
33 #include <sched.h>
34 #include <stdio.h>
35 #include <signal.h>
36 #include <sys/ipc.h>
37 #include <sys/shm.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <errno.h>
41
42 static int *g_shmptr = NULL;
43
ChildProcess(void)44 static void ChildProcess(void)
45 {
46 struct sched_param param;
47
48 param.sched_priority = sched_get_priority_max(SCHED_RR);
49 if(sched_setparam(getpid(), ¶m) != 0) {
50 printf("An error occurs when calling sched_setparam()");
51 return;
52 }
53
54 /* to avoid blocking */
55 alarm(2);
56 while(1);
57 }
58
TestProcess(void)59 static void TestProcess(void)
60 {
61 /* to avoid blocking */
62 alarm(5);
63
64 while(1) {
65 (*g_shmptr)++;
66 sched_yield();
67 }
68 }
69
ExitChildren(int sig)70 static void ExitChildren(int sig)
71 {
72 exit(0);
73 }
74
KillChildren(int childPid)75 static void KillChildren(int childPid)
76 {
77 kill(childPid, SIGTERM);
78 sleep(1); //wait for kill child finish.
79 }
80
Testcase(void)81 static int Testcase(void)
82 {
83 int childPid, oldcount, newcount, shmid;
84 struct sched_param param = {0};
85 struct sched_param paramCopy = {0};
86 int processPolicy = 0;
87 int threadPrio = 0;
88 int ret;
89 int pid;
90
91 void *ptr = reinterpret_cast<void *>(signal(SIGTERM, ExitChildren));
92 ICUNIT_ASSERT_NOT_EQUAL(ptr, NULL, ptr);
93
94 shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0600);
95 ICUNIT_ASSERT_NOT_EQUAL(shmid, -1, shmid);
96
97 g_shmptr = (int *)shmat(shmid, 0, 0);
98 ICUNIT_ASSERT_NOT_EQUAL(g_shmptr, reinterpret_cast<int *>(-1), g_shmptr);
99
100 *g_shmptr = 0;
101
102 processPolicy = sched_getscheduler(getpid());
103 ret = sched_getparam(getpid(), ¶mCopy);
104 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
105
106 param.sched_priority = (sched_get_priority_min(SCHED_RR) +
107 sched_get_priority_max(SCHED_RR)) / 2;
108 ret = sched_setscheduler(getpid(), SCHED_RR, ¶m);
109 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
110
111 ret = pthread_getschedparam(pthread_self(), &processPolicy, ¶m);
112 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
113
114 threadPrio = param.sched_priority;
115 ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
116 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
117
118 childPid = fork();
119 ICUNIT_GOTO_NOT_EQUAL(childPid, -1, childPid, OUT_SCHEDULER);
120
121 if (childPid == 0) {
122 TestProcess();
123 exit(0);
124 }
125 sleep(1);
126
127 param.sched_priority = sched_get_priority_min(SCHED_RR);
128 oldcount = *g_shmptr;
129 ret = sched_setparam(childPid, ¶m);
130 ICUNIT_GOTO_EQUAL(ret, 0, ret, OUT_SCHEDULER);
131
132 ret = 1;
133 newcount = *g_shmptr;
134 ICUNIT_GOTO_NOT_EQUAL(oldcount, newcount, newcount, OUT);
135
136 ret = 0;
137 OUT:
138 KillChildren(childPid);
139 pid = waitpid(childPid, NULL, 0);
140 ICUNIT_ASSERT_EQUAL(pid, childPid, pid);
141 (void)sched_setparam(getpid(), ¶mCopy);
142 OUT_SCHEDULER:
143 (void)sched_setscheduler(getpid(), processPolicy, ¶mCopy);
144 param.sched_priority = threadPrio;
145 pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
146
147 ret = shmdt(g_shmptr);
148 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
149
150 ret = shmctl(shmid, IPC_RMID, NULL);
151 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
152
153 return ret;
154 }
155
ItTestShm009(void)156 void ItTestShm009(void)
157 {
158 TEST_ADD_CASE("IT_MEM_SHM_009", Testcase, TEST_LOS, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
159 }
160