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.
34 #define open64 __open64
36 #define gettimeofday __gettimeofday
47 #include <sys/types.h>
53 /* Return nonzero if DIR is an existent directory. */
54 static int direxists (const char *dir)
57 return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
60 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
61 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
62 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
63 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
64 doesn't exist, none of the searched dirs exists, or there's not
65 enough space in TMPL. */
66 int attribute_hidden __path_search (char *tmpl, size_t tmpl_len, const char *dir,
67 const char *pfx, int try_tmpdir)
79 plen = __strlen (pfx);
84 /* Disable support for $TMPDIR */
88 d = __secure_getenv ("TMPDIR");
89 if (d != NULL && direxists (d))
91 else if (dir != NULL && direxists (dir))
99 if (direxists (P_tmpdir))
101 else if (__strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
105 __set_errno (ENOENT);
110 dlen = __strlen (dir);
111 while (dlen > 1 && dir[dlen - 1] == '/')
112 dlen--; /* remove trailing slashes */
114 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
115 if (tmpl_len < dlen + 1 + plen + 6 + 1)
117 __set_errno (EINVAL);
121 __sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
125 /* These are the characters used in temporary filenames. */
126 static const char letters[] =
127 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
129 static unsigned int fillrand(unsigned char *buf, unsigned int len)
132 unsigned int result = -1;
133 fd = __open("/dev/urandom", O_RDONLY);
135 fd = __open("/dev/random", O_RDONLY | O_NONBLOCK);
138 result = __read(fd, buf, len);
144 static void brain_damaged_fillrand(unsigned char *buf, unsigned int len)
148 uint32_t high, low, rh;
149 static uint64_t value;
150 gettimeofday(&tv, NULL);
151 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ __getpid();
152 low = value & UINT32_MAX;
154 for (i = 0; i < len; ++i) {
157 #define L ((UINT32_MAX % 62 + 1) % 62)
158 k = (low % 62) + (L * rh);
160 #define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62))
161 low = (low / 62) + (H * rh) + (k / 62);
168 /* Generate a temporary file name based on TMPL. TMPL must match the
169 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
170 does not exist at the time of the call to __gen_tempname. TMPL is
171 overwritten with the result.
174 __GT_NOCREATE: simply verify that the name does not exist
175 at the time of the call.
176 __GT_FILE: create the file using open(O_CREAT|O_EXCL)
177 and return a read-write fd. The file is mode 0600.
178 __GT_BIGFILE: same as __GT_FILE but use open64().
179 __GT_DIR: create a directory, which will be mode 0700.
182 int attribute_hidden __gen_tempname (char *tmpl, int kind)
186 int len, i, count, fd, save_errno = errno;
187 unsigned char randomness[6];
189 len = __strlen (tmpl);
190 if (len < 6 || __strcmp (&tmpl[len - 6], "XXXXXX"))
192 __set_errno (EINVAL);
196 /* This is where the Xs start. */
197 XXXXXX = &tmpl[len - 6];
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 (i = 0 ; i < sizeof(randomness) ; i++) {
205 k = ((randomness[i]) % 62);
206 XXXXXX[i] = letters[k];
209 for (count = 0; count < TMP_MAX; ++count)
215 if (stat (tmpl, &st) < 0)
219 __set_errno (save_errno);
230 fd = __open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
232 #if defined __UCLIBC_HAS_LFS__
234 fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
238 fd = mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
242 assert (! "invalid KIND in __gen_tempname");
246 __set_errno (save_errno);
249 else if (errno != EEXIST)
250 /* Any other error will apply also to other names we might
251 try, and there are 2^32 or so of them, so give up now. */
255 /* We got out of the loop because we ran out of combinations to try. */
256 __set_errno (EEXIST);