2 * cn_proc.c - process events connector
4 * Copyright (C) Matt Helsley, IBM Corp. 2005
5 * Based on cn_fork.c by Guillaume Thouvenin <guillaume.thouvenin@bull.net>
6 * Original copyright notice follows:
7 * Copyright (C) 2005 BULL SA.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/ktime.h>
28 #include <linux/init.h>
29 #include <linux/connector.h>
30 #include <linux/gfp.h>
31 #include <linux/ptrace.h>
32 #include <linux/atomic.h>
34 #include <asm/unaligned.h>
36 #include <linux/cn_proc.h>
38 #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
40 static atomic_t proc_event_num_listeners
= ATOMIC_INIT(0);
41 static struct cb_id cn_proc_event_id
= { CN_IDX_PROC
, CN_VAL_PROC
};
43 /* proc_event_counts is used as the sequence number of the netlink message */
44 static DEFINE_PER_CPU(__u32
, proc_event_counts
) = { 0 };
46 static inline void get_seq(__u32
*ts
, int *cpu
)
49 *ts
= __this_cpu_inc_return(proc_event_counts
) -1;
50 *cpu
= smp_processor_id();
54 void proc_fork_connector(struct task_struct
*task
)
57 struct proc_event
*ev
;
58 __u8 buffer
[CN_PROC_MSG_SIZE
];
61 if (atomic_read(&proc_event_num_listeners
) < 1)
64 msg
= (struct cn_msg
*)buffer
;
65 ev
= (struct proc_event
*)msg
->data
;
66 get_seq(&msg
->seq
, &ev
->cpu
);
67 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
68 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
69 ev
->what
= PROC_EVENT_FORK
;
70 ev
->event_data
.fork
.parent_pid
= task
->real_parent
->pid
;
71 ev
->event_data
.fork
.parent_tgid
= task
->real_parent
->tgid
;
72 ev
->event_data
.fork
.child_pid
= task
->pid
;
73 ev
->event_data
.fork
.child_tgid
= task
->tgid
;
75 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
76 msg
->ack
= 0; /* not used */
77 msg
->len
= sizeof(*ev
);
78 /* If cn_netlink_send() failed, the data is not sent */
79 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
82 void proc_exec_connector(struct task_struct
*task
)
85 struct proc_event
*ev
;
87 __u8 buffer
[CN_PROC_MSG_SIZE
];
89 if (atomic_read(&proc_event_num_listeners
) < 1)
92 msg
= (struct cn_msg
*)buffer
;
93 ev
= (struct proc_event
*)msg
->data
;
94 get_seq(&msg
->seq
, &ev
->cpu
);
95 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
96 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
97 ev
->what
= PROC_EVENT_EXEC
;
98 ev
->event_data
.exec
.process_pid
= task
->pid
;
99 ev
->event_data
.exec
.process_tgid
= task
->tgid
;
101 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
102 msg
->ack
= 0; /* not used */
103 msg
->len
= sizeof(*ev
);
104 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
107 void proc_id_connector(struct task_struct
*task
, int which_id
)
110 struct proc_event
*ev
;
111 __u8 buffer
[CN_PROC_MSG_SIZE
];
113 const struct cred
*cred
;
115 if (atomic_read(&proc_event_num_listeners
) < 1)
118 msg
= (struct cn_msg
*)buffer
;
119 ev
= (struct proc_event
*)msg
->data
;
121 ev
->event_data
.id
.process_pid
= task
->pid
;
122 ev
->event_data
.id
.process_tgid
= task
->tgid
;
124 cred
= __task_cred(task
);
125 if (which_id
== PROC_EVENT_UID
) {
126 ev
->event_data
.id
.r
.ruid
= cred
->uid
;
127 ev
->event_data
.id
.e
.euid
= cred
->euid
;
128 } else if (which_id
== PROC_EVENT_GID
) {
129 ev
->event_data
.id
.r
.rgid
= cred
->gid
;
130 ev
->event_data
.id
.e
.egid
= cred
->egid
;
136 get_seq(&msg
->seq
, &ev
->cpu
);
137 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
138 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
140 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
141 msg
->ack
= 0; /* not used */
142 msg
->len
= sizeof(*ev
);
143 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
146 void proc_sid_connector(struct task_struct
*task
)
149 struct proc_event
*ev
;
151 __u8 buffer
[CN_PROC_MSG_SIZE
];
153 if (atomic_read(&proc_event_num_listeners
) < 1)
156 msg
= (struct cn_msg
*)buffer
;
157 ev
= (struct proc_event
*)msg
->data
;
158 get_seq(&msg
->seq
, &ev
->cpu
);
159 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
160 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
161 ev
->what
= PROC_EVENT_SID
;
162 ev
->event_data
.sid
.process_pid
= task
->pid
;
163 ev
->event_data
.sid
.process_tgid
= task
->tgid
;
165 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
166 msg
->ack
= 0; /* not used */
167 msg
->len
= sizeof(*ev
);
168 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
171 void proc_ptrace_connector(struct task_struct
*task
, int ptrace_id
)
174 struct proc_event
*ev
;
176 __u8 buffer
[CN_PROC_MSG_SIZE
];
178 if (atomic_read(&proc_event_num_listeners
) < 1)
181 msg
= (struct cn_msg
*)buffer
;
182 ev
= (struct proc_event
*)msg
->data
;
183 get_seq(&msg
->seq
, &ev
->cpu
);
184 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
185 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
186 ev
->what
= PROC_EVENT_PTRACE
;
187 ev
->event_data
.ptrace
.process_pid
= task
->pid
;
188 ev
->event_data
.ptrace
.process_tgid
= task
->tgid
;
189 if (ptrace_id
== PTRACE_ATTACH
) {
190 ev
->event_data
.ptrace
.tracer_pid
= current
->pid
;
191 ev
->event_data
.ptrace
.tracer_tgid
= current
->tgid
;
192 } else if (ptrace_id
== PTRACE_DETACH
) {
193 ev
->event_data
.ptrace
.tracer_pid
= 0;
194 ev
->event_data
.ptrace
.tracer_tgid
= 0;
198 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
199 msg
->ack
= 0; /* not used */
200 msg
->len
= sizeof(*ev
);
201 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
204 void proc_exit_connector(struct task_struct
*task
)
207 struct proc_event
*ev
;
208 __u8 buffer
[CN_PROC_MSG_SIZE
];
211 if (atomic_read(&proc_event_num_listeners
) < 1)
214 msg
= (struct cn_msg
*)buffer
;
215 ev
= (struct proc_event
*)msg
->data
;
216 get_seq(&msg
->seq
, &ev
->cpu
);
217 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
218 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
219 ev
->what
= PROC_EVENT_EXIT
;
220 ev
->event_data
.exit
.process_pid
= task
->pid
;
221 ev
->event_data
.exit
.process_tgid
= task
->tgid
;
222 ev
->event_data
.exit
.exit_code
= task
->exit_code
;
223 ev
->event_data
.exit
.exit_signal
= task
->exit_signal
;
225 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
226 msg
->ack
= 0; /* not used */
227 msg
->len
= sizeof(*ev
);
228 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
232 * Send an acknowledgement message to userspace
234 * Use 0 for success, EFOO otherwise.
235 * Note: this is the negative of conventional kernel error
236 * values because it's not being returned via syscall return
239 static void cn_proc_ack(int err
, int rcvd_seq
, int rcvd_ack
)
242 struct proc_event
*ev
;
243 __u8 buffer
[CN_PROC_MSG_SIZE
];
246 if (atomic_read(&proc_event_num_listeners
) < 1)
249 msg
= (struct cn_msg
*)buffer
;
250 ev
= (struct proc_event
*)msg
->data
;
252 ktime_get_ts(&ts
); /* get high res monotonic timestamp */
253 put_unaligned(timespec_to_ns(&ts
), (__u64
*)&ev
->timestamp_ns
);
255 ev
->what
= PROC_EVENT_NONE
;
256 ev
->event_data
.ack
.err
= err
;
257 memcpy(&msg
->id
, &cn_proc_event_id
, sizeof(msg
->id
));
258 msg
->ack
= rcvd_ack
+ 1;
259 msg
->len
= sizeof(*ev
);
260 cn_netlink_send(msg
, CN_IDX_PROC
, GFP_KERNEL
);
265 * @data: message sent from userspace via the connector
267 static void cn_proc_mcast_ctl(struct cn_msg
*msg
,
268 struct netlink_skb_parms
*nsp
)
270 enum proc_cn_mcast_op
*mc_op
= NULL
;
273 if (msg
->len
!= sizeof(*mc_op
))
276 mc_op
= (enum proc_cn_mcast_op
*)msg
->data
;
278 case PROC_CN_MCAST_LISTEN
:
279 atomic_inc(&proc_event_num_listeners
);
281 case PROC_CN_MCAST_IGNORE
:
282 atomic_dec(&proc_event_num_listeners
);
288 cn_proc_ack(err
, msg
->seq
, msg
->ack
);
292 * cn_proc_init - initialization entry point
294 * Adds the connector callback to the connector driver.
296 static int __init
cn_proc_init(void)
300 if ((err
= cn_add_callback(&cn_proc_event_id
, "cn_proc",
301 &cn_proc_mcast_ctl
))) {
302 printk(KERN_WARNING
"cn_proc failed to register\n");
308 module_init(cn_proc_init
);