usb: musb: fix Kconfig
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / connector / cn_proc.c
blob3ee1fdb31ea7daac5a123d8e72252f86ab271426
1 /*
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)
48 preempt_disable();
49 *ts = __this_cpu_inc_return(proc_event_counts) -1;
50 *cpu = smp_processor_id();
51 preempt_enable();
54 void proc_fork_connector(struct task_struct *task)
56 struct cn_msg *msg;
57 struct proc_event *ev;
58 __u8 buffer[CN_PROC_MSG_SIZE];
59 struct timespec ts;
61 if (atomic_read(&proc_event_num_listeners) < 1)
62 return;
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)
84 struct cn_msg *msg;
85 struct proc_event *ev;
86 struct timespec ts;
87 __u8 buffer[CN_PROC_MSG_SIZE];
89 if (atomic_read(&proc_event_num_listeners) < 1)
90 return;
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)
109 struct cn_msg *msg;
110 struct proc_event *ev;
111 __u8 buffer[CN_PROC_MSG_SIZE];
112 struct timespec ts;
113 const struct cred *cred;
115 if (atomic_read(&proc_event_num_listeners) < 1)
116 return;
118 msg = (struct cn_msg*)buffer;
119 ev = (struct proc_event*)msg->data;
120 ev->what = which_id;
121 ev->event_data.id.process_pid = task->pid;
122 ev->event_data.id.process_tgid = task->tgid;
123 rcu_read_lock();
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;
131 } else {
132 rcu_read_unlock();
133 return;
135 rcu_read_unlock();
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)
148 struct cn_msg *msg;
149 struct proc_event *ev;
150 struct timespec ts;
151 __u8 buffer[CN_PROC_MSG_SIZE];
153 if (atomic_read(&proc_event_num_listeners) < 1)
154 return;
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)
173 struct cn_msg *msg;
174 struct proc_event *ev;
175 struct timespec ts;
176 __u8 buffer[CN_PROC_MSG_SIZE];
178 if (atomic_read(&proc_event_num_listeners) < 1)
179 return;
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;
195 } else
196 return;
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)
206 struct cn_msg *msg;
207 struct proc_event *ev;
208 __u8 buffer[CN_PROC_MSG_SIZE];
209 struct timespec ts;
211 if (atomic_read(&proc_event_num_listeners) < 1)
212 return;
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
237 * mechanisms.
239 static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
241 struct cn_msg *msg;
242 struct proc_event *ev;
243 __u8 buffer[CN_PROC_MSG_SIZE];
244 struct timespec ts;
246 if (atomic_read(&proc_event_num_listeners) < 1)
247 return;
249 msg = (struct cn_msg*)buffer;
250 ev = (struct proc_event*)msg->data;
251 msg->seq = rcvd_seq;
252 ktime_get_ts(&ts); /* get high res monotonic timestamp */
253 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
254 ev->cpu = -1;
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);
264 * cn_proc_mcast_ctl
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;
271 int err = 0;
273 if (msg->len != sizeof(*mc_op))
274 return;
276 mc_op = (enum proc_cn_mcast_op*)msg->data;
277 switch (*mc_op) {
278 case PROC_CN_MCAST_LISTEN:
279 atomic_inc(&proc_event_num_listeners);
280 break;
281 case PROC_CN_MCAST_IGNORE:
282 atomic_dec(&proc_event_num_listeners);
283 break;
284 default:
285 err = EINVAL;
286 break;
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)
298 int err;
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");
303 return err;
305 return 0;
308 module_init(cn_proc_init);