Tuesday, 24 January 2012

open() using O_WRONLY | O_RDWR

One of the lesser known Linux features is that one can open a file with the flags O_WRONLY | O_RDWR.   One requires read and write permission to perform the open(), however, the flags indicate that no reading or writing is to be done on the file descriptor.   It is useful for operations such as ioctl() where we also want to ensure we don't actually do any reading or writing to a device.  A bunch of utilities such as LILO seem to use this obscure feature. 

LILO defines these flags as O_NOACCESS as follows:

 #ifdef O_ACCMODE  
 /* open a file for "no access" */  
 # define O_NOACCESS 3  

..as in this example, you may find these flags more widely known as O_NOACCESS even though they are not defined in the standard fcntl.h headers.

Below is a very simple example of the use of O_WRONLY | O_RDWR:

 #include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 #include <sys/ioctl.h>  
 #include <fcntl.h>  
 int main(int argc, char **argv)  
      int fd;  
      struct winsize ws;  
      if ((fd = open("/dev/tty", O_WRONLY | O_RDWR)) < 0) {  
           perror("open /dev/tty failed");  
      if (ioctl(fd, TIOCGWINSZ, &ws) == 0)  
           printf("%d x %d\n", ws.ws_row, ws.ws_col);  

It is a little arcane and not portable but also an interesting feature to know about.


  1. Good blog post, but i am somewhat confused.

    Is this just talking about opening a file descriptor in read only or read/write mode? If so, are people really not using this on a regular basis?

    1. I'm opening using O_WRONLY | O_RDWR (which is flag 1 | 2, in other words flags == 3). The semantics here are peculiar, O_WRONLY would normally mean just Write Only, but we are also or'ing in O_RDWR (Read and Write) - which one would think are not compatible with O_WRONLY.

  2. Ahh, i see! That makes much more sense.. thanks. :)
    -- Daviey