Linux fcntl - unsetting flag

Asked
Active3 hr before
Viewed126 times

7 Answers

linux
90%

If you do like this,The already set O_NONBLOCK will unset. here,flags contains the which flags you want to unset. After finishing the AND(&) operation,again you have to set the flag using the value in val. I hope this will help you.,Now, i want to unset the O_NONBLOCK flag.,The following code will unset a flag, for example, the O_NONBLOCK flag:, Unsetting all flags is a bit overkill if you just want to unset the nonblocking flag. :-) – Chris Jester-Young Dec 23 '08 at 9:09

int oldfl;
oldfl = fcntl(sockfd, F_GETFL);
if (oldfl == -1) {
   /* handle error */
}
fcntl(sockfd, F_SETFL, oldfl & ~O_NONBLOCK);
load more v
88%

For details of in-depth Linux/UNIX system programming training courses that I teach, look here.

FCNTL(2) Linux Programmer 's Manual               FCNTL(2)
load more v
72%

How do i unset a already set flag using fcntl?,For e.g. I can set the socket to nonblocking mode using ,Now, i want to unset the O_NONBLOCK flag.,Also OR-ing the flags with ~O_NONBLOCK is of no use, you need to AND it, since what you want is to unset the O_NONBLOCK bit(s).

For e.g. I can set the socket to nonblocking mode using

fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)
load more v
65%

The following example demonstrates how to set the close-on-exec flag for the file descriptor fd.,The available values for cmd are defined in <fcntl.h> and are as follows:,The fcntl() function shall perform the operations described below on open files. The fildes argument is a file descriptor.,Additional implementation-defined values for cmd may be defined in <fcntl.h>. Their names shall start with F_.

The following example demonstrates how to place a lock on bytes 100 to 109 of a file and then later remove it. F_SETLK is used to perform a non-blocking lock request so that the process does not have to wait if an incompatible lock is held by another process; instead the process can take some other action.

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

int
main(int argc, char *argv[])
{
    int fd;
    struct flock fl;

    fd = open("testfile", O_RDWR);
    if (fd == -1)
        /* Handle error */;

    /* Make a non-blocking request to place a write lock
       on bytes 100-109 of testfile */

    fl.l_type = F_WRLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 100;
    fl.l_len = 10;

    if (fcntl(fd, F_SETLK, &fl) == -1) {
        if (errno == EACCES || errno == EAGAIN) {
            printf("Already locked by another process\n");

            /* We can't get the lock at the moment */

        } else {
            /* Handle unexpected error */;
        }
    } else { /* Lock was granted... */

        /* Perform I/O on bytes 100 to 109 of file */

        /* Unlock the locked bytes */

        fl.l_type = F_UNLCK;
        fl.l_whence = SEEK_SET;
        fl.l_start = 100;
        fl.l_len = 10;
        if (fcntl(fd, F_SETLK, &fl) == -1)
            /* Handle error */;
    }
    exit(EXIT_SUCCESS);
} /* main */

load more v
75%

// ...

int sd, v;

// ...

v = fcntl(sd, F_GETFD, 0);
fcntl(sd, F_SETFD, v | O_NONBLOCK);

// ...
40%

The fcntl function can fetch or change file status flags. ,If you want to modify the file status flags, you should get the current flags with F_GETFL and modify the value. Don’t assume that the flags listed here are the only ones that are implemented; your program may be run years from now and more flags may exist then. For example, here is a function to set or clear the flag O_NONBLOCK without altering any other flags: ,In case of an error, fcntl returns -1. The following errno error conditions are defined for this command: ,This macro is used as the command argument to fcntl, to read the file status flags for the open file with descriptor filedes.

fcntl(filedes, F_SETFL, new - flags)
load more v
22%

Too many segment locks open, lock table is full, or a remote locking protocol failed (e.g., locking over NFS). ,Attempted to clear the O_APPEND flag on a file that has the append-only attribute set. ,Advisory locks are not enforced and are useful only between cooperating processes. ,The Linux implementation of mandatory locking is unreliable. See BUGS below.

struct flock {
   ...
   short l_type;
   /* Type of lock: F_RDLCK,
                       F_WRLCK, F_UNLCK */
   short l_whence;
   /* How to interpret l_start:
                         SEEK_SET, SEEK_CUR, SEEK_END */
   off_t l_start; /* Starting offset for lock */
   off_t l_len; /* Number of bytes to lock */
   pid_t l_pid;
   /* PID of process blocking our lock
                      (F_GETLK only) */
   ...
};
load more v

Other "linux-undefined" queries related to "Linux fcntl - unsetting flag"