| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. | 
|  | 2 | This file is part of the GNU C Library. | 
|  | 3 |  | 
|  | 4 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 5 | modify it under the terms of the GNU Lesser General Public | 
|  | 6 | License as published by the Free Software Foundation; either | 
|  | 7 | version 2.1 of the License, or (at your option) any later version. | 
|  | 8 |  | 
|  | 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 | Lesser General Public License for more details. | 
|  | 13 |  | 
|  | 14 | You should have received a copy of the GNU Lesser General Public | 
|  | 15 | License along with the GNU C Library; if not, see | 
|  | 16 | <http://www.gnu.org/licenses/>.  */ | 
|  | 17 |  | 
|  | 18 | #if !_LIBC | 
|  | 19 | # include <config.h> | 
|  | 20 | # include "tempname.h" | 
|  | 21 | #endif | 
|  | 22 |  | 
|  | 23 | #include <sys/types.h> | 
|  | 24 | #include <assert.h> | 
|  | 25 |  | 
|  | 26 | #include <errno.h> | 
|  | 27 | #ifndef __set_errno | 
|  | 28 | # define __set_errno(Val) errno = (Val) | 
|  | 29 | #endif | 
|  | 30 |  | 
|  | 31 | #include <stdio.h> | 
|  | 32 | #ifndef P_tmpdir | 
|  | 33 | # define P_tmpdir "/tmp" | 
|  | 34 | #endif | 
|  | 35 | #ifndef TMP_MAX | 
|  | 36 | # define TMP_MAX 238328 | 
|  | 37 | #endif | 
|  | 38 | #ifndef __GT_FILE | 
|  | 39 | # define __GT_FILE	0 | 
|  | 40 | # define __GT_DIR	1 | 
|  | 41 | # define __GT_NOCREATE	2 | 
|  | 42 | #endif | 
|  | 43 | #if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR	\ | 
|  | 44 | || GT_NOCREATE != __GT_NOCREATE) | 
|  | 45 | # error report this to bug-gnulib@gnu.org | 
|  | 46 | #endif | 
|  | 47 |  | 
|  | 48 | #include <stddef.h> | 
|  | 49 | #include <stdlib.h> | 
|  | 50 | #include <string.h> | 
|  | 51 |  | 
|  | 52 | #include <fcntl.h> | 
|  | 53 | #include <sys/time.h> | 
|  | 54 | #include <stdint.h> | 
|  | 55 | #include <unistd.h> | 
|  | 56 |  | 
|  | 57 | #include <sys/stat.h> | 
|  | 58 |  | 
|  | 59 | #if _LIBC | 
|  | 60 | # define struct_stat64 struct stat64 | 
|  | 61 | # define __secure_getenv __libc_secure_getenv | 
|  | 62 | #else | 
|  | 63 | # define struct_stat64 struct stat | 
|  | 64 | # define __gen_tempname gen_tempname | 
|  | 65 | # define __getpid getpid | 
|  | 66 | # define __gettimeofday gettimeofday | 
|  | 67 | # define __mkdir mkdir | 
|  | 68 | # define __open open | 
|  | 69 | # define __lxstat64(version, file, buf) lstat (file, buf) | 
|  | 70 | # define __secure_getenv secure_getenv | 
|  | 71 | #endif | 
|  | 72 |  | 
|  | 73 | #ifdef _LIBC | 
|  | 74 | # include <hp-timing.h> | 
|  | 75 | # if HP_TIMING_AVAIL | 
|  | 76 | #  define RANDOM_BITS(Var) \ | 
|  | 77 | if (__glibc_unlikely (value == UINT64_C (0)))				      \ | 
|  | 78 | {									      \ | 
|  | 79 | /* If this is the first time this function is used initialize	      \ | 
|  | 80 | the variable we accumulate the value in to some somewhat	      \ | 
|  | 81 | random value.  If we'd not do this programs at startup time	      \ | 
|  | 82 | might have a reduced set of possible names, at least on slow	      \ | 
|  | 83 | machines.  */							      \ | 
|  | 84 | struct timeval tv;						      \ | 
|  | 85 | __gettimeofday (&tv, NULL);					      \ | 
|  | 86 | value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;		      \ | 
|  | 87 | }									      \ | 
|  | 88 | HP_TIMING_NOW (Var) | 
|  | 89 | # endif | 
|  | 90 | #endif | 
|  | 91 |  | 
|  | 92 | /* Use the widest available unsigned type if uint64_t is not | 
|  | 93 | available.  The algorithm below extracts a number less than 62**6 | 
|  | 94 | (approximately 2**35.725) from uint64_t, so ancient hosts where | 
|  | 95 | uintmax_t is only 32 bits lose about 3.725 bits of randomness, | 
|  | 96 | which is better than not having mkstemp at all.  */ | 
|  | 97 | #if !defined UINT64_MAX && !defined uint64_t | 
|  | 98 | # define uint64_t uintmax_t | 
|  | 99 | #endif | 
|  | 100 |  | 
|  | 101 | #if _LIBC | 
|  | 102 | /* Return nonzero if DIR is an existent directory.  */ | 
|  | 103 | static int | 
|  | 104 | direxists (const char *dir) | 
|  | 105 | { | 
|  | 106 | struct_stat64 buf; | 
|  | 107 | return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | /* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is | 
|  | 111 | non-null and exists, uses it; otherwise uses the first of $TMPDIR, | 
|  | 112 | P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable | 
|  | 113 | for use with mk[s]temp.  Will fail (-1) if DIR is non-null and | 
|  | 114 | doesn't exist, none of the searched dirs exists, or there's not | 
|  | 115 | enough space in TMPL. */ | 
|  | 116 | int | 
|  | 117 | __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, | 
|  | 118 | int try_tmpdir) | 
|  | 119 | { | 
|  | 120 | const char *d; | 
|  | 121 | size_t dlen, plen; | 
|  | 122 |  | 
|  | 123 | if (!pfx || !pfx[0]) | 
|  | 124 | { | 
|  | 125 | pfx = "file"; | 
|  | 126 | plen = 4; | 
|  | 127 | } | 
|  | 128 | else | 
|  | 129 | { | 
|  | 130 | plen = strlen (pfx); | 
|  | 131 | if (plen > 5) | 
|  | 132 | plen = 5; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | if (try_tmpdir) | 
|  | 136 | { | 
|  | 137 | d = __secure_getenv ("TMPDIR"); | 
|  | 138 | if (d != NULL && direxists (d)) | 
|  | 139 | dir = d; | 
|  | 140 | else if (dir != NULL && direxists (dir)) | 
|  | 141 | /* nothing */ ; | 
|  | 142 | else | 
|  | 143 | dir = NULL; | 
|  | 144 | } | 
|  | 145 | if (dir == NULL) | 
|  | 146 | { | 
|  | 147 | if (direxists (P_tmpdir)) | 
|  | 148 | dir = P_tmpdir; | 
|  | 149 | else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp")) | 
|  | 150 | dir = "/tmp"; | 
|  | 151 | else | 
|  | 152 | { | 
|  | 153 | __set_errno (ENOENT); | 
|  | 154 | return -1; | 
|  | 155 | } | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | dlen = strlen (dir); | 
|  | 159 | while (dlen > 1 && dir[dlen - 1] == '/') | 
|  | 160 | dlen--;			/* remove trailing slashes */ | 
|  | 161 |  | 
|  | 162 | /* check we have room for "${dir}/${pfx}XXXXXX\0" */ | 
|  | 163 | if (tmpl_len < dlen + 1 + plen + 6 + 1) | 
|  | 164 | { | 
|  | 165 | __set_errno (EINVAL); | 
|  | 166 | return -1; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx); | 
|  | 170 | return 0; | 
|  | 171 | } | 
|  | 172 | #endif /* _LIBC */ | 
|  | 173 |  | 
|  | 174 | /* These are the characters used in temporary file names.  */ | 
|  | 175 | static const char letters[] = | 
|  | 176 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | 
|  | 177 |  | 
|  | 178 | /* Generate a temporary file name based on TMPL.  TMPL must match the | 
|  | 179 | rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). | 
|  | 180 | The name constructed does not exist at the time of the call to | 
|  | 181 | __gen_tempname.  TMPL is overwritten with the result. | 
|  | 182 |  | 
|  | 183 | KIND may be one of: | 
|  | 184 | __GT_NOCREATE:	simply verify that the name does not exist | 
|  | 185 | at the time of the call. | 
|  | 186 | __GT_FILE:		create the file using open(O_CREAT|O_EXCL) | 
|  | 187 | and return a read-write fd.  The file is mode 0600. | 
|  | 188 | __GT_DIR:		create a directory, which will be mode 0700. | 
|  | 189 |  | 
|  | 190 | We use a clever algorithm to get hard-to-predict names. */ | 
|  | 191 | int | 
|  | 192 | __gen_tempname (char *tmpl, int suffixlen, int flags, int kind) | 
|  | 193 | { | 
|  | 194 | int len; | 
|  | 195 | char *XXXXXX; | 
|  | 196 | static uint64_t value; | 
|  | 197 | uint64_t random_time_bits; | 
|  | 198 | unsigned int count; | 
|  | 199 | int fd = -1; | 
|  | 200 | int save_errno = errno; | 
|  | 201 | struct_stat64 st; | 
|  | 202 |  | 
|  | 203 | /* A lower bound on the number of temporary files to attempt to | 
|  | 204 | generate.  The maximum total number of temporary file names that | 
|  | 205 | can exist for a given template is 62**6.  It should never be | 
|  | 206 | necessary to try all of these combinations.  Instead if a reasonable | 
|  | 207 | number of names is tried (we define reasonable as 62**3) fail to | 
|  | 208 | give the system administrator the chance to remove the problems.  */ | 
|  | 209 | #define ATTEMPTS_MIN (62 * 62 * 62) | 
|  | 210 |  | 
|  | 211 | /* The number of times to attempt to generate a temporary file.  To | 
|  | 212 | conform to POSIX, this must be no smaller than TMP_MAX.  */ | 
|  | 213 | #if ATTEMPTS_MIN < TMP_MAX | 
|  | 214 | unsigned int attempts = TMP_MAX; | 
|  | 215 | #else | 
|  | 216 | unsigned int attempts = ATTEMPTS_MIN; | 
|  | 217 | #endif | 
|  | 218 |  | 
|  | 219 | len = strlen (tmpl); | 
|  | 220 | if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6)) | 
|  | 221 | { | 
|  | 222 | __set_errno (EINVAL); | 
|  | 223 | return -1; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | /* This is where the Xs start.  */ | 
|  | 227 | XXXXXX = &tmpl[len - 6 - suffixlen]; | 
|  | 228 |  | 
|  | 229 | /* Get some more or less random data.  */ | 
|  | 230 | #ifdef RANDOM_BITS | 
|  | 231 | RANDOM_BITS (random_time_bits); | 
|  | 232 | #else | 
|  | 233 | { | 
|  | 234 | struct timeval tv; | 
|  | 235 | __gettimeofday (&tv, NULL); | 
|  | 236 | random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; | 
|  | 237 | } | 
|  | 238 | #endif | 
|  | 239 | value += random_time_bits ^ __getpid (); | 
|  | 240 |  | 
|  | 241 | for (count = 0; count < attempts; value += 7777, ++count) | 
|  | 242 | { | 
|  | 243 | uint64_t v = value; | 
|  | 244 |  | 
|  | 245 | /* Fill in the random bits.  */ | 
|  | 246 | XXXXXX[0] = letters[v % 62]; | 
|  | 247 | v /= 62; | 
|  | 248 | XXXXXX[1] = letters[v % 62]; | 
|  | 249 | v /= 62; | 
|  | 250 | XXXXXX[2] = letters[v % 62]; | 
|  | 251 | v /= 62; | 
|  | 252 | XXXXXX[3] = letters[v % 62]; | 
|  | 253 | v /= 62; | 
|  | 254 | XXXXXX[4] = letters[v % 62]; | 
|  | 255 | v /= 62; | 
|  | 256 | XXXXXX[5] = letters[v % 62]; | 
|  | 257 |  | 
|  | 258 | switch (kind) | 
|  | 259 | { | 
|  | 260 | case __GT_FILE: | 
|  | 261 | fd = __open (tmpl, | 
|  | 262 | (flags & ~O_ACCMODE) | 
|  | 263 | | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); | 
|  | 264 | break; | 
|  | 265 |  | 
|  | 266 | case __GT_DIR: | 
|  | 267 | fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR); | 
|  | 268 | break; | 
|  | 269 |  | 
|  | 270 | case __GT_NOCREATE: | 
|  | 271 | /* This case is backward from the other three.  __gen_tempname | 
|  | 272 | succeeds if __xstat fails because the name does not exist. | 
|  | 273 | Note the continue to bypass the common logic at the bottom | 
|  | 274 | of the loop.  */ | 
|  | 275 | if (__lxstat64 (_STAT_VER, tmpl, &st) < 0) | 
|  | 276 | { | 
|  | 277 | if (errno == ENOENT) | 
|  | 278 | { | 
|  | 279 | __set_errno (save_errno); | 
|  | 280 | return 0; | 
|  | 281 | } | 
|  | 282 | else | 
|  | 283 | /* Give up now. */ | 
|  | 284 | return -1; | 
|  | 285 | } | 
|  | 286 | continue; | 
|  | 287 |  | 
|  | 288 | default: | 
|  | 289 | assert (! "invalid KIND in __gen_tempname"); | 
|  | 290 | abort (); | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | if (fd >= 0) | 
|  | 294 | { | 
|  | 295 | __set_errno (save_errno); | 
|  | 296 | return fd; | 
|  | 297 | } | 
|  | 298 | else if (errno != EEXIST) | 
|  | 299 | return -1; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | /* We got out of the loop because we ran out of combinations to try.  */ | 
|  | 303 | __set_errno (EEXIST); | 
|  | 304 | return -1; | 
|  | 305 | } |