One of the many System calls in the C language there is “umask” which is specially built up for the set up of document creation mask. It defines the privileges or rights assigned to a specific file or directory at the time of creation. It is a security control by C language to restrict other system processes to assign extra rights to new file creations.
The system call of “umask” works on the digit pattern within the Linux shell. Let’s have a look at simple examples to demonstrate the “umask” system call. Start by login in from the Ubuntu 20.04 system. Open the shell terminal and set the required mask value for creating files and folders, say “0002”. This indicates that users and groups can read, write and execute the file created from now on. Although others can not write as “2” is for “write”.
Let’s create a new C language file to explore the umask system call in it. The file has been created with the “touch” query in the shell. After that, the GNU nano editor was used to open and edit the newly created file “unmask.c” with “nano” instruction.
The file “umask.c” has been opened in the editor for use. We have added the shown C code to it. This code contains the POSIX library in the header file section. Different input-output stream headers and file type headers have been included to make the code work properly. After that, the main method was initialized. Within the main method, we have defined a file descriptor “fd” of type integer. This code will be used to check the old or recent mask used or assigned to the file and folders. So, we have used the “mode_t” type variable “old” to fetch the current mask value. This old mask value will be printed out in the shell using the “printf” statement. The “S_IRWXG” argument function has been passed to the method “umask” to get the current value and saved it into the variable “old” for display.
The “if” statement has been used to check whether the file descriptor “fd” has been used to create a new file “new.txt” with the mask value less than 0 or not. If the condition satisfies, it will go through an exception that the Create function got an issue. The system will print out the mask value currently assigned to a new file “new.txt” within the else statement using the “ls –l” command. The file descriptor has been locked, and the file has been unlinked from the system then. The file has been saved using “Ctrl+S” and quits using “Ctrl+X”.
Let’s compile the C code first. For compilation, you must have some supported compiler installed in your Linux system. We have installed the GCC compiler. So, use the GCC command and the name of a C file, e.g., “unmask.c”.
Now the file should be executed after compilation to see the results. The “a.out” command has been executed in the terminal for this purpose. The output displays the old mask created for the files and folder as “2”. Also, it shows the privileges assigned to the file “new.txt” using this mask as per the output.
Let’s change the mask value to “777” indicating “no rights” to file and folders to be created.
Open the same file and edit the name of a file as “new.file” to create a new file. The remaining code is left unchanged.
Upon the compilation of the same file once again, we have got the error. This is because our current mask value is “777” which indicates no read, write, and execution rights to any user, group, or others.
Let’s change the mask value for the creation of files and directories from now on to 0777.
After the setup of mask value, let’s create a directory named “two”. Upon checking the privileges for directory “two”, we can see that it contains no rights.
This article contains the working of the “umask()” system call using C language. The concept of the Umask() system has been explained in detail. We have demonstrated its work by using a simple code example to create a file and compilation in the Ubuntu 20.04 Linux system. The same can be applied to a directory as well.