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