On Wed, Feb 02, 2005 at 03:04:23PM -0000, Pankaj G wrote: > After reading all those mails on how to read file in kernel module, i think i should share my way of reading a file in a module. > I don't know whether it is right or wrong, but i have tried and it works. > Please give me views about this way of reading a file. It is completely wrong for several reasons. Your way completely fails when the read_config_file() function is in a different namespace than the process loading the module. Yes, processes can have different namespaces (== views of the filesystem) and there is no way to guarantee that read_config_file() is in the namespace you want it to be. Figuring out *where* the config file lives is a *policy* decision, and policy belongs to userland. Today you want the config file to live in /etc/myconfig, tomorrow you want it in /var/lib/myconfig, and next week you want to read it from $HOME/myconfig (guess what: you can't do environment variable expension in the kernel). Figuring out *how* the configuration will be loaded is also a *policy* decision, and therefore also belongs to userland. Right now you appear to be using a plain file format, but you could also get the configation from a NIS database, a http server, or over an SSL link tunneled through a SSH connection to a MySQL server. The kernel doesn't offer that kind of flexibility cause the kernel only implements *mechanisms*. Userland, however, does offer that flexibility and therefor it belongs to userland. Study the kernel sources: you can't find a place where the kernel reads a configuration file. Examples: - Mounting filesystems is done by the "mount" program, which reads the config file /etc/fstab and issues mount(2) system calls to get the filesystems mounted. It's not the kernel that reads the config file, but a usermode helper. - Exporting NFS mount points is done using the "exportfs" program, which reads the config file /etc/exports and communicates to the /kernel through proc/fs/nfs/*. It's not the kernel that reads the config file, but a usermode helper. - The netfilter firewall code gets it rules through the "iptables" (or ipchains) program, which either reads a configuration file, or is called repetitevely from a script and changes the netfilter setup through a socket. Again, it's not the kernel that reads the config file, but a usermode helper. Heck, in this particular case there even isn't a standard place for the configuration file, and it clearly shows why the userspace solution is so much more flexible. Bottom line: do not read or write files from the kernel. It *will* fail when using different namespaces, and it is clear a helper program in userspace gives much more flexibility. Erik -- Erik Mouw J.A.K.Mouw@xxxxxxxxxxxxxx mouw@xxxxxxxxxxxx
Attachment:
signature.asc
Description: Digital signature