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 5*d62bc4baSyz147064 * Common Development and Distribution License (the "License"). 6*d62bc4baSyz147064 * 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 */ 217c478bd9Sstevel@tonic-gate /* 22*d62bc4baSyz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * The copyright in this file is taken from the original Leach & Salz 307c478bd9Sstevel@tonic-gate * UUID specification, from which this implementation is derived. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc. 357c478bd9Sstevel@tonic-gate * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. & 367c478bd9Sstevel@tonic-gate * Digital Equipment Corporation, Maynard, Mass. Copyright (c) 1998 377c478bd9Sstevel@tonic-gate * Microsoft. To anyone who acknowledges that this file is provided 387c478bd9Sstevel@tonic-gate * "AS IS" without any express or implied warranty: permission to use, 397c478bd9Sstevel@tonic-gate * copy, modify, and distribute this file for any purpose is hereby 407c478bd9Sstevel@tonic-gate * granted without fee, provided that the above copyright notices and 417c478bd9Sstevel@tonic-gate * this notice appears in all source code copies, and that none of the 427c478bd9Sstevel@tonic-gate * names of Open Software Foundation, Inc., Hewlett-Packard Company, 437c478bd9Sstevel@tonic-gate * or Digital Equipment Corporation be used in advertising or 447c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the software without 457c478bd9Sstevel@tonic-gate * specific, written prior permission. Neither Open Software 467c478bd9Sstevel@tonic-gate * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital 477c478bd9Sstevel@tonic-gate * Equipment Corporation makes any representations about the 487c478bd9Sstevel@tonic-gate * suitability of this software for any purpose. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * Module: uuid.c 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * Description: This module is the workhorse for generating abstract 557c478bd9Sstevel@tonic-gate * UUIDs. It delegates system-specific tasks (such 567c478bd9Sstevel@tonic-gate * as obtaining the node identifier or system time) 577c478bd9Sstevel@tonic-gate * to the sysdep module. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #include <ctype.h> 617c478bd9Sstevel@tonic-gate #include <sys/param.h> 627c478bd9Sstevel@tonic-gate #include <sys/stat.h> 637c478bd9Sstevel@tonic-gate #include <errno.h> 647c478bd9Sstevel@tonic-gate #include <stdio.h> 657c478bd9Sstevel@tonic-gate #include <stdlib.h> 667c478bd9Sstevel@tonic-gate #include <strings.h> 677c478bd9Sstevel@tonic-gate #include <fcntl.h> 687c478bd9Sstevel@tonic-gate #include <unistd.h> 697c478bd9Sstevel@tonic-gate #include <uuid/uuid.h> 707c478bd9Sstevel@tonic-gate #include <thread.h> 717c478bd9Sstevel@tonic-gate #include <synch.h> 727c478bd9Sstevel@tonic-gate #include "uuid_misc.h" 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define STATE_LOCATION "/var/sadm/system/uuid_state" 757c478bd9Sstevel@tonic-gate #define URANDOM_PATH "/dev/urandom" 767c478bd9Sstevel@tonic-gate #define MAX_RETRY 8 777c478bd9Sstevel@tonic-gate #define VER1_MASK 0xefff 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate static mutex_t ulock = DEFAULTMUTEX; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate uint16_t _get_random(void); 827c478bd9Sstevel@tonic-gate void _get_current_time(uuid_time_t *); 837c478bd9Sstevel@tonic-gate void struct_to_string(uuid_t, struct uuid *); 847c478bd9Sstevel@tonic-gate void string_to_struct(struct uuid *, uuid_t); 857c478bd9Sstevel@tonic-gate int get_ethernet_address(uuid_node_t *); 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * local functions 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate static int _lock_state(char *); 917c478bd9Sstevel@tonic-gate static void _unlock_state(int); 927c478bd9Sstevel@tonic-gate static void _read_state(int, uint16_t *, uuid_time_t *, 937c478bd9Sstevel@tonic-gate uuid_node_t *); 947c478bd9Sstevel@tonic-gate static int _write_state(int, uint16_t, uuid_time_t, uuid_node_t); 957c478bd9Sstevel@tonic-gate static void _format_uuid(struct uuid *, uint16_t, uuid_time_t, 967c478bd9Sstevel@tonic-gate uuid_node_t); 977c478bd9Sstevel@tonic-gate static void fill_random_bytes(uchar_t *, int); 987c478bd9Sstevel@tonic-gate static int uuid_create(struct uuid *); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate static void gen_ethernet_address(uuid_node_t *); 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Name: uuid_create. 1037c478bd9Sstevel@tonic-gate * 1047c478bd9Sstevel@tonic-gate * Description: Generates a uuid based on Version 1 format 1057c478bd9Sstevel@tonic-gate * 1067c478bd9Sstevel@tonic-gate * Returns: 0 on success, -1 on Error 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate static int 1097c478bd9Sstevel@tonic-gate uuid_create(struct uuid *uuid) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate uuid_time_t timestamp, last_time; 1127c478bd9Sstevel@tonic-gate uint16_t clockseq = 0; 1137c478bd9Sstevel@tonic-gate uuid_node_t last_node; 1147c478bd9Sstevel@tonic-gate uuid_node_t system_node; 1157c478bd9Sstevel@tonic-gate int locked_state_fd; 1167c478bd9Sstevel@tonic-gate int non_unique = 0; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if (mutex_lock(&ulock) != 0) { 1197c478bd9Sstevel@tonic-gate return (-1); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate gen_ethernet_address(&system_node); 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * acquire system wide lock so we're alone 1257c478bd9Sstevel@tonic-gate */ 1267c478bd9Sstevel@tonic-gate locked_state_fd = _lock_state(STATE_LOCATION); 1277c478bd9Sstevel@tonic-gate if (locked_state_fd < 0) { 1287c478bd9Sstevel@tonic-gate /* couldn't create and/or lock state; don't have access */ 1297c478bd9Sstevel@tonic-gate non_unique++; 1307c478bd9Sstevel@tonic-gate } else { 1317c478bd9Sstevel@tonic-gate /* read saved state from disk */ 1327c478bd9Sstevel@tonic-gate _read_state(locked_state_fd, &clockseq, &last_time, 1337c478bd9Sstevel@tonic-gate &last_node); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate if (clockseq == 0) { 1377c478bd9Sstevel@tonic-gate /* couldn't read clock sequence; generate a random one */ 1387c478bd9Sstevel@tonic-gate clockseq = _get_random(); 1397c478bd9Sstevel@tonic-gate non_unique++; 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate if (memcmp(&system_node, &last_node, sizeof (uuid_node_t)) != 0) { 1427c478bd9Sstevel@tonic-gate clockseq++; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * get current time 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate _get_current_time(×tamp); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * If timestamp is not set or is not in the past, 1527c478bd9Sstevel@tonic-gate * increment clock sequence. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate if ((last_time == 0) || (last_time >= timestamp)) { 1557c478bd9Sstevel@tonic-gate clockseq++; 1567c478bd9Sstevel@tonic-gate last_time = timestamp; 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (non_unique) 1607c478bd9Sstevel@tonic-gate system_node.nodeID[0] |= 0x80; 1617c478bd9Sstevel@tonic-gate /* 1627c478bd9Sstevel@tonic-gate * stuff fields into the UUID 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate _format_uuid(uuid, clockseq, timestamp, system_node); 1657c478bd9Sstevel@tonic-gate if ((locked_state_fd >= 0) && 1667c478bd9Sstevel@tonic-gate (_write_state(locked_state_fd, clockseq, timestamp, 1677c478bd9Sstevel@tonic-gate system_node) == -1)) { 1687c478bd9Sstevel@tonic-gate _unlock_state(locked_state_fd); 1697c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ulock); 1707c478bd9Sstevel@tonic-gate return (-1); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Unlock system-wide lock 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate _unlock_state(locked_state_fd); 1767c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ulock); 1777c478bd9Sstevel@tonic-gate return (0); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* 1817c478bd9Sstevel@tonic-gate * Name: gen_ethernet_address 1827c478bd9Sstevel@tonic-gate * 1837c478bd9Sstevel@tonic-gate * Description: Fills system_node with Ethernet address if available, 1847c478bd9Sstevel@tonic-gate * else fills random numbers 1857c478bd9Sstevel@tonic-gate * 1867c478bd9Sstevel@tonic-gate * Returns: Nothing 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate static void 1897c478bd9Sstevel@tonic-gate gen_ethernet_address(uuid_node_t *system_node) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate uchar_t node[6]; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (get_ethernet_address(system_node) != 0) { 1947c478bd9Sstevel@tonic-gate fill_random_bytes(node, 6); 1957c478bd9Sstevel@tonic-gate (void) memcpy(system_node->nodeID, node, 6); 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * use 8:0:20 with the multicast bit set 1987c478bd9Sstevel@tonic-gate * to avoid namespace collisions. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate system_node->nodeID[0] = 0x88; 2017c478bd9Sstevel@tonic-gate system_node->nodeID[1] = 0x00; 2027c478bd9Sstevel@tonic-gate system_node->nodeID[2] = 0x20; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Name: _format_uuid 2087c478bd9Sstevel@tonic-gate * 2097c478bd9Sstevel@tonic-gate * Description: Formats a UUID, given the clock_seq timestamp, 2107c478bd9Sstevel@tonic-gate * and node address. Fills in passed-in pointer with 2117c478bd9Sstevel@tonic-gate * the resulting uuid. 2127c478bd9Sstevel@tonic-gate * 2137c478bd9Sstevel@tonic-gate * Returns: None. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate static void 2167c478bd9Sstevel@tonic-gate _format_uuid(struct uuid *uuid, uint16_t clock_seq, 2177c478bd9Sstevel@tonic-gate uuid_time_t timestamp, uuid_node_t node) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * First set up the first 60 bits from the timestamp 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate uuid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF); 2247c478bd9Sstevel@tonic-gate uuid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF); 2257c478bd9Sstevel@tonic-gate uuid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 2267c478bd9Sstevel@tonic-gate 0x0FFF); 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * This is version 1, so say so in the UUID version field (4 bits) 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate uuid->time_hi_and_version |= (1 << 12); 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * Now do the clock sequence 2357c478bd9Sstevel@tonic-gate */ 2367c478bd9Sstevel@tonic-gate uuid->clock_seq_low = clock_seq & 0xFF; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * We must save the most-significant 2 bits for the reserved field 2407c478bd9Sstevel@tonic-gate */ 2417c478bd9Sstevel@tonic-gate uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* 2447c478bd9Sstevel@tonic-gate * The variant for this format is the 2 high bits set to 10, 2457c478bd9Sstevel@tonic-gate * so here it is 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate uuid->clock_seq_hi_and_reserved |= 0x80; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * write result to passed-in pointer 2517c478bd9Sstevel@tonic-gate */ 2527c478bd9Sstevel@tonic-gate (void) memcpy(&uuid->node_addr, &node, sizeof (uuid->node_addr)); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * Name: _read_state 2577c478bd9Sstevel@tonic-gate * 2587c478bd9Sstevel@tonic-gate * Description: Reads non-volatile state from a (possibly) saved statefile. 2597c478bd9Sstevel@tonic-gate * For each non-null pointer passed-in, the corresponding 2607c478bd9Sstevel@tonic-gate * information from the statefile is filled in. 2617c478bd9Sstevel@tonic-gate * the resulting uuid. 2627c478bd9Sstevel@tonic-gate * 2637c478bd9Sstevel@tonic-gate * Returns: Nothing. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate static void 2667c478bd9Sstevel@tonic-gate _read_state(int fd, uint16_t *clockseq, 2677c478bd9Sstevel@tonic-gate uuid_time_t *timestamp, uuid_node_t *node) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate uuid_state_t vol_state; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate bzero(node, sizeof (uuid_node_t)); 2727c478bd9Sstevel@tonic-gate *timestamp = 0; 2737c478bd9Sstevel@tonic-gate *clockseq = 0; 2747c478bd9Sstevel@tonic-gate 275*d62bc4baSyz147064 if (read(fd, &vol_state, sizeof (vol_state)) < sizeof (vol_state)) { 2767c478bd9Sstevel@tonic-gate /* This file is being accessed the first time */ 277*d62bc4baSyz147064 return; 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate *node = vol_state.node; 2817c478bd9Sstevel@tonic-gate *timestamp = vol_state.ts; 2827c478bd9Sstevel@tonic-gate *clockseq = vol_state.cs; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Name: _write_state 2887c478bd9Sstevel@tonic-gate * 2897c478bd9Sstevel@tonic-gate * Description: Writes non-volatile state from the passed-in information. 2907c478bd9Sstevel@tonic-gate * 2917c478bd9Sstevel@tonic-gate * Returns: -1 on error, 0 otherwise. 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate static int 2947c478bd9Sstevel@tonic-gate _write_state(int fd, uint16_t clockseq, 2957c478bd9Sstevel@tonic-gate uuid_time_t timestamp, uuid_node_t node) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate uuid_state_t vol_state; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate vol_state.cs = clockseq; 3007c478bd9Sstevel@tonic-gate vol_state.ts = timestamp; 3017c478bd9Sstevel@tonic-gate vol_state.node = node; 3027c478bd9Sstevel@tonic-gate /* 3037c478bd9Sstevel@tonic-gate * seek to beginning of file and write data 3047c478bd9Sstevel@tonic-gate */ 3057c478bd9Sstevel@tonic-gate if (lseek(fd, 0, SEEK_SET) != -1) { 3067c478bd9Sstevel@tonic-gate if (write(fd, &vol_state, sizeof (uuid_state_t)) != -1) { 3077c478bd9Sstevel@tonic-gate return (0); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate return (-1); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* 3167c478bd9Sstevel@tonic-gate * Name: _uuid_print 3177c478bd9Sstevel@tonic-gate * 3187c478bd9Sstevel@tonic-gate * Description: Prints a nicely-formatted uuid to stdout. 3197c478bd9Sstevel@tonic-gate * 3207c478bd9Sstevel@tonic-gate * Returns: None. 3217c478bd9Sstevel@tonic-gate * 3227c478bd9Sstevel@tonic-gate */ 3237c478bd9Sstevel@tonic-gate void 3247c478bd9Sstevel@tonic-gate uuid_print(struct uuid u) 3257c478bd9Sstevel@tonic-gate { 3267c478bd9Sstevel@tonic-gate int i; 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate (void) printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid, 3297c478bd9Sstevel@tonic-gate u.time_hi_and_version, u.clock_seq_hi_and_reserved, 3307c478bd9Sstevel@tonic-gate u.clock_seq_low); 3317c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) 3327c478bd9Sstevel@tonic-gate (void) printf("%2.2x", u.node_addr[i]); 3337c478bd9Sstevel@tonic-gate (void) printf("\n"); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * Name: _lock_state 3387c478bd9Sstevel@tonic-gate * 3397c478bd9Sstevel@tonic-gate * Description: Locks down the statefile, by first creating the file 3407c478bd9Sstevel@tonic-gate * if it doesn't exist. 3417c478bd9Sstevel@tonic-gate * 3427c478bd9Sstevel@tonic-gate * Returns: A non-negative file descriptor referring to the locked 3437c478bd9Sstevel@tonic-gate * state file, if it was able to be created and/or locked, 3447c478bd9Sstevel@tonic-gate * or -1 otherwise. 3457c478bd9Sstevel@tonic-gate */ 3467c478bd9Sstevel@tonic-gate static int 3477c478bd9Sstevel@tonic-gate _lock_state(char *loc) 3487c478bd9Sstevel@tonic-gate { 3497c478bd9Sstevel@tonic-gate int fd; 3507c478bd9Sstevel@tonic-gate struct flock lock; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate fd = open(loc, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (fd < 0) { 3557c478bd9Sstevel@tonic-gate return (-1); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 3597c478bd9Sstevel@tonic-gate lock.l_start = 0; 3607c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 3617c478bd9Sstevel@tonic-gate lock.l_len = 0; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate if (fcntl(fd, F_SETLKW, &lock) == -1) { 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * File could not be locked, bail 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate (void) close(fd); 3687c478bd9Sstevel@tonic-gate return (-1); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate return (fd); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate /* 3747c478bd9Sstevel@tonic-gate * Name: _unlock_state 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate * Description: Unlocks a locked statefile, and close()'s the file. 3777c478bd9Sstevel@tonic-gate * 3787c478bd9Sstevel@tonic-gate * Returns: Nothing. 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate void 3817c478bd9Sstevel@tonic-gate _unlock_state(int fd) 3827c478bd9Sstevel@tonic-gate { 3837c478bd9Sstevel@tonic-gate struct flock lock; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate lock.l_type = F_UNLCK; 3867c478bd9Sstevel@tonic-gate lock.l_start = 0; 3877c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 3887c478bd9Sstevel@tonic-gate lock.l_len = 0; 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate (void) fcntl(fd, F_SETLK, &lock); 3917c478bd9Sstevel@tonic-gate (void) close(fd); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * Name: fill_random_bytes 3967c478bd9Sstevel@tonic-gate * 3977c478bd9Sstevel@tonic-gate * Description: fills buf with random numbers - nbytes is the number of bytes 3987c478bd9Sstevel@tonic-gate * to fill-in. Tries to use /dev/urandom random number generator- 3997c478bd9Sstevel@tonic-gate * if that fails for some reason, it retries MAX_RETRY times. If 4007c478bd9Sstevel@tonic-gate * it still fails then it uses srand48(3C) 4017c478bd9Sstevel@tonic-gate * 4027c478bd9Sstevel@tonic-gate * Returns: Nothing. 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate static void 4057c478bd9Sstevel@tonic-gate fill_random_bytes(uchar_t *buf, int nbytes) 4067c478bd9Sstevel@tonic-gate { 4077c478bd9Sstevel@tonic-gate int i, fd, retries = 0; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate fd = open(URANDOM_PATH, O_RDONLY); 4107c478bd9Sstevel@tonic-gate if (fd >= 0) { 4117c478bd9Sstevel@tonic-gate while (nbytes > 0) { 4127c478bd9Sstevel@tonic-gate i = read(fd, buf, nbytes); 4137c478bd9Sstevel@tonic-gate if ((i < 0) && (errno == EINTR)) { 4147c478bd9Sstevel@tonic-gate continue; 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate if (i <= 0) { 4177c478bd9Sstevel@tonic-gate if (retries++ == MAX_RETRY) 4187c478bd9Sstevel@tonic-gate break; 4197c478bd9Sstevel@tonic-gate continue; 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate nbytes -= i; 4227c478bd9Sstevel@tonic-gate buf += i; 4237c478bd9Sstevel@tonic-gate retries = 0; 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate if (nbytes == 0) { 4267c478bd9Sstevel@tonic-gate (void) close(fd); 4277c478bd9Sstevel@tonic-gate return; 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate for (i = 0; i < nbytes; i++) { 4317c478bd9Sstevel@tonic-gate *buf++ = _get_random() & 0xFF; 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate if (fd >= 0) { 4347c478bd9Sstevel@tonic-gate (void) close(fd); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate /* 4397c478bd9Sstevel@tonic-gate * Name: struct_to_string 4407c478bd9Sstevel@tonic-gate * 4417c478bd9Sstevel@tonic-gate * Description: Unpacks the structure members in "struct uuid" to a char 4427c478bd9Sstevel@tonic-gate * string "uuid_t". 4437c478bd9Sstevel@tonic-gate * 4447c478bd9Sstevel@tonic-gate * Returns: Nothing. 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate void 4477c478bd9Sstevel@tonic-gate struct_to_string(uuid_t ptr, struct uuid *uu) 4487c478bd9Sstevel@tonic-gate { 4497c478bd9Sstevel@tonic-gate uint_t tmp; 4507c478bd9Sstevel@tonic-gate uchar_t *out = ptr; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate tmp = uu->time_low; 4537c478bd9Sstevel@tonic-gate out[3] = (uchar_t)tmp; 4547c478bd9Sstevel@tonic-gate tmp >>= 8; 4557c478bd9Sstevel@tonic-gate out[2] = (uchar_t)tmp; 4567c478bd9Sstevel@tonic-gate tmp >>= 8; 4577c478bd9Sstevel@tonic-gate out[1] = (uchar_t)tmp; 4587c478bd9Sstevel@tonic-gate tmp >>= 8; 4597c478bd9Sstevel@tonic-gate out[0] = (uchar_t)tmp; 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate tmp = uu->time_mid; 4627c478bd9Sstevel@tonic-gate out[5] = (uchar_t)tmp; 4637c478bd9Sstevel@tonic-gate tmp >>= 8; 4647c478bd9Sstevel@tonic-gate out[4] = (uchar_t)tmp; 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate tmp = uu->time_hi_and_version; 4677c478bd9Sstevel@tonic-gate out[7] = (uchar_t)tmp; 4687c478bd9Sstevel@tonic-gate tmp >>= 8; 4697c478bd9Sstevel@tonic-gate out[6] = (uchar_t)tmp; 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate tmp = uu->clock_seq_hi_and_reserved; 4727c478bd9Sstevel@tonic-gate out[8] = (uchar_t)tmp; 4737c478bd9Sstevel@tonic-gate tmp = uu->clock_seq_low; 4747c478bd9Sstevel@tonic-gate out[9] = (uchar_t)tmp; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate (void) memcpy(out+10, uu->node_addr, 6); 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * Name: string_to_struct 4827c478bd9Sstevel@tonic-gate * 4837c478bd9Sstevel@tonic-gate * Description: Packs the values in the "uuid_t" string into "struct uuid". 4847c478bd9Sstevel@tonic-gate * 4857c478bd9Sstevel@tonic-gate * Returns: Nothing 4867c478bd9Sstevel@tonic-gate */ 4877c478bd9Sstevel@tonic-gate void 4887c478bd9Sstevel@tonic-gate string_to_struct(struct uuid *uuid, uuid_t in) 4897c478bd9Sstevel@tonic-gate { 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate uchar_t *ptr; 4927c478bd9Sstevel@tonic-gate uint_t tmp; 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate ptr = in; 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate tmp = *ptr++; 4977c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4987c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 4997c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 5007c478bd9Sstevel@tonic-gate uuid->time_low = tmp; 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate tmp = *ptr++; 5037c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 5047c478bd9Sstevel@tonic-gate uuid->time_mid = tmp; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate tmp = *ptr++; 5077c478bd9Sstevel@tonic-gate tmp = (tmp << 8) | *ptr++; 5087c478bd9Sstevel@tonic-gate uuid->time_hi_and_version = tmp; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate tmp = *ptr++; 5117c478bd9Sstevel@tonic-gate uuid->clock_seq_hi_and_reserved = tmp; 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate tmp = *ptr++; 5147c478bd9Sstevel@tonic-gate uuid->clock_seq_low = tmp; 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate (void) memcpy(uuid->node_addr, ptr, 6); 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * Name: uuid_generate_random 5227c478bd9Sstevel@tonic-gate * 5237c478bd9Sstevel@tonic-gate * Description: Generates UUID based on DCE Version 4 5247c478bd9Sstevel@tonic-gate * 5257c478bd9Sstevel@tonic-gate * Returns: Nothing. uu contains the newly generated UUID 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate void 5287c478bd9Sstevel@tonic-gate uuid_generate_random(uuid_t uu) 5297c478bd9Sstevel@tonic-gate { 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate struct uuid uuid; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if (uu == NULL) 5347c478bd9Sstevel@tonic-gate return; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate (void) memset(uu, 0, sizeof (uuid_t)); 5377c478bd9Sstevel@tonic-gate (void) memset(&uuid, 0, sizeof (struct uuid)); 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate fill_random_bytes(uu, sizeof (uuid_t)); 5407c478bd9Sstevel@tonic-gate string_to_struct(&uuid, uu); 5417c478bd9Sstevel@tonic-gate /* 5427c478bd9Sstevel@tonic-gate * This is version 4, so say so in the UUID version field (4 bits) 5437c478bd9Sstevel@tonic-gate */ 5447c478bd9Sstevel@tonic-gate uuid.time_hi_and_version |= (1 << 14); 5457c478bd9Sstevel@tonic-gate /* 5467c478bd9Sstevel@tonic-gate * we don't want the bit 1 to be set also which is for version 1 5477c478bd9Sstevel@tonic-gate */ 5487c478bd9Sstevel@tonic-gate uuid.time_hi_and_version &= VER1_MASK; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate /* 5517c478bd9Sstevel@tonic-gate * The variant for this format is the 2 high bits set to 10, 5527c478bd9Sstevel@tonic-gate * so here it is 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate uuid.clock_seq_hi_and_reserved |= 0x80; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * Set MSB of Ethernet address to 1 to indicate that it was generated 5587c478bd9Sstevel@tonic-gate * randomly 5597c478bd9Sstevel@tonic-gate */ 5607c478bd9Sstevel@tonic-gate uuid.node_addr[0] |= 0x80; 5617c478bd9Sstevel@tonic-gate struct_to_string(uu, &uuid); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate /* 5657c478bd9Sstevel@tonic-gate * Name: uuid_generate_time 5667c478bd9Sstevel@tonic-gate * 5677c478bd9Sstevel@tonic-gate * Description: Generates UUID based on DCE Version 1. 5687c478bd9Sstevel@tonic-gate * 5697c478bd9Sstevel@tonic-gate * Returns: Nothing. uu contains the newly generated UUID. 5707c478bd9Sstevel@tonic-gate */ 5717c478bd9Sstevel@tonic-gate void 5727c478bd9Sstevel@tonic-gate uuid_generate_time(uuid_t uu) 5737c478bd9Sstevel@tonic-gate { 5747c478bd9Sstevel@tonic-gate struct uuid uuid; 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate if (uu == NULL) 5777c478bd9Sstevel@tonic-gate return; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate if (uuid_create(&uuid) == -1) { 5807c478bd9Sstevel@tonic-gate uuid_generate_random(uu); 5817c478bd9Sstevel@tonic-gate return; 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate struct_to_string(uu, &uuid); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate 5867c478bd9Sstevel@tonic-gate /* 5877c478bd9Sstevel@tonic-gate * Name: uuid_generate 5887c478bd9Sstevel@tonic-gate * 5897c478bd9Sstevel@tonic-gate * Description: Creates a new UUID. The uuid will be generated based on 5907c478bd9Sstevel@tonic-gate * high-quality randomness from /dev/urandom, if available by 5917c478bd9Sstevel@tonic-gate * calling uuid_generate_random. If it failed to generate UUID 5927c478bd9Sstevel@tonic-gate * then uuid_generate will call uuid_generate_time. 5937c478bd9Sstevel@tonic-gate * 5947c478bd9Sstevel@tonic-gate * Returns: Nothing. uu contains the newly generated UUID. 5957c478bd9Sstevel@tonic-gate */ 5967c478bd9Sstevel@tonic-gate void 5977c478bd9Sstevel@tonic-gate uuid_generate(uuid_t uu) 5987c478bd9Sstevel@tonic-gate { 5997c478bd9Sstevel@tonic-gate int fd; 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate if (uu == NULL) { 6027c478bd9Sstevel@tonic-gate return; 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate fd = open(URANDOM_PATH, O_RDONLY); 6057c478bd9Sstevel@tonic-gate if (fd >= 0) { 6067c478bd9Sstevel@tonic-gate (void) close(fd); 6077c478bd9Sstevel@tonic-gate uuid_generate_random(uu); 6087c478bd9Sstevel@tonic-gate } else { 6097c478bd9Sstevel@tonic-gate (void) uuid_generate_time(uu); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* 6147c478bd9Sstevel@tonic-gate * Name: uuid_copy 6157c478bd9Sstevel@tonic-gate * 6167c478bd9Sstevel@tonic-gate * Description: The uuid_copy function copies the UUID variable src to dst 6177c478bd9Sstevel@tonic-gate * 6187c478bd9Sstevel@tonic-gate * Returns: Nothing 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate void 6217c478bd9Sstevel@tonic-gate uuid_copy(uuid_t dst, uuid_t src) 6227c478bd9Sstevel@tonic-gate { 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate (void) memcpy(dst, src, UUID_LEN); 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate /* 6287c478bd9Sstevel@tonic-gate * Name: uuid_clear 6297c478bd9Sstevel@tonic-gate * 6307c478bd9Sstevel@tonic-gate * Description: The uuid_clear function sets the value of the supplied uuid 6317c478bd9Sstevel@tonic-gate * variable uu, to the NULL value. 6327c478bd9Sstevel@tonic-gate * 6337c478bd9Sstevel@tonic-gate * Returns: Nothing 6347c478bd9Sstevel@tonic-gate */ 6357c478bd9Sstevel@tonic-gate void 6367c478bd9Sstevel@tonic-gate uuid_clear(uuid_t uu) 6377c478bd9Sstevel@tonic-gate { 6387c478bd9Sstevel@tonic-gate (void) memset(uu, 0, UUID_LEN); 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate /* 6427c478bd9Sstevel@tonic-gate * Name: uuid_unparse 6437c478bd9Sstevel@tonic-gate * 6447c478bd9Sstevel@tonic-gate * Description: This function converts the supplied UUID uu from the internal 6457c478bd9Sstevel@tonic-gate * binary format into a 36-byte string (plus trailing null char) 6467c478bd9Sstevel@tonic-gate * and stores this value in the character string pointed to by out 6477c478bd9Sstevel@tonic-gate * 6487c478bd9Sstevel@tonic-gate * Returns: Nothing. 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate void 6517c478bd9Sstevel@tonic-gate uuid_unparse(uuid_t uu, char *out) 6527c478bd9Sstevel@tonic-gate { 6537c478bd9Sstevel@tonic-gate struct uuid uuid; 6547c478bd9Sstevel@tonic-gate uint16_t clock_seq; 6557c478bd9Sstevel@tonic-gate char etheraddr[13]; 6567c478bd9Sstevel@tonic-gate int index = 0, i; 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* basic sanity checking */ 6597c478bd9Sstevel@tonic-gate if (uu == NULL) { 6607c478bd9Sstevel@tonic-gate return; 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate /* XXX user should have allocated enough memory */ 6647c478bd9Sstevel@tonic-gate /* 6656be356c5Sgz161490 * if (strlen(out) < UUID_PRINTABLE_STRING_LENGTH) { 6667c478bd9Sstevel@tonic-gate * return; 6677c478bd9Sstevel@tonic-gate * } 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate string_to_struct(&uuid, uu); 6707c478bd9Sstevel@tonic-gate clock_seq = uuid.clock_seq_hi_and_reserved; 6717c478bd9Sstevel@tonic-gate clock_seq = (clock_seq << 8) | uuid.clock_seq_low; 6727c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) { 6737c478bd9Sstevel@tonic-gate (void) sprintf(ðeraddr[index++], "%.2x", uuid.node_addr[i]); 6747c478bd9Sstevel@tonic-gate index++; 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate etheraddr[index] = '\0'; 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate (void) snprintf(out, 25, "%08x-%04x-%04x-%04x-", 6797c478bd9Sstevel@tonic-gate uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, 6807c478bd9Sstevel@tonic-gate clock_seq); 6816be356c5Sgz161490 (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate /* 6857c478bd9Sstevel@tonic-gate * Name: uuid_is_null 6867c478bd9Sstevel@tonic-gate * 6877c478bd9Sstevel@tonic-gate * Description: The uuid_is_null function compares the value of the supplied 6887c478bd9Sstevel@tonic-gate * UUID variable uu to the NULL value. If the value is equal 6897c478bd9Sstevel@tonic-gate * to the NULL UUID, 1 is returned, otherwise 0 is returned. 6907c478bd9Sstevel@tonic-gate * 6917c478bd9Sstevel@tonic-gate * Returns: 0 if uu is NOT null, 1 if uu is NULL. 6927c478bd9Sstevel@tonic-gate */ 6937c478bd9Sstevel@tonic-gate int 6947c478bd9Sstevel@tonic-gate uuid_is_null(uuid_t uu) 6957c478bd9Sstevel@tonic-gate { 6967c478bd9Sstevel@tonic-gate int i; 6977c478bd9Sstevel@tonic-gate uuid_t null_uu; 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate (void) memset(null_uu, 0, sizeof (uuid_t)); 7007c478bd9Sstevel@tonic-gate i = memcmp(uu, null_uu, sizeof (uuid_t)); 7017c478bd9Sstevel@tonic-gate if (i == 0) { 7027c478bd9Sstevel@tonic-gate /* uu is NULL uuid */ 7037c478bd9Sstevel@tonic-gate return (1); 7047c478bd9Sstevel@tonic-gate } else { 7057c478bd9Sstevel@tonic-gate return (0); 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * Name: uuid_parse 7117c478bd9Sstevel@tonic-gate * 7127c478bd9Sstevel@tonic-gate * Description: uuid_parse converts the UUID string given by 'in' into the 7137c478bd9Sstevel@tonic-gate * internal uuid_t format. The input UUID is a string of the form 7147c478bd9Sstevel@tonic-gate * cefa7a9c-1dd2-11b2-8350-880020adbeef in printf(3C) format. 7157c478bd9Sstevel@tonic-gate * Upon successfully parsing the input string, UUID is stored 7167c478bd9Sstevel@tonic-gate * in the location pointed to by uu 7177c478bd9Sstevel@tonic-gate * 7187c478bd9Sstevel@tonic-gate * Returns: 0 if the UUID is successfully stored, -1 otherwise. 7197c478bd9Sstevel@tonic-gate */ 7207c478bd9Sstevel@tonic-gate int 7217c478bd9Sstevel@tonic-gate uuid_parse(char *in, uuid_t uu) 7227c478bd9Sstevel@tonic-gate { 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate char *ptr, buf[3]; 7257c478bd9Sstevel@tonic-gate int i; 7267c478bd9Sstevel@tonic-gate struct uuid uuid; 7277c478bd9Sstevel@tonic-gate uint16_t clock_seq; 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate /* do some sanity checking */ 7307c478bd9Sstevel@tonic-gate if ((strlen(in) != 36) || (uu == NULL) || (in[36] != '\0')) { 7317c478bd9Sstevel@tonic-gate return (-1); 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate ptr = in; 7357c478bd9Sstevel@tonic-gate for (i = 0; i < 36; i++, ptr++) { 7367c478bd9Sstevel@tonic-gate if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) { 7377c478bd9Sstevel@tonic-gate if (*ptr != '-') { 7387c478bd9Sstevel@tonic-gate return (-1); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate } else { 7417c478bd9Sstevel@tonic-gate if (!isxdigit(*ptr)) { 7427c478bd9Sstevel@tonic-gate return (-1); 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate uuid.time_low = strtoul(in, NULL, 16); 7487c478bd9Sstevel@tonic-gate uuid.time_mid = strtoul(in+9, NULL, 16); 7497c478bd9Sstevel@tonic-gate uuid.time_hi_and_version = strtoul(in+14, NULL, 16); 7507c478bd9Sstevel@tonic-gate clock_seq = strtoul(in+19, NULL, 16); 7517c478bd9Sstevel@tonic-gate uuid.clock_seq_hi_and_reserved = (clock_seq & 0xFF00) >> 8; 7527c478bd9Sstevel@tonic-gate uuid.clock_seq_low = (clock_seq & 0xFF); 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate ptr = in+24; 7557c478bd9Sstevel@tonic-gate buf[2] = '\0'; 7567c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) { 7577c478bd9Sstevel@tonic-gate buf[0] = *ptr++; 7587c478bd9Sstevel@tonic-gate buf[1] = *ptr++; 7597c478bd9Sstevel@tonic-gate uuid.node_addr[i] = strtoul(buf, NULL, 16); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate struct_to_string(uu, &uuid); 7627c478bd9Sstevel@tonic-gate return (0); 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate /* 7667c478bd9Sstevel@tonic-gate * Name: uuid_time 7677c478bd9Sstevel@tonic-gate * 7687c478bd9Sstevel@tonic-gate * Description: uuid_time extracts the time at which the supplied UUID uu 7697c478bd9Sstevel@tonic-gate * was created. This function can only extract the creation 7707c478bd9Sstevel@tonic-gate * time for UUIDs created with the uuid_generate_time function. 7717c478bd9Sstevel@tonic-gate * The time at which the UUID was created, in seconds and 7727c478bd9Sstevel@tonic-gate * microseconds since the epoch is stored in the location 7737c478bd9Sstevel@tonic-gate * pointed to by ret_tv. 7747c478bd9Sstevel@tonic-gate * 7757c478bd9Sstevel@tonic-gate * Returns: The time at which the UUID was created, in seconds since 7767c478bd9Sstevel@tonic-gate * January 1, 1970 GMT (the epoch). -1 otherwise. 7777c478bd9Sstevel@tonic-gate */ 7787c478bd9Sstevel@tonic-gate time_t 7797c478bd9Sstevel@tonic-gate uuid_time(uuid_t uu, struct timeval *ret_tv) 7807c478bd9Sstevel@tonic-gate { 7817c478bd9Sstevel@tonic-gate struct uuid uuid; 7827c478bd9Sstevel@tonic-gate uint_t high; 7837c478bd9Sstevel@tonic-gate struct timeval tv; 7847c478bd9Sstevel@tonic-gate u_longlong_t clock_reg; 7857c478bd9Sstevel@tonic-gate uint_t tmp; 7867c478bd9Sstevel@tonic-gate uint8_t clk; 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate string_to_struct(&uuid, uu); 7897c478bd9Sstevel@tonic-gate tmp = (uuid.time_hi_and_version & 0xF000) >> 12; 7907c478bd9Sstevel@tonic-gate clk = uuid.clock_seq_hi_and_reserved; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate /* check if uu is NULL, Version = 1 of DCE and Variant = 0b10x */ 7937c478bd9Sstevel@tonic-gate if ((uu == NULL) || ((tmp & 0x01) != 0x01) || ((clk & 0x80) != 0x80)) { 7947c478bd9Sstevel@tonic-gate return (-1); 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16); 7977c478bd9Sstevel@tonic-gate clock_reg = uuid.time_low | ((u_longlong_t)high << 32); 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate clock_reg -= (((u_longlong_t)0x01B21DD2) << 32) + 0x13814000; 8007c478bd9Sstevel@tonic-gate tv.tv_sec = clock_reg / 10000000; 8017c478bd9Sstevel@tonic-gate tv.tv_usec = (clock_reg % 10000000) / 10; 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate if (ret_tv) { 8047c478bd9Sstevel@tonic-gate *ret_tv = tv; 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate return (tv.tv_sec); 8087c478bd9Sstevel@tonic-gate } 809