Linux 2.4.0-test10pre3
[davej-history.git] / kernel / kmod.c
blob3fff3ed3d902a8bef1268edecd7ee8aa119f117e
1 /*
2 kmod, the new module loader (replaces kerneld)
3 Kirk Petersen
5 Reorganized not to be a daemon by Adam Richter, with guidance
6 from Greg Zornetzer.
8 Modified to avoid chroot and file sharing problems.
9 Mikael Pettersson
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/config.h>
19 #include <linux/sched.h>
20 #include <linux/unistd.h>
21 #include <linux/smp_lock.h>
23 #include <asm/uaccess.h>
26 modprobe_path is set via /proc/sys.
28 char modprobe_path[256] = "/sbin/modprobe";
30 extern int max_threads;
32 static inline void
33 use_init_fs_context(void)
35 struct fs_struct *our_fs, *init_fs;
36 struct dentry *root, *pwd;
37 struct vfsmount *rootmnt, *pwdmnt;
40 * Make modprobe's fs context be a copy of init's.
42 * We cannot use the user's fs context, because it
43 * may have a different root than init.
44 * Since init was created with CLONE_FS, we can grab
45 * its fs context from "init_task".
47 * The fs context has to be a copy. If it is shared
48 * with init, then any chdir() call in modprobe will
49 * also affect init and the other threads sharing
50 * init_task's fs context.
52 * We created the exec_modprobe thread without CLONE_FS,
53 * so we can update the fields in our fs context freely.
56 init_fs = init_task.fs;
57 read_lock(&init_fs->lock);
58 rootmnt = mntget(init_fs->rootmnt);
59 root = dget(init_fs->root);
60 pwdmnt = mntget(init_fs->pwdmnt);
61 pwd = dget(init_fs->pwd);
62 read_unlock(&init_fs->lock);
64 /* FIXME - unsafe ->fs access */
65 our_fs = current->fs;
66 our_fs->umask = init_fs->umask;
67 set_fs_root(our_fs, rootmnt, root);
68 set_fs_pwd(our_fs, pwdmnt, pwd);
69 write_lock(&our_fs->lock);
70 if (our_fs->altroot) {
71 struct vfsmount *mnt = our_fs->altrootmnt;
72 struct dentry *dentry = our_fs->altroot;
73 our_fs->altrootmnt = NULL;
74 our_fs->altroot = NULL;
75 write_unlock(&our_fs->lock);
76 dput(dentry);
77 mntput(mnt);
78 } else
79 write_unlock(&our_fs->lock);
80 dput(root);
81 mntput(rootmnt);
82 dput(pwd);
83 mntput(pwdmnt);
86 int exec_usermodehelper(char *program_path, char *argv[], char *envp[])
88 int i;
90 current->session = 1;
91 current->pgrp = 1;
93 use_init_fs_context();
95 /* Prevent parent user process from sending signals to child.
96 Otherwise, if the modprobe program does not exist, it might
97 be possible to get a user defined signal handler to execute
98 as the super user right after the execve fails if you time
99 the signal just right.
101 spin_lock_irq(&current->sigmask_lock);
102 flush_signals(current);
103 flush_signal_handlers(current);
104 spin_unlock_irq(&current->sigmask_lock);
106 for (i = 0; i < current->files->max_fds; i++ ) {
107 if (current->files->fd[i]) close(i);
110 /* Drop the "current user" thing */
112 struct user_struct *user = current->user;
113 current->user = INIT_USER;
114 atomic_inc(&INIT_USER->__count);
115 atomic_inc(&INIT_USER->processes);
116 atomic_dec(&user->processes);
117 free_uid(user);
120 /* Give kmod all effective privileges.. */
121 current->uid = current->euid = current->fsuid = 0;
122 cap_set_full(current->cap_effective);
124 /* Allow execve args to be in kernel space. */
125 set_fs(KERNEL_DS);
127 /* Go, go, go... */
128 if (execve(program_path, argv, envp) < 0)
129 return -errno;
130 return 0;
133 static int exec_modprobe(void * module_name)
135 static char * envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
136 char *argv[] = { modprobe_path, "-s", "-k", (char*)module_name, NULL };
137 int ret;
139 ret = exec_usermodehelper(modprobe_path, argv, envp);
140 if (ret) {
141 printk(KERN_ERR
142 "kmod: failed to exec %s -s -k %s, errno = %d\n",
143 modprobe_path, (char*) module_name, errno);
145 return ret;
149 * request_module - try to load a kernel module
150 * @module_name: Name of module
152 * Load a module using the user mode module loader. The function returns
153 * zero on success or a negative errno code on failure. Note that a
154 * successful module load does not mean the module did not then unload
155 * and exit on an error of its own. Callers must check that the service
156 * they requested is now available not blindly invoke it.
158 * If module auto-loading support is disabled then this function
159 * becomes a no-operation.
162 int request_module(const char * module_name)
164 int pid;
165 int waitpid_result;
166 sigset_t tmpsig;
167 int i;
168 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
169 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
170 static int kmod_loop_msg;
172 /* Don't allow request_module() before the root fs is mounted! */
173 if ( ! current->fs->root ) {
174 printk(KERN_ERR "request_module[%s]: Root fs not mounted\n",
175 module_name);
176 return -EPERM;
179 /* If modprobe needs a service that is in a module, we get a recursive
180 * loop. Limit the number of running kmod threads to max_threads/2 or
181 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
182 * would be to run the parents of this process, counting how many times
183 * kmod was invoked. That would mean accessing the internals of the
184 * process tables to get the command line, proc_pid_cmdline is static
185 * and it is not worth changing the proc code just to handle this case.
186 * KAO.
188 i = max_threads/2;
189 if (i > MAX_KMOD_CONCURRENT)
190 i = MAX_KMOD_CONCURRENT;
191 atomic_inc(&kmod_concurrent);
192 if (atomic_read(&kmod_concurrent) > i) {
193 if (kmod_loop_msg++ < 5)
194 printk(KERN_ERR
195 "kmod: runaway modprobe loop assumed and stopped\n");
196 atomic_dec(&kmod_concurrent);
197 return -ENOMEM;
200 pid = kernel_thread(exec_modprobe, (void*) module_name, 0);
201 if (pid < 0) {
202 printk(KERN_ERR "request_module[%s]: fork failed, errno %d\n", module_name, -pid);
203 atomic_dec(&kmod_concurrent);
204 return pid;
207 /* Block everything but SIGKILL/SIGSTOP */
208 spin_lock_irq(&current->sigmask_lock);
209 tmpsig = current->blocked;
210 siginitsetinv(&current->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP));
211 recalc_sigpending(current);
212 spin_unlock_irq(&current->sigmask_lock);
214 waitpid_result = waitpid(pid, NULL, __WCLONE);
215 atomic_dec(&kmod_concurrent);
217 /* Allow signals again.. */
218 spin_lock_irq(&current->sigmask_lock);
219 current->blocked = tmpsig;
220 recalc_sigpending(current);
221 spin_unlock_irq(&current->sigmask_lock);
223 if (waitpid_result != pid) {
224 printk(KERN_ERR "request_module[%s]: waitpid(%d,...) failed, errno %d\n",
225 module_name, pid, -waitpid_result);
227 return 0;
231 #ifdef CONFIG_HOTPLUG
233 hotplug path is set via /proc/sys
234 invoked by hotplug-aware bus drivers,
235 with exec_usermodehelper and some thread-spawner
237 argv [0] = hotplug_path;
238 argv [1] = "usb", "scsi", "pci", "network", etc;
239 ... plus optional type-specific parameters
240 argv [n] = 0;
242 envp [*] = HOME, PATH; optional type-specific parameters
244 a hotplug bus should invoke this for device add/remove
245 events. the command is expected to load drivers when
246 necessary, and may perform additional system setup.
248 char hotplug_path[256] = "/sbin/hotplug";
251 static int exec_helper (void *arg)
253 void **params = (void **) arg;
254 char *path = (char *) params [0];
255 char **argv = (char **) params [1];
256 char **envp = (char **) params [2];
257 return exec_usermodehelper (path, argv, envp);
261 int call_usermodehelper (char *path, char **argv, char **envp)
263 void *params [3] = { path, argv, envp };
264 int pid, pid2, retval;
265 mm_segment_t fs;
267 if ( ! current->fs->root ) {
268 printk(KERN_ERR "call_usermodehelper[%s]: no root fs\n",
269 path);
270 return -EPERM;
272 if ((pid = kernel_thread (exec_helper, (void *) params, 0)) < 0) {
273 printk(KERN_ERR "failed fork %s, errno = %d", argv [0], -pid);
274 return -1;
277 fs = get_fs ();
278 set_fs (KERNEL_DS);
279 pid2 = waitpid (pid, &retval, __WCLONE);
280 set_fs (fs);
282 if (pid2 != pid) {
283 printk(KERN_ERR "waitpid(%d) failed, %d\n", pid, pid2);
284 return -1;
286 return retval;
289 #endif