[NET]: Move netlink interface bits to linux/if.h
[hh.org.git] / drivers / connector / cn_proc.c
blob3ece6923134359a9e70c841e8e592b589ea864ab
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>
32 #include <linux/cn_proc.h>
34 #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
36 static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
37 static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
39 /* proc_event_counts is used as the sequence number of the netlink message */
40 static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
42 static inline void get_seq(__u32 *ts, int *cpu)
44 *ts = get_cpu_var(proc_event_counts)++;
45 *cpu = smp_processor_id();
46 put_cpu_var(proc_event_counts);
49 void proc_fork_connector(struct task_struct *task)
51 struct cn_msg *msg;
52 struct proc_event *ev;
53 __u8 buffer[CN_PROC_MSG_SIZE];
54 struct timespec ts;
56 if (atomic_read(&proc_event_num_listeners) < 1)
57 return;
59 msg = (struct cn_msg*)buffer;
60 ev = (struct proc_event*)msg->data;
61 get_seq(&msg->seq, &ev->cpu);
62 ktime_get_ts(&ts); /* get high res monotonic timestamp */
63 ev->timestamp_ns = timespec_to_ns(&ts);
64 ev->what = PROC_EVENT_FORK;
65 ev->event_data.fork.parent_pid = task->real_parent->pid;
66 ev->event_data.fork.parent_tgid = task->real_parent->tgid;
67 ev->event_data.fork.child_pid = task->pid;
68 ev->event_data.fork.child_tgid = task->tgid;
70 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
71 msg->ack = 0; /* not used */
72 msg->len = sizeof(*ev);
73 /* If cn_netlink_send() failed, the data is not sent */
74 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
77 void proc_exec_connector(struct task_struct *task)
79 struct cn_msg *msg;
80 struct proc_event *ev;
81 struct timespec ts;
82 __u8 buffer[CN_PROC_MSG_SIZE];
84 if (atomic_read(&proc_event_num_listeners) < 1)
85 return;
87 msg = (struct cn_msg*)buffer;
88 ev = (struct proc_event*)msg->data;
89 get_seq(&msg->seq, &ev->cpu);
90 ktime_get_ts(&ts); /* get high res monotonic timestamp */
91 ev->timestamp_ns = timespec_to_ns(&ts);
92 ev->what = PROC_EVENT_EXEC;
93 ev->event_data.exec.process_pid = task->pid;
94 ev->event_data.exec.process_tgid = task->tgid;
96 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
97 msg->ack = 0; /* not used */
98 msg->len = sizeof(*ev);
99 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
102 void proc_id_connector(struct task_struct *task, int which_id)
104 struct cn_msg *msg;
105 struct proc_event *ev;
106 __u8 buffer[CN_PROC_MSG_SIZE];
107 struct timespec ts;
109 if (atomic_read(&proc_event_num_listeners) < 1)
110 return;
112 msg = (struct cn_msg*)buffer;
113 ev = (struct proc_event*)msg->data;
114 ev->what = which_id;
115 ev->event_data.id.process_pid = task->pid;
116 ev->event_data.id.process_tgid = task->tgid;
117 if (which_id == PROC_EVENT_UID) {
118 ev->event_data.id.r.ruid = task->uid;
119 ev->event_data.id.e.euid = task->euid;
120 } else if (which_id == PROC_EVENT_GID) {
121 ev->event_data.id.r.rgid = task->gid;
122 ev->event_data.id.e.egid = task->egid;
123 } else
124 return;
125 get_seq(&msg->seq, &ev->cpu);
126 ktime_get_ts(&ts); /* get high res monotonic timestamp */
127 ev->timestamp_ns = timespec_to_ns(&ts);
129 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
130 msg->ack = 0; /* not used */
131 msg->len = sizeof(*ev);
132 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
135 void proc_exit_connector(struct task_struct *task)
137 struct cn_msg *msg;
138 struct proc_event *ev;
139 __u8 buffer[CN_PROC_MSG_SIZE];
140 struct timespec ts;
142 if (atomic_read(&proc_event_num_listeners) < 1)
143 return;
145 msg = (struct cn_msg*)buffer;
146 ev = (struct proc_event*)msg->data;
147 get_seq(&msg->seq, &ev->cpu);
148 ktime_get_ts(&ts); /* get high res monotonic timestamp */
149 ev->timestamp_ns = timespec_to_ns(&ts);
150 ev->what = PROC_EVENT_EXIT;
151 ev->event_data.exit.process_pid = task->pid;
152 ev->event_data.exit.process_tgid = task->tgid;
153 ev->event_data.exit.exit_code = task->exit_code;
154 ev->event_data.exit.exit_signal = task->exit_signal;
156 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
157 msg->ack = 0; /* not used */
158 msg->len = sizeof(*ev);
159 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
163 * Send an acknowledgement message to userspace
165 * Use 0 for success, EFOO otherwise.
166 * Note: this is the negative of conventional kernel error
167 * values because it's not being returned via syscall return
168 * mechanisms.
170 static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
172 struct cn_msg *msg;
173 struct proc_event *ev;
174 __u8 buffer[CN_PROC_MSG_SIZE];
175 struct timespec ts;
177 if (atomic_read(&proc_event_num_listeners) < 1)
178 return;
180 msg = (struct cn_msg*)buffer;
181 ev = (struct proc_event*)msg->data;
182 msg->seq = rcvd_seq;
183 ktime_get_ts(&ts); /* get high res monotonic timestamp */
184 ev->timestamp_ns = timespec_to_ns(&ts);
185 ev->cpu = -1;
186 ev->what = PROC_EVENT_NONE;
187 ev->event_data.ack.err = err;
188 memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
189 msg->ack = rcvd_ack + 1;
190 msg->len = sizeof(*ev);
191 cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
195 * cn_proc_mcast_ctl
196 * @data: message sent from userspace via the connector
198 static void cn_proc_mcast_ctl(void *data)
200 struct cn_msg *msg = data;
201 enum proc_cn_mcast_op *mc_op = NULL;
202 int err = 0;
204 if (msg->len != sizeof(*mc_op))
205 return;
207 mc_op = (enum proc_cn_mcast_op*)msg->data;
208 switch (*mc_op) {
209 case PROC_CN_MCAST_LISTEN:
210 atomic_inc(&proc_event_num_listeners);
211 break;
212 case PROC_CN_MCAST_IGNORE:
213 atomic_dec(&proc_event_num_listeners);
214 break;
215 default:
216 err = EINVAL;
217 break;
219 cn_proc_ack(err, msg->seq, msg->ack);
223 * cn_proc_init - initialization entry point
225 * Adds the connector callback to the connector driver.
227 static int __init cn_proc_init(void)
229 int err;
231 if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc",
232 &cn_proc_mcast_ctl))) {
233 printk(KERN_WARNING "cn_proc failed to register\n");
234 return err;
236 return 0;
239 module_init(cn_proc_init);