mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [Suspend2][ 00/13] Generic netlink support.
@ 2006-06-27  4:39 Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 01/13] [Suspend2] Netlink.c header Nigel Cunningham
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel


Routines used by both the user interface code and storage manager
to interface with userspace programs through netlink sockets.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 01/13] [Suspend2] Netlink.c header
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 02/13] [Suspend2] Fill skb pool Nigel Cunningham
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

kernel/power/netlink.c implements the interface between Suspend2 and
userspace helpers (the userui and storage manager).

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
new file mode 100644
index 0000000..8d27801
--- /dev/null
+++ b/kernel/power/netlink.c
@@ -0,0 +1,17 @@
+/*
+ * kernel/power/netlink.c
+ *
+ * Copyright (C) 2004-2006 Nigel Cunningham <nigel@suspend2.net>
+ *
+ * This file is released under the GPLv2.
+ *
+ * Functions for communicating with a userspace helper via netlink.
+ */
+
+
+#include <linux/suspend.h>
+#include "netlink.h"
+
+#ifdef CONFIG_NET
+struct user_helper_data *uhd_list = NULL;
+       

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 02/13] [Suspend2] Fill skb pool.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 01/13] [Suspend2] Netlink.c header Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 03/13] [Suspend2] Get and put skbs Nigel Cunningham
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Fill the skb pool used to communicate with userspace helpers.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 8d27801..d169096 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -15,3 +15,22 @@
 #ifdef CONFIG_NET
 struct user_helper_data *uhd_list = NULL;
        
+/* 
+ * Refill our pool of SKBs for use in emergencies (eg, when eating memory and none
+ * can be allocated).
+ */
+static void suspend_fill_skb_pool(struct user_helper_data *uhd)
+{
+	while (uhd->pool_level < uhd->pool_limit) {
+		struct sk_buff *new_skb =
+			alloc_skb(NLMSG_SPACE(uhd->skb_size), GFP_ATOMIC);
+
+		if (!new_skb)
+			break;
+
+		new_skb->next = uhd->emerg_skbs;
+		uhd->emerg_skbs = new_skb;
+		uhd->pool_level++;
+	}
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 03/13] [Suspend2] Get and put skbs.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 01/13] [Suspend2] Netlink.c header Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 02/13] [Suspend2] Fill skb pool Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 04/13] [Suspend2] Notify userspace of a new message Nigel Cunningham
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Get and put skbs used to communicate with userspace helpers.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index d169096..82a9f6d 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -34,3 +34,34 @@ static void suspend_fill_skb_pool(struct
 	}
 }
 
+/* 
+ * Try to allocate a single skb. If we can't get one, try to use one from
+ * our pool.
+ */
+static struct sk_buff *suspend_get_skb(struct user_helper_data *uhd)
+{
+	struct sk_buff *skb =
+		alloc_skb(NLMSG_SPACE(uhd->skb_size), GFP_ATOMIC);
+
+	if (skb)
+		return skb;
+
+	skb = uhd->emerg_skbs;
+	if (skb) {
+		uhd->pool_level--;
+		uhd->emerg_skbs = skb->next;
+		skb->next = NULL;
+	}
+
+	return skb;
+}
+
+static void put_skb(struct user_helper_data *uhd, struct sk_buff *skb)
+{
+	if (uhd->pool_level < uhd->pool_limit) {
+		skb->next = uhd->emerg_skbs;
+		uhd->emerg_skbs = skb;
+	} else
+		kfree_skb(skb);
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 04/13] [Suspend2] Notify userspace of a new message.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (2 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 03/13] [Suspend2] Get and put skbs Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 05/13] [Suspend2] Send netlink message to userspace helper Nigel Cunningham
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Wake up a userspace process when we have a new netlink message for it to
process.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 82a9f6d..c8c543a 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -65,3 +65,17 @@ static void put_skb(struct user_helper_d
 		kfree_skb(skb);
 }
 
+
+static void suspend_notify_userspace(void* data)
+{
+	struct task_struct *t;
+	struct user_helper_data *uhd = (struct user_helper_data *) data;
+
+	BUG_ON(!uhd);
+
+	read_lock(&tasklist_lock);
+	if ((t = find_task_by_pid(uhd->pid)))
+		wake_up_process(t);
+	read_unlock(&tasklist_lock);
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 05/13] [Suspend2] Send netlink message to userspace helper.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (3 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 04/13] [Suspend2] Notify userspace of a new message Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 06/13] [Suspend2] Tell userui whether debugging is enabled Nigel Cunningham
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Send a netlink message to a userspace helper.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index c8c543a..696009d 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -79,3 +79,39 @@ static void suspend_notify_userspace(voi
 	read_unlock(&tasklist_lock);
 }
 
