sysinfo.c 10.7 KB
Newer Older
1 2 3
/*
 * sysinfo.c: get SMBIOS/sysinfo information from the host
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * Copyright (C) 2010 Daniel Veillard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel Veillard <veillard@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "virterror_internal.h"
#include "sysinfo.h"
#include "util.h"
#include "conf/domain_conf.h"
#include "logging.h"
#include "memory.h"
E
Eric Blake 已提交
39
#include "command.h"
40 41 42

#define VIR_FROM_THIS VIR_FROM_SYSINFO

E
Eric Blake 已提交
43
#define virSmbiosReportError(code, ...)                               \
44
    virReportErrorHelper(VIR_FROM_SYSINFO, code, __FILE__,            \
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
                         __FUNCTION__, __LINE__, __VA_ARGS__)

#define SYSINFO_SMBIOS_DECODER "dmidecode"

/**
 * virSysinfoDefFree:
 * @def: a sysinfo structure
 *
 * Free up the sysinfo structure
 */

void virSysinfoDefFree(virSysinfoDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->bios_vendor);
    VIR_FREE(def->bios_version);
    VIR_FREE(def->bios_date);
    VIR_FREE(def->bios_release);

    VIR_FREE(def->system_manufacturer);
    VIR_FREE(def->system_product);
    VIR_FREE(def->system_version);
    VIR_FREE(def->system_serial);
    VIR_FREE(def->system_uuid);
    VIR_FREE(def->system_sku);
E
Eric Blake 已提交
72
    VIR_FREE(def->system_family);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    VIR_FREE(def);
}

/**
 * virSysinfoRead:
 *
 * Tries to read the SMBIOS information from the current host
 *
 * Returns: a filled up sysinfo structure or NULL in case of error
 */
#ifdef WIN32
virSysinfoDefPtr
virSysinfoRead(void) {
    /*
     * this can probably be extracted from Windows using API or registry
     * http://www.microsoft.com/whdc/system/platform/firmware/SMBIOS.mspx
     */
    virReportSystemError(ENOSYS, "%s",
                 _("Host sysinfo extraction not supported on this platform"));
E
Eric Blake 已提交
92
    return NULL;
93
}
E
Eric Blake 已提交
94 95 96 97 98 99 100 101 102 103 104 105

char *
virSysinfoFormat(virSysinfoDefPtr def ATTRIBUTE_UNUSED,
                 const char *prefix ATTRIBUTE_UNUSED)
{
    virReportSystemError(ENOSYS, "%s",
                 _("Host sysinfo extraction not supported on this platform"));
    return NULL;
}

#else /* !WIN32 */

106 107 108 109 110
virSysinfoDefPtr
virSysinfoRead(void) {
    char *path, *cur, *eol, *base;
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;
E
Eric Blake 已提交
111
    virCommandPtr cmd;
112 113 114 115

    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
    if (path == NULL) {
        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
116
                             _("Failed to find path for %s binary"),
117
                             SYSINFO_SMBIOS_DECODER);
E
Eric Blake 已提交
118
        return NULL;
119 120
    }

