Commit 02b51df1b07b4e9ca823c89284e704cadb323cd1
Committed by
Linus Torvalds
1 parent
88e9d34c72
Exists in
master
and in
39 other branches
proc connector: add event for process becoming session leader
The act of a process becoming a session leader is a useful signal to a supervising init daemon such as Upstart. While a daemon will normally do this as part of the process of becoming a daemon, it is rare for its children to do so. When the children do, it is nearly always a sign that the child should be considered detached from the parent and not supervised along with it. The poster-child example is OpenSSH; the per-login children call setsid() so that they may control the pty connected to them. If the primary daemon dies or is restarted, we do not want to consider the per-login children and want to respawn the primary daemon without killing the children. This patch adds a new PROC_SID_EVENT and associated structure to the proc_event event_data union, it arranges for this to be emitted when the special PIDTYPE_SID pid is set. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Scott James Remnant <scott@ubuntu.com> Acked-by: Matt Helsley <matthltc@us.ibm.com> Cc: Oleg Nesterov <oleg@tv-sign.ru> Cc: Evgeniy Polyakov <johnpol@2ka.mipt.ru> Acked-by: "David S. Miller" <davem@davemloft.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 3 changed files with 38 additions and 1 deletions Side-by-side Diff
drivers/connector/cn_proc.c
... | ... | @@ -139,6 +139,31 @@ |
139 | 139 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
140 | 140 | } |
141 | 141 | |
142 | +void proc_sid_connector(struct task_struct *task) | |
143 | +{ | |
144 | + struct cn_msg *msg; | |
145 | + struct proc_event *ev; | |
146 | + struct timespec ts; | |
147 | + __u8 buffer[CN_PROC_MSG_SIZE]; | |
148 | + | |
149 | + if (atomic_read(&proc_event_num_listeners) < 1) | |
150 | + return; | |
151 | + | |
152 | + msg = (struct cn_msg *)buffer; | |
153 | + ev = (struct proc_event *)msg->data; | |
154 | + get_seq(&msg->seq, &ev->cpu); | |
155 | + ktime_get_ts(&ts); /* get high res monotonic timestamp */ | |
156 | + put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); | |
157 | + ev->what = PROC_EVENT_SID; | |
158 | + ev->event_data.sid.process_pid = task->pid; | |
159 | + ev->event_data.sid.process_tgid = task->tgid; | |
160 | + | |
161 | + memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); | |
162 | + msg->ack = 0; /* not used */ | |
163 | + msg->len = sizeof(*ev); | |
164 | + cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); | |
165 | +} | |
166 | + | |
142 | 167 | void proc_exit_connector(struct task_struct *task) |
143 | 168 | { |
144 | 169 | struct cn_msg *msg; |
include/linux/cn_proc.h
... | ... | @@ -52,6 +52,7 @@ |
52 | 52 | PROC_EVENT_EXEC = 0x00000002, |
53 | 53 | PROC_EVENT_UID = 0x00000004, |
54 | 54 | PROC_EVENT_GID = 0x00000040, |
55 | + PROC_EVENT_SID = 0x00000080, | |
55 | 56 | /* "next" should be 0x00000400 */ |
56 | 57 | /* "last" is the last process event: exit */ |
57 | 58 | PROC_EVENT_EXIT = 0x80000000 |
... | ... | @@ -89,6 +90,11 @@ |
89 | 90 | } e; |
90 | 91 | } id; |
91 | 92 | |
93 | + struct sid_proc_event { | |
94 | + __kernel_pid_t process_pid; | |
95 | + __kernel_pid_t process_tgid; | |
96 | + } sid; | |
97 | + | |
92 | 98 | struct exit_proc_event { |
93 | 99 | __kernel_pid_t process_pid; |
94 | 100 | __kernel_pid_t process_tgid; |
... | ... | @@ -102,6 +108,7 @@ |
102 | 108 | void proc_fork_connector(struct task_struct *task); |
103 | 109 | void proc_exec_connector(struct task_struct *task); |
104 | 110 | void proc_id_connector(struct task_struct *task, int which_id); |
111 | +void proc_sid_connector(struct task_struct *task); | |
105 | 112 | void proc_exit_connector(struct task_struct *task); |
106 | 113 | #else |
107 | 114 | static inline void proc_fork_connector(struct task_struct *task) |
... | ... | @@ -112,6 +119,9 @@ |
112 | 119 | |
113 | 120 | static inline void proc_id_connector(struct task_struct *task, |
114 | 121 | int which_id) |
122 | +{} | |
123 | + | |
124 | +static inline void proc_sid_connector(struct task_struct *task) | |
115 | 125 | {} |
116 | 126 | |
117 | 127 | static inline void proc_exit_connector(struct task_struct *task) |
kernel/exit.c
... | ... | @@ -359,8 +359,10 @@ |
359 | 359 | { |
360 | 360 | struct task_struct *curr = current->group_leader; |
361 | 361 | |
362 | - if (task_session(curr) != pid) | |
362 | + if (task_session(curr) != pid) { | |
363 | 363 | change_pid(curr, PIDTYPE_SID, pid); |
364 | + proc_sid_connector(curr); | |
365 | + } | |
364 | 366 | |
365 | 367 | if (task_pgrp(curr) != pid) |
366 | 368 | change_pid(curr, PIDTYPE_PGID, pid); |