init_cmds.h 4.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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 BASE_STARTUP_INIT_CMDS_H
#define BASE_STARTUP_INIT_CMDS_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
C
fix log  
cheng_jinsong 已提交
20
#include <sys/time.h>
21
#include <sys/types.h>
C
fix log  
cheng_jinsong 已提交
22 23
#include <time.h>

24
#include "cJSON.h"
C
cheng_jinsong 已提交
25
#include "init_cmdexecutor.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

#define DEFAULT_DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) // mkdir, default mode
#define SPACES_CNT_IN_CMD_MAX 10                                           // mount, max number of spaces in cmdline
#define SPACES_CNT_IN_CMD_MIN 2                                            // mount, min number of spaces in cmdline

#define LOADCFG_BUF_SIZE 128       // loadcfg, max buffer for one cmdline
#define LOADCFG_MAX_FILE_LEN 51200 // loadcfg, max file size is 50K
#define LOADCFG_MAX_LOOP 20        // loadcfg, to prevent to be trapped in infite loop
#define OCTAL_TYPE 8               // 8 means octal to decimal
#define MAX_BUFFER 256UL
#define AUTHORITY_MAX_SIZE 128

#define MAX_CMD_NAME_LEN 32
#define MAX_CMD_CONTENT_LEN 256
#define MAX_CMD_CNT_IN_ONE_JOB 200
#define MAX_COPY_BUF_SIZE 256
#define DEFAULT_COPY_ARGS_CNT 2

#define OPTIONS_SIZE 128

#define SUPPORT_MAX_ARG_FOR_EXEC 10
// one cmd line
typedef struct {
    int cmdIndex;
    char cmdContent[MAX_CMD_CONTENT_LEN + 1];
} CmdLine;

typedef struct {
    int cmdNum;
    CmdLine cmds[0];
} CmdLines;

struct CmdArgs {
    int argc;
    char *argv[0];
};

struct CmdTable {
    char name[MAX_CMD_NAME_LEN];
    unsigned char minArg;
    unsigned char maxArg;
C
cheng_jinsong 已提交
72
    unsigned char careContext;
X
add ut  
xionglei6 已提交
73
    void (*DoFuncion)(const struct CmdArgs *ctx);
74 75
};

C
fix log  
cheng_jinsong 已提交
76 77 78 79 80
typedef struct INIT_TIMING_STAT {
    struct timespec startTime;
    struct timespec endTime;
} INIT_TIMING_STAT;

C
cheng_jinsong 已提交
81 82 83 84 85 86 87 88 89
typedef enum _initContextType {
    INIT_CONTEXT_CHIPSET,
    INIT_CONTEXT_MAIN,
} InitContextType;

typedef struct {
    InitContextType type;
} ConfigContext;

90 91 92
int GetParamValue(const char *symValue, unsigned int symLen, char *paramValue, unsigned int paramLen);
const struct CmdArgs *GetCmdArg(const char *cmdContent, const char *delim, int argsCount);
void FreeCmdArg(struct CmdArgs *cmd);
C
cheng_jinsong 已提交
93 94
// exec in context, if context == null, exec in current context
void DoCmdByIndex(int index, const char *cmdContent, const ConfigContext *context);
95 96 97 98 99 100 101
const char *GetMatchCmd(const char *cmdStr, int *index);
const char *GetCmdKey(int index);
const struct CmdTable *GetCmdTable(int *number);
int GetCmdLinesFromJson(const cJSON *root, CmdLines **cmdLines);
const struct CmdTable *GetCmdByName(const char *name);
void ExecReboot(const char *value);
char *BuildStringFromCmdArg(const struct CmdArgs *ctx, int startIndex);
X
add ut  
xionglei6 已提交
102
void ExecCmd(const struct CmdTable *cmd, const char *cmdContent);
103
int SetFileCryptPolicy(const char *dir);
M
Mupceet 已提交
104 105

void OpenHidebug(const char *name);
C
fix log  
cheng_jinsong 已提交
106
long long InitDiffTime(INIT_TIMING_STAT *stat);
C
cheng_jinsong 已提交
107 108 109 110 111 112 113 114 115 116 117

void StopSubInit(pid_t pid);
int ExecuteCmdInSubInit(const ConfigContext *context, const char *name, const char *cmdContent);
int SetSubInitContext(const ConfigContext *context, const char *service);

// exec in context, if context == null, exec in current context
void PluginExecCmdByCmdIndex(int index, const char *cmdContent, const ConfigContext *context);
const char *PluginGetCmdIndex(const char *cmdStr, int *index);
const char *GetPluginCmdNameByIndex(int index);
int AddCareContextCmdExecutor(const char *cmdName, CmdExecutor executor);

118 119 120 121 122 123 124
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

#endif // BASE_STARTUP_INITLITE_CMDS_H