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 CMDLINE_VALUE_LEN_MAX 512 57 #define STDERR_HANDLE 2 58 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) 59 #define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline" 60 61 #define FIRST_VALUE "First_Value" // 取第一个匹配值 62 #define LAST_VALUE "Last_Value" // 取最后一个匹配值 63 #define EMPTY_VALUE "EMPTY_VALUE" // 不取任何一个 64 65 #ifndef OHOS_LITE 66 void SetBootCompleted(bool isBootCompleted); 67 bool IsBootCompleted(void); 68 #endif 69 70 uid_t DecodeUid(const char *name); 71 gid_t DecodeGid(const char *name); 72 char *ReadFileToBuf(const char *configFile); 73 int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length); 74 char *ReadFileData(const char *fileName); 75 76 typedef struct INIT_TIMING_STAT { 77 struct timespec startTime; 78 struct timespec endTime; 79 } INIT_TIMING_STAT; 80 81 typedef struct NameValuePair { 82 const char *name; 83 const char *nameEnd; 84 const char *value; 85 const char *valueEnd; 86 } NAME_VALUE_PAIR; 87 int IterateNameValuePairs(const char *src, void (*iterator)(const NAME_VALUE_PAIR *nv, void *context), void *context); 88 89 int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum); 90 long long InitDiffTime(INIT_TIMING_STAT *stat); 91 void WaitForFile(const char *source, unsigned int maxSecond); 92 size_t WriteAll(int fd, const char *buffer, size_t size); 93 char *GetRealPath(const char *source); 94 int StringToInt(const char *str, int defaultValue); 95 int StringToUint(const char *name, unsigned int *value); 96 int MakeDirRecursive(const char *dir, mode_t mode); 97 void CheckAndCreateDir(const char *fileName); 98 int CheckAndCreatFile(const char *file, mode_t mode); 99 int MakeDir(const char *dir, mode_t mode); 100 int ReadFileInDir(const char *dirPath, const char *includeExt, 101 int (*processFile)(const char *fileName, void *context), void *context); 102 char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount); 103 void FreeStringVector(char **vector, int count); 104 int InUpdaterMode(void); 105 int InRescueMode(void); 106 int StringReplaceChr(char *strl, char oldChr, char newChr); 107 108 int OpenConsole(void); 109 int OpenKmsg(void); 110 void TrimTail(char *str, char c); 111 char *TrimHead(char *str, char c); 112 113 INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime); 114 115 INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out); 116 INIT_LOCAL_API int StringToLL(const char *str, long long int *out); 117 void CloseStdio(void); 118 119 int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize); 120 int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen); 121 122 /** 123 * @brief Get param value from /proc/cmdline by exact match 124 * 125 * @param paramName the name of param 126 * @param value the array to receive the value of paramName 127 * @param valueLen the length of value 128 * @return return 0 if succeed; return -1 if not found; return 1 if conflicted. 129 */ 130 int GetExactParameterFromCmdLine(const char *paramName, char *value, size_t valueLen); 131 132 /** 133 * @brief Get string index from a string array 134 * 135 * @param strArray string array 136 * Attension: last item in the array must be NULL, for example: 137 * const char *strArray[] = { "val1", "val2", NULL } 138 * @param target string to be matched 139 * @param ignoreCase 0 means exact match, others mean ignore case 140 * @return return 0 if succeed; other values if failed. 141 */ 142 int OH_StrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase); 143 144 /** 145 * @brief Get string index from a string array with extended strings 146 * 147 * @param strArray string array 148 * Attension: last item in the array must be NULL, for example: 149 * const char *strArray[] = { "val1", "val2", NULL } 150 * @param target string to be matched 151 * @param ignoreCase 0 means exact match, others mean ignore case 152 * @param extend optional extended strings array, last string must be NULL 153 * @return return 0 if succeed; other values if failed. 154 */ 155 int OH_ExtendableStrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase, const char *extend[]); 156 157 /** 158 * @brief Get string dictionary from a string dictionary array 159 * 160 * @param strDict string dictionary array 161 * Attension: last item in the array must be NULL, for example: 162 * Each item must be a structure with "const char *" as the first element 163 * For example: 164 * typedef { 165 * const char *key; // First element must be "const char *" 166 * const char *value; // Arbitrary elements 167 * // Optionally add more elements 168 * } STRING_DICT_ST; 169 * @param target string to be matched 170 * @param ignoreCase 0 means exact match, others mean ignore case 171 * @return return item pointer if succeed; NULL if failed 172 * @example 173 * // Define a name-value pair as dictionary item 174 * typedef struct { 175 * const char *name; 176 * const char *value; 177 * } NAME_VALUE_ST; 178 179 * // Fill the dictionary values 180 * NAME_VALUE_ST dict[] = { { "key1", "val1" }, { "key2", "val2" }}; 181 182 * // Find by key name 183 * NAME_VALUE_ST *found = (NAME_VALUE_ST *)StrDictGetIndex((void **)dict, sizeof(NAME_VALUE_ST), "key1", FALSE); 184 */ 185 void *OH_StrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase); 186 187 /** 188 * @brief Get string dictionary from a string dictionary array and extended string dictionary 189 * 190 * @param strDict string dictionary array 191 * Attension: last item in the array must be NULL, for example: 192 * Each item must be a structure with "const char *" as the first element 193 * For example: 194 * typedef { 195 * const char *key; // First element must be "const char *" 196 * const char *value; // Arbitrary elements 197 * // Optionally add more elements 198 * } STRING_DICT_ST; 199 * @param target string to be matched 200 * @param ignoreCase 0 means exact match, others mean ignore case 201 * @param extendStrDict optional extended strings dictionary array, last item must be NULL 202 * @return return item pointer if succeed; NULL if failed. 203 */ 204 void *OH_ExtendableStrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase, void **extendStrDict); 205 206 long long GetUptimeInMicroSeconds(const struct timespec *uptime); 207 208 #ifdef __cplusplus 209 #if __cplusplus 210 } 211 #endif 212 #endif 213 #endif // INIT_UTILS_H 214