mirror of https://github.com/D4-project/d4-core
				
				
				
			
		
			
				
	
	
		
			112 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C
		
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C
		
	
	
| /*
 | |
|  * gen_uuid.c --- generate a DCE-compatible uuid
 | |
|  *
 | |
|  * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
 | |
|  *
 | |
|  * %Begin-Header%
 | |
|  * Redistribution and use in source and binary forms, with or without
 | |
|  * modification, are permitted provided that the following conditions
 | |
|  * are met:
 | |
|  * 1. Redistributions of source code must retain the above copyright
 | |
|  *    notice, and the entire permission notice in its entirety,
 | |
|  *    including the disclaimer of warranties.
 | |
|  * 2. Redistributions in binary form must reproduce the above copyright
 | |
|  *    notice, this list of conditions and the following disclaimer in the
 | |
|  *    documentation and/or other materials provided with the distribution.
 | |
|  * 3. The name of the author may not be used to endorse or promote
 | |
|  *    products derived from this software without specific prior
 | |
|  *    written permission.
 | |
|  *
 | |
|  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 | |
|  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | |
|  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 | |
|  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
 | |
|  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 | |
|  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 | |
|  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 | |
|  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 | |
|  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | |
|  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 | |
|  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
 | |
|  * DAMAGE.
 | |
|  * %End-Header%
 | |
|  */
 | |
| 
 | |
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <unistd.h>
 | |
| #include <string.h>
 | |
| #include <fcntl.h>
 | |
| #include <errno.h>
 | |
| #include <limits.h>
 | |
| #include <sys/types.h>
 | |
| #include <sys/time.h>
 | |
| #include <sys/stat.h>
 | |
| #include "uuidP.h"
 | |
| #include "randutils.h"
 | |
| 
 | |
| #ifdef HAVE_TLS
 | |
| #define THREAD_LOCAL static __thread
 | |
| #else
 | |
| #define THREAD_LOCAL static
 | |
| #endif
 | |
| 
 | |
| /*
 | |
|  * Try using the uuidd daemon to generate the UUID
 | |
|  *
 | |
|  * Returns 0 on success, non-zero on failure.
 | |
|  */
 | |
| 
 | |
| void __uuid_generate_random(uuid_t out, int *num)
 | |
| {
 | |
| 	uuid_t	buf;
 | |
| 	struct uuid uu;
 | |
| 	int i, n;
 | |
| 
 | |
| 	if (!num || !*num)
 | |
| 		n = 1;
 | |
| 	else
 | |
| 		n = *num;
 | |
| 
 | |
| 	for (i = 0; i < n; i++) {
 | |
| 		random_get_bytes(buf, sizeof(buf));
 | |
| 		uuid_unpack(buf, &uu);
 | |
| 
 | |
| 		uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
 | |
| 		uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF)
 | |
| 			| 0x4000;
 | |
| 		uuid_pack(&uu, out);
 | |
| 		out += sizeof(uuid_t);
 | |
| 	}
 | |
| }
 | |
| 
 | |
| void uuid_generate_random(uuid_t out)
 | |
| {
 | |
| 	int	num = 1;
 | |
| 	/* No real reason to use the daemon for random uuid's -- yet */
 | |
| 
 | |
| 	__uuid_generate_random(out, &num);
 | |
| }
 | |
| 
 | |
| /*
 | |
|  * Check whether good random source (/dev/random or /dev/urandom)
 | |
|  * is available.
 | |
|  */
 | |
| static int have_random_source(void)
 | |
| {
 | |
| 	return (access("/dev/random", R_OK) == 0 ||
 | |
| 		access("/dev/urandom", R_OK) == 0);
 | |
| }
 | |
| 
 | |
| /*
 | |
|  * This is the generic front-end to uuid_generate_random and
 | |
|  * uuid_generate_time.  It uses uuid_generate_random only if
 | |
|  * /dev/urandom is available, since otherwise we won't have
 | |
|  * high-quality randomness.
 | |
|  */
 | |
| void uuid_generate(uuid_t out)
 | |
| {
 | |
| 	if (have_random_source())
 | |
| 		uuid_generate_random(out);
 | |
| }
 |