| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | kmod, the new module loader (replaces kerneld) | 
|  | 3 | Kirk Petersen | 
|  | 4 |  | 
|  | 5 | Reorganized not to be a daemon by Adam Richter, with guidance | 
|  | 6 | from Greg Zornetzer. | 
|  | 7 |  | 
|  | 8 | Modified to avoid chroot and file sharing problems. | 
|  | 9 | Mikael Pettersson | 
|  | 10 |  | 
|  | 11 | Limit the concurrent number of kmod modprobes to catch loops from | 
|  | 12 | "modprobe needs a service that is in a module". | 
|  | 13 | Keith Owens <kaos@ocs.com.au> December 1999 | 
|  | 14 |  | 
|  | 15 | Unblock all signals when we exec a usermode process. | 
|  | 16 | Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000 | 
|  | 17 |  | 
|  | 18 | call_usermodehelper wait flag, and remove exec_usermodehelper. | 
|  | 19 | Rusty Russell <rusty@rustcorp.com.au>  Jan 2003 | 
|  | 20 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> | 
|  | 22 | #include <linux/sched.h> | 
|  | 23 | #include <linux/syscalls.h> | 
|  | 24 | #include <linux/unistd.h> | 
|  | 25 | #include <linux/kmod.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> | 
| Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 27 | #include <linux/mnt_namespace.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/completion.h> | 
|  | 29 | #include <linux/file.h> | 
|  | 30 | #include <linux/workqueue.h> | 
|  | 31 | #include <linux/security.h> | 
|  | 32 | #include <linux/mount.h> | 
|  | 33 | #include <linux/kernel.h> | 
|  | 34 | #include <linux/init.h> | 
| Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 35 | #include <linux/resource.h> | 
| Rafael J. Wysocki | 8cdd493 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 36 | #include <linux/notifier.h> | 
|  | 37 | #include <linux/suspend.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> | 
|  | 39 |  | 
|  | 40 | extern int max_threads; | 
|  | 41 |  | 
|  | 42 | static struct workqueue_struct *khelper_wq; | 
|  | 43 |  | 
|  | 44 | #ifdef CONFIG_KMOD | 
|  | 45 |  | 
|  | 46 | /* | 
|  | 47 | modprobe_path is set via /proc/sys. | 
|  | 48 | */ | 
|  | 49 | char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe"; | 
|  | 50 |  | 
|  | 51 | /** | 
|  | 52 | * request_module - try to load a kernel module | 
|  | 53 | * @fmt:     printf style format string for the name of the module | 
|  | 54 | * @varargs: arguements as specified in the format string | 
|  | 55 | * | 
|  | 56 | * Load a module using the user mode module loader. The function returns | 
|  | 57 | * zero on success or a negative errno code on failure. Note that a | 
|  | 58 | * successful module load does not mean the module did not then unload | 
|  | 59 | * and exit on an error of its own. Callers must check that the service | 
|  | 60 | * they requested is now available not blindly invoke it. | 
|  | 61 | * | 
|  | 62 | * If module auto-loading support is disabled then this function | 
|  | 63 | * becomes a no-operation. | 
|  | 64 | */ | 
|  | 65 | int request_module(const char *fmt, ...) | 
|  | 66 | { | 
|  | 67 | va_list args; | 
|  | 68 | char module_name[MODULE_NAME_LEN]; | 
|  | 69 | unsigned int max_modprobes; | 
|  | 70 | int ret; | 
|  | 71 | char *argv[] = { modprobe_path, "-q", "--", module_name, NULL }; | 
|  | 72 | static char *envp[] = { "HOME=/", | 
|  | 73 | "TERM=linux", | 
|  | 74 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", | 
|  | 75 | NULL }; | 
|  | 76 | static atomic_t kmod_concurrent = ATOMIC_INIT(0); | 
|  | 77 | #define MAX_KMOD_CONCURRENT 50	/* Completely arbitrary value - KAO */ | 
|  | 78 | static int kmod_loop_msg; | 
|  | 79 |  | 
|  | 80 | va_start(args, fmt); | 
|  | 81 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | 
|  | 82 | va_end(args); | 
|  | 83 | if (ret >= MODULE_NAME_LEN) | 
|  | 84 | return -ENAMETOOLONG; | 
|  | 85 |  | 
|  | 86 | /* If modprobe needs a service that is in a module, we get a recursive | 
|  | 87 | * loop.  Limit the number of running kmod threads to max_threads/2 or | 
|  | 88 | * MAX_KMOD_CONCURRENT, whichever is the smaller.  A cleaner method | 
|  | 89 | * would be to run the parents of this process, counting how many times | 
|  | 90 | * kmod was invoked.  That would mean accessing the internals of the | 
|  | 91 | * process tables to get the command line, proc_pid_cmdline is static | 
|  | 92 | * and it is not worth changing the proc code just to handle this case. | 
|  | 93 | * KAO. | 
|  | 94 | * | 
|  | 95 | * "trace the ppid" is simple, but will fail if someone's | 
|  | 96 | * parent exits.  I think this is as good as it gets. --RR | 
|  | 97 | */ | 
|  | 98 | max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT); | 
|  | 99 | atomic_inc(&kmod_concurrent); | 
|  | 100 | if (atomic_read(&kmod_concurrent) > max_modprobes) { | 
|  | 101 | /* We may be blaming an innocent here, but unlikely */ | 
|  | 102 | if (kmod_loop_msg++ < 5) | 
|  | 103 | printk(KERN_ERR | 
|  | 104 | "request_module: runaway loop modprobe %s\n", | 
|  | 105 | module_name); | 
|  | 106 | atomic_dec(&kmod_concurrent); | 
|  | 107 | return -ENOMEM; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | ret = call_usermodehelper(modprobe_path, argv, envp, 1); | 
|  | 111 | atomic_dec(&kmod_concurrent); | 
|  | 112 | return ret; | 
|  | 113 | } | 
|  | 114 | EXPORT_SYMBOL(request_module); | 
|  | 115 | #endif /* CONFIG_KMOD */ | 
|  | 116 |  | 
|  | 117 | struct subprocess_info { | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 118 | struct work_struct work; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | struct completion *complete; | 
|  | 120 | char *path; | 
|  | 121 | char **argv; | 
|  | 122 | char **envp; | 
| David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 123 | struct key *ring; | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 124 | enum umh_wait wait; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | int retval; | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 126 | struct file *stdin; | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 127 | void (*cleanup)(char **argv, char **envp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; | 
|  | 129 |  | 
|  | 130 | /* | 
|  | 131 | * This is the task which runs the usermode application | 
|  | 132 | */ | 
|  | 133 | static int ____call_usermodehelper(void *data) | 
|  | 134 | { | 
|  | 135 | struct subprocess_info *sub_info = data; | 
| David Howells | 20e1129 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 136 | struct key *new_session, *old_session; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int retval; | 
|  | 138 |  | 
| David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 139 | /* Unblock all signals and set the session keyring. */ | 
| David Howells | 20e1129 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 140 | new_session = key_get(sub_info->ring); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | spin_lock_irq(¤t->sighand->siglock); | 
| David Howells | 20e1129 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 142 | old_session = __install_session_keyring(current, new_session); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | flush_signal_handlers(current, 1); | 
|  | 144 | sigemptyset(¤t->blocked); | 
|  | 145 | recalc_sigpending(); | 
|  | 146 | spin_unlock_irq(¤t->sighand->siglock); | 
|  | 147 |  | 
| David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 148 | key_put(old_session); | 
|  | 149 |  | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 150 | /* Install input pipe when needed */ | 
|  | 151 | if (sub_info->stdin) { | 
|  | 152 | struct files_struct *f = current->files; | 
|  | 153 | struct fdtable *fdt; | 
|  | 154 | /* no races because files should be private here */ | 
|  | 155 | sys_close(0); | 
|  | 156 | fd_install(0, sub_info->stdin); | 
|  | 157 | spin_lock(&f->file_lock); | 
|  | 158 | fdt = files_fdtable(f); | 
|  | 159 | FD_SET(0, fdt->open_fds); | 
|  | 160 | FD_CLR(0, fdt->close_on_exec); | 
|  | 161 | spin_unlock(&f->file_lock); | 
| Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 162 |  | 
|  | 163 | /* and disallow core files too */ | 
|  | 164 | current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0}; | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* We can run anywhere, unlike our parent keventd(). */ | 
|  | 168 | set_cpus_allowed(current, CPU_MASK_ALL); | 
|  | 169 |  | 
| Jan Engelhardt | b73a7e7 | 2007-05-08 00:28:24 -0700 | [diff] [blame] | 170 | /* | 
|  | 171 | * Our parent is keventd, which runs with elevated scheduling priority. | 
|  | 172 | * Avoid propagating that into the userspace child. | 
|  | 173 | */ | 
|  | 174 | set_user_nice(current, 0); | 
|  | 175 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | retval = -EPERM; | 
|  | 177 | if (current->fs->root) | 
| Arnd Bergmann | 6760856 | 2006-10-02 02:18:26 -0700 | [diff] [blame] | 178 | retval = kernel_execve(sub_info->path, | 
|  | 179 | sub_info->argv, sub_info->envp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 |  | 
|  | 181 | /* Exec failed? */ | 
|  | 182 | sub_info->retval = retval; | 
|  | 183 | do_exit(0); | 
|  | 184 | } | 
|  | 185 |  | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 186 | void call_usermodehelper_freeinfo(struct subprocess_info *info) | 
|  | 187 | { | 
|  | 188 | if (info->cleanup) | 
|  | 189 | (*info->cleanup)(info->argv, info->envp); | 
|  | 190 | kfree(info); | 
|  | 191 | } | 
|  | 192 | EXPORT_SYMBOL(call_usermodehelper_freeinfo); | 
|  | 193 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* Keventd can't block, but this (a child) can. */ | 
|  | 195 | static int wait_for_helper(void *data) | 
|  | 196 | { | 
|  | 197 | struct subprocess_info *sub_info = data; | 
|  | 198 | pid_t pid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
|  | 200 | /* Install a handler: if SIGCLD isn't handled sys_wait4 won't | 
|  | 201 | * populate the status, but will return -ECHILD. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | allow_signal(SIGCHLD); | 
|  | 203 |  | 
|  | 204 | pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD); | 
|  | 205 | if (pid < 0) { | 
|  | 206 | sub_info->retval = pid; | 
|  | 207 | } else { | 
| Björn Steinbrink | 111dbe0 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 208 | int ret; | 
|  | 209 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* | 
|  | 211 | * Normally it is bogus to call wait4() from in-kernel because | 
|  | 212 | * wait4() wants to write the exit code to a userspace address. | 
|  | 213 | * But wait_for_helper() always runs as keventd, and put_user() | 
|  | 214 | * to a kernel address works OK for kernel threads, due to their | 
|  | 215 | * having an mm_segment_t which spans the entire address space. | 
|  | 216 | * | 
|  | 217 | * Thus the __user pointer cast is valid here. | 
|  | 218 | */ | 
| Björn Steinbrink | 111dbe0 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 219 | sys_wait4(pid, (int __user *)&ret, 0, NULL); | 
|  | 220 |  | 
|  | 221 | /* | 
|  | 222 | * If ret is 0, either ____call_usermodehelper failed and the | 
|  | 223 | * real error code is already in sub_info->retval or | 
|  | 224 | * sub_info->retval is 0 anyway, so don't mess with it then. | 
|  | 225 | */ | 
|  | 226 | if (ret) | 
|  | 227 | sub_info->retval = ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 230 | if (sub_info->wait == UMH_NO_WAIT) | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 231 | call_usermodehelper_freeinfo(sub_info); | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 232 | else | 
|  | 233 | complete(sub_info->complete); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return 0; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | /* This is run by khelper thread  */ | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 238 | static void __call_usermodehelper(struct work_struct *work) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 240 | struct subprocess_info *sub_info = | 
|  | 241 | container_of(work, struct subprocess_info, work); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | pid_t pid; | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 243 | enum umh_wait wait = sub_info->wait; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | /* CLONE_VFORK: wait until the usermode helper has execve'd | 
|  | 246 | * successfully We need the data structures to stay around | 
|  | 247 | * until that is done.  */ | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 248 | if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | pid = kernel_thread(wait_for_helper, sub_info, | 
|  | 250 | CLONE_FS | CLONE_FILES | SIGCHLD); | 
|  | 251 | else | 
|  | 252 | pid = kernel_thread(____call_usermodehelper, sub_info, | 
|  | 253 | CLONE_VFORK | SIGCHLD); | 
|  | 254 |  | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 255 | switch (wait) { | 
|  | 256 | case UMH_NO_WAIT: | 
|  | 257 | break; | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 258 |  | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 259 | case UMH_WAIT_PROC: | 
|  | 260 | if (pid > 0) | 
|  | 261 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | sub_info->retval = pid; | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 263 | /* FALLTHROUGH */ | 
|  | 264 |  | 
|  | 265 | case UMH_WAIT_EXEC: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | complete(sub_info->complete); | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 267 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 270 | #ifdef CONFIG_PM | 
|  | 271 | /* | 
|  | 272 | * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY | 
|  | 273 | * (used for preventing user land processes from being created after the user | 
|  | 274 | * land has been frozen during a system-wide hibernation or suspend operation). | 
|  | 275 | */ | 
|  | 276 | static int usermodehelper_disabled; | 
|  | 277 |  | 
|  | 278 | /* Number of helpers running */ | 
|  | 279 | static atomic_t running_helpers = ATOMIC_INIT(0); | 
|  | 280 |  | 
|  | 281 | /* | 
|  | 282 | * Wait queue head used by usermodehelper_pm_callback() to wait for all running | 
|  | 283 | * helpers to finish. | 
|  | 284 | */ | 
|  | 285 | static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq); | 
|  | 286 |  | 
|  | 287 | /* | 
|  | 288 | * Time to wait for running_helpers to become zero before the setting of | 
|  | 289 | * usermodehelper_disabled in usermodehelper_pm_callback() fails | 
|  | 290 | */ | 
|  | 291 | #define RUNNING_HELPERS_TIMEOUT	(5 * HZ) | 
|  | 292 |  | 
| Rafael J. Wysocki | 8cdd493 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 293 | static int usermodehelper_pm_callback(struct notifier_block *nfb, | 
|  | 294 | unsigned long action, | 
|  | 295 | void *ignored) | 
|  | 296 | { | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 297 | long retval; | 
|  | 298 |  | 
| Rafael J. Wysocki | 8cdd493 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 299 | switch (action) { | 
|  | 300 | case PM_HIBERNATION_PREPARE: | 
|  | 301 | case PM_SUSPEND_PREPARE: | 
|  | 302 | usermodehelper_disabled = 1; | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 303 | smp_mb(); | 
|  | 304 | /* | 
|  | 305 | * From now on call_usermodehelper_exec() won't start any new | 
|  | 306 | * helpers, so it is sufficient if running_helpers turns out to | 
|  | 307 | * be zero at one point (it may be increased later, but that | 
|  | 308 | * doesn't matter). | 
|  | 309 | */ | 
|  | 310 | retval = wait_event_timeout(running_helpers_waitq, | 
|  | 311 | atomic_read(&running_helpers) == 0, | 
|  | 312 | RUNNING_HELPERS_TIMEOUT); | 
|  | 313 | if (retval) { | 
|  | 314 | return NOTIFY_OK; | 
|  | 315 | } else { | 
|  | 316 | usermodehelper_disabled = 0; | 
|  | 317 | return NOTIFY_BAD; | 
|  | 318 | } | 
| Rafael J. Wysocki | 8cdd493 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 319 | case PM_POST_HIBERNATION: | 
|  | 320 | case PM_POST_SUSPEND: | 
|  | 321 | usermodehelper_disabled = 0; | 
|  | 322 | return NOTIFY_OK; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | return NOTIFY_DONE; | 
|  | 326 | } | 
|  | 327 |  | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 328 | static void helper_lock(void) | 
|  | 329 | { | 
|  | 330 | atomic_inc(&running_helpers); | 
|  | 331 | smp_mb__after_atomic_inc(); | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | static void helper_unlock(void) | 
|  | 335 | { | 
|  | 336 | if (atomic_dec_and_test(&running_helpers)) | 
|  | 337 | wake_up(&running_helpers_waitq); | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | static void register_pm_notifier_callback(void) | 
|  | 341 | { | 
|  | 342 | pm_notifier(usermodehelper_pm_callback, 0); | 
|  | 343 | } | 
|  | 344 | #else /* CONFIG_PM */ | 
|  | 345 | #define usermodehelper_disabled	0 | 
|  | 346 |  | 
|  | 347 | static inline void helper_lock(void) {} | 
|  | 348 | static inline void helper_unlock(void) {} | 
|  | 349 | static inline void register_pm_notifier_callback(void) {} | 
|  | 350 | #endif /* CONFIG_PM */ | 
|  | 351 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /** | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 353 | * call_usermodehelper_setup - prepare to call a usermode helper | 
| Randy Dunlap | 61df47c | 2007-07-26 10:40:56 -0700 | [diff] [blame] | 354 | * @path: path to usermode executable | 
|  | 355 | * @argv: arg vector for process | 
|  | 356 | * @envp: environment for process | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | * | 
| Randy Dunlap | 61df47c | 2007-07-26 10:40:56 -0700 | [diff] [blame] | 358 | * Returns either %NULL on allocation failure, or a subprocess_info | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 359 | * structure.  This should be passed to call_usermodehelper_exec to | 
|  | 360 | * exec the process and free the structure. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | */ | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 362 | struct subprocess_info *call_usermodehelper_setup(char *path, | 
|  | 363 | char **argv, char **envp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 365 | struct subprocess_info *sub_info; | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 366 | sub_info = kzalloc(sizeof(struct subprocess_info),  GFP_ATOMIC); | 
|  | 367 | if (!sub_info) | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 368 | goto out; | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 369 |  | 
|  | 370 | INIT_WORK(&sub_info->work, __call_usermodehelper); | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 371 | sub_info->path = path; | 
|  | 372 | sub_info->argv = argv; | 
|  | 373 | sub_info->envp = envp; | 
| Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 374 |  | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 375 | out: | 
|  | 376 | return sub_info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 378 | EXPORT_SYMBOL(call_usermodehelper_setup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 |  | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 380 | /** | 
|  | 381 | * call_usermodehelper_setkeys - set the session keys for usermode helper | 
|  | 382 | * @info: a subprocess_info returned by call_usermodehelper_setup | 
|  | 383 | * @session_keyring: the session keyring for the process | 
|  | 384 | */ | 
|  | 385 | void call_usermodehelper_setkeys(struct subprocess_info *info, | 
|  | 386 | struct key *session_keyring) | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 387 | { | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 388 | info->ring = session_keyring; | 
|  | 389 | } | 
|  | 390 | EXPORT_SYMBOL(call_usermodehelper_setkeys); | 
|  | 391 |  | 
|  | 392 | /** | 
|  | 393 | * call_usermodehelper_setcleanup - set a cleanup function | 
|  | 394 | * @info: a subprocess_info returned by call_usermodehelper_setup | 
|  | 395 | * @cleanup: a cleanup function | 
|  | 396 | * | 
|  | 397 | * The cleanup function is just befor ethe subprocess_info is about to | 
|  | 398 | * be freed.  This can be used for freeing the argv and envp.  The | 
|  | 399 | * Function must be runnable in either a process context or the | 
|  | 400 | * context in which call_usermodehelper_exec is called. | 
|  | 401 | */ | 
|  | 402 | void call_usermodehelper_setcleanup(struct subprocess_info *info, | 
|  | 403 | void (*cleanup)(char **argv, char **envp)) | 
|  | 404 | { | 
|  | 405 | info->cleanup = cleanup; | 
|  | 406 | } | 
|  | 407 | EXPORT_SYMBOL(call_usermodehelper_setcleanup); | 
|  | 408 |  | 
|  | 409 | /** | 
|  | 410 | * call_usermodehelper_stdinpipe - set up a pipe to be used for stdin | 
|  | 411 | * @sub_info: a subprocess_info returned by call_usermodehelper_setup | 
|  | 412 | * @filp: set to the write-end of a pipe | 
|  | 413 | * | 
|  | 414 | * This constructs a pipe, and sets the read end to be the stdin of the | 
|  | 415 | * subprocess, and returns the write-end in *@filp. | 
|  | 416 | */ | 
|  | 417 | int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info, | 
|  | 418 | struct file **filp) | 
|  | 419 | { | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 420 | struct file *f; | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 421 |  | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 422 | f = create_write_pipe(); | 
| Akinobu Mita | 3cce485 | 2006-11-28 12:29:43 -0800 | [diff] [blame] | 423 | if (IS_ERR(f)) | 
|  | 424 | return PTR_ERR(f); | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 425 | *filp = f; | 
|  | 426 |  | 
|  | 427 | f = create_read_pipe(f); | 
| Akinobu Mita | 3cce485 | 2006-11-28 12:29:43 -0800 | [diff] [blame] | 428 | if (IS_ERR(f)) { | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 429 | free_write_pipe(*filp); | 
| Akinobu Mita | 3cce485 | 2006-11-28 12:29:43 -0800 | [diff] [blame] | 430 | return PTR_ERR(f); | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 431 | } | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 432 | sub_info->stdin = f; | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 433 |  | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 434 | return 0; | 
|  | 435 | } | 
|  | 436 | EXPORT_SYMBOL(call_usermodehelper_stdinpipe); | 
|  | 437 |  | 
|  | 438 | /** | 
|  | 439 | * call_usermodehelper_exec - start a usermode application | 
|  | 440 | * @sub_info: information about the subprocessa | 
|  | 441 | * @wait: wait for the application to finish and return status. | 
|  | 442 | *        when -1 don't wait at all, but you get no useful error back when | 
|  | 443 | *        the program couldn't be exec'ed. This makes it safe to call | 
|  | 444 | *        from interrupt context. | 
|  | 445 | * | 
|  | 446 | * Runs a user-space application.  The application is started | 
|  | 447 | * asynchronously if wait is not set, and runs as a child of keventd. | 
|  | 448 | * (ie. it runs with full root capabilities). | 
|  | 449 | */ | 
|  | 450 | int call_usermodehelper_exec(struct subprocess_info *sub_info, | 
| Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 451 | enum umh_wait wait) | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 452 | { | 
|  | 453 | DECLARE_COMPLETION_ONSTACK(done); | 
| Nigel Cunningham | 7846803 | 2008-01-17 15:21:21 -0800 | [diff] [blame] | 454 | int retval = 0; | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 455 |  | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 456 | helper_lock(); | 
| Nigel Cunningham | 7846803 | 2008-01-17 15:21:21 -0800 | [diff] [blame] | 457 | if (sub_info->path[0] == '\0') | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 458 | goto out; | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 459 |  | 
| Rafael J. Wysocki | 8cdd493 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 460 | if (!khelper_wq || usermodehelper_disabled) { | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 461 | retval = -EBUSY; | 
|  | 462 | goto out; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | sub_info->complete = &done; | 
|  | 466 | sub_info->wait = wait; | 
|  | 467 |  | 
|  | 468 | queue_work(khelper_wq, &sub_info->work); | 
| Nigel Cunningham | 7846803 | 2008-01-17 15:21:21 -0800 | [diff] [blame] | 469 | if (wait == UMH_NO_WAIT)	/* task has freed sub_info */ | 
|  | 470 | goto unlock; | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 471 | wait_for_completion(&done); | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 472 | retval = sub_info->retval; | 
|  | 473 |  | 
| Nigel Cunningham | 7846803 | 2008-01-17 15:21:21 -0800 | [diff] [blame] | 474 | out: | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 475 | call_usermodehelper_freeinfo(sub_info); | 
| Nigel Cunningham | 7846803 | 2008-01-17 15:21:21 -0800 | [diff] [blame] | 476 | unlock: | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 477 | helper_unlock(); | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 478 | return retval; | 
|  | 479 | } | 
|  | 480 | EXPORT_SYMBOL(call_usermodehelper_exec); | 
|  | 481 |  | 
|  | 482 | /** | 
|  | 483 | * call_usermodehelper_pipe - call a usermode helper process with a pipe stdin | 
|  | 484 | * @path: path to usermode executable | 
|  | 485 | * @argv: arg vector for process | 
|  | 486 | * @envp: environment for process | 
|  | 487 | * @filp: set to the write-end of a pipe | 
|  | 488 | * | 
|  | 489 | * This is a simple wrapper which executes a usermode-helper function | 
|  | 490 | * with a pipe as stdin.  It is implemented entirely in terms of | 
|  | 491 | * lower-level call_usermodehelper_* functions. | 
|  | 492 | */ | 
|  | 493 | int call_usermodehelper_pipe(char *path, char **argv, char **envp, | 
|  | 494 | struct file **filp) | 
|  | 495 | { | 
|  | 496 | struct subprocess_info *sub_info; | 
|  | 497 | int ret; | 
|  | 498 |  | 
|  | 499 | sub_info = call_usermodehelper_setup(path, argv, envp); | 
|  | 500 | if (sub_info == NULL) | 
|  | 501 | return -ENOMEM; | 
|  | 502 |  | 
|  | 503 | ret = call_usermodehelper_stdinpipe(sub_info, filp); | 
|  | 504 | if (ret < 0) | 
|  | 505 | goto out; | 
|  | 506 |  | 
| Michael Ellerman | 3210f0e | 2007-09-11 15:23:51 -0700 | [diff] [blame] | 507 | return call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC); | 
| Jeremy Fitzhardinge | 0ab4dc9 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 508 |  | 
|  | 509 | out: | 
|  | 510 | call_usermodehelper_freeinfo(sub_info); | 
|  | 511 | return ret; | 
| Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 512 | } | 
|  | 513 | EXPORT_SYMBOL(call_usermodehelper_pipe); | 
|  | 514 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | void __init usermodehelper_init(void) | 
|  | 516 | { | 
|  | 517 | khelper_wq = create_singlethread_workqueue("khelper"); | 
|  | 518 | BUG_ON(!khelper_wq); | 
| Rafael J. Wysocki | ccd4b65 | 2007-07-19 01:47:37 -0700 | [diff] [blame] | 519 | register_pm_notifier_callback(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |