corosync 3.1.10
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;
59 int32_t c_val;
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 */
92int name_match(cs_name_t *name1, cs_name_t *name2)
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;
107 cs_time_t time_now;
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
121{
122 assert (0==1);
123 exit (EXIT_FAILURE);
124}
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
166int cs_name_tisEqual (cs_name_t *str1, char *str2) {
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) {
183 res = snprintf(path, PATH_MAX, "%s", state_dir);
184 free(state_dir);
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 */
195 res = snprintf(path, PATH_MAX, "%s", state_dir);
196 } else {
197 res = snprintf(path, PATH_MAX, "%s/%s", LOCALSTATEDIR, "lib/corosync");
198 }
199
200 assert(res < PATH_MAX);
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/*
219 * val - knet crypto model to find
220 * crypto_list_str - string with concatenated list of available crypto models - can be NULL
221 * machine_parseable_str - 0 - split strings by space, 1 - use human form (split by "," and last item with "or")
222 * error_string_prefix - Prefix to add into error string
223 * error_string - Complete error string
224 */
226 const char **list_str, int machine_parseable_str,
227 const char *error_string_prefix, const char **error_string)
228{
229 size_t entries;
230 struct knet_crypto_info crypto_list[16];
231 size_t zi;
232 static char local_error_str[512];
233 static char local_list_str[256];
234 int model_found = 0;
235
236 if (list_str != NULL) {
237 *list_str = local_list_str;
238 }
239
240 memset(local_error_str, 0, sizeof(local_error_str));
241 memset(local_list_str, 0, sizeof(local_list_str));
242
243 safe_strcat(local_error_str, sizeof(local_error_str), error_string_prefix);
244
245 if (knet_get_crypto_list(NULL, &entries) != 0) {
246 *error_string = "internal error - cannot get knet crypto list";
247 return (-1);
248 }
249
250 if (entries > sizeof(crypto_list) / sizeof(crypto_list[0])) {
251 *error_string = "internal error - too many knet crypto list entries";
252 return (-1);
253 }
254
255 if (knet_get_crypto_list(crypto_list, &entries) != 0) {
256 *error_string = "internal error - cannot get knet crypto list";
257 return (-1);
258 }
259
260 for (zi = 0; zi < entries; zi++) {
261 if (zi == 0) {
262 } else if (zi == entries - 1) {
263 if (machine_parseable_str) {
264 (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
265 } else {
266 (void)safe_strcat(local_list_str, sizeof(local_list_str), " or ");
267 }
268 } else {
269 if (machine_parseable_str) {
270 (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
271 } else {
272 (void)safe_strcat(local_list_str, sizeof(local_list_str), ", ");
273 }
274 }
275
276 (void)safe_strcat(local_list_str, sizeof(local_list_str), crypto_list[zi].name);
277
278 if (val != NULL && strcmp(val, crypto_list[zi].name) == 0) {
279 model_found = 1;
280 }
281 }
282
283 if (!model_found) {
284 (void)safe_strcat(local_error_str, sizeof(local_error_str), local_list_str);
285 *error_string = local_error_str;
286 }
287
288 return (model_found);
289}
290
292 const char **list_str, int machine_parseable_str,
293 const char *error_string_prefix, const char **error_string)
294{
295 size_t entries;
296 struct knet_compress_info compress_list[16];
297 size_t zi;
298 static char local_error_str[512];
299 static char local_list_str[256];
300 int model_found = 0;
301
302 if (list_str != NULL) {
303 *list_str = local_list_str;
304 }
305
306 memset(local_error_str, 0, sizeof(local_error_str));
307 memset(local_list_str, 0, sizeof(local_list_str));
308
309 safe_strcat(local_error_str, sizeof(local_error_str), error_string_prefix);
310
311 if (knet_get_compress_list(NULL, &entries) != 0) {
312 *error_string = "internal error - cannot get knet compress list";
313 return (-1);
314 }
315
316 if (entries > sizeof(compress_list) / sizeof(compress_list[0])) {
317 *error_string = "internal error - too many knet compress list entries";
318 return (-1);
319 }
320
321 if (knet_get_compress_list(compress_list, &entries) != 0) {
322 *error_string = "internal error - cannot get knet compress list";
323 return (-1);
324 }
325
326 for (zi = 0; zi < entries; zi++) {
327 if (zi == 0) {
328 } else if (zi == entries - 1) {
329 if (machine_parseable_str) {
330 (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
331 } else {
332 (void)safe_strcat(local_list_str, sizeof(local_list_str), " or ");
333 }
334 } else {
335 if (machine_parseable_str) {
336 (void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
337 } else {
338 (void)safe_strcat(local_list_str, sizeof(local_list_str), ", ");
339 }
340 }
341
342 (void)safe_strcat(local_list_str, sizeof(local_list_str), compress_list[zi].name);
343
344 if (val != NULL && strcmp(val, compress_list[zi].name) == 0) {
345 model_found = 1;
346 }
347 }
348
349 if (!model_found) {
350 (void)safe_strcat(local_error_str, sizeof(local_error_str), local_list_str);
351 *error_string = local_error_str;
352 }
353
354 return (model_found);
355}
356
357int
358set_socket_dscp(int socket, unsigned char dscp)
359{
360 int res = 0;
361 int tos;
362
363 if (dscp) {
364 /* dscp is the upper 6 bits of TOS IP header field, RFC 2474 */
365 tos = (dscp & 0x3f) << 2;
366 res = setsockopt(socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
367 }
368
369 return res;
370}
#define LOCALSTATEDIR
Definition config.h:361
@ 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:225
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
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:291
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:358
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