2 kmod, the new module loader (replaces kerneld)
5 Reorganized not to be a daemon by Adam Richter, with guidance
8 Modified to avoid chroot and file sharing problems.
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;
48 modprobe_path is set via /proc/sys.
50 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
51 struct module_kobject kmod_mk;
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, ...)
70 char module_name[MODULE_NAME_LEN];
71 unsigned int max_modprobes;
73 char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
74 static char *envp[] = { "HOME=/",
76 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
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] = {
88 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
90 if (ret >= MODULE_NAME_LEN)
93 strcpy(&modalias[strlen("MODALIAS=")], module_name);
94 kobject_uevent_env(&kmod_mk.kobj, KOBJ_CHANGE, uevent_envp);
96 if (modprobe_path[0] == '\0')
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.
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)
117 "request_module: runaway loop modprobe %s\n",
119 atomic_dec(&kmod_concurrent);
123 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
124 atomic_dec(&kmod_concurrent);
128 EXPORT_SYMBOL(request_module);
130 static ssize_t store_mod_request(struct module_attribute *mattr,
132 const char *buffer, size_t count)
134 char name[MODULE_NAME_LEN];
137 if (count < 1 || count+1 > MODULE_NAME_LEN)
139 memcpy(name, buffer, count);
141 if (name[count-1] == '\n')
142 name[count-1] = '\0';
144 ret = request_module(name);
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,
158 const char *buffer, size_t count)
160 char name[MODULE_NAME_LEN];
163 if (count < 1 || count+1 > MODULE_NAME_LEN)
165 memcpy(name, buffer, count);
167 if (name[count-1] == '\n')
168 name[count-1] = '\0';
170 ret = delete_module(name, O_NONBLOCK);
176 static struct module_attribute mod_unload = {
177 .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE },
178 .store = store_mod_unload,
182 static ssize_t show_mod_request_helper(struct module_attribute *mattr,
186 return sprintf(buffer, "%s\n", modprobe_path);
189 static ssize_t store_mod_request_helper(struct module_attribute *mattr,
191 const char *buffer, size_t count)
193 if (count < 1 || count+1 > KMOD_PATH_LEN)
195 memcpy(modprobe_path, buffer, count);
196 modprobe_path[count] = '\0';
197 if (modprobe_path[count-1] == '\n')
198 modprobe_path[count-1] = '\0';
202 static struct module_attribute mod_request_helper = {
204 .name = "mod_request_helper",
205 .mode = S_IWUSR | S_IRUGO,
208 .show = show_mod_request_helper,
209 .store = store_mod_request_helper,
212 void __init kmod_sysfs_init(void)
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);
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);
230 kobject_uevent(&kmod_mk.kobj, KOBJ_ADD);
234 #endif /* CONFIG_KMOD */
236 struct subprocess_info {
237 struct work_struct work;
238 struct completion *complete;
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;
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(¤t->sighand->siglock);
261 old_session = __install_session_keyring(current, new_session);
262 flush_signal_handlers(current, 1);
263 sigemptyset(¤t->blocked);
265 spin_unlock_irq(¤t->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;
273 /* no races because files should be private here */
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);
290 if (current->fs->root)
291 retval = kernel_execve(sub_info->path,
292 sub_info->argv, sub_info->envp);
295 sub_info->retval = retval;
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;
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;
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);
316 sub_info->retval = pid;
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.
337 sub_info->retval = ret;
340 if (sub_info->wait < 0)
343 complete(sub_info->complete);
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);
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. */
359 pid = kernel_thread(wait_for_helper, sub_info,
360 CLONE_FS | CLONE_FILES | SIGCHLD);
362 pid = kernel_thread(____call_usermodehelper, sub_info,
363 CLONE_VFORK | SIGCHLD);
369 sub_info->retval = pid;
370 complete(sub_info->complete);
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;
406 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
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 */
421 wait_for_completion(&done);
422 retval = sub_info->retval;
426 EXPORT_SYMBOL(call_usermodehelper_keys);
428 int call_usermodehelper_pipe(char *path, char **argv, char **envp,
431 DECLARE_COMPLETION(done);
432 struct subprocess_info sub_info = {
433 .work = __WORK_INITIALIZER(sub_info.work,
434 __call_usermodehelper),
449 f = create_write_pipe();
454 f = create_read_pipe(f);
456 free_write_pipe(*filp);
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");