fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / kernel / kmod.c
index 579269c..3a7379a 100644 (file)
@@ -18,9 +18,6 @@
        call_usermodehelper wait flag, and remove exec_usermodehelper.
        Rusty Russell <rusty@rustcorp.com.au>  Jan 2003
 */
-#define __KERNEL_SYSCALLS__
-
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/sched.h>
 #include <linux/syscalls.h>
@@ -28,7 +25,7 @@
 #include <linux/kmod.h>
 #include <linux/smp_lock.h>
 #include <linux/slab.h>
-#include <linux/namespace.h>
+#include <linux/mnt_namespace.h>
 #include <linux/completion.h>
 #include <linux/file.h>
 #include <linux/workqueue.h>
@@ -36,6 +33,7 @@
 #include <linux/mount.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
+#include <linux/resource.h>
 #include <asm/uaccess.h>
 
 extern int max_threads;
@@ -115,36 +113,16 @@ int request_module(const char *fmt, ...)
 EXPORT_SYMBOL(request_module);
 #endif /* CONFIG_KMOD */
 
-#ifdef CONFIG_HOTPLUG
-/*
-       hotplug path is set via /proc/sys
-       invoked by hotplug-aware bus drivers,
-       with call_usermodehelper
-
-       argv [0] = hotplug_path;
-       argv [1] = "usb", "scsi", "pci", "network", etc;
-       ... plus optional type-specific parameters
-       argv [n] = 0;
-
-       envp [*] = HOME, PATH; optional type-specific parameters
-
-       a hotplug bus should invoke this for device add/remove
-       events.  the command is expected to load drivers when
-       necessary, and may perform additional system setup.
-*/
-char hotplug_path[KMOD_PATH_LEN] = "/sbin/hotplug";
-
-EXPORT_SYMBOL(hotplug_path);
-
-#endif /* CONFIG_HOTPLUG */
-
 struct subprocess_info {
+       struct work_struct work;
        struct completion *complete;
        char *path;
        char **argv;
        char **envp;
+       struct key *ring;
        int wait;
        int retval;
+       struct file *stdin;
 };
 
 /*
@@ -153,22 +131,45 @@ struct subprocess_info {
 static int ____call_usermodehelper(void *data)
 {
        struct subprocess_info *sub_info = data;
+       struct key *new_session, *old_session;
        int retval;
 
-       /* Unblock all signals. */
+       /* 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);
        sigemptyset(&current->blocked);
        recalc_sigpending();
        spin_unlock_irq(&current->sighand->siglock);
 
+       key_put(old_session);
+
+       /* Install input pipe when needed */
+       if (sub_info->stdin) {
+               struct files_struct *f = current->files;
+               struct fdtable *fdt;
+               /* no races because files should be private here */
+               sys_close(0);
+               fd_install(0, sub_info->stdin);
+               spin_lock(&f->file_lock);
+               fdt = files_fdtable(f);
+               FD_SET(0, fdt->open_fds);
+               FD_CLR(0, fdt->close_on_exec);
+               spin_unlock(&f->file_lock);
+
+               /* and disallow core files too */
+               current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
+       }
+
        /* We can run anywhere, unlike our parent keventd(). */
        set_cpus_allowed(current, CPU_MASK_ALL);
 
        retval = -EPERM;
        if (current->fs->root)
-               retval = execve(sub_info->path, sub_info->argv,sub_info->envp);
+               retval = kernel_execve(sub_info->path,
+                               sub_info->argv, sub_info->envp);
 
        /* Exec failed? */
        sub_info->retval = retval;
@@ -187,13 +188,15 @@ static int wait_for_helper(void *data)
        sa.sa.sa_handler = SIG_IGN;
        sa.sa.sa_flags = 0;
        siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
-       do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
+       do_sigaction(SIGCHLD, &sa, NULL);
        allow_signal(SIGCHLD);
 
        pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
        if (pid < 0) {
                sub_info->retval = pid;
        } else {
+               int ret;
+
                /*
                 * Normally it is bogus to call wait4() from in-kernel because
                 * wait4() wants to write the exit code to a userspace address.
@@ -203,7 +206,15 @@ static int wait_for_helper(void *data)
                 *
                 * Thus the __user pointer cast is valid here.
                 */
-               sys_wait4(pid, (int __user *) &sub_info->retval, 0, NULL);
+               sys_wait4(pid, (int __user *)&ret, 0, NULL);
+
+               /*
+                * If ret is 0, either ____call_usermodehelper failed and the
+                * real error code is already in sub_info->retval or
+                * sub_info->retval is 0 anyway, so don't mess with it then.
+                */
+               if (ret)
+                       sub_info->retval = ret;
        }
 
        complete(sub_info->complete);
