Re: how to call system call from kernelspace

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

 



On Wed, Apr 28, 2010 at 8:35 AM, rahul patil�<rahul.deshmukhpatil@xxxxxxxxx>�wrote:
i want to call open,write system calls from kernel space.
[oops, didn't hit reply-to-all at first, sorry rahul!]

Not sure about open, but for read/write system calls, provided you already have file opened and have the file descriptor, the following code snippets should work:

For reads:
�� � � �struct file *file = fget(fd)
�� � � �if (file) {
�� � � � � � � �loff_t pos = file_pos_read(file);
�� � � � � � � �ret = vfs_read(file, buf, count, &pos);
�� � � � � � � �file_pos_write(file, pos);
�� � � � � � � �fput(file);
�� � � �}

For writes:
�� � � �struct file *file = fget(fd)
�� � � �if (file) {
�� � � � � � � �loff_t pos = file_pos_read(file);
�� � � � � � � �ret = vfs_write(file, buf, count, &pos);
�� � � � � � � �file_pos_write(file, pos);
�� � � � � � � �fput(file);
�� � � �}

No magic there, this is essentially the same code that gets executed when you make read/write system calls defined in fs/read_write.c except that you don't have to export sys_read and sys_write

Its a better idea to call�file->f_op->read and�file->f_op->write directly instead of vfs_write in the above code, skips a lot of checks and stuff. See how loop driver does this in drivers/block/loop.c in function �__do_lo_send_write.
You could even call write_begin and write_end directly if your backs fs supports address space ops. Again good examples in loop driver.

HTH,
-Joel


[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