+DECLARE_WORK(suspend_notify_userspace_work, suspend_notify_userspace, NULL);
+
+void suspend_send_netlink_message(struct user_helper_data *uhd,
+		int type, void* params, size_t len)
+{
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	void *dest;
+
+	skb = suspend_get_skb(uhd);
+	if (!skb) {
+		printk("suspend_netlink: Can't allocate skb!\n");
+		return;
+	}
+
+	/* NLMSG_PUT contains a hidden goto nlmsg_failure */
+	nlh = NLMSG_PUT(skb, 0, uhd->sock_seq, type, len);
+	uhd->sock_seq++;
+
+	dest = NLMSG_DATA(nlh);
+	if (params && len > 0)
+		memcpy(dest, params, len);
+
+	netlink_unicast(uhd->nl, skb, uhd->pid, 0);
+
+	/* We may be in an interrupt context so defer waking up userspace */
+	suspend_notify_userspace_work.data = uhd;
+	schedule_work(&suspend_notify_userspace_work);
+
+	return;
+
+nlmsg_failure:
+	if (skb)
+		put_skb(uhd, skb);
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 06/13] [Suspend2] Tell userui whether debugging is enabled.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (4 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 05/13] [Suspend2] Send netlink message to userspace helper Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 07/13] [Suspend2] Set NoFreeze flag on a userspace helper Nigel Cunningham
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Send a netlink message to the userspace ui component (if any), notifying it
as to whether debugging support is enabled.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 696009d..0aaf90e 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -115,3 +115,15 @@ nlmsg_failure:
 		put_skb(uhd, skb);
 }
 
+#ifdef CONFIG_PM_DEBUG
+static int is_debugging = 1;
+#else
+static int is_debugging = 0;
+#endif
+
+static void send_whether_debugging(struct user_helper_data *uhd)
+{
+	suspend_send_netlink_message(uhd, NETLINK_MSG_IS_DEBUGGING,
+			&is_debugging, sizeof(int));
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 07/13] [Suspend2] Set NoFreeze flag on a userspace helper.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (5 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 06/13] [Suspend2] Tell userui whether debugging is enabled Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 08/13] [Suspend2] Userspace is ready! Nigel Cunningham
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Set the NoFreeze flag on a userspace helper (userui or storage manager) so
that it can continue to function while we're doing I/O.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 0aaf90e..7f06f7c 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -127,3 +127,28 @@ static void send_whether_debugging(struc
 			&is_debugging, sizeof(int));
 }
 
+/*
+ * Set the PF_NOFREEZE flag on the given process to ensure it can run whilst we
+ * are suspending.
+ */
+static int nl_set_nofreeze(struct user_helper_data *uhd, int pid)
+{
+	struct task_struct *t;
+
+	read_lock(&tasklist_lock);
+	if ((t = find_task_by_pid(pid)) == NULL) {
+		read_unlock(&tasklist_lock);
+		printk("Strange. Can't find the userspace task %d.\n", pid);
+		return -EINVAL;
+	}
+
+	t->flags |= PF_NOFREEZE;
+
+	read_unlock(&tasklist_lock);
+	uhd->pid = pid;
+
+	suspend_send_netlink_message(uhd, NETLINK_MSG_NOFREEZE_ACK, NULL, 0);
+
+	return 0;
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 08/13] [Suspend2] Userspace is ready!
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (6 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 07/13] [Suspend2] Set NoFreeze flag on a userspace helper Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 09/13] [Suspend2] Netlink receive message routines Nigel Cunningham
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Routine called when we are waiting for a userspace helper to start, and
receive a netlink message from it, saying it's ready to go. We check the
version for compatability, and if all looks good, call the completion
handler on the waiting code. If not, we complain bitterly and try to
continue without it.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 7f06f7c..9094c92 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -152,3 +152,22 @@ static int nl_set_nofreeze(struct user_h
 	return 0;
 }
 
