History

Driver core: let request_module() send a /sys/modules/kmod/-uevent

On recent systems, calls to /sbin/modprobe are handled by udev depending on the kind of device the kernel has discovered. This patch creates an uevent for the kernels internal request_module(), to let udev take control over the request, instead of forking the binary directly by the kernel. The direct execution of /sbin/modprobe can be disabled by setting: /sys/module/kmod/mod_request_helper (/proc/sys/kernel/modprobe) to an empty string, the same way /proc/sys/kernel/hotplug is disabled on an udev system.

Kay Sievers committed 19 years ago

Commit c353c3f1 parent 89790fd4 files · +139 −10Browse files

Unified · Split

4 files
  1. include/linux/kmod.h +2 −0
  2. kernel/kmod.c +120 −0
  3. kernel/module.c +16 −10
  4. kernel/params.c +1 −0
include/linux/kmod.h+2−0
@@ -28,8 +28,10 @@ · 27 unmodified lines
2828 #ifdef CONFIG_KMOD
2929 /* modprobe exit status on success, -ve on error. Return value
3030 * usually useless though. */
31+extern void kmod_sysfs_init(void);
3132 extern int request_module(const char * name, ...) __attribute__ ((format (printf, 1, 2)));
3233 #else
34+static inline void kmod_sysfs_init(void) {};
3335 static inline int request_module(const char * name, ...) { return -ENOSYS; }
3436 #endif
3537
kernel/kmod.c+120−0
@@ -36,6 +36,8 @@ · 35 unmodified lines
3636 #include <linux/resource.h>
3737 #include <asm/uaccess.h>
3838
39+extern int delete_module(const char *name, unsigned int flags);
40+
3941 extern int max_threads;
4042
4143 static struct workqueue_struct *khelper_wq;
@@ -46,6 +48,7 @@ static struct workqueue_struct *khelper_wq; · 4 unmodified lines
4648 modprobe_path is set via /proc/sys.
4749 */
4850 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
51+struct module_kobject kmod_mk;
4952
5053 /**
5154 * request_module - try to load a kernel module
@@ -75,6 +78,11 @@ int request_module(const char *fmt, ...) · 23 unmodified lines
7578 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
7679 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
7780 static int kmod_loop_msg;
81+ char modalias[16 + MODULE_NAME_LEN] = "MODALIAS=";
82+ char *uevent_envp[2] = {
83+ modalias,
84+ NULL
85+ };
7886
7987 va_start(args, fmt);
8088 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
@@ -82,6 +90,12 @@ int request_module(const char *fmt, ...) · 1 unmodified line
8290 if (ret >= MODULE_NAME_LEN)
8391 return -ENAMETOOLONG;
8492
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+
8599 /* If modprobe needs a service that is in a module, we get a recursive
86100 * loop. Limit the number of running kmod threads to max_threads/2 or
87101 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
@@ -108,9 +122,115 @@ int request_module(const char *fmt, ...) · 20 unmodified lines
108122
109123 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
110124 atomic_dec(&kmod_concurrent);
125+out:
111126 return ret;
112127 }
113128 EXPORT_SYMBOL(request_module);
129+
130+static 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+
150+static 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
156+static 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+
176+static 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+
182+static 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+
189+static 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+
202+static 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+
212+void __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);
231+out:
232+ return;
233+}
114234 #endif /* CONFIG_KMOD */
115235
116236 struct subprocess_info {
kernel/module.c+16−10
@@ -653,20 +653,11 @@ static void wait_for_zero_refcount(struct module *mod) · 652 unmodified lines
653653 mutex_lock(&module_mutex);
654654 }
655655
656−asmlinkage long
657−sys_delete_module(const char __user *name_user, unsigned int flags)
656+int delete_module(const char *name, unsigned int flags)
658657 {
659658 struct module *mod;
660− char name[MODULE_NAME_LEN];
661659 int ret, forced = 0;
662660
663− if (!capable(CAP_SYS_MODULE))
664− return -EPERM;
665−
666− if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
667− return -EFAULT;
668− name[MODULE_NAME_LEN-1] = '\0';
669−
670661 if (mutex_lock_interruptible(&module_mutex) != 0)
671662 return -EINTR;
672663
@@ -727,6 +718,21 @@ sys_delete_module(const char __user *name_user, unsigned int flags) · 54 unmodified lines
727718 return ret;
728719 }
729720
721+asmlinkage long
722+sys_delete_module(const char __user *name_user, unsigned int flags)
723+{
724+ char name[MODULE_NAME_LEN];
725+
726+ if (!capable(CAP_SYS_MODULE))
727+ return -EPERM;
728+
729+ if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
730+ return -EFAULT;
731+ name[MODULE_NAME_LEN-1] = '\0';
732+
733+ return delete_module(name, flags);
734+}
735+
730736 static void print_unload_info(struct seq_file *m, struct module *mod)
731737 {
732738 struct module_use *use;
kernel/params.c+1−0
@@ -714,6 +714,7 @@ static int __init param_sysfs_init(void) · 713 unmodified lines
714714 }
715715
716716 param_sysfs_builtin();
717+ kmod_sysfs_init();
717718
718719 return 0;
719720 }