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