Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux fcntl - unsetting flag

Tags:

linux

fcntl

How do i unset a already set flag using fcntl?

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

fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)

Now, i want to unset the O_NONBLOCK flag.

I tried fcntl(sockfd, F_SETFL, flags | ~O_NONBLOCK). It gave me error EINVAL

like image 828
chappar Avatar asked Dec 23 '08 08:12

chappar


2 Answers

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

Untested, but hope this helps. :-)

like image 194
Chris Jester-Young Avatar answered Sep 20 '22 20:09

Chris Jester-Young


val = fcntl(fd, F_GETFL, 0);
flags = O_NONBLOCK;
val &= ~flags;
fcntl(fd,F_SETFL,val);

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.

like image 39
sat Avatar answered Sep 17 '22 20:09

sat