Notes on advanced programming in UNIX environment-umask Function

Source: Internet
Author: User

The umask function creates a blocked word for the process setting file mode and returns the previous value. The function is defined as follows:

# Include <sys/stat. h> mode_t umask (mode_t cmask); // The function only returns a successful result: a blocked word is created in the previous file mode and no error is returned.

The value of cmask is the same as that of mode in open and creat functions:


If one of the blocked characters in file mode is set, the permission for this bit in the new file is disabled. If S_IRUSR | S_IWUSR is set in cmask,

The user's read and write operations are blocked in the new file.

In linux, you can run the umask command to view the setting file mode of the Current shell to create a blocking word.

Yan @ yan-vm :~ $ Umask
0002

What is the meaning of 0002? first look:


Therefore, 0002 blocks other write permissions. If it is 0077, the Group read/write execution and other read/write operations are blocked.


Practice:

#include <stdio.h>#include <sys/stat.h>#include <fcntl.h>int main(void){        umask(0);        if(creat("a",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)<0){                perror("creat1");                return -1;        }        umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);        if(creat("b",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)<0){                perror("creat2");                return -1;        }        return 0;}
Running result:

Yan @ yan-vm :~ /Apue $ ll a B
-Rw-1 yan 0 Jun 12 13:02
-Rw ------- 1 yan 0 Jun 12 13:02 B

Yan @ yan-vm :~ /Apue $ umask
0002

Finally, it is found that the umask value (0066) in the program does not take effect in the shell, because after the program is executed, the shell generates a sub-process,

In the child process, umask is set to 0066. After the child process ends, it returns to the parent process (shell). The shell umaks are still 0002.


int main(void){        umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH|S_ISUID|S_ISGID);        if(creat("a",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH|S_ISUID|S_ISGID)<0){                perror("creat");                return -1;        }        return 0;}

-RwS -- S --- 1 yan 0 Jun 12 13:32

The final conclusion is that umask still uses the nine parameters in the first figure. S_ISUID, S_ISGID, and S_ISVTX cannot be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.