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
19 #define __KERNEL_SYSCALLS__
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/sched.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/namespace.h>
29 #include <linux/completion.h>
31 #include <asm/uaccess.h>
33 extern int max_threads;
36 use_init_fs_context(void)
38 struct fs_struct *our_fs, *init_fs;
39 struct dentry *root, *pwd;
40 struct vfsmount *rootmnt, *pwdmnt;
41 struct namespace *our_ns, *init_ns;
44 * Make modprobe's fs context be a copy of init's.
46 * We cannot use the user's fs context, because it
47 * may have a different root than init.
48 * Since init was created with CLONE_FS, we can grab
49 * its fs context from "init_task".
51 * The fs context has to be a copy. If it is shared
52 * with init, then any chdir() call in modprobe will
53 * also affect init and the other threads sharing
54 * init_task's fs context.
56 * We created the exec_modprobe thread without CLONE_FS,
57 * so we can update the fields in our fs context freely.
60 init_fs = init_task.fs;
61 init_ns = init_task.namespace;
62 get_namespace(init_ns);
63 our_ns = current->namespace;
64 current->namespace = init_ns;
65 put_namespace(our_ns);
66 read_lock(&init_fs->lock);
67 rootmnt = mntget(init_fs->rootmnt);
68 root = dget(init_fs->root);
69 pwdmnt = mntget(init_fs->pwdmnt);
70 pwd = dget(init_fs->pwd);
71 read_unlock(&init_fs->lock);
73 /* FIXME - unsafe ->fs access */
75 our_fs->umask = init_fs->umask;
76 set_fs_root(our_fs, rootmnt, root);
77 set_fs_pwd(our_fs, pwdmnt, pwd);
78 write_lock(&our_fs->lock);
79 if (our_fs->altroot) {
80 struct vfsmount *mnt = our_fs->altrootmnt;
81 struct dentry *dentry = our_fs->altroot;
82 our_fs->altrootmnt = NULL;
83 our_fs->altroot = NULL;
84 write_unlock(&our_fs->lock);
88 write_unlock(&our_fs->lock);
95 int exec_usermodehelper(char *program_path, char *argv[], char *envp[])
98 struct task_struct *curtask = current;
100 curtask->session = 1;
103 use_init_fs_context();
105 /* Prevent parent user process from sending signals to child.
106 Otherwise, if the modprobe program does not exist, it might
107 be possible to get a user defined signal handler to execute
108 as the super user right after the execve fails if you time
109 the signal just right.
111 spin_lock_irq(&curtask->sigmask_lock);
112 sigemptyset(&curtask->blocked);
113 flush_signals(curtask);
114 flush_signal_handlers(curtask);
115 recalc_sigpending(curtask);
116 spin_unlock_irq(&curtask->sigmask_lock);
118 for (i = 0; i < curtask->files->max_fds; i++ ) {
119 if (curtask->files->fd[i]) close(i);
122 /* Drop the "current user" thing */
124 struct user_struct *user = curtask->user;
125 curtask->user = INIT_USER;
126 atomic_inc(&INIT_USER->__count);
127 atomic_inc(&INIT_USER->processes);
128 atomic_dec(&user->processes);
132 /* Give kmod all effective privileges.. */
133 curtask->euid = curtask->fsuid = 0;
134 curtask->egid = curtask->fsgid = 0;
135 cap_set_full(curtask->cap_effective);
137 /* Allow execve args to be in kernel space. */
142 if (execve(program_path, argv, envp) < 0)
150 modprobe_path is set via /proc/sys.
152 char modprobe_path[256] = "/sbin/modprobe";
154 static int exec_modprobe(void * module_name)
156 static char * envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
157 char *argv[] = { modprobe_path, "-s", "-k", "--", (char*)module_name, NULL };
160 ret = exec_usermodehelper(modprobe_path, argv, envp);
163 "kmod: failed to exec %s -s -k %s, errno = %d\n",
164 modprobe_path, (char*) module_name, errno);
170 * request_module - try to load a kernel module
171 * @module_name: Name of module
173 * Load a module using the user mode module loader. The function returns
174 * zero on success or a negative errno code on failure. Note that a
175 * successful module load does not mean the module did not then unload
176 * and exit on an error of its own. Callers must check that the service
177 * they requested is now available not blindly invoke it.
179 * If module auto-loading support is disabled then this function
180 * becomes a no-operation.
182 int request_module(const char * module_name)
188 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
189 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
190 static int kmod_loop_msg;
192 /* Don't allow request_module() before the root fs is mounted! */
193 if ( ! current->fs->root ) {
194 printk(KERN_ERR "request_module[%s]: Root fs not mounted\n",
199 /* If modprobe needs a service that is in a module, we get a recursive
200 * loop. Limit the number of running kmod threads to max_threads/2 or
201 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
202 * would be to run the parents of this process, counting how many times
203 * kmod was invoked. That would mean accessing the internals of the
204 * process tables to get the command line, proc_pid_cmdline is static
205 * and it is not worth changing the proc code just to handle this case.
209 if (i > MAX_KMOD_CONCURRENT)
210 i = MAX_KMOD_CONCURRENT;
211 atomic_inc(&kmod_concurrent);
212 if (atomic_read(&kmod_concurrent) > i) {
213 if (kmod_loop_msg++ < 5)
215 "kmod: runaway modprobe loop assumed and stopped\n");
216 atomic_dec(&kmod_concurrent);
220 pid = kernel_thread(exec_modprobe, (void*) module_name, 0);
222 printk(KERN_ERR "request_module[%s]: fork failed, errno %d\n", module_name, -pid);
223 atomic_dec(&kmod_concurrent);
227 /* Block everything but SIGKILL/SIGSTOP */
228 spin_lock_irq(¤t->sigmask_lock);
229 tmpsig = current->blocked;
230 siginitsetinv(¤t->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP));
231 recalc_sigpending(current);
232 spin_unlock_irq(¤t->sigmask_lock);
234 waitpid_result = waitpid(pid, NULL, __WCLONE);
235 atomic_dec(&kmod_concurrent);
237 /* Allow signals again.. */
238 spin_lock_irq(¤t->sigmask_lock);
239 current->blocked = tmpsig;
240 recalc_sigpending(current);
241 spin_unlock_irq(¤t->sigmask_lock);
243 if (waitpid_result != pid) {
244 printk(KERN_ERR "request_module[%s]: waitpid(%d,...) failed, errno %d\n",
245 module_name, pid, -waitpid_result);
249 #endif /* CONFIG_KMOD */
252 #ifdef CONFIG_HOTPLUG
254 hotplug path is set via /proc/sys
255 invoked by hotplug-aware bus drivers,
256 with exec_usermodehelper and some thread-spawner
258 argv [0] = hotplug_path;
259 argv [1] = "usb", "scsi", "pci", "network", etc;
260 ... plus optional type-specific parameters
263 envp [*] = HOME, PATH; optional type-specific parameters
265 a hotplug bus should invoke this for device add/remove
266 events. the command is expected to load drivers when
267 necessary, and may perform additional system setup.
269 char hotplug_path[256] = "/sbin/hotplug";
271 EXPORT_SYMBOL(hotplug_path);
273 #endif /* CONFIG_HOTPLUG */
275 struct subprocess_info {
276 struct completion *complete;
284 * This is the task which runs the usermode application
286 static int ____call_usermodehelper(void *data)
288 struct subprocess_info *sub_info = data;
292 if (current->fs->root)
293 retval = exec_usermodehelper(sub_info->path, sub_info->argv, sub_info->envp);
296 sub_info->retval = (pid_t)retval;
301 * This is run by keventd.
303 static void __call_usermodehelper(void *data)
305 struct subprocess_info *sub_info = data;
309 * CLONE_VFORK: wait until the usermode helper has execve'd successfully
310 * We need the data structures to stay around until that is done.
312 pid = kernel_thread(____call_usermodehelper, sub_info, CLONE_VFORK | SIGCHLD);
314 sub_info->retval = pid;
315 complete(sub_info->complete);
319 * call_usermodehelper - start a usermode application
320 * @path: pathname for the application
321 * @argv: null-terminated argument list
322 * @envp: null-terminated environment list
324 * Runs a user-space application. The application is started asynchronously. It
325 * runs as a child of keventd. It runs with full root capabilities. keventd silently
326 * reaps the child when it exits.
328 * Must be called from process context. Returns zero on success, else a negative
331 int call_usermodehelper(char *path, char **argv, char **envp)
333 DECLARE_COMPLETION(work);
334 struct subprocess_info sub_info = {
341 struct tq_struct tqs = {
342 routine: __call_usermodehelper,
349 if (current_is_keventd()) {
350 /* We can't wait on keventd! */
351 __call_usermodehelper(&sub_info);
354 wait_for_completion(&work);
357 return sub_info.retval;
361 * This is for the serialisation of device probe() functions
362 * against device open() functions
364 static DECLARE_MUTEX(dev_probe_sem);
366 void dev_probe_lock(void)
368 down(&dev_probe_sem);
371 void dev_probe_unlock(void)
376 EXPORT_SYMBOL(exec_usermodehelper);
377 EXPORT_SYMBOL(call_usermodehelper);
380 EXPORT_SYMBOL(request_module);