Create File filp_open/sys_open in the kernel

Source: Internet
Author: User
Tags dmesg
Previously, I thought that files could not be created in the kernel, but this is not the case. As long as the file system runs, it can operate files like in the user space. user space kernel open () sys_open (), filp_open () Close () sys_close (), filp_close () read () sys_read (), filp_read () write () sys_write (), filp_write () -------------------------------------------------- sometimes sys_xxxx cannot be used in the kernel module. Then, the function corresponding to filp_xxxx can be used. There is still a problem using sys_xxxx in the module. create an aastruct file * filep in/tmp;
Filep = filp_open ("/tmp/AA", o_creat | o_rdwr, 0 );
If (is_err (filep ))
Return-1;
Filp_close (filep, 0 );
Return 0; ========================================================== sys_open usage: # include <Linux/Fs. h> ...... u8 buff [50];
Int FD;
Memset (buff, 0x00, sizeof (buff ));
Mm_segment_t old_fs = get_fs ();
Set_fs (kernel_ds );
FD = sys_open ("/etc/INFO", o_rdonly, 0 );
If (FD> = 0)
{
Sys_read (FD, buff, 50 );
Printk ("string: % s/n", buff );
Sys_close (FD );}
Set_fs (old_fs ); ========================================================== the following open parameters are used: o_accmode <0003>;: when reading and writing files, it is used to retrieve the low 2 bits of the flag.
O_rdonly <00>;: Read-Only
O_wronly <01>;: Write-only open
O_rdwr <02>;: read/write Enabled
O_creat <0100>;: create if the file does not exist. mode_t, not fcntl is required.
O_excl <0200>;: if both o_creat is specified and the file already exists, an error occurs. Not fcntl
O_noctty <0400>;: If pathname indicates a terminal device, this device is not allocated as the control terminal for this process. Not fcntl o_trunc <01000>;: if the file exists and is read-only or write-only, the length of the file is truncated to 0. Not fcntl

O_append <02000>;: added to the end of the file every time the file is written.
O_nonblock <04000>;: If p a t h n a m e refers to an f I f o, a special block file, or a special character file, this option sets the non-blocking mode for this file's current open operation and subsequent I/O operations.

