xref: /illumos-gate/usr/src/lib/libc/port/gen/mktemp.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
30*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *	mktemp() expects a string with up to six trailing 'X's.
35*7c478bd9Sstevel@tonic-gate  *	These will be overlaid with letters, digits and symbols from
36*7c478bd9Sstevel@tonic-gate  *	the portable filename character set.  If every combination thus
37*7c478bd9Sstevel@tonic-gate  *	inserted leads to an existing file name, the string is shortened
38*7c478bd9Sstevel@tonic-gate  *	to length zero and a pointer to a null string is returned.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  *	The guarantee made by mktime() to the caller is that the
41*7c478bd9Sstevel@tonic-gate  *	generated file name string will not match the string
42*7c478bd9Sstevel@tonic-gate  *	produced by any other concurrent process using mktemp().
43*7c478bd9Sstevel@tonic-gate  *	To guarantee uniqueness across the process-id space,
44*7c478bd9Sstevel@tonic-gate  *	the process-id of the caller is encoded into the string.
45*7c478bd9Sstevel@tonic-gate  *	To allow repeated calls within the same process to generate
46*7c478bd9Sstevel@tonic-gate  *	different strings on each call, a sequence number is encoded
47*7c478bd9Sstevel@tonic-gate  *	into the string along with process-id.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  *	The encoding is performed using radix-64 (6 bits per character),
50*7c478bd9Sstevel@tonic-gate  *	with 64 characters taken from the portable file name character set.
51*7c478bd9Sstevel@tonic-gate  *	This allows the six X's to be a representation of a 36-bit integer
52*7c478bd9Sstevel@tonic-gate  *	composed of bit fields:
53*7c478bd9Sstevel@tonic-gate  *		( pid | seq )
54*7c478bd9Sstevel@tonic-gate  *	where the process-id occupies the high-order bits and the sequence
55*7c478bd9Sstevel@tonic-gate  *	number occupies the low-order bits.  The size of the pid field is
56*7c478bd9Sstevel@tonic-gate  *	not fixed at the traditional 15 bits (MAXPID = 30000); the system
57*7c478bd9Sstevel@tonic-gate  *	now allows a larger process-id space and MAXPID is obtained from
58*7c478bd9Sstevel@tonic-gate  *	the system with a call to sysconf(_SC_MAXPID).
59*7c478bd9Sstevel@tonic-gate  *
60*7c478bd9Sstevel@tonic-gate  *	mktime() should fail if fewer than six X's are presented to it.
61*7c478bd9Sstevel@tonic-gate  *	However, this has been traditionally accepted and is preserved
62*7c478bd9Sstevel@tonic-gate  *	in the present code.  The consequence is that the 36-bit integer
63*7c478bd9Sstevel@tonic-gate  *	is reduced to a (6*N)-bit integer, where N is the number of X's.
64*7c478bd9Sstevel@tonic-gate  *	mktime() fails immediately if the resulting integer is not large
65*7c478bd9Sstevel@tonic-gate  *	enough to contain MAXPID.
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  *	In an attempt to confuse and thwart hackers, the starting
68*7c478bd9Sstevel@tonic-gate  *	sequence number is randomized using the current time.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #define	XCNT  6
72*7c478bd9Sstevel@tonic-gate #pragma weak mktemp = _mktemp
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
75*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
76*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
77*7c478bd9Sstevel@tonic-gate #include <string.h>
78*7c478bd9Sstevel@tonic-gate #include <unistd.h>
79*7c478bd9Sstevel@tonic-gate #include <thread.h>
80*7c478bd9Sstevel@tonic-gate #include <synch.h>
81*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
82*7c478bd9Sstevel@tonic-gate #include <errno.h>
83*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
84*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
85*7c478bd9Sstevel@tonic-gate #include <stdio.h>
86*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * 64-bit digits, must be from the POSIX "portable file name character set".
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate static char
92*7c478bd9Sstevel@tonic-gate chars[64] = {
93*7c478bd9Sstevel@tonic-gate 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
94*7c478bd9Sstevel@tonic-gate 	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
95*7c478bd9Sstevel@tonic-gate 	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
96*7c478bd9Sstevel@tonic-gate 	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
97*7c478bd9Sstevel@tonic-gate 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_',
98*7c478bd9Sstevel@tonic-gate };
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * Find highest one bit set.
102*7c478bd9Sstevel@tonic-gate  * Returns bit number of highest bit that is set.
103*7c478bd9Sstevel@tonic-gate  * Low order bit is number 0, high order bit is number 31.
104*7c478bd9Sstevel@tonic-gate  */
105*7c478bd9Sstevel@tonic-gate static int
106*7c478bd9Sstevel@tonic-gate highbit(uint_t i)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	int h = 0;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if (i & 0xffff0000)
111*7c478bd9Sstevel@tonic-gate 		h += 16, i >>= 16;
112*7c478bd9Sstevel@tonic-gate 	if (i & 0xff00)
113*7c478bd9Sstevel@tonic-gate 		h += 8, i >>= 8;
114*7c478bd9Sstevel@tonic-gate 	if (i & 0xf0)
115*7c478bd9Sstevel@tonic-gate 		h += 4, i >>= 4;
116*7c478bd9Sstevel@tonic-gate 	if (i & 0xc)
117*7c478bd9Sstevel@tonic-gate 		h += 2, i >>= 2;
118*7c478bd9Sstevel@tonic-gate 	if (i & 0x2)
119*7c478bd9Sstevel@tonic-gate 		h += 1;
120*7c478bd9Sstevel@tonic-gate 	return (h);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate char *
124*7c478bd9Sstevel@tonic-gate mktemp(char *as)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	/* statics are protected by this static mutex */
127*7c478bd9Sstevel@tonic-gate 	static mutex_t	mktemp_lock = DEFAULTMUTEX;
128*7c478bd9Sstevel@tonic-gate 	static int	pidshift = 0;
129*7c478bd9Sstevel@tonic-gate 	static int	previous_try = 0;
130*7c478bd9Sstevel@tonic-gate 	static pid_t	previous_pid = 0;
131*7c478bd9Sstevel@tonic-gate 	static int	previous_xcnt = XCNT;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	pid_t		pid;
134*7c478bd9Sstevel@tonic-gate 	int		try;
135*7c478bd9Sstevel@tonic-gate 	int		tryshift;
136*7c478bd9Sstevel@tonic-gate 	int		max_try;
137*7c478bd9Sstevel@tonic-gate 	char		*s;
138*7c478bd9Sstevel@tonic-gate 	char		*first_x;
139*7c478bd9Sstevel@tonic-gate 	int		len;
140*7c478bd9Sstevel@tonic-gate 	uint_t		xcnt;
141*7c478bd9Sstevel@tonic-gate 	struct stat64	buf;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if (as == NULL || *as == '\0')	/* If the string passed is null then */
144*7c478bd9Sstevel@tonic-gate 		return (as);	/* a pointer to a null string is returned. */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	lmutex_lock(&mktemp_lock);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	pid = getpid();
149*7c478bd9Sstevel@tonic-gate 	if (pid != previous_pid) {	/* first time or first after fork() */
150*7c478bd9Sstevel@tonic-gate 		/*
151*7c478bd9Sstevel@tonic-gate 		 * Randomize the starting sequence number in
152*7c478bd9Sstevel@tonic-gate 		 * an attempt to confuse and thwart hackers.
153*7c478bd9Sstevel@tonic-gate 		 * Use the low 12 bits of the time in milliseconds.
154*7c478bd9Sstevel@tonic-gate 		 */
155*7c478bd9Sstevel@tonic-gate 		struct timeval tm;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tm, NULL);
158*7c478bd9Sstevel@tonic-gate 		previous_try = (tm.tv_sec * 1000 + tm.tv_usec / 1000) & 0xfff;
159*7c478bd9Sstevel@tonic-gate 		previous_pid = pid;
160*7c478bd9Sstevel@tonic-gate 		previous_xcnt = XCNT;
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/* for all possible values of pid, 0 <= pid < (1 << pidshift) */
164*7c478bd9Sstevel@tonic-gate 	if (pidshift == 0)	/* one-time initialization */
165*7c478bd9Sstevel@tonic-gate 		pidshift = highbit((uint_t)MAXPID) + 1;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	/* count the X's */
168*7c478bd9Sstevel@tonic-gate 	xcnt = 0;
169*7c478bd9Sstevel@tonic-gate 	len = (int)strlen(as);
170*7c478bd9Sstevel@tonic-gate 	s = as + (len - 1);
171*7c478bd9Sstevel@tonic-gate 	while ((len != 0) && (xcnt < XCNT) && (*s == 'X')) {
172*7c478bd9Sstevel@tonic-gate 		xcnt++;
173*7c478bd9Sstevel@tonic-gate 		len--;
174*7c478bd9Sstevel@tonic-gate 		--s;
175*7c478bd9Sstevel@tonic-gate 	}
176*7c478bd9Sstevel@tonic-gate 	first_x = s + 1;	/* Remember pointer to the first X */
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	/* fail if we don't have enough X's to represent MAXPID */
179*7c478bd9Sstevel@tonic-gate 	if ((tryshift = xcnt * 6 - pidshift) < 0) {
180*7c478bd9Sstevel@tonic-gate 		/*
181*7c478bd9Sstevel@tonic-gate 		 * Some broken programs call mktemp() repeatedly,
182*7c478bd9Sstevel@tonic-gate 		 * passing the same string without reinserting the X's.
183*7c478bd9Sstevel@tonic-gate 		 * Check to see if this is such a call by testing
184*7c478bd9Sstevel@tonic-gate 		 * the trailing characters of the string for a
185*7c478bd9Sstevel@tonic-gate 		 * match with the process-id.
186*7c478bd9Sstevel@tonic-gate 		 */
187*7c478bd9Sstevel@tonic-gate 		uint64_t xpid = 0;		/* reconstructed pid */
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 		s = as + len;
190*7c478bd9Sstevel@tonic-gate 		for (xcnt = previous_xcnt; xcnt && s > as; xcnt--) {
191*7c478bd9Sstevel@tonic-gate 			int c;
192*7c478bd9Sstevel@tonic-gate 			int i;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 			c = *--s;
195*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < 64; i++)
196*7c478bd9Sstevel@tonic-gate 				if (c == chars[i])
197*7c478bd9Sstevel@tonic-gate 					break;
198*7c478bd9Sstevel@tonic-gate 			if (i == 64)
199*7c478bd9Sstevel@tonic-gate 				goto fail;
200*7c478bd9Sstevel@tonic-gate 			xpid = xpid * 64 + i;
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 		xpid >>= (previous_xcnt * 6 - pidshift);
203*7c478bd9Sstevel@tonic-gate 		xpid &= ((1 << pidshift) - 1);
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		if (xpid == pid &&
206*7c478bd9Sstevel@tonic-gate 		    lstat64(as, &buf) == -1 && errno == ENOENT) {
207*7c478bd9Sstevel@tonic-gate 			lmutex_unlock(&mktemp_lock);
208*7c478bd9Sstevel@tonic-gate 			return (as);
209*7c478bd9Sstevel@tonic-gate 		}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 		goto fail;
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	/* we can try sequence numbers in the range 0 <= try < max_try */
215*7c478bd9Sstevel@tonic-gate 	max_try = 1 << tryshift;
216*7c478bd9Sstevel@tonic-gate 	if (previous_try >= max_try)
217*7c478bd9Sstevel@tonic-gate 		previous_try = 0;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	try = previous_try;
220*7c478bd9Sstevel@tonic-gate 	for (;;) {
221*7c478bd9Sstevel@tonic-gate 		/* num is up to a 36-bit integer ... */
222*7c478bd9Sstevel@tonic-gate 		uint64_t num = ((uint64_t)pid << tryshift) + (uint64_t)try;
223*7c478bd9Sstevel@tonic-gate 		int i;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		/* ... which we represent backwards in base 64 */
226*7c478bd9Sstevel@tonic-gate 		for (i = 0, s = first_x; i < xcnt; i++) {
227*7c478bd9Sstevel@tonic-gate 			*s++ = chars[num & 077];
228*7c478bd9Sstevel@tonic-gate 			num >>= 6;
229*7c478bd9Sstevel@tonic-gate 		}
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 		if (lstat64(as, &buf) == -1) {
232*7c478bd9Sstevel@tonic-gate 			if (errno != ENOENT)
233*7c478bd9Sstevel@tonic-gate 				break;		/* unrecoverable error */
234*7c478bd9Sstevel@tonic-gate 			/* remember where we left off for the next call */
235*7c478bd9Sstevel@tonic-gate 			previous_try = try + 1;
236*7c478bd9Sstevel@tonic-gate 			previous_xcnt = xcnt;
237*7c478bd9Sstevel@tonic-gate 			lmutex_unlock(&mktemp_lock);
238*7c478bd9Sstevel@tonic-gate 			return (as);
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 		if (++try == max_try)
242*7c478bd9Sstevel@tonic-gate 			try = 0;
243*7c478bd9Sstevel@tonic-gate 		if (try == previous_try)
244*7c478bd9Sstevel@tonic-gate 			break;
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate fail:
248*7c478bd9Sstevel@tonic-gate 	lmutex_unlock(&mktemp_lock);
249*7c478bd9Sstevel@tonic-gate 	*as = '\0';
250*7c478bd9Sstevel@tonic-gate 	return (as);
251*7c478bd9Sstevel@tonic-gate }
252