On SMBIOS > 2.7 dmidecode outputs some comments before the serial number. This ignores them and trims off any trailing spaces/newlines. --- loader/urlinstall.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
diff --git a/loader/urlinstall.c b/loader/urlinstall.c index 93a4360..a51ec4e 100644 --- a/loader/urlinstall.c +++ b/loader/urlinstall.c @@ -95,26 +95,34 @@ static char **headers() { if (FL_KICKSTART_SEND_SERIAL(flags) && !access("/sbin/dmidecode", X_OK)) { FILE *f; char sn[1024]; - size_t sn_len; + char *p;
if ((f = popen("/sbin/dmidecode -s system-serial-number", "r")) == NULL) { logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); abort(); }
- sn_len = fread(sn, sizeof(char), 1023, f); - if (ferror(f)) { - logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); - abort(); + /* dmidecode output may be multiline and may include comments, get the + * first non-comment line. + */ + while(!feof(f)) { + p = fgets(sn, sizeof(sn), f); + if (p == NULL || ferror(f)) { + logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); + abort(); + } + if (sn[0] != '#') + break; } - - sn[sn_len] = '\0'; pclose(f);
- extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *)); + /* Trim off any trailing newlines or spaces */ + for (p=sn+strlen(sn); p != sn && !isspace(*p); p--); + *p = '\0';
+ logMessage(INFO, "Extra HTTP Header: X-System-Serial-Number: %s", sn); + extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *)); checked_asprintf(&extraHeaders[len], "X-System-Serial-Number: %s", sn); - len++; }
On 02/19/2014 02:20 PM, David Shea wrote:
On 02/19/2014 02:13 PM, Brian C. Lane wrote:
extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *));
/* Trim off any trailing newlines or spaces */for (p=sn+strlen(sn); p != sn && !isspace(*p); p--);*p = '\0';Extra semicolon at the end of the "for"
Looking at it again, I guess that may have been intentional if you're just trying to back p up to the first non-space. In that case, could you make the body of the loop a continue statement so it clear that's what you're doing? Also, it may make more sense to start with sn + strlen(sn) - 1, so you're not testing isspace('\0') which I had to try out to make sure it actually isn't a space (and change the conditional to p >= sn to handle the empty string case). Also, include <ctype.h> for isspace.
On SMBIOS > 2.7 dmidecode outputs some comments before the serial number. This ignores them and trims off any trailing spaces/newlines. --- loader/urlinstall.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
diff --git a/loader/urlinstall.c b/loader/urlinstall.c index 93a4360..d7f4b0c 100644 --- a/loader/urlinstall.c +++ b/loader/urlinstall.c @@ -95,26 +95,34 @@ static char **headers() { if (FL_KICKSTART_SEND_SERIAL(flags) && !access("/sbin/dmidecode", X_OK)) { FILE *f; char sn[1024]; - size_t sn_len; + char *p;
if ((f = popen("/sbin/dmidecode -s system-serial-number", "r")) == NULL) { logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); abort(); }
- sn_len = fread(sn, sizeof(char), 1023, f); - if (ferror(f)) { - logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); - abort(); + /* dmidecode output may be multiline and may include comments, get the + * first non-comment line. + */ + while(!feof(f)) { + p = fgets(sn, sizeof(sn), f); + if (p == NULL || ferror(f)) { + logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); + abort(); + } + if (sn[0] != '#') + break; } - - sn[sn_len] = '\0'; pclose(f);
+ p = sn+strlen(sn)-1; + if (*p = '\n') + *p = '\0'; + + logMessage(INFO, "Extra HTTP Header: X-System-Serial-Number: %s", sn); extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *)); - checked_asprintf(&extraHeaders[len], "X-System-Serial-Number: %s", sn); - len++; }
On 02/19/2014 02:51 PM, Brian C. Lane wrote:
p = sn+strlen(sn)-1;if (*p = '\n')*p = '\0';
Last nitpick, I promise: dmidecode returns an empty string on stdout on error, so we should still check for that here. Only check for *p == '\n' if strlen(sn) > 0 (or if p >= sn if you prefer) so we don't read before the start of sn.
On SMBIOS > 2.7 dmidecode outputs some comments before the serial number. This ignores them and trims off any trailing spaces/newlines. --- loader/urlinstall.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/loader/urlinstall.c b/loader/urlinstall.c index 93a4360..c2fc989 100644 --- a/loader/urlinstall.c +++ b/loader/urlinstall.c @@ -95,26 +95,31 @@ static char **headers() { if (FL_KICKSTART_SEND_SERIAL(flags) && !access("/sbin/dmidecode", X_OK)) { FILE *f; char sn[1024]; - size_t sn_len; + char *p;
if ((f = popen("/sbin/dmidecode -s system-serial-number", "r")) == NULL) { logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); abort(); }
- sn_len = fread(sn, sizeof(char), 1023, f); - if (ferror(f)) { - logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); - abort(); + /* dmidecode output may be multiline and may include comments, get the + * first non-comment line. + */ + while(!feof(f)) { + p = fgets(sn, sizeof(sn), f); + if (p == NULL || ferror(f)) { + logMessage(CRITICAL, "%s: %d: %m", __func__, __LINE__); + abort(); + } + if (sn[0] != '#') + break; } - - sn[sn_len] = '\0'; pclose(f); + g_strchomp(sn);
+ logMessage(INFO, "Extra HTTP Header: X-System-Serial-Number: %s", sn); extraHeaders = realloc(extraHeaders, (len+1)*sizeof(char *)); - checked_asprintf(&extraHeaders[len], "X-System-Serial-Number: %s", sn); - len++; }
On 02/19/2014 11:30 PM, Brian C. Lane wrote:
On SMBIOS > 2.7 dmidecode outputs some comments before the serial number. This ignores them and trims off any trailing spaces/newlines.
loader/urlinstall.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
Ack.
anaconda-patches@lists.fedorahosted.org