From: Guillaume Thouvenin ChangeLog: - Remove __GFP_NOFAIL flag. If an error occurred, the message is just dropped. - Modify struct cn_fork_msg to hold the following values: ID of the cpu where the fork occurred, Parent process id, Parent thread id, Child process id Child thread id All values are from a user's point of view. An explanation has been added in the cn_fork.h header. - Remove "EXPORT_SYMBOL_GPL(fork_connector);" - Move "extern int cn_already_initialized" in file include/linux/connector.h. This declaration should be include in the next release of the connector. TODO: - Change the way the status is displayed. I don't know yet how to do it cleanly. Signed-off-by: Guillaume Thouvenin Signed-off-by: Andrew Morton --- drivers/connector/Kconfig | 12 +++ drivers/connector/Makefile | 1 drivers/connector/cn_fork.c | 166 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/cn_fork.h | 75 +++++++++++++++++++ include/linux/connector.h | 4 + kernel/fork.c | 4 + 6 files changed, 262 insertions(+) diff -puN drivers/connector/cn_fork.c~connector-add-a-fork-connector drivers/connector/cn_fork.c --- devel/drivers/connector/cn_fork.c~connector-add-a-fork-connector 2005-06-30 22:24:28.000000000 -0700 +++ devel-akpm/drivers/connector/cn_fork.c 2005-06-30 22:24:28.000000000 -0700 @@ -0,0 +1,166 @@ +/* + * cn_fork.c - Fork connector + * + * Copyright (C) 2005 BULL SA. + * Written by Guillaume Thouvenin + * + * This module implements the fork connector. It allows to send a + * netlink datagram, when enabled, from the do_fork() routine. The + * message can be read by a user space application. By this way, + * the user space application is alerted when a fork occurs. + * + * It uses the userspace <-> kernelspace connector that works on top of + * the netlink protocol. The fork connector is enabled or disabled by + * sending a message to the connector. The unique sequence number of + * messages can be used to check if a message is lost. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include + +#include + +#define CN_FORK_INFO_SIZE sizeof(struct cn_fork_msg) +#define CN_FORK_MSG_SIZE (sizeof(struct cn_msg) + CN_FORK_INFO_SIZE) + +static int cn_fork_enable = 0; +struct cb_id cb_fork_id = { CN_IDX_FORK, CN_VAL_FORK }; + +/* fork_counts is used as the sequence number of the netlink message */ +static DEFINE_PER_CPU(unsigned long, fork_counts); + +/** + * fork_connector - send information about fork through a connector + * @ppid: Parent process ID + * @ptid: Parent thread ID + * @cpid: Child process ID + * @ctid: Child thread ID + * + * It sends information to a user space application through the + * connector when a new process is created. + */ +void fork_connector(pid_t ppid, pid_t ptid, pid_t cpid, pid_t ctid) +{ + if (cn_fork_enable) { + struct cn_msg *msg; + struct cn_fork_msg *forkmsg; + __u8 buffer[CN_FORK_MSG_SIZE]; + + msg = (struct cn_msg *)buffer; + + memcpy(&msg->id, &cb_fork_id, sizeof(msg->id)); + + msg->ack = 0; /* not used */ + msg->seq = get_cpu_var(fork_counts)++; + + msg->len = CN_FORK_INFO_SIZE; + forkmsg = (struct cn_fork_msg *)msg->data; + forkmsg->type = FORK_CN_MSG_P; + forkmsg->cpu = smp_processor_id(); + forkmsg->u.s.ppid = ppid; + forkmsg->u.s.ptid = ptid; + forkmsg->u.s.cpid = cpid; + forkmsg->u.s.ctid = ctid; + + put_cpu_var(fork_counts); + + /* If cn_netlink_send() failed, the data is not send */ + cn_netlink_send(msg, CN_IDX_FORK, GFP_KERNEL); + } +} + +/** + * cn_fork_send_status - send a message with the status + * + * It sends information about the status of the fork connector + * to a user space application through the connector. The status + * is stored in the global variable "cn_fork_enable". + */ +static inline void cn_fork_send_status(void) +{ + struct cn_msg *msg; + struct cn_fork_msg *forkmsg; + __u8 buffer[CN_FORK_MSG_SIZE]; + + msg = (struct cn_msg *)buffer; + + memcpy(&msg->id, &cb_fork_id, sizeof(msg->id)); + + msg->ack = 0; /* not used */ + msg->seq = 0; /* not used */ + + msg->len = CN_FORK_INFO_SIZE; + forkmsg = (struct cn_fork_msg *)msg->data; + forkmsg->type = FORK_CN_MSG_S; + forkmsg->u.status = cn_fork_enable; + + cn_netlink_send(msg, CN_IDX_FORK, GFP_KERNEL); +} + +/** + * cn_fork_callback - enable or disable the fork connector + * @data: message send by the connector + * + * The callback allows to enable or disable the sending of information + * about fork in the do_fork() routine. To enable the fork, the user + * space application must send the integer 1 in the data part of the + * message. To disable the fork connector, it must send the integer 0. + */ +static void cn_fork_callback(void *data) +{ + struct cn_msg *msg = data; + int action; + + if (cn_already_initialized && (msg->len == sizeof(cn_fork_enable))) { + memcpy(&action, msg->data, sizeof(cn_fork_enable)); + switch (action) { + case FORK_CN_START: + cn_fork_enable = 1; + break; + case FORK_CN_STOP: + cn_fork_enable = 0; + break; + case FORK_CN_STATUS: + cn_fork_send_status(); + break; + } + } +} + +/** + * cn_fork_init - initialization entry point + * + * This routine will be run at kernel boot time because this driver is + * built in the kernel. It adds the connector callback to the connector + * driver. + */ +int __init cn_fork_init(void) +{ + int err; + + err = cn_add_callback(&cb_fork_id, "cn_fork", &cn_fork_callback); + if (err) { + printk(KERN_WARNING "Failed to register cn_fork\n"); + return -EINVAL; + } + + printk(KERN_NOTICE "cn_fork is registered\n"); + return 0; +} + +__initcall(cn_fork_init); diff -puN drivers/connector/Kconfig~connector-add-a-fork-connector drivers/connector/Kconfig --- devel/drivers/connector/Kconfig~connector-add-a-fork-connector 2005-06-30 22:24:28.000000000 -0700 +++ devel-akpm/drivers/connector/Kconfig 2005-06-30 22:24:49.000000000 -0700 @@ -20,4 +20,16 @@ config EXIT_CONNECTOR its parent. This information can be used by a user space application. The exit connector can be enable/disable by sending a message to the connector with the corresponding group id. + +config FORK_CONNECTOR + bool "Enable fork connector" + select CONNECTOR + default y + ---help--- + It adds a connector in kernel/fork.c:do_fork() function. When a fork + occurs, netlink is used to transfer information about the parent and + its child. This information can be used by a user space application. + The fork connector can be enable/disable by sending a message to the + connector with the corresponding group id. + endmenu diff -puN drivers/connector/Makefile~connector-add-a-fork-connector drivers/connector/Makefile --- devel/drivers/connector/Makefile~connector-add-a-fork-connector 2005-06-30 22:24:28.000000000 -0700 +++ devel-akpm/drivers/connector/Makefile 2005-06-30 22:24:28.000000000 -0700 @@ -1,3 +1,4 @@ obj-$(CONFIG_CONNECTOR) += cn.o +obj-$(CONFIG_FORK_CONNECTOR) += cn_fork.o cn-objs := cn_queue.o connector.o obj-$(CONFIG_EXIT_CONNECTOR) += cn_exit.o diff -puN include/linux/cn_fork.h~connector-add-a-fork-connector include/linux/cn_fork.h --- devel/include/linux/cn_fork.h~connector-add-a-fork-connector 2005-06-30 22:24:28.000000000 -0700 +++ devel-akpm/include/linux/cn_fork.h 2005-06-30 22:24:28.000000000 -0700 @@ -0,0 +1,75 @@ +/* + * cn_fork.h - Fork connector + * + * Copyright (C) 2005 Nguyen Anh Quynh + * Copyright (C) 2005 Guillaume Thouvenin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef CN_FORK_H +#define CN_FORK_H + +#include +#include + +#define FORK_CN_STOP 0 +#define FORK_CN_START 1 +#define FORK_CN_STATUS 2 + +#define FORK_CN_MSG_P 0 /* Message about processes */ +#define FORK_CN_MSG_S 1 /* Message about fork connector's state */ + +/* + * The fork connector sends information to a user-space + * application. From the user's point of view, the process + * ID is the thread group ID and thread ID is the internal + * kernel "pid". So, fields are assigned as follow: + * + * In user space - In kernel space + * + * parent process ID = parent->tgid + * parent thread ID = parent->pid + * child process ID = child->tgid + * child thread ID = child->pid + */ +struct cn_fork_msg { + int type; /* 0: information about processes + 1: fork connector's state */ + int cpu; /* ID of the cpu where the fork occurred */ + union { + struct { + pid_t ppid; /* parent process ID */ + pid_t ptid; /* parent thread ID */ + pid_t cpid; /* child process ID */ + pid_t ctid; /* child thread ID */ + } s; + int status; + } u; +}; + +/* Code above is only inside the kernel */ +#ifdef __KERNEL__ +#ifdef CONFIG_FORK_CONNECTOR +extern void fork_connector(pid_t ppid, pid_t ptid, pid_t cpid, pid_t ctid); +#else +static inline void fork_connector(pid_t ppid, pid_t ptid, pid_t cpid, + pid_t ctid) +{ + return; +} +#endif /* CONFIG_FORK_CONNECTOR */ +#endif /* __KERNEL__ */ +#endif /* CN_FORK_H */ diff -puN include/linux/connector.h~connector-add-a-fork-connector include/linux/connector.h --- devel/include/linux/connector.h~connector-add-a-fork-connector 2005-06-30 22:24:28.000000000 -0700 +++ devel-akpm/include/linux/connector.h 2005-06-30 22:24:28.000000000 -0700 @@ -26,6 +26,8 @@ #define CN_IDX_CONNECTOR 0xffffffff #define CN_VAL_CONNECTOR 0xffffffff +#define CN_IDX_FORK 0xfeed /* fork events */ +#define CN_VAL_FORK 0xbeef #define CN_IDX_EXIT 0xfeec /* exit events */ #define CN_VAL_EXIT 0xceef @@ -140,6 +142,8 @@ struct cn_dev { struct cn_queue_dev *cbdev; }; +extern int cn_already_initialized; + int cn_add_callback(struct cb_id *, char *, void (*callback) (void *)); void cn_del_callback(struct cb_id *); int cn_netlink_send(struct cn_msg *, u32, int); diff -puN kernel/fork.c~connector-add-a-fork-connector kernel/fork.c --- devel/kernel/fork.c~connector-add-a-fork-connector 2005-06-30 22:24:28.000000000 -0700 +++ devel-akpm/kernel/fork.c 2005-06-30 22:24:28.000000000 -0700 @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -1255,6 +1256,9 @@ long do_fork(unsigned long clone_flags, if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP); } + + fork_connector(current->tgid, current->pid, + p->tgid, p->pid); } else { free_pidmap(pid); pid = PTR_ERR(p); _