summaryrefslogtreecommitdiff
path: root/libbb/xfuncs_printf.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/xfuncs_printf.c')
-rw-r--r--libbb/xfuncs_printf.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index f8b1b81cd..7feb58036 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -524,3 +524,77 @@ int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
524 return ret; 524 return ret;
525} 525}
526#endif 526#endif
527
528char* FAST_FUNC xmalloc_ttyname(int fd)
529{
530 char *buf = xzalloc(128);
531 int r = ttyname_r(fd, buf, 127);
532 if (r) {
533 free(buf);
534 buf = NULL;
535 }
536 return buf;
537}
538
539void FAST_FUNC generate_uuid(uint8_t *buf)
540{
541 /* http://www.ietf.org/rfc/rfc4122.txt
542 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
543 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544 * | time_low |
545 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
546 * | time_mid | time_hi_and_version |
547 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
548 * |clk_seq_and_variant | node (0-1) |
549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
550 * | node (2-5) |
551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
552 * IOW, uuid has this layout:
553 * uint32_t time_low (big endian)
554 * uint16_t time_mid (big endian)
555 * uint16_t time_hi_and_version (big endian)
556 * version is a 4-bit field:
557 * 1 Time-based
558 * 2 DCE Security, with embedded POSIX UIDs
559 * 3 Name-based (MD5)
560 * 4 Randomly generated
561 * 5 Name-based (SHA-1)
562 * uint16_t clk_seq_and_variant (big endian)
563 * variant is a 3-bit field:
564 * 0xx Reserved, NCS backward compatibility
565 * 10x The variant specified in rfc4122
566 * 110 Reserved, Microsoft backward compatibility
567 * 111 Reserved for future definition
568 * uint8_t node[6]
569 *
570 * For version 4, these bits are set/cleared:
571 * time_hi_and_version & 0x0fff | 0x4000
572 * clk_seq_and_variant & 0x3fff | 0x8000
573 */
574 pid_t pid;
575 int i;
576
577 i = open("/dev/urandom", O_RDONLY);
578 if (i >= 0) {
579 read(i, buf, 16);
580 close(i);
581 }
582 /* Paranoia. /dev/urandom may be missing.
583 * rand() is guaranteed to generate at least [0, 2^15) range,
584 * but lowest bits in some libc are not so "random". */
585 srand(monotonic_us()); /* pulls in printf */
586 pid = getpid();
587 while (1) {
588 for (i = 0; i < 16; i++)
589 buf[i] ^= rand() >> 5;
590 if (pid == 0)
591 break;
592 srand(pid);
593 pid = 0;
594 }
595
596 /* version = 4 */
597 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
598 /* variant = 10x */
599 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
600}