corosync  3.1.0
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/icmap.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 char *getcs_name_t (cs_name_t *name)
141 {
142  static char ret_name[CS_MAX_NAME_LENGTH];
143 
144  /* if string is corrupt (non-terminated), ensure it's displayed safely */
145  if (name->length >= CS_MAX_NAME_LENGTH || name->value[name->length] != '\0') {
146  memset (ret_name, 0, sizeof (ret_name));
147  memcpy (ret_name, name->value, min(name->length, CS_MAX_NAME_LENGTH -1));
148  return (ret_name);
149  }
150  return ((char *)name->value);
151 }
152 
153 void setcs_name_t (cs_name_t *name, char *str) {
154  strncpy ((char *)name->value, str, sizeof (name->value) - 1);
155  ((char *)name->value)[sizeof (name->value) - 1] = '\0';
156  if (strlen ((char *)name->value) > CS_MAX_NAME_LENGTH) {
157  name->length = CS_MAX_NAME_LENGTH;
158  } else {
159  name->length = strlen (str);
160  }
161 }
162 
163 int cs_name_tisEqual (cs_name_t *str1, char *str2) {
164  if (str1->length == strlen (str2)) {
165  return ((strncmp ((char *)str1->value, (char *)str2,
166  str1->length)) == 0);
167  } else {
168  return 0;
169  }
170 }
171 
172 const char *get_state_dir(void)
173 {
174  static char path[PATH_MAX] = {'\0'};
175  char *cmap_state_dir;
176  int res;
177 
178  if (path[0] == '\0') {
179  if (icmap_get_string("system.state_dir", &cmap_state_dir) == CS_OK) {
180  res = snprintf(path, PATH_MAX, "%s", cmap_state_dir);
181  free(cmap_state_dir);
182  } else {
183  res = snprintf(path, PATH_MAX, "%s/%s", LOCALSTATEDIR, "lib/corosync");
184  }
185 
186  assert(res < PATH_MAX);
187  }
188 
189  return (path);
190 }
void setcs_name_t(cs_name_t *name, char *str)
Definition: util.c:153
#define min(a, b)
Definition: exec/util.h:66
int cs_name_tisEqual(cs_name_t *str1, char *str2)
Definition: util.c:163
uint8_t value[CS_MAX_NAME_LENGTH]
Definition: corotypes.h:67
int name_match(cs_name_t *name1, cs_name_t *name2)
Compare two names.
Definition: util.c:89
LOGSYS_DECLARE_SUBSYS("MAIN")
#define LOCALSTATEDIR
Definition: config.h:376
void _corosync_exit_error(enum e_corosync_done err, const char *file, unsigned int line) __attribute__((noreturn))
Definition: util.c:126
#define log_printf(level, format, args...)
Definition: logsys.h:323
const char * c_name
Definition: util.c:55
const char * short_service_name_get(uint32_t service_id, char *buf, size_t buf_size)
Get the short name of a service from the service_id.
Definition: util.c:72
int32_t c_val
Definition: util.c:56
int64_t cs_time_t
cs_time_t
Definition: corotypes.h:51
#define LOGSYS_LEVEL_ERROR
Definition: logsys.h:72
char * getcs_name_t(cs_name_t *name)
Definition: util.c:140
#define CS_MAX_NAME_LENGTH
Definition: corotypes.h:55
uint16_t length
Definition: corotypes.h:66
const char * get_state_dir(void)
Definition: util.c:172
void _corosync_out_of_memory_error(void)
Definition: util.c:116
e_corosync_done
Definition: exec/util.h:47
cs_error_t icmap_get_string(const char *key_name, char **str)
Shortcut for icmap_get for string type.
Definition: icmap.c:856
typedef __attribute__
#define LOGSYS_LEVEL_NOTICE
Definition: logsys.h:74
cs_time_t clust_time_now(void)
Get the time of day and convert to nanoseconds.
Definition: util.c:101
The cs_name_t struct.
Definition: corotypes.h:65
void logsys_system_fini(void)
logsys_system_fini
Definition: logsys.c:286