xref: /illumos-gate/usr/src/lib/libc/port/gen/mktemp.c (revision 7257d1b4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c4dcc55Scasper  * Common Development and Distribution License (the "License").
67c4dcc55Scasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	mktemp() expects a string with up to six trailing 'X's.
347c478bd9Sstevel@tonic-gate  *	These will be overlaid with letters, digits and symbols from
357c478bd9Sstevel@tonic-gate  *	the portable filename character set.  If every combination thus
367c478bd9Sstevel@tonic-gate  *	inserted leads to an existing file name, the string is shortened
377c478bd9Sstevel@tonic-gate  *	to length zero and a pointer to a null string is returned.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  *	The guarantee made by mktime() to the caller is that the
407c478bd9Sstevel@tonic-gate  *	generated file name string will not match the string
417c478bd9Sstevel@tonic-gate  *	produced by any other concurrent process using mktemp().
427c478bd9Sstevel@tonic-gate  *	To guarantee uniqueness across the process-id space,
437c478bd9Sstevel@tonic-gate  *	the process-id of the caller is encoded into the string.
447c478bd9Sstevel@tonic-gate  *	To allow repeated calls within the same process to generate
457c478bd9Sstevel@tonic-gate  *	different strings on each call, a sequence number is encoded
467c478bd9Sstevel@tonic-gate  *	into the string along with process-id.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  *	The encoding is performed using radix-64 (6 bits per character),
497c478bd9Sstevel@tonic-gate  *	with 64 characters taken from the portable file name character set.
507c478bd9Sstevel@tonic-gate  *	This allows the six X's to be a representation of a 36-bit integer
517c478bd9Sstevel@tonic-gate  *	composed of bit fields:
527c478bd9Sstevel@tonic-gate  *		( pid | seq )
537c478bd9Sstevel@tonic-gate  *	where the process-id occupies the high-order bits and the sequence
547c478bd9Sstevel@tonic-gate  *	number occupies the low-order bits.  The size of the pid field is
557c478bd9Sstevel@tonic-gate  *	not fixed at the traditional 15 bits (MAXPID = 30000); the system
567c478bd9Sstevel@tonic-gate  *	now allows a larger process-id space and MAXPID is obtained from
577c478bd9Sstevel@tonic-gate  *	the system with a call to sysconf(_SC_MAXPID).
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *	mktime() should fail if fewer than six X's are presented to it.
607c478bd9Sstevel@tonic-gate  *	However, this has been traditionally accepted and is preserved
617c478bd9Sstevel@tonic-gate  *	in the present code.  The consequence is that the 36-bit integer
627c478bd9Sstevel@tonic-gate  *	is reduced to a (6*N)-bit integer, where N is the number of X's.
637c478bd9Sstevel@tonic-gate  *	mktime() fails immediately if the resulting integer is not large
647c478bd9Sstevel@tonic-gate  *	enough to contain MAXPID.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  *	In an attempt to confuse and thwart hackers, the starting
677c478bd9Sstevel@tonic-gate  *	sequence number is randomized using the current time.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
70*7257d1b4Sraf #pragma weak _mktemp = mktemp
71*7257d1b4Sraf 
727c478bd9Sstevel@tonic-gate #define	XCNT  6
737c478bd9Sstevel@tonic-gate 
74*7257d1b4Sraf #include "lint.h"
757c478bd9Sstevel@tonic-gate #include "mtlib.h"
767c478bd9Sstevel@tonic-gate #include <sys/types.h>
777c478bd9Sstevel@tonic-gate #include <string.h>
787c478bd9Sstevel@tonic-gate #include <unistd.h>
797c478bd9Sstevel@tonic-gate #include <thread.h>
807c478bd9Sstevel@tonic-gate #include <synch.h>
817c478bd9Sstevel@tonic-gate #include <sys/stat.h>
827c478bd9Sstevel@tonic-gate #include <errno.h>
837c478bd9Sstevel@tonic-gate #include <sys/time.h>
847c478bd9Sstevel@tonic-gate #include <stdlib.h>
857c478bd9Sstevel@tonic-gate #include <stdio.h>
867c478bd9Sstevel@tonic-gate #include <sys/param.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * 64-bit digits, must be from the POSIX "portable file name character set".
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate static char
927c478bd9Sstevel@tonic-gate chars[64] = {
937c478bd9Sstevel@tonic-gate 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
947c478bd9Sstevel@tonic-gate 	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
957c478bd9Sstevel@tonic-gate 	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
967c478bd9Sstevel@tonic-gate 	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
977c478bd9Sstevel@tonic-gate 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_',
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Find highest one bit set.
1027c478bd9Sstevel@tonic-gate  * Returns bit number of highest bit that is set.
1037c478bd9Sstevel@tonic-gate  * Low order bit is number 0, high order bit is number 31.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static int
1067c478bd9Sstevel@tonic-gate highbit(uint_t i)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	int h = 0;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (i & 0xffff0000)
1117c478bd9Sstevel@tonic-gate 		h += 16, i >>= 16;
1127c478bd9Sstevel@tonic-gate 	if (i & 0xff00)
1137c478bd9Sstevel@tonic-gate 		h += 8, i >>= 8;
1147c478bd9Sstevel@tonic-gate 	if (i & 0xf0)
1157c478bd9Sstevel@tonic-gate 		h += 4, i >>= 4;
1167c478bd9Sstevel@tonic-gate 	if (i & 0xc)
1177c478bd9Sstevel@tonic-gate 		h += 2, i >>= 2;
1187c478bd9Sstevel@tonic-gate 	if (i & 0x2)
1197c478bd9Sstevel@tonic-gate 		h += 1;
1207c478bd9Sstevel@tonic-gate 	return (h);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate char *
1247c4dcc55Scasper libc_mktemps(char *as, int slen)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	/* statics are protected by this static mutex */
1277c478bd9Sstevel@tonic-gate 	static mutex_t	mktemp_lock = DEFAULTMUTEX;
1287c478bd9Sstevel@tonic-gate 	static int	pidshift = 0;
1297c478bd9Sstevel@tonic-gate 	static int	previous_try = 0;
1307c478bd9Sstevel@tonic-gate 	static pid_t	previous_pid = 0;
1317c478bd9Sstevel@tonic-gate 	static int	previous_xcnt = XCNT;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	pid_t		pid;
1347c478bd9Sstevel@tonic-gate 	int		try;
1357c478bd9Sstevel@tonic-gate 	int		tryshift;
1367c478bd9Sstevel@tonic-gate 	int		max_try;
1377c478bd9Sstevel@tonic-gate 	char		*s;
1387c478bd9Sstevel@tonic-gate 	char		*first_x;
1397c478bd9Sstevel@tonic-gate 	int		len;
1407c478bd9Sstevel@tonic-gate 	uint_t		xcnt;
1417c478bd9Sstevel@tonic-gate 	struct stat64	buf;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (as == NULL || *as == '\0')	/* If the string passed is null then */
1447c478bd9Sstevel@tonic-gate 		return (as);	/* a pointer to a null string is returned. */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	lmutex_lock(&mktemp_lock);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	pid = getpid();
1497c478bd9Sstevel@tonic-gate 	if (pid != previous_pid) {	/* first time or first after fork() */
1507c478bd9Sstevel@tonic-gate 		/*
1517c478bd9Sstevel@tonic-gate 		 * Randomize the starting sequence number in
1527c478bd9Sstevel@tonic-gate 		 * an attempt to confuse and thwart hackers.
1537c478bd9Sstevel@tonic-gate 		 * Use the low 12 bits of the time in milliseconds.
1547c478bd9Sstevel@tonic-gate 		 */
1557c478bd9Sstevel@tonic-gate 		struct timeval tm;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tm, NULL);
1587c478bd9Sstevel@tonic-gate 		previous_try = (tm.tv_sec * 1000 + tm.tv_usec / 1000) & 0xfff;
1597c478bd9Sstevel@tonic-gate 		previous_pid = pid;
1607c478bd9Sstevel@tonic-gate 		previous_xcnt = XCNT;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/* for all possible values of pid, 0 <= pid < (1 << pidshift) */
1647c478bd9Sstevel@tonic-gate 	if (pidshift == 0)	/* one-time initialization */
1657c478bd9Sstevel@tonic-gate 		pidshift = highbit((uint_t)MAXPID) + 1;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/* count the X's */
1687c478bd9Sstevel@tonic-gate 	xcnt = 0;
1697c478bd9Sstevel@tonic-gate 	len = (int)strlen(as);
1707c4dcc55Scasper 	if (slen >= len || slen < 0)
1717c4dcc55Scasper 		goto fail;
1727c4dcc55Scasper 	len -= slen;
1737c478bd9Sstevel@tonic-gate 	s = as + (len - 1);
1747c478bd9Sstevel@tonic-gate 	while ((len != 0) && (xcnt < XCNT) && (*s == 'X')) {
1757c478bd9Sstevel@tonic-gate 		xcnt++;
1767c478bd9Sstevel@tonic-gate 		len--;
1777c478bd9Sstevel@tonic-gate 		--s;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	first_x = s + 1;	/* Remember pointer to the first X */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* fail if we don't have enough X's to represent MAXPID */
1827c478bd9Sstevel@tonic-gate 	if ((tryshift = xcnt * 6 - pidshift) < 0) {
1837c478bd9Sstevel@tonic-gate 		/*
1847c478bd9Sstevel@tonic-gate 		 * Some broken programs call mktemp() repeatedly,
1857c478bd9Sstevel@tonic-gate 		 * passing the same string without reinserting the X's.
1867c478bd9Sstevel@tonic-gate 		 * Check to see if this is such a call by testing
1877c478bd9Sstevel@tonic-gate 		 * the trailing characters of the string for a
1887c478bd9Sstevel@tonic-gate 		 * match with the process-id.
1897c478bd9Sstevel@tonic-gate 		 */
1907c478bd9Sstevel@tonic-gate 		uint64_t xpid = 0;		/* reconstructed pid */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		s = as + len;
1937c478bd9Sstevel@tonic-gate 		for (xcnt = previous_xcnt; xcnt && s > as; xcnt--) {
1947c478bd9Sstevel@tonic-gate 			int c;
1957c478bd9Sstevel@tonic-gate 			int i;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 			c = *--s;
1987c478bd9Sstevel@tonic-gate 			for (i = 0; i < 64; i++)
1997c478bd9Sstevel@tonic-gate 				if (c == chars[i])
2007c478bd9Sstevel@tonic-gate 					break;
2017c478bd9Sstevel@tonic-gate 			if (i == 64)
2027c478bd9Sstevel@tonic-gate 				goto fail;
2037c478bd9Sstevel@tonic-gate 			xpid = xpid * 64 + i;
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		xpid >>= (previous_xcnt * 6 - pidshift);
2067c478bd9Sstevel@tonic-gate 		xpid &= ((1 << pidshift) - 1);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (xpid == pid &&
2097c478bd9Sstevel@tonic-gate 		    lstat64(as, &buf) == -1 && errno == ENOENT) {
2107c478bd9Sstevel@tonic-gate 			lmutex_unlock(&mktemp_lock);
2117c478bd9Sstevel@tonic-gate 			return (as);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		goto fail;
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* we can try sequence numbers in the range 0 <= try < max_try */
2187c478bd9Sstevel@tonic-gate 	max_try = 1 << tryshift;
2197c478bd9Sstevel@tonic-gate 	if (previous_try >= max_try)
2207c478bd9Sstevel@tonic-gate 		previous_try = 0;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	try = previous_try;
2237c478bd9Sstevel@tonic-gate 	for (;;) {
2247c478bd9Sstevel@tonic-gate 		/* num is up to a 36-bit integer ... */
2257c478bd9Sstevel@tonic-gate 		uint64_t num = ((uint64_t)pid << tryshift) + (uint64_t)try;
2267c478bd9Sstevel@tonic-gate 		int i;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		/* ... which we represent backwards in base 64 */
2297c478bd9Sstevel@tonic-gate 		for (i = 0, s = first_x; i < xcnt; i++) {
2307c478bd9Sstevel@tonic-gate 			*s++ = chars[num & 077];
2317c478bd9Sstevel@tonic-gate 			num >>= 6;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		if (lstat64(as, &buf) == -1) {
2357c478bd9Sstevel@tonic-gate 			if (errno != ENOENT)
2367c478bd9Sstevel@tonic-gate 				break;		/* unrecoverable error */
2377c478bd9Sstevel@tonic-gate 			/* remember where we left off for the next call */
2387c478bd9Sstevel@tonic-gate 			previous_try = try + 1;
2397c478bd9Sstevel@tonic-gate 			previous_xcnt = xcnt;
2407c478bd9Sstevel@tonic-gate 			lmutex_unlock(&mktemp_lock);
2417c478bd9Sstevel@tonic-gate 			return (as);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		if (++try == max_try)
2457c478bd9Sstevel@tonic-gate 			try = 0;
2467c478bd9Sstevel@tonic-gate 		if (try == previous_try)
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate fail:
2517c478bd9Sstevel@tonic-gate 	lmutex_unlock(&mktemp_lock);
2527c478bd9Sstevel@tonic-gate 	*as = '\0';
2537c478bd9Sstevel@tonic-gate 	return (as);
2547c478bd9Sstevel@tonic-gate }
2557c4dcc55Scasper 
2567c4dcc55Scasper char *
2577c4dcc55Scasper mktemp(char *template)
2587c4dcc55Scasper {
2597c4dcc55Scasper 	return (libc_mktemps(template, 0));
2607c4dcc55Scasper }
261