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> |
| 26 | #include <linux/smp_lock.h> |
| 27 | #include <linux/slab.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 28 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/completion.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/workqueue.h> |
| 32 | #include <linux/security.h> |
| 33 | #include <linux/mount.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/init.h> |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 36 | #include <linux/resource.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | |
Kay Sievers | c353c3f | 2007-02-02 16:39:12 +0100 | [diff] [blame] | 39 | extern int delete_module(const char *name, unsigned int flags); |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | extern int max_threads; |
| 42 | |
| 43 | static struct workqueue_struct *khelper_wq; |
| 44 | |
| 45 | #ifdef CONFIG_KMOD |
| 46 | |
| 47 | /* |
| 48 | modprobe_path is set via /proc/sys. |
| 49 | */ |
| 50 | char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe"; |
Kay Sievers | c353c3f | 2007-02-02 16:39:12 +0100 | [diff] [blame] | 51 | struct module_kobject kmod_mk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * request_module - try to load a kernel module |
| 55 | * @fmt: printf style format string for the name of the module |
| 56 | * @varargs: arguements as specified in the format string |
| 57 | * |
| 58 | * Load a module using the user mode module loader. The function returns |
| 59 | * zero on success or a negative errno code on failure. Note that a |
| 60 | * successful module load does not mean the module did not then unload |
| 61 | * and exit on an error of its own. Callers must check that the service |
| 62 | * they requested is now available not blindly invoke it. |
| 63 | * |
| 64 | * If module auto-loading support is disabled then this function |
| 65 | * becomes a no-operation. |
| 66 | */ |
| 67 | int request_module(const char *fmt, ...) |
| 68 | { |
| 69 | va_list args; |
| 70 | char module_name[MODULE_NAME_LEN]; |
| 71 | unsigned int max_modprobes; |
| 72 | int ret; |
| 73 | char *argv[] = { modprobe_path, "-q", "--", module_name, NULL }; |
| 74 | static char *envp[] = { "HOME=/", |
| 75 | "TERM=linux", |
| 76 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 77 | NULL }; |
| 78 | static atomic_t kmod_concurrent = ATOMIC_INIT(0); |
| 79 | #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */ |
| 80 | static int kmod_loop_msg; |
Kay Sievers | c353c3f | 2007-02-02 16:39:12 +0100 | [diff] [blame] | 81 | char modalias[16 + MODULE_NAME_LEN] = "MODALIAS="; |
| 82 | char *uevent_envp[2] = { |
| 83 | modalias, |
| 84 | NULL |
| 85 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | va_start(args, fmt); |
| 88 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); |
| 89 | va_end(args); |
| 90 | if (ret >= MODULE_NAME_LEN) |
| 91 | return -ENAMETOOLONG; |
| 92 | |
Kay Sievers | c353c3f | 2007-02-02 16:39:12 +0100 | [diff] [blame] | 93 | strcpy(&modalias[strlen("MODALIAS=")], module_name); |
| 94 | kobject_uevent_env(&kmod_mk.kobj, KOBJ_CHANGE, uevent_envp); |
| 95 | |
| 96 | if (modprobe_path[0] == '\0') |
| 97 | goto out; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* If modprobe needs a service that is in a module, we get a recursive |
| 100 | * loop. Limit the number of running kmod threads to max_threads/2 or |
| 101 | * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method |
| 102 | * would be to run the parents of this process, counting how many times |
| 103 | * kmod was invoked. That would mean accessing the internals of the |
| 104 | * process tables to get the command line, proc_pid_cmdline is static |
| 105 | * and it is not worth changing the proc code just to handle this case. |
| 106 | * KAO. |
| 107 | * |
| 108 | * "trace the ppid" is simple, but will fail if someone's |
| 109 | * parent exits. I think this is as good as it gets. --RR |
| 110 | */ |
| 111 | max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT); |
| 112 | atomic_inc(&kmod_concurrent); |
| 113 | if (atomic_read(&kmod_concurrent) > max_modprobes) { |
| 114 | /* We may be blaming an innocent here, but unlikely */ |
| 115 | if (kmod_loop_msg++ < 5) |
| 116 | printk(KERN_ERR |
| 117 | "request_module: runaway loop modprobe %s\n", |
| 118 | module_name); |
| 119 | atomic_dec(&kmod_concurrent); |
| 120 | return -ENOMEM; |
| 121 | } |
| 122 | |
| 123 | ret = call_usermodehelper(modprobe_path, argv, envp, 1); |
| 124 | atomic_dec(&kmod_concurrent); |
Kay Sievers | c353c3f | 2007-02-02 16:39:12 +0100 | [diff] [blame] | 125 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | EXPORT_SYMBOL(request_module); |
Kay Sievers | c353c3f | 2007-02-02 16:39:12 +0100 | [diff] [blame] | 129 | |
| 130 | static ssize_t store_mod_request(struct module_attribute *mattr, |
| 131 | struct module *mod, |
| 132 | const char *buffer, size_t count) |
| 133 | { |
| 134 | char name[MODULE_NAME_LEN]; |
| 135 | int ret; |
| 136 | |
| 137 | if (count < 1 || count+1 > MODULE_NAME_LEN) |
| 138 | return -EINVAL; |
| 139 | memcpy(name, buffer, count); |
| 140 | name[count] = '\0'; |
| 141 | if (name[count-1] == '\n') |
| 142 | name[count-1] = '\0'; |
| 143 | |
| 144 | ret = request_module(name); |
| 145 | if (ret < 0) |
| 146 | return ret; |
| 147 | return count; |
| 148 | } |
| 149 | |
| 150 | static struct module_attribute mod_request = { |
| 151 | .attr = { .name = "mod_request", .mode = S_IWUSR, .owner = THIS_MODULE }, |
| 152 | .store = store_mod_request, |
| 153 | }; |
| 154 | |
| 155 | #ifdef CONFIG_MODULE_UNLOAD |
| 156 | static ssize_t store_mod_unload(struct module_attribute *mattr, |
| 157 | struct module *mod, |
| 158 | const char *buffer, size_t count) |
| 159 | { |
| 160 | char name[MODULE_NAME_LEN]; |
| 161 | int ret; |
| 162 | |
| 163 | if (count < 1 || count+1 > MODULE_NAME_LEN) |
| 164 | return -EINVAL; |
| 165 | memcpy(name, buffer, count); |
| 166 | name[count] = '\0'; |
| 167 | if (name[count-1] == '\n') |
| 168 | name[count-1] = '\0'; |
| 169 | |
| 170 | ret = delete_module(name, O_NONBLOCK); |
| 171 | if (ret < 0) |
| 172 | return ret; |
| 173 | return count; |
| 174 | } |
| 175 | |
| 176 | static struct module_attribute mod_unload = { |
| 177 | .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE }, |
| 178 | .store = store_mod_unload, |
| 179 | }; |
| 180 | #endif |
| 181 | |
| 182 | static ssize_t show_mod_request_helper(struct module_attribute *mattr, |
| 183 | struct module *mod, |
| 184 | char *buffer) |
| 185 | { |
| 186 | return sprintf(buffer, "%s\n", modprobe_path); |
| 187 | } |
| 188 | |
| 189 | static ssize_t store_mod_request_helper(struct module_attribute *mattr, |
| 190 | struct module *mod, |
| 191 | const char *buffer, size_t count) |
| 192 | { |
| 193 | if (count < 1 || count+1 > KMOD_PATH_LEN) |
| 194 | return -EINVAL; |
| 195 | memcpy(modprobe_path, buffer, count); |
| 196 | modprobe_path[count] = '\0'; |
| 197 | if (modprobe_path[count-1] == '\n') |
| 198 | modprobe_path[count-1] = '\0'; |
| 199 | return count; |
| 200 | } |
| 201 | |
| 202 | static struct module_attribute mod_request_helper = { |
| 203 | .attr = { |
| 204 | .name = "mod_request_helper", |
| 205 | .mode = S_IWUSR | S_IRUGO, |
| 206 | .owner = THIS_MODULE |
| 207 | }, |
| 208 | .show = show_mod_request_helper, |
| 209 | .store = store_mod_request_helper, |
| 210 | }; |
| 211 | |
| 212 | void __init kmod_sysfs_init(void) |
| 213 | { |
| 214 | int ret; |
| 215 | |
| 216 | kmod_mk.mod = THIS_MODULE; |
| 217 | kobj_set_kset_s(&kmod_mk, module_subsys); |
| 218 | kobject_set_name(&kmod_mk.kobj, "kmod"); |
| 219 | kobject_init(&kmod_mk.kobj); |
| 220 | ret = kobject_add(&kmod_mk.kobj); |
| 221 | if (ret < 0) |
| 222 | goto out; |
| 223 | |
| 224 | ret = sysfs_create_file(&kmod_mk.kobj, &mod_request_helper.attr); |
| 225 | ret = sysfs_create_file(&kmod_mk.kobj, &mod_request.attr); |
| 226 | #ifdef CONFIG_MODULE_UNLOAD |
| 227 | ret = sysfs_create_file(&kmod_mk.kobj, &mod_unload.attr); |
| 228 | #endif |
| 229 | |
| 230 | kobject_uevent(&kmod_mk.kobj, KOBJ_ADD); |
| 231 | out: |
| 232 | return; |
| 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | #endif /* CONFIG_KMOD */ |
| 235 | |
| 236 | struct subprocess_info { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 237 | struct work_struct work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | struct completion *complete; |
| 239 | char *path; |
| 240 | char **argv; |
| 241 | char **envp; |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 242 | struct key *ring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | int wait; |
| 244 | int retval; |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 245 | struct file *stdin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | /* |
| 249 | * This is the task which runs the usermode application |
| 250 | */ |
| 251 | static int ____call_usermodehelper(void *data) |
| 252 | { |
| 253 | struct subprocess_info *sub_info = data; |
David Howells | 20e1129 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 254 | struct key *new_session, *old_session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | int retval; |
| 256 | |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 257 | /* Unblock all signals and set the session keyring. */ |
David Howells | 20e1129 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 258 | new_session = key_get(sub_info->ring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | flush_signals(current); |
| 260 | spin_lock_irq(¤t->sighand->siglock); |
David Howells | 20e1129 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 261 | old_session = __install_session_keyring(current, new_session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | flush_signal_handlers(current, 1); |
| 263 | sigemptyset(¤t->blocked); |
| 264 | recalc_sigpending(); |
| 265 | spin_unlock_irq(¤t->sighand->siglock); |
| 266 | |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 267 | key_put(old_session); |
| 268 | |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 269 | /* Install input pipe when needed */ |
| 270 | if (sub_info->stdin) { |
| 271 | struct files_struct *f = current->files; |
| 272 | struct fdtable *fdt; |
| 273 | /* no races because files should be private here */ |
| 274 | sys_close(0); |
| 275 | fd_install(0, sub_info->stdin); |
| 276 | spin_lock(&f->file_lock); |
| 277 | fdt = files_fdtable(f); |
| 278 | FD_SET(0, fdt->open_fds); |
| 279 | FD_CLR(0, fdt->close_on_exec); |
| 280 | spin_unlock(&f->file_lock); |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 281 | |
| 282 | /* and disallow core files too */ |
| 283 | current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0}; |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* We can run anywhere, unlike our parent keventd(). */ |
| 287 | set_cpus_allowed(current, CPU_MASK_ALL); |
| 288 | |
| 289 | retval = -EPERM; |
| 290 | if (current->fs->root) |
Arnd Bergmann | 6760856 | 2006-10-02 02:18:26 -0700 | [diff] [blame] | 291 | retval = kernel_execve(sub_info->path, |
| 292 | sub_info->argv, sub_info->envp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | /* Exec failed? */ |
| 295 | sub_info->retval = retval; |
| 296 | do_exit(0); |
| 297 | } |
| 298 | |
| 299 | /* Keventd can't block, but this (a child) can. */ |
| 300 | static int wait_for_helper(void *data) |
| 301 | { |
| 302 | struct subprocess_info *sub_info = data; |
| 303 | pid_t pid; |
| 304 | struct k_sigaction sa; |
| 305 | |
| 306 | /* Install a handler: if SIGCLD isn't handled sys_wait4 won't |
| 307 | * populate the status, but will return -ECHILD. */ |
| 308 | sa.sa.sa_handler = SIG_IGN; |
| 309 | sa.sa.sa_flags = 0; |
| 310 | siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD)); |
Oleg Nesterov | 8292d63 | 2006-03-28 16:11:10 -0800 | [diff] [blame] | 311 | do_sigaction(SIGCHLD, &sa, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | allow_signal(SIGCHLD); |
| 313 | |
| 314 | pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD); |
| 315 | if (pid < 0) { |
| 316 | sub_info->retval = pid; |
| 317 | } else { |
Björn Steinbrink | 111dbe0 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 318 | int ret; |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* |
| 321 | * Normally it is bogus to call wait4() from in-kernel because |
| 322 | * wait4() wants to write the exit code to a userspace address. |
| 323 | * But wait_for_helper() always runs as keventd, and put_user() |
| 324 | * to a kernel address works OK for kernel threads, due to their |
| 325 | * having an mm_segment_t which spans the entire address space. |
| 326 | * |
| 327 | * Thus the __user pointer cast is valid here. |
| 328 | */ |
Björn Steinbrink | 111dbe0 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 329 | sys_wait4(pid, (int __user *)&ret, 0, NULL); |
| 330 | |
| 331 | /* |
| 332 | * If ret is 0, either ____call_usermodehelper failed and the |
| 333 | * real error code is already in sub_info->retval or |
| 334 | * sub_info->retval is 0 anyway, so don't mess with it then. |
| 335 | */ |
| 336 | if (ret) |
| 337 | sub_info->retval = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 340 | if (sub_info->wait < 0) |
| 341 | kfree(sub_info); |
| 342 | else |
| 343 | complete(sub_info->complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* This is run by khelper thread */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 348 | static void __call_usermodehelper(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 350 | struct subprocess_info *sub_info = |
| 351 | container_of(work, struct subprocess_info, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | pid_t pid; |
Kenneth Lee | e4b69aa | 2006-09-16 12:15:55 -0700 | [diff] [blame] | 353 | int wait = sub_info->wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | /* CLONE_VFORK: wait until the usermode helper has execve'd |
| 356 | * successfully We need the data structures to stay around |
| 357 | * until that is done. */ |
Kenneth Lee | e4b69aa | 2006-09-16 12:15:55 -0700 | [diff] [blame] | 358 | if (wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | pid = kernel_thread(wait_for_helper, sub_info, |
| 360 | CLONE_FS | CLONE_FILES | SIGCHLD); |
| 361 | else |
| 362 | pid = kernel_thread(____call_usermodehelper, sub_info, |
| 363 | CLONE_VFORK | SIGCHLD); |
| 364 | |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 365 | if (wait < 0) |
| 366 | return; |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (pid < 0) { |
| 369 | sub_info->retval = pid; |
| 370 | complete(sub_info->complete); |
Kenneth Lee | e4b69aa | 2006-09-16 12:15:55 -0700 | [diff] [blame] | 371 | } else if (!wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | complete(sub_info->complete); |
| 373 | } |
| 374 | |
| 375 | /** |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 376 | * call_usermodehelper_keys - start a usermode application |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | * @path: pathname for the application |
| 378 | * @argv: null-terminated argument list |
| 379 | * @envp: null-terminated environment list |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 380 | * @session_keyring: session keyring for process (NULL for an empty keyring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | * @wait: wait for the application to finish and return status. |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 382 | * when -1 don't wait at all, but you get no useful error back when |
| 383 | * the program couldn't be exec'ed. This makes it safe to call |
| 384 | * from interrupt context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | * |
| 386 | * Runs a user-space application. The application is started |
| 387 | * asynchronously if wait is not set, and runs as a child of keventd. |
| 388 | * (ie. it runs with full root capabilities). |
| 389 | * |
| 390 | * Must be called from process context. Returns a negative error code |
| 391 | * if program was not execed successfully, or 0. |
| 392 | */ |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 393 | int call_usermodehelper_keys(char *path, char **argv, char **envp, |
| 394 | struct key *session_keyring, int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Ingo Molnar | 60be6b9 | 2006-07-03 00:25:26 -0700 | [diff] [blame] | 396 | DECLARE_COMPLETION_ONSTACK(done); |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 397 | struct subprocess_info *sub_info; |
| 398 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | if (!khelper_wq) |
| 401 | return -EBUSY; |
| 402 | |
| 403 | if (path[0] == '\0') |
| 404 | return 0; |
| 405 | |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 406 | sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC); |
| 407 | if (!sub_info) |
| 408 | return -ENOMEM; |
| 409 | |
| 410 | INIT_WORK(&sub_info->work, __call_usermodehelper); |
| 411 | sub_info->complete = &done; |
| 412 | sub_info->path = path; |
| 413 | sub_info->argv = argv; |
| 414 | sub_info->envp = envp; |
| 415 | sub_info->ring = session_keyring; |
| 416 | sub_info->wait = wait; |
| 417 | |
| 418 | queue_work(khelper_wq, &sub_info->work); |
| 419 | if (wait < 0) /* task has freed sub_info */ |
| 420 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | wait_for_completion(&done); |
Andi Kleen | a98f0dd | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 422 | retval = sub_info->retval; |
| 423 | kfree(sub_info); |
| 424 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
David Howells | 7888e7f | 2005-06-23 22:00:51 -0700 | [diff] [blame] | 426 | EXPORT_SYMBOL(call_usermodehelper_keys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 428 | int call_usermodehelper_pipe(char *path, char **argv, char **envp, |
| 429 | struct file **filp) |
| 430 | { |
| 431 | DECLARE_COMPLETION(done); |
| 432 | struct subprocess_info sub_info = { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 433 | .work = __WORK_INITIALIZER(sub_info.work, |
| 434 | __call_usermodehelper), |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 435 | .complete = &done, |
| 436 | .path = path, |
| 437 | .argv = argv, |
| 438 | .envp = envp, |
| 439 | .retval = 0, |
| 440 | }; |
| 441 | struct file *f; |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 442 | |
| 443 | if (!khelper_wq) |
| 444 | return -EBUSY; |
| 445 | |
| 446 | if (path[0] == '\0') |
| 447 | return 0; |
| 448 | |
| 449 | f = create_write_pipe(); |
Akinobu Mita | 3cce485 | 2006-11-28 12:29:43 -0800 | [diff] [blame] | 450 | if (IS_ERR(f)) |
| 451 | return PTR_ERR(f); |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 452 | *filp = f; |
| 453 | |
| 454 | f = create_read_pipe(f); |
Akinobu Mita | 3cce485 | 2006-11-28 12:29:43 -0800 | [diff] [blame] | 455 | if (IS_ERR(f)) { |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 456 | free_write_pipe(*filp); |
Akinobu Mita | 3cce485 | 2006-11-28 12:29:43 -0800 | [diff] [blame] | 457 | return PTR_ERR(f); |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 458 | } |
| 459 | sub_info.stdin = f; |
| 460 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 461 | queue_work(khelper_wq, &sub_info.work); |
Andi Kleen | e239ca5 | 2006-09-30 23:29:27 -0700 | [diff] [blame] | 462 | wait_for_completion(&done); |
| 463 | return sub_info.retval; |
| 464 | } |
| 465 | EXPORT_SYMBOL(call_usermodehelper_pipe); |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | void __init usermodehelper_init(void) |
| 468 | { |
| 469 | khelper_wq = create_singlethread_workqueue("khelper"); |
| 470 | BUG_ON(!khelper_wq); |
| 471 | } |