tempname.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. see <http://www.gnu.org/licenses/>. */
  14. /* March 11, 2002 Manuel Novoa III
  15. *
  16. * Modify code to remove dependency on libgcc long long arith support funcs.
  17. */
  18. /* June 6, 2004 Erik Andersen
  19. *
  20. * Don't use brain damaged getpid() based randomness.
  21. */
  22. /* April 15, 2005 Mike Frysinger
  23. *
  24. * Use brain damaged getpid() if real random fails.
  25. */
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <assert.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <sys/time.h>
  38. #include "tempname.h"
  39. /* Return nonzero if DIR is an existent directory. */
  40. static int direxists (const char *dir)
  41. {
  42. struct stat buf;
  43. return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  44. }
  45. /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
  46. non-null and exists, uses it; otherwise uses the first of $TMPDIR,
  47. P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
  48. for use with mk[s]temp. Will fail (-1) if DIR is non-null and
  49. doesn't exist, none of the searched dirs exists, or there's not
  50. enough space in TMPL. */
  51. int ___path_search (char *tmpl, size_t tmpl_len, const char *dir,
  52. const char *pfx /*, int try_tmpdir*/)
  53. {
  54. /*const char *d; */
  55. /* dir and pfx lengths should always fit into an int,
  56. so don't bother using size_t here. Especially since
  57. the printf func requires an int for precision (%*s). */
  58. int dlen, plen;
  59. if (!pfx || !pfx[0])
  60. {
  61. pfx = "file";
  62. plen = 4;
  63. }
  64. else
  65. {
  66. plen = strlen (pfx);
  67. if (plen > 5)
  68. plen = 5;
  69. }
  70. /* Disable support for $TMPDIR */
  71. #if 0
  72. if (try_tmpdir)
  73. {
  74. d = __secure_getenv ("TMPDIR");
  75. if (d != NULL && direxists (d))
  76. dir = d;
  77. else if (dir != NULL && direxists (dir))
  78. /* nothing */ ;
  79. else
  80. dir = NULL;
  81. }
  82. #endif
  83. if (dir == NULL)
  84. {
  85. if (direxists (P_tmpdir))
  86. dir = P_tmpdir;
  87. else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
  88. dir = "/tmp";
  89. else
  90. {
  91. __set_errno (ENOENT);
  92. return -1;
  93. }
  94. }
  95. dlen = strlen (dir);
  96. while (dlen > 1 && dir[dlen - 1] == '/')
  97. dlen--; /* remove trailing slashes */
  98. /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  99. if (tmpl_len < (size_t)dlen + 1 + plen + 6 + 1)
  100. {
  101. __set_errno (EINVAL);
  102. return -1;
  103. }
  104. sprintf (tmpl, "%.*s/%.*sXXXXXX", dlen, dir, plen, pfx);
  105. return 0;
  106. }
  107. /* These are the characters used in temporary filenames. */
  108. static const char letters[] =
  109. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  110. #define NUM_LETTERS (62)
  111. static unsigned int fillrand(unsigned char *buf, unsigned int len)
  112. {
  113. int fd;
  114. unsigned int result = -1;
  115. fd = open("/dev/urandom", O_RDONLY);
  116. if (fd < 0) {
  117. fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
  118. }
  119. if (fd >= 0) {
  120. result = read(fd, buf, len);
  121. close(fd);
  122. }
  123. return result;
  124. }
  125. static void brain_damaged_fillrand(unsigned char *buf, unsigned int len)
  126. {
  127. unsigned int i, k;
  128. struct timeval tv;
  129. uint32_t high, low, rh;
  130. static uint64_t value;
  131. gettimeofday(&tv, NULL);
  132. value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
  133. low = value & UINT32_MAX;
  134. high = value >> 32;
  135. for (i = 0; i < len; ++i) {
  136. rh = high % NUM_LETTERS;
  137. high /= NUM_LETTERS;
  138. #define L ((UINT32_MAX % NUM_LETTERS + 1) % NUM_LETTERS)
  139. k = (low % NUM_LETTERS) + (L * rh);
  140. #undef L
  141. #define H ((UINT32_MAX / NUM_LETTERS) + ((UINT32_MAX % NUM_LETTERS + 1) / NUM_LETTERS))
  142. low = (low / NUM_LETTERS) + (H * rh) + (k / NUM_LETTERS);
  143. #undef H
  144. k %= NUM_LETTERS;
  145. buf[i] = letters[k];
  146. }
  147. }
  148. /* Generate a temporary file name based on TMPL. TMPL must match the
  149. rules for mk[s]temp[s] (i.e. end in "prefixXXXXXXsuffix"). The name
  150. constructed does not exist at the time of the call to __gen_tempname.
  151. TMPL is overwritten with the result.
  152. KIND may be one of:
  153. __GT_NOCREATE: simply verify that the name does not exist
  154. at the time of the call. mode argument is ignored.
  155. __GT_FILE: create the file using open(O_CREAT|O_EXCL)
  156. and return a read-write fd with given mode.
  157. __GT_BIGFILE: same as __GT_FILE but use open64().
  158. __GT_DIR: create a directory with given mode.
  159. */
  160. int attribute_hidden __gen_tempname (char *tmpl, int kind, int flags,
  161. int suffixlen, mode_t mode)
  162. {
  163. char *XXXXXX;
  164. unsigned int i;
  165. int fd, save_errno = errno;
  166. unsigned char randomness[6];
  167. size_t len;
  168. len = strlen (tmpl);
  169. /* This is where the Xs start. */
  170. XXXXXX = tmpl + len - 6 - suffixlen;
  171. if (len < 6 || suffixlen < 0 || suffixlen > len - 6
  172. || strncmp (XXXXXX, "XXXXXX", 6))
  173. {
  174. __set_errno (EINVAL);
  175. return -1;
  176. }
  177. for (i = 0; i < TMP_MAX; ++i) {
  178. unsigned char j;
  179. /* Get some random data. */
  180. if (fillrand(randomness, sizeof(randomness)) != sizeof(randomness)) {
  181. /* if random device nodes failed us, lets use the braindamaged ver */
  182. brain_damaged_fillrand(randomness, sizeof(randomness));
  183. }
  184. for (j = 0; j < sizeof(randomness); ++j)
  185. XXXXXX[j] = letters[randomness[j] % NUM_LETTERS];
  186. switch (kind) {
  187. case __GT_NOCREATE:
  188. {
  189. struct stat st;
  190. if (stat (tmpl, &st) < 0) {
  191. if (errno == ENOENT) {
  192. fd = 0;
  193. goto restore_and_ret;
  194. } else
  195. /* Give up now. */
  196. return -1;
  197. } else
  198. fd = 0;
  199. }
  200. case __GT_FILE:
  201. fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | flags, mode);
  202. break;
  203. #if defined __UCLIBC_HAS_LFS__
  204. case __GT_BIGFILE:
  205. fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL | flags, mode);
  206. break;
  207. #endif
  208. case __GT_DIR:
  209. fd = mkdir (tmpl, mode);
  210. break;
  211. default:
  212. fd = -1;
  213. assert (! "invalid KIND in __gen_tempname");
  214. }
  215. if (fd >= 0) {
  216. restore_and_ret:
  217. __set_errno (save_errno);
  218. return fd;
  219. }
  220. else if (errno != EEXIST)
  221. /* Any other error will apply also to other names we might
  222. try, and there are 2^32 or so of them, so give up now. */
  223. return -1;
  224. }
  225. /* We got out of the loop because we ran out of combinations to try. */
  226. __set_errno (EEXIST);
  227. return -1;
  228. }