USB: mos7720: fix message timeouts
[linux/fpc-iii.git] / kernel / kmod.c
bloba16dac13576c5e517df68d164fc3f69b22c1ad33
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/completion.h>
28 #include <linux/cred.h>
29 #include <linux/file.h>
30 #include <linux/fdtable.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>
36 #include <linux/resource.h>
37 #include <linux/notifier.h>
38 #include <linux/suspend.h>
39 #include <asm/uaccess.h>
41 #include <trace/events/module.h>
43 extern int max_threads;
45 static struct workqueue_struct *khelper_wq;
47 #define CAP_BSET (void *)1
48 #define CAP_PI (void *)2
50 static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
51 static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
52 static DEFINE_SPINLOCK(umh_sysctl_lock);
54 #ifdef CONFIG_MODULES
57 modprobe_path is set via /proc/sys.
59 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
61 static void free_modprobe_argv(struct subprocess_info *info)
63 kfree(info->argv[3]); /* check call_modprobe() */
64 kfree(info->argv);
67 static int call_modprobe(char *module_name, int wait)
69 static char *envp[] = {
70 "HOME=/",
71 "TERM=linux",
72 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
73 NULL
76 char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
77 if (!argv)
78 goto out;
80 module_name = kstrdup(module_name, GFP_KERNEL);
81 if (!module_name)
82 goto free_argv;
84 argv[0] = modprobe_path;
85 argv[1] = "-q";
86 argv[2] = "--";
87 argv[3] = module_name; /* check free_modprobe_argv() */
88 argv[4] = NULL;
90 return call_usermodehelper_fns(modprobe_path, argv, envp,
91 wait | UMH_KILLABLE, NULL, free_modprobe_argv, NULL);
92 free_argv:
93 kfree(argv);
94 out:
95 return -ENOMEM;
98 /**
99 * __request_module - try to load a kernel module
100 * @wait: wait (or not) for the operation to complete
101 * @fmt: printf style format string for the name of the module
102 * @...: arguments as specified in the format string
104 * Load a module using the user mode module loader. The function returns
105 * zero on success or a negative errno code on failure. Note that a
106 * successful module load does not mean the module did not then unload
107 * and exit on an error of its own. Callers must check that the service
108 * they requested is now available not blindly invoke it.
110 * If module auto-loading support is disabled then this function
111 * becomes a no-operation.
113 int __request_module(bool wait, const char *fmt, ...)
115 va_list args;
116 char module_name[MODULE_NAME_LEN];
117 unsigned int max_modprobes;
118 int ret;
119 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
120 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
121 static int kmod_loop_msg;
123 va_start(args, fmt);
124 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
125 va_end(args);
126 if (ret >= MODULE_NAME_LEN)
127 return -ENAMETOOLONG;
129 ret = security_kernel_module_request(module_name);
130 if (ret)
131 return ret;
133 /* If modprobe needs a service that is in a module, we get a recursive
134 * loop. Limit the number of running kmod threads to max_threads/2 or
135 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
136 * would be to run the parents of this process, counting how many times
137 * kmod was invoked. That would mean accessing the internals of the
138 * process tables to get the command line, proc_pid_cmdline is static
139 * and it is not worth changing the proc code just to handle this case.
140 * KAO.
142 * "trace the ppid" is simple, but will fail if someone's
143 * parent exits. I think this is as good as it gets. --RR
145 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
146 atomic_inc(&kmod_concurrent);
147 if (atomic_read(&kmod_concurrent) > max_modprobes) {
148 /* We may be blaming an innocent here, but unlikely */
149 if (kmod_loop_msg < 5) {
150 printk(KERN_ERR
151 "request_module: runaway loop modprobe %s\n",
152 module_name);
153 kmod_loop_msg++;
155 atomic_dec(&kmod_concurrent);
156 return -ENOMEM;
159 trace_module_request(module_name, wait, _RET_IP_);
161 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
163 atomic_dec(&kmod_concurrent);
164 return ret;
166 EXPORT_SYMBOL(__request_module);
167 #endif /* CONFIG_MODULES */
170 * This is the task which runs the usermode application
172 static int ____call_usermodehelper(void *data)
174 struct subprocess_info *sub_info = data;
175 struct cred *new;
176 int retval;
178 spin_lock_irq(&current->sighand->siglock);
179 flush_signal_handlers(current, 1);
180 spin_unlock_irq(&current->sighand->siglock);
182 /* We can run anywhere, unlike our parent keventd(). */
183 set_cpus_allowed_ptr(current, cpu_all_mask);
186 * Our parent is keventd, which runs with elevated scheduling priority.
187 * Avoid propagating that into the userspace child.
189 set_user_nice(current, 0);
191 retval = -ENOMEM;
192 new = prepare_kernel_cred(current);
193 if (!new)
194 goto fail;
196 spin_lock(&umh_sysctl_lock);
197 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
198 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
199 new->cap_inheritable);
200 spin_unlock(&umh_sysctl_lock);
202 if (sub_info->init) {
203 retval = sub_info->init(sub_info, new);
204 if (retval) {
205 abort_creds(new);
206 goto fail;
210 commit_creds(new);
212 retval = kernel_execve(sub_info->path,
213 (const char *const *)sub_info->argv,
214 (const char *const *)sub_info->envp);
216 /* Exec failed? */
217 fail:
218 sub_info->retval = retval;
219 return 0;
222 void call_usermodehelper_freeinfo(struct subprocess_info *info)
224 if (info->cleanup)
225 (*info->cleanup)(info);
226 kfree(info);
228 EXPORT_SYMBOL(call_usermodehelper_freeinfo);
230 static void umh_complete(struct subprocess_info *sub_info)
232 struct completion *comp = xchg(&sub_info->complete, NULL);
234 * See call_usermodehelper_exec(). If xchg() returns NULL
235 * we own sub_info, the UMH_KILLABLE caller has gone away.
237 if (comp)
238 complete(comp);
239 else
240 call_usermodehelper_freeinfo(sub_info);
243 /* Keventd can't block, but this (a child) can. */
244 static int wait_for_helper(void *data)
246 struct subprocess_info *sub_info = data;
247 pid_t pid;
249 /* If SIGCLD is ignored sys_wait4 won't populate the status. */
250 spin_lock_irq(&current->sighand->siglock);
251 current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
252 spin_unlock_irq(&current->sighand->siglock);
254 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
255 if (pid < 0) {
256 sub_info->retval = pid;
257 } else {
258 int ret = -ECHILD;
260 * Normally it is bogus to call wait4() from in-kernel because
261 * wait4() wants to write the exit code to a userspace address.
262 * But wait_for_helper() always runs as keventd, and put_user()
263 * to a kernel address works OK for kernel threads, due to their
264 * having an mm_segment_t which spans the entire address space.
266 * Thus the __user pointer cast is valid here.
268 sys_wait4(pid, (int __user *)&ret, 0, NULL);
271 * If ret is 0, either ____call_usermodehelper failed and the
272 * real error code is already in sub_info->retval or
273 * sub_info->retval is 0 anyway, so don't mess with it then.
275 if (ret)
276 sub_info->retval = ret;
279 umh_complete(sub_info);
280 return 0;
283 /* This is run by khelper thread */
284 static void __call_usermodehelper(struct work_struct *work)
286 struct subprocess_info *sub_info =
287 container_of(work, struct subprocess_info, work);
288 enum umh_wait wait = sub_info->wait;
289 pid_t pid;
291 if (wait != UMH_NO_WAIT)
292 wait &= ~UMH_KILLABLE;
294 /* CLONE_VFORK: wait until the usermode helper has execve'd
295 * successfully We need the data structures to stay around
296 * until that is done. */
297 if (wait == UMH_WAIT_PROC)
298 pid = kernel_thread(wait_for_helper, sub_info,
299 CLONE_FS | CLONE_FILES | SIGCHLD);
300 else
301 pid = kernel_thread(____call_usermodehelper, sub_info,
302 CLONE_VFORK | SIGCHLD);
304 switch (wait) {
305 case UMH_NO_WAIT:
306 call_usermodehelper_freeinfo(sub_info);
307 break;
309 case UMH_WAIT_PROC:
310 if (pid > 0)
311 break;
312 /* FALLTHROUGH */
313 case UMH_WAIT_EXEC:
314 if (pid < 0)
315 sub_info->retval = pid;
316 umh_complete(sub_info);
321 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
322 * (used for preventing user land processes from being created after the user
323 * land has been frozen during a system-wide hibernation or suspend operation).
325 static int usermodehelper_disabled = 1;
327 /* Number of helpers running */
328 static atomic_t running_helpers = ATOMIC_INIT(0);
331 * Wait queue head used by usermodehelper_pm_callback() to wait for all running
332 * helpers to finish.
334 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
337 * Time to wait for running_helpers to become zero before the setting of
338 * usermodehelper_disabled in usermodehelper_pm_callback() fails
340 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
343 * usermodehelper_disable - prevent new helpers from being started
345 int usermodehelper_disable(void)
347 long retval;
349 usermodehelper_disabled = 1;
350 smp_mb();
352 * From now on call_usermodehelper_exec() won't start any new
353 * helpers, so it is sufficient if running_helpers turns out to
354 * be zero at one point (it may be increased later, but that
355 * doesn't matter).
357 retval = wait_event_timeout(running_helpers_waitq,
358 atomic_read(&running_helpers) == 0,
359 RUNNING_HELPERS_TIMEOUT);
360 if (retval)
361 return 0;
363 usermodehelper_disabled = 0;
364 return -EAGAIN;
368 * usermodehelper_enable - allow new helpers to be started again
370 void usermodehelper_enable(void)
372 usermodehelper_disabled = 0;
376 * usermodehelper_is_disabled - check if new helpers are allowed to be started
378 bool usermodehelper_is_disabled(void)
380 return usermodehelper_disabled;
382 EXPORT_SYMBOL_GPL(usermodehelper_is_disabled);
384 static void helper_lock(void)
386 atomic_inc(&running_helpers);
387 smp_mb__after_atomic_inc();
390 static void helper_unlock(void)
392 if (atomic_dec_and_test(&running_helpers))
393 wake_up(&running_helpers_waitq);
397 * call_usermodehelper_setup - prepare to call a usermode helper
398 * @path: path to usermode executable
399 * @argv: arg vector for process
400 * @envp: environment for process
401 * @gfp_mask: gfp mask for memory allocation
403 * Returns either %NULL on allocation failure, or a subprocess_info
404 * structure. This should be passed to call_usermodehelper_exec to
405 * exec the process and free the structure.
407 struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
408 char **envp, gfp_t gfp_mask)
410 struct subprocess_info *sub_info;
411 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
412 if (!sub_info)
413 goto out;
415 INIT_WORK(&sub_info->work, __call_usermodehelper);
416 sub_info->path = path;
417 sub_info->argv = argv;
418 sub_info->envp = envp;
419 out:
420 return sub_info;
422 EXPORT_SYMBOL(call_usermodehelper_setup);
425 * call_usermodehelper_setfns - set a cleanup/init function
426 * @info: a subprocess_info returned by call_usermodehelper_setup
427 * @cleanup: a cleanup function
428 * @init: an init function
429 * @data: arbitrary context sensitive data
431 * The init function is used to customize the helper process prior to
432 * exec. A non-zero return code causes the process to error out, exit,
433 * and return the failure to the calling process
435 * The cleanup function is just before ethe subprocess_info is about to
436 * be freed. This can be used for freeing the argv and envp. The
437 * Function must be runnable in either a process context or the
438 * context in which call_usermodehelper_exec is called.
440 void call_usermodehelper_setfns(struct subprocess_info *info,
441 int (*init)(struct subprocess_info *info, struct cred *new),
442 void (*cleanup)(struct subprocess_info *info),
443 void *data)
445 info->cleanup = cleanup;
446 info->init = init;
447 info->data = data;
449 EXPORT_SYMBOL(call_usermodehelper_setfns);
452 * call_usermodehelper_exec - start a usermode application
453 * @sub_info: information about the subprocessa
454 * @wait: wait for the application to finish and return status.
455 * when -1 don't wait at all, but you get no useful error back when
456 * the program couldn't be exec'ed. This makes it safe to call
457 * from interrupt context.
459 * Runs a user-space application. The application is started
460 * asynchronously if wait is not set, and runs as a child of keventd.
461 * (ie. it runs with full root capabilities).
463 int call_usermodehelper_exec(struct subprocess_info *sub_info,
464 enum umh_wait wait)
466 DECLARE_COMPLETION_ONSTACK(done);
467 int retval = 0;
469 helper_lock();
470 if (!sub_info->path) {
471 retval = -EINVAL;
472 goto out;
475 if (sub_info->path[0] == '\0')
476 goto out;
478 if (!khelper_wq || usermodehelper_disabled) {
479 retval = -EBUSY;
480 goto out;
483 sub_info->complete = &done;
484 sub_info->wait = wait;
486 queue_work(khelper_wq, &sub_info->work);
487 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
488 goto unlock;
490 if (wait & UMH_KILLABLE) {
491 retval = wait_for_completion_killable(&done);
492 if (!retval)
493 goto wait_done;
495 /* umh_complete() will see NULL and free sub_info */
496 if (xchg(&sub_info->complete, NULL))
497 goto unlock;
498 /* fallthrough, umh_complete() was already called */
501 wait_for_completion(&done);
502 wait_done:
503 retval = sub_info->retval;
504 out:
505 call_usermodehelper_freeinfo(sub_info);
506 unlock:
507 helper_unlock();
508 return retval;
510 EXPORT_SYMBOL(call_usermodehelper_exec);
512 static int proc_cap_handler(struct ctl_table *table, int write,
513 void __user *buffer, size_t *lenp, loff_t *ppos)
515 struct ctl_table t;
516 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
517 kernel_cap_t new_cap;
518 int err, i;
520 if (write && (!capable(CAP_SETPCAP) ||
521 !capable(CAP_SYS_MODULE)))
522 return -EPERM;
525 * convert from the global kernel_cap_t to the ulong array to print to
526 * userspace if this is a read.
528 spin_lock(&umh_sysctl_lock);
529 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
530 if (table->data == CAP_BSET)
531 cap_array[i] = usermodehelper_bset.cap[i];
532 else if (table->data == CAP_PI)
533 cap_array[i] = usermodehelper_inheritable.cap[i];
534 else
535 BUG();
537 spin_unlock(&umh_sysctl_lock);
539 t = *table;
540 t.data = &cap_array;
543 * actually read or write and array of ulongs from userspace. Remember
544 * these are least significant 32 bits first
546 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
547 if (err < 0)
548 return err;
551 * convert from the sysctl array of ulongs to the kernel_cap_t
552 * internal representation
554 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
555 new_cap.cap[i] = cap_array[i];
558 * Drop everything not in the new_cap (but don't add things)
560 spin_lock(&umh_sysctl_lock);
561 if (write) {
562 if (table->data == CAP_BSET)
563 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
564 if (table->data == CAP_PI)
565 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
567 spin_unlock(&umh_sysctl_lock);
569 return 0;
572 struct ctl_table usermodehelper_table[] = {
574 .procname = "bset",
575 .data = CAP_BSET,
576 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
577 .mode = 0600,
578 .proc_handler = proc_cap_handler,
581 .procname = "inheritable",
582 .data = CAP_PI,
583 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
584 .mode = 0600,
585 .proc_handler = proc_cap_handler,
590 void __init usermodehelper_init(void)
592 khelper_wq = create_singlethread_workqueue("khelper");
593 BUG_ON(!khelper_wq);