Bug Summary

File:libglusterfs/src/common-utils.c
Location:line 1655, column 14
Description:Null pointer passed as an argument to a 'nonnull' parameter

Annotated Source Code

1/*
2 Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9*/
10
11#ifndef _CONFIG_H
12#define _CONFIG_H
13#include "config.h"
14#endif
15
16#ifdef HAVE_BACKTRACE1
17#include <execinfo.h>
18#endif
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <ctype.h>
24#include <errno(*__errno_location ()).h>
25#include <limits.h>
26#include <unistd.h>
27#include <time.h>
28#include <locale.h>
29#include <sys/socket.h>
30#include <sys/wait.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <signal.h>
34#include <stdlib.h>
35
36#if defined GF_BSD_HOST_OS || defined GF_DARWIN_HOST_OS
37#include <sys/sysctl.h>
38#endif
39
40#include "logging.h"
41#include "common-utils.h"
42#include "revision.h"
43#include "glusterfs.h"
44#include "stack.h"
45#include "globals.h"
46#include "lkowner.h"
47#include "syscall.h"
48
49#ifndef AI_ADDRCONFIG0x0020
50#define AI_ADDRCONFIG0x0020 0
51#endif /* AI_ADDRCONFIG */
52
53typedef int32_t (*rw_op_t)(int32_t fd, char *buf, int32_t size);
54typedef int32_t (*rwv_op_t)(int32_t fd, const struct iovec *buf, int32_t size);
55
56struct dnscache6 {
57 struct addrinfo *first;
58 struct addrinfo *next;
59};
60
61
62/* works similar to mkdir(1) -p.
63 */
64int
65mkdir_p (char *path, mode_t mode, gf_boolean_t allow_symlinks)
66{
67 int i = 0;
68 int ret = -1;
69 char dir[PATH_MAX4096] = {0,};
70 struct stat stbuf = {0,};
71
72 strcpy (dir, path);
73 i = (dir[0] == '/')? 1: 0;
74 do {
75 if (path[i] != '/' && path[i] != '\0')
76 continue;
77
78 dir[i] = '\0';
79 ret = mkdir (dir, mode);
80 if (ret && errno(*__errno_location ()) != EEXIST17) {
81 gf_log ("", GF_LOG_ERROR, "Failed due to reason %s",do { do { if (0) printf ("Failed due to reason %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("", "common-utils.c"
, __FUNCTION__, 82, GF_LOG_ERROR, "Failed due to reason %s", strerror
((*__errno_location ()))); } while (0)
82 strerror (errno))do { do { if (0) printf ("Failed due to reason %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("", "common-utils.c"
, __FUNCTION__, 82, GF_LOG_ERROR, "Failed due to reason %s", strerror
((*__errno_location ()))); } while (0)
;
83 goto out;
84 }
85
86 if (ret && errno(*__errno_location ()) == EEXIST17 && !allow_symlinks) {
87 ret = lstat (dir, &stbuf);
88 if (ret)
89 goto out;
90
91 if (S_ISLNK (stbuf.st_mode)((((stbuf.st_mode)) & 0170000) == (0120000))) {
92 ret = -1;
93 gf_log ("", GF_LOG_ERROR, "%s is a symlink",do { do { if (0) printf ("%s is a symlink", dir); } while (0)
; _gf_log ("", "common-utils.c", __FUNCTION__, 94, GF_LOG_ERROR
, "%s is a symlink", dir); } while (0)
94 dir)do { do { if (0) printf ("%s is a symlink", dir); } while (0)
; _gf_log ("", "common-utils.c", __FUNCTION__, 94, GF_LOG_ERROR
, "%s is a symlink", dir); } while (0)
;
95 goto out;
96 }
97 }
98 dir[i] = '/';
99
100 } while (path[i++] != '\0');
101
102 ret = stat (dir, &stbuf);
103 if (ret || !S_ISDIR (stbuf.st_mode)((((stbuf.st_mode)) & 0170000) == (0040000))) {
104 ret = -1;
105 gf_log ("", GF_LOG_ERROR, "Failed to create directory, "do { do { if (0) printf ("Failed to create directory, " "possibly some of the components were not directories"
); } while (0); _gf_log ("", "common-utils.c", __FUNCTION__, 106
, GF_LOG_ERROR, "Failed to create directory, " "possibly some of the components were not directories"
); } while (0)
106 "possibly some of the components were not directories")do { do { if (0) printf ("Failed to create directory, " "possibly some of the components were not directories"
); } while (0); _gf_log ("", "common-utils.c", __FUNCTION__, 106
, GF_LOG_ERROR, "Failed to create directory, " "possibly some of the components were not directories"
); } while (0)
;
107 goto out;
108 }
109
110 ret = 0;
111out:
112
113 return ret;
114}
115
116int
117gf_lstat_dir (const char *path, struct stat *stbuf_in)
118{
119 int ret = -1;
120 struct stat stbuf = {0,};
121
122 if (path == NULL((void*)0)) {
123 errno(*__errno_location ()) = EINVAL22;
124 goto out;
125 }
126
127 ret = sys_lstat (path, &stbuf);
128 if (ret)
129 goto out;
130
131 if (!S_ISDIR (stbuf.st_mode)((((stbuf.st_mode)) & 0170000) == (0040000))) {
132 errno(*__errno_location ()) = ENOTDIR20;
133 ret = -1;
134 goto out;
135 }
136 ret = 0;
137
138out:
139 if (!ret && stbuf_in)
140 *stbuf_in = stbuf;
141
142 return ret;
143}
144
145int
146log_base2 (unsigned long x)
147{
148 int val = 0;
149
150 while (x > 1) {
151 x /= 2;
152 val++;
153 }
154
155 return val;
156}
157
158
159int32_t
160gf_resolve_ip6 (const char *hostname,
161 uint16_t port,
162 int family,
163 void **dnscache,
164 struct addrinfo **addr_info)
165{
166 int32_t ret = 0;
167 struct addrinfo hints;
168 struct dnscache6 *cache = NULL((void*)0);
169 char service[NI_MAXSERV32], host[NI_MAXHOST1025];
170
171 if (!hostname) {
172 gf_log_callingfn ("resolver", GF_LOG_WARNING, "hostname is NULL")do { do { if (0) printf ("hostname is NULL"); } while (0); _gf_log_callingfn
("resolver", "common-utils.c", __FUNCTION__, 172, GF_LOG_WARNING
, "hostname is NULL"); } while (0)
;
173 return -1;
174 }
175
176 if (!*dnscache) {
177 *dnscache = GF_CALLOC (1, sizeof (struct dnscache6),__gf_calloc (1, sizeof (struct dnscache6), gf_common_mt_dnscache6
)
178 gf_common_mt_dnscache6)__gf_calloc (1, sizeof (struct dnscache6), gf_common_mt_dnscache6
)
;
179 if (!*dnscache)
180 return -1;
181 }
182
183 cache = *dnscache;
184 if (cache->first && !cache->next) {
185 freeaddrinfo(cache->first);
186 cache->first = cache->next = NULL((void*)0);
187 gf_log ("resolver", GF_LOG_TRACE,do { do { if (0) printf ("flushing DNS cache"); } while (0); _gf_log
("resolver", "common-utils.c", __FUNCTION__, 188, GF_LOG_TRACE
, "flushing DNS cache"); } while (0)
188 "flushing DNS cache")do { do { if (0) printf ("flushing DNS cache"); } while (0); _gf_log
("resolver", "common-utils.c", __FUNCTION__, 188, GF_LOG_TRACE
, "flushing DNS cache"); } while (0)
;
189 }
190
191 if (!cache->first) {
192 char *port_str = NULL((void*)0);
193 gf_log ("resolver", GF_LOG_TRACE,do { do { if (0) printf ("DNS cache not present, freshly probing hostname: %s"
, hostname); } while (0); _gf_log ("resolver", "common-utils.c"
, __FUNCTION__, 195, GF_LOG_TRACE, "DNS cache not present, freshly probing hostname: %s"
, hostname); } while (0)
194 "DNS cache not present, freshly probing hostname: %s",do { do { if (0) printf ("DNS cache not present, freshly probing hostname: %s"
, hostname); } while (0); _gf_log ("resolver", "common-utils.c"
, __FUNCTION__, 195, GF_LOG_TRACE, "DNS cache not present, freshly probing hostname: %s"
, hostname); } while (0)
195 hostname)do { do { if (0) printf ("DNS cache not present, freshly probing hostname: %s"
, hostname); } while (0); _gf_log ("resolver", "common-utils.c"
, __FUNCTION__, 195, GF_LOG_TRACE, "DNS cache not present, freshly probing hostname: %s"
, hostname); } while (0)
;
196
197 memset(&hints, 0, sizeof(hints));
198 hints.ai_family = family;
199 hints.ai_socktype = SOCK_STREAMSOCK_STREAM;
200#ifndef __NetBSD__
201 hints.ai_flags = AI_ADDRCONFIG0x0020;
202#endif
203
204 ret = gf_asprintf (&port_str, "%d", port);
205 if (-1 == ret) {
206 gf_log ("resolver", GF_LOG_ERROR, "asprintf failed")do { do { if (0) printf ("asprintf failed"); } while (0); _gf_log
("resolver", "common-utils.c", __FUNCTION__, 206, GF_LOG_ERROR
, "asprintf failed"); } while (0)
;
207 return -1;
208 }
209 if ((ret = getaddrinfo(hostname, port_str, &hints, &cache->first)) != 0) {
210 gf_log ("resolver", GF_LOG_ERROR,do { do { if (0) printf ("getaddrinfo failed (%s)", gai_strerror
(ret)); } while (0); _gf_log ("resolver", "common-utils.c", __FUNCTION__
, 211, GF_LOG_ERROR, "getaddrinfo failed (%s)", gai_strerror (
ret)); } while (0)
211 "getaddrinfo failed (%s)", gai_strerror (ret))do { do { if (0) printf ("getaddrinfo failed (%s)", gai_strerror
(ret)); } while (0); _gf_log ("resolver", "common-utils.c", __FUNCTION__
, 211, GF_LOG_ERROR, "getaddrinfo failed (%s)", gai_strerror (
ret)); } while (0)
;
212
213 GF_FREE (*dnscache)__gf_free (*dnscache);
214 *dnscache = NULL((void*)0);
215 GF_FREE (port_str)__gf_free (port_str);
216 return -1;
217 }
218 GF_FREE (port_str)__gf_free (port_str);
219
220 cache->next = cache->first;
221 }
222
223 if (cache->next) {
224 ret = getnameinfo((struct sockaddr *)cache->next->ai_addr,
225 cache->next->ai_addrlen,
226 host, sizeof (host),
227 service, sizeof (service),
228 NI_NUMERICHOST1);
229 if (ret != 0) {
230 gf_log ("resolver", GF_LOG_ERROR,do { do { if (0) printf ("getnameinfo failed (%s)", gai_strerror
(ret)); } while (0); _gf_log ("resolver", "common-utils.c", __FUNCTION__
, 231, GF_LOG_ERROR, "getnameinfo failed (%s)", gai_strerror (
ret)); } while (0)
231 "getnameinfo failed (%s)", gai_strerror (ret))do { do { if (0) printf ("getnameinfo failed (%s)", gai_strerror
(ret)); } while (0); _gf_log ("resolver", "common-utils.c", __FUNCTION__
, 231, GF_LOG_ERROR, "getnameinfo failed (%s)", gai_strerror (
ret)); } while (0)
;
232 goto err;
233 }
234
235 gf_log ("resolver", GF_LOG_DEBUG,do { do { if (0) printf ("returning ip-%s (port-%s) for hostname: %s and port: %d"
, host, service, hostname, port); } while (0); _gf_log ("resolver"
, "common-utils.c", __FUNCTION__, 237, GF_LOG_DEBUG, "returning ip-%s (port-%s) for hostname: %s and port: %d"
, host, service, hostname, port); } while (0)
236 "returning ip-%s (port-%s) for hostname: %s and port: %d",do { do { if (0) printf ("returning ip-%s (port-%s) for hostname: %s and port: %d"
, host, service, hostname, port); } while (0); _gf_log ("resolver"
, "common-utils.c", __FUNCTION__, 237, GF_LOG_DEBUG, "returning ip-%s (port-%s) for hostname: %s and port: %d"
, host, service, hostname, port); } while (0)
237 host, service, hostname, port)do { do { if (0) printf ("returning ip-%s (port-%s) for hostname: %s and port: %d"
, host, service, hostname, port); } while (0); _gf_log ("resolver"
, "common-utils.c", __FUNCTION__, 237, GF_LOG_DEBUG, "returning ip-%s (port-%s) for hostname: %s and port: %d"
, host, service, hostname, port); } while (0)
;
238
239 *addr_info = cache->next;
240 }
241
242 if (cache->next)
243 cache->next = cache->next->ai_next;
244 if (cache->next) {
245 ret = getnameinfo((struct sockaddr *)cache->next->ai_addr,
246 cache->next->ai_addrlen,
247 host, sizeof (host),
248 service, sizeof (service),
249 NI_NUMERICHOST1);
250 if (ret != 0) {
251 gf_log ("resolver", GF_LOG_ERROR,do { do { if (0) printf ("getnameinfo failed (%s)", gai_strerror
(ret)); } while (0); _gf_log ("resolver", "common-utils.c", __FUNCTION__
, 252, GF_LOG_ERROR, "getnameinfo failed (%s)", gai_strerror (
ret)); } while (0)
252 "getnameinfo failed (%s)", gai_strerror (ret))do { do { if (0) printf ("getnameinfo failed (%s)", gai_strerror
(ret)); } while (0); _gf_log ("resolver", "common-utils.c", __FUNCTION__
, 252, GF_LOG_ERROR, "getnameinfo failed (%s)", gai_strerror (
ret)); } while (0)
;
253 goto err;
254 }
255
256 gf_log ("resolver", GF_LOG_DEBUG,do { do { if (0) printf ("next DNS query will return: ip-%s port-%s"
, host, service); } while (0); _gf_log ("resolver", "common-utils.c"
, __FUNCTION__, 257, GF_LOG_DEBUG, "next DNS query will return: ip-%s port-%s"
, host, service); } while (0)
257 "next DNS query will return: ip-%s port-%s", host, service)do { do { if (0) printf ("next DNS query will return: ip-%s port-%s"
, host, service); } while (0); _gf_log ("resolver", "common-utils.c"
, __FUNCTION__, 257, GF_LOG_DEBUG, "next DNS query will return: ip-%s port-%s"
, host, service); } while (0)
;
258 }
259
260 return 0;
261
262err:
263 freeaddrinfo (cache->first);
264 cache->first = cache->next = NULL((void*)0);
265 GF_FREE (cache)__gf_free (cache);
266 *dnscache = NULL((void*)0);
267 return -1;
268}
269
270
271struct xldump {
272 int lineno;
273 FILE *logfp;
274};
275
276
277static int
278nprintf (struct xldump *dump, const char *fmt, ...)
279{
280 va_list ap;
281 int ret = 0;
282
283
284 ret += fprintf (dump->logfp, "%3d: ", ++dump->lineno);
285
286 va_start (ap, fmt)__builtin_va_start(ap, fmt);
287 ret += vfprintf (dump->logfp, fmt, ap);
288 va_end (ap)__builtin_va_end(ap);
289
290 ret += fprintf (dump->logfp, "\n");
291
292 return ret;
293}
294
295
296static int
297xldump_options (dict_t *this, char *key, data_t *value, void *d)
298{
299 nprintf (d, " option %s %s", key, value->data);
300 return 0;
301}
302
303
304static void
305xldump_subvolumes (xlator_t *this, void *d)
306{
307 xlator_list_t *subv = NULL((void*)0);
308 int len = 0;
309 char *subvstr = NULL((void*)0);
310
311 subv = this->children;
312 if (!this->children)
313 return;
314
315 for (subv = this->children; subv; subv = subv->next)
316 len += (strlen (subv->xlator->name) + 1);
317
318 subvstr = GF_CALLOC (1, len, gf_common_mt_strdup)__gf_calloc (1, len, gf_common_mt_strdup);
319
320 len = 0;
321 for (subv = this->children; subv; subv= subv->next)
322 len += sprintf (subvstr + len, "%s%s", subv->xlator->name,
323 subv->next ? " " : "");
324
325 nprintf (d, " subvolumes %s", subvstr);
326
327 GF_FREE (subvstr)__gf_free (subvstr);
328}
329
330
331static void
332xldump (xlator_t *each, void *d)
333{
334 nprintf (d, "volume %s", each->name);
335 nprintf (d, " type %s", each->type);
336 dict_foreach (each->options, xldump_options, d);
337
338 xldump_subvolumes (each, d);
339
340 nprintf (d, "end-volume");
341 nprintf (d, "");
342}
343
344
345void
346gf_log_dump_graph (FILE *specfp, glusterfs_graph_t *graph)
347{
348 glusterfs_ctx_t *ctx;
349 struct xldump xld = {0, };
350
351
352 ctx = THIS(*__glusterfs_this_location())->ctx;
353 xld.logfp = ctx->log.gf_log_logfile;
354
355 fprintf (ctx->log.gf_log_logfile, "Final graph:\n");
356 fprintf (ctx->log.gf_log_logfile,
357 "+---------------------------------------"
358 "---------------------------------------+\n");
359
360 xlator_foreach_depth_first (graph->top, xldump, &xld);
361
362 fprintf (ctx->log.gf_log_logfile,
363 "+---------------------------------------"
364 "---------------------------------------+\n");
365 fflush (ctx->log.gf_log_logfile);
366}
367
368static void
369gf_dump_config_flags (int fd)
370{
371 int ret = 0;
372
373 ret = write (fd, "configuration details:\n", 23);
374 if (ret == -1)
375 goto out;
376
377/* have argp */
378#ifdef HAVE_ARGP1
379 ret = write (fd, "argp 1\n", 7);
380 if (ret == -1)
381 goto out;
382#endif
383
384/* ifdef if found backtrace */
385#ifdef HAVE_BACKTRACE1
386 ret = write (fd, "backtrace 1\n", 12);
387 if (ret == -1)
388 goto out;
389#endif
390
391/* Berkeley-DB version has cursor->get() */
392#ifdef HAVE_BDB_CURSOR_GET
393 ret = write (fd, "bdb->cursor->get 1\n", 19);
394 if (ret == -1)
395 goto out;
396#endif
397
398/* Define to 1 if you have the <db.h> header file. */
399#ifdef HAVE_DB_H
400 ret = write (fd, "db.h 1\n", 7);
401 if (ret == -1)
402 goto out;
403#endif
404
405/* Define to 1 if you have the <dlfcn.h> header file. */
406#ifdef HAVE_DLFCN_H1
407 ret = write (fd, "dlfcn 1\n", 8);
408 if (ret == -1)
409 goto out;
410#endif
411
412/* define if fdatasync exists */
413#ifdef HAVE_FDATASYNC1
414 ret = write (fd, "fdatasync 1\n", 12);
415 if (ret == -1)
416 goto out;
417#endif
418
419/* Define to 1 if you have the `pthread' library (-lpthread). */
420#ifdef HAVE_LIBPTHREAD1
421 ret = write (fd, "libpthread 1\n", 13);
422 if (ret == -1)
423 goto out;
424#endif
425
426/* define if llistxattr exists */
427#ifdef HAVE_LLISTXATTR1
428 ret = write (fd, "llistxattr 1\n", 13);
429 if (ret == -1)
430 goto out;
431#endif
432
433/* define if found setfsuid setfsgid */
434#ifdef HAVE_SET_FSID1
435 ret = write (fd, "setfsid 1\n", 10);
436 if (ret == -1)
437 goto out;
438#endif
439
440/* define if found spinlock */
441#ifdef HAVE_SPINLOCK1
442 ret = write (fd, "spinlock 1\n", 11);
443 if (ret == -1)
444 goto out;
445#endif
446
447/* Define to 1 if you have the <sys/epoll.h> header file. */
448#ifdef HAVE_SYS_EPOLL_H1
449 ret = write (fd, "epoll.h 1\n", 10);
450 if (ret == -1)
451 goto out;
452#endif
453
454/* Define to 1 if you have the <sys/extattr.h> header file. */
455#ifdef HAVE_SYS_EXTATTR_H
456 ret = write (fd, "extattr.h 1\n", 12);
457 if (ret == -1)
458 goto out;
459#endif
460
461/* Define to 1 if you have the <sys/xattr.h> header file. */
462#ifdef HAVE_SYS_XATTR_H1
463 ret = write (fd, "xattr.h 1\n", 10);
464 if (ret == -1)
465 goto out;
466#endif
467
468/* define if found st_atim.tv_nsec */
469#ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC1
470 ret = write (fd, "st_atim.tv_nsec 1\n", 18);
471 if (ret == -1)
472 goto out;
473#endif
474
475/* define if found st_atimespec.tv_nsec */
476#ifdef HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
477 ret = write (fd, "st_atimespec.tv_nsec 1\n",23);
478 if (ret == -1)
479 goto out;
480#endif
481
482/* Define to the full name and version of this package. */
483#ifdef PACKAGE_STRING"glusterfs 3git"
484 {
485 char msg[128];
486 sprintf (msg, "package-string: %s\n", PACKAGE_STRING"glusterfs 3git");
487 ret = write (fd, msg, strlen (msg));
488 if (ret == -1)
489 goto out;
490 }
491#endif
492
493out:
494 return;
495}
496
497/* Obtain a backtrace and print it to stdout. */
498/* TODO: It looks like backtrace_symbols allocates memory,
499 it may be problem because mostly memory allocation/free causes 'sigsegv' */
500
501void
502gf_print_trace (int32_t signum, glusterfs_ctx_t *ctx)
503{
504 char msg[1024] = {0,};
505 char timestr[64] = {0,};
506 int ret = 0;
507 int fd = 0;
508
509 fd = fileno (ctx->log.gf_log_logfile);
510
511 /* Now every gf_log call will just write to a buffer and when the
512 * buffer becomes full, its written to the log-file. Suppose the process
513 * crashes and prints the backtrace in the log-file, then the previous
514 * log information will still be in the buffer itself. So flush the
515 * contents of the buffer to the log file before printing the backtrace
516 * which helps in debugging.
517 */
518 fflush (ctx->log.gf_log_logfile);
519 /* Pending frames, (if any), list them in order */
520 ret = write (fd, "pending frames:\n", 16);
521 if (ret < 0)
522 goto out;
523
524 {
525 struct list_head *trav = ((call_pool_t *)ctx->pool)->all_frames.next;
526 while (trav != (&((call_pool_t *)ctx->pool)->all_frames)) {
527 call_frame_t *tmp = (call_frame_t *)(&((call_stack_t *)trav)->frames);
528 if (tmp->root->type == GF_OP_TYPE_FOP)
529 sprintf (msg,"frame : type(%d) op(%s)\n",
530 tmp->root->type,
531 gf_fop_list[tmp->root->op]);
532 else
533 sprintf (msg,"frame : type(%d) op(%d)\n",
534 tmp->root->type,
535 tmp->root->op);
536
537 ret = write (fd, msg, strlen (msg));
538 if (ret < 0)
539 goto out;
540
541 trav = trav->next;
542 }
543 ret = write (fd, "\n", 1);
544 if (ret < 0)
545 goto out;
546 }
547
548 sprintf (msg, "patchset: %s\n", GLUSTERFS_REPOSITORY_REVISION"git://git.gluster.com/glusterfs.git");
549 ret = write (fd, msg, strlen (msg));
550 if (ret < 0)
551 goto out;
552
553 sprintf (msg, "signal received: %d\n", signum);
554 ret = write (fd, msg, strlen (msg));
555 if (ret < 0)
556 goto out;
557
558 {
559 /* Dump the timestamp of the crash too, so the previous logs
560 can be related */
561 gf_time_fmt (timestr, sizeof timestr, time (NULL((void*)0)), gf_timefmt_FT);
562 ret = write (fd, "time of crash: ", 15);
563 if (ret < 0)
564 goto out;
565 ret = write (fd, timestr, strlen (timestr));
566 if (ret < 0)
567 goto out;
568 }
569
570 gf_dump_config_flags (fd);
571#if HAVE_BACKTRACE1
572 /* Print 'backtrace' */
573 {
574 void *array[200];
575 size_t size;
576
577 size = backtrace (array, 200);
578 backtrace_symbols_fd (&array[1], size-1, fd);
579 sprintf (msg, "---------\n");
580 ret = write (fd, msg, strlen (msg));
581 if (ret < 0)
582 goto out;
583 }
584#endif /* HAVE_BACKTRACE */
585
586out:
587 /* Send a signal to terminate the process */
588 signal (signum, SIG_DFL((__sighandler_t) 0));
589 raise (signum);
590}
591
592void
593trap (void)
594{
595
596}
597
598char *
599gf_trim (char *string)
600{
601 register char *s, *t;
602
603 if (string == NULL((void*)0)) {
604 return NULL((void*)0);
605 }
606
607 for (s = string; isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
; s++)
608 ;
609
610 if (*s == 0)
611 return s;
612
613 t = s + strlen (s) - 1;
614 while (t > s && isspace (*t)((*__ctype_b_loc ())[(int) ((*t))] & (unsigned short int)
_ISspace)
)
615 t--;
616 *++t = '\0';
617
618 return s;
619}
620
621int
622gf_strsplit (const char *str, const char *delim,
623 char ***tokens, int *token_count)
624{
625 char *_running = NULL((void*)0);
626 char *running = NULL((void*)0);
627 char *token = NULL((void*)0);
628 char **token_list = NULL((void*)0);
629 int count = 0;
630 int i = 0;
631 int j = 0;
632
633 if (str == NULL((void*)0) || delim == NULL((void*)0) || tokens == NULL((void*)0) || token_count == NULL((void*)0)) {
634 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 634, GF_LOG_WARNING, "argument invalid"); } while (0)
;
635 return -1;
636 }
637
638 _running = gf_strdup (str);
639 if (_running == NULL((void*)0))
640 return -1;
641
642 running = _running;
643
644 while ((token = strsep (&running, delim)) != NULL((void*)0)) {
645 if (token[0] != '\0')
646 count++;
647 }
648 GF_FREE (_running)__gf_free (_running);
649
650 _running = gf_strdup (str);
651 if (_running == NULL((void*)0))
652 return -1;
653
654 running = _running;
655
656 if ((token_list = GF_CALLOC (count, sizeof (char *),__gf_calloc (count, sizeof (char *), gf_common_mt_char)
657 gf_common_mt_char)__gf_calloc (count, sizeof (char *), gf_common_mt_char)) == NULL((void*)0)) {
658 GF_FREE (_running)__gf_free (_running);
659 return -1;
660 }
661
662 while ((token = strsep (&running, delim)) != NULL((void*)0)) {
663 if (token[0] == '\0')
664 continue;
665
666 token_list[i] = gf_strdup (token);
667 if (token_list[i] == NULL((void*)0))
668 goto free_exit;
669 i++;
670 }
671
672 GF_FREE (_running)__gf_free (_running);
673
674 *tokens = token_list;
675 *token_count = count;
676 return 0;
677
678free_exit:
679 GF_FREE (_running)__gf_free (_running);
680 for (j = 0; j < i; j++)
681 GF_FREE (token_list[j])__gf_free (token_list[j]);
682
683 GF_FREE (token_list)__gf_free (token_list);
684 return -1;
685}
686
687int
688gf_strstr (const char *str, const char *delim, const char *match)
689{
690 char *tmp = NULL((void*)0);
691 char *save_ptr = NULL((void*)0);
692 char *tmp_str = NULL((void*)0);
693
694 int ret = 0;
695
696 tmp_str = strdup (str);
697
698 if (str == NULL((void*)0) || delim == NULL((void*)0) || match == NULL((void*)0) || tmp_str == NULL((void*)0)) {
699 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 699, GF_LOG_WARNING, "argument invalid"); } while (0)
;
700 ret = -1;
701 goto out;
702 }
703
704
705 tmp = strtok_r (tmp_str, delim, &save_ptr);
706
707 while (tmp) {
708 ret = strcmp (tmp, match);
709
710 if (ret == 0)
711 break;
712
713 tmp = strtok_r (NULL((void*)0), delim, &save_ptr);
714 }
715
716out:
717 free (tmp_str);
718
719 return ret;
720
721}
722
723int
724gf_volume_name_validate (const char *volume_name)
725{
726 const char *vname = NULL((void*)0);
727
728 if (volume_name == NULL((void*)0)) {
729 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 729, GF_LOG_WARNING, "argument invalid"); } while (0)
;
730 return -1;
731 }
732
733 if (!isalpha (volume_name[0])((*__ctype_b_loc ())[(int) ((volume_name[0]))] & (unsigned
short int) _ISalpha)
)
734 return 1;
735
736 for (vname = &volume_name[1]; *vname != '\0'; vname++) {
737 if (!(isalnum (*vname)((*__ctype_b_loc ())[(int) ((*vname))] & (unsigned short int
) _ISalnum)
|| *vname == '_'))
738 return 1;
739 }
740
741 return 0;
742}
743
744
745int
746gf_string2time (const char *str, uint32_t *n)
747{
748 unsigned long value = 0;
749 char *tail = NULL((void*)0);
750 int old_errno = 0;
751 const char *s = NULL((void*)0);
752
753 if (str == NULL((void*)0) || n == NULL((void*)0)) {
754 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 754, GF_LOG_WARNING, "argument invalid"); } while (0)
;
755 errno(*__errno_location ()) = EINVAL22;
756 return -1;
757 }
758
759 for (s = str; *s != '\0'; s++) {
760 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
761 continue;
762 if (*s == '-')
763 return -1;
764 break;
765 }
766
767 old_errno = errno(*__errno_location ());
768 errno(*__errno_location ()) = 0;
769 value = strtol (str, &tail, 0);
770 if (str == tail)
771 errno(*__errno_location ()) = EINVAL22;
772
773 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
774 return -1;
775
776 if (errno(*__errno_location ()) == 0)
777 errno(*__errno_location ()) = old_errno;
778
779 if (!((tail[0] == '\0') ||
780 ((tail[0] == 's') && (tail[1] == '\0')) ||
781 ((tail[0] == 's') && (tail[1] == 'e') &&
782 (tail[2] == 'c') && (tail[3] == '\0'))))
783 return -1;
784
785 *n = value;
786
787 return 0;
788}
789
790int
791gf_string2percent (const char *str, double *n)
792{
793 double value = 0;
794 char *tail = NULL((void*)0);
795 int old_errno = 0;
796 const char *s = NULL((void*)0);
797
798 if (str == NULL((void*)0) || n == NULL((void*)0)) {
799 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 799, GF_LOG_WARNING, "argument invalid"); } while (0)
;
800 errno(*__errno_location ()) = EINVAL22;
801 return -1;
802 }
803
804 for (s = str; *s != '\0'; s++) {
805 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
806 continue;
807 if (*s == '-')
808 return -1;
809 break;
810 }
811
812 old_errno = errno(*__errno_location ());
813 errno(*__errno_location ()) = 0;
814 value = strtod (str, &tail);
815 if (str == tail)
816 errno(*__errno_location ()) = EINVAL22;
817
818 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
819 return -1;
820
821 if (errno(*__errno_location ()) == 0)
822 errno(*__errno_location ()) = old_errno;
823
824 if (!((tail[0] == '\0') ||
825 ((tail[0] == '%') && (tail[1] == '\0'))))
826 return -1;
827
828 *n = value;
829
830 return 0;
831}
832
833
834static int
835_gf_string2long (const char *str, long *n, int base)
836{
837 long value = 0;
838 char *tail = NULL((void*)0);
839 int old_errno = 0;
840
841 if (str == NULL((void*)0) || n == NULL((void*)0)) {
842 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 842, GF_LOG_WARNING, "argument invalid"); } while (0)
;
843 errno(*__errno_location ()) = EINVAL22;
844 return -1;
845 }
846
847 old_errno = errno(*__errno_location ());
848 errno(*__errno_location ()) = 0;
849 value = strtol (str, &tail, base);
850 if (str == tail)
851 errno(*__errno_location ()) = EINVAL22;
852
853 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
854 return -1;
855
856 if (errno(*__errno_location ()) == 0)
857 errno(*__errno_location ()) = old_errno;
858
859 if (tail[0] != '\0')
860 return -1;
861
862 *n = value;
863
864 return 0;
865}
866
867static int
868_gf_string2ulong (const char *str, unsigned long *n, int base)
869{
870 unsigned long value = 0;
871 char *tail = NULL((void*)0);
872 int old_errno = 0;
873 const char *s = NULL((void*)0);
874
875 if (str == NULL((void*)0) || n == NULL((void*)0)) {
876 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 876, GF_LOG_WARNING, "argument invalid"); } while (0)
;
877 errno(*__errno_location ()) = EINVAL22;
878 return -1;
879 }
880
881 for (s = str; *s != '\0'; s++) {
882 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
883 continue;
884 if (*s == '-')
885 return -1;
886 break;
887 }
888
889 old_errno = errno(*__errno_location ());
890 errno(*__errno_location ()) = 0;
891 value = strtoul (str, &tail, base);
892 if (str == tail)
893 errno(*__errno_location ()) = EINVAL22;
894
895 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
896 return -1;
897
898 if (errno(*__errno_location ()) == 0)
899 errno(*__errno_location ()) = old_errno;
900
901 if (tail[0] != '\0')
902 return -1;
903
904 *n = value;
905
906 return 0;
907}
908
909static int
910_gf_string2uint (const char *str, unsigned int *n, int base)
911{
912 unsigned long value = 0;
913 char *tail = NULL((void*)0);
914 int old_errno = 0;
915 const char *s = NULL((void*)0);
916
917 if (str == NULL((void*)0) || n == NULL((void*)0)) {
918 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 918, GF_LOG_WARNING, "argument invalid"); } while (0)
;
919 errno(*__errno_location ()) = EINVAL22;
920 return -1;
921 }
922
923 for (s = str; *s != '\0'; s++) {
924 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
925 continue;
926 if (*s == '-')
927 return -1;
928 break;
929 }
930
931 old_errno = errno(*__errno_location ());
932 errno(*__errno_location ()) = 0;
933 value = strtoul (str, &tail, base);
934 if (str == tail)
935 errno(*__errno_location ()) = EINVAL22;
936
937 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
938 return -1;
939
940 if (errno(*__errno_location ()) == 0)
941 errno(*__errno_location ()) = old_errno;
942
943 if (tail[0] != '\0')
944 return -1;
945
946 *n = (unsigned int)value;
947
948 return 0;
949}
950
951static int
952_gf_string2double (const char *str, double *n)
953{
954 double value = 0.0;
955 char *tail = NULL((void*)0);
956 int old_errno = 0;
957
958 if (str == NULL((void*)0) || n == NULL((void*)0)) {
959 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 959, GF_LOG_WARNING, "argument invalid"); } while (0)
;
960 errno(*__errno_location ()) = EINVAL22;
961 return -1;
962 }
963
964 old_errno = errno(*__errno_location ());
965 errno(*__errno_location ()) = 0;
966 value = strtod (str, &tail);
967 if (str == tail)
968 errno(*__errno_location ()) = EINVAL22;
969
970 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
971 return -1;
972
973 if (errno(*__errno_location ()) == 0)
974 errno(*__errno_location ()) = old_errno;
975
976 if (tail[0] != '\0')
977 return -1;
978
979 *n = value;
980
981 return 0;
982}
983
984static int
985_gf_string2longlong (const char *str, long long *n, int base)
986{
987 long long value = 0;
988 char *tail = NULL((void*)0);
989 int old_errno = 0;
990
991 if (str == NULL((void*)0) || n == NULL((void*)0)) {
992 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 992, GF_LOG_WARNING, "argument invalid"); } while (0)
;
993 errno(*__errno_location ()) = EINVAL22;
994 return -1;
995 }
996
997 old_errno = errno(*__errno_location ());
998 errno(*__errno_location ()) = 0;
999 value = strtoll (str, &tail, base);
1000 if (str == tail)
1001 errno(*__errno_location ()) = EINVAL22;
1002
1003 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
1004 return -1;
1005
1006 if (errno(*__errno_location ()) == 0)
1007 errno(*__errno_location ()) = old_errno;
1008
1009 if (tail[0] != '\0')
1010 return -1;
1011
1012 *n = value;
1013
1014 return 0;
1015}
1016
1017static int
1018_gf_string2ulonglong (const char *str, unsigned long long *n, int base)
1019{
1020 unsigned long long value = 0;
1021 char *tail = NULL((void*)0);
1022 int old_errno = 0;
1023 const char *s = NULL((void*)0);
1024
1025 if (str == NULL((void*)0) || n == NULL((void*)0)) {
1026 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 1026, GF_LOG_WARNING, "argument invalid"); } while (0)
;
1027 errno(*__errno_location ()) = EINVAL22;
1028 return -1;
1029 }
1030
1031 for (s = str; *s != '\0'; s++) {
1032 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
1033 continue;
1034 if (*s == '-')
1035 return -1;
1036 break;
1037 }
1038
1039 old_errno = errno(*__errno_location ());
1040 errno(*__errno_location ()) = 0;
1041 value = strtoull (str, &tail, base);
1042 if (str == tail)
1043 errno(*__errno_location ()) = EINVAL22;
1044
1045 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
1046 return -1;
1047
1048 if (errno(*__errno_location ()) == 0)
1049 errno(*__errno_location ()) = old_errno;
1050
1051 if (tail[0] != '\0')
1052 return -1;
1053
1054 *n = value;
1055
1056 return 0;
1057}
1058
1059int
1060gf_string2long (const char *str, long *n)
1061{
1062 return _gf_string2long (str, n, 0);
1063}
1064
1065int
1066gf_string2ulong (const char *str, unsigned long *n)
1067{
1068 return _gf_string2ulong (str, n, 0);
1069}
1070
1071int
1072gf_string2int (const char *str, int *n)
1073{
1074 long l = 0;
1075 int ret = 0;
1076
1077 ret = _gf_string2long (str, &l, 0);
1078
1079 *n = l;
1080 return ret;
1081}
1082
1083int
1084gf_string2uint (const char *str, unsigned int *n)
1085{
1086 return _gf_string2uint (str, n, 0);
1087}
1088
1089int
1090gf_string2double (const char *str, double *n)
1091{
1092 return _gf_string2double (str, n);
1093}
1094
1095int
1096gf_string2longlong (const char *str, long long *n)
1097{
1098 return _gf_string2longlong (str, n, 0);
1099}
1100
1101int
1102gf_string2ulonglong (const char *str, unsigned long long *n)
1103{
1104 return _gf_string2ulonglong (str, n, 0);
1105}
1106
1107int
1108gf_string2int8 (const char *str, int8_t *n)
1109{
1110 long l = 0L;
1111 int rv = 0;
1112
1113 rv = _gf_string2long (str, &l, 0);
1114 if (rv != 0)
1115 return rv;
1116
1117 if (l >= INT8_MIN(-128) && l <= INT8_MAX(127)) {
1118 *n = (int8_t) l;
1119 return 0;
1120 }
1121
1122 errno(*__errno_location ()) = ERANGE34;
1123 return -1;
1124}
1125
1126int
1127gf_string2int16 (const char *str, int16_t *n)
1128{
1129 long l = 0L;
1130 int rv = 0;
1131
1132 rv = _gf_string2long (str, &l, 0);
1133 if (rv != 0)
1134 return rv;
1135
1136 if (l >= INT16_MIN(-32767-1) && l <= INT16_MAX(32767)) {
1137 *n = (int16_t) l;
1138 return 0;
1139 }
1140
1141 errno(*__errno_location ()) = ERANGE34;
1142 return -1;
1143}
1144
1145int
1146gf_string2int32 (const char *str, int32_t *n)
1147{
1148 long l = 0L;
1149 int rv = 0;
1150
1151 rv = _gf_string2long (str, &l, 0);
1152 if (rv != 0)
1153 return rv;
1154
1155 if (l >= INT32_MIN(-2147483647-1) && l <= INT32_MAX(2147483647)) {
1156 *n = (int32_t) l;
1157 return 0;
1158 }
1159
1160 errno(*__errno_location ()) = ERANGE34;
1161 return -1;
1162}
1163
1164int
1165gf_string2int64 (const char *str, int64_t *n)
1166{
1167 long long l = 0LL;
1168 int rv = 0;
1169
1170 rv = _gf_string2longlong (str, &l, 0);
1171 if (rv != 0)
1172 return rv;
1173
1174 if (l >= INT64_MIN(-9223372036854775807LL -1) && l <= INT64_MAX(9223372036854775807LL)) {
1175 *n = (int64_t) l;
1176 return 0;
1177 }
1178
1179 errno(*__errno_location ()) = ERANGE34;
1180 return -1;
1181}
1182
1183int
1184gf_string2uint8 (const char *str, uint8_t *n)
1185{
1186 unsigned long l = 0L;
1187 int rv = 0;
1188
1189 rv = _gf_string2ulong (str, &l, 0);
1190 if (rv != 0)
1191 return rv;
1192
1193 if (l >= 0 && l <= UINT8_MAX(255)) {
1194 *n = (uint8_t) l;
1195 return 0;
1196 }
1197
1198 errno(*__errno_location ()) = ERANGE34;
1199 return -1;
1200}
1201
1202int
1203gf_string2uint16 (const char *str, uint16_t *n)
1204{
1205 unsigned long l = 0L;
1206 int rv = 0;
1207
1208 rv = _gf_string2ulong (str, &l, 0);
1209 if (rv != 0)
1210 return rv;
1211
1212 if (l >= 0 && l <= UINT16_MAX(65535)) {
1213 *n = (uint16_t) l;
1214 return 0;
1215 }
1216
1217 errno(*__errno_location ()) = ERANGE34;
1218 return -1;
1219}
1220
1221int
1222gf_string2uint32 (const char *str, uint32_t *n)
1223{
1224 unsigned long l = 0L;
1225 int rv = 0;
1226
1227 rv = _gf_string2ulong (str, &l, 0);
1228 if (rv != 0)
1229 return rv;
1230
1231 if (l >= 0 && l <= UINT32_MAX(4294967295U)) {
1232 *n = (uint32_t) l;
1233 return 0;
1234 }
1235
1236 errno(*__errno_location ()) = ERANGE34;
1237 return -1;
1238}
1239
1240int
1241gf_string2uint64 (const char *str, uint64_t *n)
1242{
1243 unsigned long long l = 0ULL;
1244 int rv = 0;
1245
1246 rv = _gf_string2ulonglong (str, &l, 0);
1247 if (rv != 0)
1248 return rv;
1249
1250 if (l >= 0 && l <= UINT64_MAX(18446744073709551615ULL)) {
1251 *n = (uint64_t) l;
1252 return 0;
1253 }
1254
1255 errno(*__errno_location ()) = ERANGE34;
1256 return -1;
1257}
1258
1259int
1260gf_string2ulong_base10 (const char *str, unsigned long *n)
1261{
1262 return _gf_string2ulong (str, n, 10);
1263}
1264
1265int
1266gf_string2uint_base10 (const char *str, unsigned int *n)
1267{
1268 return _gf_string2uint (str, n, 10);
1269}
1270
1271int
1272gf_string2uint8_base10 (const char *str, uint8_t *n)
1273{
1274 unsigned long l = 0L;
1275 int rv = 0;
1276
1277 rv = _gf_string2ulong (str, &l, 10);
1278 if (rv != 0)
1279 return rv;
1280
1281 if (l >= 0 && l <= UINT8_MAX(255)) {
1282 *n = (uint8_t) l;
1283 return 0;
1284 }
1285
1286 errno(*__errno_location ()) = ERANGE34;
1287 return -1;
1288}
1289
1290int
1291gf_string2uint16_base10 (const char *str, uint16_t *n)
1292{
1293 unsigned long l = 0L;
1294 int rv = 0;
1295
1296 rv = _gf_string2ulong (str, &l, 10);
1297 if (rv != 0)
1298 return rv;
1299
1300 if (l >= 0 && l <= UINT16_MAX(65535)) {
1301 *n = (uint16_t) l;
1302 return 0;
1303 }
1304
1305 errno(*__errno_location ()) = ERANGE34;
1306 return -1;
1307}
1308
1309int
1310gf_string2uint32_base10 (const char *str, uint32_t *n)
1311{
1312 unsigned long l = 0L;
1313 int rv = 0;
1314
1315 rv = _gf_string2ulong (str, &l, 10);
1316 if (rv != 0)
1317 return rv;
1318
1319 if (l >= 0 && l <= UINT32_MAX(4294967295U)) {
1320 *n = (uint32_t) l;
1321 return 0;
1322 }
1323
1324 errno(*__errno_location ()) = ERANGE34;
1325 return -1;
1326}
1327
1328int
1329gf_string2uint64_base10 (const char *str, uint64_t *n)
1330{
1331 unsigned long long l = 0ULL;
1332 int rv = 0;
1333
1334 rv = _gf_string2ulonglong (str, &l, 10);
1335 if (rv != 0)
1336 return rv;
1337
1338 if (l >= 0 && l <= UINT64_MAX(18446744073709551615ULL)) {
1339 *n = (uint64_t) l;
1340 return 0;
1341 }
1342
1343 errno(*__errno_location ()) = ERANGE34;
1344 return -1;
1345}
1346
1347char *
1348gf_uint64_2human_readable (uint64_t n)
1349{
1350 int ret = 0;
1351 char *str = NULL((void*)0);
1352
1353 if (n >= GF_UNIT_PB1125899906842624ULL) {
1354 ret = gf_asprintf (&str, "%.1lfPB", ((double) n)/GF_UNIT_PB1125899906842624ULL);
1355 if (ret < 0)
1356 goto err;
1357 } else if (n >= GF_UNIT_TB1099511627776ULL) {
1358 ret = gf_asprintf (&str, "%.1lfTB", ((double) n)/GF_UNIT_TB1099511627776ULL);
1359 if (ret < 0)
1360 goto err;
1361 } else if (n >= GF_UNIT_GB1073741824ULL) {
1362 ret = gf_asprintf (&str, "%.1lfGB", ((double) n)/GF_UNIT_GB1073741824ULL);
1363 if (ret < 0)
1364 goto err;
1365 } else if (n >= GF_UNIT_MB1048576ULL) {
1366 ret = gf_asprintf (&str, "%.1lfMB", ((double) n)/GF_UNIT_MB1048576ULL);
1367 if (ret < 0)
1368 goto err;
1369 } else if (n >= GF_UNIT_KB1024ULL) {
1370 ret = gf_asprintf (&str, "%.1lfKB", ((double) n)/GF_UNIT_KB1024ULL);
1371 if (ret < 0)
1372 goto err;
1373 } else {
1374 ret = gf_asprintf (&str, "%luBytes", n);
1375 if (ret < 0)
1376 goto err;
1377 }
1378 return str;
1379err:
1380 return NULL((void*)0);
1381}
1382
1383int
1384gf_string2bytesize (const char *str, uint64_t *n)
1385{
1386 double value = 0.0;
1387 char *tail = NULL((void*)0);
1388 int old_errno = 0;
1389 const char *s = NULL((void*)0);
1390
1391 if (str == NULL((void*)0) || n == NULL((void*)0)) {
1392 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 1392, GF_LOG_WARNING, "argument invalid"); } while (0)
;
1393 errno(*__errno_location ()) = EINVAL22;
1394 return -1;
1395 }
1396
1397 for (s = str; *s != '\0'; s++) {
1398 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
1399 continue;
1400 if (*s == '-')
1401 return -1;
1402 break;
1403 }
1404
1405 old_errno = errno(*__errno_location ());
1406 errno(*__errno_location ()) = 0;
1407 value = strtod (str, &tail);
1408 if (str == tail)
1409 errno(*__errno_location ()) = EINVAL22;
1410
1411 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
1412 return -1;
1413
1414 if (errno(*__errno_location ()) == 0)
1415 errno(*__errno_location ()) = old_errno;
1416
1417 if (tail[0] != '\0')
1418 {
1419 if (strcasecmp (tail, GF_UNIT_KB_STRING"KB") == 0)
1420 value *= GF_UNIT_KB1024ULL;
1421 else if (strcasecmp (tail, GF_UNIT_MB_STRING"MB") == 0)
1422 value *= GF_UNIT_MB1048576ULL;
1423 else if (strcasecmp (tail, GF_UNIT_GB_STRING"GB") == 0)
1424 value *= GF_UNIT_GB1073741824ULL;
1425 else if (strcasecmp (tail, GF_UNIT_TB_STRING"TB") == 0)
1426 value *= GF_UNIT_TB1099511627776ULL;
1427 else if (strcasecmp (tail, GF_UNIT_PB_STRING"PB") == 0)
1428 value *= GF_UNIT_PB1125899906842624ULL;
1429 else
1430 return -1;
1431 }
1432
1433 *n = (uint64_t) value;
1434
1435 return 0;
1436}
1437
1438int
1439gf_string2percent_or_bytesize (const char *str,
1440 uint64_t *n,
1441 gf_boolean_t *is_percent)
1442{
1443 double value = 0ULL;
1444 char *tail = NULL((void*)0);
1445 int old_errno = 0;
1446 const char *s = NULL((void*)0);
1447
1448 if (str == NULL((void*)0) || n == NULL((void*)0)) {
1449 gf_log_callingfn (THIS->name, GF_LOG_WARNING,do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 1450, GF_LOG_WARNING, "argument invalid"); } while (0)
1450 "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 1450, GF_LOG_WARNING, "argument invalid"); } while (0)
;
1451 errno(*__errno_location ()) = EINVAL22;
1452 return -1;
1453 }
1454
1455 for (s = str; *s != '\0'; s++) {
1456 if (isspace (*s)((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int)
_ISspace)
)
1457 continue;
1458 if (*s == '-')
1459 return -1;
1460 break;
1461 }
1462
1463 old_errno = errno(*__errno_location ());
1464 errno(*__errno_location ()) = 0;
1465 value = strtod (str, &tail);
1466 if (str == tail)
1467 errno(*__errno_location ()) = EINVAL22;
1468
1469 if (errno(*__errno_location ()) == ERANGE34 || errno(*__errno_location ()) == EINVAL22)
1470 return -1;
1471
1472 if (errno(*__errno_location ()) == 0)
1473 errno(*__errno_location ()) = old_errno;
1474
1475 if (tail[0] != '\0') {
1476 if (strcasecmp (tail, GF_UNIT_KB_STRING"KB") == 0)
1477 value *= GF_UNIT_KB1024ULL;
1478 else if (strcasecmp (tail, GF_UNIT_MB_STRING"MB") == 0)
1479 value *= GF_UNIT_MB1048576ULL;
1480 else if (strcasecmp (tail, GF_UNIT_GB_STRING"GB") == 0)
1481 value *= GF_UNIT_GB1073741824ULL;
1482 else if (strcasecmp (tail, GF_UNIT_TB_STRING"TB") == 0)
1483 value *= GF_UNIT_TB1099511627776ULL;
1484 else if (strcasecmp (tail, GF_UNIT_PB_STRING"PB") == 0)
1485 value *= GF_UNIT_PB1125899906842624ULL;
1486 else if (strcasecmp (tail, GF_UNIT_PERCENT_STRING"%") == 0)
1487 *is_percent = _gf_true;
1488 else
1489 return -1;
1490 }
1491
1492 *n = (uint64_t) value;
1493
1494 return 0;
1495}
1496
1497int64_t
1498gf_str_to_long_long (const char *number)
1499{
1500 int64_t unit = 1;
1501 int64_t ret = 0;
1502 char *endptr = NULL((void*)0) ;
1503 if (!number)
1504 return 0;
1505
1506 ret = strtoll (number, &endptr, 0);
1507
1508 if (endptr) {
1509 switch (*endptr) {
1510 case 'G':
1511 case 'g':
1512 if ((* (endptr + 1) == 'B') ||(* (endptr + 1) == 'b'))
1513 unit = 1024 * 1024 * 1024;
1514 break;
1515 case 'M':
1516 case 'm':
1517 if ((* (endptr + 1) == 'B') ||(* (endptr + 1) == 'b'))
1518 unit = 1024 * 1024;
1519 break;
1520 case 'K':
1521 case 'k':
1522 if ((* (endptr + 1) == 'B') ||(* (endptr + 1) == 'b'))
1523 unit = 1024;
1524 break;
1525 case '%':
1526 unit = 1;
1527 break;
1528 default:
1529 unit = 1;
1530 break;
1531 }
1532 }
1533 return ret * unit;
1534}
1535
1536int
1537gf_string2boolean (const char *str, gf_boolean_t *b)
1538{
1539 if (str == NULL((void*)0)) {
1540 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 1540, GF_LOG_WARNING, "argument invalid"); } while (0)
;
1541 return -1;
1542 }
1543
1544 if ((strcasecmp (str, "1") == 0) ||
1545 (strcasecmp (str, "on") == 0) ||
1546 (strcasecmp (str, "yes") == 0) ||
1547 (strcasecmp (str, "true") == 0) ||
1548 (strcasecmp (str, "enable") == 0)) {
1549 *b = _gf_true;
1550 return 0;
1551 }
1552
1553 if ((strcasecmp (str, "0") == 0) ||
1554 (strcasecmp (str, "off") == 0) ||
1555 (strcasecmp (str, "no") == 0) ||
1556 (strcasecmp (str, "false") == 0) ||
1557 (strcasecmp (str, "disable") == 0)) {
1558 *b = _gf_false;
1559 return 0;
1560 }
1561
1562 return -1;
1563}
1564
1565
1566int
1567gf_lockfd (int fd)
1568{
1569 struct gf_flock fl;
1570
1571 fl.l_type = F_WRLCK1;
1572 fl.l_whence = SEEK_SET0;
1573 fl.l_start = 0;
1574 fl.l_len = 0;
1575
1576 return fcntl (fd, F_SETLK13, &fl);
1577}
1578
1579
1580int
1581gf_unlockfd (int fd)
1582{
1583 struct gf_flock fl;
1584
1585 fl.l_type = F_UNLCK2;
1586 fl.l_whence = SEEK_SET0;
1587 fl.l_start = 0;
1588 fl.l_len = 0;
1589
1590 return fcntl (fd, F_SETLK13, &fl);
1591}
1592
1593static void
1594compute_checksum (char *buf, size_t size, uint32_t *checksum)
1595{
1596 int ret = -1;
1597 char *checksum_buf = NULL((void*)0);
1598
1599 checksum_buf = (char *)(checksum);
1600
1601 if (!(*checksum)) {
1602 checksum_buf [0] = 0xba;
1603 checksum_buf [1] = 0xbe;
1604 checksum_buf [2] = 0xb0;
1605 checksum_buf [3] = 0x0b;
1606 }
1607
1608 for (ret = 0; ret < (size - 4); ret += 4) {
1609 checksum_buf[0] ^= (buf[ret]);
1610 checksum_buf[1] ^= (buf[ret + 1] << 1) ;
1611 checksum_buf[2] ^= (buf[ret + 2] << 2);
1612 checksum_buf[3] ^= (buf[ret + 3] << 3);
1613 }
1614
1615 for (ret = 0; ret <= (size % 4); ret++) {
1616 checksum_buf[ret] ^= (buf[(size - 4) + ret] << ret);
1617 }
1618
1619 return;
1620}
1621
1622#define GF_CHECKSUM_BUF_SIZE1024 1024
1623
1624int
1625get_checksum_for_file (int fd, uint32_t *checksum)
1626{
1627 int ret = -1;
1628 char buf[GF_CHECKSUM_BUF_SIZE1024] = {0,};
1629
1630 /* goto first place */
1631 lseek (fd, 0L, SEEK_SET0);
1632 do {
1633 ret = read (fd, &buf, GF_CHECKSUM_BUF_SIZE1024);
1634 if (ret > 0)
1635 compute_checksum (buf, GF_CHECKSUM_BUF_SIZE1024,
1636 checksum);
1637 } while (ret > 0);
1638
1639 /* set it back */
1640 lseek (fd, 0L, SEEK_SET0);
1641
1642 return ret;
1643}
1644
1645
1646int
1647get_checksum_for_path (char *path, uint32_t *checksum)
1648{
1649 int ret = -1;
1650 int fd = -1;
1651
1652 GF_ASSERT (path)do { if (!(path)) { do { do { if (0) printf ("Assertion failed: "
"path"); } while (0); _gf_log_callingfn ("", "common-utils.c"
, __FUNCTION__, 1652, GF_LOG_ERROR, "Assertion failed: " "path"
); } while (0); } } while (0)
;
1
Within the expansion of the macro 'GF_ASSERT':
a
Assuming 'path' is null
1653 GF_ASSERT (checksum)do { if (!(checksum)) { do { do { if (0) printf ("Assertion failed: "
"checksum"); } while (0); _gf_log_callingfn ("", "common-utils.c"
, __FUNCTION__, 1653, GF_LOG_ERROR, "Assertion failed: " "checksum"
); } while (0); } } while (0)
;
1654
1655 fd = open (path, O_RDWR02);
2
Null pointer passed as an argument to a 'nonnull' parameter
1656
1657 if (fd == -1) {
1658 gf_log (THIS->name, GF_LOG_ERROR, "Unable to open %s, errno: %d",do { do { if (0) printf ("Unable to open %s, errno: %d", path
, (*__errno_location ())); } while (0); _gf_log ((*__glusterfs_this_location
())->name, "common-utils.c", __FUNCTION__, 1659, GF_LOG_ERROR
, "Unable to open %s, errno: %d", path, (*__errno_location ()
)); } while (0)
1659 path, errno)do { do { if (0) printf ("Unable to open %s, errno: %d", path
, (*__errno_location ())); } while (0); _gf_log ((*__glusterfs_this_location
())->name, "common-utils.c", __FUNCTION__, 1659, GF_LOG_ERROR
, "Unable to open %s, errno: %d", path, (*__errno_location ()
)); } while (0)
;
1660 goto out;
1661 }
1662
1663 ret = get_checksum_for_file (fd, checksum);
1664
1665out:
1666 if (fd != -1)
1667 close (fd);
1668
1669 return ret;
1670}
1671
1672char *
1673strtail (char *str, const char *pattern)
1674{
1675 int i = 0;
1676
1677 for (i = 0; str[i] == pattern[i] && str[i]; i++);
1678
1679 if (pattern[i] == '\0')
1680 return str + i;
1681
1682 return NULL((void*)0);
1683}
1684
1685void
1686skipwhite (char **s)
1687{
1688 while (isspace (**s)((*__ctype_b_loc ())[(int) ((**s))] & (unsigned short int
) _ISspace)
)
1689 (*s)++;
1690}
1691
1692char *
1693nwstrtail (char *str, char *pattern)
1694{
1695 for (;;) {
1696 skipwhite (&str);
1697 skipwhite (&pattern);
1698
1699 if (*str != *pattern || !*str)
1700 break;
1701
1702 str++;
1703 pattern++;
1704 }
1705
1706 return *pattern ? NULL((void*)0) : str;
1707}
1708
1709void
1710skipword (char **s)
1711{
1712 if (!*s)
1713 return;
1714
1715 skipwhite (s);
1716
1717 while (!isspace(**s)((*__ctype_b_loc ())[(int) ((**s))] & (unsigned short int
) _ISspace)
)
1718 (*s)++;
1719}
1720
1721char *
1722get_nth_word (const char *str, int n)
1723{
1724 char buf[4096] = {0};
1725 char *start = NULL((void*)0);
1726 char *word = NULL((void*)0);
1727 int i = 0;
1728 int word_len = 0;
1729 const char *end = NULL((void*)0);
1730
1731 if (!str)
1732 goto out;
1733
1734 snprintf (buf, sizeof (buf), "%s", str);
1735 start = buf;
1736
1737 for (i = 0; i < n-1; i++)
1738 skipword (&start);
1739
1740 skipwhite (&start);
1741 end = strpbrk ((const char *)start, " \t\n\0");
1742
1743 if (!end)
1744 goto out;
1745
1746 word_len = abs (end - start);
1747
1748 word = GF_CALLOC (1, word_len + 1, gf_common_mt_strdup)__gf_calloc (1, word_len + 1, gf_common_mt_strdup);
1749 if (!word)
1750 goto out;
1751
1752 strncpy (word, start, word_len);
1753 *(word + word_len) = '\0';
1754 out:
1755 return word;
1756}
1757
1758/* Syntax formed according to RFC 1912 (RFC 1123 & 952 are more restrictive) *
1759 <hname> ::= <gen-name>*["."<gen-name>] *
1760 <gen-name> ::= <let-or-digit> <[*[<let-or-digit-or-hyphen>]<let-or-digit>] */
1761char
1762valid_host_name (char *address, int length)
1763{
1764 int i = 0;
1765 int str_len = 0;
1766 char ret = 1;
1767 char *dup_addr = NULL((void*)0);
1768 char *temp_str = NULL((void*)0);
1769 char *save_ptr = NULL((void*)0);
1770
1771 if ((length > _POSIX_HOST_NAME_MAX255) || (length < 1)) {
1772 ret = 0;
1773 goto out;
1774 }
1775
1776 dup_addr = gf_strdup (address);
1777 if (!dup_addr) {
1778 ret = 0;
1779 goto out;
1780 }
1781
1782 if (!isalnum (dup_addr[length - 1])((*__ctype_b_loc ())[(int) ((dup_addr[length - 1]))] & (unsigned
short int) _ISalnum)
&& (dup_addr[length - 1] != '*')) {
1783 ret = 0;
1784 goto out;
1785 }
1786
1787 /* gen-name */
1788 temp_str = strtok_r (dup_addr, ".", &save_ptr);
1789 do {
1790 str_len = strlen (temp_str);
1791
1792 if (!isalnum (temp_str[0])((*__ctype_b_loc ())[(int) ((temp_str[0]))] & (unsigned short
int) _ISalnum)
||
1793 !isalnum (temp_str[str_len-1])((*__ctype_b_loc ())[(int) ((temp_str[str_len-1]))] & (unsigned
short int) _ISalnum)
) {
1794 ret = 0;
1795 goto out;
1796 }
1797 for (i = 1; i < str_len; i++) {
1798 if (!isalnum (temp_str[i])((*__ctype_b_loc ())[(int) ((temp_str[i]))] & (unsigned short
int) _ISalnum)
&& (temp_str[i] != '-')) {
1799 ret = 0;
1800 goto out;
1801 }
1802 }
1803 } while ((temp_str = strtok_r (NULL((void*)0), ".", &save_ptr)));
1804
1805out:
1806 GF_FREE (dup_addr)__gf_free (dup_addr);
1807 return ret;
1808}
1809
1810/* Matches all ipv4 address, if wildcard_acc is true '*' wildcard pattern for*
1811 subnets is considerd as valid strings as well */
1812char
1813valid_ipv4_address (char *address, int length, gf_boolean_t wildcard_acc)
1814{
1815 int octets = 0;
1816 int value = 0;
1817 char *tmp = NULL((void*)0), *ptr = NULL((void*)0), *prev = NULL((void*)0), *endptr = NULL((void*)0);
1818 char ret = 1;
1819 int is_wildcard = 0;
1820
1821 tmp = gf_strdup (address);
1822
1823 /*
1824 * To prevent cases where last character is '.' and which have
1825 * consecutive dots like ".." as strtok ignore consecutive
1826 * delimeters.
1827 */
1828 if (length <= 0 ||
1829 (strstr (address, "..")) ||
1830 (!isdigit (tmp[length - 1])((*__ctype_b_loc ())[(int) ((tmp[length - 1]))] & (unsigned
short int) _ISdigit)
&& (tmp[length - 1] != '*'))) {
1831 ret = 0;
1832 goto out;
1833 }
1834
1835 prev = tmp;
1836 prev = strtok_r (tmp, ".", &ptr);
1837
1838 while (prev != NULL((void*)0)) {
1839 octets++;
1840 if (wildcard_acc && !strcmp (prev, "*")) {
1841 is_wildcard = 1;
1842 } else {
1843 value = strtol (prev, &endptr, 10);
1844 if ((value > 255) || (value < 0) ||
1845 (endptr != NULL((void*)0) && *endptr != '\0')) {
1846 ret = 0;
1847 goto out;
1848 }
1849 }
1850 prev = strtok_r (NULL((void*)0), ".", &ptr);
1851 }
1852
1853 if ((octets > 4) || (octets < 4 && !is_wildcard)) {
1854 ret = 0;
1855 }
1856
1857out:
1858 GF_FREE (tmp)__gf_free (tmp);
1859 return ret;
1860}
1861
1862char
1863valid_ipv6_address (char *address, int length, gf_boolean_t wildcard_acc)
1864{
1865 int hex_numbers = 0;
1866 int value = 0;
1867 int i = 0;
1868 char *tmp = NULL((void*)0), *ptr = NULL((void*)0), *prev = NULL((void*)0), *endptr = NULL((void*)0);
1869 char ret = 1;
1870 int is_wildcard = 0;
1871 int is_compressed = 0;
1872
1873 tmp = gf_strdup (address);
1874
1875 /* Check for compressed form */
1876 if (length <= 0 || tmp[length - 1] == ':') {
1877 ret = 0;
1878 goto out;
1879 }
1880 for (i = 0; i < (length - 1) ; i++) {
1881 if (tmp[i] == ':' && tmp[i + 1] == ':') {
1882 if (is_compressed == 0)
1883 is_compressed = 1;
1884 else {
1885 ret = 0;
1886 goto out;
1887 }
1888 }
1889 }
1890
1891 prev = strtok_r (tmp, ":", &ptr);
1892
1893 while (prev != NULL((void*)0)) {
1894 hex_numbers++;
1895 if (wildcard_acc && !strcmp (prev, "*")) {
1896 is_wildcard = 1;
1897 } else {
1898 value = strtol (prev, &endptr, 16);
1899 if ((value > 0xffff) || (value < 0)
1900 || (endptr != NULL((void*)0) && *endptr != '\0')) {
1901 ret = 0;
1902 goto out;
1903 }
1904 }
1905 prev = strtok_r (NULL((void*)0), ":", &ptr);
1906 }
1907
1908 if ((hex_numbers > 8) || (hex_numbers < 8 && !is_wildcard
1909 && !is_compressed)) {
1910 ret = 0;
1911 }
1912
1913out:
1914 GF_FREE (tmp)__gf_free (tmp);
1915 return ret;
1916}
1917
1918char
1919valid_internet_address (char *address, gf_boolean_t wildcard_acc)
1920{
1921 char ret = 0;
1922 int length = 0;
1923
1924 if (address == NULL((void*)0)) {
1925 gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid")do { do { if (0) printf ("argument invalid"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 1925, GF_LOG_WARNING, "argument invalid"); } while (0)
;
1926 goto out;
1927 }
1928
1929 length = strlen (address);
1930 if (length == 0)
1931 goto out;
1932
1933 if (valid_ipv4_address (address, length, wildcard_acc)
1934 || valid_ipv6_address (address, length, wildcard_acc)
1935 || valid_host_name (address, length))
1936 ret = 1;
1937
1938out:
1939 return ret;
1940}
1941
1942/*Thread safe conversion function*/
1943char *
1944uuid_utoa (uuid_t uuid)
1945{
1946 char *uuid_buffer = glusterfs_uuid_buf_get(THIS(*__glusterfs_this_location())->ctx);
1947 uuid_unparse (uuid, uuid_buffer);
1948 return uuid_buffer;
1949}
1950
1951/*Re-entrant conversion function*/
1952char *
1953uuid_utoa_r (uuid_t uuid, char *dst)
1954{
1955 if(!dst)
1956 return NULL((void*)0);
1957 uuid_unparse (uuid, dst);
1958 return dst;
1959}
1960
1961/*Thread safe conversion function*/
1962char *
1963lkowner_utoa (gf_lkowner_t *lkowner)
1964{
1965 char *lkowner_buffer = glusterfs_lkowner_buf_get(THIS(*__glusterfs_this_location())->ctx);
1966 lkowner_unparse (lkowner, lkowner_buffer, GF_LKOWNER_BUF_SIZE((1024 * 2) + (1024 / 8)));
1967 return lkowner_buffer;
1968}
1969
1970/*Re-entrant conversion function*/
1971char *
1972lkowner_utoa_r (gf_lkowner_t *lkowner, char *dst, int len)
1973{
1974 if(!dst)
1975 return NULL((void*)0);
1976 lkowner_unparse (lkowner, dst, len);
1977 return dst;
1978}
1979
1980void* gf_array_elem (void *a, int index, size_t elem_size)
1981{
1982 uint8_t* ptr = a;
1983 return (void*)(ptr + index * elem_size);
1984}
1985
1986void
1987gf_elem_swap (void *x, void *y, size_t l) {
1988 uint8_t *a = x, *b = y, c;
1989 while(l--) {
1990 c = *a;
1991 *a++ = *b;
1992 *b++ = c;
1993 }
1994}
1995
1996void
1997gf_array_insertionsort (void *A, int l, int r, size_t elem_size,
1998 gf_cmp cmp)
1999{
2000 int i = l;
2001 int N = r+1;
2002 void *Temp = NULL((void*)0);
2003 int j = 0;
2004
2005 for(i = l; i < N; i++) {
2006 Temp = gf_array_elem (A, i, elem_size);
2007 j = i - 1;
2008 while((cmp (Temp, gf_array_elem (A, j, elem_size))
2009 < 0) && j>=0) {
2010 gf_elem_swap (Temp, gf_array_elem (A, j, elem_size),
2011 elem_size);
2012 Temp = gf_array_elem (A, j, elem_size);
2013 j = j-1;
2014 }
2015 }
2016}
2017
2018int
2019gf_is_str_int (const char *value)
2020{
2021 int flag = 0;
2022 char *str = NULL((void*)0);
2023 char *fptr = NULL((void*)0);
2024
2025 GF_VALIDATE_OR_GOTO (THIS->name, value, out)do { if (!value) { (*__errno_location ()) = 22; do { do { if (
0) printf ("invalid argument: " "value"); } while (0); _gf_log_callingfn
((*__glusterfs_this_location())->name, "common-utils.c", __FUNCTION__
, 2025, GF_LOG_ERROR, "invalid argument: " "value"); } while (
0); goto out; } } while (0)
;
2026
2027 str = gf_strdup (value);
2028 if (!str)
2029 goto out;
2030
2031 fptr = str;
2032
2033 while (*str) {
2034 if (!isdigit(*str)((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short int
) _ISdigit)
) {
2035 flag = 1;
2036 goto out;
2037 }
2038 str++;
2039 }
2040
2041out:
2042 GF_FREE (fptr)__gf_free (fptr);
2043
2044 return flag;
2045}
2046/*
2047 * rounds up nr to power of two. If nr is already a power of two, just returns
2048 * nr
2049 */
2050
2051inline int32_t
2052gf_roundup_power_of_two (int32_t nr)
2053{
2054 int32_t result = 1;
2055
2056 if (nr < 0) {
2057 gf_log ("common-utils", GF_LOG_WARNING,do { do { if (0) printf ("negative number passed"); } while (
0); _gf_log ("common-utils", "common-utils.c", __FUNCTION__, 2058
, GF_LOG_WARNING, "negative number passed"); } while (0)
2058 "negative number passed")do { do { if (0) printf ("negative number passed"); } while (
0); _gf_log ("common-utils", "common-utils.c", __FUNCTION__, 2058
, GF_LOG_WARNING, "negative number passed"); } while (0)
;
2059 result = -1;
2060 goto out;
2061 }
2062
2063 while (result < nr)
2064 result *= 2;
2065
2066out:
2067 return result;
2068}
2069
2070/*
2071 * rounds up nr to next power of two. If nr is already a power of two, next
2072 * power of two is returned.
2073 */
2074
2075inline int32_t
2076gf_roundup_next_power_of_two (int32_t nr)
2077{
2078 int32_t result = 1;
2079
2080 if (nr < 0) {
2081 gf_log ("common-utils", GF_LOG_WARNING,do { do { if (0) printf ("negative number passed"); } while (
0); _gf_log ("common-utils", "common-utils.c", __FUNCTION__, 2082
, GF_LOG_WARNING, "negative number passed"); } while (0)
2082 "negative number passed")do { do { if (0) printf ("negative number passed"); } while (
0); _gf_log ("common-utils", "common-utils.c", __FUNCTION__, 2082
, GF_LOG_WARNING, "negative number passed"); } while (0)
;
2083 result = -1;
2084 goto out;
2085 }
2086
2087 while (result <= nr)
2088 result *= 2;
2089
2090out:
2091 return result;
2092}
2093
2094int
2095validate_brick_name (char *brick)
2096{
2097 char *delimiter = NULL((void*)0);
2098 int ret = 0;
2099 delimiter = strrchr (brick, ':');
2100 if (!delimiter || delimiter == brick
2101 || *(delimiter+1) != '/')
2102 ret = -1;
2103
2104 return ret;
2105}
2106
2107char *
2108get_host_name (char *word, char **host)
2109{
2110 char *delimiter = NULL((void*)0);
2111 delimiter = strrchr (word, ':');
2112 if (delimiter)
2113 *delimiter = '\0';
2114 else
2115 return NULL((void*)0);
2116 *host = word;
2117 return *host;
2118}
2119
2120
2121char *
2122get_path_name (char *word, char **path)
2123{
2124 char *delimiter = NULL((void*)0);
2125 delimiter = strchr (word, '/');
2126 if (!delimiter)
2127 return NULL((void*)0);
2128 *path = delimiter;
2129 return *path;
2130}
2131
2132void
2133gf_path_strip_trailing_slashes (char *path)
2134{
2135 int i = 0;
2136 int len = 0;
2137
2138 if (!path)
2139 return;
2140
2141 len = strlen (path);
2142 for (i = len - 1; i > 0; i--) {
2143 if (path[i] != '/')
2144 break;
2145 }
2146
2147 if (i < (len -1))
2148 path [i+1] = '\0';
2149
2150 return;
2151}
2152
2153uint64_t
2154get_mem_size ()
2155{
2156 uint64_t memsize = -1;
2157
2158#if defined GF_LINUX_HOST_OS1 || defined GF_SOLARIS_HOST_OS
2159
2160 uint64_t page_size = 0;
2161 uint64_t num_pages = 0;
2162
2163 page_size = sysconf (_SC_PAGESIZE_SC_PAGESIZE);
2164 num_pages = sysconf (_SC_PHYS_PAGES_SC_PHYS_PAGES);
2165
2166 memsize = page_size * num_pages;
2167#endif
2168
2169#if defined GF_BSD_HOST_OS || defined GF_DARWIN_HOST_OS
2170
2171 size_t len = sizeof(memsize);
2172 int name [] = { CTL_HW, HW_PHYSMEM };
2173
2174 sysctl (name, 2, &memsize, &len, NULL((void*)0), 0);
2175#endif
2176 return memsize;
2177}
2178
2179/* Strips all whitespace characters in a string and returns length of new string
2180 * on success
2181 */
2182int
2183gf_strip_whitespace (char *str, int len)
2184{
2185 int i = 0;
2186 int new_len = 0;
2187 char *new_str = NULL((void*)0);
2188
2189 GF_ASSERT (str)do { if (!(str)) { do { do { if (0) printf ("Assertion failed: "
"str"); } while (0); _gf_log_callingfn ("", "common-utils.c"
, __FUNCTION__, 2189, GF_LOG_ERROR, "Assertion failed: " "str"
); } while (0); } } while (0)
;
2190
2191 new_str = GF_CALLOC (1, len + 1, gf_common_mt_char)__gf_calloc (1, len + 1, gf_common_mt_char);
2192 if (new_str == NULL((void*)0))
2193 return -1;
2194
2195 for (i = 0; i < len; i++) {
2196 if (!isspace (str[i])((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int
) _ISspace)
)
2197 new_str[new_len++] = str[i];
2198 }
2199 new_str[new_len] = '\0';
2200
2201 if (new_len != len) {
2202 memset (str, 0, len);
2203 strncpy (str, new_str, new_len);
2204 }
2205
2206 GF_FREE (new_str)__gf_free (new_str);
2207 return new_len;
2208}
2209
2210int
2211gf_canonicalize_path (char *path)
2212{
2213 int ret = -1;
2214 int path_len = 0;
2215 int dir_path_len = 0;
2216 char *tmppath = NULL((void*)0);
2217 char *dir = NULL((void*)0);
2218 char *tmpstr = NULL((void*)0);
2219
2220 if (!path || *path != '/')
2221 goto out;
2222
2223 tmppath = gf_strdup (path);
2224 if (!tmppath)
2225 goto out;
2226
2227 /* Strip the extra slashes and return */
2228 bzero (path, strlen(path));
2229 path[0] = '/';
2230 dir = strtok_r(tmppath, "/", &tmpstr);
2231
2232 while (dir) {
2233 dir_path_len = strlen(dir);
2234 strncpy ((path + path_len + 1), dir, dir_path_len);
2235 path_len += dir_path_len + 1;
2236 dir = strtok_r (NULL((void*)0), "/", &tmpstr);
2237 if (dir)
2238 strncpy ((path + path_len), "/", 1);
2239 }
2240 path[path_len] = '\0';
2241 ret = 0;
2242
2243 out:
2244 if (ret)
2245 gf_log ("common-utils", GF_LOG_ERROR,do { do { if (0) printf ("Path manipulation failed"); } while
(0); _gf_log ("common-utils", "common-utils.c", __FUNCTION__
, 2246, GF_LOG_ERROR, "Path manipulation failed"); } while (0
)
2246 "Path manipulation failed")do { do { if (0) printf ("Path manipulation failed"); } while
(0); _gf_log ("common-utils", "common-utils.c", __FUNCTION__
, 2246, GF_LOG_ERROR, "Path manipulation failed"); } while (0
)
;
2247
2248 GF_FREE(tmppath)__gf_free (tmppath);
2249
2250 return ret;
2251}
2252
2253static const char *__gf_timefmts[] = {
2254 "%F %T",
2255 "%Y/%m/%d-%T",
2256 "%b %d %T",
2257 "%F %H%M%S"
2258};
2259
2260static const char *__gf_zerotimes[] = {
2261 "0000-00-00 00:00:00",
2262 "0000/00/00-00:00:00",
2263 "xxx 00 00:00:00",
2264 "0000-00-00 000000"
2265};
2266
2267void
2268_gf_timestuff (gf_timefmts *fmt, const char ***fmts, const char ***zeros)
2269{
2270 *fmt = gf_timefmt_last;
2271 *fmts = __gf_timefmts;
2272 *zeros = __gf_zerotimes;
2273}
2274
2275
2276char *
2277generate_glusterfs_ctx_id (void)
2278{
2279 char tmp_str[1024] = {0,};
2280 char hostname[256] = {0,};
2281 struct timeval tv = {0,};
2282 char now_str[32];
2283
2284 if (gettimeofday (&tv, NULL((void*)0)) == -1) {
2285 gf_log ("glusterfsd", GF_LOG_ERROR,do { do { if (0) printf ("gettimeofday: failed %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("glusterfsd",
"common-utils.c", __FUNCTION__, 2287, GF_LOG_ERROR, "gettimeofday: failed %s"
, strerror ((*__errno_location ()))); } while (0)
2286 "gettimeofday: failed %s",do { do { if (0) printf ("gettimeofday: failed %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("glusterfsd",
"common-utils.c", __FUNCTION__, 2287, GF_LOG_ERROR, "gettimeofday: failed %s"
, strerror ((*__errno_location ()))); } while (0)
2287 strerror (errno))do { do { if (0) printf ("gettimeofday: failed %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("glusterfsd",
"common-utils.c", __FUNCTION__, 2287, GF_LOG_ERROR, "gettimeofday: failed %s"
, strerror ((*__errno_location ()))); } while (0)
;
2288 }
2289
2290 if (gethostname (hostname, 256) == -1) {
2291 gf_log ("glusterfsd", GF_LOG_ERROR,do { do { if (0) printf ("gethostname: failed %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("glusterfsd",
"common-utils.c", __FUNCTION__, 2293, GF_LOG_ERROR, "gethostname: failed %s"
, strerror ((*__errno_location ()))); } while (0)
2292 "gethostname: failed %s",do { do { if (0) printf ("gethostname: failed %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("glusterfsd",
"common-utils.c", __FUNCTION__, 2293, GF_LOG_ERROR, "gethostname: failed %s"
, strerror ((*__errno_location ()))); } while (0)
2293 strerror (errno))do { do { if (0) printf ("gethostname: failed %s", strerror (
(*__errno_location ()))); } while (0); _gf_log ("glusterfsd",
"common-utils.c", __FUNCTION__, 2293, GF_LOG_ERROR, "gethostname: failed %s"
, strerror ((*__errno_location ()))); } while (0)
;
2294 }
2295
2296 gf_time_fmt (now_str, sizeof now_str, tv.tv_sec, gf_timefmt_Ymd_T);
2297 snprintf (tmp_str, sizeof tmp_str, "%s-%d-%s:%"
2298#ifdef GF_DARWIN_HOST_OS
2299 PRId32"d",
2300#else
2301 "ld",
2302#endif
2303 hostname, getpid(), now_str, tv.tv_usec);
2304
2305 return gf_strdup (tmp_str);
2306}
2307
2308char *
2309gf_get_reserved_ports ()
2310{
2311 char *ports_info = NULL((void*)0);
2312#if defined GF_LINUX_HOST_OS1
2313 int proc_fd = -1;
2314 char *proc_file = "/proc/sys/net/ipv4/ip_local_reserved_ports";
2315 char buffer[4096] = {0,};
2316 int32_t ret = -1;
2317
2318 proc_fd = open (proc_file, O_RDONLY00);
2319 if (proc_fd == -1) {
2320 /* What should be done in this case? error out from here
2321 * and thus stop the glusterfs process from starting or
2322 * continue with older method of using any of the available
2323 * port? For now 2nd option is considered.
2324 */
2325 gf_log ("glusterfs", GF_LOG_WARNING, "could not open "do { do { if (0) printf ("could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2328, GF_LOG_WARNING, "could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0)
2326 "the file /proc/sys/net/ipv4/ip_local_reserved_ports "do { do { if (0) printf ("could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2328, GF_LOG_WARNING, "could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0)
2327 "for getting reserved ports info (%s)",do { do { if (0) printf ("could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2328, GF_LOG_WARNING, "could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0)
2328 strerror (errno))do { do { if (0) printf ("could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2328, GF_LOG_WARNING, "could not open " "the file /proc/sys/net/ipv4/ip_local_reserved_ports "
"for getting reserved ports info (%s)", strerror ((*__errno_location
()))); } while (0)
;
2329 goto out;
2330 }
2331
2332 ret = read (proc_fd, buffer, sizeof (buffer));
2333 if (ret < 0) {
2334 gf_log ("glusterfs", GF_LOG_WARNING, "could not "do { do { if (0) printf ("could not " "read the file %s for getting reserved ports "
"info (%s)", proc_file, strerror ((*__errno_location ()))); }
while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2336, GF_LOG_WARNING, "could not " "read the file %s for getting reserved ports "
"info (%s)", proc_file, strerror ((*__errno_location ()))); }
while (0)
2335 "read the file %s for getting reserved ports "do { do { if (0) printf ("could not " "read the file %s for getting reserved ports "
"info (%s)", proc_file, strerror ((*__errno_location ()))); }
while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2336, GF_LOG_WARNING, "could not " "read the file %s for getting reserved ports "
"info (%s)", proc_file, strerror ((*__errno_location ()))); }
while (0)
2336 "info (%s)", proc_file, strerror (errno))do { do { if (0) printf ("could not " "read the file %s for getting reserved ports "
"info (%s)", proc_file, strerror ((*__errno_location ()))); }
while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2336, GF_LOG_WARNING, "could not " "read the file %s for getting reserved ports "
"info (%s)", proc_file, strerror ((*__errno_location ()))); }
while (0)
;
2337 goto out;
2338 }
2339 ports_info = gf_strdup (buffer);
2340
2341out:
2342 if (proc_fd != -1)
2343 close (proc_fd);
2344#endif /* GF_LINUX_HOST_OS */
2345 return ports_info;
2346}
2347
2348int
2349gf_process_reserved_ports (gf_boolean_t *ports)
2350{
2351 int ret = -1;
2352#if defined GF_LINUX_HOST_OS1
2353 char *ports_info = NULL((void*)0);
2354 char *tmp = NULL((void*)0);
2355 char *blocked_port = NULL((void*)0);
2356
2357 ports_info = gf_get_reserved_ports ();
2358 if (!ports_info) {
2359 gf_log ("glusterfs", GF_LOG_WARNING, "Not able to get reserved "do { do { if (0) printf ("Not able to get reserved " "ports, hence there is a possibility that glusterfs "
"may consume reserved port"); } while (0); _gf_log ("glusterfs"
, "common-utils.c", __FUNCTION__, 2361, GF_LOG_WARNING, "Not able to get reserved "
"ports, hence there is a possibility that glusterfs " "may consume reserved port"
); } while (0)
2360 "ports, hence there is a possibility that glusterfs "do { do { if (0) printf ("Not able to get reserved " "ports, hence there is a possibility that glusterfs "
"may consume reserved port"); } while (0); _gf_log ("glusterfs"
, "common-utils.c", __FUNCTION__, 2361, GF_LOG_WARNING, "Not able to get reserved "
"ports, hence there is a possibility that glusterfs " "may consume reserved port"
); } while (0)
2361 "may consume reserved port")do { do { if (0) printf ("Not able to get reserved " "ports, hence there is a possibility that glusterfs "
"may consume reserved port"); } while (0); _gf_log ("glusterfs"
, "common-utils.c", __FUNCTION__, 2361, GF_LOG_WARNING, "Not able to get reserved "
"ports, hence there is a possibility that glusterfs " "may consume reserved port"
); } while (0)
;
2362 goto out;
2363 }
2364
2365 blocked_port = strtok_r (ports_info, ",\n",&tmp);
2366
2367 while (blocked_port) {
2368 gf_ports_reserved (blocked_port, ports);
2369 blocked_port = strtok_r (NULL((void*)0), ",\n", &tmp);
2370 }
2371
2372 ret = 0;
2373
2374out:
2375 GF_FREE (ports_info)__gf_free (ports_info);
2376#endif /* GF_LINUX_HOST_OS */
2377 return ret;
2378}
2379
2380gf_boolean_t
2381gf_ports_reserved (char *blocked_port, gf_boolean_t *ports)
2382{
2383 gf_boolean_t result = _gf_false;
2384 char *range_port = NULL((void*)0);
2385 int16_t tmp_port1, tmp_port2 = -1;
2386
2387 if (strstr (blocked_port, "-") == NULL((void*)0)) {
2388 /* get rid of the new line character*/
2389 if (blocked_port[strlen(blocked_port) -1] == '\n')
2390 blocked_port[strlen(blocked_port) -1] = '\0';
2391 if (gf_string2int16 (blocked_port, &tmp_port1) == 0) {
2392 if (tmp_port1 > (GF_CLIENT_PORT_CEILING1024 - 1)
2393 || tmp_port1 < 0) {
2394 gf_log ("glusterfs-socket", GF_LOG_WARNING,do { do { if (0) printf ("invalid port %d", tmp_port1); } while
(0); _gf_log ("glusterfs-socket", "common-utils.c", __FUNCTION__
, 2395, GF_LOG_WARNING, "invalid port %d", tmp_port1); } while
(0)
2395 "invalid port %d", tmp_port1)do { do { if (0) printf ("invalid port %d", tmp_port1); } while
(0); _gf_log ("glusterfs-socket", "common-utils.c", __FUNCTION__
, 2395, GF_LOG_WARNING, "invalid port %d", tmp_port1); } while
(0)
;
2396 result = _gf_true;
2397 goto out;
2398 } else {
2399 gf_log ("glusterfs", GF_LOG_DEBUG,do { do { if (0) printf ("blocking port %d", tmp_port1); } while
(0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__, 2400
, GF_LOG_DEBUG, "blocking port %d", tmp_port1); } while (0)
2400 "blocking port %d", tmp_port1)do { do { if (0) printf ("blocking port %d", tmp_port1); } while
(0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__, 2400
, GF_LOG_DEBUG, "blocking port %d", tmp_port1); } while (0)
;
2401 ports[tmp_port1] = _gf_true;
2402 }
2403 } else {
2404 gf_log ("glusterfs-socket", GF_LOG_WARNING, "%s is "do { do { if (0) printf ("%s is " "not a valid port identifier"
, blocked_port); } while (0); _gf_log ("glusterfs-socket", "common-utils.c"
, __FUNCTION__, 2405, GF_LOG_WARNING, "%s is " "not a valid port identifier"
, blocked_port); } while (0)
2405 "not a valid port identifier", blocked_port)do { do { if (0) printf ("%s is " "not a valid port identifier"
, blocked_port); } while (0); _gf_log ("glusterfs-socket", "common-utils.c"
, __FUNCTION__, 2405, GF_LOG_WARNING, "%s is " "not a valid port identifier"
, blocked_port); } while (0)
;
2406 result = _gf_true;
2407 goto out;
2408 }
2409 } else {
2410 range_port = strtok (blocked_port, "-");
2411 if (!range_port){
2412 result = _gf_true;
2413 goto out;
2414 }
2415 if (gf_string2int16 (range_port, &tmp_port1) == 0) {
2416 if (tmp_port1 > (GF_CLIENT_PORT_CEILING1024 - 1))
2417 tmp_port1 = GF_CLIENT_PORT_CEILING1024 - 1;
2418 if (tmp_port1 < 0)
2419 tmp_port1 = 0;
2420 }
2421 range_port = strtok (NULL((void*)0), "-");
2422 if (!range_port) {
2423 result = _gf_true;
2424 goto out;
2425 }
2426 /* get rid of the new line character*/
2427 if (range_port[strlen(range_port) -1] == '\n')
2428 range_port[strlen(range_port) - 1] = '\0';
2429 if (gf_string2int16 (range_port, &tmp_port2) == 0) {
2430 if (tmp_port2 >
2431 (GF_CLIENT_PORT_CEILING1024 - 1))
2432 tmp_port2 = GF_CLIENT_PORT_CEILING1024 - 1;
2433 if (tmp_port2 < 0)
2434 tmp_port2 = 0;
2435 }
2436 gf_log ("glusterfs", GF_LOG_DEBUG, "lower: %d, higher: %d",do { do { if (0) printf ("lower: %d, higher: %d", tmp_port1, tmp_port2
); } while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2437, GF_LOG_DEBUG, "lower: %d, higher: %d", tmp_port1, tmp_port2
); } while (0)
2437 tmp_port1, tmp_port2)do { do { if (0) printf ("lower: %d, higher: %d", tmp_port1, tmp_port2
); } while (0); _gf_log ("glusterfs", "common-utils.c", __FUNCTION__
, 2437, GF_LOG_DEBUG, "lower: %d, higher: %d", tmp_port1, tmp_port2
); } while (0)
;
2438 for (; tmp_port1 <= tmp_port2; tmp_port1++)
2439 ports[tmp_port1] = _gf_true;
2440 }
2441
2442out:
2443 return result;
2444}
2445
2446/* Takes in client ip{v4,v6} and returns associated hostname, if any
2447 * Also, allocates memory for the hostname.
2448 * Returns: 0 for success, -1 for failure
2449 */
2450int
2451gf_get_hostname_from_ip (char *client_ip, char **hostname)
2452{
2453 int ret = -1;
2454 struct sockaddr *client_sockaddr = NULL((void*)0);
2455 struct sockaddr_in client_sock_in = {0};
2456 struct sockaddr_in6 client_sock_in6 = {0};
2457 char client_hostname[NI_MAXHOST1025] = {0};
2458 char *client_ip_copy = NULL((void*)0);
2459 char *tmp = NULL((void*)0);
2460 char *ip = NULL((void*)0);
2461
2462 /* if ipv4, reverse lookup the hostname to
2463 * allow FQDN based rpc authentication
2464 */
2465 if (valid_ipv4_address (client_ip, strlen (client_ip), 0) == _gf_false) {
2466 /* most times, we get a.b.c.d:port form, so check that */
2467 client_ip_copy = gf_strdup (client_ip);
2468 if (!client_ip_copy)
2469 goto out;
2470
2471 ip = strtok_r (client_ip_copy, ":", &tmp);
2472 } else {
2473 ip = client_ip;
2474 }
2475
2476 if (valid_ipv4_address (ip, strlen (ip), 0) == _gf_true) {
2477 client_sockaddr = (struct sockaddr *)&client_sock_in;
2478 client_sock_in.sin_family = AF_INET2;
2479 ret = inet_pton (AF_INET2, ip,
2480 (void *)&client_sock_in.sin_addr.s_addr);
2481
2482 } else if (valid_ipv6_address (ip, strlen (ip), 0) == _gf_true) {
2483 client_sockaddr = (struct sockaddr *) &client_sock_in6;
2484
2485 client_sock_in6.sin6_family = AF_INET610;
2486 ret = inet_pton (AF_INET610, ip,
2487 (void *)&client_sock_in6.sin6_addr);
2488 } else {
2489 goto out;
2490 }
2491
2492 if (ret != 1) {
2493 ret = -1;
2494 goto out;
2495 }
2496
2497 ret = getnameinfo (client_sockaddr,
2498 sizeof (*client_sockaddr),
2499 client_hostname, sizeof (client_hostname),
2500 NULL((void*)0), 0, 0);
2501 if (ret) {
2502 gf_log ("common-utils", GF_LOG_ERROR,do { do { if (0) printf ("Could not lookup hostname of %s : %s"
, client_ip, gai_strerror (ret)); } while (0); _gf_log ("common-utils"
, "common-utils.c", __FUNCTION__, 2504, GF_LOG_ERROR, "Could not lookup hostname of %s : %s"
, client_ip, gai_strerror (ret)); } while (0)
2503 "Could not lookup hostname of %s : %s",do { do { if (0) printf ("Could not lookup hostname of %s : %s"
, client_ip, gai_strerror (ret)); } while (0); _gf_log ("common-utils"
, "common-utils.c", __FUNCTION__, 2504, GF_LOG_ERROR, "Could not lookup hostname of %s : %s"
, client_ip, gai_strerror (ret)); } while (0)
2504 client_ip, gai_strerror (ret))do { do { if (0) printf ("Could not lookup hostname of %s : %s"
, client_ip, gai_strerror (ret)); } while (0); _gf_log ("common-utils"
, "common-utils.c", __FUNCTION__, 2504, GF_LOG_ERROR, "Could not lookup hostname of %s : %s"
, client_ip, gai_strerror (ret)); } while (0)
;
2505 ret = -1;
2506 goto out;
2507 }
2508
2509 *hostname = gf_strdup ((char *)client_hostname);
2510 out:
2511 if (client_ip_copy)
2512 GF_FREE (client_ip_copy)__gf_free (client_ip_copy);
2513
2514 return ret;
2515}