Creating a file in kernel Module -- working fine

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

 



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.

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? 

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

Thanks and regards,
Srinivas G

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[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