wait_for_helper: remove unneeded do_sigaction()
[sfrench/cifs-2.6.git] / kernel / kmod.c
index 3a7379aa31ca6b0a92bec377a23e5c9debed72db..4d32eb077179a2babd3f49b9f31631d4c6e27bb7 100644 (file)
@@ -23,7 +23,6 @@
 #include <linux/syscalls.h>
 #include <linux/unistd.h>
 #include <linux/kmod.h>
-#include <linux/smp_lock.h>
 #include <linux/slab.h>
 #include <linux/mnt_namespace.h>
 #include <linux/completion.h>
@@ -136,7 +135,6 @@ static int ____call_usermodehelper(void *data)
 
        /* Unblock all signals and set the session keyring. */
        new_session = key_get(sub_info->ring);
-       flush_signals(current);
        spin_lock_irq(&current->sighand->siglock);
        old_session = __install_session_keyring(current, new_session);
        flush_signal_handlers(current, 1);
@@ -166,6 +164,12 @@ static int ____call_usermodehelper(void *data)
        /* We can run anywhere, unlike our parent keventd(). */
        set_cpus_allowed(current, CPU_MASK_ALL);
 
+       /*
+        * Our parent is keventd, which runs with elevated scheduling priority.
+        * Avoid propagating that into the userspace child.
+        */
+       set_user_nice(current, 0);
+
        retval = -EPERM;
        if (current->fs->root)
                retval = kernel_execve(sub_info->path,
@@ -181,14 +185,9 @@ static int wait_for_helper(void *data)
 {
        struct subprocess_info *sub_info = data;
        pid_t pid;
-       struct k_sigaction sa;
 
        /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
         * populate the status, but will return -ECHILD. */
-       sa.sa.sa_handler = SIG_IGN;
-       sa.sa.sa_flags = 0;
-       siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
-       do_sigaction(SIGCHLD, &sa, NULL);
        allow_signal(SIGCHLD);
 
        pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
@@ -217,7 +216,10 @@ static int wait_for_helper(void *data)
                        sub_info->retval = ret;
        }
 
-       complete(sub_info->complete);
+       if (sub_info->wait < 0)
+               kfree(sub_info);
+       else
+               complete(sub_info->complete);
        return 0;
 }
 
@@ -239,6 +241,9 @@ static void __call_usermodehelper(struct work_struct *work)
                pid = kernel_thread(____call_usermodehelper, sub_info,
                                    CLONE_VFORK | SIGCHLD);
 
+       if (wait < 0)
+               return;
+
        if (pid < 0) {
                sub_info->retval = pid;
                complete(sub_info->complete);
@@ -253,6 +258,9 @@ static void __call_usermodehelper(struct work_struct *work)
  * @envp: null-terminated environment list
  * @session_keyring: session keyring for process (NULL for an empty keyring)
  * @wait: wait for the application to finish and return status.
+ *        when -1 don't wait at all, but you get no useful error back when
+ *        the program couldn't be exec'ed. This makes it safe to call
+ *        from interrupt context.
  *
  * Runs a user-space application.  The application is started
  * asynchronously if wait is not set, and runs as a child of keventd.
@@ -265,17 +273,8 @@ int call_usermodehelper_keys(char *path, char **argv, char **envp,
                             struct key *session_keyring, int wait)
 {
        DECLARE_COMPLETION_ONSTACK(done);
-       struct subprocess_info sub_info = {
-               .work           = __WORK_INITIALIZER(sub_info.work,
-                                                    __call_usermodehelper),
-               .complete       = &done,
-               .path           = path,
-               .argv           = argv,
-               .envp           = envp,
-               .ring           = session_keyring,
-               .wait           = wait,
-               .retval         = 0,
-       };
+       struct subprocess_info *sub_info;
+       int retval;
 
        if (!khelper_wq)
                return -EBUSY;
@@ -283,9 +282,25 @@ int call_usermodehelper_keys(char *path, char **argv, char **envp,
        if (path[0] == '\0')
                return 0;
 
-       queue_work(khelper_wq, &sub_info.work);
+       sub_info = kzalloc(sizeof(struct subprocess_info),  GFP_ATOMIC);
+       if (!sub_info)
+               return -ENOMEM;
+
+       INIT_WORK(&sub_info->work, __call_usermodehelper);
+       sub_info->complete = &done;
+       sub_info->path = path;
+       sub_info->argv = argv;
+       sub_info->envp = envp;
+       sub_info->ring = session_keyring;
+       sub_info->wait = wait;
+
+       queue_work(khelper_wq, &sub_info->work);
+       if (wait < 0) /* task has freed sub_info */
+               return 0;
        wait_for_completion(&done);
-       return sub_info.retval;
+       retval = sub_info->retval;
+       kfree(sub_info);
+       return retval;
 }
 EXPORT_SYMBOL(call_usermodehelper_keys);