Global replace of Nicira Networks.
[sliver-openvswitch.git] / vswitchd / xenserver.c
1 /* Copyright (c) 2009, 2010 Nicira, Inc.
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17 #include "xenserver.h"
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include "dynamic-string.h"
24 #include "process.h"
25 #include "vlog.h"
26
27 VLOG_DEFINE_THIS_MODULE(xenserver);
28
29 static char *
30 read_host_uuid(void)
31 {
32     static const char filename[] = "/etc/xensource-inventory";
33     char line[128];
34     FILE *file;
35
36     file = fopen(filename, "r");
37     if (!file) {
38         if (errno == ENOENT) {
39             VLOG_INFO("not running on a XenServer");
40         } else {
41             VLOG_INFO("%s: open: %s", filename, strerror(errno));
42         }
43         return NULL;
44     }
45
46     while (fgets(line, sizeof line, file)) {
47         static const char leader[] = "INSTALLATION_UUID='";
48         const int leader_len = strlen(leader);
49         const int uuid_len = 36;
50         static const char trailer[] = "'\n";
51         const int trailer_len = strlen(trailer);
52
53         if (strlen(line) == leader_len + uuid_len + trailer_len
54             && !memcmp(line, leader, leader_len)
55             && !memcmp(line + leader_len + uuid_len, trailer, trailer_len)) {
56             char *host_uuid = xmemdup0(line + leader_len, uuid_len);
57             VLOG_INFO("running on XenServer, host-uuid %s", host_uuid);
58             fclose(file);
59             return host_uuid;
60         }
61     }
62     fclose(file);
63     VLOG_ERR("%s: INSTALLATION_UUID not found", filename);
64     return NULL;
65 }
66
67 const char *
68 xenserver_get_host_uuid(void)
69 {
70     static char *host_uuid;
71     static bool inited;
72
73     if (!inited) {
74         host_uuid = read_host_uuid();
75         inited = true;
76     }
77     return host_uuid;
78 }
79