[POWERPC] Add error return from htab_remove_mapping()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / kmod.c
blob22be3ff3f363ac71a319deeb2f55fd0ccbe27c26
1 /*
2 kmod, the new module loader (replaces kerneld)
3 Kirk Petersen
5 Reorganized not to be a daemon by Adam Richter, with guidance
6 from Greg Zornetzer.
8 Modified to avoid chroot and file sharing problems.
9 Mikael Pettersson
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
15 Unblock all signals when we exec a usermode process.
16 Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
18 call_usermodehelper wait flag, and remove exec_usermodehelper.
19 Rusty Russell <rusty@rustcorp.com.au> Jan 2003
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/slab.h>
27 #include <linux/mnt_namespace.h>
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>
35 #include <linux/resource.h>
36 #include <linux/notifier.h>
37 #include <linux/suspend.h>
38 #include <asm/uaccess.h>
40 extern int max_threads;
42 static struct workqueue_struct *khelper_wq;
44 #ifdef CONFIG_KMOD
47 modprobe_path is set via /proc/sys.
49 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
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
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.
62 * If module auto-loading support is disabled then this function
63 * becomes a no-operation.
65 int request_module(const char *fmt, ...)
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;
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;
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.
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
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;
110 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
111 atomic_dec(&kmod_concurrent);
112 return ret;
114 EXPORT_SYMBOL(request_module);
115 #endif /* CONFIG_KMOD */
117 struct subprocess_info {
118 struct work_struct work;
119 struct completion *complete;
120 char *path;
121 char **argv;
122 char **envp;
123 struct key *ring;
124 enum umh_wait wait;
125 int retval;
126 struct file *stdin;
127 void (*cleanup)(char **argv, char **envp);
131 * This is the task which runs the usermode application
133 static int ____call_usermodehelper(void *data)
135 struct subprocess_info *sub_info = data;
136 struct key *new_session, *old_session;
137 int retval;
139 /* Unblock all signals and set the session keyring. */
140 new_session = key_get(sub_info->ring);
141 spin_lock_irq(&current->sighand->siglock);
142 old_session = __install_session_keyring(current, new_session);
143 flush_signal_handlers(current, 1);
144 sigemptyset(&current->blocked);
145 recalc_sigpending();
146 spin_unlock_irq(&current->sighand->siglock);
148 key_put(old_session);
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);
163 /* and disallow core files too */
164 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
167 /* We can run anywhere, unlike our parent keventd(). */
168 set_cpus_allowed(current, CPU_MASK_ALL);
171 * Our parent is keventd, which runs with elevated scheduling priority.
172 * Avoid propagating that into the userspace child.
174 set_user_nice(current, 0);
176 retval = kernel_execve(sub_info->path, sub_info->argv, sub_info->envp);
178 /* Exec failed? */
179 sub_info->retval = retval;
180 do_exit(0);
183 void call_usermodehelper_freeinfo(struct subprocess_info *info)
185 if (info->cleanup)
186 (*info->cleanup)(info->argv, info->envp);
187 kfree(info);
189 EXPORT_SYMBOL(call_usermodehelper_freeinfo);
191 /* Keventd can't block, but this (a child) can. */
192 static int wait_for_helper(void *data)
194 struct subprocess_info *sub_info = data;
195 pid_t pid;
197 /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
198 * populate the status, but will return -ECHILD. */
199 allow_signal(SIGCHLD);
201 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
202 if (pid < 0) {
203 sub_info->retval = pid;
204 } else {
205 int ret;
208 * Normally it is bogus to call wait4() from in-kernel because
209 * wait4() wants to write the exit code to a userspace address.
210 * But wait_for_helper() always runs as keventd, and put_user()
211 * to a kernel address works OK for kernel threads, due to their
212 * having an mm_segment_t which spans the entire address space.
214 * Thus the __user pointer cast is valid here.
216 sys_wait4(pid, (int __user *)&ret, 0, NULL);
219 * If ret is 0, either ____call_usermodehelper failed and the
220 * real error code is already in sub_info->retval or
221 * sub_info->retval is 0 anyway, so don't mess with it then.
223 if (ret)
224 sub_info->retval = ret;
227 if (sub_info->wait == UMH_NO_WAIT)
228 call_usermodehelper_freeinfo(sub_info);
229 else
230 complete(sub_info->complete);
231 return 0;
234 /* This is run by khelper thread */
235 static void __call_usermodehelper(struct work_struct *work)
237 struct subprocess_info *sub_info =
238 container_of(work, struct subprocess_info, work);
239 pid_t pid;
240 enum umh_wait wait = sub_info->wait;
242 /* CLONE_VFORK: wait until the usermode helper has execve'd
243 * successfully We need the data structures to stay around
244 * until that is done. */
245 if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT)
246 pid = kernel_thread(wait_for_helper, sub_info,
247 CLONE_FS | CLONE_FILES | SIGCHLD);
248 else
249 pid = kernel_thread(____call_usermodehelper, sub_info,
250 CLONE_VFORK | SIGCHLD);
252 switch (wait) {
253 case UMH_NO_WAIT:
254 break;
256 case UMH_WAIT_PROC:
257 if (pid > 0)
258 break;
259 sub_info->retval = pid;
260 /* FALLTHROUGH */
262 case UMH_WAIT_EXEC:
263 complete(sub_info->complete);
267 #ifdef CONFIG_PM
269 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
270 * (used for preventing user land processes from being created after the user
271 * land has been frozen during a system-wide hibernation or suspend operation).
273 static int usermodehelper_disabled;
275 /* Number of helpers running */
276 static atomic_t running_helpers = ATOMIC_INIT(0);
279 * Wait queue head used by usermodehelper_pm_callback() to wait for all running
280 * helpers to finish.
282 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
285 * Time to wait for running_helpers to become zero before the setting of
286 * usermodehelper_disabled in usermodehelper_pm_callback() fails
288 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
290 static int usermodehelper_pm_callback(struct notifier_block *nfb,
291 unsigned long action,
292 void *ignored)
294 long retval;
296 switch (action) {
297 case PM_HIBERNATION_PREPARE:
298 case PM_SUSPEND_PREPARE:
299 usermodehelper_disabled = 1;
300 smp_mb();
302 * From now on call_usermodehelper_exec() won't start any new
303 * helpers, so it is sufficient if running_helpers turns out to
304 * be zero at one point (it may be increased later, but that
305 * doesn't matter).
307 retval = wait_event_timeout(running_helpers_waitq,
308 atomic_read(&running_helpers) == 0,
309 RUNNING_HELPERS_TIMEOUT);
310 if (retval) {
311 return NOTIFY_OK;
312 } else {
313 usermodehelper_disabled = 0;
314 return NOTIFY_BAD;
316 case PM_POST_HIBERNATION:
317 case PM_POST_SUSPEND:
318 usermodehelper_disabled = 0;
319 return NOTIFY_OK;
322 return NOTIFY_DONE;
325 static void helper_lock(void)
327 atomic_inc(&running_helpers);
328 smp_mb__after_atomic_inc();
331 static void helper_unlock(void)
333 if (atomic_dec_and_test(&running_helpers))
334 wake_up(&running_helpers_waitq);
337 static void register_pm_notifier_callback(void)
339 pm_notifier(usermodehelper_pm_callback, 0);
341 #else /* CONFIG_PM */
342 #define usermodehelper_disabled 0
344 static inline void helper_lock(void) {}
345 static inline void helper_unlock(void) {}
346 static inline void register_pm_notifier_callback(void) {}
347 #endif /* CONFIG_PM */
350 * call_usermodehelper_setup - prepare to call a usermode helper
351 * @path: path to usermode executable
352 * @argv: arg vector for process
353 * @envp: environment for process
355 * Returns either %NULL on allocation failure, or a subprocess_info
356 * structure. This should be passed to call_usermodehelper_exec to
357 * exec the process and free the structure.
359 struct subprocess_info *call_usermodehelper_setup(char *path,
360 char **argv, char **envp)
362 struct subprocess_info *sub_info;
363 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
364 if (!sub_info)
365 goto out;
367 INIT_WORK(&sub_info->work, __call_usermodehelper);
368 sub_info->path = path;
369 sub_info->argv = argv;
370 sub_info->envp = envp;
372 out:
373 return sub_info;
375 EXPORT_SYMBOL(call_usermodehelper_setup);
378 * call_usermodehelper_setkeys - set the session keys for usermode helper
379 * @info: a subprocess_info returned by call_usermodehelper_setup
380 * @session_keyring: the session keyring for the process
382 void call_usermodehelper_setkeys(struct subprocess_info *info,
383 struct key *session_keyring)
385 info->ring = session_keyring;
387 EXPORT_SYMBOL(call_usermodehelper_setkeys);
390 * call_usermodehelper_setcleanup - set a cleanup function
391 * @info: a subprocess_info returned by call_usermodehelper_setup
392 * @cleanup: a cleanup function
394 * The cleanup function is just befor ethe subprocess_info is about to
395 * be freed. This can be used for freeing the argv and envp. The
396 * Function must be runnable in either a process context or the
397 * context in which call_usermodehelper_exec is called.
399 void call_usermodehelper_setcleanup(struct subprocess_info *info,
400 void (*cleanup)(char **argv, char **envp))
402 info->cleanup = cleanup;
404 EXPORT_SYMBOL(call_usermodehelper_setcleanup);
407 * call_usermodehelper_stdinpipe - set up a pipe to be used for stdin
408 * @sub_info: a subprocess_info returned by call_usermodehelper_setup
409 * @filp: set to the write-end of a pipe
411 * This constructs a pipe, and sets the read end to be the stdin of the
412 * subprocess, and returns the write-end in *@filp.
414 int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
415 struct file **filp)
417 struct file *f;
419 f = create_write_pipe();
420 if (IS_ERR(f))
421 return PTR_ERR(f);
422 *filp = f;
424 f = create_read_pipe(f);
425 if (IS_ERR(f)) {
426 free_write_pipe(*filp);
427 return PTR_ERR(f);
429 sub_info->stdin = f;
431 return 0;
433 EXPORT_SYMBOL(call_usermodehelper_stdinpipe);
436 * call_usermodehelper_exec - start a usermode application
437 * @sub_info: information about the subprocessa
438 * @wait: wait for the application to finish and return status.
439 * when -1 don't wait at all, but you get no useful error back when
440 * the program couldn't be exec'ed. This makes it safe to call
441 * from interrupt context.
443 * Runs a user-space application. The application is started
444 * asynchronously if wait is not set, and runs as a child of keventd.
445 * (ie. it runs with full root capabilities).
447 int call_usermodehelper_exec(struct subprocess_info *sub_info,
448 enum umh_wait wait)
450 DECLARE_COMPLETION_ONSTACK(done);
451 int retval = 0;
453 helper_lock();
454 if (sub_info->path[0] == '\0')
455 goto out;
457 if (!khelper_wq || usermodehelper_disabled) {
458 retval = -EBUSY;
459 goto out;
462 sub_info->complete = &done;
463 sub_info->wait = wait;
465 queue_work(khelper_wq, &sub_info->work);
466 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
467 goto unlock;
468 wait_for_completion(&done);
469 retval = sub_info->retval;
471 out:
472 call_usermodehelper_freeinfo(sub_info);
473 unlock:
474 helper_unlock();
475 return retval;
477 EXPORT_SYMBOL(call_usermodehelper_exec);
480 * call_usermodehelper_pipe - call a usermode helper process with a pipe stdin
481 * @path: path to usermode executable
482 * @argv: arg vector for process
483 * @envp: environment for process
484 * @filp: set to the write-end of a pipe
486 * This is a simple wrapper which executes a usermode-helper function
487 * with a pipe as stdin. It is implemented entirely in terms of
488 * lower-level call_usermodehelper_* functions.
490 int call_usermodehelper_pipe(char *path, char **argv, char **envp,
491 struct file **filp)
493 struct subprocess_info *sub_info;
494 int ret;
496 sub_info = call_usermodehelper_setup(path, argv, envp);
497 if (sub_info == NULL)
498 return -ENOMEM;
500 ret = call_usermodehelper_stdinpipe(sub_info, filp);
501 if (ret < 0)
502 goto out;
504 return call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
506 out:
507 call_usermodehelper_freeinfo(sub_info);
508 return ret;
510 EXPORT_SYMBOL(call_usermodehelper_pipe);
512 void __init usermodehelper_init(void)
514 khelper_wq = create_singlethread_workqueue("khelper");
515 BUG_ON(!khelper_wq);
516 register_pm_notifier_callback();