corosync
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#include <sys/socket.h>
46
47#include <libknet.h>
48
49#include <corosync/corotypes.h>
50#include <corosync/corodefs.h>
51#include <corosync/icmap.h>
52#include <corosync/logsys.h>
53#include "util.h"
54
56
58 const char *c_name;
60};
61
62static struct service_names servicenames[] =
63{
64 { "CFG", CFG_SERVICE },
65 { "CPG", CPG_SERVICE },
66 { "QUORUM", QUORUM_SERVICE },
67 { "PLOAD", PLOAD_SERVICE },
68 { "VOTEQUORUM", VOTEQUORUM_SERVICE },
69 { "MON", MON_SERVICE },
70 { "WD", WD_SERVICE },
71 { "CMAP", CMAP_SERVICE },
72 { NULL, -1 }
73};
74
75const char * short_service_name_get(uint32_t service_id,
76 char *buf, size_t buf_size)
77{
78 uint32_t i;
79
80 for (i = 0; servicenames[i].c_name != NULL; i++) {
81 if (service_id == servicenames[i].c_val) {
82 return (servicenames[i].c_name);
83 }
84 }
85 snprintf(buf, buf_size, "%d", service_id);
86 return buf;
87}
88
89/*
90 * Compare two names. returns non-zero on match.
91 */
93{
94 if (name1->length == name2->length) {
95 return ((strncmp ((char *)name1->value, (char *)name2->value,
96 name1->length)) == 0);
97 }
98 return 0;
99}
100
101/*
102 * Get the time of day and convert to nanoseconds
103 */
105{
106 struct timeval tv;
108
109 if (gettimeofday(&tv, 0)) {
110 return 0ULL;
111 }
112
113 time_now = (cs_time_t)(tv.tv_sec) * 1000000000ULL;
114 time_now += (cs_time_t)(tv.tv_usec) * 1000ULL;
115
116 return time_now;
117}
118
125
127 enum e_corosync_done err, const char *file, unsigned int line) __attribute__((noreturn));
128
130 enum e_corosync_done err, const char *file, unsigned int line)
131{
132 if (err == COROSYNC_DONE_EXIT) {
134 "Corosync Cluster Engine exiting normally");
135 } else {
136 log_printf (LOGSYS_LEVEL_ERROR, "Corosync Cluster Engine exiting "
137 "with status %d at %s:%u.", err, file, line);
138 }
140 exit (err);
141}
142
144{
145 static char ret_name[CS_MAX_NAME_LENGTH];
146
147 /* if string is corrupt (non-terminated), ensure it's displayed safely */
148 if (name->length >= CS_MAX_NAME_LENGTH || name->value[name->length] != '\0') {
149 memset (ret_name, 0, sizeof (ret_name));
150 memcpy (ret_name, name->value, min(name->length, CS_MAX_NAME_LENGTH -1));
151 return (ret_name);
152 }
153 return ((char *)name->value);
154}
155
156void setcs_name_t (cs_name_t *name, char *str) {
157 strncpy ((char *)name->value, str, sizeof (name->value) - 1);
158 ((char *)name->value)[sizeof (name->value) - 1] = '\0';
159 if (strlen ((char *)name->value) > CS_MAX_NAME_LENGTH) {
161 } else {
162 name->length = strlen (str);
163 }
164}
165
167 if (str1->length == strlen (str2)) {
168 return ((strncmp ((char *)str1->value, (char *)str2,
169 str1->length)) == 0);
170 } else {
171 return 0;
172 }
173}
174
175const char *get_state_dir(void)
176{
177 static char path[PATH_MAX] = {'\0'};
178 char *state_dir;
179 int res;
180
181 if (path[0] == '\0') {
182 if (icmap_get_string("system.state_dir", &state_dir) == CS_OK) {
185 } else if ((state_dir = getenv("STATE_DIRECTORY")) != NULL) {
186 /*
187 * systemd allows multiple directory names that are
188 * passed to env variable separated by colon. Support for this feature
189 * is deliberately not implemented because corosync always
190 * uses just one state directory and it is unclear what behavior should
191 * be taken for multiple ones. If reasonable need for
192 * supporting multiple directories appear, it must be implemented also
193 * for cmap.
194 */
196 } else {
197 res = snprintf(path, PATH_MAX, "%s/%s", LOCALSTATEDIR, "lib/corosync");
198 }
199
201 }
202
203 return (path);
204}
205
206static int safe_strcat(char *dst, size_t dst_len, const char *src)
207{
208
209 if (strlen(dst) + strlen(src) >= dst_len - 1) {
210 return (-1);
211 }
212
213 strcat(dst, src);
214
215 return (0);
216}
217
218#define UTILS_IS_VALID_KNET_LIST_MAX_ITEMS 256
219
220/*
221 * val - string in items array to find
222 * items - array of const char * of items
223 * no_items - size of array
224 * list_str - string with concatenated list of items - can be NULL
225 * machine_parseable_str - 0 - split strings by space, 1 - use human form (split by "," and last item with "or")
226 * error_string_prefix - Prefix to add into error string
227 * error_string - Complete error string
228 */
229static int util_is_valid_knet_list_helper(const char *val,
230 const char **items, size_t no_items,
231 const char **list_str, int machine_parseable_str,
232 const char *error_string_prefix, const char **error_string)
233{
234 static char local_error_str[512];
235 static char local_list_str[256];
236 size_t zi;
237 int model_found = 0;
238
239 if (list_str != NULL) {
241 }
242
245
247
248 for (zi = 0; zi < no_items; zi++) {
249 if (zi == 0) {
250 } else if (zi == no_items - 1) {
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 {
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), items[zi]);
265
266 if (val != NULL && strcmp(val, items[zi]) == 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);
274 }
275
276 return (model_found);
277}
278
279/*
280 * val - knet crypto model to find
281 * crypto_list_str - string with concatenated list of available crypto models - can be NULL
282 * machine_parseable_str - 0 - split strings by space, 1 - use human form (split by "," and last item with "or")
283 * error_string_prefix - Prefix to add into error string
284 * error_string - Complete error string
285 */
287 const char **list_str, int machine_parseable_str,
288 const char *error_string_prefix, const char **error_string)
289{
290 size_t entries;
292 const char *items[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
293 size_t zi;
294
295 if (knet_get_crypto_list(NULL, &entries) != 0) {
296 *error_string = "internal error - cannot get knet crypto list";
297 return (-1);
298 }
299
300 if (entries > sizeof(crypto_list) / sizeof(crypto_list[0])) {
301 *error_string = "internal error - too many knet crypto list entries";
302 return (-1);
303 }
304
305 if (knet_get_crypto_list(crypto_list, &entries) != 0) {
306 *error_string = "internal error - cannot get knet crypto list";
307 return (-1);
308 }
309
310 for (zi = 0; zi < entries; zi++) {
311 items[zi] = crypto_list[zi].name;
312 }
313
314 return (util_is_valid_knet_list_helper(val, items, entries, list_str,
316}
317
318/*
319 * Similar to util_is_valid_knet_crypto_model
320 */
322 const char **list_str, int machine_parseable_str,
323 const char *error_string_prefix, const char **error_string)
324{
325 size_t entries;
327 const char *items[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
328 size_t zi;
329
330 if (knet_get_compress_list(NULL, &entries) != 0) {
331 *error_string = "internal error - cannot get knet compress list";
332 return (-1);
333 }
334
335 if (entries > sizeof(compress_list) / sizeof(compress_list[0])) {
336 *error_string = "internal error - too many knet compress list entries";
337 return (-1);
338 }
339
340 if (knet_get_compress_list(compress_list, &entries) != 0) {
341 *error_string = "internal error - cannot get knet compress list";
342 return (-1);
343 }
344
345 for (zi = 0; zi < entries; zi++) {
346 items[zi] = compress_list[zi].name;
347 }
348
349 return (util_is_valid_knet_list_helper(val, items, entries, list_str,
351}
352
353/*
354 * Similar to util_is_valid_knet_crypto_model
355 */
357 const char **list_str, int machine_parseable_str,
358 const char *error_string_prefix, const char **error_string)
359{
360 const char *items[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
361 size_t items_out_idx = 0;
362#ifdef HAVE_KNET_GET_CRYPTO_CIPHER_LIST
363 size_t entries;
364 size_t zi;
366#endif
367
368#ifdef ENABLE_UNENCRYPTED
369 items[items_out_idx++] = "none";
370#endif
371
372#ifdef HAVE_KNET_GET_CRYPTO_CIPHER_LIST
373 if (knet_get_crypto_cipher_list(NULL, &entries) != 0) {
374 *error_string = "internal error - cannot get crypto cipher list";
375 return (-1);
376 }
377
378 if (entries > (sizeof(crypto_cipher_list) / sizeof(crypto_cipher_list[0])) - items_out_idx) {
379 *error_string = "internal error - too many knet crypto cipher list entries";
380 return (-1);
381 }
382
384 *error_string = "internal error - cannot get knet crypto cipher list";
385 return (-1);
386 }
387
388 for (zi = 0; zi < entries; zi++) {
389 items[items_out_idx++] = crypto_cipher_list[zi].name;
390 }
391#else
392 items[items_out_idx++] = "aes256";
393 items[items_out_idx++] = "aes192";
394 items[items_out_idx++] = "aes128";
395#endif
396
397 return (util_is_valid_knet_list_helper(val, items, items_out_idx, list_str,
399}
400
401/*
402 * Similar to util_is_valid_knet_crypto_model
403 */
405 const char **list_str, int machine_parseable_str,
406 const char *error_string_prefix, const char **error_string)
407{
408 const char *items[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
409 size_t items_out_idx = 0;
410#ifdef HAVE_KNET_GET_CRYPTO_HASH_LIST
411 size_t entries;
412 size_t zi;
414#endif
415
416#ifdef ENABLE_UNENCRYPTED
417 items[items_out_idx++] = "none";
418#endif
419
420#ifdef HAVE_KNET_GET_CRYPTO_HASH_LIST
421 if (knet_get_crypto_hash_list(NULL, &entries) != 0) {
422 *error_string = "internal error - cannot get crypto hash list";
423 return (-1);
424 }
425
426 if (entries > (sizeof(crypto_hash_list) / sizeof(crypto_hash_list[0])) - items_out_idx) {
427 *error_string = "internal error - too many knet crypto hash list entries";
428 return (-1);
429 }
430
431 if (knet_get_crypto_hash_list(crypto_hash_list, &entries) != 0) {
432 *error_string = "internal error - cannot get knet crypto hash list";
433 return (-1);
434 }
435
436 for (zi = 0; zi < entries; zi++) {
437 items[items_out_idx++] = crypto_hash_list[zi].name;
438 }
439#else
440 items[items_out_idx++] = "md5";
441 items[items_out_idx++] = "sha1";
442 items[items_out_idx++] = "sha256";
443 items[items_out_idx++] = "sha384";
444 items[items_out_idx++] = "sha512";
445#endif
446
447 return (util_is_valid_knet_list_helper(val, items, items_out_idx, list_str,
449}
450
451int
452set_socket_dscp(int socket, unsigned char dscp)
453{
454 int res = 0;
455 int tos;
456
457 if (dscp) {
458 /* dscp is the upper 6 bits of TOS IP header field, RFC 2474 */
459 tos = (dscp & 0x3f) << 2;
460 res = setsockopt(socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
461 }
462
463 return res;
464}
#define LOCALSTATEDIR
Definition config.h:373
unsigned int nodeid
the ID of the node whose info needs updating
Definition coroapi.h:0
@ 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:860
#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:284
#define LOGSYS_LEVEL_NOTICE
Definition logsys.h:74
#define LOGSYS_DECLARE_SUBSYS(subsys)
The LOGSYS_DECLARE_SUBSYS macro.
Definition logsys.h:306
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:59
const char * c_name
Definition util.c:58
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:286
char * getcs_name_t(cs_name_t *name)
Definition util.c:143
const char * get_state_dir(void)
Definition util.c:175
cs_time_t clust_time_now(void)
Get the time of day and convert to nanoseconds.
Definition util.c:104
int util_is_valid_knet_crypto_cipher(const char *val, const char **list_str, int machine_parseable_str, const char *error_string_prefix, const char **error_string)
Definition util.c:356
int util_is_valid_knet_crypto_hash(const char *val, const char **list_str, int machine_parseable_str, const char *error_string_prefix, const char **error_string)
Definition util.c:404
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:75
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:321
#define UTILS_IS_VALID_KNET_LIST_MAX_ITEMS
Definition util.c:218
int name_match(cs_name_t *name1, cs_name_t *name2)
Compare two names.
Definition util.c:92
void setcs_name_t(cs_name_t *name, char *str)
Definition util.c:156
int set_socket_dscp(int socket, unsigned char dscp)
Definition util.c:452
void _corosync_out_of_memory_error(void)
Definition util.c:119
void _corosync_exit_error(enum e_corosync_done err, const char *file, unsigned int line) __attribute__((noreturn))
Definition util.c:129
int cs_name_tisEqual(cs_name_t *str1, char *str2)
Definition util.c:166