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