许多模块中会调用两个函数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;
}
阅读(6587) | 评论(1) | 转发(0) |