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 <fcntl.h>
31 #include <cstdio>
32 #include <cstdlib>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <cstring>
37 #include <mqueue.h>
38 #include <gtest/gtest.h>
39 #include "It_process_plimits.h"
40
41 static const int MQUEUE_STANDARD_NAME_LENGTH = 50;
42 static const int g_buffSize = 512;
43 static const int g_arryLen = 4;
44 static const int g_readLen = 254;
45 static const int MQ_MAX_LIMIT_COUNT = 10;
46
FreeResource(mqd_t * mqueue,int index,char * mqname)47 static int FreeResource(mqd_t *mqueue, int index, char *mqname)
48 {
49 int ret = -1;
50 for (int k = 0; k < index; ++k) {
51 if (snprintf_s(mqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d",
52 k, LosCurTaskIDGet()) < 0) {
53 return -1;
54 }
55 ret = mq_close(mqueue[k]);
56 if (ret != 0) {
57 return -2; /* 2: errno */
58 }
59 ret = mq_unlink(mqname);
60 if (ret != 0) {
61 return -3; /* 3: errno */
62 }
63 (void)memset_s(mqname, MQUEUE_STANDARD_NAME_LENGTH, 0, MQUEUE_STANDARD_NAME_LENGTH);
64 }
65 return ret;
66 }
67
68 // plimits_ipc_07, plimits_ipc_14, plimits_ipc_17
ItProcessPlimitsIpc006(void)69 void ItProcessPlimitsIpc006(void)
70 {
71 INT32 ret;
72 mode_t mode;
73 char buf[g_buffSize] = { 0 };
74 char *array[g_arryLen] = { nullptr };
75 int mqSuccessCount = -1, mqFailedCount = -1;
76 CHAR mqname[MQUEUE_STANDARD_NAME_LENGTH] = "";
77 mqd_t mqueue[g_readLen];
78 std::string subPlimitsPath = "/proc/plimits/test";
79 std::string configFileMqCount = "/proc/plimits/test/ipc.mq_limit";
80 std::string limitMqCount = "10";
81 struct mq_attr attr = { 0 };
82 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
83 attr.mq_maxmsg = 1;
84 int index = 0;
85
86 ret = mkdir(subPlimitsPath.c_str(), S_IFDIR | mode);
87 ASSERT_EQ(ret, 0);
88
89 ret = WriteFile(configFileMqCount.c_str(), limitMqCount.c_str());
90 ASSERT_NE(ret, -1);
91
92 for (index = 0; index < MQ_MAX_LIMIT_COUNT; ++index) {
93 ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d",
94 index, LosCurTaskIDGet());
95 ASSERT_NE(ret, -1);
96 mqueue[index] = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
97 ASSERT_NE(mqueue[index], (mqd_t)-1);
98 (void)memset_s(mqname, sizeof(mqname), 0, sizeof(mqname));
99 }
100 ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d", index, LosCurTaskIDGet());
101 ASSERT_NE(ret, -1);
102 mqueue[index] = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
103 ASSERT_EQ(mqueue[index], (mqd_t)-1);
104 (void)memset_s(mqname, sizeof(mqname), 0, sizeof(mqname));
105
106 ret = ReadFile("/proc/plimits/test/ipc.stat", buf);
107 printf("/proc/plimits/test/ipc.stat: %s\n", buf);
108 GetLine(buf, g_arryLen, g_readLen, array);
109 mqSuccessCount = atoi(array[0] + strlen("mq count: "));
110 mqFailedCount = atoi((array[1] + strlen("mq failed count: ")));
111 ASSERT_EQ(mqSuccessCount, MQ_MAX_LIMIT_COUNT);
112 ASSERT_EQ(mqFailedCount, 1);
113 ret = FreeResource(mqueue, index, mqname);
114 ASSERT_EQ(ret, 0);
115
116 ret = rmdir(subPlimitsPath.c_str());
117 ASSERT_EQ(ret, 0);
118 return;
119 }
120