~serge-hallyn/ubuntu/raring/libvirt/libvirt-hugepages

« back to all changes in this revision

Viewing changes to src/uuid.c

  • Committer: James Westby
  • Author(s): Jamie Strandboge
  • Date: 2009-12-02 14:22:21 UTC
  • mfrom: (1.2.3 upstream) (3.4.9 squeeze)
  • Revision ID: james.westby@canonical.com-20091202142221-ltkr0to6h52mla1y
Tags: 0.7.2-4ubuntu1
* Merge from debian testing. Remaining changes:
  - debian/control:
    + Don't build-depend on QEmu
    + Bump bridge-utils, dnsmasq-base, netcat-openbsd, and iptables
      to Depends of libvirt-bin
    + Recommends qemu-kvm (>= 0.11.0-0ubuntu6)
    + Add versioned Conflicts/Replaces to libvirt0 for libvirt0-dbg,
      since we used to ship them as such
    + We call libxen-dev libxen3-dev, so change all references
    + Build-Depends on libxml2-utils
    + Build-Depends on open-iscsi-utils instead of open-iscsi due to
      LP: #414986
  - debian/postinst:
    + rename the libvirt group to libvirtd
    + add each admin user to the libvirtd group
  - debian/libvirt-bin.postrm: rename the libvirt group to libvirtd
  - debian/rules: add DEB_MAKE_CHECK_TARGET := check
  - debian/patches/900[0-7]: updated/refreshed for new paths in 0.7.2
  - debian/patches/series: don't apply 0002-qemu-disable-network.diff.patch
  - AppArmor integration:
    + debian/control: Build-Depends on libapparmor-dev and Suggests
      apparmor (>= 2.3+1289-0ubuntu14)
    + debian/libvirt-bin.dirs: add /etc/apparmor.d/abstractions,
      /etc/apparmor.d/force-complain, /etc/apparmor.d/libvirt,
      /etc/cron.daily and /usr/share/apport/package-hooks
    + add debian/libvirt-bin.cron.daily (LP: #438165)
    + add debian/libvirt-bin.apport
    + debian/libvirt-bin.install: install apparmor profiles, abstractions
      and apport hook
    + debian/postinst: reload apparmor profiles
    + debian/libvirt-bin.postrm: remove apparmor symlinks on purge
    + debian/libvirt-bin.preinst: added to force complain on certain
      upgrades
    + debian/README.Debian: add AppArmor section based on the upstream
      documentation
    + debian/rules: use --with-apparmor and copy apparmor and apport hook to
      debian/tmp
  - Dropped the following patches now included upstream:
    + 0005-Close-logfile-fd-after-spawning-qemu.patch
    + 9090-reenable-nonfile-labels.patch
    + 9091-apparmor.patch
    + 9092-apparmor-autoreconf.patch
* AppArmor integration updates:
  - debian/apparmor/usr.sbin.libvirtd: allow libvirtd access to
    /usr/lib/libvirt/* (LP: #480478)
  - debian/apparmor/libvirt-qemu: allow guests access to
    /etc/pki/libvirt-vnc/** (LP: #484562)
  - debian/libvirt-bin.postinst: 0.7.2 moved /usr/bin/virt-aa-helper to
    /usr/lib/libvirt, so the profile changed from usr.bin.virt-aa-helper
    to usr.lib.libvirt.virt-aa-helper and needs to be migrated. If the user
    made no changes to the old profile, remove it, otherwise, update the
    paths, preserving the shipped usr.lib.libvirt.virt-aa-helper
  - update to 0.7.4 version of the sVirt AppArmor driver (can be dropped in
    0.7.4):
    + debian/patches/9008-apparmor-caps-mockup.patch
    + debian/patches/9009-apparmor-lp453335.patch
    + debian/patches/9010-apparmor-lp460271.patch
    + debian/patches/9011-apparmor-code-cleanups.patch
  - add virt-aa-helper-test and examples/apparmor that were omitted from the
    upstream tarball (can be dropped in 0.7.5):
    + debian/patches/9012-apparmor-add-virt-aa-helper-test.patch
    + debian/patches/9013-apparmor-examples.patch
    + debian/rules: add post-patches target to make virt-aa-helper-test
      executable
* debian/patches/0005-Fix-SELinux-linking-issues.patch: updated to work
  when both apparmor and selinux are available. This patch should be
  dropped in 0.7.4.
* debian/patches/9007-default-config-test-case.patch: updated to not fail
  if building in a deep directory
* debian/patches/9014-event-fuzz.patch: add a little fuzz to not be quite
  so precise with expected expiry time. Fixes FTBFS with HZ=100 kernels.
  Can be dropped in 0.7.5.
* debian/patches/9015-hal-startup-failure-is-nonfatal.patch: disable hal
  driver if hald is not running instead of dying. Can be dropped in
  0.7.4.
* debian/control: temporarily remove Build-Depends on libcap-ng-dev, which
  isn't available in Ubuntu main yet
* revert change to new source format 3.0 (quilt) since Launchpad can't
  handle it yet (see LP: #293106)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2007, 2008, 2009 Red Hat, Inc.
3
 
 *
4
 
 * This library is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU Lesser General Public
6
 
 * License as published by the Free Software Foundation; either
7
 
 * version 2.1 of the License, or (at your option) any later version.
8
 
 *
9
 
 * This library is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * Lesser General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Lesser General Public
15
 
 * License along with this library; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
17
 
 *
18
 
 * Authors:
19
 
 *     Mark McLoughlin <markmc@redhat.com>
20
 
 */
21
 
 
22
 
#include <config.h>
23
 
 
24
 
#include "uuid.h"
25
 
 
26
 
#include <errno.h>
27
 
#include <fcntl.h>
28
 
#include <stdlib.h>
29
 
#include <stdio.h>
30
 
#include <string.h>
31
 
#include <sys/types.h>
32
 
#include <sys/stat.h>
33
 
#include <time.h>
34
 
#include <unistd.h>
35
 
 
36
 
#include "c-ctype.h"
37
 
#include "internal.h"
38
 
#include "util.h"
39
 
#include "virterror_internal.h"
40
 
#include "logging.h"
41
 
 
42
 
#ifndef ENODATA
43
 
#define ENODATA EIO
44
 
#endif
45
 
 
46
 
static int
47
 
virUUIDGenerateRandomBytes(unsigned char *buf,
48
 
                           int buflen)
49
 
{
50
 
    int fd;
51
 
 
52
 
    if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
53
 
        return errno;
54
 
 
55
 
    while (buflen > 0) {
56
 
        int n;
57
 
 
58
 
        if ((n = read(fd, buf, buflen)) <= 0) {
59
 
            if (errno == EINTR)
60
 
                continue;
61
 
            close(fd);
62
 
            return n < 0 ? errno : ENODATA;
63
 
        }
64
 
 
65
 
        buf += n;
66
 
        buflen -= n;
67
 
    }
68
 
 
69
 
    close(fd);
70
 
 
71
 
    return 0;
72
 
}
73
 
 
74
 
static int
75
 
virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
76
 
                                 int buflen)
