Created
June 21, 2014 01:32
-
-
Save user454322/9f32f69dd8caa8c972eb to your computer and use it in GitHub Desktop.
umask chmod open
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#define RW_UGO (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH| S_IWOTH) | |
int | |
main(int argc, char **argv, char **env) { | |
const char *path = "/tmp/test"; | |
const mode_t process_umask = umask(S_IWGRP ); | |
umask(S_IWGRP); | |
const mode_t mode = (S_IRUSR | S_IWUSR); | |
// final permission: 666 - 022 = 644 | |
// RW_UGO ^ process_umask = -rw-r--r-- | |
if (open(path, O_RDWR | O_CREAT | O_EXCL, mode) == -1) | |
err(1, "open for '%s' failed", path); | |
struct stat sb; | |
if (stat(path, &sb) != 0) | |
err(2, "stat failed"); | |
const mode_t file_mode = sb.st_mode & ACCESSPERMS; | |
const mode_t umasked_mode = (RW_UGO ^ process_umask) & mode; | |
if (umasked_mode != file_mode) | |
printf("umasked mode (%o) and file_mode (%o) are different\n", | |
umasked_mode, file_mode); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment