The library writes logs to stderr. Instead of rewriting the logging
framework to be able to redirect, this will redirect stderr
temporarily, and will swallow the error only if it's the type of error
we want to swallow.
Attached is a patch with a couple of more comments.
On 20 August 2014 13:14, Ken Goldman <[email protected]> wrote:
> There's evidently something clever going on with stderr. My sense is
> that it's something you've used before, so it's familiar to you. How
> about some comments on the clever parts, for future maintainers?
>
> On 8/14/2014 4:42 AM, Thomas Habets wrote:
>> My TPM chip got out of whack (I'm going to try a reboot), but in the
>> mean time here's a patch to make tpm_version not fail silently.
>
>
>
> ------------------------------------------------------------------------------
> Slashdot TV.
> Video for Nerds. Stuff that matters.
> http://tv.slashdot.org/
> _______________________________________________
> TrouSerS-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/trousers-users
--
typedef struct me_s {
char name[] = { "Thomas Habets" };
char email[] = { "[email protected]" };
char kernel[] = { "Linux" };
char *pgpKey[] = { "http://www.habets.pp.se/pubkey.txt" };
char pgp[] = { "A8A3 D1DD 4AE0 8467 7FDE 0945 286A E90A AD48 E854" };
char coolcmd[] = { "echo '. ./_&. ./_'>_;. ./_" };
} me_t;
commit 19fbf91d381506a3080341355aeb9d197d9ad546
Author: Thomas Habets <[email protected]>
Date: Wed Aug 20 15:27:35 2014 +0100
tpm_version: Don't suppress communication errors with TPM chip.
Signed-off-by: Thomas Habets <[email protected]>
diff --git a/src/tpm_mgmt/tpm_version.c b/src/tpm_mgmt/tpm_version.c
index 2378dba..e4a1fa8 100644
--- a/src/tpm_mgmt/tpm_version.c
+++ b/src/tpm_mgmt/tpm_version.c
@@ -18,14 +18,81 @@
* along with this program; if not, a copy can be viewed at
* http://www.opensource.org/licenses/cpl1.0.php.
*/
+#include <sys/stat.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h> //for def. exit
+#include <unistd.h>
#include "tpm_tspi.h"
TSS_HCONTEXT hContext = 0;
+// Redirect all stderr output to a temporary file.
+// Returns a cloned version of the original stderr, for use with end_capture_stderr().
+int capture_stderr()
+{
+ int olderr = dup(STDERR_FILENO);
+ if (olderr < 0) {
+ perror("dup(STDERR_FILENO)");
+ return -1;
+ }
+ FILE* f = tmpfile();
+ if (f == NULL) {
+ perror("tmpfile()");
+ return -1;
+ }
+ if (0 > dup2(fileno(f), STDERR_FILENO)) {
+ perror("dup2(, STDERR_FILENO)");
+ return -1;
+ }
+ fclose(f);
+ return olderr;
+}
+
+// Restore stderr and return a newly allocated buffer with the captured stderr output.
+// Caller frees the returned buffer.
+char* end_capture_stderr(int olderr)
+{
+ char* buf = NULL;
+ struct stat st;
+ int memsize;
+ if (olderr == -1) {
+ // capture_stderr() must have failed.
+ return strdup("");
+ }
+ if (0 > fstat(STDERR_FILENO, &st)) {
+ perror("fstat()");
+ goto errout;
+ }
+ if (0 > lseek(STDERR_FILENO, 0, SEEK_SET)) {
+ perror("lseek()");
+ goto errout;
+ }
+ memsize = st.st_size + 1;
+ if (memsize <= st.st_size) {
+ perror("int overflow");
+ memsize = 100;
+ st.st_size = memsize - 1;
+ }
+ if (!(buf = malloc(memsize))) {
+ perror("malloc()");
+ goto errout;
+ }
+ if (st.st_size != read(STDERR_FILENO, buf, st.st_size)) {
+ perror("read()");
+ }
+ errout:
+ if (0 > dup2(olderr, STDERR_FILENO)) {
+ perror("dup2()");
+ return NULL;
+ }
+ if (0 > close(olderr)) {
+ perror("close()");
+ }
+ return buf;
+}
+
int cmdVersion(const char *a_szCmd)
{
TSS_HTPM hTpm;
@@ -43,26 +110,34 @@ int cmdVersion(const char *a_szCmd)
if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS)
goto out_close;
-
#ifdef TSS_LIB_IS_12
{
UINT64 offset;
TSS_RESULT uiResult;
TPM_CAP_VERSION_INFO versionInfo;
- int tmpLogLevel = iLogLevel;
-
- /* disable logging during this call. If we're on a 1.1 TPM, it'd throw an error */
- iLogLevel = LOG_LEVEL_NONE;
-
- if ((uiResult = getCapability(hTpm, TSS_TPMCAP_VERSION_VAL, 0, NULL, &uiResultLen,
- &pResult)) != TSS_SUCCESS) {
- iLogLevel = tmpLogLevel;
- if (uiResult == TPM_E_BAD_MODE)
+ char buf[4096] = {0};
+ int olderr;
+
+ /* Disable logging to of "Bad Mode" during this call. If we're on a 1.1 TPM, it'd throw an error
+ * Save data sent to stderr in case it was any other type of error. */
+ olderr = capture_stderr(buf, sizeof(buf));
+ uiResult = getCapability(hTpm, TSS_TPMCAP_VERSION_VAL, 0, NULL, &uiResultLen,
+ &pResult);
+ char* errbuf = end_capture_stderr(olderr);
+
+ if (uiResult != TSS_SUCCESS) {
+ if (uiResult == TPM_E_BAD_MODE) {
+ free(errbuf);
goto print_cap_version;
- else
+ } else {
+ if (iLogLevel >= LOG_LEVEL_ERROR) {
+ fprintf(stderr, "%s", errbuf);
+ }
+ free(errbuf);
goto out_close;
+ }
}
- iLogLevel = tmpLogLevel;
+ free(errbuf);
offset = 0;
if ((uiResult = unloadVersionInfo(&offset, pResult, &versionInfo))) {
------------------------------------------------------------------------------
Slashdot TV.
Video for Nerds. Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
TrouSerS-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/trousers-users