• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 InRescueMode(void);
87 int StringReplaceChr(char *strl, char oldChr, char newChr);
88 
89 int OpenConsole(void);
90 int OpenKmsg(void);
91 void TrimTail(char *str, char c);
92 char *TrimHead(char *str, char c);
93 
94 INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime);
95 
96 INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out);
97 INIT_LOCAL_API int StringToLL(const char *str, long long int *out);
98 void CloseStdio(void);
99 
100 int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize);
101 int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen);
102 
103 /**
104  * @brief Get string index from a string array
105  *
106  * @param strArray string array
107  *     Attension: last item in the array must be NULL, for example:
108  *     const char *strArray[] = { "val1", "val2", NULL }
109  * @param target string to be matched
110  * @param ignoreCase 0 means exact match, others mean ignore case
111  * @return return 0 if succeed; other values if failed.
112  */
113 int OH_StrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase);
114 
115 /**
116  * @brief Get string index from a string array with extended strings
117  *
118  * @param strArray string array
119  *     Attension: last item in the array must be NULL, for example:
120  *     const char *strArray[] = { "val1", "val2", NULL }
121  * @param target string to be matched
122  * @param ignoreCase 0 means exact match, others mean ignore case
123  * @param extend optional extended strings array, last string must be NULL
124  * @return return 0 if succeed; other values if failed.
125  */
126 int OH_ExtendableStrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase, const char *extend[]);
127 
128 /**
129  * @brief Get string dictionary from a string dictionary array
130  *
131  * @param strDict string dictionary array
132  *     Attension: last item in the array must be NULL, for example:
133  *     Each item must be a structure with "const char *" as the first element
134  *     For example:
135  *     typedef {
136  *         const char *key;   // First element must be "const char *"
137  *         const char *value; // Arbitrary elements
138  *         // Optionally add more elements
139  *     } STRING_DICT_ST;
140  * @param target string to be matched
141  * @param ignoreCase 0 means exact match, others mean ignore case
142  * @return return item pointer if succeed; NULL if failed
143  * @example
144  * // Define a name-value pair as dictionary item
145  * typedef struct {
146  *     const char *name;
147  *     const char *value;
148  * } NAME_VALUE_ST;
149 
150  * // Fill the dictionary values
151  * NAME_VALUE_ST dict[] = { { "key1", "val1" }, { "key2", "val2" }};
152 
153  * // Find by key name
154  * NAME_VALUE_ST *found = (NAME_VALUE_ST *)StrDictGetIndex((void **)dict, sizeof(NAME_VALUE_ST), "key1", FALSE);
155  */
156 void *OH_StrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase);
157 
158 /**
159  * @brief Get string dictionary from a string dictionary array and extended string dictionary
160  *
161  * @param strDict string dictionary array
162  *     Attension: last item in the array must be NULL, for example:
163  *     Each item must be a structure with "const char *" as the first element
164  *     For example:
165  *     typedef {
166  *         const char *key;   // First element must be "const char *"
167  *         const char *value; // Arbitrary elements
168  *         // Optionally add more elements
169  *     } STRING_DICT_ST;
170  * @param target string to be matched
171  * @param ignoreCase 0 means exact match, others mean ignore case
172  * @param extendStrDict optional extended strings dictionary array, last item must be NULL
173  * @return return item pointer if succeed; NULL if failed.
174  */
175 void *OH_ExtendableStrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase, void **extendStrDict);
176 
177 long long GetUptimeInMicroSeconds(const struct timespec *uptime);
178 
179 #ifdef __cplusplus
180 #if __cplusplus
181 }
182 #endif
183 #endif
184 #endif // INIT_UTILS_H
185