Chinaunix首页 | 论坛 | 博客
  • 博客访问: 971684
  • 博文数量: 192
  • 博客积分: 3070
  • 博客等级: 中校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-27 23:44
个人简介

Start Linux Leave Linux a while Back to Linux

文章分类

全部博文(192)

文章存档

2023年(18)

2022年(11)

2021年(8)

2020年(14)

2019年(7)

2018年(13)

2017年(16)

2016年(4)

2012年(2)

2011年(13)

2010年(26)

2009年(13)

2008年(27)

2007年(20)

我的朋友

分类: LINUX

2019-03-01 11:40:11

1. 创建 monitor_fw_pthread.c 
    用于检测:
    a. 是否已插入U盘
    b. 是否发现新的固件 "NEW_FW_MAA"
    c. 复制固件到指定的目录下 "/usr/myinst/"

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <stdbool.h>
  6. //#include <io.h>

  7. #define WIN_FIRMWARE_NAME "NEW_FW_MAA"
  8. #define WIN_TARGET_PATH "/usr/myinst/"

  9. static char g_fwPath[255];


  10. static int execmd(char* cmd,char* result)
  11. {
  12.     char buffer[128]; //定义缓冲区
  13.     FILE* pipe = popen(cmd, "r"); //打开管道,并执行命令
  14.     if (!pipe)
  15.         return 0; //返回0表示运行失败

  16.     while(!feof(pipe)) {
  17.         if(fgets(buffer, 128, pipe)){ //将管道输出到result中
  18.             strcat(result,buffer);
  19.         }
  20.     }
  21.     
  22.     pclose(pipe); //关闭管道
  23.     return 1; //返回1表示运行成功
  24. }



  25. // is removed udisk ?
  26. bool remove_udisk()
  27. {
  28.     bool ret = false;
  29.     char cmd[255], result[1024];

  30.     memset(cmd, 0x00, sizeof(cmd));
  31.     memset(result, 0x00, sizeof(result));
  32.     sprintf(cmd, "%s", "mount | grep /media/root");
  33.     execmd(cmd, result);
  34.     if(strlen(result) > 10)
  35.     {
  36.         printf("Info: UDisk until alive. \n");
  37.     }
  38.     else
  39.     {
  40.         printf("Info: UDisk was removed. \n");
  41.         ret = true;
  42.     }
  43.     return ret;
  44. }

  45. bool found_udisk()
  46. {
  47.     bool ret = false;
  48.     char cmd[255], result[1024];
  49.     char *p;
  50.     int i = 0;
  51.     memset(cmd, 0x00, sizeof(cmd));
  52.     memset(result, 0x00, sizeof(result));
  53.     
  54.     sprintf(cmd, "%s", "mount | grep /media/root");
  55.     execmd(cmd, result);

  56.     p = strstr(result, "/media/root/");
  57.     if(p)
  58.     {
  59.         printf("Result: %s \n", result);
  60.         printf("Info: Found an udisk insert. \n");
  61.         memset(g_fwPath, 0x00, sizeof(g_fwPath));

  62.         while(*p != 0x20)
  63.         {
  64.             g_fwPath[i] = *p;
  65.             p++;
  66.             i++;
  67.         }
  68.         printf("Fw path: %s \n", g_fwPath);
  69.         
  70.         ret = true;
  71.     }

  72.     return ret;
  73. }

  74. bool found_firmware()
  75. {
  76.     bool ret = false;
  77.     char fileName[255];
  78.     memset(fileName, 0x00, sizeof(fileName));
  79.     sprintf(fileName, "%s/%s", g_fwPath, WIN_FIRMWARE_NAME);
  80.     if(access(fileName, F_OK) == 0)
  81.     {
  82.         ret = true;
  83.         printf("^_^ Found the new firmware. \n");
  84.     }
  85.     else
  86.     {
  87.         printf("^?^ Not found the new firmware. \n");
  88.     }
  89.     return ret;
  90. }

  91. void copy_firmware()
  92. {
  93.     char fileName[255], cmd[255];
  94.     memset(fileName, 0x00, sizeof(fileName));
  95.     memset(cmd, 0x00, sizeof(cmd));
  96.     sprintf(fileName, "%s/%s", g_fwPath, WIN_FIRMWARE_NAME);
  97.     sprintf(cmd, "cp %s %s/MAAV", fileName, WIN_TARGET_PATH);
  98.     system(cmd);
  99.     printf("copy firmware: %s to %s done. \n", fileName, WIN_TARGET_PATH);
  100. }


  101. int main(int argc, char* argv[])
  102. {
  103.     bool isIn = false;
  104.     bool isRemove = false;
  105.     bool isNewFw = false;
  106.     memset(g_fwPath, 0x00, sizeof(g_fwPath));
  107.     
  108.     while(1)
  109.     {
  110.         if(isIn)
  111.         {
  112.             isRemove = remove_udisk();
  113.             if(isRemove)
  114.             {
  115.                 isRemove = false;
  116.                 isIn = false;
  117.             }
  118.         }
  119.         else
  120.         {
  121.             isIn = found_udisk();
  122.             if(isIn)
  123.             {
  124.                 sleep(1);
  125.                 isNewFw = found_firmware();
  126.                 if(isNewFw)
  127.                 {
  128.                     sleep(1);
  129.                     copy_firmware();
  130.                 }
  131.             }
  132.         }

  133.         sleep(2);
  134.     }

  135. }
2. 在 /root/.profile 下加入 
/usr/lginst/monitor_fw_pthread &
阅读(5124) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~