+/*
+ * Called when the userspace process has informed us that it's ready to roll.
+ */
+static int nl_ready(struct user_helper_data *uhd, int version)
+{
+	if (version != uhd->interface_version) {
+		printk("%s userspace process using invalid interface version."
+				" Trying to continue without it.\n",
+				uhd->name);
+		if (uhd->not_ready)
+			uhd->not_ready();
+		return 1;
+	}
+
+	complete(&uhd->wait_for_process);
+
+	return 0;
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 09/13] [Suspend2] Netlink receive message routines.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (7 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 08/13] [Suspend2] Userspace is ready! Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:39 ` [Suspend2][ 10/13] [Suspend2] Prepare/cleanup netlink connections Nigel Cunningham
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Suspend2's routines for receiving netlink messages. We don't accept
that many messages, so the logic is pretty simple.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   86 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 9094c92..c5b4bea 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -171,3 +171,89 @@ static int nl_ready(struct user_helper_d
 	return 0;
 }
 
+static int suspend_nl_gen_rcv_msg(struct user_helper_data *uhd,
+		struct sk_buff *skb, struct nlmsghdr *nlh)
+{
+	int type;
+	int *data;
+	int err;
+
+	/* Let the more specific handler go first. It returns
+	 * 1 for valid messages that it doesn't know. */
+	if ((err = uhd->rcv_msg(skb, nlh)) != 1)
+		return err;
+	
+	type = nlh->nlmsg_type;
+
+	/* Only allow one task to receive NOFREEZE privileges */
+	if (type == NETLINK_MSG_NOFREEZE_ME && uhd->pid != -1) {
+		printk("Received extra nofreeze me requests.\n");
+		return -EBUSY;
+	}
+
+	data = (int*)NLMSG_DATA(nlh);
+
+	switch (type) {
+		case NETLINK_MSG_NOFREEZE_ME:
+			if ((err = nl_set_nofreeze(uhd, nlh->nlmsg_pid)) != 0)
+				return err;
+			break;
+		case NETLINK_MSG_GET_DEBUGGING:
+			send_whether_debugging(uhd);
+			break;
+		case NETLINK_MSG_READY:
+			if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
+				printk("Invalid ready mesage.\n");
+				return -EINVAL;
+			}
+			if ((err = nl_ready(uhd, *data)) != 0)
+				return err;
+			break;
+	}
+
+	return 0;
+}
+
+static void suspend_user_rcv_skb(struct user_helper_data *uhd,
+				  struct sk_buff *skb)
+{
+	int err;
+	struct nlmsghdr *nlh;
+
+	while (skb->len >= NLMSG_SPACE(0)) {
+		u32 rlen;
+
+		nlh = (struct nlmsghdr *) skb->data;
+		if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
+			return;
+
+		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
+		if (rlen > skb->len)
+			rlen = skb->len;
+
+		if ((err = suspend_nl_gen_rcv_msg(uhd, skb, nlh)) != 0)
+			netlink_ack(skb, nlh, err);
+		else if (nlh->nlmsg_flags & NLM_F_ACK)
+			netlink_ack(skb, nlh, 0);
+		skb_pull(skb, rlen);
+	}
+}
+
+static void suspend_netlink_input(struct sock *sk, int len)
+{
+	struct user_helper_data *uhd = uhd_list;
+
+	while (uhd && uhd->netlink_id != sk->sk_protocol)
+		uhd= uhd->next;
+
+	BUG_ON(!uhd);
+
+	do {
+		struct sk_buff *skb;
+		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
+			suspend_user_rcv_skb(uhd, skb);
+			put_skb(uhd, skb);
+		}
+	} while (uhd->nl && uhd->nl->sk_receive_queue.qlen);
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 10/13] [Suspend2] Prepare/cleanup netlink connections.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (8 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 09/13] [Suspend2] Netlink receive message routines Nigel Cunningham
@ 2006-06-27  4:39 ` Nigel Cunningham
  2006-06-27  4:40 ` [Suspend2][ 11/13] [Suspend2] Launch a userspace helper Nigel Cunningham
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:39 UTC (permalink / raw)
  To: linux-kernel

Prepare to communicate with a userspace helper over a netlink socket and
cleanup afterwards.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   33 +++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index c5b4bea..7b75f4f 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -257,3 +257,36 @@ static void suspend_netlink_input(struct
 	} while (uhd->nl && uhd->nl->sk_receive_queue.qlen);
 }
 
+static int netlink_prepare(struct user_helper_data *uhd)
+{
+	uhd->next = uhd_list;
+	uhd_list = uhd;
+
+	uhd->sock_seq = 0x42c0ffee;
+	uhd->nl = netlink_kernel_create(uhd->netlink_id, 0,
+			suspend_netlink_input, THIS_MODULE);
+	if (!uhd->nl) {
+		printk("Failed to allocate netlink socket for %s.\n",
+				uhd->name);
+		return -ENOMEM;
+	}
+
+	suspend_fill_skb_pool(uhd);
+
+	return 0;
+}
+
+void suspend_netlink_close(struct user_helper_data *uhd)
+{
+	if (uhd->nl) {
+		sock_release(uhd->nl->sk_socket);
+		uhd->nl = NULL;
+	}
+
+	while (uhd->emerg_skbs) {
+		struct sk_buff *next = uhd->emerg_skbs->next;
+		kfree_skb(uhd->emerg_skbs);
+		uhd->emerg_skbs = next;
+	}
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 11/13] [Suspend2] Launch a userspace helper.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (9 preceding siblings ...)
  2006-06-27  4:39 ` [Suspend2][ 10/13] [Suspend2] Prepare/cleanup netlink connections Nigel Cunningham
