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