1 /* 2 * Copyright (c) 2020-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 #ifndef BASE_STARTUP_INIT_CMDS_H 16 #define BASE_STARTUP_INIT_CMDS_H 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <sys/stat.h> 20 #include <sys/time.h> 21 #include <sys/types.h> 22 #include <time.h> 23 24 #include "cJSON.h" 25 #include "init_cmdexecutor.h" 26 #ifdef __cplusplus 27 #if __cplusplus 28 extern "C" { 29 #endif 30 #endif 31 32 #define DEFAULT_DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) // mkdir, default mode 33 #define SPACES_CNT_IN_CMD_MAX 10 // mount, max number of spaces in cmdline 34 #define SPACES_CNT_IN_CMD_MIN 2 // mount, min number of spaces in cmdline 35 36 #define LOADCFG_BUF_SIZE 128 // loadcfg, max buffer for one cmdline 37 #define LOADCFG_MAX_FILE_LEN 51200 // loadcfg, max file size is 50K 38 #define LOADCFG_MAX_LOOP 20 // loadcfg, to prevent to be trapped in infite loop 39 #define OCTAL_TYPE 8 // 8 means octal to decimal 40 #define MAX_BUFFER 256UL 41 #define AUTHORITY_MAX_SIZE 128 42 43 #define MAX_CMD_NAME_LEN 32 44 #define MAX_CMD_CONTENT_LEN 256 45 #define MAX_CMD_CNT_IN_ONE_JOB 200 46 #define MAX_COPY_BUF_SIZE 256 47 #define DEFAULT_COPY_ARGS_CNT 2 48 49 #define OPTIONS_SIZE 128 50 51 #define SUPPORT_MAX_ARG_FOR_EXEC 10 52 // one cmd line 53 typedef struct { 54 int cmdIndex; 55 char cmdContent[MAX_CMD_CONTENT_LEN + 1]; 56 } CmdLine; 57 58 typedef struct { 59 int cmdNum; 60 CmdLine cmds[0]; 61 } CmdLines; 62 63 struct CmdArgs { 64 int argc; 65 char *argv[0]; 66 }; 67 68 struct CmdTable { 69 char name[MAX_CMD_NAME_LEN]; 70 unsigned char minArg; 71 unsigned char maxArg; 72 unsigned char careContext; 73 void (*DoFuncion)(const struct CmdArgs *ctx); 74 }; 75 76 typedef struct INIT_TIMING_STAT { 77 struct timespec startTime; 78 struct timespec endTime; 79 } INIT_TIMING_STAT; 80 81 typedef enum _initContextType { 82 INIT_CONTEXT_CHIPSET, 83 INIT_CONTEXT_MAIN, 84 } InitContextType; 85 86 typedef struct { 87 InitContextType type; 88 } ConfigContext; 89 90 int GetParamValue(const char *symValue, unsigned int symLen, char *paramValue, unsigned int paramLen); 91 const struct CmdArgs *GetCmdArg(const char *cmdContent, const char *delim, int argsCount); 92 void FreeCmdArg(struct CmdArgs *cmd); 93 // exec in context, if context == null, exec in current context 94 void DoCmdByIndex(int index, const char *cmdContent, const ConfigContext *context); 95 const char *GetMatchCmd(const char *cmdStr, int *index); 96 const char *GetCmdKey(int index); 97 const struct CmdTable *GetCmdTable(int *number); 98 int GetCmdLinesFromJson(const cJSON *root, CmdLines **cmdLines); 99 const struct CmdTable *GetCmdByName(const char *name); 100 void ExecReboot(const char *value); 101 char *BuildStringFromCmdArg(const struct CmdArgs *ctx, int startIndex); 102 void ExecCmd(const struct CmdTable *cmd, const char *cmdContent); 103 int SetFileCryptPolicy(const char *dir); 104 105 void OpenHidebug(const char *name); 106 long long InitDiffTime(INIT_TIMING_STAT *stat); 107 108 void StopSubInit(pid_t pid); 109 int ExecuteCmdInSubInit(const ConfigContext *context, const char *name, const char *cmdContent); 110 int SetSubInitContext(const ConfigContext *context, const char *service); 111 int CheckExecuteInSubInit(const ConfigContext *context); 112 113 // exec in context, if context == null, exec in current context 114 void PluginExecCmdByCmdIndex(int index, const char *cmdContent, const ConfigContext *context); 115 const char *PluginGetCmdIndex(const char *cmdStr, int *index); 116 const char *GetPluginCmdNameByIndex(int index); 117 int AddCareContextCmdExecutor(const char *cmdName, CmdExecutor executor); 118 119 #ifdef __cplusplus 120 #if __cplusplus 121 } 122 #endif 123 #endif 124 125 #endif // BASE_STARTUP_INITLITE_CMDS_H 126