《转》OpenWrt 系统日志之logread

转自:http://blog.csdn.net/qianguozheng/article/details/38498633

前言

刚开始接触OpenWrt的时候,根本不知道如何调试各个进程,我之前从事IP Camera开发可能也局限了我的知识面,认为系统就改是那个样子。

其实不然,就像Linux发行版那样,他们都有各自都管理系统,同一个的消息通知系统,dbus和ubus这些。系统调试也是一样dmesg, 现在还接触到了logread。


初探

logread是在调试luci的时候用到的,极其方便,对于不太了解OpenWrt系统构成对人尤甚。

这个需要写进程对人对syslogd提供支持,否则说来知识惘然,我们需要做系统,需要做好对系统,就需要油完善对日志管理,精简无冗余对才是最有用的,这是我们使用其的目的。废话不多说,直接看卡logread的组成吧。


在busybox中实现了syslogd 和 logread.

syslogd用来记录log, logged则用来读取log.


logread的代码很简洁,主要实现过程是:连接共享内存->信号量加锁->读取共享内存中的信息并输出->信号量解锁。


连接共享内存

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span>log_shmid = shmget(KEY_ID, 0, 0);  
  2.     if (log_shmid == -1)  
  3.         bb_perror_msg_and_die("can't %s syslogd buffer""find");  
  4.   
  5.     /* Attach shared memory to our char* */  
  6.     shbuf = shmat(log_shmid, NULL, SHM_RDONLY);  
  7.     if (shbuf == NULL)  
  8.         bb_perror_msg_and_die("can't %s syslogd buffer""access");  
  9.   
  10.     log_semid = semget(KEY_ID, 0, 0);  
  11.     if (log_semid == -1)  
  12.         error_exit("can't get access to semaphores for syslogd buffer");  

信号量加锁

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span>if (semop(log_semid, SMrdn, 2) == -1)  
  2.         error_exit("semop[SMrdn]");  

读取共享内存中的信息并输出

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     /* Suppose atomic memory read */  
  2.     /* Max possible value for tail is shbuf->size - 1 */  
  3.     cur = shbuf->tail;  
  4.   
  5.     /* Loop for logread -f, one pass if there was no -f */  
  6.     do {  
  7.         unsigned shbuf_size;  
  8.         unsigned shbuf_tail;  
  9.         const char *shbuf_data;  
  10. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING  
  11.         int i;  
  12.         int len_first_part;  
  13.         int len_total = len_total; /* for gcc */  
  14.         char *copy = copy; /* for gcc */  
  15. #endif  
  16.         if (semop(log_semid, SMrdn, 2) == -1)  
  17.             error_exit("semop[SMrdn]");  
  18.   
  19.         /* Copy the info, helps gcc to realize that it doesn't change */  
  20.         shbuf_size = shbuf->size;  
  21.         shbuf_tail = shbuf->tail;  
  22.         shbuf_data = shbuf->data; /* pointer! */  
  23.   
  24.         if (DEBUG)  
  25.             printf("cur:%u tail:%u size:%u\n",  
  26.                     cur, shbuf_tail, shbuf_size);  
  27.   
  28.         if (!follow) {  
  29.             /* advance to oldest complete message */  
  30.             /* find NUL */  
  31.             cur += strlen(shbuf_data + cur);  
  32.             if (cur >= shbuf_size) { /* last byte in buffer? */  
  33.                 cur = strnlen(shbuf_data, shbuf_tail);  
  34.                 if (cur == shbuf_tail)  
  35.                     goto unlock; /* no complete messages */  
  36.             }  
  37.             /* advance to first byte of the message */  
  38.             cur++;  
  39.             if (cur >= shbuf_size) /* last byte in buffer? */  
  40.                 cur = 0;  
  41.         } else { /* logread -f */  
  42.             if (cur == shbuf_tail) {  
  43.                 sem_up(log_semid);  
  44.                 fflush_all();  
  45.                 sleep(1); /* TODO: replace me with a sleep_on */  
  46.                 continue;  
  47.             }  
  48.         }  
  49.   
  50.         /* Read from cur to tail */  
  51. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING  
  52.         len_first_part = len_total = shbuf_tail - cur;  
  53.         if (len_total < 0) {  
  54.             /* message wraps: */  
  55.             /* [SECOND PART.........FIRST PART] */  
  56.             /*  ^data      ^tail    ^cur      ^size */  
  57.             len_total += shbuf_size;  
  58.         }  
  59.         copy = xmalloc(len_total + 1);  
  60.         if (len_first_part < 0) {  
  61.             /* message wraps (see above) */  
  62.             len_first_part = shbuf_size - cur;  
  63.             memcpy(copy + len_first_part, shbuf_data, shbuf_tail);  
  64.         }  
  65.         memcpy(copy, shbuf_data + cur, len_first_part);  
  66.         copy[len_total] = '\0';  
  67.         cur = shbuf_tail;  
  68. #else  
  69.         while (cur != shbuf_tail) {  
  70.             fputs(shbuf_data + cur, stdout);  
  71.             cur += strlen(shbuf_data + cur) + 1;  
  72.             if (cur >= shbuf_size)  
  73.                 cur = 0;  
  74.         }  
  75. #endif  


信号量解锁

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.  unlock:  
  2.         /* release the lock on the log chain */  
  3.         sem_up(log_semid);  
  4.   
  5. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING  
  6.         for (i = 0; i < len_total; i += strlen(copy + i) + 1) {  
  7.             fputs(copy + i, stdout);  
  8.         }  
  9.         free(copy);  
  10. #endif  
  11.         fflush_all();  


http://man.cx/syslogd(8)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值