tempname.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. /* March 11, 2002 Manuel Novoa III
  16. *
  17. * Modify code to remove dependency on libgcc long long arith support funcs.
  18. */
  19. /* June 6, 2004 Erik Andersen
  20. *
  21. * Don't use brain damaged getpid() based randomness.
  22. */
  23. /* April 15, 2005 Mike Frysinger
  24. *
  25. * Use brain damaged getpid() if real random fails.
  26. */
  27. #define open64 __open64
  28. #include <stddef.h>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <assert.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/time.h>
  40. #include "tempname.h"
  41. /* Return nonzero if DIR is an existent directory. */
  42. static int direxists (const char *dir)
  43. {
  44. struct stat buf;
  45. return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  46. }
  47. /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
  48. non-null and exists, uses it; otherwise uses the first of $TMPDIR,
  49. P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
  50. for use with mk[s]temp. Will fail (-1) if DIR is non-null and
  51. doesn't exist, none of the searched dirs exists, or there's not
  52. enough space in TMPL. */
  53. int attribute_hidden __path_search (char *tmpl, size_t tmpl_len, const char *dir,
  54. const char *pfx, int try_tmpdir)
  55. {
  56. //const char *d;
  57. size_t dlen, plen;
  58. if (!pfx || !pfx[0])
  59. {
  60. pfx = "file";
  61. plen = 4;
  62. }
  63. else
  64. {
  65. plen = __strlen (pfx);
  66. if (plen > 5)
  67. plen = 5;
  68. }
  69. /* Disable support for $TMPDIR */
  70. #if 0
  71. if (try_tmpdir)
  72. {
  73. d = __secure_getenv ("TMPDIR");
  74. if (d != NULL && direxists (d))
  75. dir = d;
  76. else if (dir != NULL && direxists (dir))
  77. /* nothing */ ;
  78. else
  79. dir = NULL;
  80. }
  81. #endif
  82. if (dir == NULL)
  83. {
  84. if (direxists (P_tmpdir))
  85. dir = P_tmpdir;
  86. else if (__strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
  87. dir = "/tmp";
  88. else
  89. {
  90. __set_errno (ENOENT);
  91. return -1;
  92. }
  93. }
  94. dlen = __strlen (dir);
  95. while (dlen > 1 && dir[dlen - 1] == '/')
  96. dlen--; /* remove trailing slashes */
  97. /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  98. if (tmpl_len < dlen + 1 + plen + 6 + 1)
  99. {
  100. __set_errno (EINVAL);
  101. return -1;
  102. }
  103. sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
  104. return 0;
  105. }
  106. /* These are the characters used in temporary filenames. */
  107. static const char letters[] =
  108. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  109. static unsigned int fillrand(unsigned char *buf, unsigned int len)
  110. {
  111. int fd;
  112. unsigned int result = -1;
  113. fd = __open("/dev/urandom", O_RDONLY);
  114. if (fd < 0) {
  115. fd = __open("/dev/random", O_RDONLY | O_NONBLOCK);
  116. }
  117. if (fd >= 0) {
  118. result = __read(fd, buf, len);
  119. __close(fd);
  120. }
  121. return result;
  122. }
  123. static void brain_damaged_fillrand(unsigned char *buf, unsigned int len)
  124. {
  125. int i, k;
  126. struct timeval tv;
  127. uint32_t high, low, rh;
  128. static uint64_t value;
  129. gettimeofday(&tv, NULL);
  130. value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ __getpid();
  131. low = value & UINT32_MAX;
  132. high = value >> 32;
  133. for (i = 0; i < len; ++i) {
  134. rh = high % 62;
  135. high /= 62;
  136. #define L ((UINT32_MAX % 62 + 1) % 62)
  137. k = (low % 62) + (L * rh);
  138. #undef L
  139. #define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62))
  140. low = (low / 62) + (H * rh) + (k / 62);
  141. #undef H
  142. k %= 62;
  143. buf[i] = letters[k];
  144. }
  145. }
  146. /* Generate a temporary file name based on TMPL. TMPL must match the
  147. rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
  148. does not exist at the time of the call to __gen_tempname. TMPL is
  149. overwritten with the result.
  150. KIND may be one of:
  151. __GT_NOCREATE: simply verify that the name does not exist
  152. at the time of the call.
  153. __GT_FILE: create the file using open(O_CREAT|O_EXCL)
  154. and return a read-write fd. The file is mode 0600.
  155. __GT_BIGFILE: same as __GT_FILE but use open64().
  156. __GT_DIR: create a directory, which will be mode 0700.
  157. */
  158. int attribute_hidden __gen_tempname (char *tmpl, int kind)
  159. {
  160. char *XXXXXX;
  161. unsigned int k;
  162. int len, i, count, fd, save_errno = errno;
  163. unsigned char randomness[6];
  164. len = __strlen (tmpl);
  165. if (len < 6 || __strcmp (&tmpl[len - 6], "XXXXXX"))
  166. {
  167. __set_errno (EINVAL);
  168. return -1;
  169. }
  170. /* This is where the Xs start. */
  171. XXXXXX = &tmpl[len - 6];
  172. /* Get some random data. */
  173. if (fillrand(randomness, sizeof(randomness)) != sizeof(randomness)) {
  174. /* if random device nodes failed us, lets use the braindamaged ver */
  175. brain_damaged_fillrand(randomness, sizeof(randomness));
  176. }
  177. for (i = 0 ; i < sizeof(randomness) ; i++) {
  178. k = ((randomness[i]) % 62);
  179. XXXXXX[i] = letters[k];
  180. }
  181. for (count = 0; count < TMP_MAX; ++count)
  182. {
  183. switch(kind) {
  184. case __GT_NOCREATE:
  185. {
  186. struct stat st;
  187. if (stat (tmpl, &st) < 0)
  188. {
  189. if (errno == ENOENT)
  190. {
  191. __set_errno (save_errno);
  192. return 0;
  193. }
  194. else
  195. /* Give up now. */
  196. return -1;
  197. }
  198. else
  199. continue;
  200. }
  201. case __GT_FILE:
  202. fd = __open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  203. break;
  204. #if defined __UCLIBC_HAS_LFS__
  205. case __GT_BIGFILE:
  206. fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  207. break;
  208. #endif
  209. case __GT_DIR:
  210. fd = mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
  211. break;
  212. default:
  213. fd = -1;
  214. assert (! "invalid KIND in __gen_tempname");
  215. }
  216. if (fd >= 0) {
  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. }