daemon reparented / init --user http://blog.csdn.net/ixidof/article/details/42806239
2015-10-03 16:36
621 查看
daemon reparented / init --user
分类: Linux2015-01-1716:31 234人阅读 评论(0) 收藏 举报
文章出处:https://www.osso.nl/blog/daemon-reparented-init-user/
While I was battling an obscure Ubuntu shutdown issue — more about that later — I noticed that daemonized jobs started from my X session were not reparented to PID 1
init, but to a custom
init --user, owned by me.
What? I cannot start daemon that outlive my X session?
That's right, I cannot. Check this out:
[html] view
plaincopy
$ sh -c 'sleep 61 &'
$ ps faxu | egrep 'init|sleep 61'
root 1 ... /sbin/init
walter 2198 ... \_ init --user
walter 6673 ... | | \_ egrep --color=auto init|sleep 61
walter 6671 ... \_ sleep 61
Okay then. What is this black magic?
It's apparently caused by
PR_SET_CHILD_SUBREAPER; available through
prctlsince Linux kernel 3.4. Ubuntu added that in Raring (13.04), according toRaring
Upstart User Sessions, PIDtracking.
Can I work around that?
Short answer: no, the
PR_SET_CHILD_SUBREAPERinterface allows a single process to enable or disable the feature, but not for someone else to disable it.
Long answer: yes, but only if we alter the subreaper state of the UserSession init; like this:
[html] view
plaincopy
$ sudo gdb `which init` `pgrep -xf 'init --user'` \
-batch -ex 'callprctl(36,0,0,0,0)'
Password:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007f3b7a6848c3 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:81
81 ../sysdeps/unix/syscall-template.S: No such file or directory.
$1 = 0
$ sh -c 'sleep 61 &'
$ ps faxu | egrep 'init|sleep 61'
root 1 ... /sbin/init
walter 2198 ... \_ init --user
walter 6986 ... | | \_ egrep --color=auto init|sleep 61
walter 6957 ... | \_ man 5 init
walter 6984 ... sleep 61
Hah!
sleep 61is now owned by PID 1 directly. By the way, reverting that hack is as easy as changing the second argument toprctl from 0 to1.
So, apparently I really am barred from creating PID 1 owned daemons unless I hack init --user.
That does raise the question how initctl daemons are spawned, but that's done by asking
/sbin/initto do that for us:
[html] view
plaincopy
# netstat -lnAunix | grep '/com/ubuntu/upstart$'
unix 2 [ ACC ] STREAM LISTENING 8049 @/com/ubuntu/upstart
# strace start cups
...
connect(3, {sa_family=AF_LOCAL, sun_path=@"/com/ubuntu/upstart"}, 22) = 0
...
cups start/running, process 6883
Yuck! Did I mention I'm glad we're moving to
systemd?
相关文章推荐
- GOF 23 设计模式之 责任链模式(Chain Of Responsibility)
- HUST-2015 Multi-University Training Contest 9
- Linux内核中的进程组及会话 http://blog.csdn.net/daniel_h1986/article/details/6067685
- Gradle project sync failed. Please fix your project and try again
- HDU 1023 Train Problem II(组合数学)
- AI设计---Robot(v1.0),简单对话的实现
- 一. 作业管理 http://blog.csdn.net/lwbeyond/article/details/7361850
- Linux进程控制 http://blog.csdn.net/kennyrose/article/details/7533534
- linux进程间通信方式之匿名管道http://blog.csdn.net/guoyang1007/article/details/4720984
- Aizu 2450 Do use segment tree LCT
- AI---个人主页
- 1079. Total Sales of Supply Chain (25) - 类树操作
- 基于显卡的光栅化渲染器Gaius计划
- Codeforces Round #320 (Div. 2) 579A. Raising Bacteria
- UVa253 Cube painting(骰子涂色)(27行,比较简洁的样子)
- Climbing Stairs
- http://jingyan.baidu.com/article/7908e85c74d85faf491ad260.html
- Aizu 2456 Usoperanto 贪心 拓扑排序
- Aizu 2450 Do use segment tree 树链剖分+线段树
- http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html