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
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
;
44 modprobe_path is set via /proc/sys.
46 char modprobe_path
[256] = "/sbin/modprobe";
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
65 char module_name
[MODULENAME_SIZE
];
66 unsigned int max_modprobes
;
68 char *argv
[] = { modprobe_path
, "--", module_name
, NULL
};
69 static char *envp
[] = { "HOME=/",
71 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
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
;
78 ret
= vsnprintf(module_name
, MODULENAME_SIZE
, fmt
, args
);
80 if (ret
>= MODULENAME_SIZE
)
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.
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)
101 "request_module: runaway loop modprobe %s\n",
103 atomic_dec(&kmod_concurrent
);
107 ret
= call_usermodehelper(modprobe_path
, argv
, envp
, 1);
109 static unsigned long last
;
110 unsigned long now
= jiffies
;
111 if (now
- last
> HZ
) {
114 "request_module: failed %s -- %s. error = %d\n",
115 modprobe_path
, module_name
, ret
);
118 atomic_dec(&kmod_concurrent
);
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
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
;
156 * This is the task which runs the usermode application
158 static int ____call_usermodehelper(void *data
)
160 struct subprocess_info
*sub_info
= data
;
163 /* Unblock all signals. */
164 flush_signals(current
);
165 spin_lock_irq(¤t
->sighand
->siglock
);
166 flush_signal_handlers(current
, 1);
167 sigemptyset(¤t
->blocked
);
169 spin_unlock_irq(¤t
->sighand
->siglock
);
172 if (current
->fs
->root
)
173 retval
= execve(sub_info
->path
, sub_info
->argv
,sub_info
->envp
);
176 sub_info
->retval
= retval
;
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
;
186 sub_info
->retval
= 0;
187 pid
= kernel_thread(____call_usermodehelper
, sub_info
, SIGCHLD
);
189 sub_info
->retval
= pid
;
191 /* We don't have a SIGCHLD signal handler, so this
192 * always returns -ECHILD, but the important thing is
194 sys_wait4(pid
, NULL
, 0, NULL
);
196 complete(sub_info
->complete
);
201 * This is run by keventd.
203 static void __call_usermodehelper(void *data
)
205 struct subprocess_info
*sub_info
= data
;
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. */
212 pid
= kernel_thread(wait_for_helper
, sub_info
,
213 CLONE_KERNEL
| SIGCHLD
);
215 pid
= kernel_thread(____call_usermodehelper
, sub_info
,
216 CLONE_VFORK
| SIGCHLD
);
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
= {
250 DECLARE_WORK(work
, __call_usermodehelper
, &sub_info
);
258 if (current_is_keventd()) {
259 /* We can't wait on keventd! */
260 __call_usermodehelper(&sub_info
);
262 schedule_work(&work
);
263 wait_for_completion(&done
);
266 return sub_info
.retval
;
269 EXPORT_SYMBOL(call_usermodehelper
);
272 EXPORT_SYMBOL(request_module
);