77
 
{
78
 
    while (buflen > 0) {
79
 
        *buf = virRandom(256);
80
 
        buflen--;
81
 
    }
82
 
 
83
 
    return 0;
84
 
}
85
 
 
86
 
/**
87
 
 * virUUIDGenerate:
88
 
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the new UUID
89
 
 *
90
 
 * Generates a randomized unique identifier.
91
 
 *
92
 
 * Returns 0 in case of success and -1 in case of failure
93
 
 */
94
 
int
95
 
virUUIDGenerate(unsigned char *uuid)
96
 
{
97
 
    int err;
98
 
 
99
 
    if (uuid == NULL)
100
 
        return(-1);
101
 
 
102
 
    if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
103
 
        char ebuf[1024];
104
 
        VIR_WARN(_("Falling back to pseudorandom UUID,"
105
 
                   " failed to generate random bytes: %s"),
106
 
                 virStrerror(err, ebuf, sizeof ebuf));
107
 
    }
108
 
 
109
 
    return virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
110
 
}
111
 
 
112
 
/* Convert C from hexadecimal character to integer.  */
113
 
static int
114
 
hextobin (unsigned char c)
115
 
{
116
 
  switch (c)
117
 
    {
118
 
    default: return c - '0';
119
 
    case 'a': case 'A': return 10;
120
 
    case 'b': case 'B': return 11;
121
 
    case 'c': case 'C': return 12;
122
 
    case 'd': case 'D': return 13;
123
 
    case 'e': case 'E': return 14;
124
 
    case 'f': case 'F': return 15;
125
 
    }
126
 
}
127
 
 
128
 
