c - Usage of F_GETFL and F_SETFL

前端 未结 2 1851
你的背包
你的背包 2021-02-09 00:32

While trying to use fcntl() with command F_GETFL and F_SETFL, I got some questions:

  1. Why the flag returned by fcntl(

2条回答
  •  别跟我提以往
    2021-02-09 01:18

    1) The return of fcnl is a code that described if the function succceded and how:

    RETURN VALUE

       For a successful call, the return value depends on the operation:
    
       F_DUPFD  The new descriptor.
    
       F_GETFD  Value of file descriptor flags.
    
       F_GETFL  Value of file status flags.
    
       F_GETLEASE
                Type of lease held on file descriptor.
    
       F_GETOWN Value of descriptor owner.
    
       F_GETSIG Value of signal sent when read or write becomes possible, or
                zero for traditional SIGIO behavior.
    
       F_GETPIPE_SZ, F_SETPIPE_SZ
                The pipe capacity.
    
       F_GET_SEALS
                A bit mask identifying the seals that have been set for the
                inode referred to by fd.
    
       All other commands
                Zero.
    
       On error, -1 is returned, and errno is set appropriately.
    

    ERRORS

       EACCES or EAGAIN
              Operation is prohibited by locks held by other processes.
    
       EAGAIN The operation is prohibited because the file has been memory-
              mapped by another process.
    
       EBADF  fd is not an open file descriptor
    
       EBADF  cmd is F_SETLK or F_SETLKW and the file descriptor open mode
              doesn't match with the type of lock requested.
    
       EBUSY  cmd is F_SETPIPE_SZ and the new pipe capacity specified in arg
              is smaller than the amount of buffer space currently used to
              store data in the pipe.
    
       EBUSY  cmd is F_ADD_SEALS, arg includes F_SEAL_WRITE, and there
              exists a writable, shared mapping on the file referred to by
              fd.
    
       EDEADLK
              It was detected that the specified F_SETLKW command would
              cause a deadlock.
    
       EFAULT lock is outside your accessible address space.
    
       EINTR  cmd is F_SETLKW or F_OFD_SETLKW and the operation was
              interrupted by a signal; see signal(7).
    
       EINTR  cmd is F_GETLK, F_SETLK, F_OFD_GETLK, or F_OFD_SETLK, and the
              operation was interrupted by a signal before the lock was
              checked or acquired.  Most likely when locking a remote file
              (e.g., locking over NFS), but can sometimes happen locally.
    
       EINVAL The value specified in cmd is not recognized by this kernel.
    
       EINVAL cmd is F_ADD_SEALS and arg includes an unrecognized sealing
              bit.
    
       EINVAL cmd is F_ADD_SEALS or F_GET_SEALS and the filesystem
              containing the inode referred to by fd does not support
              sealing.
    
       EINVAL cmd is F_DUPFD and arg is negative or is greater than the
              maximum allowable value (see the discussion of RLIMIT_NOFILE
              in getrlimit(2)).
    
       EINVAL cmd is F_SETSIG and arg is not an allowable signal number.
    
       EINVAL cmd is F_OFD_SETLK, F_OFD_SETLKW, or F_OFD_GETLK, and l_pid
              was not specified as zero.
    
       EMFILE cmd is F_DUPFD and the process already has the maximum number
              of file descriptors open.
    
       ENOLCK Too many segment locks open, lock table is full, or a remote
              locking protocol failed (e.g., locking over NFS).
    
       ENOTDIR
              F_NOTIFY was specified in cmd, but fd does not refer to a
              directory.
    
       EPERM  Attempted to clear the O_APPEND flag on a file that has the
              append-only attribute set.
    
       EPERM  cmd was F_ADD_SEALS, but fd was not open for writing or the
              current set of seals on the file already includes F_SEAL_SEAL.
    

    2) Flags to be set is your choice: :

    F_SETFL (int)

       Set the file status flags to the value specified by arg.  File
       access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation
       flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are
       ignored.  On Linux this command can change only the O_APPEND,
       O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.  It is not
       possible to change the O_DSYNC and O_SYNC flags; see BUGS,
       below.
    

    3) HERE you have a complete description.

提交回复
热议问题