lock_kernel();
siginitsetinv(¤t->blocked, SHUTDOWN_SIGS);
daemonize();
target_data.thread_id = current;
unlock_kernel();
注:SHUTDOWN_SIGS前面定义为# define SHUTDOWN_SIGS (sigmask(SIGKILL)|sigmask(SIGINT)|sigmask(SIGTERM)) target_data是前面定义的一个全局数据结构,thread_id是它的一个task_struct 结构的一个成员。这里的daemonize函数是什么作用?
1. linux-2.4.20\kernel\sched.c start from line 1283: 2. 3. /* 4. * Put all the gunge required to become a kernel thread without 5. * attached user resources in one place where it belongs. 6. */ 7. 8. void daemonize(void) 9. { 10. struct fs_struct *fs; 11. 12. 13. /* 14. * If we were started as result of loading a module, close all of the 15. * user space pages. We don't need them, and if we didn't close them 16. * they would be locked into memory. 17. */ 18. exit_mm(current); //因为是内核级daemon,所以不会再需要用户级的内存页面,释放之。这些页面是因为加载模块而申请到的。 19. 20. current->session = 1; 21. current->pgrp = 1; 22. current->tty = NULL;//因为是内核级daemon,所以不再需要终端控制台,所属会话为init会话,parent group为1(init),这样就成为init任务的一部分了。下面的代码则是释放文件系统相关资源,并且转为指向init任务的文件资源。 23. /* Become as one with the init task */ 24. 25. exit_fs(current); /* current->fs->count--; */ 26. fs = init_task.fs; 27. current->fs = fs; 28. atomic_inc(&fs->count); 29. exit_files(current); 30. current->files = init_task.files; 31. atomic_inc(*t->files->count); 32. }