blob: 3ece6923134359a9e70c841e8e592b589ea864ab [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>
Matt Helsley9f460802005-11-07 00:59:16 -080030#include <asm/atomic.h>
31
32#include <linux/cn_proc.h>
33
34#define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
35
36static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
37static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
38
David S. Millercc398c22006-01-08 01:03:34 -080039/* proc_event_counts is used as the sequence number of the netlink message */
Matt Helsley9f460802005-11-07 00:59:16 -080040static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
41
42static inline void get_seq(__u32 *ts, int *cpu)
43{
44 *ts = get_cpu_var(proc_event_counts)++;
45 *cpu = smp_processor_id();
David S. Millercc398c22006-01-08 01:03:34 -080046 put_cpu_var(proc_event_counts);
Matt Helsley9f460802005-11-07 00:59:16 -080047}
48
49void proc_fork_connector(struct task_struct *task)
50{
51 struct cn_msg *msg;
52 struct proc_event *ev;
53 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070054 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -080055
56 if (atomic_read(&proc_event_num_listeners) < 1)
57 return;
58
59 msg = (struct cn_msg*)buffer;
60 ev = (struct proc_event*)msg->data;
61 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070062 ktime_get_ts(&ts); /* get high res monotonic timestamp */
63 ev->timestamp_ns = timespec_to_ns(&ts);
Matt Helsley9f460802005-11-07 00:59:16 -080064 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;
69
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);
75}
76
77void proc_exec_connector(struct task_struct *task)
78{
79 struct cn_msg *msg;
80 struct proc_event *ev;
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070081 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -080082 __u8 buffer[CN_PROC_MSG_SIZE];
83
84 if (atomic_read(&proc_event_num_listeners) < 1)
85 return;
86
87 msg = (struct cn_msg*)buffer;
88 ev = (struct proc_event*)msg->data;
89 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -070090 ktime_get_ts(&ts); /* get high res monotonic timestamp */
91 ev->timestamp_ns = timespec_to_ns(&ts);
Matt Helsley9f460802005-11-07 00:59:16 -080092 ev->what = PROC_EVENT_EXEC;
93 ev->event_data.exec.process_pid = task->pid;
94 ev->event_data.exec.process_tgid = task->tgid;
95
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);
100}
101
102void proc_id_connector(struct task_struct *task, int which_id)
103{
104 struct cn_msg *msg;
105 struct proc_event *ev;
106 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700107 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800108
109 if (atomic_read(&proc_event_num_listeners) < 1)
110 return;
111
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);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700126 ktime_get_ts(&ts); /* get high res monotonic timestamp */
127 ev->timestamp_ns = timespec_to_ns(&ts);
Matt Helsley9f460802005-11-07 00:59:16 -0800128
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);
133}
134
135void proc_exit_connector(struct task_struct *task)
136{
137 struct cn_msg *msg;
138 struct proc_event *ev;
139 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700140 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800141
142 if (atomic_read(&proc_event_num_listeners) < 1)
143 return;
144
145 msg = (struct cn_msg*)buffer;
146 ev = (struct proc_event*)msg->data;
147 get_seq(&msg->seq, &ev->cpu);
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700148 ktime_get_ts(&ts); /* get high res monotonic timestamp */
149 ev->timestamp_ns = timespec_to_ns(&ts);
Matt Helsley9f460802005-11-07 00:59:16 -0800150 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;
155
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);
160}
161
162/*
163 * Send an acknowledgement message to userspace
164 *
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.
169 */
170static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
171{
172 struct cn_msg *msg;
173 struct proc_event *ev;
174 __u8 buffer[CN_PROC_MSG_SIZE];
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700175 struct timespec ts;
Matt Helsley9f460802005-11-07 00:59:16 -0800176
177 if (atomic_read(&proc_event_num_listeners) < 1)
178 return;
179
180 msg = (struct cn_msg*)buffer;
181 ev = (struct proc_event*)msg->data;
182 msg->seq = rcvd_seq;
Chandra Seetharaman822cfbff2006-07-30 03:03:04 -0700183 ktime_get_ts(&ts); /* get high res monotonic timestamp */
184 ev->timestamp_ns = timespec_to_ns(&ts);
Matt Helsley9f460802005-11-07 00:59:16 -0800185 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);
192}
193
194/**
195 * cn_proc_mcast_ctl
196 * @data: message sent from userspace via the connector
197 */
198static void cn_proc_mcast_ctl(void *data)
199{
200 struct cn_msg *msg = data;
201 enum proc_cn_mcast_op *mc_op = NULL;
202 int err = 0;
203
204 if (msg->len != sizeof(*mc_op))
205 return;
206
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;
218 }
219 cn_proc_ack(err, msg->seq, msg->ack);
220}
221
222/*
223 * cn_proc_init - initialization entry point
224 *
225 * Adds the connector callback to the connector driver.
226 */
227static int __init cn_proc_init(void)
228{
229 int err;
230
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;
235 }
236 return 0;
237}
238
239module_init(cn_proc_init);