LCOV - code coverage report
Current view: top level - vswitchd - xenserver.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 10 24 41.7 %
Date: 2016-09-14 01:02:56 Functions: 3 3 100.0 %
Branches: 3 20 15.0 %

           Branch data     Line data    Source code
       1                 :            : /* Copyright (c) 2009, 2010, 2013 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 <pthread.h>
      21                 :            : #include <stdlib.h>
      22                 :            : #include <string.h>
      23                 :            : #include <unistd.h>
      24                 :            : #include "openvswitch/dynamic-string.h"
      25                 :            : #include "process.h"
      26                 :            : #include "util.h"
      27                 :            : #include "openvswitch/vlog.h"
      28                 :            : 
      29                 :       1288 : VLOG_DEFINE_THIS_MODULE(xenserver);
      30                 :            : 
      31                 :            : /* If running on a XenServer, the XenServer host UUID as a 36-character string,
      32                 :            :  * otherwise null. */
      33                 :            : static char *host_uuid;
      34                 :            : 
      35                 :            : static void
      36                 :        213 : read_host_uuid(void)
      37                 :            : {
      38                 :            :     static const char filename[] = "/etc/xensource-inventory";
      39                 :            :     char line[128];
      40                 :            :     FILE *file;
      41                 :            : 
      42                 :        213 :     file = fopen(filename, "r");
      43         [ +  - ]:        213 :     if (!file) {
      44         [ +  - ]:        213 :         if (errno == ENOENT) {
      45         [ -  + ]:        213 :             VLOG_DBG("not running on a XenServer");
      46                 :            :         } else {
      47         [ #  # ]:          0 :             VLOG_INFO("%s: open: %s", filename, ovs_strerror(errno));
      48                 :            :         }
      49                 :        213 :         return;
      50                 :            :     }
      51                 :            : 
      52         [ #  # ]:          0 :     while (fgets(line, sizeof line, file)) {
      53                 :            :         static const char leader[] = "INSTALLATION_UUID='";
      54                 :          0 :         const int leader_len = strlen(leader);
      55                 :          0 :         const int uuid_len = 36;
      56                 :            :         static const char trailer[] = "'\n";
      57                 :          0 :         const int trailer_len = strlen(trailer);
      58                 :            : 
      59         [ #  # ]:          0 :         if (strlen(line) == leader_len + uuid_len + trailer_len
      60         [ #  # ]:          0 :             && !memcmp(line, leader, leader_len)
      61         [ #  # ]:          0 :             && !memcmp(line + leader_len + uuid_len, trailer, trailer_len)) {
      62                 :          0 :             host_uuid = xmemdup0(line + leader_len, uuid_len);
      63         [ #  # ]:          0 :             VLOG_INFO("running on XenServer, host-uuid %s", host_uuid);
      64                 :          0 :             fclose(file);
      65                 :          0 :             return;
      66                 :            :         }
      67                 :            :     }
      68                 :          0 :     fclose(file);
      69         [ #  # ]:          0 :     VLOG_ERR("%s: INSTALLATION_UUID not found", filename);
      70                 :            : }
      71                 :            : 
      72                 :            : const char *
      73                 :       3311 : xenserver_get_host_uuid(void)
      74                 :            : {
      75                 :            :     static pthread_once_t once = PTHREAD_ONCE_INIT;
      76                 :       3311 :     pthread_once(&once, read_host_uuid);
      77                 :       3311 :     return host_uuid;
      78                 :            : }
      79                 :            : 

Generated by: LCOV version 1.12