|
|
@@ -1,28 +1,33 @@
|
|
|
-#include <stdlib.h>
|
|
|
+#include <stdlib.h>
|
|
|
#include <stdio.h>
|
|
|
#include <pthread.h>
|
|
|
-#include <sys/types.h>
|
|
|
-#include <sys/stat.h>
|
|
|
+#include <sys/types.h>
|
|
|
+#include <sys/stat.h>
|
|
|
#include <sys/ipc.h>
|
|
|
#include <sys/msg.h>
|
|
|
-#include <fcntl.h>
|
|
|
+#include <fcntl.h>
|
|
|
#include <errno.h>
|
|
|
-#include <unistd.h>
|
|
|
+#include <unistd.h>
|
|
|
#include <signal.h>
|
|
|
-#include <string.h>
|
|
|
+#include <string.h>
|
|
|
#include <stdarg.h>
|
|
|
#include <dirent.h>
|
|
|
#include "upgrade.h"
|
|
|
#include "md5.h"
|
|
|
|
|
|
|
|
|
+//#define LOG log_print
|
|
|
+#define LOG printf
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
typedef struct {
|
|
|
char *pbuf;
|
|
|
int dlen;
|
|
|
int blen;
|
|
|
}node_t;
|
|
|
-typedef struct {
|
|
|
- uint32_t src; //
|
|
|
+typedef struct {
|
|
|
+ uint32_t src; //
|
|
|
uint8_t obj; //升级对象,解压后扫描文件,赋值到该变量
|
|
|
uint8_t stat; //当前升级所处状态
|
|
|
uint8_t step; //当前升级所处阶段
|
|
|
@@ -30,14 +35,10 @@ typedef struct {
|
|
|
pthread_t tid;
|
|
|
FILE* log;
|
|
|
key_t key;
|
|
|
-}upg_handle_t;
|
|
|
+}upg_handle_t;
|
|
|
static upg_handle_t upgHandle;
|
|
|
static int log_print(const char *fmt, ...);
|
|
|
|
|
|
-//#define LOG log_print
|
|
|
-#define LOG printf
|
|
|
-
|
|
|
-
|
|
|
|
|
|
static int node_free(node_t *nd)
|
|
|
{
|
|
|
@@ -49,6 +50,8 @@ static int node_free(node_t *nd)
|
|
|
nd->pbuf = NULL;
|
|
|
nd->dlen = 0;
|
|
|
nd->blen = 0;
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
static int exe_cmd(char *cmd, node_t *nd)
|
|
|
{
|
|
|
@@ -123,12 +126,12 @@ static int log_init(void)
|
|
|
static int log_print(const char *fmt, ...)
|
|
|
{
|
|
|
int r;
|
|
|
- va_list va;
|
|
|
+ va_list va;
|
|
|
char buf[1000];
|
|
|
|
|
|
- va_start(va, fmt);
|
|
|
- r = vsprintf(buf, fmt, va);
|
|
|
- va_end(va);
|
|
|
+ va_start(va, fmt);
|
|
|
+ r = vsprintf(buf, fmt, va);
|
|
|
+ va_end(va);
|
|
|
|
|
|
if (upgHandle.log) {
|
|
|
fprintf(upgHandle.log, "%s", buf);
|
|
|
@@ -136,7 +139,7 @@ static int log_print(const char *fmt, ...)
|
|
|
else {
|
|
|
printf("%s", buf);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
static int log_flush(void)
|
|
|
@@ -147,7 +150,7 @@ static int log_flush(void)
|
|
|
fflush(upgHandle.log);
|
|
|
|
|
|
return 0;
|
|
|
-}
|
|
|
+}
|
|
|
static int file_exist(char* path)
|
|
|
{
|
|
|
int r;
|
|
|
@@ -166,13 +169,13 @@ static int file_length(char* path)
|
|
|
int r;
|
|
|
struct stat st;
|
|
|
|
|
|
- r = stat(path, &st);
|
|
|
+ r = stat(path, &st);
|
|
|
if (r) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
return st.st_size;
|
|
|
-}
|
|
|
+}
|
|
|
static int file_touch(int id)
|
|
|
{
|
|
|
int r;
|
|
|
@@ -214,14 +217,14 @@ static int file_save(char *path, void *buf, int len)
|
|
|
int r;
|
|
|
FILE *fp;
|
|
|
|
|
|
- fp = fopen(path, "wb");
|
|
|
+ fp = fopen(path, "wb");
|
|
|
if(!fp) {
|
|
|
- LOG("file_save, fopen %s failed, %d, %d\n", path);
|
|
|
+ LOG("file_save, fopen %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
r = fwrite(buf, 1, len, fp);
|
|
|
if(r!=len) {
|
|
|
- LOG("file_save %s failed, %d, %d\n", path, r, len);
|
|
|
+ LOG("file_save %s failed\n", path);
|
|
|
r = -1;
|
|
|
}
|
|
|
else {
|
|
|
@@ -236,14 +239,14 @@ static int file_load(char* path, void* buf, int len)
|
|
|
int r;
|
|
|
FILE* fp;
|
|
|
|
|
|
- fp = fopen(path, "rb");
|
|
|
+ fp = fopen(path, "rb");
|
|
|
if (!fp) {
|
|
|
- LOG("file_load, fopen %s failed, %d, %d\n", path);
|
|
|
+ LOG("file_load, fopen %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
r = fread(buf, 1, len, fp);
|
|
|
if (r != len) {
|
|
|
- LOG("file_load %s failed, %d, %d\n", path, r, len);
|
|
|
+ LOG("file_load %s failed\n", path);
|
|
|
r = -1;
|
|
|
}
|
|
|
else {
|
|
|
@@ -261,8 +264,8 @@ static int file_remove(char *path)
|
|
|
/*
|
|
|
tar pdutar to run/
|
|
|
*/
|
|
|
-static int file_cover(char *path)
|
|
|
-{
|
|
|
+static int file_cover(char *path)
|
|
|
+{
|
|
|
int r;
|
|
|
char cmd[200];
|
|
|
|
|
|
@@ -272,7 +275,7 @@ static int file_cover(char *path)
|
|
|
LOG("%s failed\n", cmd);
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -284,7 +287,7 @@ static int dir_is_empty(char *path)
|
|
|
|
|
|
DIR *dir = opendir(path);
|
|
|
if (dir == NULL) {
|
|
|
- perror("opendir");
|
|
|
+ perror("opendir failed\n");
|
|
|
return -1; // 错误处理
|
|
|
}
|
|
|
|
|
|
@@ -301,8 +304,8 @@ static int dir_is_empty(char *path)
|
|
|
}
|
|
|
|
|
|
//when app run success, copy run/* to bak/
|
|
|
-static int file_backup(void)
|
|
|
-{
|
|
|
+static int file_backup(void)
|
|
|
+{
|
|
|
int r;
|
|
|
char tmp[100];
|
|
|
char cmd[200];
|
|
|
@@ -337,28 +340,28 @@ static int file_backup(void)
|
|
|
snprintf(cmd, sizeof(cmd), "tar -czpPf %s/%s %s > /dev/null 2>&1", BAK_FOLDER, UPG_TAR_NAME, RUN_FOLDER);
|
|
|
r = system(cmd);
|
|
|
if(r) {
|
|
|
- LOG("% failed, %d\n", cmd, r);
|
|
|
+ LOG("%s failed, %d\n", cmd, r);
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
when app run failed, copy to bak/ run/
|
|
|
*/
|
|
|
-static int file_restore(void)
|
|
|
-{
|
|
|
+static int file_restore(void)
|
|
|
+{
|
|
|
int r;
|
|
|
char cmd[200];
|
|
|
|
|
|
snprintf(cmd, sizeof(cmd), "tar -xpf %s/%s -C / > /dev/null 2>&1", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
r = system(cmd);
|
|
|
if(r) {
|
|
|
- LOG("% failed, %d\n", cmd, r);
|
|
|
+ LOG("%s failed, %d\n", cmd, r);
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -388,7 +391,7 @@ static int md5_check(upgrade_file_t *h)
|
|
|
md5_t m1,m2;
|
|
|
|
|
|
md5_init(&ctx);
|
|
|
- md5_update(&ctx, (char*)h, h->dlen+sizeof(upgrade_file_t));
|
|
|
+ md5_update(&ctx, (uint8_t*)h, h->dlen+sizeof(upgrade_file_t));
|
|
|
md5_final(&ctx, tmp);
|
|
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
@@ -520,7 +523,7 @@ static int file_clear(void)
|
|
|
|
|
|
|
|
|
|
|
|
-//////////////////////////////////////////////////
|
|
|
+//////////////////////////////////////////////////
|
|
|
static int app_start(void)
|
|
|
{
|
|
|
return system("/etc/init.d/S90PDUApp start");
|
|
|
@@ -533,7 +536,7 @@ static int app_restart(void)
|
|
|
{
|
|
|
return system("/etc/init.d/S90PDUApp restart");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//////////////////////////////////////////////////
|
|
|
static void* upg_thread(void *arg)
|
|
|
{
|
|
|
@@ -586,8 +589,8 @@ static int upg_init(upg_handle_t *h)
|
|
|
if(key<0) {
|
|
|
LOG("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
|
|
|
}
|
|
|
- h->src = UPGRADE_SRC_NONE;
|
|
|
- h->stat = UPGRADE_STAT_NONE;
|
|
|
+ h->src = UPGRADE_SRC_NONE;
|
|
|
+ h->stat = UPGRADE_STAT_NONE;
|
|
|
h->key = key;
|
|
|
|
|
|
return pthread_create(&h->tid, NULL, upg_thread, h);
|
|
|
@@ -733,8 +736,8 @@ static int upg_handle(upg_handle_t *h)
|
|
|
return r;
|
|
|
}
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
-int main(void)
|
|
|
-{
|
|
|
+int main(void)
|
|
|
+{
|
|
|
int id,r=0;
|
|
|
upg_handle_t *h=&upgHandle;
|
|
|
|
|
|
@@ -746,8 +749,9 @@ int main(void)
|
|
|
upg_handle(h);
|
|
|
sleep(1);
|
|
|
}
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|