Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / vswitchd / xenserver.c
1 /* Copyright (c) 2009 Nicira Networks
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * In addition, as a special exception, Nicira Networks gives permission
17  * to link the code of its release of vswitchd with the OpenSSL project's
18  * "OpenSSL" library (or with modified versions of it that use the same
19  * license as the "OpenSSL" library), and distribute the linked
20  * executables.  You must obey the GNU General Public License in all
21  * respects for all of the code used other than "OpenSSL".  If you modify
22  * this file, you may extend this exception to your version of the file,
23  * but you are not obligated to do so.  If you do not wish to do so,
24  * delete this exception statement from your version.
25  */
26
27 #include <config.h>
28 #include "xenserver.h"
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include "dynamic-string.h"
35 #include "process.h"
36
37 #include "vlog.h"
38 #define THIS_MODULE VLM_xenserver
39
40 static char *
41 read_host_uuid(void)
42 {
43     static const char filename[] = "/etc/xensource-inventory";
44     char line[128];
45     FILE *file;
46
47     file = fopen(filename, "r");
48     if (!file) {
49         if (errno == ENOENT) {
50             VLOG_INFO("not running on a XenServer");
51         } else {
52             VLOG_INFO("%s: open: %s", filename, strerror(errno));
53         }
54         return NULL;
55     }
56
57     while (fgets(line, sizeof line, file)) {
58         static const char leader[] = "INSTALLATION_UUID='";
59         const int leader_len = strlen(leader);
60         const int uuid_len = 36;
61         static const char trailer[] = "'\n";
62         const int trailer_len = strlen(trailer);
63
64         if (strlen(line) == leader_len + uuid_len + trailer_len
65             && !memcmp(line, leader, leader_len)
66             && !memcmp(line + leader_len + uuid_len, trailer, trailer_len)) {
67             char *host_uuid = xmemdup0(line + leader_len, uuid_len);
68             VLOG_INFO("running on XenServer, host-uuid %s", host_uuid);
69             fclose(file);
70             return host_uuid;
71         }
72     }
73     fclose(file);
74     VLOG_ERR("%s: INSTALLATION_UUID not found", filename);
75     return NULL;
76 }
77
78 const char *
79 xenserver_get_host_uuid(void)
80 {
81     static char *host_uuid;
82     static bool inited;
83
84     if (!inited) {
85         host_uuid = read_host_uuid();
86         inited = true;
87     }
88     return host_uuid;
89 }
90