From: Yu Watanabe Date: Thu, 12 Dec 2024 02:01:00 +0000 (+0900) Subject: tpm2-util: allow to control if legend and/or footer shown by tpm2_list_devices() X-Git-Tag: v258-rc1~1887^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=66d2c693acb820467a7cc92fa0a5ef2aade2d9e4;p=thirdparty%2Fsystemd.git tpm2-util: allow to control if legend and/or footer shown by tpm2_list_devices() --- diff --git a/src/creds/creds.c b/src/creds/creds.c index 7635aee37ca..866fcd63671 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -911,7 +911,7 @@ static int parse_argv(int argc, char *argv[]) { case ARG_TPM2_DEVICE: if (streq(optarg, "list")) - return tpm2_list_devices(); + return tpm2_list_devices(arg_legend, arg_quiet); arg_tpm2_device = streq(optarg, "auto") ? NULL : optarg; break; diff --git a/src/cryptenroll/cryptenroll.c b/src/cryptenroll/cryptenroll.c index 3fb58c2874b..a3ca50fd0d6 100644 --- a/src/cryptenroll/cryptenroll.c +++ b/src/cryptenroll/cryptenroll.c @@ -493,7 +493,7 @@ static int parse_argv(int argc, char *argv[]) { _cleanup_free_ char *device = NULL; if (streq(optarg, "list")) - return tpm2_list_devices(); + return tpm2_list_devices(/* legend = */ true, /* quiet = */ false); if (arg_enroll_type >= 0 || arg_tpm2_device) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), diff --git a/src/measure/measure.c b/src/measure/measure.c index e583444e0bf..22dd1831253 100644 --- a/src/measure/measure.c +++ b/src/measure/measure.c @@ -295,7 +295,7 @@ static int parse_argv(int argc, char *argv[]) { _cleanup_free_ char *device = NULL; if (streq(optarg, "list")) - return tpm2_list_devices(); + return tpm2_list_devices(/* legend = */ true, /* quiet = */ false); if (!streq(optarg, "auto")) { device = strdup(optarg); diff --git a/src/pcrextend/pcrextend.c b/src/pcrextend/pcrextend.c index 6bdb5418b53..ef4d1a1f178 100644 --- a/src/pcrextend/pcrextend.c +++ b/src/pcrextend/pcrextend.c @@ -131,7 +131,7 @@ static int parse_argv(int argc, char *argv[]) { _cleanup_free_ char *device = NULL; if (streq(optarg, "list")) - return tpm2_list_devices(); + return tpm2_list_devices(/* legend = */ true, /* quiet = */ false); if (!streq(optarg, "auto")) { device = strdup(optarg); diff --git a/src/repart/repart.c b/src/repart/repart.c index 7e6fd2a29a4..e485cd5231d 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -8160,7 +8160,7 @@ static int parse_argv(int argc, char *argv[], X509 **ret_certificate, EVP_PKEY * _cleanup_free_ char *device = NULL; if (streq(optarg, "list")) - return tpm2_list_devices(); + return tpm2_list_devices(/* legend = */ true, /* quiet = */ false); if (!streq(optarg, "auto")) { device = strdup(optarg); diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 36a0f906daa..252136af3e1 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -6165,7 +6165,7 @@ int tpm2_unseal_data( } #endif /* HAVE_TPM2 */ -int tpm2_list_devices(void) { +int tpm2_list_devices(bool legend, bool quiet) { #if HAVE_TPM2 _cleanup_(table_unrefp) Table *t = NULL; _cleanup_closedir_ DIR *d = NULL; @@ -6179,6 +6179,8 @@ int tpm2_list_devices(void) { if (!t) return log_oom(); + (void) table_set_header(t, legend); + d = opendir("/sys/class/tpmrm"); if (!d) { log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to open /sys/class/tpmrm: %m"); @@ -6224,7 +6226,7 @@ int tpm2_list_devices(void) { } } - if (table_isempty(t)) { + if (table_isempty(t) && !quiet) { log_info("No suitable TPM2 devices found."); return 0; } diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h index 77cd7dbcaf7..76b4dd3cc15 100644 --- a/src/shared/tpm2-util.h +++ b/src/shared/tpm2-util.h @@ -385,7 +385,7 @@ static inline int tpm2_pcrlock_search_file(const char *path, FILE **ret_file, ch #endif /* HAVE_TPM2 */ -int tpm2_list_devices(void); +int tpm2_list_devices(bool legend, bool quiet); int tpm2_find_device_auto(char **ret); int tpm2_make_pcr_json_array(uint32_t pcr_mask, sd_json_variant **ret); diff --git a/src/tpm2-setup/tpm2-setup.c b/src/tpm2-setup/tpm2-setup.c index ee9d243d5ee..ab5bd9bff9d 100644 --- a/src/tpm2-setup/tpm2-setup.c +++ b/src/tpm2-setup/tpm2-setup.c @@ -91,7 +91,7 @@ static int parse_argv(int argc, char *argv[]) { case ARG_TPM2_DEVICE: if (streq(optarg, "list")) - return tpm2_list_devices(); + return tpm2_list_devices(/* legend = */ true, /* quiet = */ false); if (free_and_strdup(&arg_tpm2_device, streq(optarg, "auto") ? NULL : optarg) < 0) return log_oom();