Merge with Linux 2.5.73.
[linux-2.6/linux-mips.git] / kernel / kmod.c
blobe011611afb7df0c29b5579a7f7229bdac42194be
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
15 Unblock all signals when we exec a usermode process.
16 Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
18 call_usermodehelper wait flag, and remove exec_usermodehelper.
19 Rusty Russell <rusty@rustcorp.com.au> Jan 2003
21 #define __KERNEL_SYSCALLS__
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/unistd.h>
27 #include <linux/kmod.h>
28 #include <linux/smp_lock.h>
29 #include <linux/slab.h>
30 #include <linux/namespace.h>
31 #include <linux/completion.h>
32 #include <linux/file.h>
33 #include <linux/workqueue.h>
34 #include <linux/security.h>
35 #include <linux/mount.h>
36 #include <linux/kernel.h>
37 #include <asm/uaccess.h>
39 extern int max_threads, system_running;
41 #ifdef CONFIG_KMOD
44 modprobe_path is set via /proc/sys.
46 char modprobe_path[256] = "/sbin/modprobe";
48 /**
49 * request_module - try to load a kernel module
50 * @module_name: Name of module
52 * Load a module using the user mode module loader. The function returns
53 * zero on success or a negative errno code on failure. Note that a
54 * successful module load does not mean the module did not then unload
55 * and exit on an error of its own. Callers must check that the service
56 * they requested is now available not blindly invoke it.
58 * If module auto-loading support is disabled then this function
59 * becomes a no-operation.
61 int request_module(const char *fmt, ...)
63 #define MODULENAME_SIZE 32
64 va_list args;
65 char module_name[MODULENAME_SIZE];
66 unsigned int max_modprobes;
67 int ret;
68 char *argv[] = { modprobe_path, "--", module_name, NULL };
69 static char *envp[] = { "HOME=/",
70 "TERM=linux",
71 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
72 NULL };
73 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
74 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
75 static int kmod_loop_msg;
77 va_start(args, fmt);
78 ret = vsnprintf(module_name, MODULENAME_SIZE, fmt, args);
79 va_end(args);
80 if (ret >= MODULENAME_SIZE)
81 return -ENAMETOOLONG;
83 /* If modprobe needs a service that is in a module, we get a recursive
84 * loop. Limit the number of running kmod threads to max_threads/2 or
85 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
86 * would be to run the parents of this process, counting how many times
87 * kmod was invoked. That would mean accessing the internals of the
88 * process tables to get the command line, proc_pid_cmdline is static
89 * and it is not worth changing the proc code just to handle this case.
90 * KAO.
92 * "trace the ppid" is simple, but will fail if someone's
93 * parent exits. I think this is as good as it gets. --RR
95 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
96 atomic_inc(&kmod_concurrent);
97 if (atomic_read(&kmod_concurrent) > max_modprobes) {
98 /* We may be blaming an innocent here, but unlikely */
99 if (kmod_loop_msg++ < 5)
100 printk(KERN_ERR
101 "request_module: runaway loop modprobe %s\n",
102 module_name);
103 atomic_dec(&kmod_concurrent);
104 return -ENOMEM;
107 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
108 if (ret != 0) {
109 static unsigned long last;
110 unsigned long now = jiffies;
111 if (now - last > HZ) {
112 last = now;
113 printk(KERN_DEBUG
114 "request_module: failed %s -- %s. error = %d\n",
115 modprobe_path, module_name, ret);
118 atomic_dec(&kmod_concurrent);
119 return ret;
121 #endif /* CONFIG_KMOD */
123 #ifdef CONFIG_HOTPLUG
125 hotplug path is set via /proc/sys
126 invoked by hotplug-aware bus drivers,
127 with call_usermodehelper
129 argv [0] = hotplug_path;
130 argv [1] = "usb", "scsi", "pci", "network", etc;
131 ... plus optional type-specific parameters
132 argv [n] = 0;
134 envp [*] = HOME, PATH; optional type-specific parameters
136 a hotplug bus should invoke this for device add/remove
137 events. the command is expected to load drivers when
138 necessary, and may perform additional system setup.
140 char hotplug_path[256] = "/sbin/hotplug";
142 EXPORT_SYMBOL(hotplug_path);
144 #endif /* CONFIG_HOTPLUG */
146 struct subprocess_info {
147 struct completion *complete;
148 char *path;
149 char **argv;
150 char **envp;
151 int wait;
152 int retval;
156 * This is the task which runs the usermode application
158 static int ____call_usermodehelper(void *data)
160 struct subprocess_info *sub_info = data;
161 int retval;
163 /* Unblock all signals. */
164 flush_signals(current);
165 spin_lock_irq(&current->sighand->siglock);
166 flush_signal_handlers(current, 1);
167 sigemptyset(&current->blocked);
168 recalc_sigpending();
169 spin_unlock_irq(&current->sighand->siglock);
171 retval = -EPERM;
172 if (current->fs->root)
173 retval = execve(sub_info->path, sub_info->argv,sub_info->envp);
175 /* Exec failed? */
176 sub_info->retval = retval;
177 do_exit(0);
180 /* Keventd can't block, but this (a child) can. */
181 static int wait_for_helper(void *data)
183 struct subprocess_info *sub_info = data;
184 pid_t pid;
186 sub_info->retval = 0;
187 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
188 if (pid < 0)
189 sub_info->retval = pid;
190 else
191 /* We don't have a SIGCHLD signal handler, so this
192 * always returns -ECHILD, but the important thing is
193 * that it blocks. */
194 sys_wait4(pid, NULL, 0, NULL);
196 complete(sub_info->complete);
197 return 0;
201 * This is run by keventd.
203 static void __call_usermodehelper(void *data)
205 struct subprocess_info *sub_info = data;
206 pid_t pid;
208 /* CLONE_VFORK: wait until the usermode helper has execve'd
209 * successfully We need the data structures to stay around
210 * until that is done. */
211 if (sub_info->wait)
212 pid = kernel_thread(wait_for_helper, sub_info,
213 CLONE_KERNEL | SIGCHLD);
214 else
215 pid = kernel_thread(____call_usermodehelper, sub_info,
216 CLONE_VFORK | SIGCHLD);
218 if (pid < 0) {
219 sub_info->retval = pid;
220 complete(sub_info->complete);
221 } else if (!sub_info->wait)
222 complete(sub_info->complete);
226 * call_usermodehelper - start a usermode application
227 * @path: pathname for the application
228 * @argv: null-terminated argument list
229 * @envp: null-terminated environment list
230 * @wait: wait for the application to finish and return status.
232 * Runs a user-space application. The application is started
233 * asynchronously if wait is not set, and runs as a child of keventd.
234 * (ie. it runs with full root capabilities).
236 * Must be called from process context. Returns a negative error code
237 * if program was not execed successfully, or 0.
239 int call_usermodehelper(char *path, char **argv, char **envp, int wait)
241 DECLARE_COMPLETION(done);
242 struct subprocess_info sub_info = {
243 .complete = &done,
244 .path = path,
245 .argv = argv,
246 .envp = envp,
247 .wait = wait,
248 .retval = 0,
250 DECLARE_WORK(work, __call_usermodehelper, &sub_info);
252 if (!system_running)
253 return -EBUSY;
255 if (path[0] == '\0')
256 goto out;
258 if (current_is_keventd()) {
259 /* We can't wait on keventd! */
260 __call_usermodehelper(&sub_info);
261 } else {
262 schedule_work(&work);
263 wait_for_completion(&done);
265 out:
266 return sub_info.retval;
269 EXPORT_SYMBOL(call_usermodehelper);
271 #ifdef CONFIG_KMOD
272 EXPORT_SYMBOL(request_module);
273 #endif