Generic netlink support.
Subsystems (2)
cover Generic netlink support.
Routines used by both the user interface code and storage manager to interface with userspace programs through netlink sockets.
PATCH 1/13 Netlink.c header
kernel/power/netlink.c implements the interface between Suspend2 and userspace helpers (the userui and storage manager).
kernel/power/netlink.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
kernel/power/netlink.c new file +17 -0
@@ -0,0 +1,17 @@ +/* +* kernel/power/netlink.c +* +* Copyright (C) 2004-2006 Nigel Cunningham <[email protected]> +* +* 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
PATCH 2/13 Fill skb pool.
In reply to Nigel CunninghamFill the skb pool used to communicate with userspace helpers.
kernel/power/netlink.c | 19 +++++++++++++++++++ 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/power/netlink.c +19 -0
@@ -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
PATCH 3/13 Get and put skbs.
In reply to Nigel CunninghamGet and put skbs used to communicate with userspace helpers.
kernel/power/netlink.c | 31 +++++++++++++++++++++++++++++++ 1 files changed, 31 insertions(+), 0 deletions(-)
kernel/power/netlink.c +31 -0
@@ -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
PATCH 4/13 Notify userspace of a new message.
In reply to Nigel CunninghamWake up a userspace process when we have a new netlink message for it to process.
kernel/power/netlink.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
kernel/power/netlink.c +14 -0
@@ -65,3 +65,17 @@ static void put_skb(struct user_helper_dkfree_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
PATCH 5/13 Send netlink message to userspace helper.
In reply to Nigel CunninghamSend a netlink message to a userspace helper.
kernel/power/netlink.c | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-)
kernel/power/netlink.c +36 -0
@@ -79,3 +79,39 @@ static void suspend_notify_userspace(voiread_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
PATCH 6/13 Tell userui whether debugging is enabled.
In reply to Nigel CunninghamSend a netlink message to the userspace ui component (if any), notifying it as to whether debugging support is enabled.
kernel/power/netlink.c | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-)
kernel/power/netlink.c +12 -0
@@ -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
PATCH 7/13 Set NoFreeze flag on a userspace helper.
In reply to Nigel CunninghamSet the NoFreeze flag on a userspace helper (userui or storage manager) so that it can continue to function while we're doing I/O.
kernel/power/netlink.c | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-)
kernel/power/netlink.c +25 -0
@@ -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
PATCH 8/13 Userspace is ready!
In reply to Nigel CunninghamRoutine 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.
kernel/power/netlink.c | 19 +++++++++++++++++++ 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/power/netlink.c +19 -0
@@ -152,3 +152,22 @@ static int nl_set_nofreeze(struct user_hreturn 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
PATCH 9/13 Netlink receive message routines.
In reply to Nigel CunninghamSuspend2's routines for receiving netlink messages. We don't accept that many messages, so the logic is pretty simple.
kernel/power/netlink.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 86 insertions(+), 0 deletions(-)
kernel/power/netlink.c +86 -0
@@ -171,3 +171,89 @@ static int nl_ready(struct user_helper_dreturn 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
PATCH 10/13 Prepare/cleanup netlink connections.
In reply to Nigel CunninghamPrepare to communicate with a userspace helper over a netlink socket and cleanup afterwards.
kernel/power/netlink.c | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-)
kernel/power/netlink.c +33 -0
@@ -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
PATCH 11/13 Launch a userspace helper.
In reply to Nigel CunninghamLaunch 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.
kernel/power/netlink.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 54 insertions(+), 0 deletions(-)
kernel/power/netlink.c +54 -0
@@ -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
PATCH 12/13 Start a userspace helper.
In reply to Nigel CunninghamAttempt to start a userspace helper and set up the netlink socket connection to it.
kernel/power/netlink.c | 26 ++++++++++++++++++++++++++ 1 files changed, 26 insertions(+), 0 deletions(-)
kernel/power/netlink.c +26 -0
@@ -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
PATCH 13/13 Netlink header.
In reply to Nigel CunninghamHeader file describing userspace helper data, generic message numbers and exported functions.
kernel/power/netlink.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 59 insertions(+), 0 deletions(-)
kernel/power/netlink.h new file +59 -0
@@ -0,0 +1,59 @@ +/* +* kernel/power/netlink.h +* +* Copyright (C) 2004-2006 Nigel Cunningham <[email protected]> +* +* 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