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 24 #include "beget_ext.h" 25 26 #ifdef __cplusplus 27 #if __cplusplus 28 extern "C" { 29 #endif 30 #endif 31 32 typedef struct { 33 char *name; 34 int value; 35 } InitArgInfo; 36 37 #define BASE_MS_UNIT 1000 38 #define MAX_INT_LEN 20 39 #define HEX_BASE 16 40 #define BINARY_BASE 2 41 #define OCTAL_BASE 8 42 #define DECIMAL_BASE 10 43 #define WAIT_MAX_SECOND 5 44 #define MAX_BUFFER_LEN 256 45 #define CMDLINE_VALUE_LEN_MAX 512 46 #define STDERR_HANDLE 2 47 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) 48 #define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline" 49 50 uid_t DecodeUid(const char *name); 51 gid_t DecodeGid(const char *name); 52 char *ReadFileToBuf(const char *configFile); 53 int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length); 54 char *ReadFileData(const char *fileName); 55 56 typedef struct NameValuePair { 57 const char *name; 58 const char *nameEnd; 59 const char *value; 60 const char *valueEnd; 61 } NAME_VALUE_PAIR; 62 int IterateNameValuePairs(const char *src, void (*iterator)(const NAME_VALUE_PAIR *nv, void *context), void *context); 63 64 int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum); 65 void WaitForFile(const char *source, unsigned int maxSecond); 66 size_t WriteAll(int fd, const char *buffer, size_t size); 67 char *GetRealPath(const char *source); 68 int StringToInt(const char *str, int defaultValue); 69 int StringToUint(const char *name, unsigned int *value); 70 int MakeDirRecursive(const char *dir, mode_t mode); 71 void CheckAndCreateDir(const char *fileName); 72 int CheckAndCreatFile(const char *file, mode_t mode); 73 int MakeDir(const char *dir, mode_t mode); 74 int ReadFileInDir(const char *dirPath, const char *includeExt, 75 int (*processFile)(const char *fileName, void *context), void *context); 76 char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount); 77 void FreeStringVector(char **vector, int count); 78 int InUpdaterMode(void); 79 int StringReplaceChr(char *strl, char oldChr, char newChr); 80 81 void OpenConsole(void); 82 void TrimTail(char *str, char c); 83 char *TrimHead(char *str, char c); 84 85 INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime); 86 87 INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out); 88 INIT_LOCAL_API int StringToLL(const char *str, long long int *out); 89 void CloseStdio(void); 90 void RedirectStdio(int fd); 91 92 int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize); 93 int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen); 94 95 /** 96 * @brief Get string index from a string array 97 * 98 * @param strArray string array 99 * Attension: last item in the array must be NULL, for example: 100 * const char *strArray[] = { "val1", "val2", NULL } 101 * @param target string to be matched 102 * @param ignoreCase 0 means exact match, others mean ignore case 103 * @return return 0 if succeed; other values if failed. 104 */ 105 int StrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase); 106 107 #ifdef __cplusplus 108 #if __cplusplus 109 } 110 #endif 111 #endif 112 #endif // INIT_UTILS_H 113