@ 2006-06-27  4:40 ` Nigel Cunningham
  2006-06-27  4:40 ` [Suspend2][ 12/13] [Suspend2] Start " Nigel Cunningham
  2006-06-27  4:40 ` [Suspend2][ 13/13] [Suspend2] Netlink header Nigel Cunningham
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:40 UTC (permalink / raw)
  To: linux-kernel

Launch a userspace helper. The usermodehelper function can't take a simple
string with args, so we split the arguments up into words (as required)
before invoking usermodehelper.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index 7b75f4f..d7a3a90 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -290,3 +290,57 @@ void suspend_netlink_close(struct user_h
 	}
 }
 
+int suspend2_launch_userspace_program(char *command, int channel_no)
+{
+	int retval;
+	static char *envp[] = {
+			"HOME=/",
+			"TERM=linux",
+			"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
+			NULL };
+	static char *argv[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+	char *channel = kmalloc(6, GFP_KERNEL);
+	int arg = 0, size;
+	char test_read[255];
+	char *orig_posn = command;
+
+	if (!strlen(orig_posn))
+		return 1;
+
+	/* Up to 7 args supported */
+	while (arg < 7) {
+		sscanf(orig_posn, "%s", test_read);
+		size = strlen(test_read);
+		if (!(size))
+			break;
+		argv[arg] = kmalloc(size + 1, GFP_ATOMIC);
+		strcpy(argv[arg], test_read);
+		orig_posn += size + 1;
+		*test_read = 0;
+		arg++;
+	}
+	
+	if (channel_no) {
+		sprintf(channel, "-c%d", channel_no);
+		argv[arg] = channel;
+	} else
+		arg--;
+
+	retval = call_usermodehelper(argv[0], argv, envp, 0);
+
+	if (retval)
+		printk("Failed to launch userspace program '%s': Error %d\n",
+				command, retval);
+
+	{
+		int i;
+		for (i = 0; i < arg; i++)
+			if (argv[i] && argv[i] != channel)
+				kfree(argv[i]);
+	}
+
+	kfree(channel);
+
+	return retval;
+}
+

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 12/13] [Suspend2] Start a userspace helper.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (10 preceding siblings ...)
  2006-06-27  4:40 ` [Suspend2][ 11/13] [Suspend2] Launch a userspace helper Nigel Cunningham
@ 2006-06-27  4:40 ` Nigel Cunningham
  2006-06-27  4:40 ` [Suspend2][ 13/13] [Suspend2] Netlink header Nigel Cunningham
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:40 UTC (permalink / raw)
  To: linux-kernel

Attempt to start a userspace helper and set up the netlink socket
connection to it.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.c |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.c b/kernel/power/netlink.c
index d7a3a90..0a379c3 100644
--- a/kernel/power/netlink.c
+++ b/kernel/power/netlink.c
@@ -344,3 +344,29 @@ int suspend2_launch_userspace_program(ch
 	return retval;
 }
 
