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/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;
45 * If set, both call_usermodehelper_keys() and call_usermodehelper_pipe() exit
46 * immediately returning -EBUSY. Used for preventing user land processes from
47 * being created after the user land has been frozen during a system-wide
48 * hibernation or suspend operation.
50 static int usermodehelper_disabled;
55 modprobe_path is set via /proc/sys.
57 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
60 * request_module - try to load a kernel module
61 * @fmt: printf style format string for the name of the module
62 * @varargs: arguements as specified in the format string
64 * Load a module using the user mode module loader. The function returns
65 * zero on success or a negative errno code on failure. Note that a
66 * successful module load does not mean the module did not then unload
67 * and exit on an error of its own. Callers must check that the service
68 * they requested is now available not blindly invoke it.
70 * If module auto-loading support is disabled then this function
71 * becomes a no-operation.
73 int request_module(const char *fmt, ...)
76 char module_name[MODULE_NAME_LEN];
77 unsigned int max_modprobes;
79 char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
80 static char *envp[] = { "HOME=/",
82 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
84 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
85 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
86 static int kmod_loop_msg;
89 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
91 if (ret >= MODULE_NAME_LEN)
94 /* If modprobe needs a service that is in a module, we get a recursive
95 * loop. Limit the number of running kmod threads to max_threads/2 or
96 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
97 * would be to run the parents of this process, counting how many times
98 * kmod was invoked. That would mean accessing the internals of the
99 * process tables to get the command line, proc_pid_cmdline is static
100 * and it is not worth changing the proc code just to handle this case.
103 * "trace the ppid" is simple, but will fail if someone's
104 * parent exits. I think this is as good as it gets. --RR
106 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
107 atomic_inc(&kmod_concurrent);
108 if (atomic_read(&kmod_concurrent) > max_modprobes) {
109 /* We may be blaming an innocent here, but unlikely */
110 if (kmod_loop_msg++ < 5)
112 "request_module: runaway loop modprobe %s\n",
114 atomic_dec(&kmod_concurrent);
118 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
119 atomic_dec(&kmod_concurrent);
122 EXPORT_SYMBOL(request_module);
123 #endif /* CONFIG_KMOD */
125 struct subprocess_info {
126 struct work_struct work;
127 struct completion *complete;
135 void (*cleanup)(char **argv, char **envp);
139 * This is the task which runs the usermode application
141 static int ____call_usermodehelper(void *data)
143 struct subprocess_info *sub_info = data;
144 struct key *new_session, *old_session;
147 /* Unblock all signals and set the session keyring. */
148 new_session = key_get(sub_info->ring);
149 spin_lock_irq(¤t->sighand->siglock);
150 old_session = __install_session_keyring(current, new_session);
151 flush_signal_handlers(current, 1);
152 sigemptyset(¤t->blocked);
154 spin_unlock_irq(¤t->sighand->siglock);
156 key_put(old_session);
158 /* Install input pipe when needed */
159 if (sub_info->stdin) {
160 struct files_struct *f = current->files;
162 /* no races because files should be private here */
164 fd_install(0, sub_info->stdin);
165 spin_lock(&f->file_lock);
166 fdt = files_fdtable(f);
167 FD_SET(0, fdt->open_fds);
168 FD_CLR(0, fdt->close_on_exec);
169 spin_unlock(&f->file_lock);
171 /* and disallow core files too */
172 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
175 /* We can run anywhere, unlike our parent keventd(). */
176 set_cpus_allowed(current, CPU_MASK_ALL);
179 * Our parent is keventd, which runs with elevated scheduling priority.
180 * Avoid propagating that into the userspace child.
182 set_user_nice(current, 0);
185 if (current->fs->root)
186 retval = kernel_execve(sub_info->path,
187 sub_info->argv, sub_info->envp);
190 sub_info->retval = retval;
194 void call_usermodehelper_freeinfo(struct subprocess_info *info)
197 (*info->cleanup)(info->argv, info->envp);
200 EXPORT_SYMBOL(call_usermodehelper_freeinfo);
202 /* Keventd can't block, but this (a child) can. */
203 static int wait_for_helper(void *data)
205 struct subprocess_info *sub_info = data;
208 /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
209 * populate the status, but will return -ECHILD. */
210 allow_signal(SIGCHLD);
212 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
214 sub_info->retval = pid;
219 * Normally it is bogus to call wait4() from in-kernel because
220 * wait4() wants to write the exit code to a userspace address.
221 * But wait_for_helper() always runs as keventd, and put_user()
222 * to a kernel address works OK for kernel threads, due to their
223 * having an mm_segment_t which spans the entire address space.
225 * Thus the __user pointer cast is valid here.
227 sys_wait4(pid, (int __user *)&ret, 0, NULL);
230 * If ret is 0, either ____call_usermodehelper failed and the
231 * real error code is already in sub_info->retval or
232 * sub_info->retval is 0 anyway, so don't mess with it then.
235 sub_info->retval = ret;
238 if (sub_info->wait == UMH_NO_WAIT)
239 call_usermodehelper_freeinfo(sub_info);
241 complete(sub_info->complete);
245 /* This is run by khelper thread */
246 static void __call_usermodehelper(struct work_struct *work)
248 struct subprocess_info *sub_info =
249 container_of(work, struct subprocess_info, work);
251 enum umh_wait wait = sub_info->wait;
253 /* CLONE_VFORK: wait until the usermode helper has execve'd
254 * successfully We need the data structures to stay around
255 * until that is done. */
256 if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT)
257 pid = kernel_thread(wait_for_helper, sub_info,
258 CLONE_FS | CLONE_FILES | SIGCHLD);
260 pid = kernel_thread(____call_usermodehelper, sub_info,
261 CLONE_VFORK | SIGCHLD);
270 sub_info->retval = pid;
274 complete(sub_info->complete);
278 static int usermodehelper_pm_callback(struct notifier_block *nfb,
279 unsigned long action,
283 case PM_HIBERNATION_PREPARE:
284 case PM_SUSPEND_PREPARE:
285 usermodehelper_disabled = 1;
287 case PM_POST_HIBERNATION:
288 case PM_POST_SUSPEND:
289 usermodehelper_disabled = 0;
297 * call_usermodehelper_setup - prepare to call a usermode helper
298 * @path - path to usermode executable
299 * @argv - arg vector for process
300 * @envp - environment for process
302 * Returns either NULL on allocation failure, or a subprocess_info
303 * structure. This should be passed to call_usermodehelper_exec to
304 * exec the process and free the structure.
306 struct subprocess_info *call_usermodehelper_setup(char *path,
307 char **argv, char **envp)
309 struct subprocess_info *sub_info;
310 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
314 INIT_WORK(&sub_info->work, __call_usermodehelper);
315 sub_info->path = path;
316 sub_info->argv = argv;
317 sub_info->envp = envp;
322 EXPORT_SYMBOL(call_usermodehelper_setup);
325 * call_usermodehelper_setkeys - set the session keys for usermode helper
326 * @info: a subprocess_info returned by call_usermodehelper_setup
327 * @session_keyring: the session keyring for the process
329 void call_usermodehelper_setkeys(struct subprocess_info *info,
330 struct key *session_keyring)
332 info->ring = session_keyring;
334 EXPORT_SYMBOL(call_usermodehelper_setkeys);
337 * call_usermodehelper_setcleanup - set a cleanup function
338 * @info: a subprocess_info returned by call_usermodehelper_setup
339 * @cleanup: a cleanup function
341 * The cleanup function is just befor ethe subprocess_info is about to
342 * be freed. This can be used for freeing the argv and envp. The
343 * Function must be runnable in either a process context or the
344 * context in which call_usermodehelper_exec is called.
346 void call_usermodehelper_setcleanup(struct subprocess_info *info,
347 void (*cleanup)(char **argv, char **envp))
349 info->cleanup = cleanup;
351 EXPORT_SYMBOL(call_usermodehelper_setcleanup);
354 * call_usermodehelper_stdinpipe - set up a pipe to be used for stdin
355 * @sub_info: a subprocess_info returned by call_usermodehelper_setup
356 * @filp: set to the write-end of a pipe
358 * This constructs a pipe, and sets the read end to be the stdin of the
359 * subprocess, and returns the write-end in *@filp.
361 int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
366 f = create_write_pipe();
371 f = create_read_pipe(f);
373 free_write_pipe(*filp);
380 EXPORT_SYMBOL(call_usermodehelper_stdinpipe);
383 * call_usermodehelper_exec - start a usermode application
384 * @sub_info: information about the subprocessa
385 * @wait: wait for the application to finish and return status.
386 * when -1 don't wait at all, but you get no useful error back when
387 * the program couldn't be exec'ed. This makes it safe to call
388 * from interrupt context.
390 * Runs a user-space application. The application is started
391 * asynchronously if wait is not set, and runs as a child of keventd.
392 * (ie. it runs with full root capabilities).
394 int call_usermodehelper_exec(struct subprocess_info *sub_info,
397 DECLARE_COMPLETION_ONSTACK(done);
400 if (sub_info->path[0] == '\0') {
405 if (!khelper_wq || usermodehelper_disabled) {
410 sub_info->complete = &done;
411 sub_info->wait = wait;
413 queue_work(khelper_wq, &sub_info->work);
414 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
416 wait_for_completion(&done);
417 retval = sub_info->retval;
420 call_usermodehelper_freeinfo(sub_info);
423 EXPORT_SYMBOL(call_usermodehelper_exec);
426 * call_usermodehelper_pipe - call a usermode helper process with a pipe stdin
427 * @path: path to usermode executable
428 * @argv: arg vector for process
429 * @envp: environment for process
430 * @filp: set to the write-end of a pipe
432 * This is a simple wrapper which executes a usermode-helper function
433 * with a pipe as stdin. It is implemented entirely in terms of
434 * lower-level call_usermodehelper_* functions.
436 int call_usermodehelper_pipe(char *path, char **argv, char **envp,
439 struct subprocess_info *sub_info;
442 sub_info = call_usermodehelper_setup(path, argv, envp);
443 if (sub_info == NULL)
446 ret = call_usermodehelper_stdinpipe(sub_info, filp);
450 return call_usermodehelper_exec(sub_info, 1);
453 call_usermodehelper_freeinfo(sub_info);
456 EXPORT_SYMBOL(call_usermodehelper_pipe);
458 void __init usermodehelper_init(void)
460 khelper_wq = create_singlethread_workqueue("khelper");
462 pm_notifier(usermodehelper_pm_callback, 0);