Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148544
  • 博文数量: 34
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 269
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-06 10:49
文章分类
文章存档

2011年(12)

2010年(22)

我的朋友

分类: LINUX

2010-11-23 14:24:12

许多模块中会调用两个函数daemonize和allow_signal,作用如下:
 
来自:linux-2.6.18\kernel\exit.c
/*
 * Put all the gunge required to become a kernel thread without
 * attached user resources in one place where it belongs.
 */
void daemonize(const char *name, ...)
{
 va_list args;
 struct fs_struct *fs;
 sigset_t blocked;
 va_start(args, name);
 vsnprintf(current->comm, sizeof(current->comm), name, args);
 va_end(args);
 /*
  * If we were started as result of loading a module, close all of the
  * user space pages.  We don't need them, and if we didn't close them
  * they would be locked into memory.
  */
 exit_mm(current);    /*释放用户空间内存*/
 set_special_pids(1, 1);
 mutex_lock(&tty_mutex);
 current->signal->tty = NULL;
 mutex_unlock(&tty_mutex);
 /* Block and flush all signals */  /* 禁用所有信号 */
 sigfillset(&blocked);      
 sigprocmask(SIG_BLOCK, &blocked, NULL);
 flush_signals(current);
 /* Become as one with the init task */
 exit_fs(current); /* current->fs->count--; */ /* 释放文件 */
 fs = init_task.fs;
 current->fs = fs;
 atomic_inc(&fs->count);
 exit_namespace(current); /* 命名空间 */
 current->namespace = init_task.namespace;
 get_namespace(current->namespace);
  exit_files(current);
 current->files = init_task.files;
 atomic_inc(¤t->files->count);
 reparent_to_init();  /* 父进程改为init进程 */
}
 
 
 
 
/*
 * Let kernel threads use this to say that they
 * allow a certain signal (since daemonize() will
 * have disabled all of them by default).
 */
int allow_signal(int sig)
{
 if (!valid_signal(sig) || sig < 1)
  return -EINVAL;
 spin_lock_irq(¤t->sighand->siglock);
 sigdelset(¤t->blocked, sig);
 if (!current->mm) {
  /* Kernel threads handle their own signals.
     Let the signal code know it'll be handled, so
     that they don't get converted to SIGKILL or
     just silently dropped */
  current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
 }
 recalc_sigpending();
 spin_unlock_irq(¤t->sighand->siglock);
 return 0;
}
阅读(6542) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-24 14:32:03

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com