This source file includes following definitions.
- random_bits
- direxists
- __path_search
- try_file
- try_dir
- try_nocreate
- gen_tempname_len
- try_tempname_len
- __gen_tempname
- try_tempname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #if !_LIBC
19 # include <libc-config.h>
20 # include "tempname.h"
21 #endif
22
23 #include <sys/types.h>
24 #include <assert.h>
25 #include <stdbool.h>
26
27 #include <errno.h>
28
29 #include <stdio.h>
30 #ifndef P_tmpdir
31 # define P_tmpdir "/tmp"
32 #endif
33 #ifndef TMP_MAX
34 # define TMP_MAX 238328
35 #endif
36 #ifndef __GT_FILE
37 # define __GT_FILE 0
38 # define __GT_DIR 1
39 # define __GT_NOCREATE 2
40 #endif
41 #if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \
42 || GT_NOCREATE != __GT_NOCREATE)
43 # error report this to bug-gnulib@gnu.org
44 #endif
45
46 #include <stddef.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <fcntl.h>
51 #include <stdalign.h>
52 #include <stdint.h>
53 #include <sys/random.h>
54 #include <sys/stat.h>
55 #include <time.h>
56
57 #if _LIBC
58 # define struct_stat64 struct stat64
59 # define __secure_getenv __libc_secure_getenv
60 #else
61 # define struct_stat64 struct stat
62 # define __gen_tempname gen_tempname
63 # define __mkdir mkdir
64 # define __open open
65 # define __lstat64(file, buf) lstat (file, buf)
66 # define __stat64(file, buf) stat (file, buf)
67 # define __getrandom getrandom
68 # define __clock_gettime64 clock_gettime
69 # define __timespec64 timespec
70 #endif
71
72
73
74
75 typedef uint_fast64_t random_value;
76 #define RANDOM_VALUE_MAX UINT_FAST64_MAX
77 #define BASE_62_DIGITS 10
78 #define BASE_62_POWER (62LL * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62)
79
80 static random_value
81 random_bits (random_value var, bool use_getrandom)
82 {
83 random_value r;
84
85 if (use_getrandom && __getrandom (&r, sizeof r, GRND_NONBLOCK) == sizeof r)
86 return r;
87 #if _LIBC || (defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME)
88
89 struct __timespec64 tv;
90 __clock_gettime64 (CLOCK_MONOTONIC, &tv);
91 var ^= tv.tv_nsec;
92 #endif
93 return 2862933555777941757 * var + 3037000493;
94 }
95
96 #if _LIBC
97
98 static int
99 direxists (const char *dir)
100 {
101 struct_stat64 buf;
102 return __stat64 (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
103 }
104
105
106
107
108
109
110
111 int
112 __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
113 int try_tmpdir)
114 {
115 const char *d;
116 size_t dlen, plen;
117
118 if (!pfx || !pfx[0])
119 {
120 pfx = "file";
121 plen = 4;
122 }
123 else
124 {
125 plen = strlen (pfx);
126 if (plen > 5)
127 plen = 5;
128 }
129
130 if (try_tmpdir)
131 {
132 d = __secure_getenv ("TMPDIR");
133 if (d != NULL && direxists (d))
134 dir = d;
135 else if (dir != NULL && direxists (dir))
136 ;
137 else
138 dir = NULL;
139 }
140 if (dir == NULL)
141 {
142 if (direxists (P_tmpdir))
143 dir = P_tmpdir;
144 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
145 dir = "/tmp";
146 else
147 {
148 __set_errno (ENOENT);
149 return -1;
150 }
151 }
152
153 dlen = strlen (dir);
154 while (dlen > 1 && dir[dlen - 1] == '/')
155 dlen--;
156
157
158 if (tmpl_len < dlen + 1 + plen + 6 + 1)
159 {
160 __set_errno (EINVAL);
161 return -1;
162 }
163
164 sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
165 return 0;
166 }
167 #endif
168
169 #if _LIBC
170 static int try_tempname_len (char *, int, void *, int (*) (char *, void *),
171 size_t);
172 #endif
173
174 static int
175 try_file (char *tmpl, void *flags)
176 {
177 int *openflags = flags;
178 return __open (tmpl,
179 (*openflags & ~O_ACCMODE)
180 | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
181 }
182
183 static int
184 try_dir (char *tmpl, _GL_UNUSED void *flags)
185 {
186 return __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
187 }
188
189 static int
190 try_nocreate (char *tmpl, _GL_UNUSED void *flags)
191 {
192 struct_stat64 st;
193
194 if (__lstat64 (tmpl, &st) == 0 || errno == EOVERFLOW)
195 __set_errno (EEXIST);
196 return errno == ENOENT ? 0 : -1;
197 }
198
199
200 static const char letters[] =
201 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 #ifdef _LIBC
218 static
219 #endif
220 int
221 gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind,
222 size_t x_suffix_len)
223 {
224 static int (*const tryfunc[]) (char *, void *) =
225 {
226 [__GT_FILE] = try_file,
227 [__GT_DIR] = try_dir,
228 [__GT_NOCREATE] = try_nocreate
229 };
230 return try_tempname_len (tmpl, suffixlen, &flags, tryfunc[kind],
231 x_suffix_len);
232 }
233
234 #ifdef _LIBC
235 static
236 #endif
237 int
238 try_tempname_len (char *tmpl, int suffixlen, void *args,
239 int (*tryfunc) (char *, void *), size_t x_suffix_len)
240 {
241 size_t len;
242 char *XXXXXX;
243 unsigned int count;
244 int fd = -1;
245 int save_errno = errno;
246
247
248
249
250
251
252
253
254 #define ATTEMPTS_MIN (62 * 62 * 62)
255
256
257
258 #if ATTEMPTS_MIN < TMP_MAX
259 unsigned int attempts = TMP_MAX;
260 #else
261 unsigned int attempts = ATTEMPTS_MIN;
262 #endif
263
264
265
266
267
268 random_value v = ((uintptr_t) &v) / alignof (max_align_t);
269
270
271 int vdigits = 0;
272
273
274
275
276
277
278 bool use_getrandom = tryfunc == try_nocreate;
279
280
281
282 random_value const unfair_min
283 = RANDOM_VALUE_MAX - RANDOM_VALUE_MAX % BASE_62_POWER;
284
285 len = strlen (tmpl);
286 if (len < x_suffix_len + suffixlen
287 || strspn (&tmpl[len - x_suffix_len - suffixlen], "X") < x_suffix_len)
288 {
289 __set_errno (EINVAL);
290 return -1;
291 }
292
293
294 XXXXXX = &tmpl[len - x_suffix_len - suffixlen];
295
296 for (count = 0; count < attempts; ++count)
297 {
298 for (size_t i = 0; i < x_suffix_len; i++)
299 {
300 if (vdigits == 0)
301 {
302 do
303 {
304 v = random_bits (v, use_getrandom);
305 use_getrandom = true;
306 }
307 while (unfair_min <= v);
308
309 vdigits = BASE_62_DIGITS;
310 }
311
312 XXXXXX[i] = letters[v % 62];
313 v /= 62;
314 vdigits--;
315 }
316
317 fd = tryfunc (tmpl, args);
318 if (fd >= 0)
319 {
320 __set_errno (save_errno);
321 return fd;
322 }
323 else if (errno != EEXIST)
324 return -1;
325 }
326
327
328 __set_errno (EEXIST);
329 return -1;
330 }
331
332 int
333 __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
334 {
335 return gen_tempname_len (tmpl, suffixlen, flags, kind, 6);
336 }
337
338 #if !_LIBC
339 int
340 try_tempname (char *tmpl, int suffixlen, void *args,
341 int (*tryfunc) (char *, void *))
342 {
343 return try_tempname_len (tmpl, suffixlen, args, tryfunc, 6);
344 }
345 #endif