Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Helsley | caf3c9d | 2006-01-09 20:52:40 -0800 | [diff] [blame] | 27 | #include <linux/ktime.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 28 | #include <linux/init.h> |
Matt Helsley | 1d31a4e | 2006-06-23 02:05:42 -0700 | [diff] [blame] | 29 | #include <linux/connector.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/gfp.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 31 | #include <asm/atomic.h> |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 33 | |
| 34 | #include <linux/cn_proc.h> |
| 35 | |
| 36 | #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event)) |
| 37 | |
| 38 | static atomic_t proc_event_num_listeners = ATOMIC_INIT(0); |
| 39 | static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC }; |
| 40 | |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 41 | /* proc_event_counts is used as the sequence number of the netlink message */ |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 42 | static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 }; |
| 43 | |
| 44 | static inline void get_seq(__u32 *ts, int *cpu) |
| 45 | { |
| 46 | *ts = get_cpu_var(proc_event_counts)++; |
| 47 | *cpu = smp_processor_id(); |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 48 | put_cpu_var(proc_event_counts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void proc_fork_connector(struct task_struct *task) |
| 52 | { |
| 53 | struct cn_msg *msg; |
| 54 | struct proc_event *ev; |
| 55 | __u8 buffer[CN_PROC_MSG_SIZE]; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 56 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 57 | |
| 58 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 59 | return; |
| 60 | |
| 61 | msg = (struct cn_msg*)buffer; |
| 62 | ev = (struct proc_event*)msg->data; |
| 63 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 64 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 65 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 66 | ev->what = PROC_EVENT_FORK; |
| 67 | ev->event_data.fork.parent_pid = task->real_parent->pid; |
| 68 | ev->event_data.fork.parent_tgid = task->real_parent->tgid; |
| 69 | ev->event_data.fork.child_pid = task->pid; |
| 70 | ev->event_data.fork.child_tgid = task->tgid; |
| 71 | |
| 72 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 73 | msg->ack = 0; /* not used */ |
| 74 | msg->len = sizeof(*ev); |
| 75 | /* If cn_netlink_send() failed, the data is not sent */ |
| 76 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 77 | } |
| 78 | |
| 79 | void proc_exec_connector(struct task_struct *task) |
| 80 | { |
| 81 | struct cn_msg *msg; |
| 82 | struct proc_event *ev; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 83 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 84 | __u8 buffer[CN_PROC_MSG_SIZE]; |
| 85 | |
| 86 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 87 | return; |
| 88 | |
| 89 | msg = (struct cn_msg*)buffer; |
| 90 | ev = (struct proc_event*)msg->data; |
| 91 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 92 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 93 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 94 | ev->what = PROC_EVENT_EXEC; |
| 95 | ev->event_data.exec.process_pid = task->pid; |
| 96 | ev->event_data.exec.process_tgid = task->tgid; |
| 97 | |
| 98 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 99 | msg->ack = 0; /* not used */ |
| 100 | msg->len = sizeof(*ev); |
| 101 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 102 | } |
| 103 | |
| 104 | void proc_id_connector(struct task_struct *task, int which_id) |
| 105 | { |
| 106 | struct cn_msg *msg; |
| 107 | struct proc_event *ev; |
| 108 | __u8 buffer[CN_PROC_MSG_SIZE]; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 109 | struct timespec ts; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 110 | const struct cred *cred; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 111 | |
| 112 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 113 | return; |
| 114 | |
| 115 | msg = (struct cn_msg*)buffer; |
| 116 | ev = (struct proc_event*)msg->data; |
| 117 | ev->what = which_id; |
| 118 | ev->event_data.id.process_pid = task->pid; |
| 119 | ev->event_data.id.process_tgid = task->tgid; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 120 | rcu_read_lock(); |
| 121 | cred = __task_cred(task); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 122 | if (which_id == PROC_EVENT_UID) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 123 | ev->event_data.id.r.ruid = cred->uid; |
| 124 | ev->event_data.id.e.euid = cred->euid; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 125 | } else if (which_id == PROC_EVENT_GID) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 126 | ev->event_data.id.r.rgid = cred->gid; |
| 127 | ev->event_data.id.e.egid = cred->egid; |
| 128 | } else { |
| 129 | rcu_read_unlock(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 130 | return; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 131 | } |
| 132 | rcu_read_unlock(); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 133 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 134 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 135 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 136 | |
| 137 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 138 | msg->ack = 0; /* not used */ |
| 139 | msg->len = sizeof(*ev); |
| 140 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 141 | } |
| 142 | |
Scott James Remnant | 02b51df | 2009-09-22 16:43:44 -0700 | [diff] [blame] | 143 | void proc_sid_connector(struct task_struct *task) |
| 144 | { |
| 145 | struct cn_msg *msg; |
| 146 | struct proc_event *ev; |
| 147 | struct timespec ts; |
| 148 | __u8 buffer[CN_PROC_MSG_SIZE]; |
| 149 | |
| 150 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 151 | return; |
| 152 | |
| 153 | msg = (struct cn_msg *)buffer; |
| 154 | ev = (struct proc_event *)msg->data; |
| 155 | get_seq(&msg->seq, &ev->cpu); |
| 156 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
| 157 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
| 158 | ev->what = PROC_EVENT_SID; |
| 159 | ev->event_data.sid.process_pid = task->pid; |
| 160 | ev->event_data.sid.process_tgid = task->tgid; |
| 161 | |
| 162 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 163 | msg->ack = 0; /* not used */ |
| 164 | msg->len = sizeof(*ev); |
| 165 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 166 | } |
| 167 | |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 168 | void proc_exit_connector(struct task_struct *task) |
| 169 | { |
| 170 | struct cn_msg *msg; |
| 171 | struct proc_event *ev; |
| 172 | __u8 buffer[CN_PROC_MSG_SIZE]; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 173 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 174 | |
| 175 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 176 | return; |
| 177 | |
| 178 | msg = (struct cn_msg*)buffer; |
| 179 | ev = (struct proc_event*)msg->data; |
| 180 | get_seq(&msg->seq, &ev->cpu); |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 181 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 182 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 183 | ev->what = PROC_EVENT_EXIT; |
| 184 | ev->event_data.exit.process_pid = task->pid; |
| 185 | ev->event_data.exit.process_tgid = task->tgid; |
| 186 | ev->event_data.exit.exit_code = task->exit_code; |
| 187 | ev->event_data.exit.exit_signal = task->exit_signal; |
| 188 | |
| 189 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 190 | msg->ack = 0; /* not used */ |
| 191 | msg->len = sizeof(*ev); |
| 192 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Send an acknowledgement message to userspace |
| 197 | * |
| 198 | * Use 0 for success, EFOO otherwise. |
| 199 | * Note: this is the negative of conventional kernel error |
| 200 | * values because it's not being returned via syscall return |
| 201 | * mechanisms. |
| 202 | */ |
| 203 | static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack) |
| 204 | { |
| 205 | struct cn_msg *msg; |
| 206 | struct proc_event *ev; |
| 207 | __u8 buffer[CN_PROC_MSG_SIZE]; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 208 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 209 | |
| 210 | if (atomic_read(&proc_event_num_listeners) < 1) |
| 211 | return; |
| 212 | |
| 213 | msg = (struct cn_msg*)buffer; |
| 214 | ev = (struct proc_event*)msg->data; |
| 215 | msg->seq = rcvd_seq; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 216 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
Erik Jacobson | af3e095 | 2007-01-05 16:37:05 -0800 | [diff] [blame] | 217 | put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 218 | ev->cpu = -1; |
| 219 | ev->what = PROC_EVENT_NONE; |
| 220 | ev->event_data.ack.err = err; |
| 221 | memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id)); |
| 222 | msg->ack = rcvd_ack + 1; |
| 223 | msg->len = sizeof(*ev); |
| 224 | cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * cn_proc_mcast_ctl |
| 229 | * @data: message sent from userspace via the connector |
| 230 | */ |
Stephen Boyd | f0b2593 | 2009-10-06 01:39:51 -0700 | [diff] [blame] | 231 | static void cn_proc_mcast_ctl(struct cn_msg *msg, |
| 232 | struct netlink_skb_parms *nsp) |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 233 | { |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 234 | enum proc_cn_mcast_op *mc_op = NULL; |
| 235 | int err = 0; |
| 236 | |
| 237 | if (msg->len != sizeof(*mc_op)) |
| 238 | return; |
| 239 | |
| 240 | mc_op = (enum proc_cn_mcast_op*)msg->data; |
| 241 | switch (*mc_op) { |
| 242 | case PROC_CN_MCAST_LISTEN: |
| 243 | atomic_inc(&proc_event_num_listeners); |
| 244 | break; |
| 245 | case PROC_CN_MCAST_IGNORE: |
| 246 | atomic_dec(&proc_event_num_listeners); |
| 247 | break; |
| 248 | default: |
| 249 | err = EINVAL; |
| 250 | break; |
| 251 | } |
| 252 | cn_proc_ack(err, msg->seq, msg->ack); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * cn_proc_init - initialization entry point |
| 257 | * |
| 258 | * Adds the connector callback to the connector driver. |
| 259 | */ |
| 260 | static int __init cn_proc_init(void) |
| 261 | { |
| 262 | int err; |
| 263 | |
| 264 | if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc", |
| 265 | &cn_proc_mcast_ctl))) { |
| 266 | printk(KERN_WARNING "cn_proc failed to register\n"); |
| 267 | return err; |
| 268 | } |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | module_init(cn_proc_init); |