[PATCH] lockdep: annotate BLKPG_DEL_PARTITION
[firewire-audio.git] / kernel / kmod.c
blob9f923f8ce6a0cd3c225f73eb79c865777ac60967
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/smp_lock.h>
27 #include <linux/slab.h>
28 #include <linux/mnt_namespace.h>
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>
36 #include <linux/resource.h>
37 #include <asm/uaccess.h>
39 extern int delete_module(const char *name, unsigned int flags);
41 extern int max_threads;
43 static struct workqueue_struct *khelper_wq;
45 #ifdef CONFIG_KMOD
48 modprobe_path is set via /proc/sys.
50 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
51 struct module_kobject kmod_mk;
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
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.
64 * If module auto-loading support is disabled then this function
65 * becomes a no-operation.
67 int request_module(const char *fmt, ...)
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;
81 char modalias[16 + MODULE_NAME_LEN] = "MODALIAS=";
82 char *uevent_envp[2] = {
83 modalias,
84 NULL
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;
93 strcpy(&modalias[strlen("MODALIAS=")], module_name);
94 kobject_uevent_env(&kmod_mk.kobj, KOBJ_CHANGE, uevent_envp);
96 if (modprobe_path[0] == '\0')
97 goto out;
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.
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
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;
123 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
124 atomic_dec(&kmod_concurrent);
125 out:
126 return ret;
128 EXPORT_SYMBOL(request_module);
130 static ssize_t store_mod_request(struct module_attribute *mattr,
131 struct module *mod,
132 const char *buffer, size_t count)
134 char name[MODULE_NAME_LEN];
135 int ret;
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';
144 ret = request_module(name);
145 if (ret < 0)
146 return ret;
147 return count;
150 static struct module_attribute mod_request = {
151 .attr = { .name = "mod_request", .mode = S_IWUSR, .owner = THIS_MODULE },
152 .store = store_mod_request,
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)
160 char name[MODULE_NAME_LEN];
161 int ret;
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';
170 ret = delete_module(name, O_NONBLOCK);
171 if (ret < 0)
172 return ret;
173 return count;
176 static struct module_attribute mod_unload = {
177 .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE },
178 .store = store_mod_unload,
180 #endif
182 static ssize_t show_mod_request_helper(struct module_attribute *mattr,
183 struct module *mod,
184 char *buffer)
186 return sprintf(buffer, "%s\n", modprobe_path);
189 static ssize_t store_mod_request_helper(struct module_attribute *mattr,
190 struct module *mod,
191 const char *buffer, size_t count)
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;
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
208 .show = show_mod_request_helper,
209 .store = store_mod_request_helper,
212 void __init kmod_sysfs_init(void)
214 int ret;
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;
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
230 kobject_uevent(&kmod_mk.kobj, KOBJ_ADD);
231 out:
232 return;
234 #endif /* CONFIG_KMOD */
236 struct subprocess_info {
237 struct work_struct work;
238 struct completion *complete;
239 char *path;
240 char **argv;
241 char **envp;
242 struct key *ring;
243 int wait;
244 int retval;
245 struct file *stdin;
249 * This is the task which runs the usermode application
251 static int ____call_usermodehelper(void *data)
253 struct subprocess_info *sub_info = data;
254 struct key *new_session, *old_session;
255 int retval;
257 /* Unblock all signals and set the session keyring. */
258 new_session = key_get(sub_info->ring);
259 flush_signals(current);
260 spin_lock_irq(&current->sighand->siglock);
261 old_session = __install_session_keyring(current, new_session);
262 flush_signal_handlers(current, 1);
263 sigemptyset(&current->blocked);
264 recalc_sigpending();
265 spin_unlock_irq(&current->sighand->siglock);
267 key_put(old_session);
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);
282 /* and disallow core files too */
283 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
286 /* We can run anywhere, unlike our parent keventd(). */
287 set_cpus_allowed(current, CPU_MASK_ALL);
289 retval = -EPERM;
290 if (current->fs->root)
291 retval = kernel_execve(sub_info->path,
292 sub_info->argv, sub_info->envp);
294 /* Exec failed? */
295 sub_info->retval = retval;
296 do_exit(0);
299 /* Keventd can't block, but this (a child) can. */
300 static int wait_for_helper(void *data)
302 struct subprocess_info *sub_info = data;
303 pid_t pid;
304 struct k_sigaction sa;
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));
311 do_sigaction(SIGCHLD, &sa, NULL);
312 allow_signal(SIGCHLD);
314 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
315 if (pid < 0) {
316 sub_info->retval = pid;
317 } else {
318 int ret;
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.
327 * Thus the __user pointer cast is valid here.
329 sys_wait4(pid, (int __user *)&ret, 0, NULL);
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.
336 if (ret)
337 sub_info->retval = ret;
340 if (sub_info->wait < 0)
341 kfree(sub_info);
342 else
343 complete(sub_info->complete);
344 return 0;
347 /* This is run by khelper thread */
348 static void __call_usermodehelper(struct work_struct *work)
350 struct subprocess_info *sub_info =
351 container_of(work, struct subprocess_info, work);
352 pid_t pid;
353 int wait = sub_info->wait;
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. */
358 if (wait)
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);
365 if (wait < 0)
366 return;
368 if (pid < 0) {
369 sub_info->retval = pid;
370 complete(sub_info->complete);
371 } else if (!wait)
372 complete(sub_info->complete);
376 * call_usermodehelper_keys - start a usermode application
377 * @path: pathname for the application
378 * @argv: null-terminated argument list
379 * @envp: null-terminated environment list
380 * @session_keyring: session keyring for process (NULL for an empty keyring)
381 * @wait: wait for the application to finish and return status.
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.
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).
390 * Must be called from process context. Returns a negative error code
391 * if program was not execed successfully, or 0.
393 int call_usermodehelper_keys(char *path, char **argv, char **envp,
394 struct key *session_keyring, int wait)
396 DECLARE_COMPLETION_ONSTACK(done);
397 struct subprocess_info *sub_info;
398 int retval;
400 if (!khelper_wq)
401 return -EBUSY;
403 if (path[0] == '\0')
404 return 0;
406 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
407 if (!sub_info)
408 return -ENOMEM;
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;
418 queue_work(khelper_wq, &sub_info->work);
419 if (wait < 0) /* task has freed sub_info */
420 return 0;
421 wait_for_completion(&done);
422 retval = sub_info->retval;
423 kfree(sub_info);
424 return retval;
426 EXPORT_SYMBOL(call_usermodehelper_keys);
428 int call_usermodehelper_pipe(char *path, char **argv, char **envp,
429 struct file **filp)
431 DECLARE_COMPLETION(done);
432 struct subprocess_info sub_info = {
433 .work = __WORK_INITIALIZER(sub_info.work,
434 __call_usermodehelper),
435 .complete = &done,
436 .path = path,
437 .argv = argv,
438 .envp = envp,
439 .retval = 0,
441 struct file *f;
443 if (!khelper_wq)
444 return -EBUSY;
446 if (path[0] == '\0')
447 return 0;
449 f = create_write_pipe();
450 if (IS_ERR(f))
451 return PTR_ERR(f);
452 *filp = f;
454 f = create_read_pipe(f);
455 if (IS_ERR(f)) {
456 free_write_pipe(*filp);
457 return PTR_ERR(f);
459 sub_info.stdin = f;
461 queue_work(khelper_wq, &sub_info.work);
462 wait_for_completion(&done);
463 return sub_info.retval;
465 EXPORT_SYMBOL(call_usermodehelper_pipe);
467 void __init usermodehelper_init(void)
469 khelper_wq = create_singlethread_workqueue("khelper");
470 BUG_ON(!khelper_wq);