O_ndelay ;;
O_sync <010000>;: enables each write operation to wait until the physical I/O operation is completed.
Fasync <020000>;: compatible with BSD fcntl synchronization operations
O_direct <040000>;: ID of the direct disk operation.
O_largefile <0100000>;: large file ID
O_directory <0200000>;: the directory must be
O_nofollow <0400000>;: the connection file is not obtained.
O_noatime <01000000>;: None
When creating a new file, you must specify the mode parameter. The format described below is macro definition name <actual constant value>;: description.
S_irwxu <00700>;: The file owner has read and write permissions.
S_irusr (s_iread) <00400>;: The file owner has only the read permission.
S_iwusr (s_iwrite) <00200>;: The file owner has only the write permission.
S_ixusr (s_iexec) <00100>;: The file owner has only the execution permission.
S_irwxg <00070>;: The group user has read and write permissions.
S_irgrp <00040>;: The group user has only the read permission.
S_iwgrp <00020>;: The group user has only the write permission.
S_ixgrp <00010>;: The group user has only the execution permission.
S_irwxo <00007>; other users have read and write permissions.
S_iroth <00004>;: other users have only the read permission.
S_iwoth <00002>;: other users only have write permission.
S_ixoth <00001>, I have used the kernel mode to access the device file. Now I will share with you the program. I hope it will be helpful to anyone who just got started. In my kernel module call driver, a simple character device file program is provided, which can be used as the driver object in this article. I will not introduce it much here. The module that calls the driver is as follows: # include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/STAT. h>
# Include <Linux/fs. h>
# Include <ASM/unistd. h>
# Include <ASM/uaccess. h>
# Include <Linux/types. h>
# Include <Linux/IOCTL. h>
# Include "chardev. H" module_license ("GPL"); // # DEFINE _ kernel_syscils __
# Define Bao "/dev/baovar"
Static char buf1 [20];
Static char buf2 [20];
Static int _ init testmod_init (void)
{
Mm_segment_t old_fs;
Ssize_t result;
Ssize_t ret;
Sprintf (buf1, "% s", "baoqunmin ");
Struct file * file = NULL;
File = filp_open (Bao, o_rdwr, 0 );
If (is_err (File) goto fail0;
Old_fs = get_fs ();
Set_fs (get_ds ());
Ret = file-> f_op-> write (file, buf1, sizeof (buf1), & file-> f_pos );
Result = file-> f_op-> Read (file, buf2, sizeof (buf2), & file-> f_pos );
If (result> = 0) {buf2 [20] = '/N'; printk ("buf2 --> % s/n", buf2 );}
Else printk ("failed/N ");
Result = file-> f_op-> IOCTL (file, buf2, sizeof (buf2), & file-> f_pos );
Result = file-> f_op-> Read (file, buf2, sizeof (buf2), & file-> f_pos );
Set_fs (old_fs );
Filp_close (file, null );
Printk ("file loaded/N ");
Return 0;
Fail0: {filp_close (file, null); printk ("load failed/N ");}
Return 1;
} Static void _ exit testmod_cleanup (void)
{
 
Printk ("module exit ................................... ................... /n ");
} Module_init (testmod_init );
Module_exit (testmod_cleanup); The above is a complete program that can be compiled and run directly. # Include "chardev. H" header file is defined as follows. This header file must also be included in the driver! # Include <Linux/IOCTL. h> # define bao_ioctl 'T'
# Define ioctl_read _ ior (bao_ioctl, 0, INT)
# Define ioctl_write _ Iow (bao_ioctl, 1, INT)
# Define bao_ioctl_maxnr 1 and below give my MAKEFILE file: CC = gcc
Modcflags: =-wall-dmodule-d1_kernel _-dlinux-I/usr/src/linux-2.4.20-8/include
Test. O: Test. c
$ (CC) $ (modcflags)-C test. c
Echo insmod test. O to turn it on
Echo rmmod test to turn it off
Echo 1. load the device driver first. My device file is Bao, 2. make the above file 3. /sbin/insmod test. O loading Module 4. dmesg to view the running result 5. /sbin/rmmod test6. uninstall the loaded driver. I successfully run the driver in Linux Red Hat 9.0. All I implement is to call the driver between modules. It can be said that this is the simplest example. I hope you will get something!

Previously, I thought that files could not be created in the kernel, but this is not the case. As long as the file system runs, it can operate files like in the user space. user space kernel open () sys_open (), filp_open () Close () sys_close (), filp_close () read () sys_read (), filp_read () write () sys_write (), filp_write () -------------------------------------------------- sometimes sys_xxxx cannot be used in the kernel module. Then, the function corresponding to filp_xxxx can be used. There is still a problem using sys_xxxx in the module. create an aastruct file * filep in/tmp;
Filep = filp_open ("/tmp/AA", o_creat | o_rdwr, 0 );
If (is_err (filep ))
Return-1;
Filp_close (filep, 0 );
Return 0; ========================================================== sys_open usage: # include <Linux/Fs. h> ...... u8 buff [50];
Int FD;
Memset (buff, 0x00, sizeof (buff ));
Mm_segment_t old_fs = get_fs ();
Set_fs (kernel_ds );
FD = sys_open ("/etc/INFO", o_rdonly, 0 );
If (FD> = 0)
{
Sys_read (FD, buff, 50 );
Printk ("string: % s/n", buff );
Sys_close (FD );}
Set_fs (old_fs ); ========================================================== the following open parameters are used: o_accmode <0003>;: when reading and writing files, it is used to retrieve the low 2 bits of the flag.
O_rdonly <00>;: Read-Only
O_wronly <01>;: Write-only open
O_rdwr <02>;: read/write Enabled
O_creat <0100>;: create if the file does not exist. mode_t, not fcntl is required.
O_excl <0200>;: if both o_creat is specified and the file already exists, an error occurs. Not fcntl
O_noctty <0400>;: If pathname indicates a terminal device, this device is not allocated as the control terminal for this process. Not fcntl o_trunc <01000>;: if the file exists and is read-only or write-only, the length of the file is truncated to 0. Not fcntl

O_append <02000>;: added to the end of the file every time the file is written.
O_nonblock <04000>;: If p a t h n a m e refers to an f I f o, a special block file, or a special character file, this option sets the non-blocking mode for this file's current open operation and subsequent I/O operations.

O_ndelay ;;
O_sync <010000>;: enables each write operation to wait until the physical I/O operation is completed.
Fasync <020000>;: compatible with BSD fcntl synchronization operations
O_direct <040000>;: ID of the direct disk operation.
O_largefile <0100000>;: large file ID
O_directory <0200000>;: the directory must be
O_nofollow <0400000>;: the connection file is not obtained.
O_noatime <01000000>;: None
When creating a new file, you must specify the mode parameter. The format described below is macro definition name <actual constant value>;: description.
S_irwxu <00700>;: The file owner has read and write permissions.
S_irusr (s_iread) <00400>;: The file owner has only the read permission.
S_iwusr (s_iwrite) <00200>;: The file owner has only the write permission.
S_ixusr (s_iexec) <00100>;: The file owner has only the execution permission.
S_irwxg <00070>;: The group user has read and write permissions.
S_irgrp <00040>;: The group user has only the read permission.
S_iwgrp <00020>;: The group user has only the write permission.
S_ixgrp <00010>;: The group user has only the execution permission.
S_irwxo <00007>; other users have read and write permissions.
S_iroth <00004>;: other users have only the read permission.
S_iwoth <00002>;: other users only have write permission.
S_ixoth <00001>, I have used the kernel mode to access the device file. Now I will share with you the program. I hope it will be helpful to anyone who just got started. In my kernel module call driver, a simple character device file program is provided, which can be used as the driver object in this article. I will not introduce it much here. The module that calls the driver is as follows: # include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/STAT. h>
# Include <Linux/fs. h>
# Include <ASM/unistd. h>
# Include <ASM/uaccess. h>
# Include <Linux/types. h>
# Include <Linux/IOCTL. h>
# Include "chardev. H" module_license ("GPL"); // # DEFINE _ kernel_syscils __
# Define Bao "/dev/baovar"
Static char buf1 [20];
Static char buf2 [20];
Static int _ init testmod_init (void)
{
Mm_segment_t old_fs;
Ssize_t result;
Ssize_t ret;
Sprintf (buf1, "% s", "baoqunmin ");
Struct file * file = NULL;
File = filp_open (Bao, o_rdwr, 0 );
If (is_err (File) goto fail0;
Old_fs = get_fs ();
Set_fs (get_ds ());
Ret = file-> f_op-> write (file, buf1, sizeof (buf1), & file-> f_pos );
Result = file-> f_op-> Read (file, buf2, sizeof (buf2), & file-> f_pos );
If (result> = 0) {buf2 [20] = '/N'; printk ("buf2 --> % s/n", buf2 );}
Else printk ("failed/N ");
Result = file-> f_op-> IOCTL (file, buf2, sizeof (buf2), & file-> f_pos );
Result = file-> f_op-> Read (file, buf2, sizeof (buf2), & file-> f_pos );
Set_fs (old_fs );
Filp_close (file, null );
Printk ("file loaded/N ");
Return 0;
Fail0: {filp_close (file, null); printk ("load failed/N ");}
Return 1;
} Static void _ exit testmod_cleanup (void)
{
 
Printk ("module exit ................................... ................... /n ");
} Module_init (testmod_init );
Module_exit (testmod_cleanup); The above is a complete program that can be compiled and run directly. # Include "chardev. H" header file is defined as follows. This header file must also be included in the driver! # Include <Linux/IOCTL. h> # define bao_ioctl 'T'
# Define ioctl_read _ ior (bao_ioctl, 0, INT)
# Define ioctl_write _ Iow (bao_ioctl, 1, INT)
# Define bao_ioctl_maxnr 1 and below give my MAKEFILE file: CC = gcc
Modcflags: =-wall-dmodule-d1_kernel _-dlinux-I/usr/src/linux-2.4.20-8/include
Test. O: Test. c
$ (CC) $ (modcflags)-C test. c
Echo insmod test. O to turn it on
Echo rmmod test to turn it off
Echo 1. load the device driver first. My device file is Bao, 2. make the above file 3. /sbin/insmod test. O loading Module 4. dmesg to view the running result 5. /sbin/rmmod test6. uninstall the loaded driver. I successfully run the driver in Linux Red Hat 9.0. All I implement is to call the driver between modules. It can be said that this is the simplest example. I hope you will get something!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.