Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

connector: add comm change event report to proc connector

Add an event to monitor comm value changes of tasks. Such an event
becomes vital, if someone desires to control threads of a process in
different manner.

A natural characteristic of threads is its comm value, and helpfully
application developers have an opportunity to change it in runtime.
Reporting about such events via proc connector allows to fine-grain
monitoring and control potentials, for instance a process control daemon
listening to proc connector and following comm value policies can place
specific threads to assigned cgroup partitions.

It might be possible to achieve a pale partial one-shot likeness without
this update, if an application changes comm value of a thread generator
task beforehand, then a new thread is cloned, and after that proc
connector listener gets the fork event and reads new thread's comm value
from procfs stat file, but this change visibly simplifies and extends the
matter.

Signed-off-by: Vladimir Zapolskiy <vzapolskiy@gmail.com>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
Cc: David Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Vladimir Zapolskiy and committed by
David S. Miller
f786ecba 5dd17e08

+38
+26
drivers/connector/cn_proc.c
··· 205 205 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); 206 206 } 207 207 208 + void proc_comm_connector(struct task_struct *task) 209 + { 210 + struct cn_msg *msg; 211 + struct proc_event *ev; 212 + struct timespec ts; 213 + __u8 buffer[CN_PROC_MSG_SIZE]; 214 + 215 + if (atomic_read(&proc_event_num_listeners) < 1) 216 + return; 217 + 218 + msg = (struct cn_msg *)buffer; 219 + ev = (struct proc_event *)msg->data; 220 + get_seq(&msg->seq, &ev->cpu); 221 + ktime_get_ts(&ts); /* get high res monotonic timestamp */ 222 + put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); 223 + ev->what = PROC_EVENT_COMM; 224 + ev->event_data.comm.process_pid = task->pid; 225 + ev->event_data.comm.process_tgid = task->tgid; 226 + get_task_comm(ev->event_data.comm.comm, task); 227 + 228 + memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); 229 + msg->ack = 0; /* not used */ 230 + msg->len = sizeof(*ev); 231 + cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); 232 + } 233 + 208 234 void proc_exit_connector(struct task_struct *task) 209 235 { 210 236 struct cn_msg *msg;
+11
include/linux/cn_proc.h
··· 54 54 PROC_EVENT_GID = 0x00000040, 55 55 PROC_EVENT_SID = 0x00000080, 56 56 PROC_EVENT_PTRACE = 0x00000100, 57 + PROC_EVENT_COMM = 0x00000200, 57 58 /* "next" should be 0x00000400 */ 58 59 /* "last" is the last process event: exit */ 59 60 PROC_EVENT_EXIT = 0x80000000 ··· 104 103 __kernel_pid_t tracer_tgid; 105 104 } ptrace; 106 105 106 + struct comm_proc_event { 107 + __kernel_pid_t process_pid; 108 + __kernel_pid_t process_tgid; 109 + char comm[16]; 110 + } comm; 111 + 107 112 struct exit_proc_event { 108 113 __kernel_pid_t process_pid; 109 114 __kernel_pid_t process_tgid; ··· 125 118 void proc_id_connector(struct task_struct *task, int which_id); 126 119 void proc_sid_connector(struct task_struct *task); 127 120 void proc_ptrace_connector(struct task_struct *task, int which_id); 121 + void proc_comm_connector(struct task_struct *task); 128 122 void proc_exit_connector(struct task_struct *task); 129 123 #else 130 124 static inline void proc_fork_connector(struct task_struct *task) ··· 139 131 {} 140 132 141 133 static inline void proc_sid_connector(struct task_struct *task) 134 + {} 135 + 136 + static inline void proc_comm_connector(struct task_struct *task) 142 137 {} 143 138 144 139 static inline void proc_ptrace_connector(struct task_struct *task,
+1
kernel/sys.c
··· 1759 1759 sizeof(me->comm) - 1) < 0) 1760 1760 return -EFAULT; 1761 1761 set_task_comm(me, comm); 1762 + proc_comm_connector(me); 1762 1763 return 0; 1763 1764 case PR_GET_NAME: 1764 1765 get_task_comm(comm, me);