blob: 2b46a7efa0ac78c5c4213609d7846422842ef17f [file] [log] [blame]
Matt Helsley9f460802005-11-07 00:59:16 -08001/*
2 * cn_proc.c - process events connector
3 *
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.
8 *
9 *
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.
14 *
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.
19 *
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
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
Matt Helsleycaf3c9d2006-01-09 20:52:40 -080027#include <linux/ktime.h>
Matt Helsley9f460802005-11-07 00:59:16 -080028#include <linux/init.h>
Matt Helsley1d31a4e2006-06-23 02:05:42 -070029#include <linux/connector.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Matt Helsley9f460802005-11-07 00:59:16 -080031#include <asm/atomic.h>
Erik Jacobsonaf3e0952007-01-05 16:37:05 -080032#include <asm/unaligned.h>
Matt Helsley9f460802005-11-07 00:59:16 -080033
34#include <linux/cn_proc.h>
35
36#define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
37
38static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
39static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
40
David S. Millercc398c22006-01-08 01:03:34 -080041/* proc_event_counts is used as the sequence number of the netlink message */
Matt Helsley9f460802005-11-07 00:59:16 -080042static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
43
44static inline void get_seq(__u32 *ts, int *cpu)
45{
Christoph Lameter3ea9f682010-12-08 17:42:23 +010046 preempt_disable();
47 *ts = __this_cpu_inc_return(proc_event_counts) -1;
Matt Helsley9f460802005-11-07 00:59:16 -080048 *cpu = smp_processor_id();
Christoph Lameter3ea9f682010-12-08 17:42:23 +010049 preempt_enable();
Matt Helsley9f460802005-11-07 00:59:16 -080050}
51
52void proc_fork_connector(struct task_struct *task)
53{
54 struct cn_msg *msg;
55 struct proc_event *ev;
56 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070057 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -080058
59 if (atomic_read(&proc_event_num_listeners) < 1)
60 return;
61
62 msg = (struct cn_msg*)buffer;
63 ev = (struct proc_event*)msg->data;
64 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070065 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -080066 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -080067 ev->what = PROC_EVENT_FORK;
68 ev->event_data.fork.parent_pid = task->real_parent->pid;
69 ev->event_data.fork.parent_tgid = task->real_parent->tgid;
70 ev->event_data.fork.child_pid = task->pid;
71 ev->event_data.fork.child_tgid = task->tgid;
72
73 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
74 msg->ack = 0; /* not used */
75 msg->len = sizeof(*ev);
76 /* If cn_netlink_send() failed, the data is not sent */
77 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
78}
79
80void proc_exec_connector(struct task_struct *task)
81{
82 struct cn_msg *msg;
83 struct proc_event *ev;
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070084 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -080085 __u8 buffer[CN_PROC_MSG_SIZE];
86
87 if (atomic_read(&proc_event_num_listeners) < 1)
88 return;
89
90 msg = (struct cn_msg*)buffer;
91 ev = (struct proc_event*)msg->data;
92 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070093 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -080094 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -080095 ev->what = PROC_EVENT_EXEC;
96 ev->event_data.exec.process_pid = task->pid;
97 ev->event_data.exec.process_tgid = task->tgid;
98
99 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
100 msg->ack = 0; /* not used */
101 msg->len = sizeof(*ev);
102 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
103}
104
105void proc_id_connector(struct task_struct *task, int which_id)
106{
107 struct cn_msg *msg;
108 struct proc_event *ev;
109 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700110 struct timespec ts;
David Howellsc69e8d92008-11-14 10:39:19 +1100111 const struct cred *cred;
Matt Helsley9f460802005-11-07 00:59:16 -0800112
113 if (atomic_read(&proc_event_num_listeners) < 1)
114 return;
115
116 msg = (struct cn_msg*)buffer;
117 ev = (struct proc_event*)msg->data;
118 ev->what = which_id;
119 ev->event_data.id.process_pid = task->pid;
120 ev->event_data.id.process_tgid = task->tgid;
David Howellsc69e8d92008-11-14 10:39:19 +1100121 rcu_read_lock();
122 cred = __task_cred(task);
Matt Helsley9f460802005-11-07 00:59:16 -0800123 if (which_id == PROC_EVENT_UID) {
David Howellsc69e8d92008-11-14 10:39:19 +1100124 ev->event_data.id.r.ruid = cred->uid;
125 ev->event_data.id.e.euid = cred->euid;
Matt Helsley9f460802005-11-07 00:59:16 -0800126 } else if (which_id == PROC_EVENT_GID) {
David Howellsc69e8d92008-11-14 10:39:19 +1100127 ev->event_data.id.r.rgid = cred->gid;
128 ev->event_data.id.e.egid = cred->egid;
129 } else {
130 rcu_read_unlock();
Matt Helsley9f460802005-11-07 00:59:16 -0800131 return;
David Howellsc69e8d92008-11-14 10:39:19 +1100132 }
133 rcu_read_unlock();
Matt Helsley9f460802005-11-07 00:59:16 -0800134 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700135 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800136 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800137
138 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
139 msg->ack = 0; /* not used */
140 msg->len = sizeof(*ev);
141 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
142}
143
Scott James Remnant02b51df2009-09-22 16:43:44 -0700144void proc_sid_connector(struct task_struct *task)
145{
146 struct cn_msg *msg;
147 struct proc_event *ev;
148 struct timespec ts;
149 __u8 buffer[CN_PROC_MSG_SIZE];
150
151 if (atomic_read(&proc_event_num_listeners) < 1)
152 return;
153
154 msg = (struct cn_msg *)buffer;
155 ev = (struct proc_event *)msg->data;
156 get_seq(&msg->seq, &ev->cpu);
157 ktime_get_ts(&ts); /* get high res monotonic timestamp */
158 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
159 ev->what = PROC_EVENT_SID;
160 ev->event_data.sid.process_pid = task->pid;
161 ev->event_data.sid.process_tgid = task->tgid;
162
163 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
164 msg->ack = 0; /* not used */
165 msg->len = sizeof(*ev);
166 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
167}
168
Matt Helsley9f460802005-11-07 00:59:16 -0800169void proc_exit_connector(struct task_struct *task)
170{
171 struct cn_msg *msg;
172 struct proc_event *ev;
173 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700174 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800175
176 if (atomic_read(&proc_event_num_listeners) < 1)
177 return;
178
179 msg = (struct cn_msg*)buffer;
180 ev = (struct proc_event*)msg->data;
181 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700182 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800183 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800184 ev->what = PROC_EVENT_EXIT;
185 ev->event_data.exit.process_pid = task->pid;
186 ev->event_data.exit.process_tgid = task->tgid;
187 ev->event_data.exit.exit_code = task->exit_code;
188 ev->event_data.exit.exit_signal = task->exit_signal;
189
190 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
191 msg->ack = 0; /* not used */
192 msg->len = sizeof(*ev);
193 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
194}
195
196/*
197 * Send an acknowledgement message to userspace
198 *
199 * Use 0 for success, EFOO otherwise.
200 * Note: this is the negative of conventional kernel error
201 * values because it's not being returned via syscall return
202 * mechanisms.
203 */
204static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
205{
206 struct cn_msg *msg;
207 struct proc_event *ev;
208 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700209 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800210
211 if (atomic_read(&proc_event_num_listeners) < 1)
212 return;
213
214 msg = (struct cn_msg*)buffer;
215 ev = (struct proc_event*)msg->data;
216 msg->seq = rcvd_seq;
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700217 ktime_get_ts(&ts); /* get high res monotonic timestamp */
Erik Jacobsonaf3e0952007-01-05 16:37:05 -0800218 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
Matt Helsley9f460802005-11-07 00:59:16 -0800219 ev->cpu = -1;
220 ev->what = PROC_EVENT_NONE;
221 ev->event_data.ack.err = err;
222 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
223 msg->ack = rcvd_ack + 1;
224 msg->len = sizeof(*ev);
225 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
226}
227
228/**
229 * cn_proc_mcast_ctl
230 * @data: message sent from userspace via the connector
231 */
Stephen Boydf0b25932009-10-06 01:39:51 -0700232static void cn_proc_mcast_ctl(struct cn_msg *msg,
233 struct netlink_skb_parms *nsp)
Matt Helsley9f460802005-11-07 00:59:16 -0800234{
Matt Helsley9f460802005-11-07 00:59:16 -0800235 enum proc_cn_mcast_op *mc_op = NULL;
236 int err = 0;
237
238 if (msg->len != sizeof(*mc_op))
239 return;
240
241 mc_op = (enum proc_cn_mcast_op*)msg->data;
242 switch (*mc_op) {
243 case PROC_CN_MCAST_LISTEN:
244 atomic_inc(&proc_event_num_listeners);
245 break;
246 case PROC_CN_MCAST_IGNORE:
247 atomic_dec(&proc_event_num_listeners);
248 break;
249 default:
250 err = EINVAL;
251 break;
252 }
253 cn_proc_ack(err, msg->seq, msg->ack);
254}
255
256/*
257 * cn_proc_init - initialization entry point
258 *
259 * Adds the connector callback to the connector driver.
260 */
261static int __init cn_proc_init(void)
262{
263 int err;
264
265 if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc",
266 &cn_proc_mcast_ctl))) {
267 printk(KERN_WARNING "cn_proc failed to register\n");
268 return err;
269 }
270 return 0;
271}
272
273module_init(cn_proc_init);