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> |
| 31 | |
| 32 | #include <linux/cn_proc.h> |
| 33 | |
| 34 | #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event)) |
| 35 | |
| 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 }; |
| 38 | |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 39 | /* 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] | 40 | static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 }; |
| 41 | |
| 42 | static inline void get_seq(__u32 *ts, int *cpu) |
| 43 | { |
| 44 | *ts = get_cpu_var(proc_event_counts)++; |
| 45 | *cpu = smp_processor_id(); |
David S. Miller | cc398c2 | 2006-01-08 01:03:34 -0800 | [diff] [blame] | 46 | put_cpu_var(proc_event_counts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 54 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 55 | |
| 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 62 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
| 63 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 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; |
| 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 | |
| 77 | void proc_exec_connector(struct task_struct *task) |
| 78 | { |
| 79 | struct cn_msg *msg; |
| 80 | struct proc_event *ev; |
Chandra Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 81 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 82 | __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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 90 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
| 91 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 92 | 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 | |
| 102 | void 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 107 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 108 | |
| 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 126 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
| 127 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 128 | |
| 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 | |
| 135 | void 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 140 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 141 | |
| 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 148 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
| 149 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 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; |
| 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 | */ |
| 170 | static 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 175 | struct timespec ts; |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 176 | |
| 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 Seetharaman | 822cfbff | 2006-07-30 03:03:04 -0700 | [diff] [blame] | 183 | ktime_get_ts(&ts); /* get high res monotonic timestamp */ |
| 184 | ev->timestamp_ns = timespec_to_ns(&ts); |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 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); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * cn_proc_mcast_ctl |
| 196 | * @data: message sent from userspace via the connector |
| 197 | */ |
| 198 | static 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 | */ |
| 227 | static 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 | |
| 239 | module_init(cn_proc_init); |