[PATCH] request_module_fmt(char "fmt, ...)

David Woodhouse (dwmw2@infradead.org)
Sat, 17 May 2003 15:36:39 +0100


We do this all over the kernel, badly. Many people use sprintf, others
use snprintf but don't seem to have noticed they need to put the
trailing zero into the buffer if it truncates, etc.

If we put it in one place, we can get it right.

I didn't just change the prototype of request_module() because that
would cause stuff like 'ifconfig eth%s up' to cause oopsen.

Untested -- 2.5 didn't even get to a prompt last time I tried to boot it
a week or so ago. Does compile though :)

===== include/linux/kmod.h 1.3 vs edited =====
--- 1.3/include/linux/kmod.h Thu Feb 13 03:35:39 2003
+++ edited/include/linux/kmod.h Sat May 17 15:30:01 2003
@@ -25,8 +25,12 @@

#ifdef CONFIG_KMOD
extern int request_module(const char * name);
+extern int request_module_fmt(const char *fmt, ...)
+ __attribute__ ((format (printf, 1, 2)));
#else
static inline int request_module(const char * name) { return -ENOSYS; }
+static inline int request_module_fmt(const char *fmt, ...)
+ __attribute__ ((format (printf, 1, 2))) { return -ENOSYS; }
#endif

#define try_then_request_module(x, mod) ((x) ?: request_module(mod), (x))
===== kernel/kmod.c 1.25 vs edited =====
--- 1.25/kernel/kmod.c Mon Feb 24 17:40:34 2003
+++ edited/kernel/kmod.c Sat May 17 15:30:51 2003
@@ -110,6 +110,29 @@
atomic_dec(&kmod_concurrent);
return ret;
}
+
+int request_module_fmt(const char *fmt, ...)
+{
+ char name[32];
+ va_list args;
+ int ret;
+
+ name[sizeof(name)-1] = 0;
+
+ va_start(args, fmt);
+ ret = vsnprintf(name, sizeof(name)-1, fmt, args);
+ va_end(args);
+
+ if (ret >= sizeof(name)-1) {
+ printk(KERN_WARNING "Module name \"%s\"... too long.\n",
+ name);
+ return -ENAMETOOLONG;
+ }
+ return request_module(name);
+}
+
+EXPORT_SYMBOL(request_module);
+EXPORT_SYMBOL(request_module_fmt);
#endif /* CONFIG_KMOD */

#ifdef CONFIG_HOTPLUG
@@ -273,8 +296,4 @@
}

EXPORT_SYMBOL(call_usermodehelper);
-
-#ifdef CONFIG_KMOD
-EXPORT_SYMBOL(request_module);
-#endif

-- 
dwmw2

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/