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 MAX_INT_LEN 20 38 #define HEX_BASE 16 39 #define BINARY_BASE 2 40 #define OCTAL_BASE 8 41 #define DECIMAL_BASE 10 42 #define WAIT_MAX_SECOND 5 43 #define MAX_BUFFER_LEN 256 44 #define CMDLINE_VALUE_LEN_MAX 512 45 #define STDERR_HANDLE 2 46 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) 47 48 #ifndef STARTUP_INIT_UT_PATH 49 #ifdef STARTUP_INIT_TEST // ut 50 #define STARTUP_INIT_UT_PATH "/data/init_ut" 51 #else 52 #define STARTUP_INIT_UT_PATH "" 53 #endif 54 #endif 55 #define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline" 56 57 uid_t DecodeUid(const char *name); 58 gid_t DecodeGid(const char *name); 59 char *ReadFileToBuf(const char *configFile); 60 int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length); 61 char *ReadFileData(const char *fileName); 62 63 int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum); 64 void WaitForFile(const char *source, unsigned int maxSecond); 65 size_t WriteAll(int fd, const char *buffer, size_t size); 66 char *GetRealPath(const char *source); 67 int StringToInt(const char *str, int defaultValue); 68 int StringToUint(const char *name, unsigned int *value); 69 int MakeDirRecursive(const char *dir, mode_t mode); 70 void CheckAndCreateDir(const char *fileName); 71 int CheckAndCreatFile(const char *file, mode_t mode); 72 int MakeDir(const char *dir, mode_t mode); 73 int ReadFileInDir(const char *dirPath, const char *includeExt, 74 int (*processFile)(const char *fileName, void *context), void *context); 75 char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount); 76 void FreeStringVector(char **vector, int count); 77 int InUpdaterMode(void); 78 int StringReplaceChr(char *strl, char oldChr, char newChr); 79 80 void OpenConsole(void); 81 void TrimTail(char *str, char c); 82 char *TrimHead(char *str, char c); 83 84 INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out); 85 INIT_LOCAL_API int StringToLL(const char *str, long long int *out); 86 void CloseStdio(void); 87 void RedirectStdio(int fd); 88 #ifdef __cplusplus 89 #if __cplusplus 90 } 91 #endif 92 #endif 93 #endif // INIT_UTILS_H 94