2021-06-28 16:04:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INIT_UTILS_H
|
|
|
|
#define INIT_UTILS_H
|
2021-09-17 07:43:10 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2022-05-25 04:41:36 +00:00
|
|
|
#include <stdint.h>
|
2022-06-21 07:58:42 +00:00
|
|
|
#include <stdbool.h>
|
2021-09-13 11:58:03 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-07-19 01:48:20 +00:00
|
|
|
#include "beget_ext.h"
|
|
|
|
|
2021-06-28 16:04:23 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#if __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-01-19 08:08:48 +00:00
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
int value;
|
|
|
|
} InitArgInfo;
|
|
|
|
|
2023-05-29 02:52:37 +00:00
|
|
|
#define BASE_MS_UNIT 1000
|
2022-07-19 01:48:20 +00:00
|
|
|
#define MAX_INT_LEN 20
|
|
|
|
#define HEX_BASE 16
|
2021-07-06 15:45:38 +00:00
|
|
|
#define BINARY_BASE 2
|
|
|
|
#define OCTAL_BASE 8
|
|
|
|
#define DECIMAL_BASE 10
|
2022-01-20 09:28:02 +00:00
|
|
|
#define WAIT_MAX_SECOND 5
|
2021-12-06 06:40:14 +00:00
|
|
|
#define MAX_BUFFER_LEN 256
|
2022-03-10 07:34:16 +00:00
|
|
|
#define CMDLINE_VALUE_LEN_MAX 512
|
2022-09-06 08:07:37 +00:00
|
|
|
#define STDERR_HANDLE 2
|
2021-09-13 11:58:03 +00:00
|
|
|
#define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0]))
|
2022-04-28 10:53:40 +00:00
|
|
|
#define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline"
|
|
|
|
|
2021-09-13 11:58:03 +00:00
|
|
|
uid_t DecodeUid(const char *name);
|
2022-06-21 07:58:42 +00:00
|
|
|
gid_t DecodeGid(const char *name);
|
2021-09-17 07:43:10 +00:00
|
|
|
char *ReadFileToBuf(const char *configFile);
|
2021-11-11 07:37:48 +00:00
|
|
|
int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length);
|
|
|
|
char *ReadFileData(const char *fileName);
|
2021-12-06 03:23:20 +00:00
|
|
|
|
2023-06-27 04:17:05 +00:00
|
|
|
typedef struct NameValuePair {
|
2023-02-06 07:45:55 +00:00
|
|
|
const char *name;
|
2023-06-27 04:17:05 +00:00
|
|
|
const char *nameEnd;
|
2023-02-06 07:45:55 +00:00
|
|
|
const char *value;
|
2023-06-27 04:17:05 +00:00
|
|
|
const char *valueEnd;
|
2023-02-06 07:45:55 +00:00
|
|
|
} NAME_VALUE_PAIR;
|
|
|
|
int IterateNameValuePairs(const char *src, void (*iterator)(const NAME_VALUE_PAIR *nv, void *context), void *context);
|
|
|
|
|
2021-10-14 08:43:33 +00:00
|
|
|
int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum);
|
2022-01-20 09:28:02 +00:00
|
|
|
void WaitForFile(const char *source, unsigned int maxSecond);
|
2021-09-28 12:06:58 +00:00
|
|
|
size_t WriteAll(int fd, const char *buffer, size_t size);
|
2021-10-14 08:43:33 +00:00
|
|
|
char *GetRealPath(const char *source);
|
2021-09-17 07:43:10 +00:00
|
|
|
int StringToInt(const char *str, int defaultValue);
|
2022-06-22 08:16:11 +00:00
|
|
|
int StringToUint(const char *name, unsigned int *value);
|
2021-09-17 07:43:10 +00:00
|
|
|
int MakeDirRecursive(const char *dir, mode_t mode);
|
2022-05-26 04:27:57 +00:00
|
|
|
void CheckAndCreateDir(const char *fileName);
|
2022-05-30 12:44:25 +00:00
|
|
|
int CheckAndCreatFile(const char *file, mode_t mode);
|
2021-09-17 07:43:10 +00:00
|
|
|
int MakeDir(const char *dir, mode_t mode);
|
2021-10-14 08:43:33 +00:00
|
|
|
int ReadFileInDir(const char *dirPath, const char *includeExt,
|
|
|
|
int (*processFile)(const char *fileName, void *context), void *context);
|
|
|
|
char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount);
|
|
|
|
void FreeStringVector(char **vector, int count);
|
|
|
|
int InUpdaterMode(void);
|
2021-10-27 02:14:23 +00:00
|
|
|
int StringReplaceChr(char *strl, char oldChr, char newChr);
|
|
|
|
|
2022-07-01 08:53:15 +00:00
|
|
|
void OpenConsole(void);
|
2022-07-27 08:24:08 +00:00
|
|
|
void TrimTail(char *str, char c);
|
|
|
|
char *TrimHead(char *str, char c);
|
2022-07-14 10:52:39 +00:00
|
|
|
|
2023-05-29 02:52:37 +00:00
|
|
|
INIT_LOCAL_API uint32_t IntervalTime(struct timespec *startTime, struct timespec *endTime);
|
|
|
|
|
2022-07-14 10:52:39 +00:00
|
|
|
INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out);
|
|
|
|
INIT_LOCAL_API int StringToLL(const char *str, long long int *out);
|
2022-09-06 08:07:37 +00:00
|
|
|
void CloseStdio(void);
|
|
|
|
void RedirectStdio(int fd);
|
2023-04-21 09:32:51 +00:00
|
|
|
|
|
|
|
int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize);
|
2023-05-08 07:09:35 +00:00
|
|
|
int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen);
|
2023-07-17 01:25:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get string index from a string array
|
|
|
|
*
|
|
|
|
* @param strArray string array
|
|
|
|
* Attension: last item in the array must be NULL, for example:
|
|
|
|
* const char *strArray[] = { "val1", "val2", NULL }
|
|
|
|
* @param target string to be matched
|
|
|
|
* @param ignoreCase 0 means exact match, others mean ignore case
|
|
|
|
* @return return 0 if succeed; other values if failed.
|
|
|
|
*/
|
|
|
|
int StrArrayGetIndex(const char *strArray[], const char *target, int ignoreCase);
|
|
|
|
|
2021-06-28 16:04:23 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#if __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif // INIT_UTILS_H
|