tempname.c 6.9 KB

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