corosync  2.3.2
util.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2004 MontaVista Software, Inc.
3  * Copyright (c) 2004 Open Source Development Lab
4  * Copyright (c) 2006-2012 Red Hat, Inc.
5  *
6  * All rights reserved.
7  *
8  * Author: Steven Dake (sdake@redhat.com), Mark Haverkamp (markh@osdl.org)
9  *
10  * This software licensed under BSD license, the text of which follows:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * - Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * - Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  * - Neither the name of the MontaVista Software, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived from this
22  * software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <config.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 #include <sys/time.h>
44 #include <assert.h>
45 
46 #include <corosync/corotypes.h>
47 #include <corosync/corodefs.h>
48 #include <corosync/list.h>
49 #include <corosync/logsys.h>
50 #include "util.h"
51 
52 LOGSYS_DECLARE_SUBSYS ("MAIN");
53 
54 struct service_names {
55  const char *c_name;
56  int32_t c_val;
57 };
58 
59 static struct service_names servicenames[] =
60 {
61  { "CFG", CFG_SERVICE },
62  { "CPG", CPG_SERVICE },
63  { "QUORUM", QUORUM_SERVICE },
64  { "PLOAD", PLOAD_SERVICE },
65  { "VOTEQUORUM", VOTEQUORUM_SERVICE },
66  { "MON", MON_SERVICE },
67  { "WD", WD_SERVICE },
68  { "CMAP", CMAP_SERVICE },
69  { NULL, -1 }
70 };
71 
72 const char * short_service_name_get(uint32_t service_id,
73  char *buf, size_t buf_size)
74 {
75  uint32_t i;
76 
77  for (i = 0; servicenames[i].c_name != NULL; i++) {
78  if (service_id == servicenames[i].c_val) {
79  return (servicenames[i].c_name);
80  }
81  }
82  snprintf(buf, buf_size, "%d", service_id);
83  return buf;
84 }
85 
86 /*
87  * Compare two names. returns non-zero on match.
88  */
89 int name_match(cs_name_t *name1, cs_name_t *name2)
90 {
91  if (name1->length == name2->length) {
92  return ((strncmp ((char *)name1->value, (char *)name2->value,
93  name1->length)) == 0);
94  }
95  return 0;
96 }
97 
98 /*
99  * Get the time of day and convert to nanoseconds
100  */
102 {
103  struct timeval tv;
104  cs_time_t time_now;
105 
106  if (gettimeofday(&tv, 0)) {
107  return 0ULL;
108  }
109 
110  time_now = (cs_time_t)(tv.tv_sec) * 1000000000ULL;
111  time_now += (cs_time_t)(tv.tv_usec) * 1000ULL;
112 
113  return time_now;
114 }
115 
118 {
119  assert (0==1);
120  exit (EXIT_FAILURE);
121 }
122 
124  enum e_corosync_done err, const char *file, unsigned int line) __attribute__((noreturn));
125 
127  enum e_corosync_done err, const char *file, unsigned int line)
128 {
129  if (err == COROSYNC_DONE_EXIT) {
131  "Corosync Cluster Engine exiting normally");
132  } else {
133  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Cluster Engine exiting "
134  "with status %d at %s:%u.", err, file, line);
135  }
137  exit (err);
138 }
139 
140 #define min(a,b) ((a) < (b) ? (a) : (b))
141 
142 char *getcs_name_t (cs_name_t *name)
143 {
144  static char ret_name[CS_MAX_NAME_LENGTH];
145 
146  /* if string is corrupt (non-terminated), ensure it's displayed safely */
147  if (name->length >= CS_MAX_NAME_LENGTH || name->value[name->length] != '\0') {
148  memset (ret_name, 0, sizeof (ret_name));
149  memcpy (ret_name, name->value, min(name->length, CS_MAX_NAME_LENGTH -1));
150  return (ret_name);
151  }
152  return ((char *)name->value);
153 }
154 
155 void setcs_name_t (cs_name_t *name, char *str) {
156  strncpy ((char *)name->value, str, sizeof (name->value));
157  ((char *)name->value)[sizeof (name->value) - 1] = '\0';
158  if (strlen ((char *)name->value) > CS_MAX_NAME_LENGTH) {
159  name->length = CS_MAX_NAME_LENGTH;
160  } else {
161  name->length = strlen (str);
162  }
163 }
164 
165 int cs_name_tisEqual (cs_name_t *str1, char *str2) {
166  if (str1->length == strlen (str2)) {
167  return ((strncmp ((char *)str1->value, (char *)str2,
168  str1->length)) == 0);
169  } else {
170  return 0;
171  }
172 }