* [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path
@ 2013-04-15 21:08 Stefan Berger
2013-04-15 21:13 ` Eric Blake
2013-04-16 18:16 ` Anthony Liguori
0 siblings, 2 replies; 4+ messages in thread
From: Stefan Berger @ 2013-04-15 21:08 UTC (permalink / raw)
To: qemu-devel@nongnu.org; +Cc: coreyb
Simplify the creation of the cancel path given the TPM's device path.
Given the path /dev/tpm0 build the path /sys/class/misc/tpm0/device/cancel.
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
---
tpm/tpm_passthrough.c | 74 +++++++++++---------------------------------------
1 file changed, 17 insertions(+), 57 deletions(-)
Index: qemu-git.pt/tpm/tpm_passthrough.c
===================================================================
--- qemu-git.pt.orig/tpm/tpm_passthrough.c
+++ qemu-git.pt/tpm/tpm_passthrough.c
@@ -336,45 +336,17 @@ static int tpm_passthrough_test_tpmdev(i
}
/*
- * Check whether the given base path, e.g., /sys/class/misc/tpm0/device,
- * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely
- * recognizable by the file entries 'pcrs' and 'cancel'.
- * Upon success 'true' is returned and the basebath buffer has '/cancel'
- * appended.
- */
-static bool tpm_passthrough_check_sysfs_cancel(char *basepath, size_t bufsz)
-{
- char path[PATH_MAX];
- struct stat statbuf;
-
- snprintf(path, sizeof(path), "%s/pcrs", basepath);
- if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) {
- return false;
- }
-
- snprintf(path, sizeof(path), "%s/cancel", basepath);
- if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) {
- return false;
- }
-
- strncpy(basepath, path, bufsz);
-
- return true;
-}
-
-/*
* Unless path or file descriptor set has been provided by user,
* determine the sysfs cancel file following kernel documentation
* in Documentation/ABI/stable/sysfs-class-tpm.
+ * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
*/
static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
{
+ TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
int fd = -1;
- unsigned int idx;
- DIR *pnp_dir;
+ char *dev;
char path[PATH_MAX];
- struct dirent entry, *result;
- int len;
if (tb->cancel_path) {
fd = qemu_open(tb->cancel_path, O_WRONLY);
@@ -385,34 +357,22 @@ static int tpm_passthrough_open_sysfs_ca
return fd;
}
- snprintf(path, sizeof(path), "/sys/class/misc");
- pnp_dir = opendir(path);
- if (pnp_dir != NULL) {
- while (readdir_r(pnp_dir, &entry, &result) == 0 &&
- result != NULL) {
- /*
- * only allow /sys/class/misc/tpm%u type of paths
- */
- if (sscanf(entry.d_name, "tpm%u%n", &idx, &len) < 1 ||
- len <= strlen("tpm") ||
- len != strlen(entry.d_name)) {
- continue;
- }
-
- snprintf(path, sizeof(path), "/sys/class/misc/%s/device",
- entry.d_name);
- if (!tpm_passthrough_check_sysfs_cancel(path, sizeof(path))) {
- continue;
- }
-
+ dev = strrchr(tpm_pt->tpm_dev, '/');
+ if (dev) {
+ dev++;
+ if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
+ dev) < sizeof(path)) {
fd = qemu_open(path, O_WRONLY);
- break;
+ if (fd >= 0) {
+ tb->cancel_path = g_strdup(path);
+ } else {
+ error_report("tpm_passthrough: Could not open TPM cancel "
+ "path %s : %s", path, strerror(errno));
+ }
}
- closedir(pnp_dir);
- }
-
- if (fd >= 0) {
- tb->cancel_path = g_strdup(path);
+ } else {
+ error_report("tpm_passthrough: Bad TPM device path %s",
+ tpm_pt->tpm_dev);
}
return fd;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path
2013-04-15 21:08 [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path Stefan Berger
@ 2013-04-15 21:13 ` Eric Blake
2013-04-16 18:16 ` Anthony Liguori
1 sibling, 0 replies; 4+ messages in thread
From: Eric Blake @ 2013-04-15 21:13 UTC (permalink / raw)
To: Stefan Berger; +Cc: coreyb, qemu-devel@nongnu.org
[-- Attachment #1: Type: text/plain, Size: 632 bytes --]
On 04/15/2013 03:08 PM, Stefan Berger wrote:
> Simplify the creation of the cancel path given the TPM's device path.
> Given the path /dev/tpm0 build the path /sys/class/misc/tpm0/device/cancel.
>
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
>
> ---
> tpm/tpm_passthrough.c | 74 +++++++++++---------------------------------------
> 1 file changed, 17 insertions(+), 57 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com>
and matches a counterpart change you proposed for libvirt.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path
2013-04-15 21:08 [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path Stefan Berger
2013-04-15 21:13 ` Eric Blake
@ 2013-04-16 18:16 ` Anthony Liguori
2013-04-18 17:38 ` Stefan Berger
1 sibling, 1 reply; 4+ messages in thread
From: Anthony Liguori @ 2013-04-16 18:16 UTC (permalink / raw)
To: Stefan Berger, qemu-devel@nongnu.org; +Cc: coreyb
Stefan Berger <stefanb@linux.vnet.ibm.com> writes:
> Simplify the creation of the cancel path given the TPM's device path.
> Given the path /dev/tpm0 build the path /sys/class/misc/tpm0/device/cancel.
>
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
No longer applies. Had you sent with git-send-patch, git am --3way
would have figured it out. Please use git-send-patch in the future.
Regards,
Anthony Liguori
>
> ---
> tpm/tpm_passthrough.c | 74 +++++++++++---------------------------------------
> 1 file changed, 17 insertions(+), 57 deletions(-)
>
> Index: qemu-git.pt/tpm/tpm_passthrough.c
> ===================================================================
> --- qemu-git.pt.orig/tpm/tpm_passthrough.c
> +++ qemu-git.pt/tpm/tpm_passthrough.c
> @@ -336,45 +336,17 @@ static int tpm_passthrough_test_tpmdev(i
> }
>
> /*
> - * Check whether the given base path, e.g., /sys/class/misc/tpm0/device,
> - * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely
> - * recognizable by the file entries 'pcrs' and 'cancel'.
> - * Upon success 'true' is returned and the basebath buffer has '/cancel'
> - * appended.
> - */
> -static bool tpm_passthrough_check_sysfs_cancel(char *basepath, size_t bufsz)
> -{
> - char path[PATH_MAX];
> - struct stat statbuf;
> -
> - snprintf(path, sizeof(path), "%s/pcrs", basepath);
> - if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) {
> - return false;
> - }
> -
> - snprintf(path, sizeof(path), "%s/cancel", basepath);
> - if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) {
> - return false;
> - }
> -
> - strncpy(basepath, path, bufsz);
> -
> - return true;
> -}
> -
> -/*
> * Unless path or file descriptor set has been provided by user,
> * determine the sysfs cancel file following kernel documentation
> * in Documentation/ABI/stable/sysfs-class-tpm.
> + * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
> */
> static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
> {
> + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
> int fd = -1;
> - unsigned int idx;
> - DIR *pnp_dir;
> + char *dev;
> char path[PATH_MAX];
> - struct dirent entry, *result;
> - int len;
>
> if (tb->cancel_path) {
> fd = qemu_open(tb->cancel_path, O_WRONLY);
> @@ -385,34 +357,22 @@ static int tpm_passthrough_open_sysfs_ca
> return fd;
> }
>
> - snprintf(path, sizeof(path), "/sys/class/misc");
> - pnp_dir = opendir(path);
> - if (pnp_dir != NULL) {
> - while (readdir_r(pnp_dir, &entry, &result) == 0 &&
> - result != NULL) {
> - /*
> - * only allow /sys/class/misc/tpm%u type of paths
> - */
> - if (sscanf(entry.d_name, "tpm%u%n", &idx, &len) < 1 ||
> - len <= strlen("tpm") ||
> - len != strlen(entry.d_name)) {
> - continue;
> - }
> -
> - snprintf(path, sizeof(path), "/sys/class/misc/%s/device",
> - entry.d_name);
> - if (!tpm_passthrough_check_sysfs_cancel(path, sizeof(path))) {
> - continue;
> - }
> -
> + dev = strrchr(tpm_pt->tpm_dev, '/');
> + if (dev) {
> + dev++;
> + if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
> + dev) < sizeof(path)) {
> fd = qemu_open(path, O_WRONLY);
> - break;
> + if (fd >= 0) {
> + tb->cancel_path = g_strdup(path);
> + } else {
> + error_report("tpm_passthrough: Could not open TPM cancel "
> + "path %s : %s", path, strerror(errno));
> + }
> }
> - closedir(pnp_dir);
> - }
> -
> - if (fd >= 0) {
> - tb->cancel_path = g_strdup(path);
> + } else {
> + error_report("tpm_passthrough: Bad TPM device path %s",
> + tpm_pt->tpm_dev);
> }
>
> return fd;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path
2013-04-16 18:16 ` Anthony Liguori
@ 2013-04-18 17:38 ` Stefan Berger
0 siblings, 0 replies; 4+ messages in thread
From: Stefan Berger @ 2013-04-18 17:38 UTC (permalink / raw)
To: Anthony Liguori; +Cc: coreyb, qemu-devel@nongnu.org
On 04/16/2013 02:16 PM, Anthony Liguori wrote:
> Stefan Berger <stefanb@linux.vnet.ibm.com> writes:
>
>> Simplify the creation of the cancel path given the TPM's device path.
>> Given the path /dev/tpm0 build the path /sys/class/misc/tpm0/device/cancel.
>>
>> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> No longer applies. Had you sent with git-send-patch, git am --3way
> would have figured it out. Please use git-send-patch in the future.
I reposted it.
http://lists.gnu.org/archive/html/qemu-devel/2013-04/msg03330.html
Stefan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-04-18 17:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-15 21:08 [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path Stefan Berger
2013-04-15 21:13 ` Eric Blake
2013-04-16 18:16 ` Anthony Liguori
2013-04-18 17:38 ` Stefan Berger
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).