4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
30 * mktemp() expects a string with up to six trailing 'X's.
31 * These will be overlaid with letters, digits and symbols from
32 * the portable filename character set. If every combination thus
33 * inserted leads to an existing file name, the string is shortened
34 * to length zero and a pointer to a null string is returned.
36 * The guarantee made by mktime() to the caller is that the
37 * generated file name string will not match the string
38 * produced by any other concurrent process using mktemp().
39 * To guarantee uniqueness across the process-id space,
40 * the process-id of the caller is encoded into the string.
41 * To allow repeated calls within the same process to generate
42 * different strings on each call, a sequence number is encoded
43 * into the string along with process-id.
45 * The encoding is performed using radix-64 (6 bits per character),
46 * with 64 characters taken from the portable file name character set.
47 * This allows the six X's to be a representation of a 36-bit integer
48 * composed of bit fields:
50 * where the process-id occupies the high-order bits and the sequence
51 * number occupies the low-order bits. The size of the pid field is
52 * not fixed at the traditional 15 bits (MAXPID = 30000); the system
53 * now allows a larger process-id space and MAXPID is obtained from
54 * the system with a call to sysconf(_SC_MAXPID).
56 * mktime() should fail if fewer than six X's are presented to it.
57 * However, this has been traditionally accepted and is preserved
58 * in the present code. The consequence is that the 36-bit integer
59 * is reduced to a (6*N)-bit integer, where N is the number of X's.
60 * mktime() fails immediately if the resulting integer is not large
61 * enough to contain MAXPID.
63 * In an attempt to confuse and thwart hackers, the starting
64 * sequence number is randomized using the current time.
67 #pragma weak _mktemp = mktemp
73 #include <sys/types.h>
83 #include <sys/param.h>
86 * 64-bit digits, must be from the POSIX "portable file name character set".
90 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
91 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
92 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
93 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
94 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_',
98 libc_mktemps(char *as
, int slen
)
100 /* statics are protected by this static mutex */
101 static mutex_t mktemp_lock
= DEFAULTMUTEX
;
102 static int pidshift
= 0;
103 static int previous_try
= 0;
104 static pid_t previous_pid
= 0;
105 static int previous_xcnt
= XCNT
;
117 if (as
== NULL
|| *as
== '\0') /* If the string passed is null then */
118 return (as
); /* a pointer to a null string is returned. */
120 lmutex_lock(&mktemp_lock
);
123 if (pid
!= previous_pid
) { /* first time or first after fork() */
125 * Randomize the starting sequence number in
126 * an attempt to confuse and thwart hackers.
127 * Use the low 12 bits of the time in milliseconds.
131 (void) gettimeofday(&tm
, NULL
);
132 previous_try
= (tm
.tv_sec
* 1000 + tm
.tv_usec
/ 1000) & 0xfff;
134 previous_xcnt
= XCNT
;
137 /* for all possible values of pid, 0 <= pid < (1 << pidshift) */
138 if (pidshift
== 0) /* one-time initialization */
139 pidshift
= fls((uint_t
)MAXPID
); /* high bit number */
143 len
= (int)strlen(as
);
144 if (slen
>= len
|| slen
< 0)
148 while ((len
!= 0) && (xcnt
< XCNT
) && (*s
== 'X')) {
153 first_x
= s
+ 1; /* Remember pointer to the first X */
155 /* fail if we don't have enough X's to represent MAXPID */
156 if ((tryshift
= xcnt
* 6 - pidshift
) < 0) {
158 * Some broken programs call mktemp() repeatedly,
159 * passing the same string without reinserting the X's.
160 * Check to see if this is such a call by testing
161 * the trailing characters of the string for a
162 * match with the process-id.
164 uint64_t xpid
= 0; /* reconstructed pid */
167 for (xcnt
= previous_xcnt
; xcnt
&& s
> as
; xcnt
--) {
172 for (i
= 0; i
< 64; i
++)
177 xpid
= xpid
* 64 + i
;
179 xpid
>>= (previous_xcnt
* 6 - pidshift
);
180 xpid
&= ((1 << pidshift
) - 1);
183 lstat64(as
, &buf
) == -1 && errno
== ENOENT
) {
184 lmutex_unlock(&mktemp_lock
);
191 /* we can try sequence numbers in the range 0 <= try < max_try */
192 max_try
= 1 << tryshift
;
193 if (previous_try
>= max_try
)
198 /* num is up to a 36-bit integer ... */
199 uint64_t num
= ((uint64_t)pid
<< tryshift
) + (uint64_t)try;
202 /* ... which we represent backwards in base 64 */
203 for (i
= 0, s
= first_x
; i
< xcnt
; i
++) {
204 *s
++ = chars
[num
& 077];
208 if (lstat64(as
, &buf
) == -1) {
210 break; /* unrecoverable error */
211 /* remember where we left off for the next call */
212 previous_try
= try + 1;
213 previous_xcnt
= xcnt
;
214 lmutex_unlock(&mktemp_lock
);
218 if (++try == max_try
)
220 if (try == previous_try
)
225 lmutex_unlock(&mktemp_lock
);
231 mktemp(char *template)
233 return (libc_mktemps(template, 0));