Search code examples
c++linuxfile-permissionspcaplibpcap

pcap_dump() lower permission to user only


i'm working on improve a given code (don't know who wrote that )

i'm using pcap_dump()and #include <pcap.h> to capture and wri files to disk. The file that was writing is with read permission also for group and other (-rw-r--r--)

Is there a way to change it to user read/write only (-rw-------) or that i'll have to do a workaround. thanks


Solution

  • new files are created using permissions from the current umask. umask specifies which permissions are not allowed, e.g. umask 077 means no permissions are allowed for group/other, 022 (the default) means write permissions are not allowed for group/other

    You can set it in the shell before running your program, i.e.

    umask 077
    ./pcap_program
    

    you can also set it inside the program like this:

     #include <sys/types.h>
     #include <sys/stat.h>
    
     void foo() {
         umask(0077);  
     }
    

    note: the 0 at the start is required to make the number an octal number.

    These will both set new file's permissions to 600 (= 0666 & ~0077), or -rw-------