@@ -211,15 +222,17 @@ static int wait_for_helper(void *data)
 }
 
 /* This is run by khelper thread  */
-static void __call_usermodehelper(void *data)
+static void __call_usermodehelper(struct work_struct *work)
 {
-       struct subprocess_info *sub_info = data;
+       struct subprocess_info *sub_info =
+               container_of(work, struct subprocess_info, work);
        pid_t pid;
+       int wait = sub_info->wait;
 
        /* CLONE_VFORK: wait until the usermode helper has execve'd
         * successfully We need the data structures to stay around
         * until that is done.  */
-       if (sub_info->wait)
+       if (wait)
                pid = kernel_thread(wait_for_helper, sub_info,
                                    CLONE_FS | CLONE_FILES | SIGCHLD);
        else
@@ -229,15 +242,16 @@ static void __call_usermodehelper(void *data)
        if (pid < 0) {
                sub_info->retval = pid;
                complete(sub_info->complete);
-       } else if (!sub_info->wait)
+       } else if (!wait)
                complete(sub_info->complete);
 }
 
 /**
- * call_usermodehelper - start a usermode application
+ * call_usermodehelper_keys - start a usermode application
  * @path: pathname for the application
  * @argv: null-terminated argument list
  * @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.
  *
  * Runs a user-space application.  The application is started
@@ -247,18 +261,21 @@ static void __call_usermodehelper(void *data)
  * Must be called from process context.  Returns a negative error code
  * if program was not execed successfully, or 0.
  */
-int call_usermodehelper(char *path, char **argv, char **envp, int wait)
+int call_usermodehelper_keys(char *path, char **argv, char **envp,
+                            struct key *session_keyring, int wait)
 {
-       DECLARE_COMPLETION(done);
+       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,
        };
-       DECLARE_WORK(work, __call_usermodehelper, &sub_info);
 
        if (!khelper_wq)
                return -EBUSY;
@@ -266,16 +283,53 @@ int call_usermodehelper(char *path, char **argv, char **envp, int wait)
        if (path[0] == '\0')
                return 0;
 
-       queue_work(khelper_wq, &work);
+       queue_work(khelper_wq, &sub_info.work);
        wait_for_completion(&done);
        return sub_info.retval;
 }
-EXPORT_SYMBOL(call_usermodehelper);
+EXPORT_SYMBOL(call_usermodehelper_keys);
 
-static __init int usermodehelper_init(void)
+int call_usermodehelper_pipe(char *path, char **argv, char **envp,
+                            struct file **filp)
+{
+       DECLARE_COMPLETION(done);
+       struct subprocess_info sub_info = {
+               .work           = __WORK_INITIALIZER(sub_info.work,
+                                                    __call_usermodehelper),
+               .complete       = &done,
+               .path           = path,
+               .argv           = argv,
+               .envp           = envp,
+               .retval         = 0,
+       };
+       struct file *f;
+
+       if (!khelper_wq)
+               return -EBUSY;
+
+       if (path[0] == '\0')
+               return 0;
+
+       f = create_write_pipe();
+       if (IS_ERR(f))
+               return PTR_ERR(f);
+       *filp = f;
+
+       f = create_read_pipe(f);
+       if (IS_ERR(f)) {
+               free_write_pipe(*filp);
+               return PTR_ERR(f);
+       }
+       sub_info.stdin = f;
+
+       queue_work(khelper_wq, &sub_info.work);
+       wait_for_completion(&done);
+       return sub_info.retval;
+}
+EXPORT_SYMBOL(call_usermodehelper_pipe);
+
+void __init usermodehelper_init(void)
 {
        khelper_wq = create_singlethread_workqueue("khelper");
        BUG_ON(!khelper_wq);
-       return 0;
 }
-core_initcall(usermodehelper_init);