1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INIT_UTILS_H 17 #define INIT_UTILS_H 18 #include <fcntl.h> 19 #include <sys/stat.h> 20 #include <stdint.h> 21 #include <stdbool.h> 22 #include <unistd.h> 23 #include <time.h> 24 25 #include "beget_ext.h" 26 27 #ifndef STARTUP_INIT_TEST 28 #ifndef INIT_STATIC 29 #define INIT_STATIC static 30 #endif 31 #else 32 #ifndef INIT_STATIC 33 #define INIT_STATIC 34 #endif 35 #endif 36 37 #ifdef __cplusplus 38 #if __cplusplus 39 extern "C" { 40 #endif 41 #endif 42 43 typedef struct { 44 char *name; 45 int value; 46 } InitArgInfo; 47 48 #define BASE_MS_UNIT 1000 49 #define MAX_INT_LEN 20 50 #define HEX_BASE 16 51 #define BINARY_BASE 2 52 #define OCTAL_BASE 8 53 #define DECIMAL_BASE 10 54 #define WAIT_MAX_SECOND 5 55 #define MAX_BUFFER_LEN 256 56 #define REPAIR_MODE_LEN 256 57 #define MAINTENANCE_RECOVERY "2" 58 #define MAINTENANCE_RECOVERY_TYPE 2 59 #define MAINTENANCE_RECOVERY_COMPLETE "3" 60 #define MAINTENANCE_RECOVERY_COMPLETE_TYPE 3 61 #define CMDLINE_VALUE_LEN_MAX 512 62 #define STDERR_HANDLE 2 63 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) 64 #define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline" 65 66 #define FIRST_VALUE "First_Value" // 取第一个匹配值 67 #define LAST_VALUE "Last_Value" // 取最后一个匹配值 68 #define EMPTY_VALUE "EMPTY_VALUE" // 不取任何一个 69 70 #ifndef OHOS_LITE 71 void SetBootCompleted(bool isBootCompleted); 72 bool IsBootCompleted(void); 73 #endif 74 75 uid_t DecodeUid(const char *name); 76 gid_t DecodeGid(const char *name); 77 char *ReadFileToBuf(const char *configFile); 78 int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length); 79 char *ReadFileData(const char *fileName); 80 81 typedef struct INIT_TIMING_STAT { 82 struct timespec startTime; 83 struct timespec endTime; 84 } INIT_TIMING_STAT; 85 86 typedef struct NameValuePair { 87 const char *name; 88 const char *nameEnd; 89 const char *value; 90 const char *valueEnd; 91 } NAME_VALUE_PAIR; 92 int IterateNameValuePairs(const char *src, void (*iterator)(const NAME_VALUE_PAIR *nv, void *context), void *context); 93 94 int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum); 95 long long InitDiffTime(INIT_TIMING_STAT *stat); 96 void WaitForFile(const char *source, unsigned int maxSecond); 97 size_t WriteAll(int fd, const char *buffer, size_t size); 98 char *GetRealPath(const char *source); 99 int StringToInt(const char *str, int defaultValue); 100 int StringToUint(const char *name, unsigned int *value); 101 int MakeDirRecursive(const char *dir, mode_t mode); 102 void CheckAndCreateDir(const char *fileName); 103 int CheckAndCreatFile(const char *file, mode_t mode); 104 int MakeDir(const char *dir, mode_t mode); 105 int ReadFileInDir(const char *dirPath, const char *includeExt, 106 int (*processFile)(const char *fileName, void *context), void *context); 107 char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount); 108 void FreeStringVector(char **vector, int count); 109 int InUpdaterMode(void); 110 int InRescueMode(void); 111 int InRepairMode(void); 112 bool IsPenglaiMode(void); 113 int StringReplaceChr(char *strl, char oldChr, char newChr); 114 115 int OpenConsole(void); 116 int OpenKmsg(void); 117 void TrimTail(char *str, char c); 118 char *TrimHead(char *str, char c); 119 120 INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime); 121 122 INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out); 123 INIT_LOCAL_API int StringToLL(const char *str, long long int *out); 124 void CloseStdio(void); 125 126 int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize); 127 int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen); 128 129 /** 130 * @brief Get param value from /proc/cmdline by exact match 131 * 132 * @param paramName the name of param 133 * @param value the array to receive the value of paramName 134 * @param valueLen the length of value 135 * @return return 0 if succeed; return -1 if not found; return 1 if conflicted. 136 */ 137 int GetExactParameterFromCmdLine(const char *paramName, char *value, size_t valueLen); 138 139 /** 140 * @brief Get string index from a string array 141 * 142 * @param strArray string array 143 * Attension: last item in the array must be NULL, for example: 144 * const char *strArray[] = { "val1", "val2", NULL } 145 * @param target string to be matched 146 * @param ignoreCase 0 means exact match, others mean ignore case 147 * @return return 0 if succeed; other values if failed. 148 */ 149 int OH_StrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase); 150 151 /** 152 * @brief Get string index from a string array with extended strings 153 * 154 * @param strArray string array 155 * Attension: last item in the array must be NULL, for example: 156 * const char *strArray[] = { "val1", "val2", NULL } 157 * @param target string to be matched 158 * @param ignoreCase 0 means exact match, others mean ignore case 159 * @param extend optional extended strings array, last string must be NULL 160 * @return return 0 if succeed; other values if failed. 161 */ 162 int OH_ExtendableStrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase, const char *extend[]); 163 164 /** 165 * @brief Get string dictionary from a string dictionary array 166 * 167 * @param strDict string dictionary array 168 * Attension: last item in the array must be NULL, for example: 169 * Each item must be a structure with "const char *" as the first element 170 * For example: 171 * typedef { 172 * const char *key; // First element must be "const char *" 173 * const char *value; // Arbitrary elements 174 * // Optionally add more elements 175 * } STRING_DICT_ST; 176 * @param target string to be matched 177 * @param ignoreCase 0 means exact match, others mean ignore case 178 * @return return item pointer if succeed; NULL if failed 179 * @example 180 * // Define a name-value pair as dictionary item 181 * typedef struct { 182 * const char *name; 183 * const char *value; 184 * } NAME_VALUE_ST; 185 186 * // Fill the dictionary values 187 * NAME_VALUE_ST dict[] = { { "key1", "val1" }, { "key2", "val2" }}; 188 189 * // Find by key name 190 * NAME_VALUE_ST *found = (NAME_VALUE_ST *)StrDictGetIndex((void **)dict, sizeof(NAME_VALUE_ST), "key1", FALSE); 191 */ 192 void *OH_StrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase); 193 194 /** 195 * @brief Get string dictionary from a string dictionary array and extended string dictionary 196 * 197 * @param strDict string dictionary array 198 * Attension: last item in the array must be NULL, for example: 199 * Each item must be a structure with "const char *" as the first element 200 * For example: 201 * typedef { 202 * const char *key; // First element must be "const char *" 203 * const char *value; // Arbitrary elements 204 * // Optionally add more elements 205 * } STRING_DICT_ST; 206 * @param target string to be matched 207 * @param ignoreCase 0 means exact match, others mean ignore case 208 * @param extendStrDict optional extended strings dictionary array, last item must be NULL 209 * @return return item pointer if succeed; NULL if failed. 210 */ 211 void *OH_ExtendableStrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase, void **extendStrDict); 212 213 long long GetUptimeInMicroSeconds(const struct timespec *uptime); 214 215 #ifdef __cplusplus 216 #if __cplusplus 217 } 218 #endif 219 #endif 220 #endif // INIT_UTILS_H 221