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
39 static int g_pidGroup[10] = {0};
40 static int g_index = 0;
41 static int g_sleepTime = 5;
42
PidWrite(const char * filepath,char * buf)43 static int PidWrite(const char *filepath, char *buf)
44 {
45 int fd = open(filepath, O_WRONLY);
46 size_t ret = write(fd, buf, strlen(buf));
47 close(fd);
48 return ret;
49 }
50
PidFork(int index)51 static void PidFork(int index)
52 {
53 pid_t fpid = fork();
54 if (fpid == 0) { // children proc
55 sleep(g_sleepTime);
56 exit(0);
57 } else if (fpid > 0) {
58 g_pidGroup[index] = fpid;
59 return;
60 }
61 }
62
ItProcessPlimitsPid002(void)63 void ItProcessPlimitsPid002(void)
64 {
65 mode_t mode = 0;
66 int status;
67 const char *pidMaxPath = "/proc/plimits/test/pids.max";
68 const char *procsPath = "/proc/plimits/test/plimits.procs";
69 std::string path = "/proc/plimits/test";
70 char writeBuf[3];
71 int num = 4;
72 int i = 0;
73 pid_t fpid;
74 int pidnum = 5;
75 int ret = mkdir(path.c_str(), S_IFDIR | mode);
76 ASSERT_EQ(ret, 0);
77
78 (void)memset_s(writeBuf, sizeof(writeBuf), 0, sizeof(writeBuf));
79 ret = sprintf_s(writeBuf, sizeof(writeBuf), "%d", pidnum);
80 ASSERT_NE(ret, -1);
81 ret = PidWrite(pidMaxPath, writeBuf);
82 ASSERT_NE(ret, -1);
83
84 (void)memset_s(writeBuf, sizeof(writeBuf), 0, sizeof(writeBuf));
85 ret = sprintf_s(writeBuf, sizeof(writeBuf), "%d", getpid());
86 ASSERT_NE(ret, -1);
87 ret = PidWrite(procsPath, writeBuf);
88 ASSERT_NE(ret, -1);
89
90 for (i = g_index; i < num; ++i) {
91 PidFork(i);
92 }
93 g_index = i;
94 fpid = fork();
95 if (fpid == 0) {
96 exit(0);
97 } else if (fpid > 0) {
98 waitpid(fpid, &status, 0);
99 }
100
101 for (i = 0; i < g_index; ++i) {
102 waitpid(g_pidGroup[i], &status, 0);
103 ASSERT_EQ(WEXITSTATUS(status), 0);
104 }
105 ret = rmdir(path.c_str());
106 ASSERT_EQ(ret, 0);
107 return;
108 }
109