We expose various other forms of UUID helpers already, i.e.
SD_ID128_UUID_FORMAT_STR and SD_ID128_MAKE_UUID_STR(), and we parse
UUIDs, hence add a high-level helper for formatting UUIDs too.
This doesn't add any new code, it just moves some helpers
id128-util.[ch] → sd-id128.[ch], to make them public.
['sd_id128_randomize', '3', [], ''],
['sd_id128_to_string',
'3',
- ['SD_ID128_STRING_MAX', 'SD_ID128_TO_STRING', 'sd_id128_from_string'],
+ ['SD_ID128_STRING_MAX',
+ 'SD_ID128_TO_STRING',
+ 'SD_ID128_TO_UUID_STRING',
+ 'SD_ID128_UUID_STRING_MAX',
+ 'sd_id128_from_string',
+ 'sd_id128_to_uuid_string'],
''],
['sd_is_fifo',
'3',
<refnamediv>
<refname>sd_id128_to_string</refname>
<refname>SD_ID128_TO_STRING</refname>
- <refname>sd_id128_from_string</refname>
<refname>SD_ID128_STRING_MAX</refname>
+ <refname>sd_id128_to_uuid_string</refname>
+ <refname>SD_ID128_TO_UUID_STRING</refname>
+ <refname>SD_ID128_UUID_STRING_MAX</refname>
+ <refname>sd_id128_from_string</refname>
<refpurpose>Format or parse 128-bit IDs as strings</refpurpose>
</refnamediv>
<funcsynopsisinfo>#define SD_ID128_STRING_MAX 33U</funcsynopsisinfo>
+ <funcsynopsisinfo>#define SD_ID128_UUID_STRING_MAX 37U</funcsynopsisinfo>
+
<funcsynopsisinfo>#define SD_ID128_TO_STRING(id) …</funcsynopsisinfo>
+ <funcsynopsisinfo>#define SD_ID128_TO_UUID_STRING(id) …</funcsynopsisinfo>
+
<funcprototype>
<funcdef>char *<function>sd_id128_to_string</function></funcdef>
<paramdef>sd_id128_t <parameter>id</parameter>, char <parameter>s</parameter>[static SD_ID128_STRING_MAX]</paramdef>
</funcprototype>
+ <funcprototype>
+ <funcdef>char *<function>sd_id128_uuid_string</function></funcdef>
+ <paramdef>sd_id128_t <parameter>id</parameter>, char <parameter>s</parameter>[static SD_ID128_UUID_STRING_MAX]</paramdef>
+ </funcprototype>
+
<funcprototype>
<funcdef>int <function>sd_id128_from_string</function></funcdef>
<paramdef>const char *<parameter>s</parameter>, sd_id128_t *<parameter>ret</parameter></paramdef>
which remains valid until the end of the current code block. This is usually the simplest way to acquire
a string representation of a 128-bit ID in a buffer that is valid in the current code block.</para>
+ <para><function>sd_id128_to_uuid_string()</function> and <function>SD_ID128_TO_UUID_STRING()</function>
+ are similar to these two functions/macros, but format the 128bit values as RFC4122 UUIDs, i.e. a series
+ of 36 lowercase hexadeciaml digits and dashes, terminated by a <constant>NUL</constant> byte.</para>
+
<para><function>sd_id128_from_string()</function> implements the reverse operation: it takes a 33
character string with 32 hexadecimal digits (either lowercase or uppercase, terminated by
<constant>NUL</constant>) and parses them back into a 128-bit ID returned in
ID formatted as RFC UUID. If <parameter>ret</parameter> is passed as <constant>NULL</constant> the
function will validate the passed ID string, but not actually return it in parsed form.</para>
- <para>Note that when parsing 37 character UUIDs this is done strictly in Big Endian byte order,
+ <para>Note that when formatting and parsing 36 character UUIDs this is done strictly in Big Endian byte order,
i.e. according to <ulink url="https://tools.ietf.org/html/rfc4122">RFC4122</ulink> Variant 1 rules, even
if the UUID encodes a different variant. This matches behaviour in various other Linux userspace
tools. It's probably wise to avoid UUIDs of other variant types.</para>
CRYPT_LUKS2,
user_record_luks_cipher(hr),
user_record_luks_cipher_mode(hr),
- ID128_TO_UUID_STRING(uuid),
+ SD_ID128_TO_UUID_STRING(uuid),
volume_key,
volume_key_size,
&(struct crypt_params_luks2) {
if (r < 0)
return log_error_errno(r, "Failed to set partition name: %m");
- r = fdisk_partition_set_uuid(p, ID128_TO_UUID_STRING(uuid));
+ r = fdisk_partition_set_uuid(p, SD_ID128_TO_UUID_STRING(uuid));
if (r < 0)
return log_error_errno(r, "Failed to set partition UUID: %m");
if (!result)
return log_oom();
- fdisk_ask_string_set_result(ask, id128_to_uuid_string(*(sd_id128_t*) userdata, result));
+ fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) userdata, result));
break;
default:
r = json_build(&new_binding_entry,
JSON_BUILD_OBJECT(
JSON_BUILD_PAIR_CONDITION(!!image_path, "imagePath", JSON_BUILD_STRING(image_path)),
- JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(partition_uuid), "partitionUuid", JSON_BUILD_STRING(ID128_TO_UUID_STRING(partition_uuid))),
- JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(luks_uuid), "luksUuid", JSON_BUILD_STRING(ID128_TO_UUID_STRING(luks_uuid))),
- JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(fs_uuid), "fileSystemUuid", JSON_BUILD_STRING(ID128_TO_UUID_STRING(fs_uuid))),
+ JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(partition_uuid), "partitionUuid", JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(partition_uuid))),
+ JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(luks_uuid), "luksUuid", JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(luks_uuid))),
+ JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(fs_uuid), "fileSystemUuid", JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(fs_uuid))),
JSON_BUILD_PAIR_CONDITION(!!luks_cipher, "luksCipher", JSON_BUILD_STRING(luks_cipher)),
JSON_BUILD_PAIR_CONDITION(!!luks_cipher_mode, "luksCipherMode", JSON_BUILD_STRING(luks_cipher_mode)),
JSON_BUILD_PAIR_CONDITION(luks_volume_key_size != UINT64_MAX, "luksVolumeKeySize", JSON_BUILD_UNSIGNED(luks_volume_key_size)),
sd_event_add_inotify_fd;
sd_event_source_set_ratelimit_expire_callback;
} LIBSYSTEMD_249;
+
+LIBSYSTEMD_251 {
+global:
+ sd_id128_to_uuid_string;
+} LIBSYSTEMD_250;
if (r < 0)
return r;
- j = strjoina(s, " ", ID128_TO_UUID_STRING(u));
+ j = strjoina(s, " ", SD_ID128_TO_UUID_STRING(u));
r = sd_device_set_sysattr_value(device, "uevent", j);
if (r < 0)
#include "string-util.h"
#include "sync-util.h"
-char *id128_to_uuid_string(sd_id128_t id, char s[static ID128_UUID_STRING_MAX]) {
- unsigned n, k = 0;
-
- assert(s);
-
- /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
-
- for (n = 0; n < 16; n++) {
-
- if (IN_SET(n, 4, 6, 8, 10))
- s[k++] = '-';
-
- s[k++] = hexchar(id.bytes[n] >> 4);
- s[k++] = hexchar(id.bytes[n] & 0xF);
- }
-
- assert(k == 36);
-
- s[k] = 0;
-
- return s;
-}
-
bool id128_is_valid(const char *s) {
size_t i, l;
assert(f < _ID128_FORMAT_MAX);
if (f != ID128_UUID) {
- sd_id128_to_string(id, buffer);
- buffer[32] = '\n';
- sz = 33;
+ assert_se(sd_id128_to_string(id, buffer));
+ buffer[SD_ID128_STRING_MAX - 1] = '\n';
+ sz = SD_ID128_STRING_MAX;
} else {
- id128_to_uuid_string(id, buffer);
- buffer[36] = '\n';
- sz = 37;
+ assert_se(sd_id128_to_uuid_string(id, buffer));
+ buffer[SD_ID128_UUID_STRING_MAX - 1] = '\n';
+ sz = SD_ID128_UUID_STRING_MAX;
}
r = loop_write(fd, buffer, sz, false);
#include "hash-funcs.h"
#include "macro.h"
-#define ID128_UUID_STRING_MAX 37
-
-char *id128_to_uuid_string(sd_id128_t id, char s[static ID128_UUID_STRING_MAX]);
-
-#define ID128_TO_UUID_STRING(id) id128_to_uuid_string((id), (char[ID128_UUID_STRING_MAX]) {})
-
bool id128_is_valid(const char *s) _pure_;
typedef enum Id128Format {
#include "util.h"
_public_ char *sd_id128_to_string(sd_id128_t id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX]) {
- unsigned n;
-
assert_return(s, NULL);
- for (n = 0; n < 16; n++) {
+ for (size_t n = 0; n < 16; n++) {
s[n*2] = hexchar(id.bytes[n] >> 4);
s[n*2+1] = hexchar(id.bytes[n] & 0xF);
}
- s[32] = 0;
+ s[SD_ID128_STRING_MAX-1] = 0;
+
+ return s;
+}
+
+_public_ char *sd_id128_to_uuid_string(sd_id128_t id, char s[_SD_ARRAY_STATIC SD_ID128_UUID_STRING_MAX]) {
+ size_t k = 0;
+
+ assert_return(s, NULL);
+
+ /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
+
+ for (size_t n = 0; n < 16; n++) {
+
+ if (IN_SET(n, 4, 6, 8, 10))
+ s[k++] = '-';
+
+ s[k++] = hexchar(id.bytes[n] >> 4);
+ s[k++] = hexchar(id.bytes[n] & 0xF);
+ }
+
+ assert(k == SD_ID128_UUID_STRING_MAX - 1);
+ s[k] = 0;
return s;
}
assert(!sd_id128_is_null(arg_uuid));
- if (asprintf(envp + n_env++, "container_uuid=%s", ID128_TO_UUID_STRING(arg_uuid)) < 0)
+ if (asprintf(envp + n_env++, "container_uuid=%s", SD_ID128_TO_UUID_STRING(arg_uuid)) < 0)
return log_oom();
if (fdset_size(fds) > 0) {
if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
return -EINVAL;
- ids = new(char, ID128_UUID_STRING_MAX);
+ ids = new(char, SD_ID128_UUID_STRING_MAX);
if (!ids)
return -ENOMEM;
- r = fdisk_ask_string_set_result(ask, id128_to_uuid_string(*(sd_id128_t*) data, ids));
+ r = fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids));
if (r < 0)
return r;
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL;
- char uuid_buffer[ID128_UUID_STRING_MAX];
+ char uuid_buffer[SD_ID128_UUID_STRING_MAX];
const char *label, *activity = NULL;
if (p->dropped)
else
id = p->type_uuid;
- buf = strdup(ID128_TO_UUID_STRING(id));
+ buf = strdup(SD_ID128_TO_UUID_STRING(id));
done:
if (!buf)
CRYPT_LUKS2,
"aes",
"xts-plain64",
- ID128_TO_UUID_STRING(uuid),
+ SD_ID128_TO_UUID_STRING(uuid),
volume_key,
volume_key_size,
&(struct crypt_params_luks2) {
if (gpt_partition_type_knows_no_auto(p->type_uuid))
SET_FLAG(f, GPT_FLAG_NO_AUTO, p->no_auto);
else {
- char buffer[ID128_UUID_STRING_MAX];
+ char buffer[SD_ID128_UUID_STRING_MAX];
log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
yes_no(p->no_auto),
gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
if (gpt_partition_type_knows_read_only(p->type_uuid))
SET_FLAG(f, GPT_FLAG_READ_ONLY, p->read_only);
else {
- char buffer[ID128_UUID_STRING_MAX];
+ char buffer[SD_ID128_UUID_STRING_MAX];
log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
yes_no(p->read_only),
gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
if (gpt_partition_type_knows_growfs(p->type_uuid))
SET_FLAG(f, GPT_FLAG_GROWFS, p->growfs);
else {
- char buffer[ID128_UUID_STRING_MAX];
+ char buffer[SD_ID128_UUID_STRING_MAX];
log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
yes_no(p->growfs),
gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
assert(!sd_id128_is_null(p->new_uuid));
- r = fdisk_partition_set_uuid(p->current_partition, ID128_TO_UUID_STRING(p->new_uuid));
+ r = fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid));
if (r < 0)
return log_error_errno(r, "Failed to set partition UUID: %m");
if (!t)
return log_oom();
- r = fdisk_parttype_set_typestr(t, ID128_TO_UUID_STRING(p->type_uuid));
+ r = fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type_uuid));
if (r < 0)
return log_error_errno(r, "Failed to initialize partition type: %m");
if (r < 0)
return log_error_errno(r, "Failed to set partition number: %m");
- r = fdisk_partition_set_uuid(q, ID128_TO_UUID_STRING(p->new_uuid));
+ r = fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid));
if (r < 0)
return log_error_errno(r, "Failed to set partition UUID: %m");
case TABLE_UUID: {
char *p;
- p = new(char, ID128_UUID_STRING_MAX);
+ p = new(char, SD_ID128_UUID_STRING_MAX);
if (!p)
return NULL;
- d->formatted = id128_to_uuid_string(d->id128, p);
+ d->formatted = sd_id128_to_uuid_string(d->id128, p);
break;
}
return json_variant_new_string(ret, SD_ID128_TO_STRING(d->id128));
case TABLE_UUID:
- return json_variant_new_string(ret, ID128_TO_UUID_STRING(d->id128));
+ return json_variant_new_string(ret, SD_ID128_TO_UUID_STRING(d->id128));
case TABLE_UID:
if (!uid_is_valid(d->uid))
const char *gpt_partition_type_uuid_to_string_harder(
sd_id128_t id,
- char buffer[static ID128_UUID_STRING_MAX]) {
+ char buffer[static SD_ID128_UUID_STRING_MAX]) {
const char *s;
if (s)
return s;
- return id128_to_uuid_string(id, buffer);
+ return sd_id128_to_uuid_string(id, buffer);
}
int gpt_partition_type_uuid_from_string(const char *s, sd_id128_t *ret) {
const char *gpt_partition_type_uuid_to_string(sd_id128_t id);
const char *gpt_partition_type_uuid_to_string_harder(
sd_id128_t id,
- char buffer[static ID128_UUID_STRING_MAX]);
+ char buffer[static SD_ID128_UUID_STRING_MAX]);
int gpt_partition_type_uuid_from_string(const char *s, sd_id128_t *ret);
Architecture gpt_partition_type_uuid_to_arch(sd_id128_t id);
static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
_cleanup_close_pair_ int pair[2] = { -1, -1 };
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
- char buf[ID128_UUID_STRING_MAX];
+ char buf[SD_ID128_UUID_STRING_MAX];
pid_t pid, child;
ssize_t k;
int r;
bool discard) {
_cleanup_free_ char *mkfs = NULL, *mangled_label = NULL;
- char vol_id[CONST_MAX(ID128_UUID_STRING_MAX, 8 + 1)] = {};
+ char vol_id[CONST_MAX(SD_ID128_UUID_STRING_MAX, 8U + 1U)] = {};
int r;
assert(node);
}
if (isempty(vol_id))
- id128_to_uuid_string(uuid, vol_id);
+ assert_se(sd_id128_to_uuid_string(uuid, vol_id));
r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR, NULL);
if (r < 0)
strempty(str), isempty(str) ? "" : " ",
seqnum, strna(device_action_to_string(action)),
sd_id128_is_null(event_id) ? "" : ", UUID=",
- sd_id128_is_null(event_id) ? "" : id128_to_uuid_string(event_id, (char[ID128_UUID_STRING_MAX]){}));
+ sd_id128_is_null(event_id) ? "" : SD_ID128_TO_UUID_STRING(event_id));
}
int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos) {
};
#define SD_ID128_STRING_MAX 33U
+#define SD_ID128_UUID_STRING_MAX 37U
char *sd_id128_to_string(sd_id128_t id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX]);
+char *sd_id128_to_uuid_string(sd_id128_t id, char s[_SD_ARRAY_STATIC SD_ID128_UUID_STRING_MAX]);
int sd_id128_from_string(const char *s, sd_id128_t *ret);
#define SD_ID128_TO_STRING(id) sd_id128_to_string((id), (char[SD_ID128_STRING_MAX]) {})
+#define SD_ID128_TO_UUID_STRING(id) sd_id128_to_uuid_string((id), (char[SD_ID128_UUID_STRING_MAX]) {})
int sd_id128_randomize(sd_id128_t *ret);
TEST(id128) {
sd_id128_t id, id2;
- char t[SD_ID128_STRING_MAX], q[ID128_UUID_STRING_MAX];
+ char t[SD_ID128_STRING_MAX], q[SD_ID128_UUID_STRING_MAX];
_cleanup_free_ char *b = NULL;
_cleanup_close_ int fd = -1;
printf("waldi2: %s\n", b);
assert_se(streq(t, b));
- printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
+ printf("waldi3: %s\n", sd_id128_to_uuid_string(ID128_WALDI, q));
assert_se(streq(q, UUID_WALDI));
b = mfree(b);
assert_se(ftruncate(fd, 0) >= 0);
assert_se(sd_id128_randomize(&id) >= 0);
- assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36);
+ assert_se(write(fd, sd_id128_to_uuid_string(id, q), 36) == 36);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
if (!*data_what) {
memcpy(&data_uuid, m, sizeof(data_uuid));
- *data_what = path_join("/dev/disk/by-partuuid", ID128_TO_UUID_STRING(data_uuid));
+ *data_what = path_join("/dev/disk/by-partuuid", SD_ID128_TO_UUID_STRING(data_uuid));
if (!*data_what)
return log_oom();
}
if (!*hash_what) {
memcpy(&verity_uuid, (uint8_t*) m + l - sizeof(verity_uuid), sizeof(verity_uuid));
- *hash_what = path_join("/dev/disk/by-partuuid", ID128_TO_UUID_STRING(verity_uuid));
+ *hash_what = path_join("/dev/disk/by-partuuid", SD_ID128_TO_UUID_STRING(verity_uuid));
if (!*hash_what)
return log_oom();
}