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
;
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 * @fmt: printf style format string for the name of the module
51 * @varargs: arguements as specified in the format string
53 * Load a module using the user mode module loader. The function returns
54 * zero on success or a negative errno code on failure. Note that a
55 * successful module load does not mean the module did not then unload
56 * and exit on an error of its own. Callers must check that the service
57 * they requested is now available not blindly invoke it.
59 * If module auto-loading support is disabled then this function
60 * becomes a no-operation.
62 int request_module(const char *fmt
, ...)
65 char module_name
[MODULE_NAME_LEN
];
66 unsigned int max_modprobes
;
68 char *argv
[] = { modprobe_path
, "-q", "--", 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
, MODULE_NAME_LEN
, fmt
, args
);
80 if (ret
>= MODULE_NAME_LEN
)
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
;
185 struct k_sigaction sa
;
187 /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
188 * populate the status, but will return -ECHILD. */
189 sa
.sa
.sa_handler
= SIG_IGN
;
191 siginitset(&sa
.sa
.sa_mask
, sigmask(SIGCHLD
));
192 do_sigaction(SIGCHLD
, &sa
, (struct k_sigaction
*)0);
193 allow_signal(SIGCHLD
);
195 pid
= kernel_thread(____call_usermodehelper
, sub_info
, SIGCHLD
);
197 sub_info
->retval
= pid
;
199 sys_wait4(pid
, &sub_info
->retval
, 0, NULL
);
201 complete(sub_info
->complete
);
206 * This is run by keventd.
208 static void __call_usermodehelper(void *data
)
210 struct subprocess_info
*sub_info
= data
;
213 /* CLONE_VFORK: wait until the usermode helper has execve'd
214 * successfully We need the data structures to stay around
215 * until that is done. */
217 pid
= kernel_thread(wait_for_helper
, sub_info
,
218 CLONE_FS
| CLONE_FILES
| SIGCHLD
);
220 pid
= kernel_thread(____call_usermodehelper
, sub_info
,
221 CLONE_VFORK
| SIGCHLD
);
224 sub_info
->retval
= pid
;
225 complete(sub_info
->complete
);
226 } else if (!sub_info
->wait
)
227 complete(sub_info
->complete
);
231 * call_usermodehelper - start a usermode application
232 * @path: pathname for the application
233 * @argv: null-terminated argument list
234 * @envp: null-terminated environment list
235 * @wait: wait for the application to finish and return status.
237 * Runs a user-space application. The application is started
238 * asynchronously if wait is not set, and runs as a child of keventd.
239 * (ie. it runs with full root capabilities).
241 * Must be called from process context. Returns a negative error code
242 * if program was not execed successfully, or 0.
244 int call_usermodehelper(char *path
, char **argv
, char **envp
, int wait
)
246 DECLARE_COMPLETION(done
);
247 struct subprocess_info sub_info
= {
255 DECLARE_WORK(work
, __call_usermodehelper
, &sub_info
);
263 if (current_is_keventd()) {
264 /* We can't wait on keventd! */
265 __call_usermodehelper(&sub_info
);
267 schedule_work(&work
);
268 wait_for_completion(&done
);
271 return sub_info
.retval
;
274 EXPORT_SYMBOL(call_usermodehelper
);
277 EXPORT_SYMBOL(request_module
);