From 97cc10364b662d1d52692a0c9117ada41391f821 Mon Sep 17 00:00:00 2001 From: zzh Date: Fri, 13 Jun 2025 12:07:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B9=BF=E6=92=AD=E7=82=B9=E4=BA=AE=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E9=87=8D=E7=BD=AEsn=E5=8F=98=E9=87=8F=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E8=A2=AB3015=E6=B1=A1=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- main.h | 3 ++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 5d0d88d..52f99b3 100644 --- a/main.c +++ b/main.c @@ -99,6 +99,54 @@ void doCommand_help(int argc, char *argv[]) "************************************************************\r\n"); } +static int safe_rename(const char *src, const char *dst) { + if (rename(src, dst) != 0) { + LOG_E("Failed to rename %s to %s: %s", src, dst, strerror(errno)); + return -1; + } + return 0; +} + +static int safe_remove_dir(const char *path) { + if (rmdir(path) != 0) { + LOG_E("Failed to remove directory %s: %s", path, strerror(errno)); + return -1; + } + return 0; +} + +static int safe_remove_file(const char *path) { + if (unlink(path) != 0) { + LOG_E("Failed to remove file %s: %s", path, strerror(errno)); + return -1; + } + return 0; +} + +static int safe_mkdir(const char *path) { + if (mkdir(path, 0755) != 0 && errno != EEXIST) { + LOG_E("Failed to create directory %s: %s", path, strerror(errno)); + return -1; + } + return 0; +} + +// 安全的命令执行函数 +static int safe_exec_cmd(const char *cmd) { + // 检查命令是否包含危险字符 + if (strstr(cmd, ";") || strstr(cmd, "|") || strstr(cmd, "&") || strstr(cmd, "..")) { + LOG_E("Dangerous command detected: %s", cmd); + return -1; + } + + int ret = system(cmd); + if (ret != 0) { + LOG_E("Command failed: %s, error: %s", cmd, strerror(errno)); + return -1; + } + return 0; +} + void doCommand(int argc, char *argv[]) { //sendData(argv[1],argv[0]); @@ -1576,6 +1624,56 @@ void *thread_remove_duplicate_tag(void *arg){ } } +// 检查文件是否存在的函数 +static int file_exists(const char *path) { + struct stat st; + return (stat(path, &st) == 0); +} + +// 检查压缩包是否解压完成 +static int check_unzip_complete(const char *zip_file) { + char cmd[256]; + char last_file[256] = {0}; + FILE *fp; + + // 获取压缩包中最后一个文件 + snprintf(cmd, sizeof(cmd), "unzip -l %s | tail -n 1 | awk '{print $4}'", zip_file); + fp = popen(cmd, "r"); + if (fp == NULL) { + LOG_E("Failed to execute command: %s", cmd); + return -1; + } + + if (fgets(last_file, sizeof(last_file), fp) == NULL) { + LOG_E("Failed to get last file from zip"); + pclose(fp); + return -1; + } + pclose(fp); + + // 去除换行符 + last_file[strcspn(last_file, "\n")] = 0; + + // 检查文件是否存在 + char full_path[512]; + snprintf(full_path, sizeof(full_path), "tx_ota/%s", last_file); + + int retry_count = 0; + const int max_retries = 30; // 最多等待30秒 + + while (!file_exists(full_path) && retry_count < max_retries) { + usleep(1000000); // 等待1秒 + retry_count++; + } + + if (retry_count >= max_retries) { + LOG_E("Timeout waiting for unzip to complete"); + return -1; + } + + return 0; +} + void *thread_mqtt_recv(void *arg){ prctl(PR_SET_NAME, "mqtt_recv"); char payload[1024]={0}; @@ -1818,12 +1916,18 @@ void *thread_mqtt_recv(void *arg){ if(isLightEnable){ mqtt_service_reply(stationsn,mqtt_parm.msg_messageId,"ok",1,productid); - // 设置规则并广播点亮 + // 设置规则并广播点亮,重置变量,防止被3015污染 isSendComEnd=false; - isLightOn=true; + isLightOn=false; isLightOnByRule=true; // 使用规则控制 isLabelUp=false; + memset(lightsn1, 0, sizeof(lightsn1)); + memset(lightsn2, 0, sizeof(lightsn2)); + memset(lightsn3, 0, sizeof(lightsn3)); + memset(lightsn4, 0, sizeof(lightsn4)); + memset(lightsn5, 0, sizeof(lightsn5)); + // 设置label1和label2不参与匹配 strcpy(mqtt_parm.msg_label1Rule, "04"); // ∅ 不参与匹配 strcpy(mqtt_parm.msg_label2Rule, "04"); // ∅ 不参与匹配 diff --git a/main.h b/main.h index 6667f63..39abf52 100644 --- a/main.h +++ b/main.h @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts) char buf; int wdt_val;