Open,close system Call

Source: Internet
Author: User
Tags goto

http://blog.chinaunix.net/space.php?uid=12567959&do=blog&id=161001

Open System call

The open () system calls a service routine that is a sys_open () function that receives the path name filename of the file to be opened, flags for the access mode, and the required license bit mask mode if the file is created. If the system call succeeds, it returns a file descriptor, which is an array of pointers to the file object current->files-> Fd_array or current-> files-> The index of the newly assigned file in the FDTABLE.FD; otherwise, returns-1.

All flags for the open () system call

---------------------------------------------------------------------

Include/asm-generic/fcntl.h

#defineO_ACCMODE00000003

#defineO_RDONLY 00000000/* Open for Read Only * *

#defineO_WRONLY 00000001/* For write only and open * *

#defineO_RDWR 00000002/* Open for Read and write * *

#ifndefO_CREAT/* Create it if the file does not exist/*

#defineO_CREAT 00000100/* Not FCNTL * *

#endif

#ifndefO_EXCL/* For the O_CREAT flag, failure if the file already exists

#defineO_EXCL 00000200/* Not FCNTL * *

#endif

#ifndefO_NOCTTY/* Never consider a file as a terminal * *

#defineO_NOCTTY 00000400/* Not FCNTL * *

#endif

#ifndefO_TRUNC/* Truncate file (delete all existing content) * *

#defineO_TRUNC 00001000/* Not FCNTL * *

#endif

#ifndefO_APPEND/* Always write at the end of the file * *

#defineO_APPEND 00002000

#endif

#ifndefO_NONBLOCK/* Non-blocking open * *

#defineO_NONBLOCK 00004000

#endif

#ifndefO_DSYNC/* Sync write (blocking until the physical write terminates) * *

#defineO_DSYNC 00010000/* Used to be o_sync, below * *

#endif

#ifndefFASYNC/* Notification of I/O events via signal//

#defineFASYNC 00020000/* fcntl, for BSD compatibility * *

#endif

#ifndefO_DIRECT

#defineO_DIRECT 00040000/* DIRECT DISK access hint * *

#endif

/* Large file (the file length is greater than the range off_t can represent but less than off64_t) * *

#ifndefO_LARGEFILE

#defineO_LARGEFILE 00100000

#endif

#ifndefO_DIRECTORY/* Failure If the file is not a directory

#defineO_DIRECTORY 00200000/* Must be a DIRECTORY * *

#endif

#ifndefO_NOFOLLOW///Do not resolve the symbolic link at the end of the path name * *

#defineO_NOFOLLOW 00400000/* Don t follow links */

#endif

#ifndefO_NOATIME/* Does not update the access time of the index node. */

#defineO_NOATIME01000000

#endif

#ifndefO_CLOEXEC

#defineO_CLOEXEC02000000/* Set CLOSE_ON_EXEC * *

#endif

---------------------------------------------------------------------

Some of the definitions of the logo are different from the architecture.

Sys_open () is defined as follows:

---------------------------------------------------------------------

Fs/open.c

Syscall_define3 (open,const char __user *, filename, int, flags, int, mode)

