From cbb9c859a31d7cb0f724afbcdb41cffd39abe91c Mon Sep 17 00:00:00 2001 From: Hang Zhao Date: Sun, 17 Apr 2022 10:47:51 +0800 Subject: [PATCH] replace sysak with sysak.c Signed-off-by: Hang Zhao --- Makefile | 1 + source/sysak | 216 --------------------------------------------- source/sysak.c | 235 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 236 insertions(+), 216 deletions(-) delete mode 100755 source/sysak create mode 100644 source/sysak.c diff --git a/Makefile b/Makefile index 50a486f1..2402211e 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ $(TARGET_LIST): make -C $@ -j binary: + $(CC) -o $(SRC)/sysak $(SRC)/sysak.c cp $(SRC)/sysak $(OBJPATH)/ chmod +x $(OBJPATH)/sysak chmod +x $(OBJPATH)/.sysak_compoents/tools/* -R diff --git a/source/sysak b/source/sysak deleted file mode 100755 index cd2b648a..00000000 --- a/source/sysak +++ /dev/null @@ -1,216 +0,0 @@ -#!/bin/sh -#****************************************************************# -# ScriptName: sysak.sh -# Author: weipu.zy@alibaba-inc.com -# Create Date: 2021-02-22 11:44 -# Modify Author: $SHTERM_REAL_USER@alibaba-inc.com -# Modify Date: 2021-02-22 11:45 -# Function: -#***************************************************************# -WORK_PATH=$(dirname $(readlink -e "$0"))/.sysak_compoents -TOOLS_ROOT=$WORK_PATH/tools -TOOLS_PATH=$WORK_PATH/tools/`uname -r` -LIB_PATH=$WORK_PATH/lib/`uname -r` -SYSAK_MOD=$LIB_PATH/sysak.ko -export SYSAK_WORK_PATH=$WORK_PATH - -function usage() { - echo "Usage: $0 [opt] subcmd [cmdargs]" - echo " opt: -h, help information" - echo " subcmd:" - echo " list, show all components" - echo " help, help information for specify component" -} - -function upgrade() { - echo upgrade -} - -function get() { - echo get $1 - return 1 -} - -declare -A sysak_list_map=() -parse_sysak_list() { - if [ -f $TOOLS_ROOT/.sysak.rules ]; then - while read line - do - local cmd=`echo $line | awk -F: '{print $1}'` - if [ -n "$cmd" ]; then - sysak_list_map[$cmd]=`echo $line | awk -F: '{print $2}'` - fi - done < $TOOLS_ROOT/.sysak.rules - fi - - if [ -f $TOOLS_PATH/.sysak.rules ]; then - while read line - do - local cmd=`echo $line | awk -F: '{print $1}'` - if [ -n "$cmd" ]; then - sysak_list_map[$cmd]=`echo $line | awk -F: '{print $2}'` - fi - done < $TOOLS_PATH/.sysak.rules - fi -} - -function list() { - parse_sysak_list - for key in ${!sysak_list_map[@]}; do - echo $key - done -} - -sysakmod_depend() -{ - res=`lsmod | grep sysak` - if [ -z "$res" ]; then - insmod $LIB_PATH/sysak.ko - if [ $? -ne 0 ]; then - echo "insmod ko failed, command maybe not supported for this kernel." - exit $? - fi - fi -} - -component_depend_prev() { - - local rules=${sysak_list_map[$1]} - local prev_rules_str=${rules%'};post'*} - prev_rules_str=${prev_rules_str#*'prev{'} - oldIFS=$IFS - IFS=, - local prev_rules=($prev_rules_str) - IFS=$oldIFS - for item in "${prev_rules[@]}"; do - local rule=($item) - key=${rule[0]} - val=${rule[1]} - if [[ $key == 'default' ]] || [[ "${*:2}" =~ "$key" ]]; then - case $val in - "modin") - sysakmod_depend - ;; - *) - ;; - esac - fi - done -} - -component_depend_post() { - local rules=${sysak_list_map[$1]} - local post_rules_str=${rules#*'post{'} - post_rules_str=${post_rules_str%'}'*} - oldIFS=$IFS - IFS=, - local post_rules=($post_rules_str) - IFS=$oldIFS - - for item in "${post_rules[@]}"; do - local rule=($item) - key=${rule[0]} - val=${rule[1]} - if [[ $key == 'default' ]] || [[ "${*:2}" =~ "$key" ]]; then - case $val in - "modun") - rmmod sysak - ;; - *) - ;; - esac - fi - done - -} - - -#execute command,every command need such args: -# -h/--help: command usage -# -f/--file: output files, default stdout -# output format jason -# -e/--enable -# -d/--disable -function execute() { - parse_sysak_list - exec_file=$TOOLS_ROOT/$1 - if [ ! -f $exec_file ];then - exec_file=$TOOLS_PATH/$1 - fi - - if [ ! -f $exec_file ];then - if [ $get_component ];then - get $1 - if [ $? != 0 ]; then - echo subcmd:$1 is not support - exit -1 - fi - else - echo "no components, you should get first" - exit -1 - fi - fi - - chmod -R +x $exec_file - component_depend_prev $* - $exec_file ${*:2} - component_depend_post $* -} - -#upload result data -function upload() { - echo upload -} - -while getopts 'h:g' OPT; do - case $OPT in - "h") - usage - exit 0 - ;; - "g") - get_component=true - break - ;; - *) - usage - exit -1 - break - ;; - esac -done - -if [ $get_component ]; then - subcmd=$2; - subargs=${*:3} -else - subcmd=$1 - subargs=${*:2}; -fi - -if [ -z $subcmd ];then - usage - exit -1 -fi - -#echo subcmd=$subcmd -case $subcmd in - "help") - execute ${subargs[0]} -h - ;; - "upgrade") - upgrade - ;; - "list") - list - ;; - "get") - get $2 - ;; - "upload") - upload - ;; - *) - execute $subcmd $subargs - ;; -esac diff --git a/source/sysak.c b/source/sysak.c new file mode 100644 index 00000000..2d10ff90 --- /dev/null +++ b/source/sysak.c @@ -0,0 +1,235 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define KVERSION 64 +#define MAX_SUBCMD_ARGS 128 +#define MAX_NAME_LEM 64 +#define MAX_WORK_PATH 256 +#define bool int +#define true 1 +#define false 0 + +char *module = "/sysak.ko"; +char *log_path="/var/log/sysak"; +char *system_modules = "/proc/modules"; + +char kern_version[KVERSION]; +char modin[MAX_SUBCMD_ARGS]; +char modun[MAX_SUBCMD_ARGS]; +char tools_path[MAX_WORK_PATH]; +char sysak_rule[MAX_WORK_PATH]; +char module_path[MAX_WORK_PATH]; +char sysak_other_rule[MAX_WORK_PATH]; +bool pre_module = false; +bool post_module = false; + +static void usage(void) +{ + fprintf(stdout, + "Usage: sysak [cmd] [subcmd [cmdargs]]\n" + " cmd:\n" + " list, show all of subcmds\n" + " help, help information for specify subcmd\n" + " subcmd: see the result of list\n"); +} + +static void kern_release(void) +{ + struct utsname name; + + if (uname (&name) == -1){ + printf("cannot get system name\n"); + return; + } + strncpy(kern_version,name.release,sizeof(name.release)); +} + +static void mod_ctrl(bool enable) +{ + FILE *modlist_fp; + char exec_mod[4*KVERSION]; + bool has_ko = false; + char modinfo[MAX_SUBCMD_ARGS]; + + modlist_fp = fopen(system_modules, "r"); + if (!modlist_fp){ + printf("open %s failed\n", system_modules); + return; + } + while(fgets(modinfo, sizeof(modinfo), modlist_fp)) + { + if (strstr(modinfo,"sysak")){ + has_ko = true; + break; + } + + } + fclose(modlist_fp); + + if (enable && !has_ko) { + snprintf(exec_mod, sizeof(exec_mod), "insmod %s%s%s", module_path, kern_version, module); + system(exec_mod); + + } + else if (!enable && has_ko) { + snprintf(exec_mod, sizeof(exec_mod), "rmmod %s%s%s", module_path, kern_version, module); + system(exec_mod); + } +} + +static void exectue(int argc, char *argv[]) +{ + int i; + char subcmd_name[MAX_NAME_LEM+MAX_SUBCMD_ARGS]; + char subcmd_args[MAX_SUBCMD_ARGS]; + + if (pre_module) + mod_ctrl(true); + + snprintf(subcmd_name, sizeof(subcmd_name), "%s%s", tools_path, argv[1]); + + if (access(subcmd_name,0) != 0) + snprintf(subcmd_name, sizeof(subcmd_name), "%s%s", tools_path, kern_version, argv[1]); + + for(i = 2; i <= (argc-1); i++){ + snprintf(subcmd_args, sizeof(subcmd_args), " %s", argv[i]); + strcat(subcmd_name,subcmd_args); + } + + system(subcmd_name); + + if (post_module) + mod_ctrl(false); +} + +static void print_each_tool(char *path) +{ + FILE *fp; + char buf[MAX_NAME_LEM + MAX_SUBCMD_ARGS]; + char tools_name[MAX_NAME_LEM]; + + fp = fopen(path, "r"); + if (!fp){ + printf("open %s failed\n", path); + return; + } + + while(fgets(buf, sizeof(buf), fp)) + { + sscanf(buf,"%[^:]",tools_name); + printf(" %s\n",tools_name); + } + fclose(fp); +} + +static void subcmd_list(void) +{ + fputs("subcmd list:\n",stdout); + print_each_tool(sysak_rule); + print_each_tool(sysak_other_rule); +} + +static bool tool_lookup_mod(char *path, char *tool) +{ + FILE *fp; + char buf[MAX_NAME_LEM + MAX_SUBCMD_ARGS]; + + fp = fopen(path, "r"); + if (!fp){ + printf("open %s failed\n", path); + return false; + } + while(fgets(buf, sizeof(buf), fp)) + { + char tools_name[MAX_NAME_LEM]; + + sscanf(buf,"%[^:]",tools_name); + if (strcmp(tools_name, tool)){ + continue; + } + + sscanf(buf,"%*[^:]:prev{%[^}]};post{%[^}]}", modin, modun); + fclose(fp); + return true; + } + fclose(fp); + return false; +} + +static void subcmd_parse(int argc, char *argv[]) +{ + int i; + + if (!tool_lookup_mod(sysak_other_rule, argv[1]) && + !tool_lookup_mod(sysak_rule, argv[1])){ + printf("no components, you should get first\n"); + return; + } + + if (strstr(modin, "default") != NULL|| strstr(modun, "default") != NULL){ + pre_module = true; + post_module = true; + goto exec; + } + + for(i = 2; i <= (argc-1); i++) + { + if (strstr(modin, argv[i])){ + pre_module = true; + break; + } + else if (strstr(modun, argv[i])){ + post_module = true; + break; + } + } +exec: + exectue(argc, argv); +} + +static void parse_arg(int argc, char *argv[]) +{ + if (argc < 2) + usage(); + + if (!strcmp(argv[1],"list")){ + subcmd_list(); + return; + } + + if (!strcmp(argv[1],"help")){ + usage(); + return; + } + subcmd_parse(argc, argv); +} + +int main(int argc, char *argv[]) +{ + char tmp[MAX_WORK_PATH]; + char *work_path; + + if (access(log_path,0) != 0) + mkdir(log_path, 0755 ); + + kern_release(); + work_path = getcwd(tmp,MAX_WORK_PATH); + + snprintf(tools_path, sizeof(tools_path), "%s%s", work_path, + "/.sysak_compoents/tools/"); + snprintf(module_path, sizeof(module_path), "%s%s", work_path, + "/.sysak_compoents/lib/"); + snprintf(sysak_rule, sizeof(sysak_rule), "%s%s", work_path, + "/.sysak_compoents/tools/.sysak.rules"); + snprintf(sysak_other_rule, sizeof(sysak_other_rule), "%s%s%s%s", work_path, + "/.sysak_compoents/tools/",kern_version,"/.sysak.rules"); + + parse_arg(argc, argv); + return 0; +} \ No newline at end of file -- Gitee