E
Eric Blake 已提交
121 122 123 124
    cmd = virCommandNewArgList(path, "-q", "-t", "0,1", NULL);
    VIR_FREE(path);
    virCommandSetOutputBuffer(cmd, &outbuf);
    if (virCommandRun(cmd, NULL) < 0) {
125
        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
126
                             _("Failed to execute command %s"),
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                             path);
        goto cleanup;
    }

    if (VIR_ALLOC(ret) < 0)
        goto no_memory;

    ret->type = VIR_DOMAIN_SYSINFO_SMBIOS;

    base = outbuf;

    if ((cur = strstr(base, "Vendor: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_vendor = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
142
            goto no_memory;
143 144 145 146 147
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_version = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
148
            goto no_memory;
149 150 151 152 153
    }
    if ((cur = strstr(base, "Release Date: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_date = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
154
            goto no_memory;
155 156 157 158 159
    }
    if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_release = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
160
            goto no_memory;
161 162 163 164 165 166 167 168
    }
    if ((base = strstr(outbuf, "System Information")) == NULL)
        goto cleanup;
    if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
        if ((eol) &&
            ((ret->system_manufacturer = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
169
            goto no_memory;
170 171 172 173 174
    }
    if ((cur = strstr(base, "Product Name: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_product = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
175
            goto no_memory;
176 177 178 179 180
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_version = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
181
            goto no_memory;
182 183 184 185 186
    }
    if ((cur = strstr(base, "Serial Number: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_serial = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
187
            goto no_memory;
188 189 190 191 192
    }
    if ((cur = strstr(base, "UUID: ")) != NULL) {
        cur += 6;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_uuid = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
193
            goto no_memory;
194 195 196 197 198
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_sku = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
199
            goto no_memory;
200
    }
E
Eric Blake 已提交
201 202 203 204
    if ((cur = strstr(base, "Family: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_family = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
205
            goto no_memory;
E
Eric Blake 已提交
206
    }
207 208 209

cleanup:
    VIR_FREE(outbuf);
E
Eric Blake 已提交
210
    virCommandFree(cmd);
211

E
Eric Blake 已提交
212
    return ret;
213 214 215 216 217 218 219 220

no_memory:
    virReportOOMError();

    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
E
Eric Blake 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

/**
 * virSysinfoFormat:
 * @def: structure to convert to xml string
 * @prefix: string to prefix before each line of xml
 *
 * This returns the XML description of the sysinfo, or NULL after
 * generating an error message.
 */
char *
virSysinfoFormat(virSysinfoDefPtr def, const char *prefix)
{
    const char *type = virDomainSysinfoTypeToString(def->type);
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    size_t len = strlen(prefix);

    if (!type) {
        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
                             _("unexpected sysinfo type model %d"),
                             def->type);
        return NULL;
    }

244
    virBufferAsprintf(&buf, "%s<sysinfo type='%s'>\n", prefix, type);
E
Eric Blake 已提交
245 246
    if ((def->bios_vendor != NULL) || (def->bios_version != NULL) ||
        (def->bios_date != NULL) || (def->bios_release != NULL)) {
247
        virBufferAsprintf(&buf, "%s  <bios>\n", prefix);
E
Eric Blake 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if (def->bios_vendor != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='vendor'>%s</entry>\n",
                                  def->bios_vendor);
        }
        if (def->bios_version != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='version'>%s</entry>\n",
                                  def->bios_version);
        }
        if (def->bios_date != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='date'>%s</entry>\n",
                                  def->bios_date);
        }
        if (def->bios_release != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='release'>%s</entry>\n",
                                  def->bios_release);
        }
272
        virBufferAsprintf(&buf, "%s  </bios>\n", prefix);
E
Eric Blake 已提交
273 274 275 276 277
    }
    if ((def->system_manufacturer != NULL) || (def->system_product != NULL) ||
        (def->system_version != NULL) || (def->system_serial != NULL) ||
        (def->system_uuid != NULL) || (def->system_sku != NULL) ||
        (def->system_family != NULL)) {
278
        virBufferAsprintf(&buf, "%s  <system>\n", prefix);
E
Eric Blake 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        if (def->system_manufacturer != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='manufacturer'>%s</entry>\n",
                                  def->system_manufacturer);
        }
        if (def->system_product != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='product'>%s</entry>\n",
                                  def->system_product);
        }
        if (def->system_version != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='version'>%s</entry>\n",
                                  def->system_version);
        }
        if (def->system_serial != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='serial'>%s</entry>\n",
                                  def->system_serial);
        }
        if (def->system_uuid != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='uuid'>%s</entry>\n",
                                  def->system_uuid);
        }
        if (def->system_sku != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='sku'>%s</entry>\n",
                                  def->system_sku);
        }
        if (def->system_family != NULL) {
            virBufferAdd(&buf, prefix, len);
            virBufferEscapeString(&buf,
                                  "    <entry name='family'>%s</entry>\n",
                                  def->system_family);
        }
321
        virBufferAsprintf(&buf, "%s  </system>\n", prefix);
E
Eric Blake 已提交
322 323
    }

324
    virBufferAsprintf(&buf, "%s</sysinfo>\n", prefix);
E
Eric Blake 已提交
325 326 327 328 329

    return virBufferContentAndReset(&buf);
}

#endif /* !WIN32 */