UUIDGEN(2)

✨ UUIDGEN(2) ✨

okay bestie, welcome to the system calls manual πŸ’– this one's called uuidgen and it lives in section 2, so it's a syscall, not some lil command-line tool (that's a different page, we'll get there). buckle up πŸŽ€


🌈 NAME 🌈

uuidgen β€” generate universally unique identifiers πŸ’…


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) 🫢


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/uuid.h>

int
uuidgen(struct uuid *store, int count);

πŸ’œ DESCRIPTION πŸ’œ

so like. the uuidgen() system call generates count universally unique identifiers (UUIDs, bestie) and writes them into the buffer that store points at. they're generated according to the syntax and semantics of the DCE version 1 variant of universally unique identifiers β€” and yeah, there's a whole deeper breakdown of the identifiers down below, we're getting there, don't rush me πŸ’…

when there's no IEEE 802 address available for the node field? a random multicast address gets generated for each invocation of the system call. and per the algorithm for generating time-based UUIDs, that also forces a new random clock sequence, which just makes the identifier even more likely to be unique. it's giving "we did our best and our best is very good actually" ✨

when multiple identifiers are generated, uuidgen() produces a set that is dense β€” meaning there is no identifier that is larger than the smallest identifier in the set and smaller than the largest identifier in the set and that is not already in the set. no gaps, no crumbs, fully contiguous, periodt πŸŽ€

universally unique identifiers β€” also known as globally unique identifiers (GUIDs), iykyk β€” have a binary representation of 128 bits. the grouping and meaning of those bits is described by this structure, plus the field descriptions that follow it:

struct uuid {
        uint32_t        time_low;
        uint16_t        time_mid;
        uint16_t        time_hi_and_version;
        uint8_t         clock_seq_hi_and_reserved;
        uint8_t         clock_seq_low;
        uint8_t         node[_UUID_NODE_LEN];
};
  • time_low β€” the least significant 32 bits of a 60-bit timestamp. this field is stored in the native byte-order. πŸ’–
  • time_mid β€” the least significant 16 bits of the most significant 28 bits of the 60-bit timestamp. stored in the native byte-order.
  • time_hi_and_version β€” the most significant 12 bits of the 60-bit timestamp, multiplexed with a 4-bit version number. the version number is stored in the most significant 4 bits of the 16-bit field. stored in the native byte-order.
  • clock_seq_hi_and_reserved β€” the most significant 6 bits of a 14-bit sequence number, multiplexed with a 2-bit variant value. heads up: the width of the variant value is determined by the variant itself. identifiers generated by uuidgen() have variant value 10b. the variant value is stored in the most significant bits of the field.
  • clock_seq_low β€” the least significant 8 bits of a 14-bit sequence number.
  • node β€” the 6-byte IEEE 802 (MAC) address of one of the interfaces of the node. if no such interface exists, a random multi-cast address is used instead.

and here's the tea πŸ«–: the binary representation is sensitive to byte ordering. any multi-byte field is to be stored in the local or native byte-order, and identifiers must be converted when transmitted to hosts that do not agree on the byte-order. the specification does not document what this means in concrete terms though, and it's otherwise beyond the scope of this system call. so like. good luck, bestie 😭


πŸ’« RETURN VALUES πŸ’«

upon successful completion, the value 0 is returned. otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic. πŸ’…


πŸ”₯ ERRORS πŸ”₯

the uuidgen() system call can fail with:

  • [EFAULT] β€” the buffer pointed to by store could not be written to, for any or all identifiers. πŸ’€
  • [EINVAL] β€” the count argument is less than 1 or larger than the hard upper limit of 2048.

πŸ‘‘ SEE ALSO πŸ‘‘


🌸 STANDARDS 🌸

the identifiers are represented and generated in conformance with the DCE 1.1 RPC specification. the uuidgen() system call is itself not part of the specification. it's the icon that wasn't on the original guest list, and honestly? still slayed πŸŽ€


⭐ HISTORY ⭐

the uuidgen() system call first appeared in FreeBSD 5.0. ✨


and that's the whole page, bestie!! this one's from Debian, May 26, 2002 β€” yes it's older than us, and no, we're not gonna say anything about it 🫢 go forth and generate those UUIDs, you're gonna eat πŸ’…πŸŒˆπŸ’–

πŸ“¦ where this page comes from