corosync  3.1.7
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 <libknet.h>
47 
48 #include <corosync/corotypes.h>
49 #include <corosync/corodefs.h>
50 #include <corosync/icmap.h>
51 #include <corosync/logsys.h>
52 #include "util.h"
53 
55 
56 struct service_names {
57  const char *c_name;
58  int32_t c_val;
59 };
60 
61 static struct service_names servicenames[] =
62 {
63  { "CFG", CFG_SERVICE },
64  { "CPG", CPG_SERVICE },
65  { "QUORUM", QUORUM_SERVICE },
66  { "PLOAD", PLOAD_SERVICE },
67  { "VOTEQUORUM", VOTEQUORUM_SERVICE },
68  { "MON", MON_SERVICE },
69  { "WD", WD_SERVICE },
70  { "CMAP", CMAP_SERVICE },
71  { NULL, -1 }
72 };
73 
74 const char * short_service_name_get(uint32_t service_id,
75  char *buf, size_t buf_size)
76 {
77  uint32_t i;
78 
79  for (i = 0; servicenames[i].c_name != NULL; i++) {
80  if (service_id == servicenames[i].c_val) {
81  return (servicenames[i].c_name);
82  }
83  }
84  snprintf(buf, buf_size, "%d", service_id);
85  return buf;
86 }
87 
88 /*
89  * Compare two names. returns non-zero on match.
90  */
91 int name_match(cs_name_t *name1, cs_name_t *name2)
92 {
93  if (name1->length == name2->length) {
94  return ((strncmp ((char *)name1->value, (char *)name2->value,
95  name1->length)) == 0);
96  }
97  return 0;
98 }
99 
100 /*
101  * Get the time of day and convert to nanoseconds
102  */
104 {
105  struct timeval tv;
106  cs_time_t time_now;
107 
108  if (gettimeofday(&tv, 0)) {
109  return 0ULL;
110  }
111 
112  time_now = (cs_time_t)(tv.tv_sec) * 1000000000ULL;
113  time_now += (cs_time_t)(tv.tv_usec) * 1000ULL;
114 
115  return time_now;
116 }
117 
120 {
121  assert (0==1);
122  exit (EXIT_FAILURE);
123 }
124 
126  enum e_corosync_done err, const char *file, unsigned int line) __attribute__((noreturn));
127 
129  enum e_corosync_done err, const char *file, unsigned int line)
130 {
131  if (err == COROSYNC_DONE_EXIT) {
133  "Corosync Cluster Engine exiting normally");
134  } else {
135  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Cluster Engine exiting "
136  "with status %d at %s:%u.", err, file, line);
137  }
139  exit (err);
140 }
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) - 1);
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 }
173 
174 const char *get_state_dir(void)
175 {
176  static char path[PATH_MAX] = {'\0'};
177  char *cmap_state_dir;
178  int res;
179 
180  if (path[0] == '\0') {
181  if (icmap_get_string("system.state_dir", &cmap_state_dir) == CS_OK) {
182  res = snprintf(path, PATH_MAX, "%s", cmap_state_dir);
183  free(cmap_state_dir);
184  } else {
185  res = snprintf(path, PATH_MAX, "%s/%s", LOCALSTATEDIR, "lib/corosync");
186  }
187 
188  assert(res < PATH_MAX);
189  }
190 
191  return (path);
192 }
193 
194 static int safe_strcat(char *dst, size_t dst_len, const char *src)
195 {
196 
197  if (strlen(dst) + strlen(src) >= dst_len - 1) {
198  return (-1);
199  }
200 
201  strcat(dst, src);
202 
203  return (0);
204 }
205 
206 /*
207  * val - knet crypto model to find
208  * crypto_list_str - string with concatenated list of available crypto models - can be NULL
209  * machine_parseable_str - 0 - split strings by space, 1 - use human form (split by "," and last item with "or")
210  * error_string_prefix - Prefix to add into error string
211  * error_string - Complete error string
212  */
214  const char **list_str, int machine_parseable_str,
215  const char *error_string_prefix, const char **error_string)
216 {
217  size_t entries;
218  struct knet_crypto_info crypto_list[16];
219  size_t zi;
220  static char local_error_str[512];
221  static char local_list_str[256];
222  int model_found = 0;
223 
224  if (list_str != NULL) {
225  *list_str = local_list_str;
226  }
227 
228  memset(local_error_str, 0, sizeof(local_error_str));
229  memset(local_list_str, 0, sizeof(local_list_str));
230 
231  safe_strcat(local_error_str, sizeof(local_error_str), error_string_prefix);
232 
233  if (knet_get_crypto_list(NULL, &entries) != 0) {
234  *error_string = "internal error - cannot get knet crypto list";
235  return (-1);
236  }
237 
238  if (entries > sizeof(crypto_list) / sizeof(crypto_list[0])) {
239  *error_string = "internal error - too many knet crypto list entries";
240  return (-1);
241  }
242 
243  if (knet_get_crypto_list(crypto_list, &entries) != 0) {
244  *error_string = "internal error - cannot get knet crypto list";
245  return (-1);
246  }
247 
248  for (zi = 0; zi < entries; zi++) {
249  if (zi == 0) {
250  } else if (zi == entries - 1) {
251  if (machine_parseable_str) {
252  (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
253  } else {
254  (void)safe_strcat(local_list_str, sizeof(local_list_str), " or ");
255  }
256  } else {
257  if (machine_parseable_str) {
258  (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
259  } else {
260  (void)safe_strcat(local_list_str, sizeof(local_list_str), ", ");
261  }
262  }
263 
264  (void)safe_strcat(local_list_str, sizeof(local_list_str), crypto_list[zi].name);
265 
266  if (val != NULL && strcmp(val, crypto_list[zi].name) == 0) {
267  model_found = 1;
268  }
269  }
270 
271  if (!model_found) {
272  (void)safe_strcat(local_error_str, sizeof(local_error_str), local_list_str);
273  *error_string = local_error_str;
274  }
275 
276  return (model_found);
277 }
278 
280  const char **list_str, int machine_parseable_str,
281  const char *error_string_prefix, const char **error_string)
282 {
283  size_t entries;
284  struct knet_compress_info compress_list[16];
285  size_t zi;
286  static char local_error_str[512];
287  static char local_list_str[256];
288  int model_found = 0;
289 
290  if (list_str != NULL) {
291  *list_str = local_list_str;
292  }
293 
294  memset(local_error_str, 0, sizeof(local_error_str));
295  memset(local_list_str, 0, sizeof(local_list_str));
296 
297  safe_strcat(local_error_str, sizeof(local_error_str), error_string_prefix);
298 
299  if (knet_get_compress_list(NULL, &entries) != 0) {
300  *error_string = "internal error - cannot get knet compress list";
301  return (-1);
302  }
303 
304  if (entries > sizeof(compress_list) / sizeof(compress_list[0])) {
305  *error_string = "internal error - too many knet compress list entries";
306  return (-1);
307  }
308 
309  if (knet_get_compress_list(compress_list, &entries) != 0) {
310  *error_string = "internal error - cannot get knet compress list";
311  return (-1);
312  }
313 
314  for (zi = 0; zi < entries; zi++) {
315  if (zi == 0) {
316  } else if (zi == entries - 1) {
317  if (machine_parseable_str) {
318  (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
319  } else {
320  (void)safe_strcat(local_list_str, sizeof(local_list_str), " or ");
321  }
322  } else {
323  if (machine_parseable_str) {
324  (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
325  } else {
326  (void)safe_strcat(local_list_str, sizeof(local_list_str), ", ");
327  }
328  }
329 
330  (void)safe_strcat(local_list_str, sizeof(local_list_str), compress_list[zi].name);
331 
332  if (val != NULL && strcmp(val, compress_list[zi].name) == 0) {
333  model_found = 1;
334  }
335  }
336 
337  if (!model_found) {
338  (void)safe_strcat(local_error_str, sizeof(local_error_str), local_list_str);
339  *error_string = local_error_str;
340  }
341 
342  return (model_found);
343 }
#define LOCALSTATEDIR
Definition: config.h:373
@ CPG_SERVICE
Definition: corodefs.h:46
@ MON_SERVICE
Definition: corodefs.h:50
@ WD_SERVICE
Definition: corodefs.h:51
@ PLOAD_SERVICE
Definition: corodefs.h:48
@ CMAP_SERVICE
Definition: corodefs.h:44
@ CFG_SERVICE
Definition: corodefs.h:45
@ QUORUM_SERVICE
Definition: corodefs.h:47
@ VOTEQUORUM_SERVICE
Definition: corodefs.h:49
int64_t cs_time_t
cs_time_t
Definition: corotypes.h:51
#define CS_MAX_NAME_LENGTH
Definition: corotypes.h:55
@ CS_OK
Definition: corotypes.h:99
e_corosync_done
Definition: exec/util.h:47
@ COROSYNC_DONE_EXIT
Definition: exec/util.h:48
#define min(a, b)
Definition: exec/util.h:66
cs_error_t icmap_get_string(const char *key_name, char **str)
Shortcut for icmap_get for string type.
Definition: icmap.c:856
#define LOGSYS_LEVEL_ERROR
Definition: logsys.h:72
#define log_printf(level, format, args...)
Definition: logsys.h:332
void logsys_system_fini(void)
logsys_system_fini
Definition: logsys.c:286
#define LOGSYS_LEVEL_NOTICE
Definition: logsys.h:74
The cs_name_t struct.
Definition: corotypes.h:66
uint8_t value[CS_MAX_NAME_LENGTH]
Definition: corotypes.h:68
uint16_t length
Definition: corotypes.h:67
int32_t c_val
Definition: util.c:58
const char * c_name
Definition: util.c:57
typedef __attribute__
int util_is_valid_knet_crypto_model(const char *val, const char **list_str, int machine_parseable_str, const char *error_string_prefix, const char **error_string)
Definition: util.c:213
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:74
cs_time_t clust_time_now(void)
Get the time of day and convert to nanoseconds.
Definition: util.c:103
int util_is_valid_knet_compress_model(const char *val, const char **list_str, int machine_parseable_str, const char *error_string_prefix, const char **error_string)
Definition: util.c:279
int name_match(cs_name_t *name1, cs_name_t *name2)
Compare two names.
Definition: util.c:91
void setcs_name_t(cs_name_t *name, char *str)
Definition: util.c:155
char * getcs_name_t(cs_name_t *name)
Definition: util.c:142
const char * get_state_dir(void)
Definition: util.c:174
void _corosync_out_of_memory_error(void)
Definition: util.c:118
LOGSYS_DECLARE_SUBSYS("MAIN")
void _corosync_exit_error(enum e_corosync_done err, const char *file, unsigned int line) __attribute__((noreturn))
Definition: util.c:128
int cs_name_tisEqual(cs_name_t *str1, char *str2)
Definition: util.c:165