+
+static ssize_t store_mod_request(struct module_attribute *mattr,
+ struct module *mod,
+ const char *buffer, size_t count)
+{
+ char name[MODULE_NAME_LEN];
+ int ret;
+
+ if (count < 1 || count+1 > MODULE_NAME_LEN)
+ return -EINVAL;
+ memcpy(name, buffer, count);
+ name[count] = '\0';
+ if (name[count-1] == '\n')
+ name[count-1] = '\0';
+
+ ret = request_module(name);
+ if (ret < 0)
+ return ret;
+ return count;
+}
+
+static struct module_attribute mod_request = {
+ .attr = { .name = "mod_request", .mode = S_IWUSR, .owner = THIS_MODULE },
+ .store = store_mod_request,
+};
+
+#ifdef CONFIG_MODULE_UNLOAD
+static ssize_t store_mod_unload(struct module_attribute *mattr,
+ struct module *mod,
+ const char *buffer, size_t count)
+{
+ char name[MODULE_NAME_LEN];
+ int ret;
+
+ if (count < 1 || count+1 > MODULE_NAME_LEN)
+ return -EINVAL;
+ memcpy(name, buffer, count);
+ name[count] = '\0';
+ if (name[count-1] == '\n')
+ name[count-1] = '\0';
+
+ ret = delete_module(name, O_NONBLOCK);
+ if (ret < 0)
+ return ret;
+ return count;
+}
+
+static struct module_attribute mod_unload = {
+ .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE },
+ .store = store_mod_unload,
+};
+#endif
+
+static ssize_t show_mod_request_helper(struct module_attribute *mattr,
+ struct module *mod,
+ char *buffer)
+{
+ return sprintf(buffer, "%s\n", modprobe_path);
+}
+
+static ssize_t store_mod_request_helper(struct module_attribute *mattr,
+ struct module *mod,
+ const char *buffer, size_t count)
+{
+ if (count < 1 || count+1 > KMOD_PATH_LEN)
+ return -EINVAL;
+ memcpy(modprobe_path, buffer, count);
+ modprobe_path[count] = '\0';
+ if (modprobe_path[count-1] == '\n')
+ modprobe_path[count-1] = '\0';
+ return count;
+}
+
+static struct module_attribute mod_request_helper = {
+ .attr = {
+ .name = "mod_request_helper",
+ .mode = S_IWUSR | S_IRUGO,
+ .owner = THIS_MODULE
+ },
+ .show = show_mod_request_helper,
+ .store = store_mod_request_helper,
+};
+
+void __init kmod_sysfs_init(void)
+{
+ int ret;
+
+ kmod_mk.mod = THIS_MODULE;
+ kobj_set_kset_s(&kmod_mk, module_subsys);
+ kobject_set_name(&kmod_mk.kobj, "kmod");
+ kobject_init(&kmod_mk.kobj);
+ ret = kobject_add(&kmod_mk.kobj);
+ if (ret < 0)
+ goto out;
+
+ ret = sysfs_create_file(&kmod_mk.kobj, &mod_request_helper.attr);
+ ret = sysfs_create_file(&kmod_mk.kobj, &mod_request.attr);
+#ifdef CONFIG_MODULE_UNLOAD
+ ret = sysfs_create_file(&kmod_mk.kobj, &mod_unload.attr);
+#endif
+
+ kobject_uevent(&kmod_mk.kobj, KOBJ_ADD);
+out:
+ return;
+}