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 MQUEUE_STANDARD_NAME_LENGTH_MAX = 102500;
43 static const int g_buff = 512;
44 static const int MQ_MAX_LIMIT_COUNT = 10;
45
FreeResource(mqd_t * mqueue,int index,char * mqname)46 static int FreeResource(mqd_t *mqueue, int index, char *mqname)
47 {
48 int ret = -1;
49 for (int k = 0; k < index; ++k) {
50 ret = snprintf_s(mqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d",
51 k, LosCurTaskIDGet());
52 if (ret < 0) {
53 return -1; /* 1: errno */
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
ItProcessPlimitsIpc013(void)68 void ItProcessPlimitsIpc013(void)
69 {
70 INT32 ret;
71 mode_t mode;
72 CHAR mqname[MQUEUE_STANDARD_NAME_LENGTH] = "";
73 mqd_t mqueue[g_buff];
74 std::string subPlimitsPath = "/proc/plimits/test";
75 std::string configFileMqCount = "/proc/plimits/test/ipc.mq_limit";
76 std::string limitMqCount = "11";
77 struct mq_attr attr = { 0 };
78 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
79 attr.mq_maxmsg = 1;
80 struct mq_attr attr_err = { 0 };
81 attr_err.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH_MAX;
82 attr_err.mq_maxmsg = 1;
83 int index = 0;
84
85 ret = mkdir(subPlimitsPath.c_str(), S_IFDIR | mode);
86 ASSERT_EQ(ret, 0);
87
88 ret = WriteFile(configFileMqCount.c_str(), limitMqCount.c_str());
89 ASSERT_NE(ret, -1);
90
91 for (index = 0; index < MQ_MAX_LIMIT_COUNT; ++index) {
92 ret = snprintf_s(mqname, sizeof(mqname), MQUEUE_STANDARD_NAME_LENGTH, "/mq_006_%d_%d",
93 index, LosCurTaskIDGet());
94 ASSERT_NE(ret, -1);
95 mqueue[index] = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
96 ASSERT_NE(mqueue[index], (mqd_t)-1);
97 (void)memset_s(mqname, sizeof(mqname), 0, sizeof(mqname));
98 }
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_err);
103 ASSERT_EQ(mqueue[index], (mqd_t)-1);
104 (void)memset_s(mqname, sizeof(mqname), 0, sizeof(mqname));
105 ret = FreeResource(mqueue, index, mqname);
106 ASSERT_EQ(ret, 0);
107
108 ret = rmdir(subPlimitsPath.c_str());
109 ASSERT_EQ(ret, 0);
110 return;
111 }
112