From b37e8184a5a376749fbf68674ed6d7a4fc9901aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 26 Aug 2023 13:39:02 +0200 Subject: [PATCH] id128: allow combining --app with show This effectively exposes sd_id128_get_app_specific() on the commandline. Fixes https://github.com/systemd/systemd/issues/27514. --- man/systemd-id128.xml | 47 +++++++++++++++++++++++++------- src/id128/id128.c | 14 ++++++++-- src/test/test-id128.c | 1 + test/units/testsuite-74.id128.sh | 8 ++++++ 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/man/systemd-id128.xml b/man/systemd-id128.xml index c4521ea945..bf71a7c3a8 100644 --- a/man/systemd-id128.xml +++ b/man/systemd-id128.xml @@ -70,12 +70,6 @@ With boot-id, the identifier of the current boot will be printed. - Both machine-id and boot-id may be combined - with the switch to - generate application-specific IDs. See - sd_id128_get_machine3 - for the discussion when this is useful. - With invocation-id, the identifier of the current service invocation will be printed. This is available in systemd services. See systemd.exec5. @@ -87,6 +81,12 @@ then printed with their name, or arbitrary IDs, which are then printed with a placeholder name. Combine with to list the IDs in UUID style, i.e. the way GPT partition type UUIDs are usually shown. + + machine-id, boot-id, and show may be + combined with the switch to generate + application-specific IDs. See + sd_id128_get_machine3 + for the discussion when this is useful. @@ -116,10 +116,14 @@ - With this option, an identifier that is the result of hashing the - application identifier app-id and the machine identifier will be - printed. The app-id argument must be a valid sd-id128 string - identifying the application. + With this option, identifiers will be printed that are the result of hashing the + application identifier app-id and another ID. The + app-id argument must be a valid sd-id128 string identifying the + application. When used with machine-id, the other ID will be the machine ID as + described in + machine-id5, when + used with boot-id, the other ID will be the boot ID, and when used with + show, the other ID or IDs should be specified via the positional arguments. @@ -165,6 +169,29 @@ NAME ID user-home 773f91ef66d449b5bd83d683bf40ad16 + + + Generate an application-specific UUID + + +$ systemd-id128 machine-id -u +3a9d668b-4db7-4939-8a4a-5e78a03bffb7 + +$ systemd-id128 new -u +1fb8f24b-02df-458d-9659-cc8ace68e28a + +$ systemd-id128 --app=1fb8f24b-02df-458d-9659-cc8ace68e28a +47b82cb1-5339-43da-b2a6-1c350aef1bd1 + +$ systemd-id128 -Pu show 3a9d668b-4db7-4939-8a4a-5e78a03bffb7 --app=1fb8f24b-02df-458d-9659-cc8ace68e28a +47b82cb1-5339-43da-b2a6-1c350aef1bd1 + + + On a given machine with the ID 3a9d668b-4db7-4939-8a4a-5e78a03bffb7, for the application + 1fb8f24b-02df-458d-9659-cc8ace68e28a, we generate an application-specific machine ID. If we want to + later recreate the same calculation on a different machine, we need to specify both IDs explicitly. + + diff --git a/src/id128/id128.c b/src/id128/id128.c index 0ec2df059a..29cd2e2dc0 100644 --- a/src/id128/id128.c +++ b/src/id128/id128.c @@ -68,8 +68,16 @@ static int verb_invocation_id(int argc, char **argv, void *userdata) { } static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) { + sd_id128_t u; int r; + assert(table); + + if (sd_id128_is_null(arg_app)) + u = uuid; + else + assert_se(sd_id128_get_app_specific(uuid, arg_app, &u) == 0); + if (arg_mode == ID128_PRINT_PRETTY) { _cleanup_free_ char *id = NULL; @@ -79,7 +87,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first ascii_strupper(id); - r = id128_pretty_print_sample(id, uuid); + r = id128_pretty_print_sample(id, u); if (r < 0) return r; if (!first) @@ -87,7 +95,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first return 0; } else if (arg_value) - return id128_pretty_print(uuid, arg_mode); + return id128_pretty_print(u, arg_mode); else { if (!*table) { @@ -100,7 +108,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first return table_add_many(*table, TABLE_STRING, name, arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID, - uuid); + u); } } diff --git a/src/test/test-id128.c b/src/test/test-id128.c index ca204d3718..10c061b917 100644 --- a/src/test/test-id128.c +++ b/src/test/test-id128.c @@ -180,6 +180,7 @@ TEST(id128) { assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_FORMAT_ANY, NULL) == -EUCLEAN); + /* build/systemd-id128 -a f03daaeb1c334b43a732172944bf772e show 51df0b4bc3b04c9780e299b98ca373b8 */ assert_se(sd_id128_get_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id) >= 0); assert_se(sd_id128_equal(id, SD_ID128_MAKE(1d,ee,59,54,e7,5c,4d,6f,b9,6c,c6,c0,4c,a1,8a,86))); diff --git a/test/units/testsuite-74.id128.sh b/test/units/testsuite-74.id128.sh index 4e2e1257dc..c1b80d660e 100755 --- a/test/units/testsuite-74.id128.sh +++ b/test/units/testsuite-74.id128.sh @@ -13,6 +13,14 @@ systemd-id128 show --pretty | tail systemd-id128 show --value | tail systemd-id128 show 4f68bce3e8cd4db196e7fbcaf984b709 # root-x86-64 systemd-id128 show --pretty 4f68bce3e8cd4db196e7fbcaf984b709 +systemd-id128 show root-x86-64 +systemd-id128 show --pretty root-x86-64 +[[ "$(systemd-id128 show 4f68bce3e8cd4db196e7fbcaf984b709)" = "$(systemd-id128 show root-x86-64)" ]] +[[ "$(systemd-id128 show 4f68bce3-e8cd-4db1-96e7-fbcaf984b709)" = "$(systemd-id128 show root-x86-64)" ]] + +systemd-id128 show root-x86-64 --app-specific=4f68bce3e8cd4db196e7fbcaf984b709 +systemd-id128 show --pretty root-x86-64 --app-specific=4f68bce3e8cd4db196e7fbcaf984b709 +[[ "$(systemd-id128 show root-x86-64 --app-specific=4f68bce3e8cd4db196e7fbcaf984b709 -P)" = "8ee5535e7cb14c249e1d28b8dfbb939c" ]] [[ "$(systemd-id128 new | wc -c)" -eq 33 ]] systemd-id128 new -p -- 2.25.1