tempname.c 5.7 KB

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