[klibc] klibc insmod for recent kernels

Marco d'Itri klibc@zytor.com, md@Linux.IT
Mon, 23 Dec 2002 13:50:23 +0100


--9jxsPFA5p3P2qPhR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I stripped down the insmod command from modules-init-tools for use with
klibc.
Rusty agreed to have this contributed to klibc.

The second patch removes old style modules support.

-- 
ciao,
Marco

--9jxsPFA5p3P2qPhR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="klibc.insmod.diff"

diff -urN klibc-0.70/klibc/SYSCALLS klibc-0.70+insmod/klibc/SYSCALLS
--- klibc-0.70/klibc/SYSCALLS	2002-09-11 07:00:58.000000000 +0200
+++ klibc-0.70+insmod/klibc/SYSCALLS	2002-12-20 01:05:35.000000000 +0100
@@ -142,5 +142,6 @@
 void * create_module(const char *, size_t)
 int delete_module(const char *)
 int query_module(const char *, int, void *, size_t, size_t)
+long init_module(void *, unsigned long, const char *)
 int reboot::__reboot(int, int, int, void *)
 int syslog::klogctl(int, char *, int)
Binary files klibc-0.70/utils/insmod and klibc-0.70+insmod/utils/insmod differ
diff -urN klibc-0.70/utils/insmod.c klibc-0.70+insmod/utils/insmod.c
--- klibc-0.70/utils/insmod.c	1970-01-01 01:00:00.000000000 +0100
+++ klibc-0.70+insmod/utils/insmod.c	2002-12-23 13:40:16.000000000 +0100
@@ -0,0 +1,81 @@
+/* insmod.c: insert a module into the kernel.
+    Copyright (C) 2001  Rusty Russell.
+    Copyright (C) 2002  Rusty Russell, IBM Corporation.
+
+    Modified for klibc by Marco d'Itri <md@linux.it>.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+#include <asm/unistd.h>
+
+long init_module(void *umod, unsigned long len, const char *uargs); /* XXX */
+
+int main(int argc, char *argv[])
+{
+	int i, fd;
+	struct stat st;
+	void *map;
+	char *filename, *options = malloc(32);
+
+	if (!(filename = argv[1])) {
+	    fprintf(stderr, "Usage: %s filename [args]\n", argv[0]);
+	    return 1;
+	}
+
+	/* Rest is options */
+	*options = '\0';
+	for (i = 2; i < argc; i++) {
+		options = realloc(options,
+				  strlen(options) + 2 + strlen(argv[i]) + 2);
+		/* Spaces handled by "" pairs, but no way of escaping
+                   quotes */
+		if (strchr(argv[i], ' '))
+			strcat(options, "\"");
+		strcat(options, argv[i]);
+		if (strchr(argv[i], ' '))
+			strcat(options, "\"");
+		strcat(options, " ");
+	}
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		perror("open");
+		return 1;
+	}
+
+	fstat(fd, &st);
+	map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+	if (map == MAP_FAILED) {
+		perror("mmap");
+		return 1;
+	}
+
+	if (init_module(map, st.st_size, options) != 0) {
+		perror("init_module");
+		return 1;
+	}
+
+	return 0;
+}
diff -urN klibc-0.70/utils/Makefile klibc-0.70+insmod/utils/Makefile
--- klibc-0.70/utils/Makefile	2002-08-23 23:06:05.000000000 +0200
+++ klibc-0.70+insmod/utils/Makefile	2002-12-17 14:07:37.000000000 +0100
@@ -32,6 +32,9 @@
 umount: umount.o $(CRT0) $(LIBS)
 	$(LD) $(LDFLAGS) -o $@ $(CRT0) umount.o $(LIBS)
 
+insmod: insmod.o $(CRT0) $(LIBS)
+	$(LD) $(LDFLAGS) -o $@ $(CRT0) insmod.o $(LIBS)
+
 $(CRT0) $(LIBS):
 	@echo '*** error: $@ not up to date' || exit 1
 

--9jxsPFA5p3P2qPhR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="klibc.nooldmodules.diff"

diff -urN klibc-0.70+insmod/klibc/include/sys/module.h klibc-0.70+nooldmodules/klibc/include/sys/module.h
--- klibc-0.70+insmod/klibc/include/sys/module.h	2002-08-06 06:28:41.000000000 +0200
+++ klibc-0.70+nooldmodules/klibc/include/sys/module.h	1970-01-01 01:00:00.000000000 +0100
@@ -1,158 +0,0 @@
-/*
- * sys/module.h
- *
- * This is a bastardized version of linux/module.h, since the latter
- * doesn't have __KERNEL__ guards where it needs them...
- */
-
-#ifndef _SYS_MODULE_H
-#define _SYS_MODULE_H
-
-/*
- * Dynamic loading of modules into the kernel.
- *
- * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
- */
-
-#include <asm/atomic.h>
-
-/* Don't need to bring in all of uaccess.h just for this decl.  */
-struct exception_table_entry;
-
-/* Used by get_kernel_syms, which is obsolete.  */
-struct kernel_sym
-{
-	unsigned long value;
-	char name[60];		/* should have been 64-sizeof(long); oh well */
-};
-
-struct module_symbol
-{
-	unsigned long value;
-	const char *name;
-};
-
-struct module_ref
-{
-	struct module *dep;	/* "parent" pointer */
-	struct module *ref;	/* "child" pointer */
-	struct module_ref *next_ref;
-};
-
-/* TBD */
-struct module_persist;
-
-struct module
-{
-	unsigned long size_of_struct;	/* == sizeof(module) */
-	struct module *next;
-	const char *name;
-	unsigned long size;
-
-	union
-	{
-		atomic_t usecount;
-		long pad;
-	} uc;				/* Needs to keep its size - so says rth */
-
-	unsigned long flags;		/* AUTOCLEAN et al */
-
-	unsigned nsyms;
-	unsigned ndeps;
-
-	struct module_symbol *syms;
-	struct module_ref *deps;
-	struct module_ref *refs;
-	int (*init)(void);
-	void (*cleanup)(void);
-	const struct exception_table_entry *ex_table_start;
-	const struct exception_table_entry *ex_table_end;
-#ifdef __alpha__
-	unsigned long gp;
-#endif
-	/* Members past this point are extensions to the basic
-	   module support and are optional.  Use mod_member_present()
-	   to examine them.  */
-	const struct module_persist *persist_start;
-	const struct module_persist *persist_end;
-	int (*can_unload)(void);
-	int runsize;			/* In modutils, not currently used */
-	const char *kallsyms_start;	/* All symbols for kernel debugging */
-	const char *kallsyms_end;
-	const char *archdata_start;	/* arch specific data for module */
-	const char *archdata_end;
-	const char *kernel_data;	/* Reserved for kernel internal use */
-};
-
-struct module_info
-{
-	unsigned long addr;
-	unsigned long size;
-	unsigned long flags;
-	long usecount;
-};
-
-/* Bits of module.flags.  */
-
-#define MOD_UNINITIALIZED	0
-#define MOD_RUNNING		1
-#define MOD_DELETED		2
-#define MOD_AUTOCLEAN		4
-#define MOD_VISITED  		8
-#define MOD_USED_ONCE		16
-#define MOD_JUST_FREED		32
-#define MOD_INITIALIZING	64
-
-/* Values for query_module's which.  */
-
-#define QM_MODULES	1
-#define QM_DEPS		2
-#define QM_REFS		3
-#define QM_SYMBOLS	4
-#define QM_INFO		5
-
-/* Can the module be queried? */
-#define MOD_CAN_QUERY(mod) (((mod)->flags & (MOD_RUNNING | MOD_INITIALIZING)) && !((mod)->flags & MOD_DELETED))
-
-/* When struct module is extended, we must test whether the new member
-   is present in the header received from insmod before we can use it.  
-   This function returns true if the member is present.  */
-
-#define mod_member_present(mod,member) 					\
-	((unsigned long)(&((struct module *)0L)->member + 1)		\
-	 <= (mod)->size_of_struct)
-
-/*
- * Ditto for archdata.  Assumes mod->archdata_start and mod->archdata_end
- * are validated elsewhere.
- */
-#define mod_archdata_member_present(mod, type, member)			\
-	(((unsigned long)(&((type *)0L)->member) +			\
-	  sizeof(((type *)0L)->member)) <=				\
-	 ((mod)->archdata_end - (mod)->archdata_start))
-	 
-
-/* Check if an address p with number of entries n is within the body of module m */
-#define mod_bound(p, n, m) ((unsigned long)(p) >= ((unsigned long)(m) + ((m)->size_of_struct)) && \
-	         (unsigned long)((p)+(n)) <= (unsigned long)(m) + (m)->size)
-
-/* Backwards compatibility definition.  */
-
-#define GET_USE_COUNT(module)	(atomic_read(&(module)->uc.usecount))
-
-/* Poke the use count of a module.  */
-
-#define __MOD_INC_USE_COUNT(mod)					\
-	(atomic_inc(&(mod)->uc.usecount), (mod)->flags |= MOD_VISITED|MOD_USED_ONCE)
-#define __MOD_DEC_USE_COUNT(mod)					\
-	(atomic_dec(&(mod)->uc.usecount), (mod)->flags |= MOD_VISITED)
-#define __MOD_IN_USE(mod)						\
-	(mod_member_present((mod), can_unload) && (mod)->can_unload	\
-	 ? (mod)->can_unload() : atomic_read(&(mod)->uc.usecount))
-
-/* Indirect stringification.  */
-
-#define __MODULE_STRING_1(x)	#x
-#define __MODULE_STRING(x)	__MODULE_STRING_1(x)
-
-#endif /* _SYS_MODULE_H */
diff -urN klibc-0.70+insmod/klibc/SYSCALLS klibc-0.70+nooldmodules/klibc/SYSCALLS
--- klibc-0.70+insmod/klibc/SYSCALLS	2002-12-20 01:05:35.000000000 +0100
+++ klibc-0.70+nooldmodules/klibc/SYSCALLS	2002-12-23 13:46:39.000000000 +0100
@@ -138,10 +138,6 @@
 int uname(struct utsname *)
 int setdomainname(const char *, size_t)
 int sethostname(const char *, size_t)
-int init_module(const char *, struct module *)
-void * create_module(const char *, size_t)
-int delete_module(const char *)
-int query_module(const char *, int, void *, size_t, size_t)
 long init_module(void *, unsigned long, const char *)
 int reboot::__reboot(int, int, int, void *)
 int syslog::klogctl(int, char *, int)
diff -urN klibc-0.70+insmod/klibc/syscommon.h klibc-0.70+nooldmodules/klibc/syscommon.h
--- klibc-0.70+insmod/klibc/syscommon.h	2002-08-13 09:02:41.000000000 +0200
+++ klibc-0.70+nooldmodules/klibc/syscommon.h	2002-12-23 13:46:42.000000000 +0100
@@ -15,7 +15,6 @@
 #include <sys/dirent.h>
 #include <sys/klog.h>
 #include <sys/mman.h>
-#include <sys/module.h>
 #include <sys/resource.h>
 #include <sys/select.h>
 #include <sys/stat.h>

--9jxsPFA5p3P2qPhR--