• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
31 #include <unistd.h>
32 #include <cstdlib>
33 #include <fcntl.h>
34 #include <vector>
35 #include <cstring>
36 #include <sys/wait.h>
37 #include "It_process_plimits.h"
38 
PidWrite(const char * filepath,char * buf)39 static int PidWrite(const char *filepath, char *buf)
40 {
41     int fd = open(filepath, O_WRONLY);
42     size_t ret = write(fd, buf, strlen(buf));
43     close(fd);
44     return ret;
45 }
46 
ItProcessPlimitsPid003(void)47 void ItProcessPlimitsPid003(void)
48 {
49     mode_t mode = 0;
50     int status;
51     struct sched_param param = { 0 };
52     const char *pidMaxPath = "/proc/plimits/test/pids.priority";
53     std::string path = "/proc/plimits/test";
54     char writeBuf[3];
55     int pidnum = 15; /* 15: priority limit */
56     int ret = mkdir(path.c_str(), S_IFDIR | mode);
57     ASSERT_EQ(ret, 0);
58 
59     (void)memset_s(writeBuf, sizeof(writeBuf), 0, sizeof(writeBuf));
60     ret = sprintf_s(writeBuf, sizeof(writeBuf), "%d", pidnum);
61     ASSERT_NE(ret, -1);
62 
63     ret = PidWrite(pidMaxPath, writeBuf);
64     ASSERT_NE(ret, -1);
65 
66     int currProcessPri = getpriority(PRIO_PROCESS, getpid());
67 
68     param.sched_priority = pidnum - 1;
69     ret = sched_setscheduler(getpid(), SCHED_RR, &param);
70     ASSERT_NE(ret, 0);
71 
72     ret = setpriority(PRIO_PROCESS, getpid(), param.sched_priority);
73     ASSERT_NE(ret, 0);
74 
75     ret = sched_setparam(getpid(), &param);
76     ASSERT_NE(ret, 0);
77 
78     param.sched_priority = currProcessPri + 1;
79     ret = sched_setscheduler(getpid(), SCHED_RR, &param);
80     ASSERT_EQ(ret, 0);
81 
82     param.sched_priority++;
83     ret = setpriority(PRIO_PROCESS, getpid(), param.sched_priority);
84     ASSERT_EQ(ret, 0);
85 
86     param.sched_priority++;
87     ret = sched_setparam(getpid(), &param);
88     ASSERT_EQ(ret, 0);
89 
90     ret = rmdir(path.c_str());
91     ASSERT_EQ(ret, 0);
92     return;
93 }
94