1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-31 16:21:26 +03:00

Merge pull request #19365 from keszybz/sd_id128_equals

Add sd_id128_in_set()
This commit is contained in:
Lennart Poettering 2021-04-21 21:45:14 +02:00 committed by GitHub
commit e19479aa7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 59 deletions

View File

@ -119,7 +119,8 @@ manpages = [
['sd-hwdb', '3', [], ''],
['sd-id128',
'3',
['SD_ID128_CONST_STR',
['SD_ID128_ALLF',
'SD_ID128_CONST_STR',
'SD_ID128_FORMAT_STR',
'SD_ID128_FORMAT_VAL',
'SD_ID128_MAKE',
@ -127,6 +128,10 @@ manpages = [
'SD_ID128_NULL',
'SD_ID128_UUID_FORMAT_STR',
'sd_id128_equal',
'sd_id128_in_set',
'sd_id128_in_set_sentinel',
'sd_id128_in_setv',
'sd_id128_is_allf',
'sd_id128_is_null',
'sd_id128_t'],
''],

View File

@ -18,16 +18,21 @@
<refnamediv>
<refname>sd-id128</refname>
<refname>sd_id128_t</refname>
<refname>SD_ID128_ALLF</refname>
<refname>SD_ID128_CONST_STR</refname>
<refname>SD_ID128_FORMAT_STR</refname>
<refname>SD_ID128_FORMAT_VAL</refname>
<refname>SD_ID128_MAKE</refname>
<refname>SD_ID128_MAKE_STR</refname>
<refname>SD_ID128_NULL</refname>
<refname>SD_ID128_CONST_STR</refname>
<refname>SD_ID128_FORMAT_STR</refname>
<refname>SD_ID128_UUID_FORMAT_STR</refname>
<refname>SD_ID128_FORMAT_VAL</refname>
<refname>sd_id128_equal</refname>
<refname>sd_id128_in_set</refname>
<refname>sd_id128_in_set_sentinel</refname>
<refname>sd_id128_in_setv</refname>
<refname>sd_id128_is_allf</refname>
<refname>sd_id128_is_null</refname>
<refname>sd_id128_t</refname>
<refpurpose>APIs for processing 128-bit IDs</refpurpose>
</refnamediv>
@ -65,8 +70,8 @@
union type:</para>
<programlisting>typedef union sd_id128 {
uint8_t bytes[16];
uint64_t qwords[2];
uint8_t bytes[16];
uint64_t qwords[2];
} sd_id128_t;</programlisting>
<para>This union type allows accessing the 128-bit ID as 16
@ -85,7 +90,7 @@
<programlisting>#define SD_MESSAGE_COREDUMP SD_ID128_MAKE(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)</programlisting>
<para><constant>SD_ID128_NULL</constant> may be used to refer to the 128bit ID consisting of only
<para><constant>SD_ID128_NULL</constant> may be used to refer to the 128-bit ID consisting of only
<constant>NUL</constant> bytes.</para>
<para><function>SD_ID128_MAKE_STR()</function> is similar to <function>SD_ID128_MAKE()</function>, but creates a
@ -95,7 +100,7 @@
#define SD_MESSAGE_COREDUMP_STR SD_ID128_MAKE_STR(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)
int main(int argc, char **argv) {
puts("Match for coredumps: MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
puts("Match for coredumps: MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
}
</programlisting>
@ -104,7 +109,7 @@ int main(int argc, char **argv) {
following example code will output the string
"fc2e22bc6ee647b6b90729ab34a250b1":</para>
<programlisting>int main(int argc, char *argv[]) {
puts("Match for coredumps: %s", SD_ID128_CONST_STR(SD_MESSAGE_COREDUMP));
puts("Match for coredumps: %s", SD_ID128_CONST_STR(SD_MESSAGE_COREDUMP));
}</programlisting>
<para><constant>SD_ID128_FORMAT_STR</constant> and <function>SD_ID128_FORMAT_VAL()</function> may
@ -113,10 +118,10 @@ int main(int argc, char **argv) {
format string, as shown in the following example:</para>
<programlisting>int main(int argc, char *argv[]) {
sd_id128_t id;
id = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
printf("The ID encoded in this C file is " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(id));
return 0;
sd_id128_t id;
id = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
printf("The ID encoded in this C file is " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(id));
return 0;
}</programlisting>
<para><constant>SD_ID128_UUID_FORMAT_STR</constant> is similar to
@ -127,21 +132,50 @@ int main(int argc, char **argv) {
<para>Use <function>sd_id128_equal()</function> to compare two 128-bit IDs:</para>
<programlisting>int main(int argc, char *argv[]) {
sd_id128_t a, b, c;
a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
b = SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e);
c = a;
assert(sd_id128_equal(a, c));
assert(!sd_id128_equal(a, b));
return 0;
sd_id128_t a, b, c;
a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
b = SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e);
c = a;
assert(sd_id128_equal(a, c));
assert(!sd_id128_equal(a, b));
return 0;
}</programlisting>
<para>Use <function>sd_id128_is_null()</function> to check if an 128bit ID consists of only
<para>Use <function>sd_id128_is_null()</function> to check if an 128-bit ID consists of only
<constant>NUL</constant> bytes:</para>
<programlisting>assert(sd_id128_is_null(SD_ID128_NULL));</programlisting>
<para>Similarly, use <function>sd_id128_is_allf()</function> to check if an 128-bit ID consists of only
<constant>0xFF</constant> bytes (all bits on):</para>
<programlisting>assert(sd_id128_is_allf(SD_ID128_ALLF));</programlisting>
<para>For convenience, <function>sd_id128_in_set()</function> takes a list of IDs and
returns true if any are equal to the first argument:</para>
<programlisting>int main(int argc, char *argv[]) {
assert(sd_id128_is_null(SD_ID128_NULL));
}</programlisting>
sd_id12_t a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
assert(sd_id128_in_set(a, a));
assert(sd_id128_in_set(a, a, a));
assert(!sd_id128_in_set(a));
assert(!sd_id128_in_set(a,
SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e)
SD_ID128_MAKE(2f,88,28,5f,9c,44,09,9d,d7,15,77,04,bc,85,7e,e3)
SD_ID128_ALLF));
return 0;
}
</programlisting>
<para><function>sd_id128_in_set()</function> is defined as a macro over
<function>sd_id128_in_set_sentinel()</function>, adding the <constant>SD_ID128_NULL</constant>
sentinel. Since <function>sd_id128_in_set_sentinel()</function> uses <constant>SD_ID128_NULL</constant>
as the sentinel, <constant>SD_ID128_NULL</constant> cannot be otherwise placed in the argument list.
</para>
<para><function>sd_id128_in_setv()</function> is similar to
<function>sd_id128_in_set_sentinel()</function>, but takes a <structname>struct varargs</structname>
argument.</para>
<para>Note that new, randomized IDs may be generated with
<citerefentry><refentrytitle>systemd-id128</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s

View File

@ -3115,9 +3115,8 @@ static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *re
if (p == q)
continue;
if (sd_id128_equal(q->current_uuid, result.id) ||
sd_id128_equal(q->new_uuid, result.id)) {
log_warning("Partition UUID calculated from seed for partition %" PRIu64 " exists already, reverting to randomized UUID.", p->partno);
if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) {
log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno);
r = sd_id128_randomize(&result.id);
if (r < 0)

View File

@ -108,52 +108,57 @@ int gpt_partition_label_valid(const char *s) {
}
bool gpt_partition_type_is_root(sd_id128_t id) {
return sd_id128_equal(id, GPT_ROOT_X86) ||
sd_id128_equal(id, GPT_ROOT_X86_64) ||
sd_id128_equal(id, GPT_ROOT_ARM) ||
sd_id128_equal(id, GPT_ROOT_ARM_64) ||
sd_id128_equal(id, GPT_ROOT_IA64) ||
sd_id128_equal(id, GPT_ROOT_RISCV32) ||
sd_id128_equal(id, GPT_ROOT_RISCV64);
return sd_id128_in_set(id,
GPT_ROOT_X86,
GPT_ROOT_X86_64,
GPT_ROOT_ARM,
GPT_ROOT_ARM_64,
GPT_ROOT_IA64,
GPT_ROOT_RISCV32,
GPT_ROOT_RISCV64);
}
bool gpt_partition_type_is_root_verity(sd_id128_t id) {
return sd_id128_equal(id, GPT_ROOT_X86_VERITY) ||
sd_id128_equal(id, GPT_ROOT_X86_64_VERITY) ||
sd_id128_equal(id, GPT_ROOT_ARM_VERITY) ||
sd_id128_equal(id, GPT_ROOT_ARM_64_VERITY) ||
sd_id128_equal(id, GPT_ROOT_IA64_VERITY) ||
sd_id128_equal(id, GPT_ROOT_RISCV32_VERITY) ||
sd_id128_equal(id, GPT_ROOT_RISCV64_VERITY);
return sd_id128_in_set(id,
GPT_ROOT_X86_VERITY,
GPT_ROOT_X86_64_VERITY,
GPT_ROOT_ARM_VERITY,
GPT_ROOT_ARM_64_VERITY,
GPT_ROOT_IA64_VERITY,
GPT_ROOT_RISCV32_VERITY,
GPT_ROOT_RISCV64_VERITY);
}
bool gpt_partition_type_is_usr(sd_id128_t id) {
return sd_id128_equal(id, GPT_USR_X86) ||
sd_id128_equal(id, GPT_USR_X86_64) ||
sd_id128_equal(id, GPT_USR_ARM) ||
sd_id128_equal(id, GPT_USR_ARM_64) ||
sd_id128_equal(id, GPT_USR_IA64) ||
sd_id128_equal(id, GPT_USR_RISCV32) ||
sd_id128_equal(id, GPT_USR_RISCV64);
return sd_id128_in_set(id,
GPT_USR_X86,
GPT_USR_X86_64,
GPT_USR_ARM,
GPT_USR_ARM_64,
GPT_USR_IA64,
GPT_USR_RISCV32,
GPT_USR_RISCV64);
}
bool gpt_partition_type_is_usr_verity(sd_id128_t id) {
return sd_id128_equal(id, GPT_USR_X86_VERITY) ||
sd_id128_equal(id, GPT_USR_X86_64_VERITY) ||
sd_id128_equal(id, GPT_USR_ARM_VERITY) ||
sd_id128_equal(id, GPT_USR_ARM_64_VERITY) ||
sd_id128_equal(id, GPT_USR_IA64_VERITY) ||
sd_id128_equal(id, GPT_USR_RISCV32_VERITY) ||
sd_id128_equal(id, GPT_USR_RISCV64_VERITY);
return sd_id128_in_set(id,
GPT_USR_X86_VERITY,
GPT_USR_X86_64_VERITY,
GPT_USR_ARM_VERITY,
GPT_USR_ARM_64_VERITY,
GPT_USR_IA64_VERITY,
GPT_USR_RISCV32_VERITY,
GPT_USR_RISCV64_VERITY);
}
bool gpt_partition_type_knows_read_only(sd_id128_t id) {
return gpt_partition_type_is_root(id) ||
gpt_partition_type_is_usr(id) ||
sd_id128_equal(id, GPT_HOME) ||
sd_id128_equal(id, GPT_SRV) ||
sd_id128_equal(id, GPT_VAR) ||
sd_id128_equal(id, GPT_TMP) ||
sd_id128_in_set(id,
GPT_HOME,
GPT_SRV,
GPT_VAR,
GPT_TMP) ||
gpt_partition_type_is_root_verity(id) || /* pretty much implied, but let's set the bit to make things really clear */
gpt_partition_type_is_usr_verity(id); /* ditto */
}

View File

@ -18,6 +18,7 @@
***/
#include <inttypes.h>
#include <stdarg.h>
#include <string.h>
#include "_sd-common.h"
@ -119,6 +120,32 @@ _sd_pure_ static __inline__ int sd_id128_is_allf(sd_id128_t a) {
#define SD_ID128_NULL ((const sd_id128_t) { .qwords = { 0, 0 }})
#define SD_ID128_ALLF ((const sd_id128_t) { .qwords = { UINT64_C(0xFFFFFFFFFFFFFFFF), UINT64_C(0xFFFFFFFFFFFFFFFF) }})
_sd_pure_ static __inline__ int sd_id128_in_setv(sd_id128_t a, va_list ap) {
for (;;) {
sd_id128_t b = va_arg(ap, sd_id128_t);
if (sd_id128_is_null(b))
return 0;
if (sd_id128_equal(a, b))
return 1;
}
}
_sd_pure_ static __inline__ int sd_id128_in_set_sentinel(sd_id128_t a, ...) {
va_list ap;
int r;
va_start(ap, a);
r = sd_id128_in_setv(a, ap);
va_end(ap);
return r;
}
#define sd_id128_in_set(a, ...) \
sd_id128_in_set_sentinel(a, ##__VA_ARGS__, SD_ID128_NULL)
_SD_END_DECLARATIONS;
#endif

View File

@ -31,6 +31,13 @@ int main(int argc, char *argv[]) {
assert_se(sd_id128_from_string(t, &id2) == 0);
assert_se(sd_id128_equal(id, id2));
assert_se(sd_id128_in_set(id, id));
assert_se(sd_id128_in_set(id, id2));
assert_se(sd_id128_in_set(id, id2, id));
assert_se(sd_id128_in_set(id, ID128_WALDI, id));
assert_se(!sd_id128_in_set(id));
assert_se(!sd_id128_in_set(id, ID128_WALDI));
assert_se(!sd_id128_in_set(id, ID128_WALDI, ID128_WALDI));
if (sd_booted() > 0) {
assert_se(sd_id128_get_machine(&id) == 0);