1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 see <http://www.gnu.org/licenses/>. */
18 /* March 11, 2002 Manuel Novoa III
20 * Modify code to remove dependency on libgcc long long arith support funcs.
23 /* June 6, 2004 Erik Andersen
25 * Don't use brain damaged getpid() based randomness.
28 /* April 15, 2005 Mike Frysinger
30 * Use brain damaged getpid() if real random fails.
42 #include <sys/types.h>
47 /* Return nonzero if DIR is an existent directory. */
48 static int direxists (const char *dir)
51 return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
54 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
55 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
56 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
57 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
58 doesn't exist, none of the searched dirs exists, or there's not
59 enough space in TMPL. */
60 int ___path_search (char *tmpl, size_t tmpl_len, const char *dir,
61 const char *pfx /*, int try_tmpdir*/)
64 /* dir and pfx lengths should always fit into an int,
65 so don't bother using size_t here. Especially since
66 the printf func requires an int for precision (%*s). */
81 /* Disable support for $TMPDIR */
85 d = __secure_getenv ("TMPDIR");
86 if (d != NULL && direxists (d))
88 else if (dir != NULL && direxists (dir))
96 if (direxists (P_tmpdir))
98 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
102 __set_errno (ENOENT);
108 while (dlen > 1 && dir[dlen - 1] == '/')
109 dlen--; /* remove trailing slashes */
111 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
112 if (tmpl_len < (size_t)dlen + 1 + plen + 6 + 1)
114 __set_errno (EINVAL);
118 sprintf (tmpl, "%.*s/%.*sXXXXXX", dlen, dir, plen, pfx);
122 /* These are the characters used in temporary filenames. */
123 static const char letters[] =
124 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
125 #define NUM_LETTERS (62)
127 static unsigned int fillrand(unsigned char *buf, unsigned int len)
130 unsigned int result = -1;
131 fd = open("/dev/urandom", O_RDONLY);
133 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
136 result = read(fd, buf, len);
142 static void brain_damaged_fillrand(unsigned char *buf, unsigned int len)
146 uint32_t high, low, rh;
147 static uint64_t value;
148 gettimeofday(&tv, NULL);
149 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
150 low = value & UINT32_MAX;
152 for (i = 0; i < len; ++i) {
153 rh = high % NUM_LETTERS;
155 #define L ((UINT32_MAX % NUM_LETTERS + 1) % NUM_LETTERS)
156 k = (low % NUM_LETTERS) + (L * rh);
158 #define H ((UINT32_MAX / NUM_LETTERS) + ((UINT32_MAX % NUM_LETTERS + 1) / NUM_LETTERS))
159 low = (low / NUM_LETTERS) + (H * rh) + (k / NUM_LETTERS);
166 /* Generate a temporary file name based on TMPL. TMPL must match the
167 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
168 does not exist at the time of the call to __gen_tempname. TMPL is
169 overwritten with the result.
172 __GT_NOCREATE: simply verify that the name does not exist
173 at the time of the call. mode argument is ignored.
174 __GT_FILE: create the file using open(O_CREAT|O_EXCL)
175 and return a read-write fd with given mode.
176 __GT_BIGFILE: same as __GT_FILE but use open64().
177 __GT_DIR: create a directory with given mode.
180 int attribute_hidden __gen_tempname (char *tmpl, int kind, int flags, mode_t mode)
184 int fd, save_errno = errno;
185 unsigned char randomness[6];
189 /* This is where the Xs start. */
190 XXXXXX = tmpl + len - 6;
191 if (len < 6 || strcmp (XXXXXX, "XXXXXX"))
193 __set_errno (EINVAL);
197 for (i = 0; i < TMP_MAX; ++i) {
199 /* Get some random data. */
200 if (fillrand(randomness, sizeof(randomness)) != sizeof(randomness)) {
201 /* if random device nodes failed us, lets use the braindamaged ver */
202 brain_damaged_fillrand(randomness, sizeof(randomness));
204 for (j = 0; j < sizeof(randomness); ++j)
205 XXXXXX[j] = letters[randomness[j] % NUM_LETTERS];
211 if (stat (tmpl, &st) < 0) {
212 if (errno == ENOENT) {
214 goto restore_and_ret;
222 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | flags, mode);
224 #if defined __UCLIBC_HAS_LFS__
226 fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL | flags, mode);
230 fd = mkdir (tmpl, mode);
234 assert (! "invalid KIND in __gen_tempname");
239 __set_errno (save_errno);
242 else if (errno != EEXIST)
243 /* Any other error will apply also to other names we might
244 try, and there are 2^32 or so of them, so give up now. */
248 /* We got out of the loop because we ran out of combinations to try. */
249 __set_errno (EEXIST);