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 #ifdef __cplusplus 28 #if __cplusplus 29 extern "C" { 30 #endif 31 #endif 32 33 typedef struct { 34 char *name; 35 int value; 36 } InitArgInfo; 37 38 #define BASE_MS_UNIT 1000 39 #define MAX_INT_LEN 20 40 #define HEX_BASE 16 41 #define BINARY_BASE 2 42 #define OCTAL_BASE 8 43 #define DECIMAL_BASE 10 44 #define WAIT_MAX_SECOND 5 45 #define MAX_BUFFER_LEN 256 46 #define CMDLINE_VALUE_LEN_MAX 512 47 #define STDERR_HANDLE 2 48 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) 49 #define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline" 50 51 #ifndef OHOS_LITE 52 void SetBootCompleted(bool isBootCompleted); 53 bool IsBootCompleted(void); 54 #endif 55 56 uid_t DecodeUid(const char *name); 57 gid_t DecodeGid(const char *name); 58 char *ReadFileToBuf(const char *configFile); 59 int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length); 60 char *ReadFileData(const char *fileName); 61 62 typedef struct INIT_TIMING_STAT { 63 struct timespec startTime; 64 struct timespec endTime; 65 } INIT_TIMING_STAT; 66 67 typedef struct NameValuePair { 68 const char *name; 69 const char *nameEnd; 70 const char *value; 71 const char *valueEnd; 72 } NAME_VALUE_PAIR; 73 int IterateNameValuePairs(const char *src, void (*iterator)(const NAME_VALUE_PAIR *nv, void *context), void *context); 74 75 int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum); 76 long long InitDiffTime(INIT_TIMING_STAT *stat); 77 void WaitForFile(const char *source, unsigned int maxSecond); 78 size_t WriteAll(int fd, const char *buffer, size_t size); 79 char *GetRealPath(const char *source); 80 int StringToInt(const char *str, int defaultValue); 81 int StringToUint(const char *name, unsigned int *value); 82 int MakeDirRecursive(const char *dir, mode_t mode); 83 void CheckAndCreateDir(const char *fileName); 84 int CheckAndCreatFile(const char *file, mode_t mode); 85 int MakeDir(const char *dir, mode_t mode); 86 int ReadFileInDir(const char *dirPath, const char *includeExt, 87 int (*processFile)(const char *fileName, void *context), void *context); 88 char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount); 89 void FreeStringVector(char **vector, int count); 90 int InUpdaterMode(void); 91 int InRescueMode(void); 92 int StringReplaceChr(char *strl, char oldChr, char newChr); 93 94 int OpenConsole(void); 95 int OpenKmsg(void); 96 void TrimTail(char *str, char c); 97 char *TrimHead(char *str, char c); 98 99 INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime); 100 101 INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out); 102 INIT_LOCAL_API int StringToLL(const char *str, long long int *out); 103 void CloseStdio(void); 104 105 int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize); 106 int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen); 107 108 /** 109 * @brief Get string index from a string array 110 * 111 * @param strArray string array 112 * Attension: last item in the array must be NULL, for example: 113 * const char *strArray[] = { "val1", "val2", NULL } 114 * @param target string to be matched 115 * @param ignoreCase 0 means exact match, others mean ignore case 116 * @return return 0 if succeed; other values if failed. 117 */ 118 int OH_StrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase); 119 120 /** 121 * @brief Get string index from a string array with extended strings 122 * 123 * @param strArray string array 124 * Attension: last item in the array must be NULL, for example: 125 * const char *strArray[] = { "val1", "val2", NULL } 126 * @param target string to be matched 127 * @param ignoreCase 0 means exact match, others mean ignore case 128 * @param extend optional extended strings array, last string must be NULL 129 * @return return 0 if succeed; other values if failed. 130 */ 131 int OH_ExtendableStrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase, const char *extend[]); 132 133 /** 134 * @brief Get string dictionary from a string dictionary array 135 * 136 * @param strDict string dictionary array 137 * Attension: last item in the array must be NULL, for example: 138 * Each item must be a structure with "const char *" as the first element 139 * For example: 140 * typedef { 141 * const char *key; // First element must be "const char *" 142 * const char *value; // Arbitrary elements 143 * // Optionally add more elements 144 * } STRING_DICT_ST; 145 * @param target string to be matched 146 * @param ignoreCase 0 means exact match, others mean ignore case 147 * @return return item pointer if succeed; NULL if failed 148 * @example 149 * // Define a name-value pair as dictionary item 150 * typedef struct { 151 * const char *name; 152 * const char *value; 153 * } NAME_VALUE_ST; 154 155 * // Fill the dictionary values 156 * NAME_VALUE_ST dict[] = { { "key1", "val1" }, { "key2", "val2" }}; 157 158 * // Find by key name 159 * NAME_VALUE_ST *found = (NAME_VALUE_ST *)StrDictGetIndex((void **)dict, sizeof(NAME_VALUE_ST), "key1", FALSE); 160 */ 161 void *OH_StrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase); 162 163 /** 164 * @brief Get string dictionary from a string dictionary array and extended string dictionary 165 * 166 * @param strDict string dictionary array 167 * Attension: last item in the array must be NULL, for example: 168 * Each item must be a structure with "const char *" as the first element 169 * For example: 170 * typedef { 171 * const char *key; // First element must be "const char *" 172 * const char *value; // Arbitrary elements 173 * // Optionally add more elements 174 * } STRING_DICT_ST; 175 * @param target string to be matched 176 * @param ignoreCase 0 means exact match, others mean ignore case 177 * @param extendStrDict optional extended strings dictionary array, last item must be NULL 178 * @return return item pointer if succeed; NULL if failed. 179 */ 180 void *OH_ExtendableStrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase, void **extendStrDict); 181 182 long long GetUptimeInMicroSeconds(const struct timespec *uptime); 183 184 #ifdef __cplusplus 185 #if __cplusplus 186 } 187 #endif 188 #endif 189 #endif // INIT_UTILS_H 190