Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[deliverable/linux.git] / kernel / kmod.c
CommitLineData
1da177e4
LT
1/*
2 kmod, the new module loader (replaces kerneld)
3 Kirk Petersen
4
5 Reorganized not to be a daemon by Adam Richter, with guidance
6 from Greg Zornetzer.
7
8 Modified to avoid chroot and file sharing problems.
9 Mikael Pettersson
10
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
14
15 Unblock all signals when we exec a usermode process.
16 Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
17
18 call_usermodehelper wait flag, and remove exec_usermodehelper.
19 Rusty Russell <rusty@rustcorp.com.au> Jan 2003
20*/
1da177e4
LT
21#include <linux/module.h>
22#include <linux/sched.h>
23#include <linux/syscalls.h>
24#include <linux/unistd.h>
25#include <linux/kmod.h>
26#include <linux/smp_lock.h>
27#include <linux/slab.h>
6b3286ed 28#include <linux/mnt_namespace.h>
1da177e4
LT
29#include <linux/completion.h>
30#include <linux/file.h>
31#include <linux/workqueue.h>
32#include <linux/security.h>
33#include <linux/mount.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
d025c9db 36#include <linux/resource.h>
1da177e4
LT
37#include <asm/uaccess.h>
38
c353c3fb
KS
39extern int delete_module(const char *name, unsigned int flags);
40
1da177e4
LT
41extern int max_threads;
42
43static struct workqueue_struct *khelper_wq;
44
45#ifdef CONFIG_KMOD
46
47/*
48 modprobe_path is set via /proc/sys.
49*/
50char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
c353c3fb 51struct module_kobject kmod_mk;
1da177e4
LT
52
53/**
54 * request_module - try to load a kernel module
55 * @fmt: printf style format string for the name of the module
56 * @varargs: arguements as specified in the format string
57 *
58 * Load a module using the user mode module loader. The function returns
59 * zero on success or a negative errno code on failure. Note that a
60 * successful module load does not mean the module did not then unload
61 * and exit on an error of its own. Callers must check that the service
62 * they requested is now available not blindly invoke it.
63 *
64 * If module auto-loading support is disabled then this function
65 * becomes a no-operation.
66 */
67int request_module(const char *fmt, ...)
68{
69 va_list args;
70 char module_name[MODULE_NAME_LEN];
71 unsigned int max_modprobes;
72 int ret;
73 char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
74 static char *envp[] = { "HOME=/",
75 "TERM=linux",
76 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
77 NULL };
78 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
79#define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
80 static int kmod_loop_msg;
c353c3fb
KS
81 char modalias[16 + MODULE_NAME_LEN] = "MODALIAS=";
82 char *uevent_envp[2] = {
83 modalias,
84 NULL
85 };
1da177e4
LT
86
87 va_start(args, fmt);
88 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
89 va_end(args);
90 if (ret >= MODULE_NAME_LEN)
91 return -ENAMETOOLONG;
92
c353c3fb
KS
93 strcpy(&modalias[strlen("MODALIAS=")], module_name);
94 kobject_uevent_env(&kmod_mk.kobj, KOBJ_CHANGE, uevent_envp);
95
96 if (modprobe_path[0] == '\0')
97 goto out;
98
1da177e4
LT
99 /* If modprobe needs a service that is in a module, we get a recursive
100 * loop. Limit the number of running kmod threads to max_threads/2 or
101 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
102 * would be to run the parents of this process, counting how many times
103 * kmod was invoked. That would mean accessing the internals of the
104 * process tables to get the command line, proc_pid_cmdline is static
105 * and it is not worth changing the proc code just to handle this case.
106 * KAO.
107 *
108 * "trace the ppid" is simple, but will fail if someone's
109 * parent exits. I think this is as good as it gets. --RR
110 */
111 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
112 atomic_inc(&kmod_concurrent);
113 if (atomic_read(&kmod_concurrent) > max_modprobes) {
114 /* We may be blaming an innocent here, but unlikely */
115 if (kmod_loop_msg++ < 5)
116 printk(KERN_ERR
117 "request_module: runaway loop modprobe %s\n",
118 module_name);
119 atomic_dec(&kmod_concurrent);
120 return -ENOMEM;
121 }
122
123 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
124 atomic_dec(&kmod_concurrent);
c353c3fb 125out:
1da177e4
LT
126 return ret;
127}
128EXPORT_SYMBOL(request_module);
c353c3fb
KS
129
130static ssize_t store_mod_request(struct module_attribute *mattr,
131 struct module *mod,
132 const char *buffer, size_t count)
133{
134 char name[MODULE_NAME_LEN];
135 int ret;
136
137 if (count < 1 || count+1 > MODULE_NAME_LEN)
138 return -EINVAL;
139 memcpy(name, buffer, count);
140 name[count] = '\0';
141 if (name[count-1] == '\n')
142 name[count-1] = '\0';
143
144 ret = request_module(name);
145 if (ret < 0)
146 return ret;
147 return count;
148}
149
150static struct module_attribute mod_request = {
151 .attr = { .name = "mod_request", .mode = S_IWUSR, .owner = THIS_MODULE },
152 .store = store_mod_request,
153};
154
155#ifdef CONFIG_MODULE_UNLOAD
156static ssize_t store_mod_unload(struct module_attribute *mattr,
157 struct module *mod,
158 const char *buffer, size_t count)
159{
160 char name[MODULE_NAME_LEN];
161 int ret;
162
163 if (count < 1 || count+1 > MODULE_NAME_LEN)
164 return -EINVAL;
165 memcpy(name, buffer, count);
166 name[count] = '\0';
167 if (name[count-1] == '\n')
168 name[count-1] = '\0';
169
170 ret = delete_module(name, O_NONBLOCK);
171 if (ret < 0)
172 return ret;
173 return count;
174}
175
176static struct module_attribute mod_unload = {
177 .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE },
178 .store = store_mod_unload,
179};
180#endif
181
182static ssize_t show_mod_request_helper(struct module_attribute *mattr,
183 struct module *mod,
184 char *buffer)
185{
186 return sprintf(buffer, "%s\n", modprobe_path);
187}
188
189static ssize_t store_mod_request_helper(struct module_attribute *mattr,
190 struct module *mod,
191 const char *buffer, size_t count)
192{
193 if (count < 1 || count+1 > KMOD_PATH_LEN)
194 return -EINVAL;
195 memcpy(modprobe_path, buffer, count);
196 modprobe_path[count] = '\0';
197 if (modprobe_path[count-1] == '\n')
198 modprobe_path[count-1] = '\0';
199 return count;
200}
201
202static struct module_attribute mod_request_helper = {
203 .attr = {
204 .name = "mod_request_helper",
205 .mode = S_IWUSR | S_IRUGO,
206 .owner = THIS_MODULE
207 },
208 .show = show_mod_request_helper,
209 .store = store_mod_request_helper,
210};
211
212void __init kmod_sysfs_init(void)
213{
214 int ret;
215
216 kmod_mk.mod = THIS_MODULE;
217 kobj_set_kset_s(&kmod_mk, module_subsys);
218 kobject_set_name(&kmod_mk.kobj, "kmod");
219 kobject_init(&kmod_mk.kobj);
220 ret = kobject_add(&kmod_mk.kobj);
221 if (ret < 0)
222 goto out;
223
224 ret = sysfs_create_file(&kmod_mk.kobj, &mod_request_helper.attr);
225 ret = sysfs_create_file(&kmod_mk.kobj, &mod_request.attr);
226#ifdef CONFIG_MODULE_UNLOAD
227 ret = sysfs_create_file(&kmod_mk.kobj, &mod_unload.attr);
228#endif
229
230 kobject_uevent(&kmod_mk.kobj, KOBJ_ADD);
231out:
232 return;
233}
1da177e4
LT
234#endif /* CONFIG_KMOD */
235
236struct subprocess_info {
65f27f38 237 struct work_struct work;
1da177e4
LT
238 struct completion *complete;
239 char *path;
240 char **argv;
241 char **envp;
7888e7ff 242 struct key *ring;
1da177e4
LT
243 int wait;
244 int retval;
e239ca54 245 struct file *stdin;
1da177e4
LT
246};
247
248/*
249 * This is the task which runs the usermode application
250 */
251static int ____call_usermodehelper(void *data)
252{
253 struct subprocess_info *sub_info = data;
20e1129a 254 struct key *new_session, *old_session;
1da177e4
LT
255 int retval;
256
7888e7ff 257 /* Unblock all signals and set the session keyring. */
20e1129a 258 new_session = key_get(sub_info->ring);
1da177e4
LT
259 flush_signals(current);
260 spin_lock_irq(&current->sighand->siglock);
20e1129a 261 old_session = __install_session_keyring(current, new_session);
1da177e4
LT
262 flush_signal_handlers(current, 1);
263 sigemptyset(&current->blocked);
264 recalc_sigpending();
265 spin_unlock_irq(&current->sighand->siglock);
266
7888e7ff
DH
267 key_put(old_session);
268
e239ca54
AK
269 /* Install input pipe when needed */
270 if (sub_info->stdin) {
271 struct files_struct *f = current->files;
272 struct fdtable *fdt;
273 /* no races because files should be private here */
274 sys_close(0);
275 fd_install(0, sub_info->stdin);
276 spin_lock(&f->file_lock);
277 fdt = files_fdtable(f);
278 FD_SET(0, fdt->open_fds);
279 FD_CLR(0, fdt->close_on_exec);
280 spin_unlock(&f->file_lock);
d025c9db
AK
281
282 /* and disallow core files too */
283 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
e239ca54
AK
284 }
285
1da177e4
LT
286 /* We can run anywhere, unlike our parent keventd(). */
287 set_cpus_allowed(current, CPU_MASK_ALL);
288
289 retval = -EPERM;
290 if (current->fs->root)
67608567
AB
291 retval = kernel_execve(sub_info->path,
292 sub_info->argv, sub_info->envp);
1da177e4
LT
293
294 /* Exec failed? */
295 sub_info->retval = retval;
296 do_exit(0);
297}
298
299/* Keventd can't block, but this (a child) can. */
300static int wait_for_helper(void *data)
301{
302 struct subprocess_info *sub_info = data;
303 pid_t pid;
304 struct k_sigaction sa;
305
306 /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
307 * populate the status, but will return -ECHILD. */
308 sa.sa.sa_handler = SIG_IGN;
309 sa.sa.sa_flags = 0;
310 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
8292d633 311 do_sigaction(SIGCHLD, &sa, NULL);
1da177e4
LT
312 allow_signal(SIGCHLD);
313
314 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
315 if (pid < 0) {
316 sub_info->retval = pid;
317 } else {
111dbe0c
BS
318 int ret;
319
1da177e4
LT
320 /*
321 * Normally it is bogus to call wait4() from in-kernel because
322 * wait4() wants to write the exit code to a userspace address.
323 * But wait_for_helper() always runs as keventd, and put_user()
324 * to a kernel address works OK for kernel threads, due to their
325 * having an mm_segment_t which spans the entire address space.
326 *
327 * Thus the __user pointer cast is valid here.
328 */
111dbe0c
BS
329 sys_wait4(pid, (int __user *)&ret, 0, NULL);
330
331 /*
332 * If ret is 0, either ____call_usermodehelper failed and the
333 * real error code is already in sub_info->retval or
334 * sub_info->retval is 0 anyway, so don't mess with it then.
335 */
336 if (ret)
337 sub_info->retval = ret;
1da177e4
LT
338 }
339
a98f0dd3
AK
340 if (sub_info->wait < 0)
341 kfree(sub_info);
342 else
343 complete(sub_info->complete);
1da177e4
LT
344 return 0;
345}
346
347/* This is run by khelper thread */
65f27f38 348static void __call_usermodehelper(struct work_struct *work)
1da177e4 349{
65f27f38
DH
350 struct subprocess_info *sub_info =
351 container_of(work, struct subprocess_info, work);
1da177e4 352 pid_t pid;
e4b69aa2 353 int wait = sub_info->wait;
1da177e4
LT
354
355 /* CLONE_VFORK: wait until the usermode helper has execve'd
356 * successfully We need the data structures to stay around
357 * until that is done. */
e4b69aa2 358 if (wait)
1da177e4
LT
359 pid = kernel_thread(wait_for_helper, sub_info,
360 CLONE_FS | CLONE_FILES | SIGCHLD);
361 else
362 pid = kernel_thread(____call_usermodehelper, sub_info,
363 CLONE_VFORK | SIGCHLD);
364
a98f0dd3
AK
365 if (wait < 0)
366 return;
367
1da177e4
LT
368 if (pid < 0) {
369 sub_info->retval = pid;
370 complete(sub_info->complete);
e4b69aa2 371 } else if (!wait)
1da177e4
LT
372 complete(sub_info->complete);
373}
374
375/**
7888e7ff 376 * call_usermodehelper_keys - start a usermode application
1da177e4
LT
377 * @path: pathname for the application
378 * @argv: null-terminated argument list
379 * @envp: null-terminated environment list
7888e7ff 380 * @session_keyring: session keyring for process (NULL for an empty keyring)
1da177e4 381 * @wait: wait for the application to finish and return status.
a98f0dd3
AK
382 * when -1 don't wait at all, but you get no useful error back when
383 * the program couldn't be exec'ed. This makes it safe to call
384 * from interrupt context.
1da177e4
LT
385 *
386 * Runs a user-space application. The application is started
387 * asynchronously if wait is not set, and runs as a child of keventd.
388 * (ie. it runs with full root capabilities).
389 *
390 * Must be called from process context. Returns a negative error code
391 * if program was not execed successfully, or 0.
392 */
7888e7ff
DH
393int call_usermodehelper_keys(char *path, char **argv, char **envp,
394 struct key *session_keyring, int wait)
1da177e4 395{
60be6b9a 396 DECLARE_COMPLETION_ONSTACK(done);
a98f0dd3
AK
397 struct subprocess_info *sub_info;
398 int retval;
1da177e4
LT
399
400 if (!khelper_wq)
401 return -EBUSY;
402
403 if (path[0] == '\0')
404 return 0;
405
a98f0dd3
AK
406 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
407 if (!sub_info)
408 return -ENOMEM;
409
410 INIT_WORK(&sub_info->work, __call_usermodehelper);
411 sub_info->complete = &done;
412 sub_info->path = path;
413 sub_info->argv = argv;
414 sub_info->envp = envp;
415 sub_info->ring = session_keyring;
416 sub_info->wait = wait;
417
418 queue_work(khelper_wq, &sub_info->work);
419 if (wait < 0) /* task has freed sub_info */
420 return 0;
1da177e4 421 wait_for_completion(&done);
a98f0dd3
AK
422 retval = sub_info->retval;
423 kfree(sub_info);
424 return retval;
1da177e4 425}
7888e7ff 426EXPORT_SYMBOL(call_usermodehelper_keys);
1da177e4 427
e239ca54
AK
428int call_usermodehelper_pipe(char *path, char **argv, char **envp,
429 struct file **filp)
430{
431 DECLARE_COMPLETION(done);
432 struct subprocess_info sub_info = {
65f27f38
DH
433 .work = __WORK_INITIALIZER(sub_info.work,
434 __call_usermodehelper),
e239ca54
AK
435 .complete = &done,
436 .path = path,
437 .argv = argv,
438 .envp = envp,
439 .retval = 0,
440 };
441 struct file *f;
e239ca54
AK
442
443 if (!khelper_wq)
444 return -EBUSY;
445
446 if (path[0] == '\0')
447 return 0;
448
449 f = create_write_pipe();
3cce4856
AM
450 if (IS_ERR(f))
451 return PTR_ERR(f);
e239ca54
AK
452 *filp = f;
453
454 f = create_read_pipe(f);
3cce4856 455 if (IS_ERR(f)) {
e239ca54 456 free_write_pipe(*filp);
3cce4856 457 return PTR_ERR(f);
e239ca54
AK
458 }
459 sub_info.stdin = f;
460
65f27f38 461 queue_work(khelper_wq, &sub_info.work);
e239ca54
AK
462 wait_for_completion(&done);
463 return sub_info.retval;
464}
465EXPORT_SYMBOL(call_usermodehelper_pipe);
466
1da177e4
LT
467void __init usermodehelper_init(void)
468{
469 khelper_wq = create_singlethread_workqueue("khelper");
470 BUG_ON(!khelper_wq);
471}
This page took 0.292081 seconds and 5 git commands to generate.