2007-02-26 15:32:27 +00:00
|
|
|
/*
|
2010-05-19 10:52:23 +00:00
|
|
|
* Copyright (C) 2007, 2008, 2009, 2010 Red Hat, Inc.
|
2007-02-26 15:32:27 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Mark McLoughlin <markmc@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-02-26 15:32:27 +00:00
|
|
|
|
|
|
|
#include "uuid.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2008-11-04 23:22:06 +00:00
|
|
|
#include <stdio.h>
|
2007-02-26 15:32:27 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
start using c-ctype functions
Up to now, we've been avoiding ctype functions like isspace, isdigit,
etc. because they are locale-dependent. Now that we have the c-ctype
functions, we can start using *them*, to make the code more readable
with changes like these:
- /* This may not work on EBCDIC. */
- if ((*p >= 'a' && *p <= 'z') ||
- (*p >= 'A' && *p <= 'Z') ||
- (*p >= '0' && *p <= '9'))
+ if (c_isalnum(*p))
- while ((*cur >= '0') && (*cur <= '9')) {
+ while (c_isdigit(*cur)) {
Also, some macros in conf.c used names that conflicted with
standard meaning of "BLANK" and "SPACE", so I've adjusted them
to be in line with the definition of e.g., isblank.
In addition, I've wrapped those statement macros with do {...} while (0),
so that we can't forget the ";" after a use. There was one like that
already (fixed below). The missing semicolon would mess up automatic
indenting.
* src/buf.c (virBufferURIEncodeString):
* src/conf.c (IS_EOL, SKIP_BLANKS_AND_EOL, SKIP_BLANKS)
(virConfParseLong, virConfParseValue, virConfParseName)
(virConfParseSeparator, virConfParseStatement, IS_BLANK, IS_CHAR)
(IS_DIGIT, IS_SPACE, SKIP_SPACES):
* src/nodeinfo.c:
* src/qemu_conf.c (qemudParseInterfaceXML):
* src/qemu_driver.c (qemudDomainBlockStats):
* src/sexpr.c:
* src/stats_linux.c:
* src/util.c (virParseNumber, virDiskNameToIndex):
* src/uuid.c (hextobin, virUUIDParse):
* src/virsh.c:
* src/xml.c (parseCpuNumber, virParseCpuSet):
2008-05-16 09:37:44 +00:00
|
|
|
#include "c-ctype.h"
|
2007-02-26 15:32:27 +00:00
|
|
|
#include "internal.h"
|
2009-01-22 20:27:01 +00:00
|
|
|
#include "util.h"
|
2009-02-05 16:28:30 +00:00
|
|
|
#include "virterror_internal.h"
|
2009-03-03 11:40:08 +00:00
|
|
|
#include "logging.h"
|
2010-05-25 14:33:51 +00:00
|
|
|
#include "memory.h"
|
2010-11-09 20:48:48 +00:00
|
|
|
#include "files.h"
|
2007-06-26 23:48:46 +00:00
|
|
|
|
2007-12-07 14:56:37 +00:00
|
|
|
#ifndef ENODATA
|
2010-03-09 18:22:22 +00:00
|
|
|
# define ENODATA EIO
|
2007-12-07 14:56:37 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-25 14:33:51 +00:00
|
|
|
static unsigned char host_uuid[VIR_UUID_BUFLEN];
|
|
|
|
|
2007-02-26 15:32:27 +00:00
|
|
|
static int
|
2007-06-26 22:19:38 +00:00
|
|
|
virUUIDGenerateRandomBytes(unsigned char *buf,
|
|
|
|
int buflen)
|
2007-02-26 15:32:27 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
while (buflen > 0) {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if ((n = read(fd, buf, buflen)) <= 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2010-11-09 20:48:48 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2007-02-26 15:32:27 +00:00
|
|
|
return n < 0 ? errno : ENODATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf += n;
|
|
|
|
buflen -= n;
|
|
|
|
}
|
|
|
|
|
2010-11-09 20:48:48 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2007-02-26 15:32:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-06-26 22:19:38 +00:00
|
|
|
virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
|
|
|
|
int buflen)
|
2007-02-26 15:32:27 +00:00
|
|
|
{
|
|
|
|
while (buflen > 0) {
|
2009-01-22 20:27:01 +00:00
|
|
|
*buf = virRandom(256);
|
2007-02-26 15:32:27 +00:00
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* virUUIDGenerate:
|
2007-08-09 20:19:12 +00:00
|
|
|
* @uuid: array of VIR_UUID_BUFLEN bytes to store the new UUID
|
2007-06-29 13:23:13 +00:00
|
|
|
*
|
|
|
|
* Generates a randomized unique identifier.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure
|
|
|
|
*/
|
2007-02-26 15:32:27 +00:00
|
|
|
int
|
2007-06-26 22:19:38 +00:00
|
|
|
virUUIDGenerate(unsigned char *uuid)
|
2007-02-26 15:32:27 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
if (uuid == NULL)
|
|
|
|
return(-1);
|
|
|
|
|
2009-02-05 16:28:30 +00:00
|
|
|
if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
|
|
|
|
char ebuf[1024];
|
2010-05-19 10:52:23 +00:00
|
|
|
VIR_WARN("Falling back to pseudorandom UUID,"
|
|
|
|
" failed to generate random bytes: %s",
|
2009-02-05 16:28:30 +00:00
|
|
|
virStrerror(err, ebuf, sizeof ebuf));
|
2010-01-11 09:05:38 +00:00
|
|
|
err = virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
|
2009-02-05 16:28:30 +00:00
|
|
|
}
|
2007-02-26 15:32:27 +00:00
|
|
|
|
2010-01-11 09:05:38 +00:00
|
|
|
return(err);
|
2007-02-26 15:32:27 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* virUUIDParse:
|
2007-08-09 20:19:12 +00:00
|
|
|
* @uuidstr: zero terminated string representation of the UUID
|
|
|
|
* @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
|
2007-06-29 13:23:13 +00:00
|
|
|
*
|
|
|
|
* Parses the external string representation, allowing spaces and '-'
|
|
|
|
* character in the sequence, and storing the result as a raw UUID
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of error.
|
|
|
|
*/
|
2007-02-26 15:34:24 +00:00
|
|
|
int
|
2007-08-09 20:19:12 +00:00
|
|
|
virUUIDParse(const char *uuidstr, unsigned char *uuid) {
|
2007-02-26 15:34:24 +00:00
|
|
|
const char *cur;
|
|
|
|
int i;
|
|
|
|
|
2007-08-09 20:19:12 +00:00
|
|
|
if ((uuidstr == NULL) || (uuid == NULL))
|
2007-06-29 13:23:13 +00:00
|
|
|
return(-1);
|
|
|
|
|
2007-02-26 15:34:24 +00:00
|
|
|
/*
|
|
|
|
* do a liberal scan allowing '-' and ' ' anywhere between character
|
2010-01-21 14:28:56 +00:00
|
|
|
* pairs, and surrounding whitespace, as long as there are exactly
|
|
|
|
* 32 hexadecimal digits the end.
|
2007-02-26 15:34:24 +00:00
|
|
|
*/
|
2007-08-09 20:19:12 +00:00
|
|
|
cur = uuidstr;
|
2010-01-21 14:28:56 +00:00
|
|
|
while (c_isspace(*cur))
|
|
|
|
cur++;
|
|
|
|
|
2007-08-09 20:19:12 +00:00
|
|
|
for (i = 0;i < VIR_UUID_BUFLEN;) {
|
|
|
|
uuid[i] = 0;
|
2007-02-26 15:34:24 +00:00
|
|
|
if (*cur == 0)
|
|
|
|
goto error;
|
|
|
|
if ((*cur == '-') || (*cur == ' ')) {
|
|
|
|
cur++;
|
|
|
|
continue;
|
|
|
|
}
|
start using c-ctype functions
Up to now, we've been avoiding ctype functions like isspace, isdigit,
etc. because they are locale-dependent. Now that we have the c-ctype
functions, we can start using *them*, to make the code more readable
with changes like these:
- /* This may not work on EBCDIC. */
- if ((*p >= 'a' && *p <= 'z') ||
- (*p >= 'A' && *p <= 'Z') ||
- (*p >= '0' && *p <= '9'))
+ if (c_isalnum(*p))
- while ((*cur >= '0') && (*cur <= '9')) {
+ while (c_isdigit(*cur)) {
Also, some macros in conf.c used names that conflicted with
standard meaning of "BLANK" and "SPACE", so I've adjusted them
to be in line with the definition of e.g., isblank.
In addition, I've wrapped those statement macros with do {...} while (0),
so that we can't forget the ";" after a use. There was one like that
already (fixed below). The missing semicolon would mess up automatic
indenting.
* src/buf.c (virBufferURIEncodeString):
* src/conf.c (IS_EOL, SKIP_BLANKS_AND_EOL, SKIP_BLANKS)
(virConfParseLong, virConfParseValue, virConfParseName)
(virConfParseSeparator, virConfParseStatement, IS_BLANK, IS_CHAR)
(IS_DIGIT, IS_SPACE, SKIP_SPACES):
* src/nodeinfo.c:
* src/qemu_conf.c (qemudParseInterfaceXML):
* src/qemu_driver.c (qemudDomainBlockStats):
* src/sexpr.c:
* src/stats_linux.c:
* src/util.c (virParseNumber, virDiskNameToIndex):
* src/uuid.c (hextobin, virUUIDParse):
* src/virsh.c:
* src/xml.c (parseCpuNumber, virParseCpuSet):
2008-05-16 09:37:44 +00:00
|
|
|
if (!c_isxdigit(*cur))
|
2007-02-26 15:34:24 +00:00
|
|
|
goto error;
|
2010-08-27 21:13:45 +00:00
|
|
|
uuid[i] = virHexToBin(*cur);
|
2007-08-09 20:19:12 +00:00
|
|
|
uuid[i] *= 16;
|
2007-02-26 15:34:24 +00:00
|
|
|
cur++;
|
|
|
|
if (*cur == 0)
|
|
|
|
goto error;
|
start using c-ctype functions
Up to now, we've been avoiding ctype functions like isspace, isdigit,
etc. because they are locale-dependent. Now that we have the c-ctype
functions, we can start using *them*, to make the code more readable
with changes like these:
- /* This may not work on EBCDIC. */
- if ((*p >= 'a' && *p <= 'z') ||
- (*p >= 'A' && *p <= 'Z') ||
- (*p >= '0' && *p <= '9'))
+ if (c_isalnum(*p))
- while ((*cur >= '0') && (*cur <= '9')) {
+ while (c_isdigit(*cur)) {
Also, some macros in conf.c used names that conflicted with
standard meaning of "BLANK" and "SPACE", so I've adjusted them
to be in line with the definition of e.g., isblank.
In addition, I've wrapped those statement macros with do {...} while (0),
so that we can't forget the ";" after a use. There was one like that
already (fixed below). The missing semicolon would mess up automatic
indenting.
* src/buf.c (virBufferURIEncodeString):
* src/conf.c (IS_EOL, SKIP_BLANKS_AND_EOL, SKIP_BLANKS)
(virConfParseLong, virConfParseValue, virConfParseName)
(virConfParseSeparator, virConfParseStatement, IS_BLANK, IS_CHAR)
(IS_DIGIT, IS_SPACE, SKIP_SPACES):
* src/nodeinfo.c:
* src/qemu_conf.c (qemudParseInterfaceXML):
* src/qemu_driver.c (qemudDomainBlockStats):
* src/sexpr.c:
* src/stats_linux.c:
* src/util.c (virParseNumber, virDiskNameToIndex):
* src/uuid.c (hextobin, virUUIDParse):
* src/virsh.c:
* src/xml.c (parseCpuNumber, virParseCpuSet):
2008-05-16 09:37:44 +00:00
|
|
|
if (!c_isxdigit(*cur))
|
2007-02-26 15:34:24 +00:00
|
|
|
goto error;
|
2010-08-27 21:13:45 +00:00
|
|
|
uuid[i] += virHexToBin(*cur);
|
2007-02-26 15:34:24 +00:00
|
|
|
i++;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
2010-01-21 14:28:56 +00:00
|
|
|
while (*cur) {
|
|
|
|
if (!c_isspace(*cur))
|
|
|
|
goto error;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
2007-02-26 15:34:24 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-08-09 20:19:12 +00:00
|
|
|
/**
|
|
|
|
* virUUIDFormat:
|
|
|
|
* @uuid: array of VIR_UUID_RAW_LEN bytes to store the raw UUID
|
|
|
|
* @uuidstr: array of VIR_UUID_STRING_BUFLEN bytes to store the
|
|
|
|
* string representation of the UUID in. The resulting string
|
|
|
|
* will be NULL terminated.
|
|
|
|
*
|
|
|
|
* Converts the raw UUID into printable format, with embedded '-'
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of error.
|
|
|
|
*/
|
|
|
|
void virUUIDFormat(const unsigned char *uuid, char *uuidstr)
|
|
|
|
{
|
|
|
|
snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
|
|
|
|
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
uuid[0], uuid[1], uuid[2], uuid[3],
|
|
|
|
uuid[4], uuid[5], uuid[6], uuid[7],
|
|
|
|
uuid[8], uuid[9], uuid[10], uuid[11],
|
|
|
|
uuid[12], uuid[13], uuid[14], uuid[15]);
|
|
|
|
uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';
|
|
|
|
}
|
2010-05-25 14:33:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virUUIDIsValid
|
|
|
|
*
|
|
|
|
* @uuid: The UUID to test
|
|
|
|
*
|
|
|
|
* Do some basic tests to check whether the given UUID is
|
|
|
|
* valid as a host UUID.
|
|
|
|
* Basic tests:
|
|
|
|
* - Not all of the digits may be equal
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virUUIDIsValid(unsigned char *uuid)
|
|
|
|
{
|
|
|
|
unsigned int i, ctr = 1;
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
if (!uuid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
c = uuid[0];
|
|
|
|
|
|
|
|
for (i = 1; i < VIR_UUID_BUFLEN; i++)
|
|
|
|
if (uuid[i] == c)
|
|
|
|
ctr++;
|
|
|
|
|
|
|
|
return (ctr != VIR_UUID_BUFLEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getDMISystemUUID(char *uuid, int len)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
const char *paths[] = {
|
|
|
|
"/sys/devices/virtual/dmi/id/product_uuid",
|
|
|
|
"/sys/class/dmi/id/product_uuid",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
while (paths[i]) {
|
|
|
|
int fd = open(paths[i], O_RDONLY);
|
|
|
|
if (fd > 0) {
|
|
|
|
if (saferead(fd, uuid, len) == len) {
|
2010-11-09 20:48:48 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2010-05-25 14:33:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-11-09 20:48:48 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2010-05-25 14:33:51 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setHostUUID
|
|
|
|
*
|
|
|
|
* @host_uuid: UUID that the host is supposed to have
|
|
|
|
*
|
|
|
|
* Set the UUID of the host if it hasn't been set, yet
|
|
|
|
* Returns 0 in case of success, an error code in case of error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virSetHostUUIDStr(const char *uuid)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char dmiuuid[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
|
|
|
if (virUUIDIsValid(host_uuid))
|
|
|
|
return EEXIST;
|
|
|
|
|
|
|
|
if (!uuid) {
|
2010-07-30 14:19:51 +00:00
|
|
|
memset(dmiuuid, 0, sizeof(dmiuuid));
|
|
|
|
if (!getDMISystemUUID(dmiuuid, sizeof(dmiuuid) - 1)) {
|
2010-05-25 14:33:51 +00:00
|
|
|
if (!virUUIDParse(dmiuuid, host_uuid))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virUUIDIsValid(host_uuid))
|
|
|
|
return virUUIDGenerate(host_uuid);
|
|
|
|
} else {
|
|
|
|
rc = virUUIDParse(uuid, host_uuid);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
if (!virUUIDIsValid(host_uuid))
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getHostUUID:
|
|
|
|
*
|
|
|
|
* @host_uuid: memory to store the host_uuid into
|
|
|
|
*
|
|
|
|
* Get the UUID of the host. Returns 0 in case of success,
|
|
|
|
* an error code otherwise.
|
|
|
|
* Returns 0 in case of success, an error code in case of error.
|
|
|
|
*/
|
|
|
|
int virGetHostUUID(unsigned char *uuid)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!virUUIDIsValid(host_uuid))
|
|
|
|
ret = virSetHostUUIDStr(NULL);
|
|
|
|
|
|
|
|
memcpy(uuid, host_uuid, sizeof(host_uuid));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|