dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libuuid / common / uuid.c
blob42d9abb0e4d78b2e02aff0f5f7e198ed68161758
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
25 * Copyright 2015 Joyent, Inc. All rights reserved.
26 * Copyright 2014 Andrew Stormont.
30 * The copyright in this file is taken from the original Leach & Salz
31 * UUID specification, from which this implementation is derived.
35 * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
36 * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
37 * Digital Equipment Corporation, Maynard, Mass. Copyright (c) 1998
38 * Microsoft. To anyone who acknowledges that this file is provided
39 * "AS IS" without any express or implied warranty: permission to use,
40 * copy, modify, and distribute this file for any purpose is hereby
41 * granted without fee, provided that the above copyright notices and
42 * this notice appears in all source code copies, and that none of the
43 * names of Open Software Foundation, Inc., Hewlett-Packard Company,
44 * or Digital Equipment Corporation be used in advertising or
45 * publicity pertaining to distribution of the software without
46 * specific, written prior permission. Neither Open Software
47 * Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
48 * Equipment Corporation makes any representations about the
49 * suitability of this software for any purpose.
53 * This module is the workhorse for generating abstract
54 * UUIDs. It delegates system-specific tasks (such
55 * as obtaining the node identifier or system time)
56 * to the sysdep module.
59 #include <ctype.h>
60 #include <sys/param.h>
61 #include <sys/stat.h>
62 #include <errno.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <strings.h>
66 #include <fcntl.h>
67 #include <unistd.h>
68 #include <synch.h>
69 #include <sys/mman.h>
70 #include "uuid_misc.h"
72 shared_buffer_t *data;
74 static uuid_node_t node_id_cache;
75 static int node_init;
76 static int fd;
79 * misc routines
81 uint16_t get_random(void);
82 void get_current_time(uuid_time_t *);
84 void struct_to_string(uuid_t, struct uuid *);
85 void string_to_struct(struct uuid *, uuid_t);
86 int get_ethernet_address(uuid_node_t *);
89 * local functions
91 static int map_state();
92 static void format_uuid(struct uuid *, uint16_t, uuid_time_t,
93 uuid_node_t);
94 static int uuid_create(struct uuid *);
95 static void gen_ethernet_address(uuid_node_t *);
96 static void revalidate_data(uuid_node_t *);
99 * Generates a uuid based on version 1 format.
100 * Returns 0 on success and -1 on failure.
102 static int
103 uuid_create(struct uuid *uuid)
105 uuid_time_t timestamp;
106 uuid_node_t system_node;
107 int ret, non_unique = 0;
110 * Get the system MAC address and/or cache it
112 if (node_init) {
113 bcopy(&node_id_cache, &system_node, sizeof (uuid_node_t));
114 } else {
115 gen_ethernet_address(&system_node);
116 bcopy(&system_node, &node_id_cache, sizeof (uuid_node_t));
117 node_init = 1;
121 * Access the state file, mmap it and initialize the shared lock.
123 if (map_state() == -1)
124 return (-1);
127 * Acquire the lock
129 for (;;) {
130 if ((ret = mutex_lock(&data->lock)) == 0)
131 break;
132 else
133 switch (ret) {
134 case EOWNERDEAD:
135 revalidate_data(&system_node);
136 (void) mutex_consistent(&data->lock);
137 (void) mutex_unlock(&data->lock);
138 break;
139 case ENOTRECOVERABLE:
140 return (ret);
144 /* State file is either new or is temporary, get a random clock seq */
145 if (data->state.clock == 0) {
146 data->state.clock = get_random();
147 non_unique++;
150 if (memcmp(&system_node, &data->state.node, sizeof (uuid_node_t)) != 0)
151 data->state.clock++;
153 get_current_time(&timestamp);
156 * If timestamp is not set or is not in the past, bump
157 * data->state.clock
159 if ((data->state.ts == 0) || (data->state.ts >= timestamp)) {
160 data->state.clock++;
161 data->state.ts = timestamp;
164 if (non_unique)
165 system_node.nodeID[0] |= 0x80;
167 /* Stuff fields into the UUID struct */
168 format_uuid(uuid, data->state.clock, timestamp, system_node);
170 (void) mutex_unlock(&data->lock);
172 return (0);
176 * Fills system_node with Ethernet address if available,
177 * else fills random numbers
179 static void
180 gen_ethernet_address(uuid_node_t *system_node)
182 uchar_t node[6];
184 if (get_ethernet_address(system_node) != 0) {
185 arc4random_buf(node, 6);
186 (void) memcpy(system_node->nodeID, node, 6);
188 * use 8:0:20 with the multicast bit set
189 * to avoid namespace collisions.
191 system_node->nodeID[0] = 0x88;
192 system_node->nodeID[1] = 0x00;
193 system_node->nodeID[2] = 0x20;
198 * Formats a UUID, given the clock_seq timestamp, and node address.
199 * Fills in passed-in pointer with the resulting uuid.
201 static void
202 format_uuid(struct uuid *uuid, uint16_t clock_seq,
203 uuid_time_t timestamp, uuid_node_t node)
207 * First set up the first 60 bits from the timestamp
209 uuid->time_low = (uint32_t)(timestamp & 0xFFFFFFFF);
210 uuid->time_mid = (uint16_t)((timestamp >> 32) & 0xFFFF);
211 uuid->time_hi_and_version = (uint16_t)((timestamp >> 48) & 0x0FFF);
214 * This is version 1, so say so in the UUID version field (4 bits)
216 uuid->time_hi_and_version |= (1 << 12);
219 * Now do the clock sequence
221 uuid->clock_seq_low = clock_seq & 0xFF;
224 * We must save the most-significant 2 bits for the reserved field
226 uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
229 * The variant for this format is the 2 high bits set to 10,
230 * so here it is
232 uuid->clock_seq_hi_and_reserved |= 0x80;
235 * write result to passed-in pointer
237 (void) memcpy(&uuid->node_addr, &node, sizeof (uuid->node_addr));
241 * Opens/creates the state file, falling back to a tmp
243 static int
244 map_state()
246 FILE *tmp;
248 /* If file's mapped, return */
249 if (data)
250 return (1);
252 if ((fd = open(STATE_LOCATION, O_RDWR)) < 0) {
253 if ((tmp = tmpfile()) == NULL)
254 return (-1);
255 else
256 fd = fileno(tmp);
259 (void) ftruncate(fd, (off_t)sizeof (shared_buffer_t));
261 /* LINTED - alignment */
262 data = mmap(NULL, sizeof (shared_buffer_t),
263 PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
265 if (data == MAP_FAILED) {
266 data = NULL;
267 return (-1);
270 (void) mutex_init(&data->lock, USYNC_PROCESS|LOCK_ROBUST, 0);
272 (void) close(fd);
274 return (1);
277 static void
278 revalidate_data(uuid_node_t *node)
280 int i;
282 data->state.ts = 0;
284 for (i = 0; i < sizeof (data->state.node.nodeID); i++)
285 data->state.node.nodeID[i] = 0;
287 data->state.clock = 0;
289 gen_ethernet_address(node);
290 bcopy(node, &node_id_cache, sizeof (uuid_node_t));
291 node_init = 1;
295 * Prints a nicely-formatted uuid to stdout.
297 void
298 uuid_print(struct uuid u)
300 int i;
302 (void) printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
303 u.time_hi_and_version, u.clock_seq_hi_and_reserved,
304 u.clock_seq_low);
305 for (i = 0; i < 6; i++)
306 (void) printf("%2.2x", u.node_addr[i]);
307 (void) printf("\n");
311 * Unpacks the structure members in "struct uuid" to a char string "uuid_t".
313 void
314 struct_to_string(uuid_t ptr, struct uuid *uu)
316 uint_t tmp;
317 uchar_t *out = ptr;
319 tmp = uu->time_low;
320 out[3] = (uchar_t)tmp;
321 tmp >>= 8;
322 out[2] = (uchar_t)tmp;
323 tmp >>= 8;
324 out[1] = (uchar_t)tmp;
325 tmp >>= 8;
326 out[0] = (uchar_t)tmp;
328 tmp = uu->time_mid;
329 out[5] = (uchar_t)tmp;
330 tmp >>= 8;
331 out[4] = (uchar_t)tmp;
333 tmp = uu->time_hi_and_version;
334 out[7] = (uchar_t)tmp;
335 tmp >>= 8;
336 out[6] = (uchar_t)tmp;
338 tmp = uu->clock_seq_hi_and_reserved;
339 out[8] = (uchar_t)tmp;
340 tmp = uu->clock_seq_low;
341 out[9] = (uchar_t)tmp;
343 (void) memcpy(out+10, uu->node_addr, 6);
348 * Packs the values in the "uuid_t" string into "struct uuid".
350 void
351 string_to_struct(struct uuid *uuid, uuid_t in)
353 uchar_t *ptr;
354 uint_t tmp;
356 ptr = in;
358 tmp = *ptr++;
359 tmp = (tmp << 8) | *ptr++;
360 tmp = (tmp << 8) | *ptr++;
361 tmp = (tmp << 8) | *ptr++;
362 uuid->time_low = tmp;
364 tmp = *ptr++;
365 tmp = (tmp << 8) | *ptr++;
366 uuid->time_mid = tmp;
368 tmp = *ptr++;
369 tmp = (tmp << 8) | *ptr++;
370 uuid->time_hi_and_version = tmp;
372 tmp = *ptr++;
373 uuid->clock_seq_hi_and_reserved = tmp;
375 tmp = *ptr++;
376 uuid->clock_seq_low = tmp;
378 (void) memcpy(uuid->node_addr, ptr, 6);
383 * Generates UUID based on DCE Version 4
385 void
386 uuid_generate_random(uuid_t uu)
388 struct uuid uuid;
390 if (uu == NULL)
391 return;
393 (void) memset(uu, 0, sizeof (uuid_t));
394 (void) memset(&uuid, 0, sizeof (struct uuid));
396 arc4random_buf(uu, sizeof (uuid_t));
397 string_to_struct(&uuid, uu);
399 * This is version 4, so say so in the UUID version field (4 bits)
401 uuid.time_hi_and_version |= (1 << 14);
403 * we don't want the bit 1 to be set also which is for version 1
405 uuid.time_hi_and_version &= VER1_MASK;
408 * The variant for this format is the 2 high bits set to 10,
409 * so here it is
411 uuid.clock_seq_hi_and_reserved |= 0x80;
414 * Set MSB of Ethernet address to 1 to indicate that it was generated
415 * randomly
417 uuid.node_addr[0] |= 0x80;
418 struct_to_string(uu, &uuid);
422 * Generates UUID based on DCE Version 1.
424 void
425 uuid_generate_time(uuid_t uu)
427 struct uuid uuid;
429 if (uu == NULL)
430 return;
432 if (uuid_create(&uuid) < 0) {
433 uuid_generate_random(uu);
434 return;
437 struct_to_string(uu, &uuid);
441 * Creates a new UUID. The uuid will be generated based on high-quality
442 * randomness from arc4random(3C).
444 void
445 uuid_generate(uuid_t uu)
447 uuid_generate_random(uu);
451 * Copies the UUID variable src to dst.
453 void
454 uuid_copy(uuid_t dst, uuid_t src)
456 (void) memcpy(dst, src, UUID_LEN);
460 * Sets the value of the supplied uuid variable uu, to the NULL value.
462 void
463 uuid_clear(uuid_t uu)
465 (void) memset(uu, 0, UUID_LEN);
469 * This function converts the supplied UUID uu from the internal
470 * binary format into a 36-byte string (plus trailing null char)
471 * and stores this value in the character string pointed to by out.
473 static void
474 uuid_unparse_common(uuid_t uu, char *out, boolean_t upper)
476 struct uuid uuid;
477 uint16_t clock_seq;
478 char etheraddr[13];
479 int index = 0, i;
481 /* basic sanity checking */
482 if (uu == NULL) {
483 return;
486 string_to_struct(&uuid, uu);
487 clock_seq = uuid.clock_seq_hi_and_reserved;
488 clock_seq = (clock_seq << 8) | uuid.clock_seq_low;
489 for (i = 0; i < 6; i++) {
490 (void) sprintf(&etheraddr[index++], upper ? "%.2X" : "%.2x",
491 uuid.node_addr[i]);
492 index++;
494 etheraddr[index] = '\0';
496 (void) snprintf(out, 25,
497 upper ? "%08X-%04X-%04X-%04X-" : "%08x-%04x-%04x-%04x-",
498 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq);
499 (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH);
502 void
503 uuid_unparse_upper(uuid_t uu, char *out)
505 uuid_unparse_common(uu, out, B_TRUE);
508 void
509 uuid_unparse_lower(uuid_t uu, char *out)
511 uuid_unparse_common(uu, out, B_FALSE);
514 void
515 uuid_unparse(uuid_t uu, char *out)
518 * Historically uuid_unparse on Solaris returns lower case,
519 * for compatibility we preserve this behaviour.
521 uuid_unparse_common(uu, out, B_FALSE);
525 * The uuid_is_null function compares the value of the supplied
526 * UUID variable uu to the NULL value. If the value is equal
527 * to the NULL UUID, 1 is returned, otherwise 0 is returned.
530 uuid_is_null(uuid_t uu)
532 int i;
533 uuid_t null_uu;
535 (void) memset(null_uu, 0, sizeof (uuid_t));
536 i = memcmp(uu, null_uu, sizeof (uuid_t));
537 if (i == 0) {
538 /* uu is NULL uuid */
539 return (1);
540 } else {
541 return (0);
546 * uuid_parse converts the UUID string given by 'in' into the
547 * internal uuid_t format. The input UUID is a string of the form
548 * cefa7a9c-1dd2-11b2-8350-880020adbeef in printf(3C) format.
549 * Upon successfully parsing the input string, UUID is stored
550 * in the location pointed to by uu
553 uuid_parse(char *in, uuid_t uu)
556 char *ptr, buf[3];
557 int i;
558 struct uuid uuid;
559 uint16_t clock_seq;
561 /* do some sanity checking */
562 if ((strlen(in) != 36) || (uu == NULL) || (in[36] != '\0')) {
563 return (-1);
566 ptr = in;
567 for (i = 0; i < 36; i++, ptr++) {
568 if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
569 if (*ptr != '-') {
570 return (-1);
572 } else {
573 if (!isxdigit(*ptr)) {
574 return (-1);
579 uuid.time_low = strtoul(in, NULL, 16);
580 uuid.time_mid = strtoul(in+9, NULL, 16);
581 uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
582 clock_seq = strtoul(in+19, NULL, 16);
583 uuid.clock_seq_hi_and_reserved = (clock_seq & 0xFF00) >> 8;
584 uuid.clock_seq_low = (clock_seq & 0xFF);
586 ptr = in+24;
587 buf[2] = '\0';
588 for (i = 0; i < 6; i++) {
589 buf[0] = *ptr++;
590 buf[1] = *ptr++;
591 uuid.node_addr[i] = strtoul(buf, NULL, 16);
593 struct_to_string(uu, &uuid);
594 return (0);
598 * uuid_time extracts the time at which the supplied UUID uu
599 * was created. This function can only extract the creation
600 * time for UUIDs created with the uuid_generate_time function.
601 * The time at which the UUID was created, in seconds and
602 * microseconds since the epoch is stored in the location
603 * pointed to by ret_tv.
605 time_t
606 uuid_time(uuid_t uu, struct timeval *ret_tv)
608 struct uuid uuid;
609 uint_t high;
610 struct timeval tv;
611 u_longlong_t clock_reg;
612 uint_t tmp;
613 uint8_t clk;
615 string_to_struct(&uuid, uu);
616 tmp = (uuid.time_hi_and_version & 0xF000) >> 12;
617 clk = uuid.clock_seq_hi_and_reserved;
619 /* check if uu is NULL, Version = 1 of DCE and Variant = 0b10x */
620 if ((uu == NULL) || ((tmp & 0x01) != 0x01) || ((clk & 0x80) != 0x80)) {
621 return (-1);
623 high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
624 clock_reg = uuid.time_low | ((u_longlong_t)high << 32);
626 clock_reg -= (((u_longlong_t)0x01B21DD2) << 32) + 0x13814000;
627 tv.tv_sec = clock_reg / 10000000;
628 tv.tv_usec = (clock_reg % 10000000) / 10;
630 if (ret_tv) {
631 *ret_tv = tv;
634 return (tv.tv_sec);