{

LONG ret;

if (Force_o_largefile ())

Flags |= O_largefile;

ret = Do_sys_open (AT_FDCWD, filename, flags,mode);

/* Avoid regparm breakage on x86: * *

Asmlinkage_protect (3, ret, filename, flags,mode);

return ret;

}

---------------------------------------------------------------------

The operation of this function is as follows:

First, call Force_o_largefile () to determine whether to support large files, if so, set the o_largefile bit of the flag. Force_o_largefile () is actually a macro. This macro is also different from the architecture.

Second, call Do_sys_open (AT_FDCWD, filename, flags, mode) to complete the actual task of opening the file. Here is a more detailed description.

Finally, call Asmlinkage_protect () to return the system call correctly. It is also a macro that is set to prevent compiler errors. Other platforms are empty, only the x86 platform is defined as:

---------------------------------------------------------------------

Arch/x86/include/asm/linkage.h

/*

* Make sure the compiler doesn ' t does anythingstupid with the

* Arguments on the Stack-they are owned bythe *caller*

* The callee. This just fools gcc to notspilling into them,

* and keeps it from doing tailcall recursionand/or using the

* Stack slots for temporaries, since they arelive and "used"

* All the way to the "end of" the function.

*

* note! On x86-64, all the arguments are inregisters

* Only matters on a 32-bit kernel.

* * #define ASMLINKAGE_PROTECT (n, ret, args ...) \

__asmlinkage_protect# #n (ret, # #args)

#define__asmlinkage_protect_n (ret, args ...) \

__asm__ __volatile__ ("": "=r" (ret): "0" (ret), # #args)

#define__asmlinkage_protect0 (ret) \

__asmlinkage_protect_n (ret)

#define__asmlinkage_protect1 (ret, arg1) \

__asmlinkage_protect_n (ret, "G" (Arg1))

#define__asmlinkage_protect2 (ret, arg1, arg2) \

__asmlinkage_protect_n (ret, "G" (Arg1), "G" (ARG2))

#define__asmlinkage_protect3 (ret, arg1, arg2, arg3) \

__asmlinkage_protect_n (ret, "G" (Arg1), "G" (Arg2), "G" (ARG3))

---------------------------------------------------------------------

The Do_sys_open () function is defined as follows:

---------------------------------------------------------------------

Fs/open.c

Longdo_sys_open (int DFD, const char __user *filename, int flags, int mode)

{

Char *tmp = getname (filename);

int FD = PTR_ERR (TMP);

if (!is_err (TMP)) {

FD = Get_unused_fd_flags (flags);

if (FD >= 0) {

struct File *f = Do_filp_open (dfd,tmp, flags, mode, 0);

if (Is_err (f)) {

PUT_UNUSED_FD (FD);

FD = Ptr_err (f);

} else {

Fsnotify_open (F->f_path.dentry);

Fd_install (FD, F);

}

}

Putname (TMP);

}

return FD;

}

---------------------------------------------------------------------

How familiar is the first parameter? The first parameter of the Do_path_lookup () function that looks up the pathname is also it, which describes the base directory where the path name is found. For this, the previous path name lookup is already explained. The rest of the arguments are Sys_open () that pass the incoming arguments to it.

This function performs the following actions:

1. Call getname (filename) to read the path name of the file from the process address space and store the path name address in local variable TMP. GetName (filename) essentially allocates the memory area from the slab cache named "Names_cache" and then copies the pathname from user space into that memory area.

2. Call Get_unused_fd_flags (flags) to find an empty space from the file descriptor table of the current process. It is defined as:

---------------------------------------------------------------------

Include/linux/file.h

#defineget_unused_fd_flags (Flags) alloc_fd (0, (flags))

---------------------------------------------------------------------

This is a macro, just the alias for the special parameter of the ALLOC_FD () function, and the ALLOC_FD () function is defined as follows:

---------------------------------------------------------------------

Fs/file.c

/*

* Allocate a file descriptor, mark it busy.

*/

INTALLOC_FD (unsigned start, unsigned flags)

{

struct Files_struct *files =current->files;

unsigned int fd;

int error;

struct fdtable *fdt;

Spin_lock (&files->file_lock);

Repeat

FDT = files_fdtable (files);

FD = start;

if (FD < FILES->NEXT_FD)

FD = files->next_fd;

if (FD < FDT->MAX_FDS)

FD = Find_next_zero_bit (Fdt->open_fds->fds_bits,

Fdt->max_fds, FD);

Error = expand_files (files, FD);

if (Error < 0)

Goto out;

/*

* Ifwe needed to expand the FS array we

*might have blocked-try again.

*/

if (Error)

Goto repeat;

if (Start <= files->next_fd)

FILES->NEXT_FD = fd + 1;

Fd_set (FD, Fdt->open_fds);

if (Flags & o_cloexec)

Fd_set (FD, fdt->close_on_exec);

Else

FD_CLR (FD, fdt->close_on_exec);

Error = FD;

#if1

/* Sanity Check * *

if (Rcu_dereference_raw (FDT->FD[FD])!=null) {

PRINTK (kern_warning "alloc_fd:slot%d not null!\n", FD);

Rcu_assign_pointer (FDT->FD[FD], NULL);

}

#endif

Out

Spin_unlock (&files->file_lock);

return error;

}

-------------------------------------------------------------

Related Article

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.