+int suspend_netlink_setup(struct user_helper_data *uhd)
+{
+	if (netlink_prepare(uhd) < 0) {
+		printk("Netlink prepare failed.\n");
+		return 1;
+	}
+
+	if (suspend2_launch_userspace_program(uhd->program, uhd->netlink_id) < 0) {
+		printk("Launch userspace program failed.\n");
+		suspend_netlink_close(uhd);
+		return 1;
+	}
+
+	/* Wait 2 seconds for the userspace process to make contact */
+	wait_for_completion_timeout(&uhd->wait_for_process, 2*HZ);
+
+	if (uhd->pid == -1) {
+		printk("%s: Failed to contact userspace process.\n",
+				uhd->name);
+		suspend_netlink_close(uhd);
+		return 1;
+	}
+
+	return 0;
+}
+#endif

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Suspend2][ 13/13] [Suspend2] Netlink header.
  2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
                   ` (11 preceding siblings ...)
  2006-06-27  4:40 ` [Suspend2][ 12/13] [Suspend2] Start " Nigel Cunningham
@ 2006-06-27  4:40 ` Nigel Cunningham
  12 siblings, 0 replies; 14+ messages in thread
From: Nigel Cunningham @ 2006-06-27  4:40 UTC (permalink / raw)
  To: linux-kernel

Header file describing userspace helper data, generic message numbers and
exported functions.

Signed-off-by: Nigel Cunningham <nigel@suspend2.net>

 kernel/power/netlink.h |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/kernel/power/netlink.h b/kernel/power/netlink.h
new file mode 100644
index 0000000..e55533a
--- /dev/null
+++ b/kernel/power/netlink.h
@@ -0,0 +1,59 @@
+/*
+ * kernel/power/netlink.h
+ *
+ * Copyright (C) 2004-2006 Nigel Cunningham <nigel@suspend2.net>
+ *
+ * This file is released under the GPLv2.
+ *
+ * Declarations for functions for communicating with a userspace helper
+ * via netlink.
+ */
+
+#include <linux/netlink.h>
+#include <net/sock.h>
+
+#define NETLINK_MSG_BASE 0x10
+
+#define NETLINK_MSG_READY 0x10
+#define	NETLINK_MSG_NOFREEZE_ME 0x16
+#define NETLINK_MSG_GET_DEBUGGING 0x19
+#define NETLINK_MSG_CLEANUP 0x24
+#define NETLINK_MSG_NOFREEZE_ACK 0x27
+#define NETLINK_MSG_IS_DEBUGGING 0x28
+
+struct user_helper_data {
+	int (*rcv_msg) (struct sk_buff *skb, struct nlmsghdr *nlh);
+	void (* not_ready) (void);
+	struct sock *nl;
+	u32 sock_seq;
+	pid_t pid;
+	char *comm;
+	char program[256];
+	int pool_level;
+	int pool_limit;
+	struct sk_buff *emerg_skbs;
+	int skb_size;
+	int netlink_id;	
+	char *name;
+	struct user_helper_data *next;
+	struct completion wait_for_process;
+	int interface_version;
+	int must_init;
+};
+
+
+#ifdef CONFIG_NET
+int suspend_netlink_setup(struct user_helper_data *uhd);
+void suspend_netlink_close(struct user_helper_data *uhd);
+void suspend_send_netlink_message(struct user_helper_data *uhd,
+		int type, void* params, size_t len);
+#else
+static inline int suspend_netlink_setup(struct user_helper_data *uhd)
+{
+	return 0;
+}
+
+static inline void suspend_netlink_close(struct user_helper_data *uhd) { };
+static inline void suspend_send_netlink_message(struct user_helper_data *uhd,
+		int type, void* params, size_t len) { };
+#endif

--
Nigel Cunningham		nigel at suspend2 dot net

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2006-06-27  5:07 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-27  4:39 [Suspend2][ 00/13] Generic netlink support Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 01/13] [Suspend2] Netlink.c header Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 02/13] [Suspend2] Fill skb pool Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 03/13] [Suspend2] Get and put skbs Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 04/13] [Suspend2] Notify userspace of a new message Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 05/13] [Suspend2] Send netlink message to userspace helper Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 06/13] [Suspend2] Tell userui whether debugging is enabled Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 07/13] [Suspend2] Set NoFreeze flag on a userspace helper Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 08/13] [Suspend2] Userspace is ready! Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 09/13] [Suspend2] Netlink receive message routines Nigel Cunningham
2006-06-27  4:39 ` [Suspend2][ 10/13] [Suspend2] Prepare/cleanup netlink connections Nigel Cunningham
2006-06-27  4:40 ` [Suspend2][ 11/13] [Suspend2] Launch a userspace helper Nigel Cunningham
2006-06-27  4:40 ` [Suspend2][ 12/13] [Suspend2] Start " Nigel Cunningham
2006-06-27  4:40 ` [Suspend2][ 13/13] [Suspend2] Netlink header Nigel Cunningham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome