* Re: readdir within kernel
2002-12-19 5:06 readdir within kernel Herbert Poetzl
@ 2002-12-19 13:09 ` Matthew Wilcox
0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2002-12-19 13:09 UTC (permalink / raw)
To: Herbert Poetzl; +Cc: linux-fsdevel
On Thu, Dec 19, 2002 at 06:06:32AM +0100, Herbert Poetzl wrote:
> I would like to know if this is the right way to
> read a directory contents for a given directory
> dentry within a kernel module ...
well, you probably _shouldn't_ be anyway... but since you are, this is
how I did it for debugging why my system couldn't find /sbin/init.
diff -u -p -r1.11 main.c
--- init/main.c 10 Dec 2002 22:03:28 -0000 1.11
+++ init/main.c 19 Dec 2002 13:08:45 -0000
@@ -501,6 +501,26 @@ static void do_pre_smp_initcalls(void)
extern void prepare_namespace(void);
+static int kernel_filler(void *buf, const char *name, int namlen, loff_t offset,
+ ino_t ino, unsigned int d_type)
+{
+ printk("%.*s\n", namlen, name);
+}
+
+static void kernel_ls(char *name)
+{
+ struct file *file;
+
+ file = filp_open(name, O_RDONLY, 0);
+ if (!file) {
+ printk("Could not open %s\n", name);
+ return;
+ }
+
+ vfs_readdir(file, kernel_filler, NULL);
+ filp_close(file, current->files);
+}
+
static int init(void * unused)
{
static char * argv_sh[] = { "sh", NULL, };
@@ -540,6 +560,9 @@ static int init(void * unused)
(void) dup(0);
(void) dup(0);
+
+ kernel_ls("/");
+ kernel_ls("/sbin");
/*
* We try each of these until one succeeds.
--
"It's not Hollywood. War is real, war is primarily not about defeat or
victory, it is about death. I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
^ permalink raw reply [flat|nested] 2+ messages in thread