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
ChildFun(void * p)32 static int ChildFun(void *p)
33 {
34 (void)p;
35 int ret = 0;
36 int val, currPolicy;
37 struct sched_param param = { 0 };
38 const int DEFALUTE_PRIORITY = 31;
39 int testProcessPri = 0;
40 int currProcessPri = getpriority(PRIO_PROCESS, getpid());
41 if ((currProcessPri > DEFALUTE_PRIORITY) || (currProcessPri < 0)) {
42 return EXIT_CODE_ERRNO_1;
43 }
44
45 testProcessPri = currProcessPri + 1;
46 currPolicy = sched_getscheduler(getpid());
47 if (currPolicy != SCHED_RR) {
48 return EXIT_CODE_ERRNO_2;
49 }
50
51 val = getpriority(PRIO_PROCESS, 0);
52 if (val != currProcessPri) {
53 return EXIT_CODE_ERRNO_3;
54 }
55
56 ret = sched_getparam(0, ¶m);
57 if ((ret != 0) || (param.sched_priority != currProcessPri)) {
58 return EXIT_CODE_ERRNO_4;
59 }
60
61 val = sched_getscheduler(0);
62 if (val != currPolicy) {
63 return EXIT_CODE_ERRNO_5;
64 }
65
66 ret = setpriority(PRIO_PROCESS, 0, testProcessPri);
67 if (ret != 0) {
68 return EXIT_CODE_ERRNO_6;
69 }
70
71 ret = getpriority(PRIO_PROCESS, 0);
72 if (ret != testProcessPri) {
73 return EXIT_CODE_ERRNO_7;
74 }
75
76 param.sched_priority = currProcessPri;
77 ret = sched_setparam(0, ¶m);
78 if (ret != 0) {
79 return EXIT_CODE_ERRNO_8;
80 }
81
82 ret = getpriority(PRIO_PROCESS, getpid());
83 if (ret != currProcessPri) {
84 return EXIT_CODE_ERRNO_9;
85 }
86
87 ret = sched_setscheduler(0, SCHED_FIFO, ¶m);
88 if ((ret != -1) || (errno != EINVAL)) {
89 return EXIT_CODE_ERRNO_10;
90 }
91
92 ret = sched_setscheduler(0, SCHED_RR, ¶m);
93 if (ret != 0) {
94 return EXIT_CODE_ERRNO_11;
95 }
96
97 ret = sched_setscheduler(1, SCHED_FIFO, ¶m);
98 if ((ret != -1) || (errno != EINVAL)) {
99 return EXIT_CODE_ERRNO_12;
100 }
101
102 ret = sched_setscheduler(2, SCHED_FIFO, ¶m); // 2, input the pid.
103 if ((ret != -1) || (errno != ESRCH)) {
104 return EXIT_CODE_ERRNO_13;
105 }
106
107 ret = sched_setparam(1, ¶m);
108 if (ret != 0) {
109 return EXIT_CODE_ERRNO_14;
110 }
111
112 ret = sched_setparam(2, ¶m); // 2, set the param.
113 if ((ret != -1) || (errno != ESRCH)) {
114 return EXIT_CODE_ERRNO_15;
115 }
116
117 ret = setpriority(PRIO_PROCESS, 1, testProcessPri);
118 if (ret != 0) {
119 return EXIT_CODE_ERRNO_16;
120 }
121
122 ret = setpriority(PRIO_PROCESS, 2, testProcessPri); // 2, Used to calculate priorities.
123 if ((ret != -1) || (errno != ESRCH)) {
124 return EXIT_CODE_ERRNO_255;
125 }
126
127 return 0;
128 }
129
ItPidContainer014(void)130 void ItPidContainer014(void)
131 {
132 int status;
133 int ret;
134 void *pstk = malloc(STACK_SIZE);
135 ASSERT_TRUE(pstk != NULL);
136 int childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
137 free(pstk);
138 ASSERT_NE(childPid, -1);
139
140 ret = waitpid(childPid, &status, 0);
141 ASSERT_EQ(ret, childPid);
142 ret = WIFEXITED(status);
143 ASSERT_NE(ret, 0);
144 ret = WEXITSTATUS(status);
145 ASSERT_EQ(ret, 0);
146 }
147