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
16 #define __KERNEL_SYSCALLS__
18 #include <linux/sched.h>
19 #include <linux/unistd.h>
20 #include <linux/smp_lock.h>
22 #include <asm/uaccess.h>
25 modprobe_path is set via /proc/sys.
27 char modprobe_path
[256] = "/sbin/modprobe";
29 extern int max_threads
;
32 use_init_fs_context(void)
34 struct fs_struct
*our_fs
, *init_fs
;
35 struct dentry
*root
, *pwd
;
36 struct vfsmount
*rootmnt
, *pwdmnt
;
39 * Make modprobe's fs context be a copy of init's.
41 * We cannot use the user's fs context, because it
42 * may have a different root than init.
43 * Since init was created with CLONE_FS, we can grab
44 * its fs context from "init_task".
46 * The fs context has to be a copy. If it is shared
47 * with init, then any chdir() call in modprobe will
48 * also affect init and the other threads sharing
49 * init_task's fs context.
51 * We created the exec_modprobe thread without CLONE_FS,
52 * so we can update the fields in our fs context freely.
55 init_fs
= init_task
.fs
;
56 read_lock(&init_fs
->lock
);
57 rootmnt
= mntget(init_fs
->rootmnt
);
58 root
= dget(init_fs
->root
);
59 pwdmnt
= mntget(init_fs
->pwdmnt
);
60 pwd
= dget(init_fs
->pwd
);
61 read_unlock(&init_fs
->lock
);
63 /* FIXME - unsafe ->fs access */
65 our_fs
->umask
= init_fs
->umask
;
66 set_fs_root(our_fs
, rootmnt
, root
);
67 set_fs_pwd(our_fs
, pwdmnt
, pwd
);
68 write_lock(&our_fs
->lock
);
69 if (our_fs
->altroot
) {
70 struct vfsmount
*mnt
= our_fs
->altrootmnt
;
71 struct dentry
*dentry
= our_fs
->altroot
;
72 our_fs
->altrootmnt
= NULL
;
73 our_fs
->altroot
= NULL
;
74 write_unlock(&our_fs
->lock
);
78 write_unlock(&our_fs
->lock
);
85 int exec_usermodehelper(char *program_path
, char *argv
[], char *envp
[])
92 use_init_fs_context();
94 /* Prevent parent user process from sending signals to child.
95 Otherwise, if the modprobe program does not exist, it might
96 be possible to get a user defined signal handler to execute
97 as the super user right after the execve fails if you time
98 the signal just right.
100 spin_lock_irq(¤t
->sigmask_lock
);
101 flush_signals(current
);
102 flush_signal_handlers(current
);
103 spin_unlock_irq(¤t
->sigmask_lock
);
105 for (i
= 0; i
< current
->files
->max_fds
; i
++ ) {
106 if (current
->files
->fd
[i
]) close(i
);
109 /* Drop the "current user" thing */
112 /* Give kmod all effective privileges.. */
113 current
->uid
= current
->euid
= current
->fsuid
= 0;
114 cap_set_full(current
->cap_effective
);
116 /* Allow execve args to be in kernel space. */
120 if (execve(program_path
, argv
, envp
) < 0)
125 static int exec_modprobe(void * module_name
)
127 static char * envp
[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL
};
128 char *argv
[] = { modprobe_path
, "-s", "-k", (char*)module_name
, NULL
};
131 ret
= exec_usermodehelper(modprobe_path
, argv
, envp
);
134 "kmod: failed to exec %s -s -k %s, errno = %d\n",
135 modprobe_path
, (char*) module_name
, errno
);
141 * request_module - try to load a kernel module
142 * @module_name: Name of module
144 * Load a module using the user mode module loader. The function returns
145 * zero on success or a negative errno code on failure. Note that a
146 * successful module load does not mean the module did not then unload
147 * and exit on an error of its own. Callers must check that the service
148 * they requested is now available not blindly invoke it.
150 * If module auto-loading support is disabled then this function
151 * becomes a no-operation.
154 int request_module(const char * module_name
)
160 static atomic_t kmod_concurrent
= ATOMIC_INIT(0);
161 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
162 static int kmod_loop_msg
;
164 /* Don't allow request_module() before the root fs is mounted! */
165 if ( ! current
->fs
->root
) {
166 printk(KERN_ERR
"request_module[%s]: Root fs not mounted\n",
171 /* If modprobe needs a service that is in a module, we get a recursive
172 * loop. Limit the number of running kmod threads to max_threads/2 or
173 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
174 * would be to run the parents of this process, counting how many times
175 * kmod was invoked. That would mean accessing the internals of the
176 * process tables to get the command line, proc_pid_cmdline is static
177 * and it is not worth changing the proc code just to handle this case.
181 if (i
> MAX_KMOD_CONCURRENT
)
182 i
= MAX_KMOD_CONCURRENT
;
183 atomic_inc(&kmod_concurrent
);
184 if (atomic_read(&kmod_concurrent
) > i
) {
185 if (kmod_loop_msg
++ < 5)
187 "kmod: runaway modprobe loop assumed and stopped\n");
188 atomic_dec(&kmod_concurrent
);
192 pid
= kernel_thread(exec_modprobe
, (void*) module_name
, 0);
194 printk(KERN_ERR
"request_module[%s]: fork failed, errno %d\n", module_name
, -pid
);
195 atomic_dec(&kmod_concurrent
);
199 /* Block everything but SIGKILL/SIGSTOP */
200 spin_lock_irq(¤t
->sigmask_lock
);
201 tmpsig
= current
->blocked
;
202 siginitsetinv(¤t
->blocked
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
203 recalc_sigpending(current
);
204 spin_unlock_irq(¤t
->sigmask_lock
);
206 waitpid_result
= waitpid(pid
, NULL
, __WCLONE
);
207 atomic_dec(&kmod_concurrent
);
209 /* Allow signals again.. */
210 spin_lock_irq(¤t
->sigmask_lock
);
211 current
->blocked
= tmpsig
;
212 recalc_sigpending(current
);
213 spin_unlock_irq(¤t
->sigmask_lock
);
215 if (waitpid_result
!= pid
) {
216 printk(KERN_ERR
"request_module[%s]: waitpid(%d,...) failed, errno %d\n",
217 module_name
, pid
, -waitpid_result
);
223 #ifdef CONFIG_HOTPLUG
225 hotplug path is set via /proc/sys
226 invoked by hotplug-aware bus drivers,
227 with exec_usermodehelper and some thread-spawner
229 argv [0] = hotplug_path;
230 argv [1] = "usb", "scsi", "pci", "network", etc;
231 ... plus optional type-specific parameters
234 envp [*] = HOME, PATH; optional type-specific parameters
236 a hotplug bus should invoke this for device add/remove
237 events. the command is expected to load drivers when
238 necessary, and may perform additional system setup.
240 char hotplug_path
[256] = "/sbin/hotplug";