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>
30 #include <linux/file.h>
31 #include <linux/workqueue.h>
33 #include <asm/uaccess.h>
35 extern int max_threads
, system_running
;
38 use_init_fs_context(void)
40 struct fs_struct
*our_fs
, *init_fs
;
41 struct dentry
*root
, *pwd
;
42 struct vfsmount
*rootmnt
, *pwdmnt
;
43 struct namespace *our_ns
, *init_ns
;
46 * Make modprobe's fs context be a copy of init's.
48 * We cannot use the user's fs context, because it
49 * may have a different root than init.
50 * Since init was created with CLONE_FS, we can grab
51 * its fs context from "init_task".
53 * The fs context has to be a copy. If it is shared
54 * with init, then any chdir() call in modprobe will
55 * also affect init and the other threads sharing
56 * init_task's fs context.
58 * We created the exec_modprobe thread without CLONE_FS,
59 * so we can update the fields in our fs context freely.
62 init_fs
= init_task
.fs
;
63 init_ns
= init_task
.namespace;
64 get_namespace(init_ns
);
65 our_ns
= current
->namespace;
66 current
->namespace = init_ns
;
67 put_namespace(our_ns
);
68 read_lock(&init_fs
->lock
);
69 rootmnt
= mntget(init_fs
->rootmnt
);
70 root
= dget(init_fs
->root
);
71 pwdmnt
= mntget(init_fs
->pwdmnt
);
72 pwd
= dget(init_fs
->pwd
);
73 read_unlock(&init_fs
->lock
);
75 /* FIXME - unsafe ->fs access */
77 our_fs
->umask
= init_fs
->umask
;
78 set_fs_root(our_fs
, rootmnt
, root
);
79 set_fs_pwd(our_fs
, pwdmnt
, pwd
);
80 write_lock(&our_fs
->lock
);
81 if (our_fs
->altroot
) {
82 struct vfsmount
*mnt
= our_fs
->altrootmnt
;
83 struct dentry
*dentry
= our_fs
->altroot
;
84 our_fs
->altrootmnt
= NULL
;
85 our_fs
->altroot
= NULL
;
86 write_unlock(&our_fs
->lock
);
90 write_unlock(&our_fs
->lock
);
97 int exec_usermodehelper(char *program_path
, char *argv
[], char *envp
[])
100 struct task_struct
*curtask
= current
;
102 curtask
->session
= 1;
105 use_init_fs_context();
107 /* Prevent parent user process from sending signals to child.
108 Otherwise, if the modprobe program does not exist, it might
109 be possible to get a user defined signal handler to execute
110 as the super user right after the execve fails if you time
111 the signal just right.
113 spin_lock_irq(&curtask
->sig
->siglock
);
114 sigemptyset(&curtask
->blocked
);
115 flush_signals(curtask
);
116 flush_signal_handlers(curtask
);
118 spin_unlock_irq(&curtask
->sig
->siglock
);
120 for (i
= 0; i
< curtask
->files
->max_fds
; i
++ ) {
121 if (curtask
->files
->fd
[i
]) close(i
);
124 /* Drop the "current user" thing */
126 struct user_struct
*user
= curtask
->user
;
127 curtask
->user
= INIT_USER
;
128 atomic_inc(&INIT_USER
->__count
);
129 atomic_inc(&INIT_USER
->processes
);
130 atomic_dec(&user
->processes
);
134 /* Give kmod all effective privileges.. */
135 curtask
->euid
= curtask
->fsuid
= 0;
136 curtask
->egid
= curtask
->fsgid
= 0;
137 security_ops
->task_kmod_set_label();
139 /* Allow execve args to be in kernel space. */
143 if (execve(program_path
, argv
, envp
) < 0)
151 modprobe_path is set via /proc/sys.
153 char modprobe_path
[256] = "/sbin/modprobe";
155 static int exec_modprobe(void * module_name
)
157 static char * envp
[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL
};
158 char *argv
[] = { modprobe_path
, "-s", "-k", "--", (char*)module_name
, NULL
};
164 ret
= exec_usermodehelper(modprobe_path
, argv
, envp
);
166 static unsigned long last
;
167 unsigned long now
= jiffies
;
168 if (now
- last
> HZ
) {
171 "kmod: failed to exec %s -s -k %s, errno = %d\n",
172 modprobe_path
, (char*) module_name
, errno
);
179 * request_module - try to load a kernel module
180 * @module_name: Name of module
182 * Load a module using the user mode module loader. The function returns
183 * zero on success or a negative errno code on failure. Note that a
184 * successful module load does not mean the module did not then unload
185 * and exit on an error of its own. Callers must check that the service
186 * they requested is now available not blindly invoke it.
188 * If module auto-loading support is disabled then this function
189 * becomes a no-operation.
191 int request_module(const char * module_name
)
197 static atomic_t kmod_concurrent
= ATOMIC_INIT(0);
198 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
199 static int kmod_loop_msg
;
200 unsigned long saved_policy
= current
->policy
;
202 current
->policy
= SCHED_NORMAL
;
203 /* Don't allow request_module() when the system isn't set up */
204 if ( ! system_running
) {
205 printk(KERN_ERR
"request_module[%s]: not ready\n", module_name
);
210 /* If modprobe needs a service that is in a module, we get a recursive
211 * loop. Limit the number of running kmod threads to max_threads/2 or
212 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
213 * would be to run the parents of this process, counting how many times
214 * kmod was invoked. That would mean accessing the internals of the
215 * process tables to get the command line, proc_pid_cmdline is static
216 * and it is not worth changing the proc code just to handle this case.
220 if (i
> MAX_KMOD_CONCURRENT
)
221 i
= MAX_KMOD_CONCURRENT
;
222 atomic_inc(&kmod_concurrent
);
223 if (atomic_read(&kmod_concurrent
) > i
) {
224 if (kmod_loop_msg
++ < 5)
226 "kmod: runaway modprobe loop assumed and stopped\n");
227 atomic_dec(&kmod_concurrent
);
232 pid
= kernel_thread(exec_modprobe
, (void*) module_name
, 0);
234 printk(KERN_ERR
"request_module[%s]: fork failed, errno %d\n", module_name
, -pid
);
235 atomic_dec(&kmod_concurrent
);
240 /* Block everything but SIGKILL/SIGSTOP */
241 spin_lock_irq(¤t
->sig
->siglock
);
242 tmpsig
= current
->blocked
;
243 siginitsetinv(¤t
->blocked
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
245 spin_unlock_irq(¤t
->sig
->siglock
);
247 waitpid_result
= waitpid(pid
, NULL
, __WCLONE
);
248 atomic_dec(&kmod_concurrent
);
250 /* Allow signals again.. */
251 spin_lock_irq(¤t
->sig
->siglock
);
252 current
->blocked
= tmpsig
;
254 spin_unlock_irq(¤t
->sig
->siglock
);
256 if (waitpid_result
!= pid
) {
257 printk(KERN_ERR
"request_module[%s]: waitpid(%d,...) failed, errno %d\n",
258 module_name
, pid
, -waitpid_result
);
262 current
->policy
= saved_policy
;
265 #endif /* CONFIG_KMOD */
268 #ifdef CONFIG_HOTPLUG
270 hotplug path is set via /proc/sys
271 invoked by hotplug-aware bus drivers,
272 with exec_usermodehelper and some thread-spawner
274 argv [0] = hotplug_path;
275 argv [1] = "usb", "scsi", "pci", "network", etc;
276 ... plus optional type-specific parameters
279 envp [*] = HOME, PATH; optional type-specific parameters
281 a hotplug bus should invoke this for device add/remove
282 events. the command is expected to load drivers when
283 necessary, and may perform additional system setup.
285 char hotplug_path
[256] = "/sbin/hotplug";
287 EXPORT_SYMBOL(hotplug_path
);
289 #endif /* CONFIG_HOTPLUG */
291 struct subprocess_info
{
292 struct completion
*complete
;
300 * This is the task which runs the usermode application
302 static int ____call_usermodehelper(void *data
)
304 struct subprocess_info
*sub_info
= data
;
308 if (current
->fs
->root
)
309 retval
= exec_usermodehelper(sub_info
->path
, sub_info
->argv
, sub_info
->envp
);
312 sub_info
->retval
= (pid_t
)retval
;
317 * This is run by keventd.
319 static void __call_usermodehelper(void *data
)
321 struct subprocess_info
*sub_info
= data
;
325 * CLONE_VFORK: wait until the usermode helper has execve'd successfully
326 * We need the data structures to stay around until that is done.
328 pid
= kernel_thread(____call_usermodehelper
, sub_info
, CLONE_VFORK
| SIGCHLD
);
330 sub_info
->retval
= pid
;
331 complete(sub_info
->complete
);
335 * call_usermodehelper - start a usermode application
336 * @path: pathname for the application
337 * @argv: null-terminated argument list
338 * @envp: null-terminated environment list
340 * Runs a user-space application. The application is started asynchronously. It
341 * runs as a child of keventd. It runs with full root capabilities. keventd silently
342 * reaps the child when it exits.
344 * Must be called from process context. Returns zero on success, else a negative
347 int call_usermodehelper(char *path
, char **argv
, char **envp
)
349 DECLARE_COMPLETION(done
);
350 struct subprocess_info sub_info
= {
357 DECLARE_WORK(work
, __call_usermodehelper
, &sub_info
);
365 if (current_is_keventd()) {
366 /* We can't wait on keventd! */
367 __call_usermodehelper(&sub_info
);
369 schedule_work(&work
);
370 wait_for_completion(&done
);
373 return sub_info
.retval
;
377 * This is for the serialisation of device probe() functions
378 * against device open() functions
380 static DECLARE_MUTEX(dev_probe_sem
);
382 void dev_probe_lock(void)
384 down(&dev_probe_sem
);
387 void dev_probe_unlock(void)
392 EXPORT_SYMBOL(exec_usermodehelper
);
393 EXPORT_SYMBOL(call_usermodehelper
);
396 EXPORT_SYMBOL(request_module
);