/**
129
 
 * virUUIDParse:
130
 
 * @uuidstr: zero terminated string representation of the UUID
131
 
 * @uuid: array of VIR_UUID_BUFLEN bytes to store the raw UUID
132
 
 *
133
 
 * Parses the external string representation, allowing spaces and '-'
134
 
 * character in the sequence, and storing the result as a raw UUID
135
 
 *
136
 
 * Returns 0 in case of success and -1 in case of error.
137
 
 */
138
 
int
139
 
virUUIDParse(const char *uuidstr, unsigned char *uuid) {
140
 
    const char *cur;
141
 
    int i;
142
 
 
143
 
    if ((uuidstr == NULL) || (uuid == NULL))
144
 
        return(-1);
145
 
 
146
 
    /*
147
 
     * do a liberal scan allowing '-' and ' ' anywhere between character
148
 
     * pairs as long as there is 32 of them in the end.
149
 
     */
150
 
    cur = uuidstr;
151
 
    for (i = 0;i < VIR_UUID_BUFLEN;) {
152
 
        uuid[i] = 0;
153
 
        if (*cur == 0)
154
 
            goto error;
155
 
        if ((*cur == '-') || (*cur == ' ')) {
156
 
            cur++;
157
 
            continue;
158
 
        }
159
 
        if (!c_isxdigit(*cur))
160
 
            goto error;
161
 
        uuid[i] = hextobin(*cur);
162
 
        uuid[i] *= 16;
163
 
        cur++;
164
 
        if (*cur == 0)
165
 
            goto error;
166
 
        if (!c_isxdigit(*cur))
167
 
            goto error;
168
 
        uuid[i] += hextobin(*cur);
169
 
        i++;
170
 
        cur++;
171
 
    }
172
 
 
173
 
    return 0;
174
 
 
175
 
 error:
176
 
    return -1;
177
 
}
178
 
 
179
 
/**
180
 
 * virUUIDFormat:
181
 
 * @uuid: array of VIR_UUID_RAW_LEN bytes to store the raw UUID
182
 
 * @uuidstr: array of VIR_UUID_STRING_BUFLEN bytes to store the
183
 
 * string representation of the UUID in. The resulting string
184
 
 * will be NULL terminated.
185
 
 *
186
 
 * Converts the raw UUID into printable format, with embedded '-'
187
 
 *
188
 
 * Returns 0 in case of success and -1 in case of error.
189
 
 */
190
 
void virUUIDFormat(const unsigned char *uuid, char *uuidstr)
191
 
{
192
 
    snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
193
 
             "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
194
 
             uuid[0], uuid[1], uuid[2], uuid[3],
195
 
             uuid[4], uuid[5], uuid[6], uuid[7],
196
 
             uuid[8], uuid[9], uuid[10], uuid[11],
197
 
             uuid[12], uuid[13], uuid[14], uuid[15]);
198
 
    uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';
199
 
}