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 #ifndef _IT_CONTAINER_TEST_H 31 #define _IT_CONTAINER_TEST_H 32 33 #include <string> 34 #include <vector> 35 #include <iostream> 36 #include <sstream> 37 #include <regex> 38 #include <csignal> 39 #include <sys/syscall.h> 40 #include <sys/capability.h> 41 #include <cstring> 42 #include "osTest.h" 43 #include "mqueue.h" 44 #include "sys/time.h" 45 #include "sys/shm.h" 46 #include "sys/types.h" 47 48 const int EXIT_CODE_ERRNO_1 = 1; 49 const int EXIT_CODE_ERRNO_2 = 2; 50 const int EXIT_CODE_ERRNO_3 = 3; 51 const int EXIT_CODE_ERRNO_4 = 4; 52 const int EXIT_CODE_ERRNO_5 = 5; 53 const int EXIT_CODE_ERRNO_6 = 6; 54 const int EXIT_CODE_ERRNO_7 = 7; 55 const int EXIT_CODE_ERRNO_8 = 8; 56 const int EXIT_CODE_ERRNO_9 = 9; 57 const int EXIT_CODE_ERRNO_10 = 10; 58 const int EXIT_CODE_ERRNO_11 = 11; 59 const int EXIT_CODE_ERRNO_12 = 12; 60 const int EXIT_CODE_ERRNO_13 = 13; 61 const int EXIT_CODE_ERRNO_14 = 14; 62 const int EXIT_CODE_ERRNO_15 = 15; 63 const int EXIT_CODE_ERRNO_16 = 16; 64 const int EXIT_CODE_ERRNO_17 = 17; 65 const int EXIT_CODE_ERRNO_255 = 255; 66 const int CONTAINER_FIRST_PID = 1; 67 const int CONTAINER_SECOND_PID = 2; 68 const int CONTAINER_THIRD_PID = 3; 69 70 const int MQUEUE_TEST_SIZE = 50; 71 const int MQUEUE_TEST_MAX_MSG = 255; 72 73 const int SHM_TEST_DATA_SIZE = 1024; 74 const int SHM_TEST_KEY1 = 1234; 75 const int SHM_TEST_OPEN_PERM = 0666; 76 const int CLONE_STACK_MMAP_FLAG = MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK; 77 78 extern const char *USERDATA_DIR_NAME; 79 extern const char *ACCESS_FILE_NAME; 80 extern const char *MNT_ACCESS_FILE_NAME; 81 extern const char *USERDATA_DEV_NAME; 82 extern const char *FS_TYPE; 83 84 extern const int BIT_ON_RETURN_VALUE; 85 extern const int STACK_SIZE; 86 extern const int CHILD_FUNC_ARG; 87 88 const int MQUEUE_STANDARD_NAME_LENGTH = 255; 89 90 extern "C" { 91 #define CLONE_NEWTIME 0x00000080 92 } 93 94 int WriteFile(const char *filepath, const char *buf); 95 int ReadFile(const char *filepath, char *buf); 96 int GetLine(char *buf, int count, int maxLen, char **array); 97 98 int ChildFunction(void *args); 99 100 pid_t CloneWrapper(int (*func)(void *), int flag, void *args); 101 102 int WaitChild(pid_t pid, int *status, int errNo1, int errNo2); 103 104 int NetContainerResetNetAddr(const char *ifname, const char *ip, const char *netmask, const char *gw); 105 106 int NetContainerGetLocalIP(const char *ifname, char *ip, int ipLen); 107 108 std::string GenContainerLinkPath(int pid, const std::string& containerType); 109 110 extern std::string ReadlinkContainer(int pid, const std::string& containerType); 111 112 class MQueueFinalizer { 113 public: MQueueFinalizer(mqd_t mqueueParent,const std::string & mqname)114 explicit MQueueFinalizer(mqd_t mqueueParent, const std::string& mqname) 115 { 116 m_mqueueParent = mqueueParent; 117 m_mqname = mqname; 118 } ~MQueueFinalizer()119 ~MQueueFinalizer() 120 { 121 if (m_mqueueParent >= 0) { 122 mq_close(m_mqueueParent); 123 mq_unlink(m_mqname.c_str()); 124 } 125 } 126 private: 127 mqd_t m_mqueueParent; 128 std::string m_mqname; 129 }; 130 131 class ShmFinalizer { 132 public: ShmFinalizer(void * shm,int shmid)133 explicit ShmFinalizer(void* shm, int shmid) 134 { 135 m_shm = shm; 136 m_shmid = shmid; 137 } ~ShmFinalizer()138 ~ShmFinalizer() 139 { 140 shmdt(m_shm); 141 shmctl(m_shmid, IPC_RMID, nullptr); 142 } 143 private: 144 void* m_shm; 145 int m_shmid; 146 }; 147 148 void ItUserContainer001(void); 149 void ItUserContainer002(void); 150 void ItUserContainer003(void); 151 void ItUserContainer004(void); 152 void ItUserContainer005(void); 153 void ItUserContainer006(void); 154 void ItUserContainer007(void); 155 void ItContainer001(void); 156 void ItContainerChroot001(void); 157 void ItContainerChroot002(void); 158 void ItPidContainer023(void); 159 void ItPidContainer025(void); 160 void ItPidContainer026(void); 161 void ItPidContainer027(void); 162 void ItPidContainer028(void); 163 void ItPidContainer029(void); 164 void ItPidContainer030(void); 165 void ItPidContainer031(void); 166 void ItPidContainer032(void); 167 void ItPidContainer033(void); 168 void ItUtsContainer001(void); 169 void ItUtsContainer002(void); 170 void ItUtsContainer004(void); 171 void ItUtsContainer005(void); 172 void ItUtsContainer006(void); 173 void ItUtsContainer007(void); 174 void ItUtsContainer008(void); 175 void ItMntContainer001(void); 176 void ItMntContainer002(void); 177 void ItMntContainer003(void); 178 void ItMntContainer004(void); 179 void ItMntContainer005(void); 180 void ItMntContainer006(void); 181 void ItMntContainer007(void); 182 void ItMntContainer008(void); 183 void ItMntContainer009(void); 184 void ItMntContainer010(void); 185 void ItIpcContainer001(void); 186 void ItIpcContainer002(void); 187 void ItIpcContainer003(void); 188 void ItIpcContainer004(void); 189 void ItIpcContainer005(void); 190 void ItIpcContainer006(void); 191 void ItIpcContainer007(void); 192 void ItIpcContainer008(void); 193 void ItTimeContainer001(void); 194 void ItTimeContainer002(void); 195 void ItTimeContainer003(void); 196 void ItTimeContainer004(void); 197 void ItTimeContainer005(void); 198 void ItTimeContainer006(void); 199 void ItTimeContainer007(void); 200 void ItTimeContainer008(void); 201 void ItTimeContainer009(void); 202 void ItTimeContainer010(void); 203 void ItPidContainer001(void); 204 void ItPidContainer002(void); 205 void ItPidContainer003(void); 206 void ItPidContainer004(void); 207 void ItPidContainer005(void); 208 void ItPidContainer006(void); 209 void ItPidContainer007(void); 210 void ItPidContainer008(void); 211 void ItPidContainer009(void); 212 void ItPidContainer010(void); 213 void ItPidContainer011(void); 214 void ItPidContainer012(void); 215 void ItPidContainer013(void); 216 void ItPidContainer014(void); 217 void ItPidContainer015(void); 218 void ItPidContainer016(void); 219 void ItPidContainer017(void); 220 void ItPidContainer018(void); 221 void ItPidContainer019(void); 222 void ItPidContainer020(void); 223 void ItPidContainer021(void); 224 void ItPidContainer022(void); 225 void ItPidContainer024(void); 226 void ItUtsContainer003(void); 227 void ItNetContainer001(void); 228 void ItNetContainer002(void); 229 void ItNetContainer003(void); 230 void ItNetContainer004(void); 231 void ItNetContainer005(void); 232 void ItNetContainer006(void); 233 void ItNetContainer007(void); 234 void ItNetContainer008(void); 235 void ItNetContainer009(void); 236 void ItNetContainer010(void); 237 void ItNetContainer011(void); 238 void ItNetContainer012(void); 239 #endif /* _IT_CONTAINER_TEST_H */ 240