Initial commit
[wrt350n-kernel.git] / drivers / connector / cn_proc.c
blob5c9f67f98d10b43e58b2736d07ca76b8f498faac
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 <asm/atomic.h>
31 #include <asm/unaligned.h>
33 #include <linux/cn_proc.h>
35 #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
37 static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
38 static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
40 /* proc_event_counts is used as the sequence number of the netlink message */
41 static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
43 static inline void get_seq(__u32 *ts, int *cpu)
45 *ts = get_cpu_var(proc_event_counts)++;
46 *cpu = smp_processor_id();
47 put_cpu_var(proc_event_counts);
50 void proc_fork_connector(struct task_struct *task)
52 struct cn_msg *msg;
53 struct proc_event *ev;
54 __u8 buffer[CN_PROC_MSG_SIZE];
55 struct timespec ts;
57 if (atomic_read(&proc_event_num_listeners) < 1)
58 return;
60 msg = (struct cn_msg*)buffer;
61 ev = (struct proc_event*)msg->data;
62 get_seq(&msg->seq, &ev->cpu);
63 ktime_get_ts(&ts); /* get high res monotonic timestamp */
64 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
65 ev->what = PROC_EVENT_FORK;
66 ev->event_data.fork.parent_pid = task->real_parent->pid;
67 ev->event_data.fork.parent_tgid = task->real_parent->tgid;
68 ev->event_data.fork.child_pid = task->pid;
69 ev->event_data.fork.child_tgid = task->tgid;
71 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
72 msg->ack = 0; /* not used */
73 msg->len = sizeof(*ev);
74 /* If cn_netlink_send() failed, the data is not sent */
75 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
78 void proc_exec_connector(struct task_struct *task)
80 struct cn_msg *msg;
81 struct proc_event *ev;
82 struct timespec ts;
83 __u8 buffer[CN_PROC_MSG_SIZE];
85 if (atomic_read(&proc_event_num_listeners) < 1)
86 return;
88 msg = (struct cn_msg*)buffer;
89 ev = (struct proc_event*)msg->data;
90 get_seq(&msg->seq, &ev->cpu);
91 ktime_get_ts(&ts); /* get high res monotonic timestamp */
92 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
93 ev->what = PROC_EVENT_EXEC;
94 ev->event_data.exec.process_pid = task->pid;
95 ev->event_data.exec.process_tgid = task->tgid;
97 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
98 msg->ack = 0; /* not used */
99 msg->len = sizeof(*ev);
100 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
103 void proc_id_connector(struct task_struct *task, int which_id)
105 struct cn_msg *msg;
106 struct proc_event *ev;
107 __u8 buffer[CN_PROC_MSG_SIZE];
108 struct timespec ts;
110 if (atomic_read(&proc_event_num_listeners) < 1)
111 return;
113 msg = (struct cn_msg*)buffer;
114 ev = (struct proc_event*)msg->data;
115 ev->what = which_id;
116 ev->event_data.id.process_pid = task->pid;
117 ev->event_data.id.process_tgid = task->tgid;
118 if (which_id == PROC_EVENT_UID) {
119 ev->event_data.id.r.ruid = task->uid;
120 ev->event_data.id.e.euid = task->euid;
121 } else if (which_id == PROC_EVENT_GID) {
122 ev->event_data.id.r.rgid = task->gid;
123 ev->event_data.id.e.egid = task->egid;
124 } else
125 return;
126 get_seq(&msg->seq, &ev->cpu);
127 ktime_get_ts(&ts); /* get high res monotonic timestamp */
128 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
130 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
131 msg->ack = 0; /* not used */
132 msg->len = sizeof(*ev);
133 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
136 void proc_exit_connector(struct task_struct *task)
138 struct cn_msg *msg;
139 struct proc_event *ev;
140 __u8 buffer[CN_PROC_MSG_SIZE];
141 struct timespec ts;
143 if (atomic_read(&proc_event_num_listeners) < 1)
144 return;
146 msg = (struct cn_msg*)buffer;
147 ev = (struct proc_event*)msg->data;
148 get_seq(&msg->seq, &ev->cpu);
149 ktime_get_ts(&ts); /* get high res monotonic timestamp */
150 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
151 ev->what = PROC_EVENT_EXIT;
152 ev->event_data.exit.process_pid = task->pid;
153 ev->event_data.exit.process_tgid = task->tgid;
154 ev->event_data.exit.exit_code = task->exit_code;
155 ev->event_data.exit.exit_signal = task->exit_signal;
157 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
158 msg->ack = 0; /* not used */
159 msg->len = sizeof(*ev);
160 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
164 * Send an acknowledgement message to userspace
166 * Use 0 for success, EFOO otherwise.
167 * Note: this is the negative of conventional kernel error
168 * values because it's not being returned via syscall return
169 * mechanisms.
171 static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
173 struct cn_msg *msg;
174 struct proc_event *ev;
175 __u8 buffer[CN_PROC_MSG_SIZE];
176 struct timespec ts;
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 msg->seq = rcvd_seq;
184 ktime_get_ts(&ts); /* get high res monotonic timestamp */
185 put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
186 ev->cpu = -1;
187 ev->what = PROC_EVENT_NONE;
188 ev->event_data.ack.err = err;
189 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
190 msg->ack = rcvd_ack + 1;
191 msg->len = sizeof(*ev);
192 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
196 * cn_proc_mcast_ctl
197 * @data: message sent from userspace via the connector
199 static void cn_proc_mcast_ctl(void *data)
201 struct cn_msg *msg = data;
202 enum proc_cn_mcast_op *mc_op = NULL;
203 int err = 0;
205 if (msg->len != sizeof(*mc_op))
206 return;
208 mc_op = (enum proc_cn_mcast_op*)msg->data;
209 switch (*mc_op) {
210 case PROC_CN_MCAST_LISTEN:
211 atomic_inc(&proc_event_num_listeners);
212 break;
213 case PROC_CN_MCAST_IGNORE:
214 atomic_dec(&proc_event_num_listeners);
215 break;
216 default:
217 err = EINVAL;
218 break;
220 cn_proc_ack(err, msg->seq, msg->ack);
224 * cn_proc_init - initialization entry point
226 * Adds the connector callback to the connector driver.
228 static int __init cn_proc_init(void)
230 int err;
232 if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc",
233 &cn_proc_mcast_ctl))) {
234 printk(KERN_WARNING "cn_proc failed to register\n");
235 return err;
237 return 0;
240 module_init(cn_proc_init);