void       g_main_loop_run        (GMainLoop    *loop)
{
 int open_max = sysconf(_SC_OPEN_MAX);
 struct pollfd *ufds = malloc(open_max * sizeof(struct pollfd));

 while (!loop->bail) {//外层主循环,本循环内没有函数改变loop的值,

//所以这是个无限循环;

//我猜测hcid->main.c中关于信号的处理将影响loop的值,

 memset(&sa, 0, sizeof(sa));
 sa.sa_flags = SA_NOCLDSTOP;
 sa.sa_handler = sig_term;
 //sigaction change default action by system when the first para signal happened;
 sigaction(SIGTERM, &sa, NULL);
 sigaction(SIGINT,  &sa, NULL);

sig_term的操作很简单loop->bail=1,这样g_main_loop_run就会退出无限循环;

比如,hcid是一个deamon,我们可以调用kill向hcid进程发出SIGTERM信号;
  int nfds, rc, i;
  struct watch *p, *w;

  nfds = 0;
  for (w = watch_head.next; w != NULL; w = w->next) {
   ufds[nfds].fd = w->channel->fd;
   ufds[nfds].events = w->condition;
   ufds[nfds].revents = 0;
   nfds++;
  }
  
  rc = poll(ufds, nfds, -1);

  if (rc < 0 && (errno == EINTR))

//poll’s man page http://jamesthornton.com/linux/man/poll.2.html

//EINTR A signal occurred before any requested event. 
   continue;

  if (rc < 0) {
   perror(“poll”);
   continue;
  }
 //poll的返回值大于0,是正常情况,我们处理;注意中文翻译书出错 
  p = &watch_head;
  w = watch_head.next;
  i = 0;
  while (w) {
   if (ufds[i].revents) {
    gboolean keep = w->func(w->channel, ufds[i].revents, w->user_data);
    if (!keep) {
      p->next = w->next;
      memset(w, 0, sizeof(*w));
      w = p->next;
      i++;
      continue;
    }
   } 
  
   p = w;
   w = w->next;
   i++;
  }

 }

 free(ufds);
}


评论

该日志第一篇评论

发表评论

评论也有版权!