]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/uuid/gen_uuid.c
2 * gen_uuid.c --- generate a DCE-compatible uuid
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
36 * Force inclusion of SVID stuff since we need it if we're compiling in
51 #include <sys/types.h>
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>
63 #ifdef HAVE_SYS_SOCKIO_H
64 #include <sys/sockio.h>
69 #ifdef HAVE_NETINET_IN_H
70 #include <netinet/in.h>
72 #ifdef HAVE_NET_IF_DL_H
73 #include <net/if_dl.h>
75 #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
76 #include <sys/syscall.h>
83 #define srand(x) srandom(x)
84 #define rand() random()
88 #define THREAD_LOCAL static TLS
90 #define THREAD_LOCAL static
93 #if defined(__linux__) && defined(__NR_gettid) && defined(HAVE_JRAND48)
95 THREAD_LOCAL
unsigned short jrand_seed
[3];
98 static int get_random_fd(void)
105 gettimeofday(&tv
, 0);
106 fd
= open("/dev/urandom", O_RDONLY
);
108 fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
);
110 i
= fcntl(fd
, F_GETFD
);
112 fcntl(fd
, F_SETFD
, i
| FD_CLOEXEC
);
114 srand((getpid() << 16) ^ getuid() ^ tv
.tv_sec
^ tv
.tv_usec
);
116 jrand_seed
[0] = getpid() ^ (tv
.tv_sec
& 0xFFFF);
117 jrand_seed
[1] = getppid() ^ (tv
.tv_usec
& 0xFFFF);
118 jrand_seed
[2] = (tv
.tv_sec
^ tv
.tv_usec
) >> 16;
121 /* Crank the random number generator a few times */
122 gettimeofday(&tv
, 0);
123 for (i
= (tv
.tv_sec
^ tv
.tv_usec
) & 0x1F; i
> 0; i
--)
130 * Generate a series of random bytes. Use /dev/urandom if possible,
131 * and if not, use srandom/random.
133 static void get_random_bytes(void *buf
, int nbytes
)
135 int i
, n
= nbytes
, fd
= get_random_fd();
136 int lose_counter
= 0;
137 unsigned char *cp
= (unsigned char *) buf
;
138 unsigned short tmp_seed
[3];
144 if (lose_counter
++ > 16)
155 * We do this all the time, but this is the only source of
156 * randomness if /dev/random/urandom is out to lunch.
158 for (cp
= buf
, i
= 0; i
< nbytes
; i
++)
159 *cp
++ ^= (rand() >> 7) & 0xFF;
161 memcpy(tmp_seed
, jrand_seed
, sizeof(tmp_seed
));
162 jrand_seed
[2] = jrand_seed
[2] ^ syscall(__NR_gettid
);
163 for (cp
= buf
, i
= 0; i
< nbytes
; i
++)
164 *cp
++ ^= (jrand48(tmp_seed
) >> 7) & 0xFF;
165 memcpy(jrand_seed
, tmp_seed
,
166 sizeof(jrand_seed
)-sizeof(unsigned short));
173 * Get the ethernet hardware address, if we can find it...
175 static int get_node_id(unsigned char *node_id
)
179 struct ifreq ifr
, *ifrp
;
184 #ifdef HAVE_NET_IF_DL_H
185 struct sockaddr_dl
*sdlp
;
189 * BSD 4.4 defines the size of an ifreq to be
190 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
191 * However, under earlier systems, sa_len isn't present, so the size is
192 * just sizeof(struct ifreq)
196 #define max(a,b) ((a) > (b) ? (a) : (b))
198 #define ifreq_size(i) max(sizeof(struct ifreq),\
199 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
201 #define ifreq_size(i) sizeof(struct ifreq)
202 #endif /* HAVE_SA_LEN*/
204 sd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
208 memset(buf
, 0, sizeof(buf
));
209 ifc
.ifc_len
= sizeof(buf
);
211 if (ioctl (sd
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
216 for (i
= 0; i
< n
; i
+= ifreq_size(*ifrp
) ) {
217 ifrp
= (struct ifreq
*)((char *) ifc
.ifc_buf
+i
);
218 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
, IFNAMSIZ
);
220 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) < 0)
222 a
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
225 if (ioctl(sd
, SIOCGENADDR
, &ifr
) < 0)
227 a
= (unsigned char *) ifr
.ifr_enaddr
;
229 #ifdef HAVE_NET_IF_DL_H
230 sdlp
= (struct sockaddr_dl
*) &ifrp
->ifr_addr
;
231 if ((sdlp
->sdl_family
!= AF_LINK
) || (sdlp
->sdl_alen
!= 6))
233 a
= (unsigned char *) &sdlp
->sdl_data
[sdlp
->sdl_nlen
];
236 * XXX we don't have a way of getting the hardware
241 #endif /* HAVE_NET_IF_DL_H */
242 #endif /* SIOCGENADDR */
243 #endif /* SIOCGIFHWADDR */
244 if (!a
[0] && !a
[1] && !a
[2] && !a
[3] && !a
[4] && !a
[5])
247 memcpy(node_id
, a
, 6);
257 /* Assume that the gettimeofday() has microsecond granularity */
258 #define MAX_ADJUSTMENT 10
260 static int get_clock(uint32_t *clock_high
, uint32_t *clock_low
,
261 uint16_t *ret_clock_seq
, int *num
)
263 THREAD_LOCAL
int adjustment
= 0;
264 THREAD_LOCAL
struct timeval last
= {0, 0};
265 THREAD_LOCAL
int state_fd
= -2;
266 THREAD_LOCAL
FILE *state_f
;
267 THREAD_LOCAL
uint16_t clock_seq
;
270 unsigned long long clock_reg
;
273 if (state_fd
== -2) {
274 save_umask
= umask(0);
275 state_fd
= open("/var/lib/libuuid/clock.txt",
276 O_RDWR
|O_CREAT
, 0660);
277 (void) umask(save_umask
);
278 state_f
= fdopen(state_fd
, "r+");
285 fl
.l_whence
= SEEK_SET
;
291 while (fcntl(state_fd
, F_SETLKW
, &fl
) < 0) {
292 if ((errno
== EAGAIN
) || (errno
== EINTR
))
302 unsigned long tv1
, tv2
;
305 if (fscanf(state_f
, "clock: %04x tv: %lu %lu adj: %d\n",
306 &cl
, &tv1
, &tv2
, &a
) == 4) {
307 clock_seq
= cl
& 0x3FFF;
314 if ((last
.tv_sec
== 0) && (last
.tv_usec
== 0)) {
315 get_random_bytes(&clock_seq
, sizeof(clock_seq
));
322 gettimeofday(&tv
, 0);
323 if ((tv
.tv_sec
< last
.tv_sec
) ||
324 ((tv
.tv_sec
== last
.tv_sec
) &&
325 (tv
.tv_usec
< last
.tv_usec
))) {
326 clock_seq
= (clock_seq
+1) & 0x3FFF;
329 } else if ((tv
.tv_sec
== last
.tv_sec
) &&
330 (tv
.tv_usec
== last
.tv_usec
)) {
331 if (adjustment
>= MAX_ADJUSTMENT
)
339 clock_reg
= tv
.tv_usec
*10 + adjustment
;
340 clock_reg
+= ((unsigned long long) tv
.tv_sec
)*10000000;
341 clock_reg
+= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
343 if (num
&& (*num
> 1)) {
344 adjustment
+= *num
- 1;
345 last
.tv_usec
+= adjustment
/ 10;
346 adjustment
= adjustment
% 10;
347 last
.tv_sec
+= last
.tv_usec
/ 1000000;
348 last
.tv_usec
= last
.tv_usec
% 1000000;
353 ftruncate(state_fd
, 0);
354 fprintf(state_f
, "clock: %04x tv: %lu %lu adj: %d\n",
355 clock_seq
, last
.tv_sec
, last
.tv_usec
, adjustment
);
359 fcntl(state_fd
, F_SETLK
, &fl
);
362 *clock_high
= clock_reg
>> 32;
363 *clock_low
= clock_reg
;
364 *ret_clock_seq
= clock_seq
;
368 static ssize_t
read_all(int fd
, char *buf
, size_t count
)
373 memset(buf
, 0, count
);
375 ret
= read(fd
, buf
, count
);
377 if ((errno
== EAGAIN
) || (errno
== EINTR
))
390 * Try using the uuidd daemon to generate the UUID
392 * Returns 0 on success, non-zero on failure.
394 static int get_uuid_via_daemon(int op
, uuid_t out
, int *num
)
401 int32_t reply_len
= 0, expected
= 16;
402 struct sockaddr_un srv_addr
;
404 static const char *uuidd_path
= UUIDD_PATH
;
405 static int access_ret
= -2;
406 static int start_attempts
= 0;
408 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
411 srv_addr
.sun_family
= AF_UNIX
;
412 strcpy(srv_addr
.sun_path
, UUIDD_SOCKET_PATH
);
414 if (connect(s
, (const struct sockaddr
*) &srv_addr
,
415 sizeof(struct sockaddr_un
)) < 0) {
416 if (access_ret
== -2)
417 access_ret
= access(uuidd_path
, X_OK
);
418 if (access_ret
== 0 && start_attempts
++ < 5) {
419 if ((pid
= fork()) == 0) {
420 execl(uuidd_path
, "uuidd", "-qT", "300",
424 (void) waitpid(pid
, 0, 0);
425 if (connect(s
, (const struct sockaddr
*) &srv_addr
,
426 sizeof(struct sockaddr_un
)) < 0)
433 if (op
== UUIDD_OP_BULK_TIME_UUID
) {
434 memcpy(op_buf
+1, num
, sizeof(*num
));
435 op_len
+= sizeof(*num
);
436 expected
+= sizeof(*num
);
439 ret
= write(s
, op_buf
, op_len
);
443 ret
= read_all(s
, (char *) &reply_len
, sizeof(reply_len
));
447 if (reply_len
!= expected
)
450 ret
= read_all(s
, op_buf
, reply_len
);
452 if (op
== UUIDD_OP_BULK_TIME_UUID
)
453 memcpy(op_buf
+16, num
, sizeof(int));
455 memcpy(out
, op_buf
, 16);
458 return ((ret
== expected
) ? 0 : -1);
466 void uuid__generate_time(uuid_t out
, int *num
)
468 static unsigned char node_id
[6];
469 static int has_init
= 0;
474 if (get_node_id(node_id
) <= 0) {
475 get_random_bytes(node_id
, 6);
477 * Set multicast bit, to prevent conflicts
478 * with IEEE 802 addresses obtained from
485 get_clock(&clock_mid
, &uu
.time_low
, &uu
.clock_seq
, num
);
486 uu
.clock_seq
|= 0x8000;
487 uu
.time_mid
= (uint16_t) clock_mid
;
488 uu
.time_hi_and_version
= ((clock_mid
>> 16) & 0x0FFF) | 0x1000;
489 memcpy(uu
.node
, node_id
, 6);
493 void uuid_generate_time(uuid_t out
)
496 THREAD_LOCAL
int num
= 0;
497 THREAD_LOCAL
struct uuid uu
;
498 THREAD_LOCAL
time_t last_time
= 0;
503 if (now
> last_time
+1)
508 if (get_uuid_via_daemon(UUIDD_OP_BULK_TIME_UUID
,
511 uuid_unpack(out
, &uu
);
519 if (uu
.time_low
== 0) {
521 if (uu
.time_mid
== 0)
522 uu
.time_hi_and_version
++;
529 if (get_uuid_via_daemon(UUIDD_OP_TIME_UUID
, out
, 0) == 0)
533 uuid__generate_time(out
, 0);
537 void uuid__generate_random(uuid_t out
, int *num
)
548 for (i
= 0; i
< n
; i
++) {
549 get_random_bytes(buf
, sizeof(buf
));
550 uuid_unpack(buf
, &uu
);
552 uu
.clock_seq
= (uu
.clock_seq
& 0x3FFF) | 0x8000;
553 uu
.time_hi_and_version
= (uu
.time_hi_and_version
& 0x0FFF)
556 out
+= sizeof(uuid_t
);
560 void uuid_generate_random(uuid_t out
)
563 /* No real reason to use the daemon for random uuid's -- yet */
565 uuid__generate_random(out
, &num
);
570 * This is the generic front-end to uuid_generate_random and
571 * uuid_generate_time. It uses uuid_generate_random only if
572 * /dev/urandom is available, since otherwise we won't have
573 * high-quality randomness.
575 void uuid_generate(uuid_t out
)
577 if (get_random_fd() >= 0)
578 uuid_generate_random(out
);
580 uuid_generate_time(out
);