public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: christophe barbe <christophe.barbe@lineo.fr>
To: linux-kernel@vger.kernel.org
Subject: scan directory content in kernel-level code
Date: Thu, 8 Mar 2001 13:30:31 +0100	[thread overview]
Message-ID: <20010308133031.A21034@pc8.inup.com> (raw)

I'm trying to scan the content of the /dev directory in a init_module function.
I've not found example in the kernel source. So I try to reproduce the work of the system call.
I open the directory, check if the readdir pointer is not null in the f_op structure and then use it with my filldir function.
Because I don't need to stock names, I use filename directly in the filldir callback.
So in the filldir function, I check if the name match a simple regexp and if yes, I open the file and store the filp pointer.

First I don't understand why I need to call several times readdir in order to scan the directory (I imagine each times I obtain the content of one buffer). I check the number of callback call to stop calling readdir. It works but it seems strange.

Second problem, Sometimes the modprobe dead-locks in my filldir function. And I've found that it never arrives if i do a "ls /dev" before modprobe. Moreover if I don't take the directory inode semaphore, the problem never arrives. 
What's wrong ?

Christophe ...


struct my_dentry {
	char fullname[5];
	char name[MAX_DEVNAME_SIZE+1];
	int count;
};

int my_filldir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino, unsigned unused)
{
	struct my_dentry * buf = (struct my_dentry *)__buf;

	if (namlen>MAX_DEVNAME_SIZE) namlen=MAX_DEVNAME_SIZE;

	memcpy((char *)buf->name, name, namlen);
	buf->name[namlen]='\0';
	check_device(buf->fullname);  // here I open the file if the string match criteria
	buf->count++;
	return 0;
}

void scan_device_directory(void)
{
	struct file * filp;
	struct inode * d_inode;
	struct my_dentry dir_entry={"/dev/",};
	int rc;

	filp = filp_open("/dev", O_RDONLY|O_SYNC, 0600);
	if ((rc=IS_ERR(filp))>0)  {
		printk("bad filp : %d\n", rc);
		return;
	}

	d_inode = filp->f_dentry->d_inode;
	if (!d_inode) {
		printk("NULL inode\n");
		goto out_close_dir;
	}

	if (! S_ISDIR(d_inode->i_mode)) {
		printk("/dev is not a directory (?)\n");
		goto out_close_dir;
	}

	if (filp->f_op->readdir == NULL) {
		printk("can't find readidir f_ops\n");
		goto out_close_dir;
	}

	v_printk("/dev : inode=%ld\n", d_inode->i_ino);

	down(&d_inode->i_sem);

	do {
		dir_entry.count=0;
		rc=filp->f_op->readdir(filp, (void *)&dir_entry, my_filldir);
	} while (dir_entry.count>0);

	up(&d_inode->i_sem);

out_close_dir:
	fput(filp);
}

-- 
Christophe Barbé
Software Engineer
Lineo High Availability Group
42-46, rue Médéric
92110 Clichy - France
phone (33).1.41.40.02.12
fax (33).1.41.40.02.01
www.lineo.com

                 reply	other threads:[~2001-03-08 12:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010308133031.A21034@pc8.inup.com \
    --to=christophe.barbe@lineo.fr \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox