From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matthew Wilcox Subject: Re: readdir within kernel Date: Thu, 19 Dec 2002 13:09:52 +0000 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <20021219130952.O9994@parcelfarce.linux.theplanet.co.uk> References: <20021219050632.GB16235@www.13thfloor.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: linux-fsdevel@vger.kernel.org Return-path: To: Herbert Poetzl Content-Disposition: inline In-Reply-To: <20021219050632.GB16235@www.13thfloor.at>; from herbert@13thfloor.at on Thu, Dec 19, 2002 at 06:06:32AM +0100 List-Id: linux-fsdevel.vger.kernel.org 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