upgrade.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <dirent.h>
  15. #include "upgrade.h"
  16. #include "md5.h"
  17. #if 0
  18. #define LOGD log_print
  19. #define LOGE log_print
  20. #else
  21. #define LOGD printf
  22. #define LOGE printf
  23. #endif
  24. typedef struct {
  25. char *pbuf;
  26. int dlen;
  27. int blen;
  28. }node_t;
  29. typedef struct {
  30. uint32_t src; //
  31. uint8_t obj; //升级对象,解压后扫描文件,赋值到该变量
  32. uint8_t stat; //当前升级所处状态
  33. uint8_t step; //当前升级所处阶段
  34. pthread_t tid;
  35. FILE* log;
  36. }upg_handle_t;
  37. static upg_handle_t upgHandle;
  38. static int log_print(const char *fmt, ...);
  39. static int node_free(node_t *nd)
  40. {
  41. if(!nd) {
  42. return -1;
  43. }
  44. free(nd->pbuf);
  45. nd->pbuf = NULL;
  46. nd->dlen = 0;
  47. nd->blen = 0;
  48. return 0;
  49. }
  50. static int exe_cmd(char *cmd, node_t *nd)
  51. {
  52. int r=0;
  53. FILE *fp=NULL;
  54. char *pbuf=NULL;
  55. int onelen=1000;
  56. int dlen=0,blen=onelen;
  57. pbuf = malloc(onelen);
  58. if(!pbuf) {
  59. return -1;
  60. }
  61. fp = popen(cmd, "r");
  62. if (fp == NULL) {
  63. return -1;
  64. }
  65. while(1) {
  66. r = fread(pbuf+dlen, sizeof(char), onelen, fp);
  67. if(r<=0) {
  68. LOGE("upgrade relloac %d failed\n", dlen+onelen);
  69. break;
  70. }
  71. dlen += r;
  72. if(dlen+onelen>blen) {
  73. char* p = realloc(pbuf, dlen+onelen);
  74. if(!p) {
  75. LOGE("upgrade relloac %d failed\n", dlen+onelen);
  76. free(pbuf); pclose(fp);
  77. return -1;
  78. }
  79. pbuf = p;
  80. blen = dlen+onelen;
  81. }
  82. }
  83. pclose(fp);
  84. nd->pbuf = pbuf;
  85. nd->dlen = dlen;
  86. nd->blen = blen;
  87. return 0;
  88. }
  89. static int exe_cmd2(char *cmd)
  90. {
  91. return system(cmd);
  92. }
  93. //https://blog.csdn.net/qq_38650447/article/details/136284071
  94. static int log_init(void)
  95. {
  96. char *path=UPG_LOG_PATH;
  97. char tmp[200];
  98. if (upgHandle.log) {
  99. return -1;
  100. }
  101. snprintf(tmp, sizeof(tmp), "mkdir -p %s", path);
  102. system(tmp);
  103. upgHandle.log = fopen(path, "wt");
  104. if (!upgHandle.log) {
  105. LOGE("%s open failed\n", path);
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. static int log_print(const char *fmt, ...)
  111. {
  112. int r;
  113. va_list va;
  114. char buf[1000];
  115. va_start(va, fmt);
  116. r = vsprintf(buf, fmt, va);
  117. va_end(va);
  118. if (upgHandle.log) {
  119. fprintf(upgHandle.log, "%s", buf);
  120. }
  121. else {
  122. printf("%s", buf);
  123. }
  124. return 0;
  125. }
  126. static int log_flush(void)
  127. {
  128. if (upgHandle.log) {
  129. return -1;
  130. }
  131. fflush(upgHandle.log);
  132. return 0;
  133. }
  134. static int file_exist(char* path)
  135. {
  136. int r;
  137. struct stat st;
  138. r = stat(path, &st);
  139. if (r == 0) {
  140. return 1;
  141. }
  142. else {
  143. return 0;
  144. }
  145. }
  146. static int file_length(char* path)
  147. {
  148. int r;
  149. struct stat st;
  150. r = stat(path, &st);
  151. if (r) {
  152. return -1;
  153. }
  154. return st.st_size;
  155. }
  156. static int file_touch(int id)
  157. {
  158. int r;
  159. char tmp[200];
  160. snprintf(tmp, sizeof(tmp), "touch %s/s%d", UPG_FOLDER, id);
  161. return system(tmp); // 使用写入模式创建一个新文件
  162. }
  163. static int file_untouch(int id)
  164. {
  165. int r;
  166. char tmp[200];
  167. snprintf(tmp, sizeof(tmp), "rm %s/s%d", UPG_FOLDER, id);
  168. return system(tmp); // 使用写入模式创建一个新文件
  169. }
  170. static int folder_init(void)
  171. {
  172. char tmp[200];
  173. snprintf(tmp, sizeof(tmp), "mkdir -p %s", RUN_FOLDER);
  174. system(tmp);
  175. snprintf(tmp, sizeof(tmp), "mkdir -p %s", BAK_FOLDER);
  176. system(tmp);
  177. snprintf(tmp, sizeof(tmp), "mkdir -p %s", UPG_FOLDER);
  178. system(tmp);
  179. snprintf(tmp, sizeof(tmp), "mkdir -p %s", DOWN_FOLDER);
  180. system(tmp);
  181. return 0;
  182. }
  183. static int file_save(char *path, void *buf, int len)
  184. {
  185. int r;
  186. FILE *fp;
  187. fp = fopen(path, "wb");
  188. if(!fp) {
  189. LOGE("file_save, fopen %s failed\n", path);
  190. return -1;
  191. }
  192. r = fwrite(buf, 1, len, fp);
  193. if(r!=len) {
  194. LOGE("file_save %s failed\n", path);
  195. r = -1;
  196. }
  197. else {
  198. r = 0;
  199. }
  200. fclose(fp);
  201. return r;
  202. }
  203. static int file_load(char* path, void* buf, int len)
  204. {
  205. int r;
  206. FILE* fp;
  207. fp = fopen(path, "rb");
  208. if (!fp) {
  209. LOGE("file_load, fopen %s failed\n", path);
  210. return -1;
  211. }
  212. r = fread(buf, 1, len, fp);
  213. if (r != len) {
  214. LOGE("file_load %s failed\n", path);
  215. r = -1;
  216. }
  217. else {
  218. r = 0;
  219. }
  220. fclose(fp);
  221. return r;
  222. }
  223. static int file_remove(char *path)
  224. {
  225. return remove(path);
  226. }
  227. /*
  228. tar pdutar to run/
  229. */
  230. static int file_cover(char *path)
  231. {
  232. int r;
  233. char cmd[200];
  234. snprintf(cmd, sizeof(cmd), "tar -xpf %s -C / > /dev/null 2>&1", path);
  235. r = system(cmd);
  236. if(r) {
  237. LOGE("%s failed\n", cmd);
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. static int dir_is_empty(char *path)
  243. {
  244. struct dirent *entry;
  245. int is_empty = 1;
  246. DIR *dir = opendir(path);
  247. if (dir == NULL) {
  248. perror("opendir failed\n");
  249. return -1; // 错误处理
  250. }
  251. while ((entry = readdir(dir)) != NULL) {
  252. // 忽略'.'和'..'
  253. if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  254. is_empty = 0;
  255. break;
  256. }
  257. }
  258. closedir(dir);
  259. return is_empty;
  260. }
  261. //when app run success, copy run/* to bak/
  262. static int file_backup(void)
  263. {
  264. int r;
  265. char tmp[100];
  266. char cmd[200];
  267. //dl目录有tar压缩包,则拷贝到bak目录
  268. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  269. if(file_exist(tmp)) {
  270. LOGD("%s exist\n", tmp);
  271. snprintf(cmd, sizeof(cmd), "cp -f %s %s/", tmp, BAK_FOLDER);
  272. r = system(cmd);
  273. if(r==0) {
  274. return 0;
  275. }
  276. LOGD("cp %s to %s failed\n", tmp, BAK_FOLDER);
  277. }
  278. else {
  279. LOGD("%s not exist\n", tmp);
  280. }
  281. //bak目录有tar压缩包,则退出
  282. snprintf(tmp, sizeof(tmp), "%s/%s", BAK_FOLDER, UPG_TAR_NAME);
  283. if(file_exist(tmp)) {
  284. LOGD("%s exist\n", tmp);
  285. return 0;
  286. }
  287. else {
  288. LOGD("%s not exist\n", tmp);
  289. }
  290. //bak目录没有tar压缩包,则将run目录打包存进bak目录
  291. LOGD("tar %s to %s/%s\n", RUN_FOLDER, BAK_FOLDER, UPG_TAR_NAME);
  292. snprintf(cmd, sizeof(cmd), "tar -czpPf %s/%s %s > /dev/null 2>&1", BAK_FOLDER, UPG_TAR_NAME, RUN_FOLDER);
  293. r = system(cmd);
  294. if(r) {
  295. LOGE("%s failed, %d\n", cmd, r);
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. /*
  301. when app run failed, copy to bak/ run/
  302. */
  303. static int file_restore(void)
  304. {
  305. int r;
  306. char cmd[200];
  307. snprintf(cmd, sizeof(cmd), "tar -xpf %s/%s -C / > /dev/null 2>&1", DOWN_FOLDER, UPG_TAR_NAME);
  308. r = system(cmd);
  309. if(r) {
  310. LOGE("%s failed, %d\n", cmd, r);
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. #define CHAR(b) (((b)<=9)?((b)+0x30):((b)-10+0x61));
  316. static void byte2char(char *chr, uint8_t b)
  317. {
  318. chr[0] = CHAR(b>>4);
  319. chr[1] = CHAR(b&0x0f);
  320. }
  321. static void print_md5(char *s, md5_t *m)
  322. {
  323. int i;
  324. LOGD("__%s", s);
  325. for(i=0; i<32; i++) {
  326. LOGD("%c", m->data[i]);
  327. }
  328. LOGD("\n");
  329. }
  330. //md5相同返回0,否则返回1
  331. static int md5_check(upgrade_file_t *h)
  332. {
  333. md5_ctx_t ctx;
  334. uint8_t i,tmp[16];
  335. md5_t m1,m2;
  336. md5_init(&ctx);
  337. md5_update(&ctx, (uint8_t*)h, h->dlen+sizeof(upgrade_file_t));
  338. md5_final(&ctx, tmp);
  339. for (i=0; i<16; i++) {
  340. byte2char(&m2.data[i*2], tmp[i]);
  341. }
  342. memcpy(&m1, h->data+h->dlen, sizeof(md5_t));
  343. print_md5("ori md5: ", &m1);
  344. print_md5("cal md5: ", &m2);
  345. if(memcmp(&m1, &m2, sizeof(md5_t))) {
  346. return -1;
  347. }
  348. return 0;
  349. }
  350. static int file_split(void)
  351. {
  352. int r=0,flen;
  353. char *fbuf=NULL;
  354. char path[200];
  355. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  356. flen = file_length(path);
  357. if(flen<=0) {
  358. LOGE("file_length %s failed\n", path);
  359. return -1;
  360. }
  361. fbuf = (char*)malloc(flen);
  362. if(!fbuf) {
  363. LOGE("malloc %d failed\n", flen);
  364. return -1;
  365. }
  366. r = file_load(path, fbuf, flen);
  367. if(r) {
  368. LOGE("%s load failed\n", path);
  369. return -1;
  370. }
  371. upgrade_file_t *hfile = (upgrade_file_t*)fbuf;
  372. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
  373. r = file_save(path, hfile, sizeof(upgrade_file_t));
  374. if(r) {
  375. LOGE("file_save %s failed\n", path);
  376. return -1;
  377. }
  378. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  379. r = file_save(path, hfile->data, hfile->dlen);
  380. if(r) {
  381. LOGE("file_save %s failed\n", path);
  382. return -1;
  383. }
  384. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  385. r = file_remove(path);
  386. if(r) {
  387. LOGE("file_remove %s failed\n", path);
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. static int file_check(char *path)
  393. {
  394. int r=0,flen;
  395. char *fbuf=NULL;
  396. upgrade_file_t *hfile=NULL;
  397. flen = file_length(path);
  398. if(flen<=0) {
  399. LOGE("file_length %s failed\n", path);
  400. return -1;
  401. }
  402. fbuf = (char*)malloc(flen);
  403. if(!fbuf) {
  404. LOGE("malloc %d failed\n", flen);
  405. return -1;
  406. }
  407. r = file_load(path, fbuf, flen);
  408. if(r) {
  409. LOGE("%s load failed\n", path);
  410. return -1;
  411. }
  412. hfile = (upgrade_file_t*)fbuf;
  413. if(hfile->magic!=UPG_FILE_MAGIC ||
  414. hfile->dlen!=flen-sizeof(upgrade_file_t)-sizeof(md5_t)) {
  415. r = -1;
  416. LOGE("magic: 0x%08x, should be: 0x%08x\n", hfile->magic, UPG_FILE_MAGIC);
  417. LOGE("num: %d, should < %d\n", hfile->obj.num, UPGRADE_NUM_MAX);
  418. LOGE("stra: %d, should < %d\n", hfile->obj.stra, UPGRADE_STRA_MAX);
  419. LOGE("file hdr check failed\n");
  420. goto fail;
  421. }
  422. r = md5_check(hfile);
  423. if(r) {
  424. LOGE("md5 check failed\n");
  425. goto fail;
  426. }
  427. fail:
  428. free(fbuf);
  429. return r;
  430. }
  431. static int file_scan(upg_handle_t *h)
  432. {
  433. //检查DOWN目录,如有升级文件
  434. //udisk检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
  435. //tcard检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
  436. return 0;
  437. }
  438. static int file_clear(void)
  439. {
  440. char tmp[200];
  441. snprintf(tmp, sizeof(tmp), "rm -r %s/* %s/*", DOWN_FOLDER, UPG_FOLDER);
  442. return system(tmp);
  443. }
  444. //////////////////////////////////////////////////
  445. static int app_start(void)
  446. {
  447. return 0;//system("smartMonitor.sh &");
  448. }
  449. static int app_stop(void)
  450. {
  451. return 0;//system("killall smartMonitor.sh && /etc/init.d/S90PDUApp stop");
  452. }
  453. //////////////////////////////////////////////////
  454. static void* upg_thread(void *arg)
  455. {
  456. int r=0;
  457. int msgid;
  458. key_t key;
  459. msgbuf_t mb;
  460. upg_handle_t *h=(upg_handle_t*)arg;
  461. LOGD("upg_thread running\n");
  462. key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
  463. if(key<0) {
  464. LOGE("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
  465. pthread_exit(NULL);
  466. }
  467. msgid = msgget(key, 0666|IPC_CREAT);
  468. LOGD("___ key: %d, msgid: %d\n", key, msgid);
  469. //msgctl(msgid, IPC_RMID, NULL);
  470. while(1) {
  471. r = msgrcv(msgid, &mb, sizeof(mb.evt), 0, 0);
  472. if(r<0) {
  473. LOGE("___msgrcv failed, %d\n", errno);
  474. continue;
  475. }
  476. LOGD("___ msgrcv evt: 0x%02x\n", mb.evt);
  477. switch(mb.evt) {
  478. case UPGRADE_EVT_APP_RUNNING:
  479. {
  480. LOGD("___ smartPDU running!\n");
  481. h->step = UPGRADE_STEP_SUCCESS;
  482. }
  483. break;
  484. case UPGRADE_EVT_UPLOAD_FINISH:
  485. {
  486. LOGD("___ upgrade package upload finished!\n");
  487. h->step = UPGRADE_STEP_DOWN;
  488. file_touch(UPGRADE_STEP_DOWN);
  489. }
  490. break;
  491. }
  492. }
  493. pthread_exit(NULL);
  494. }
  495. static int upg_init(upg_handle_t *h)
  496. {
  497. int r;
  498. folder_init();
  499. h->src = UPGRADE_SRC_NONE;
  500. h->stat = UPGRADE_STAT_NONE;
  501. return pthread_create(&h->tid, NULL, upg_thread, h);
  502. }
  503. static int upg_check(upg_handle_t *h)
  504. {
  505. int i,r;
  506. char tmp[200];
  507. for(i=1; i<UPGRADE_STEP_MAX; i++) {
  508. snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, i);
  509. r = file_exist(tmp);
  510. if(r) {
  511. LOGD("%s exist\n", tmp);
  512. //重启后如果是覆盖完成,则将进度置为SUCCESS
  513. h->step = i+1;
  514. break;
  515. }
  516. LOGD("%s not exist\n", tmp);
  517. }
  518. return r;
  519. }
  520. static int upg_handle(upg_handle_t *h)
  521. {
  522. int r=-1;
  523. char tmp[200];
  524. LOGD("___h->step: %d\n", h->step);
  525. while(h->step!=UPGRADE_STEP_NONE) {
  526. switch(h->step) {
  527. case UPGRADE_STEP_DOWN:
  528. {
  529. LOGD("___ UPGRADE_STEP_DOWN\n");
  530. //先检查下载目录是否存在pdupkg文件
  531. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  532. if(file_exist(tmp)) {
  533. file_touch(UPGRADE_STEP_DOWN);
  534. h->step = UPGRADE_STEP_SPLIT;
  535. }
  536. else {
  537. LOGD("___ %s not exist, ", tmp);
  538. //下载目录不存在pdupkg文件,则继续检查是否存在pdutar文件
  539. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  540. LOGD("read %s\n", tmp);
  541. if(file_exist(tmp)) {
  542. file_touch(UPGRADE_STEP_DOWN);
  543. h->step = UPGRADE_STEP_COVER;
  544. }
  545. else {
  546. file_untouch(UPGRADE_STEP_DOWN);
  547. h->step = UPGRADE_STEP_NONE;
  548. }
  549. }
  550. }
  551. break;
  552. case UPGRADE_STEP_SPLIT:
  553. {
  554. LOGD("___ UPGRADE_STEP_SPLIT\n");
  555. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  556. r = file_check(tmp);
  557. if(r==0) {
  558. r = file_split();
  559. if(r==0) {
  560. file_touch(UPGRADE_STEP_SPLIT);
  561. h->step = UPGRADE_STEP_COVER;
  562. }
  563. else {
  564. file_untouch(UPGRADE_STEP_SPLIT);
  565. h->step = UPGRADE_STEP_NONE;
  566. }
  567. }
  568. else {
  569. file_remove(tmp);
  570. h->step = UPGRADE_STEP_NONE;
  571. }
  572. file_untouch(UPGRADE_STEP_DOWN);
  573. }
  574. break;
  575. case UPGRADE_STEP_COVER:
  576. {
  577. LOGD("___ UPGRADE_STEP_COVER\n");
  578. app_stop();
  579. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  580. r = file_cover(tmp);
  581. if(r==0) {
  582. file_touch(UPGRADE_STEP_COVER);
  583. h->step = UPGRADE_STEP_FINISH;
  584. app_start();
  585. }
  586. else {
  587. LOGD("file_cover, delete %s!\n", tmp);
  588. file_remove(tmp);
  589. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
  590. file_remove(tmp);
  591. h->step = UPGRADE_STEP_NONE;
  592. file_untouch(UPGRADE_STEP_COVER);
  593. file_restore();
  594. app_start();
  595. }
  596. file_untouch(UPGRADE_STEP_DOWN);
  597. file_untouch(UPGRADE_STEP_SPLIT);
  598. }
  599. break;
  600. case UPGRADE_STEP_FINISH:
  601. {
  602. LOGD("___ UPGRADE_STEP_FINISH\n");
  603. h->step = UPGRADE_STEP_NONE;
  604. }
  605. break;
  606. case UPGRADE_STEP_SUCCESS:
  607. {
  608. LOGD("___ UPGRADE_STEP_SUCCESS\n");
  609. h->step = UPGRADE_STEP_NONE;
  610. file_backup(); //启动成功才证明程序有效,可进行备份, 备份成功,删除下载目录中的文件以节省空间
  611. file_clear();
  612. app_start();
  613. }
  614. break;
  615. default:
  616. LOGD("___ wrong step: %d\n", h->step);
  617. r = 1;
  618. break;
  619. }
  620. }
  621. return r;
  622. }
  623. /////////////////////////////////////////////////////////////////////////
  624. int main(void)
  625. {
  626. int id,r=0;
  627. upg_handle_t *h=&upgHandle;
  628. LOGD("___upgrade running ...\n");
  629. upg_init(h);
  630. upg_check(h);
  631. while(1) {
  632. upg_handle(h);
  633. sleep(1);
  634. }
  635. return 0;
  636. }