Import 2.3.99pre10-1
[davej-history.git] / kernel / kmod.c
blobbbfaf2992d64760cb204c28e411d3ad61726221b
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/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;
31 static inline void
32 use_init_fs_context(void)
34 struct fs_struct *our_fs, *init_fs;
37 * Make modprobe's fs context be a copy of init's.
39 * We cannot use the user's fs context, because it
40 * may have a different root than init.
41 * Since init was created with CLONE_FS, we can grab
42 * its fs context from "init_task".
44 * The fs context has to be a copy. If it is shared
45 * with init, then any chdir() call in modprobe will
46 * also affect init and the other threads sharing
47 * init_task's fs context.
49 * We created the exec_modprobe thread without CLONE_FS,
50 * so we can update the fields in our fs context freely.
52 lock_kernel();
54 our_fs = current->fs;
55 init_fs = init_task.fs;
56 our_fs->umask = init_fs->umask;
57 set_fs_root(our_fs, init_fs->rootmnt, init_fs->root);
58 set_fs_pwd(our_fs, init_fs->pwdmnt, init_fs->pwd);
59 if (our_fs->altroot) {
60 struct vfsmount *mnt = our_fs->altrootmnt;
61 struct dentry *dentry = our_fs->altroot;
62 our_fs->altrootmnt = NULL;
63 our_fs->altroot = NULL;
64 dput(dentry);
65 mntput(mnt);
68 unlock_kernel();
71 int exec_usermodehelper(char *program_path, char *argv[], char *envp[])
73 int i;
75 current->session = 1;
76 current->pgrp = 1;
78 use_init_fs_context();
80 /* Prevent parent user process from sending signals to child.
81 Otherwise, if the modprobe program does not exist, it might
82 be possible to get a user defined signal handler to execute
83 as the super user right after the execve fails if you time
84 the signal just right.
86 spin_lock_irq(&current->sigmask_lock);
87 flush_signals(current);
88 flush_signal_handlers(current);
89 spin_unlock_irq(&current->sigmask_lock);
91 for (i = 0; i < current->files->max_fds; i++ ) {
92 if (current->files->fd[i]) close(i);
95 /* Drop the "current user" thing */
96 free_uid(current);
98 /* Give kmod all privileges.. */
99 current->uid = current->euid = current->fsuid = 0;
100 cap_set_full(current->cap_inheritable);
101 cap_set_full(current->cap_effective);
103 /* Allow execve args to be in kernel space. */
104 set_fs(KERNEL_DS);
106 /* Go, go, go... */
107 if (execve(program_path, argv, envp) < 0)
108 return -errno;
109 return 0;
112 static int exec_modprobe(void * module_name)
114 static char * envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
115 char *argv[] = { modprobe_path, "-s", "-k", (char*)module_name, NULL };
116 int ret;
118 ret = exec_usermodehelper(modprobe_path, argv, envp);
119 if (ret) {
120 printk(KERN_ERR
121 "kmod: failed to exec %s -s -k %s, errno = %d\n",
122 modprobe_path, (char*) module_name, errno);
124 return ret;
128 * request_module - try to load a kernel module
129 * @module_name: Name of module
131 * Load a module using the user mode module loader. The function returns
132 * zero on success or a negative errno code on failure. Note that a
133 * successful module load does not mean the module did not then unload
134 * and exit on an error of its own. Callers must check that the service
135 * they requested is now available not blindly invoke it.
137 * If module auto-loading support is disabled then this function
138 * becomes a no-operation.
141 int request_module(const char * module_name)
143 int pid;
144 int waitpid_result;
145 sigset_t tmpsig;
146 int i;
147 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
148 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
149 static int kmod_loop_msg;
151 /* Don't allow request_module() before the root fs is mounted! */
152 if ( ! current->fs->root ) {
153 printk(KERN_ERR "request_module[%s]: Root fs not mounted\n",
154 module_name);
155 return -EPERM;
158 /* If modprobe needs a service that is in a module, we get a recursive
159 * loop. Limit the number of running kmod threads to max_threads/2 or
160 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
161 * would be to run the parents of this process, counting how many times
162 * kmod was invoked. That would mean accessing the internals of the
163 * process tables to get the command line, proc_pid_cmdline is static
164 * and it is not worth changing the proc code just to handle this case.
165 * KAO.
167 i = max_threads/2;
168 if (i > MAX_KMOD_CONCURRENT)
169 i = MAX_KMOD_CONCURRENT;
170 atomic_inc(&kmod_concurrent);
171 if (atomic_read(&kmod_concurrent) > i) {
172 if (kmod_loop_msg++ < 5)
173 printk(KERN_ERR
174 "kmod: runaway modprobe loop assumed and stopped\n");
175 atomic_dec(&kmod_concurrent);
176 return -ENOMEM;
179 pid = kernel_thread(exec_modprobe, (void*) module_name, 0);
180 if (pid < 0) {
181 printk(KERN_ERR "request_module[%s]: fork failed, errno %d\n", module_name, -pid);
182 atomic_dec(&kmod_concurrent);
183 return pid;
186 /* Block everything but SIGKILL/SIGSTOP */
187 spin_lock_irq(&current->sigmask_lock);
188 tmpsig = current->blocked;
189 siginitsetinv(&current->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP));
190 recalc_sigpending(current);
191 spin_unlock_irq(&current->sigmask_lock);
193 waitpid_result = waitpid(pid, NULL, __WCLONE);
194 atomic_dec(&kmod_concurrent);
196 /* Allow signals again.. */
197 spin_lock_irq(&current->sigmask_lock);
198 current->blocked = tmpsig;
199 recalc_sigpending(current);
200 spin_unlock_irq(&current->sigmask_lock);
202 if (waitpid_result != pid) {
203 printk(KERN_ERR "request_module[%s]: waitpid(%d,...) failed, errno %d\n",
204 module_name, pid, -waitpid_result);
206 return 0;