This source file includes following definitions.
- getumask
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 #include <config.h>
35
36
37
38
39
40 #include <sys/stat.h>
41
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include "clean-temp.h"
48 #include "tempname.h"
49
50 mode_t
51 getumask (void)
52 {
53 #if 0
54
55 mode_t mask = umask (0);
56 umask (mask);
57 return mask;
58 #else
59 # if ASSUME_UMASK_CONSTANT
60 static int cached_umask = -1;
61 if (cached_umask >= 0)
62 return cached_umask;
63 # endif
64
65 int mask = -1;
66 # if defined __linux__
67 {
68
69
70 char buf[4096];
71 int fd = open ("/proc/self/status", O_RDONLY);
72 if (fd >= 0)
73 {
74 ssize_t n = read (fd, buf, sizeof (buf));
75 if (n > 0)
76 {
77 const char *p_end = buf + n;
78 const char *p = buf;
79
80 for (;;)
81 {
82
83 if (p_end - p > 8 && memcmp (p, "Umask:\t0", 8) == 0)
84 {
85 unsigned int value = 0;
86 p += 8;
87 for (; p < p_end && *p >= '0' && *p <= '7'; p++)
88 value = 8 * value + (*p - '0');
89 if (p < p_end && *p == '\n')
90 mask = value;
91 break;
92 }
93
94 for (; p < p_end && *p != '\n'; p++)
95 ;
96 if (p == p_end)
97 break;
98 p++;
99 }
100 }
101 close (fd);
102 }
103 }
104 # endif
105 if (mask < 0)
106 {
107
108 const char *tmpdir = getenv ("TMPDIR");
109 if (tmpdir == NULL || *tmpdir == '\0')
110 tmpdir = "/tmp";
111 size_t tmpdir_length = strlen (tmpdir);
112 char *temp_filename = (char *) malloc (tmpdir_length + 15 + 1);
113 if (temp_filename != NULL)
114 {
115 memcpy (temp_filename, tmpdir, tmpdir_length);
116 strcpy (temp_filename + tmpdir_length, "/gtumask.XXXXXX");
117 int fd = gen_register_open_temp (temp_filename, 0, O_RDWR,
118 S_IRWXU|S_IRWXG|S_IRWXO);
119 if (fd >= 0)
120 {
121 struct stat statbuf;
122 if (fstat (fd, &statbuf) >= 0)
123 mask = (S_IRWXU|S_IRWXG|S_IRWXO) & ~statbuf.st_mode;
124 close_temp (fd);
125 cleanup_temporary_file (temp_filename, false);
126 }
127 free (temp_filename);
128 }
129 }
130 if (mask < 0)
131 {
132
133 mask = 077;
134 }
135 # if ASSUME_UMASK_CONSTANT
136 cached_umask = mask;
137 # endif
138 return mask;
139 #endif
140 }