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