]> git.proxmox.com Git - mirror_ovs.git/blame - vswitchd/xenserver.c
bridge: Fix a NULL dereference
[mirror_ovs.git] / vswitchd / xenserver.c
CommitLineData
10a89ef0 1/* Copyright (c) 2009, 2010, 2013 Nicira, Inc.
064af421 2 *
a14bc59f
BP
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
064af421 6 *
a14bc59f 7 * http://www.apache.org/licenses/LICENSE-2.0
064af421 8 *
a14bc59f
BP
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
064af421
BP
14 */
15
16#include <config.h>
17#include "xenserver.h"
18#include <ctype.h>
19#include <errno.h>
d3aa3710 20#include <pthread.h>
064af421
BP
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24#include "dynamic-string.h"
25#include "process.h"
064af421 26#include "vlog.h"
5136ce49 27
d98e6007 28VLOG_DEFINE_THIS_MODULE(xenserver);
064af421 29
d3aa3710
BP
30/* If running on a XenServer, the XenServer host UUID as a 36-character string,
31 * otherwise null. */
32static char *host_uuid;
33
34static void
064af421
BP
35read_host_uuid(void)
36{
37 static const char filename[] = "/etc/xensource-inventory";
38 char line[128];
39 FILE *file;
40
41 file = fopen(filename, "r");
42 if (!file) {
43 if (errno == ENOENT) {
11f8bde3 44 VLOG_DBG("not running on a XenServer");
064af421 45 } else {
10a89ef0 46 VLOG_INFO("%s: open: %s", filename, ovs_strerror(errno));
064af421 47 }
d3aa3710 48 return;
064af421
BP
49 }
50
51 while (fgets(line, sizeof line, file)) {
52 static const char leader[] = "INSTALLATION_UUID='";
53 const int leader_len = strlen(leader);
54 const int uuid_len = 36;
55 static const char trailer[] = "'\n";
56 const int trailer_len = strlen(trailer);
57
58 if (strlen(line) == leader_len + uuid_len + trailer_len
59 && !memcmp(line, leader, leader_len)
60 && !memcmp(line + leader_len + uuid_len, trailer, trailer_len)) {
d3aa3710 61 host_uuid = xmemdup0(line + leader_len, uuid_len);
064af421
BP
62 VLOG_INFO("running on XenServer, host-uuid %s", host_uuid);
63 fclose(file);
d3aa3710 64 return;
064af421
BP
65 }
66 }
67 fclose(file);
68 VLOG_ERR("%s: INSTALLATION_UUID not found", filename);
064af421
BP
69}
70
71const char *
72xenserver_get_host_uuid(void)
73{
d3aa3710
BP
74 static pthread_once_t once = PTHREAD_ONCE_INIT;
75 pthread_once(&once, read_host_uuid);
064af421
BP
76 return host_uuid;
77}
78