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 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* March 11, 2002 Manuel Novoa III
21 * Modify code to remove dependency on libgcc long long arith support funcs.
24 /* June 6, 2004 Erik Andersen
26 * Don't use brain damaged getpid() based randomness.
29 /* April 15, 2005 Mike Frysinger
31 * Use brain damaged getpid() if real random fails.
43 #include <sys/types.h>
48 /* Return nonzero if DIR is an existent directory. */
49 static int direxists (const char *dir)
52 return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
55 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
56 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
57 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
58 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
59 doesn't exist, none of the searched dirs exists, or there's not
60 enough space in TMPL. */
61 int attribute_hidden ___path_search (char *tmpl, size_t tmpl_len, const char *dir,
62 const char *pfx /*, int try_tmpdir*/)
79 /* Disable support for $TMPDIR */
83 d = __secure_getenv ("TMPDIR");
84 if (d != NULL && direxists (d))
86 else if (dir != NULL && direxists (dir))
94 if (direxists (P_tmpdir))
96 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
100 __set_errno (ENOENT);
106 while (dlen > 1 && dir[dlen - 1] == '/')
107 dlen--; /* remove trailing slashes */
109 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
110 if (tmpl_len < dlen + 1 + plen + 6 + 1)
112 __set_errno (EINVAL);
116 sprintf (tmpl, "%.*s/%.*sXXXXXX", (int)dlen, dir, (int)plen, pfx);
120 /* These are the characters used in temporary filenames. */
121 static const char letters[] =
122 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
123 #define NUM_LETTERS (62)
125 static unsigned int fillrand(unsigned char *buf, unsigned int len)
128 unsigned int result = -1;
129 fd = open("/dev/urandom", O_RDONLY);
131 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
134 result = read(fd, buf, len);
140 static void brain_damaged_fillrand(unsigned char *buf, unsigned int len)
144 uint32_t high, low, rh;
145 static uint64_t value;
146 gettimeofday(&tv, NULL);
147 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
148 low = value & UINT32_MAX;
150 for (i = 0; i < len; ++i) {
151 rh = high % NUM_LETTERS;
153 #define L ((UINT32_MAX % NUM_LETTERS + 1) % NUM_LETTERS)
154 k = (low % NUM_LETTERS) + (L * rh);
156 #define H ((UINT32_MAX / NUM_LETTERS) + ((UINT32_MAX % NUM_LETTERS + 1) / NUM_LETTERS))
157 low = (low / NUM_LETTERS) + (H * rh) + (k / NUM_LETTERS);
164 /* Generate a temporary file name based on TMPL. TMPL must match the
165 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
166 does not exist at the time of the call to __gen_tempname. TMPL is
167 overwritten with the result.
170 __GT_NOCREATE: simply verify that the name does not exist
171 at the time of the call. mode argument is ignored.
172 __GT_FILE: create the file using open(O_CREAT|O_EXCL)
173 and return a read-write fd with given mode.
174 __GT_BIGFILE: same as __GT_FILE but use open64().
175 __GT_DIR: create a directory with given mode.
178 int __gen_tempname (char *tmpl, int kind, mode_t mode)
182 int fd, save_errno = errno;
183 unsigned char randomness[6];
187 /* This is where the Xs start. */
188 XXXXXX = tmpl + len - 6;
189 if (len < 6 || strcmp (XXXXXX, "XXXXXX"))
191 __set_errno (EINVAL);
195 for (i = 0; i < TMP_MAX; ++i) {
197 /* Get some random data. */
198 if (fillrand(randomness, sizeof(randomness)) != sizeof(randomness)) {
199 /* if random device nodes failed us, lets use the braindamaged ver */
200 brain_damaged_fillrand(randomness, sizeof(randomness));
202 for (j = 0; j < sizeof(randomness); ++j)
203 XXXXXX[j] = letters[randomness[j] % NUM_LETTERS];
209 if (stat (tmpl, &st) < 0) {
210 if (errno == ENOENT) {
212 goto restore_and_ret;
220 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, mode);
222 #if defined __UCLIBC_HAS_LFS__
224 fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, mode);
228 fd = mkdir (tmpl, mode);
232 assert (! "invalid KIND in __gen_tempname");
237 __set_errno (save_errno);
240 else if (errno != EEXIST)
241 /* Any other error will apply also to other names we might
242 try, and there are 2^32 or so of them, so give up now. */
246 /* We got out of the loop because we ran out of combinations to try. */
247 __set_errno (EEXIST);