xref: /openbmc/linux/drivers/connector/cn_proc.c (revision f0b25932)
19f46080cSMatt Helsley /*
29f46080cSMatt Helsley  * cn_proc.c - process events connector
39f46080cSMatt Helsley  *
49f46080cSMatt Helsley  * Copyright (C) Matt Helsley, IBM Corp. 2005
59f46080cSMatt Helsley  * Based on cn_fork.c by Guillaume Thouvenin <guillaume.thouvenin@bull.net>
69f46080cSMatt Helsley  * Original copyright notice follows:
79f46080cSMatt Helsley  * Copyright (C) 2005 BULL SA.
89f46080cSMatt Helsley  *
99f46080cSMatt Helsley  *
109f46080cSMatt Helsley  * This program is free software; you can redistribute it and/or modify
119f46080cSMatt Helsley  * it under the terms of the GNU General Public License as published by
129f46080cSMatt Helsley  * the Free Software Foundation; either version 2 of the License, or
139f46080cSMatt Helsley  * (at your option) any later version.
149f46080cSMatt Helsley  *
159f46080cSMatt Helsley  * This program is distributed in the hope that it will be useful,
169f46080cSMatt Helsley  * but WITHOUT ANY WARRANTY; without even the implied warranty of
179f46080cSMatt Helsley  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
189f46080cSMatt Helsley  * GNU General Public License for more details.
199f46080cSMatt Helsley  *
209f46080cSMatt Helsley  * You should have received a copy of the GNU General Public License
219f46080cSMatt Helsley  * along with this program; if not, write to the Free Software
229f46080cSMatt Helsley  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
239f46080cSMatt Helsley  */
249f46080cSMatt Helsley 
259f46080cSMatt Helsley #include <linux/module.h>
269f46080cSMatt Helsley #include <linux/kernel.h>
27caf3c9dcSMatt Helsley #include <linux/ktime.h>
289f46080cSMatt Helsley #include <linux/init.h>
291d31a4eaSMatt Helsley #include <linux/connector.h>
309f46080cSMatt Helsley #include <asm/atomic.h>
31af3e095aSErik Jacobson #include <asm/unaligned.h>
329f46080cSMatt Helsley 
339f46080cSMatt Helsley #include <linux/cn_proc.h>
349f46080cSMatt Helsley 
359f46080cSMatt Helsley #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
369f46080cSMatt Helsley 
379f46080cSMatt Helsley static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
389f46080cSMatt Helsley static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
399f46080cSMatt Helsley 
40cc398c2eSDavid S. Miller /* proc_event_counts is used as the sequence number of the netlink message */
419f46080cSMatt Helsley static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
429f46080cSMatt Helsley 
439f46080cSMatt Helsley static inline void get_seq(__u32 *ts, int *cpu)
449f46080cSMatt Helsley {
459f46080cSMatt Helsley 	*ts = get_cpu_var(proc_event_counts)++;
469f46080cSMatt Helsley 	*cpu = smp_processor_id();
47cc398c2eSDavid S. Miller 	put_cpu_var(proc_event_counts);
489f46080cSMatt Helsley }
499f46080cSMatt Helsley 
509f46080cSMatt Helsley void proc_fork_connector(struct task_struct *task)
519f46080cSMatt Helsley {
529f46080cSMatt Helsley 	struct cn_msg *msg;
539f46080cSMatt Helsley 	struct proc_event *ev;
549f46080cSMatt Helsley 	__u8 buffer[CN_PROC_MSG_SIZE];
55822cfbffSChandra Seetharaman 	struct timespec ts;
569f46080cSMatt Helsley 
579f46080cSMatt Helsley 	if (atomic_read(&proc_event_num_listeners) < 1)
589f46080cSMatt Helsley 		return;
599f46080cSMatt Helsley 
609f46080cSMatt Helsley 	msg = (struct cn_msg*)buffer;
619f46080cSMatt Helsley 	ev = (struct proc_event*)msg->data;
629f46080cSMatt Helsley 	get_seq(&msg->seq, &ev->cpu);
63822cfbffSChandra Seetharaman 	ktime_get_ts(&ts); /* get high res monotonic timestamp */
64af3e095aSErik Jacobson 	put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
659f46080cSMatt Helsley 	ev->what = PROC_EVENT_FORK;
669f46080cSMatt Helsley 	ev->event_data.fork.parent_pid = task->real_parent->pid;
679f46080cSMatt Helsley 	ev->event_data.fork.parent_tgid = task->real_parent->tgid;
689f46080cSMatt Helsley 	ev->event_data.fork.child_pid = task->pid;
699f46080cSMatt Helsley 	ev->event_data.fork.child_tgid = task->tgid;
709f46080cSMatt Helsley 
719f46080cSMatt Helsley 	memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
729f46080cSMatt Helsley 	msg->ack = 0; /* not used */
739f46080cSMatt Helsley 	msg->len = sizeof(*ev);
749f46080cSMatt Helsley 	/*  If cn_netlink_send() failed, the data is not sent */
759f46080cSMatt Helsley 	cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
769f46080cSMatt Helsley }
779f46080cSMatt Helsley 
789f46080cSMatt Helsley void proc_exec_connector(struct task_struct *task)
799f46080cSMatt Helsley {
809f46080cSMatt Helsley 	struct cn_msg *msg;
819f46080cSMatt Helsley 	struct proc_event *ev;
82822cfbffSChandra Seetharaman 	struct timespec ts;
839f46080cSMatt Helsley 	__u8 buffer[CN_PROC_MSG_SIZE];
849f46080cSMatt Helsley 
859f46080cSMatt Helsley 	if (atomic_read(&proc_event_num_listeners) < 1)
869f46080cSMatt Helsley 		return;
879f46080cSMatt Helsley 
889f46080cSMatt Helsley 	msg = (struct cn_msg*)buffer;
899f46080cSMatt Helsley 	ev = (struct proc_event*)msg->data;
909f46080cSMatt Helsley 	get_seq(&msg->seq, &ev->cpu);
91822cfbffSChandra Seetharaman 	ktime_get_ts(&ts); /* get high res monotonic timestamp */
92af3e095aSErik Jacobson 	put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
939f46080cSMatt Helsley 	ev->what = PROC_EVENT_EXEC;
949f46080cSMatt Helsley 	ev->event_data.exec.process_pid = task->pid;
959f46080cSMatt Helsley 	ev->event_data.exec.process_tgid = task->tgid;
969f46080cSMatt Helsley 
979f46080cSMatt Helsley 	memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
989f46080cSMatt Helsley 	msg->ack = 0; /* not used */
999f46080cSMatt Helsley 	msg->len = sizeof(*ev);
1009f46080cSMatt Helsley 	cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
1019f46080cSMatt Helsley }
1029f46080cSMatt Helsley 
1039f46080cSMatt Helsley void proc_id_connector(struct task_struct *task, int which_id)
1049f46080cSMatt Helsley {
1059f46080cSMatt Helsley 	struct cn_msg *msg;
1069f46080cSMatt Helsley 	struct proc_event *ev;
1079f46080cSMatt Helsley 	__u8 buffer[CN_PROC_MSG_SIZE];
108822cfbffSChandra Seetharaman 	struct timespec ts;
109c69e8d9cSDavid Howells 	const struct cred *cred;
1109f46080cSMatt Helsley 
1119f46080cSMatt Helsley 	if (atomic_read(&proc_event_num_listeners) < 1)
1129f46080cSMatt Helsley 		return;
1139f46080cSMatt Helsley 
1149f46080cSMatt Helsley 	msg = (struct cn_msg*)buffer;
1159f46080cSMatt Helsley 	ev = (struct proc_event*)msg->data;
1169f46080cSMatt Helsley 	ev->what = which_id;
1179f46080cSMatt Helsley 	ev->event_data.id.process_pid = task->pid;
1189f46080cSMatt Helsley 	ev->event_data.id.process_tgid = task->tgid;
119c69e8d9cSDavid Howells 	rcu_read_lock();
120c69e8d9cSDavid Howells 	cred = __task_cred(task);
1219f46080cSMatt Helsley 	if (which_id == PROC_EVENT_UID) {
122c69e8d9cSDavid Howells 		ev->event_data.id.r.ruid = cred->uid;
123c69e8d9cSDavid Howells 		ev->event_data.id.e.euid = cred->euid;
1249f46080cSMatt Helsley 	} else if (which_id == PROC_EVENT_GID) {
125c69e8d9cSDavid Howells 		ev->event_data.id.r.rgid = cred->gid;
126c69e8d9cSDavid Howells 		ev->event_data.id.e.egid = cred->egid;
127c69e8d9cSDavid Howells 	} else {
128c69e8d9cSDavid Howells 		rcu_read_unlock();
1299f46080cSMatt Helsley 	     	return;
130c69e8d9cSDavid Howells 	}
131c69e8d9cSDavid Howells 	rcu_read_unlock();
1329f46080cSMatt Helsley 	get_seq(&msg->seq, &ev->cpu);
133822cfbffSChandra Seetharaman 	ktime_get_ts(&ts); /* get high res monotonic timestamp */
134af3e095aSErik Jacobson 	put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
1359f46080cSMatt Helsley 
1369f46080cSMatt Helsley 	memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
1379f46080cSMatt Helsley 	msg->ack = 0; /* not used */
1389f46080cSMatt Helsley 	msg->len = sizeof(*ev);
1399f46080cSMatt Helsley 	cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
1409f46080cSMatt Helsley }
1419f46080cSMatt Helsley 
14202b51df1SScott James Remnant void proc_sid_connector(struct task_struct *task)
14302b51df1SScott James Remnant {
14402b51df1SScott James Remnant 	struct cn_msg *msg;
14502b51df1SScott James Remnant 	struct proc_event *ev;
14602b51df1SScott James Remnant 	struct timespec ts;
14702b51df1SScott James Remnant 	__u8 buffer[CN_PROC_MSG_SIZE];
14802b51df1SScott James Remnant 
14902b51df1SScott James Remnant 	if (atomic_read(&proc_event_num_listeners) < 1)
15002b51df1SScott James Remnant 		return;
15102b51df1SScott James Remnant 
15202b51df1SScott James Remnant 	msg = (struct cn_msg *)buffer;
15302b51df1SScott James Remnant 	ev = (struct proc_event *)msg->data;
15402b51df1SScott James Remnant 	get_seq(&msg->seq, &ev->cpu);
15502b51df1SScott James Remnant 	ktime_get_ts(&ts); /* get high res monotonic timestamp */
15602b51df1SScott James Remnant 	put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
15702b51df1SScott James Remnant 	ev->what = PROC_EVENT_SID;
15802b51df1SScott James Remnant 	ev->event_data.sid.process_pid = task->pid;
15902b51df1SScott James Remnant 	ev->event_data.sid.process_tgid = task->tgid;
16002b51df1SScott James Remnant 
16102b51df1SScott James Remnant 	memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
16202b51df1SScott James Remnant 	msg->ack = 0; /* not used */
16302b51df1SScott James Remnant 	msg->len = sizeof(*ev);
16402b51df1SScott James Remnant 	cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
16502b51df1SScott James Remnant }
16602b51df1SScott James Remnant 
1679f46080cSMatt Helsley void proc_exit_connector(struct task_struct *task)
1689f46080cSMatt Helsley {
1699f46080cSMatt Helsley 	struct cn_msg *msg;
1709f46080cSMatt Helsley 	struct proc_event *ev;
1719f46080cSMatt Helsley 	__u8 buffer[CN_PROC_MSG_SIZE];
172822cfbffSChandra Seetharaman 	struct timespec ts;
1739f46080cSMatt Helsley 
1749f46080cSMatt Helsley 	if (atomic_read(&proc_event_num_listeners) < 1)
1759f46080cSMatt Helsley 		return;
1769f46080cSMatt Helsley 
1779f46080cSMatt Helsley 	msg = (struct cn_msg*)buffer;
1789f46080cSMatt Helsley 	ev = (struct proc_event*)msg->data;
1799f46080cSMatt Helsley 	get_seq(&msg->seq, &ev->cpu);
180822cfbffSChandra Seetharaman 	ktime_get_ts(&ts); /* get high res monotonic timestamp */
181af3e095aSErik Jacobson 	put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
1829f46080cSMatt Helsley 	ev->what = PROC_EVENT_EXIT;
1839f46080cSMatt Helsley 	ev->event_data.exit.process_pid = task->pid;
1849f46080cSMatt Helsley 	ev->event_data.exit.process_tgid = task->tgid;
1859f46080cSMatt Helsley 	ev->event_data.exit.exit_code = task->exit_code;
1869f46080cSMatt Helsley 	ev->event_data.exit.exit_signal = task->exit_signal;
1879f46080cSMatt Helsley 
1889f46080cSMatt Helsley 	memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
1899f46080cSMatt Helsley 	msg->ack = 0; /* not used */
1909f46080cSMatt Helsley 	msg->len = sizeof(*ev);
1919f46080cSMatt Helsley 	cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
1929f46080cSMatt Helsley }
1939f46080cSMatt Helsley 
1949f46080cSMatt Helsley /*
1959f46080cSMatt Helsley  * Send an acknowledgement message to userspace
1969f46080cSMatt Helsley  *
1979f46080cSMatt Helsley  * Use 0 for success, EFOO otherwise.
1989f46080cSMatt Helsley  * Note: this is the negative of conventional kernel error
1999f46080cSMatt Helsley  * values because it's not being returned via syscall return
2009f46080cSMatt Helsley  * mechanisms.
2019f46080cSMatt Helsley  */
2029f46080cSMatt Helsley static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
2039f46080cSMatt Helsley {
2049f46080cSMatt Helsley 	struct cn_msg *msg;
2059f46080cSMatt Helsley 	struct proc_event *ev;
2069f46080cSMatt Helsley 	__u8 buffer[CN_PROC_MSG_SIZE];
207822cfbffSChandra Seetharaman 	struct timespec ts;
2089f46080cSMatt Helsley 
2099f46080cSMatt Helsley 	if (atomic_read(&proc_event_num_listeners) < 1)
2109f46080cSMatt Helsley 		return;
2119f46080cSMatt Helsley 
2129f46080cSMatt Helsley 	msg = (struct cn_msg*)buffer;
2139f46080cSMatt Helsley 	ev = (struct proc_event*)msg->data;
2149f46080cSMatt Helsley 	msg->seq = rcvd_seq;
215822cfbffSChandra Seetharaman 	ktime_get_ts(&ts); /* get high res monotonic timestamp */
216af3e095aSErik Jacobson 	put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
2179f46080cSMatt Helsley 	ev->cpu = -1;
2189f46080cSMatt Helsley 	ev->what = PROC_EVENT_NONE;
2199f46080cSMatt Helsley 	ev->event_data.ack.err = err;
2209f46080cSMatt Helsley 	memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
2219f46080cSMatt Helsley 	msg->ack = rcvd_ack + 1;
2229f46080cSMatt Helsley 	msg->len = sizeof(*ev);
2239f46080cSMatt Helsley 	cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
2249f46080cSMatt Helsley }
2259f46080cSMatt Helsley 
2269f46080cSMatt Helsley /**
2279f46080cSMatt Helsley  * cn_proc_mcast_ctl
2289f46080cSMatt Helsley  * @data: message sent from userspace via the connector
2299f46080cSMatt Helsley  */
230f0b25932SStephen Boyd static void cn_proc_mcast_ctl(struct cn_msg *msg,
231f0b25932SStephen Boyd 			      struct netlink_skb_parms *nsp)
2329f46080cSMatt Helsley {
2339f46080cSMatt Helsley 	enum proc_cn_mcast_op *mc_op = NULL;
2349f46080cSMatt Helsley 	int err = 0;
2359f46080cSMatt Helsley 
2369f46080cSMatt Helsley 	if (msg->len != sizeof(*mc_op))
2379f46080cSMatt Helsley 		return;
2389f46080cSMatt Helsley 
2399f46080cSMatt Helsley 	mc_op = (enum proc_cn_mcast_op*)msg->data;
2409f46080cSMatt Helsley 	switch (*mc_op) {
2419f46080cSMatt Helsley 	case PROC_CN_MCAST_LISTEN:
2429f46080cSMatt Helsley 		atomic_inc(&proc_event_num_listeners);
2439f46080cSMatt Helsley 		break;
2449f46080cSMatt Helsley 	case PROC_CN_MCAST_IGNORE:
2459f46080cSMatt Helsley 		atomic_dec(&proc_event_num_listeners);
2469f46080cSMatt Helsley 		break;
2479f46080cSMatt Helsley 	default:
2489f46080cSMatt Helsley 		err = EINVAL;
2499f46080cSMatt Helsley 		break;
2509f46080cSMatt Helsley 	}
2519f46080cSMatt Helsley 	cn_proc_ack(err, msg->seq, msg->ack);
2529f46080cSMatt Helsley }
2539f46080cSMatt Helsley 
2549f46080cSMatt Helsley /*
2559f46080cSMatt Helsley  * cn_proc_init - initialization entry point
2569f46080cSMatt Helsley  *
2579f46080cSMatt Helsley  * Adds the connector callback to the connector driver.
2589f46080cSMatt Helsley  */
2599f46080cSMatt Helsley static int __init cn_proc_init(void)
2609f46080cSMatt Helsley {
2619f46080cSMatt Helsley 	int err;
2629f46080cSMatt Helsley 
2639f46080cSMatt Helsley 	if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc",
2649f46080cSMatt Helsley 	 			   &cn_proc_mcast_ctl))) {
2659f46080cSMatt Helsley 		printk(KERN_WARNING "cn_proc failed to register\n");
2669f46080cSMatt Helsley 		return err;
2679f46080cSMatt Helsley 	}
2689f46080cSMatt Helsley 	return 0;
2699f46080cSMatt Helsley }
2709f46080cSMatt Helsley 
2719f46080cSMatt Helsley module_init(cn_proc_init);
272