Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 51 additions & 39 deletions bin/uuidgen/uuidgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
#include <stdlib.h>
#include <unistd.h>
#include <uuid.h>
/*
* sys_uuidgen(2) limits the number of UUIDs generated per call
* to 2048 (see sys/kern/kern_uuid.c). As this limit is not
* exposed via a sysctl or public header, mirror it here to
* avoid EINVAL and unbounded allocations.
*/
#define UUIDGEN_BATCH_MAX 2048

static void
usage(void)
Expand Down Expand Up @@ -156,46 +163,51 @@ main(int argc, char *argv[])
if (count == -1)
count = 1;

store = (uuid_t *)malloc(sizeof(uuid_t) * count);
if (store == NULL)
err(1, "malloc()");

if (!iterate) {
/* Get them all in a single batch */
if (version == 1) {
if (uuidgen(store, count) != 0)
err(1, "uuidgen()");
} else if (version == 4) {
if (uuidgen_v4(store, count) != 0)
err(1, "uuidgen_v4()");
} else {
err(1, "unsupported version");
}
} else {
uuid = store;
for (i = 0; i < count; i++) {
if (version == 1) {
if (uuidgen(uuid++, 1) != 0)
err(1, "uuidgen()");
} else if (version == 4) {
if (uuidgen_v4(uuid++, 1) != 0)
err(1, "uuidgen_v4()");
} else {
err(1, "unsupported version");
}
}
}

uuid = store;
while (count--) {
tostring(uuid++, &p, &status);
if (status != uuid_s_ok)
err(1, "cannot stringify a UUID");
fprintf(fp, "%s\n", p);
free(p);
}
store = calloc(UUIDGEN_BATCH_MAX, sizeof(uuid_t));
if (store == NULL)
err(1, "calloc()");

while (count > 0) {
int batch = (count > UUIDGEN_BATCH_MAX) ? UUIDGEN_BATCH_MAX : count;

if (!iterate) {
if (version == 1) {
if (uuidgen(store, batch) != 0)
err(1, "uuidgen()");
} else if (version == 4) {
if (uuidgen_v4(store, batch) != 0)
err(1, "uuidgen_v4()");
} else {
err(1, "unsupported version");
}
} else {
uuid = store;
for (i = 0; i < batch; i++) {
if (version == 1) {
if (uuidgen(uuid++, 1) != 0)
err(1, "uuidgen()");
} else if (version == 4) {
if (uuidgen_v4(uuid++, 1) != 0)
err(1, "uuidgen_v4()");
} else {
err(1, "unsupported version");
}
}
}

uuid = store;
for (i = 0; i < batch; i++) {
tostring(uuid++, &p, &status);
if (status != uuid_s_ok)
err(1, "cannot stringify a UUID");
fprintf(fp, "%s\n", p);
free(p);
}

count -= batch;
}
free(store);

free(store);
if (fp != stdout)
fclose(fp);
return (0);
Expand Down
Loading