Re: Creating a file in kernel Module -- working fine

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 21, 2005 at 20:17:33 +0530, Srinivas G. wrote:
> Dear All,
> 
> I have gone through your mails. Based on those mail messages I have
> created a test module in the kernel to create a file. It was compiled
> fine with out any error messages and working fine.
> 
> I am using Red Hat 7.3 with 2.4.18-3 kernel version. Please fine the
> code below.

It is working by pure luck. It wouldn't work if it was not all done in
init_module, because the other function would likely run in different
process and thus would have different file table and thus the descriptor
would not exist -- or worse, you would garble some application's
precious file!

I am *NOT* going to explain to you how to fix the code. If you have
really read all those threads, you know, that
IT IS FORBIDDEN TO CREATE, READ AND WRITE FILES FROM KERNEL.

It is forbidden because reading and writing files needs some policy and
policy belongs to userland. The correct way of handling this is to make
available and/or accept the data over a device, proc (or sys) file or
netlink socket and have a userland helper manage the actual data
source/destination. You can call_usermodehelper() if you need to
start the helper on-demand (as done e.g. by kmod). The helper should
then connect to the interface and process the data.

> cre_fil.c
> ------------------------------------------------------------------
> #include <linux/kernel.h>
> #include <linux/module.h>
> #include <asm/fcntl.h>		/* for O_WRONLY */
> #include <linux/syscall.h>	/* for sys_ functions */
> #include <asm/uaccess.h>	/* for set_fs(), get_fs() etc. */
> #include <linux/string.h>	/* for string length */
> #include <linux/slab.h>		/* for kmalloc */
> 
> MODULE_LICENSE("GPL");
> 
> typedef struct tagWRITE_TEST
> {
> 	unsigned long fd;
> 	unsigned long x;
> 	
> }WRITE_TEST, *PWRITE_TEST;
> 
> PWRITE_TEST ptest;
> 
> void SysPrint(char * pString, ...)
> {
> 	static char buff[1024];
> 	va_list ap;
> 
> 	va_start(ap,pString);
> 	vsprintf((char *)buff, pString, ap);
> 	va_end(ap);
> 	
> 	sys_write(ptest->fd,(char *)buff,strlen(buff));
> }
> 
> int init_module(void)
> {
> 	
> 	printk("<%s> invoked!\n",__FUNCTION__);
> 	printk("File Creation Testing in Kernel Module!\n");
> 
> 	set_fs(get_ds());
> 	
> 	/* allocate the memory for structre */
> 	ptest = (PWRITE_TEST)kmalloc(sizeof(WRITE_TEST),GFP_KERNEL);
> 	if(ptest == NULL)
> 	{
> 		printk("Structure Memory Allocation Fails!\n");
> 		return -ENOMEM;
> 	}
> 	
> 	ptest->fd = sys_open("srcdebug.txt", O_CREAT | O_WRONLY, 644);
> 	if (ptest->fd == 0)
> 	{
> 		printk("File Creation Error!!!\n");
> 		return 1;
> 	}
> 	
> 	SysPrint("File Creation Testing in Kernel Module!\n");
> 	SysPrint("Srinivas Testing the File Creation\n");
> 
> 	/* close the file here */
> 	sys_close(ptest->fd);
> 	
> 	return 0;
> }
> 
> void cleanup_module(void)
> {
> 	printk("Good bye!\n");
> 
> 	/* free the allocated memory */
> 	kfree(ptest);
> 	
> }
> 
> Makefile
> ------------------------------------------------
> INCDIR=/usr/src/linux-2.4/include
> 
> CFLAGS=-DMODULE -D__KERNEL__ -I$(INCDIR) -O2 -Wall
> 
> OBJS=cre_fil.o
> TARGET=cf.o
> 
> $(TARGET): $(OBJS)
> 	$(LD) -r -o $(TARGET) $(OBJS)
> 
> clean:
> 	$(RM) -f $(TARGET) $(OBJS)
> 	
> ------------------------------------------------------------------------
> --
> 
> I need some information. Where can I get the information for sys_create,
> sys_write, sys_read, set_fs, get_fs, get_ds etc calls? 

You don't want to call any of them in the first place. You want to call
the workhorses inside.

> Please provide any links which are related to that calls or any books.

You have the source ;-)

-------------------------------------------------------------------------------
						 Jan 'Bulb' Hudec <bulb@xxxxxx>

Attachment: signature.asc
Description: Digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux