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
39extern "C" {
40#endif
41#endif
42
43typedef 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
61uid_t DecodeUid(const char *name);
62gid_t DecodeGid(const char *name);
63char *ReadFileToBuf(const char *configFile);
64int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length);
65char *ReadFileData(const char *fileName);
66
67typedef struct INIT_TIMING_STAT {
68    struct timespec startTime;
69    struct timespec endTime;
70} INIT_TIMING_STAT;
71
72typedef struct NameValuePair {
73    const char *name;
74    const char *nameEnd;
75    const char *value;
76    const char *valueEnd;
77} NAME_VALUE_PAIR;
78int IterateNameValuePairs(const char *src, void (*iterator)(const NAME_VALUE_PAIR *nv, void *context), void *context);
79
80int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum);
81long long InitDiffTime(INIT_TIMING_STAT *stat);
82void WaitForFile(const char *source, unsigned int maxSecond);
83size_t WriteAll(int fd, const char *buffer, size_t size);
84char *GetRealPath(const char *source);
85int StringToInt(const char *str, int defaultValue);
86int StringToUint(const char *name, unsigned int *value);
87int MakeDirRecursive(const char *dir, mode_t mode);
88void CheckAndCreateDir(const char *fileName);
89int CheckAndCreatFile(const char *file, mode_t mode);
90int MakeDir(const char *dir, mode_t mode);
91int ReadFileInDir(const char *dirPath, const char *includeExt,
92    int (*processFile)(const char *fileName, void *context), void *context);
93char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount);
94void FreeStringVector(char **vector, int count);
95int InUpdaterMode(void);
96int InRescueMode(void);
97int StringReplaceChr(char *strl, char oldChr, char newChr);
98
99int OpenConsole(void);
100int OpenKmsg(void);
101void TrimTail(char *str, char c);
102char *TrimHead(char *str, char c);
103
104INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime);
105
106INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out);
107INIT_LOCAL_API int StringToLL(const char *str, long long int *out);
108void CloseStdio(void);
109
110int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize);
111int 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 */
123int 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 */
136int 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 */
166void *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 */
185void *OH_ExtendableStrDictGet(void **strDict, int dictSize, const char *target, int ignoreCase, void **extendStrDict);
186
187long long GetUptimeInMicroSeconds(const struct timespec *uptime);
188
189#ifdef __cplusplus
190#if __cplusplus
191}
192#endif
193#endif
194#endif // INIT_UTILS_H
195