-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
common-utils.c
5423 lines (4592 loc) · 129 KB
/
common-utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#else
#include "execinfo_compat.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <assert.h>
#include <libgen.h> /* for dirname() */
#include <grp.h>
#if defined(GF_BSD_HOST_OS) || defined(GF_DARWIN_HOST_OS)
#include <sys/sysctl.h>
#endif
#ifndef GF_LINUX_HOST_OS
#include <sys/resource.h>
#endif
#include "glusterfs/compat-errno.h"
#include "glusterfs/common-utils.h"
#include "glusterfs/revision.h"
#include "glusterfs/glusterfs.h"
#include "glusterfs/stack.h"
#include "glusterfs/lkowner.h"
#include "glusterfs/syscall.h"
#include "cli1-xdr.h"
#include "glusterfs/globals.h"
#define XXH_INLINE_ALL
#include "xxhash.h"
#include <ifaddrs.h>
#include "glusterfs/libglusterfs-messages.h"
#include "protocol-common.h"
#ifdef __FreeBSD__
#include <pthread_np.h>
#undef BIT_SET
#endif
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0
#endif /* AI_ADDRCONFIG */
char *vol_type_str[] = {
"Distribute",
"Stripe [NOT SUPPORTED from v6.0]",
"Replicate",
"Striped-Replicate [NOT SUPPORTED from v6.0]",
"Disperse",
"Tier [NOT SUPPORTED from v6.0]",
"Distributed-Stripe [NOT SUPPORTED from v6.0]",
"Distributed-Replicate",
"Distributed-Striped-Replicate [NOT SUPPORTED from v6.0]",
"Distributed-Disperse",
};
typedef int32_t (*rw_op_t)(int32_t fd, char *buf, int32_t size);
typedef int32_t (*rwv_op_t)(int32_t fd, const struct iovec *buf, int32_t size);
void
gf_xxh64_wrapper(const unsigned char *data, size_t const len,
unsigned long long const seed, char *xxh64)
{
unsigned short i = 0;
const unsigned short lim = GF_XXH64_DIGEST_LENGTH * 2 + 1;
XXH64_hash_t hash = 0;
XXH64_canonical_t c_hash = {
{
0,
},
};
const uint8_t *p = (const uint8_t *)&c_hash;
hash = XXH64(data, len, seed);
XXH64_canonicalFromHash(&c_hash, hash);
for (i = 0; i < GF_XXH64_DIGEST_LENGTH; i++)
snprintf(xxh64 + i * 2, lim - i * 2, "%02x", p[i]);
}
/**
* This function takes following arguments
* @this: xlator
* @gfid: The gfid which has to be filled
* @hash: the 8 byte hash which has to be filled inside the gfid
* @index: the array element of the uuid_t structure (which is
* a array of unsigned char) from where the 8 bytes of
* the hash has to be filled. Since uuid_t contains 16
* char elements in the array, each byte of the hash has
* to be filled in one array element.
*
* This function is called twice for 2 hashes (of 8 byte each) to
* be filled in the gfid.
*
* The for loop in this function actually is doing these 2 things
* for each hash
*
* 1) One of the hashes
* tmp[0] = (hash_2 >> 56) & 0xff;
* tmp[1] = (hash_2 >> 48) & 0xff;
* tmp[2] = (hash_2 >> 40) & 0xff;
* tmp[3] = (hash_2 >> 32) & 0xff;
* tmp[4] = (hash_2 >> 24) & 0xff;
* tmp[5] = (hash_2 >> 16) & 0xff;
* tmp[6] = (hash_2 >> 8) & 0xff;
* tmp[7] = (hash_2) & 0xff;
*
* 2) The other hash:
* tmp[8] = (hash_1 >> 56) & 0xff;
* tmp[9] = (hash_1 >> 48) & 0xff;
* tmp[10] = (hash_1 >> 40) & 0xff;
* tmp[11] = (hash_1 >> 32) & 0xff;
* tmp[12] = (hash_1 >> 24) & 0xff;
* tmp[13] = (hash_1 >> 16) & 0xff;
* tmp[14] = (hash_1 >> 8) & 0xff;
* tmp[15] = (hash_1) & 0xff;
**/
static int
gf_gfid_from_xxh64(xlator_t *this, uuid_t gfid, XXH64_hash_t hash,
unsigned short index)
{
int ret = -1;
int i = -1;
if ((index != 0) && (index != 8)) {
gf_msg_callingfn("gfid-from-xxh64", GF_LOG_WARNING, 0,
LG_MSG_INDEX_NOT_FOUND,
"index can only be either 0 or 8, as this"
"function's purpose is to encode a 8 byte "
"hash inside the gfid (index: %d)",
index);
goto out;
}
for (i = 0; i < sizeof(hash); i++) {
/*
* As of now the below statement is equivalent of this.
* gfid[index+i] = (hash >> (64 - (8 * (i+1)))) & 0xff;
*/
gfid[index + i] = (hash >> ((sizeof(hash) * 8) - (8 * (i + 1)))) &
(0xff);
}
ret = 0;
out:
return ret;
}
/**
* This function does the same thing as gf_xxh64_wrapper. But gf_xxh64_wrapper
* does not return anything and in this xlator there is a need for both the
* actual hash and the canonicalized form of the hash.
*
* To summarize:
* - XXH64_hash_t is needed as return because, those bytes which contain the
* hash can be used for different purposes as needed. One example is
* to have those bytes copied into the uuid_t structure to be used as gfid
* - xxh64 string is needed because, it can be used as the key for generating
* the next hash (and any other purpose which might require canonical form
* of the hash).
**/
XXH64_hash_t
gf_xxh64_hash_wrapper(const unsigned char *data, size_t const len,
unsigned long long const seed, char *xxh64)
{
unsigned short i = 0;
const unsigned short lim = GF_XXH64_DIGEST_LENGTH * 2 + 1;
XXH64_hash_t hash = 0;
XXH64_canonical_t c_hash = {
{
0,
},
};
const uint8_t *p = (const uint8_t *)&c_hash;
hash = XXH64(data, len, seed);
XXH64_canonicalFromHash(&c_hash, hash);
for (i = 0; i < GF_XXH64_DIGEST_LENGTH; i++)
snprintf(xxh64 + i * 2, lim - i * 2, "%02x", p[i]);
return hash;
}
/**
* This is the algorithm followed for generating new gfid
* 1) generate xxh64 hash using snapname and original gfid of the object
* 2) Using the canonicalized form of above hash as the key, generate
* another hash
* 3) Combine both of the 8 byte hashes to generate a 16 byte uuid_t type
* 4) Use the above uuid as the gfid
*
* Each byte of the hash is stored separately in different elements of the
* character array represented by uuid_t
* Ex: tmp[0] = (hash_2 >> 56) & 0xFF
* This saves the most significant byte of hash_2 in tmp[0]
* tmp[1] = (hash_2 >> 48) & 0xFF
* This saves next most significant byte of hash_2 in tmp[1]
* .
* .
* So on.
* tmp[0] - tmp[7] holds the contents of hash_2
* tmp[8] - tmp[15] hold the conents of hash_1
*
* The hash generated (i.e. of type XXH64_hash_t) is 8 bytes long. And for
* gfid 16 byte uuid is needed. Hecne the 2 hashes are combined to form
* one 16 byte entity.
**/
int
gf_gfid_generate_from_xxh64(uuid_t gfid, char *key)
{
char xxh64_1[GF_XXH64_DIGEST_LENGTH * 2 + 1] = {
0,
};
char xxh64_2[GF_XXH64_DIGEST_LENGTH * 2 + 1] = {
0,
};
XXH64_hash_t hash_1 = 0;
XXH64_hash_t hash_2 = 0;
int ret = -1;
xlator_t *this = THIS;
hash_1 = gf_xxh64_hash_wrapper((unsigned char *)key, strlen(key),
GF_XXHSUM64_DEFAULT_SEED, xxh64_1);
hash_2 = gf_xxh64_hash_wrapper((unsigned char *)xxh64_1, strlen(xxh64_1),
GF_XXHSUM64_DEFAULT_SEED, xxh64_2);
/* hash_2 is saved in 1st 8 elements of uuid_t char array */
if (gf_gfid_from_xxh64(this, gfid, hash_2, 0)) {
gf_msg_callingfn(this->name, GF_LOG_WARNING, 0,
LG_MSG_XXH64_TO_GFID_FAILED,
"failed to encode the hash %llx into the 1st"
"half of gfid",
hash_2);
goto out;
}
/* hash_1 is saved in the remaining 8 elements of uuid_t */
if (gf_gfid_from_xxh64(this, gfid, hash_1, 8)) {
gf_msg_callingfn(this->name, GF_LOG_WARNING, 0,
LG_MSG_XXH64_TO_GFID_FAILED,
"failed to encode the hash %llx into the 2nd"
"half of gfid",
hash_1);
goto out;
}
gf_msg_debug(this->name, 0,
"gfid generated is %s (hash1: %llx) "
"hash2: %llx, xxh64_1: %s xxh64_2: %s",
uuid_utoa(gfid), hash_1, hash_2, xxh64_1, xxh64_2);
ret = 0;
out:
return ret;
}
/* works similar to mkdir(1) -p.
*/
int
mkdir_p(char *path, mode_t mode, gf_boolean_t allow_symlinks)
{
int i = 0;
int ret = -1;
char dir[PATH_MAX] = {
0,
};
struct stat stbuf = {
0,
};
const int path_len = min(strlen(path), PATH_MAX - 1);
snprintf(dir, path_len + 1, "%s", path);
i = (dir[0] == '/') ? 1 : 0;
do {
if (path[i] != '/' && path[i] != '\0')
continue;
dir[i] = '\0';
ret = sys_mkdir(dir, mode);
if (ret && errno != EEXIST) {
gf_msg("", GF_LOG_ERROR, errno, LG_MSG_DIR_OP_FAILED,
"Failed due to reason");
goto out;
}
if (ret && errno == EEXIST && !allow_symlinks) {
ret = sys_lstat(dir, &stbuf);
if (ret)
goto out;
if (S_ISLNK(stbuf.st_mode)) {
ret = -1;
gf_msg("", GF_LOG_ERROR, 0, LG_MSG_DIR_IS_SYMLINK,
"%s is a "
"symlink",
dir);
goto out;
}
}
dir[i] = '/';
} while (path[i++] != '\0');
ret = sys_stat(dir, &stbuf);
if (ret || !S_ISDIR(stbuf.st_mode)) {
if (ret == 0)
errno = 0;
ret = -1;
gf_msg("", GF_LOG_ERROR, errno, LG_MSG_DIR_OP_FAILED,
"Failed"
" to create directory, possibly some of the components"
" were not directories");
goto out;
}
ret = 0;
out:
return ret;
}
int
gf_lstat_dir(const char *path, struct stat *stbuf_in)
{
int ret = -1;
struct stat stbuf = {
0,
};
if (path == NULL) {
errno = EINVAL;
goto out;
}
ret = sys_lstat(path, &stbuf);
if (ret)
goto out;
if (!S_ISDIR(stbuf.st_mode)) {
errno = ENOTDIR;
ret = -1;
goto out;
}
ret = 0;
out:
if (!ret && stbuf_in)
*stbuf_in = stbuf;
return ret;
}
int
log_base2(unsigned long x)
{
int val = 0;
while (x > 1) {
x /= 2;
val++;
}
return val;
}
/**
* gf_rev_dns_lookup -- Perform a reverse DNS lookup on the IP address.
*
* @ip: The IP address to perform a reverse lookup on
*
* @return: success: Allocated string containing the hostname
* failure: NULL
*/
char *
gf_rev_dns_lookup(const char *ip)
{
char *fqdn = NULL;
int ret = 0;
GF_VALIDATE_OR_GOTO("resolver", ip, out);
/* Get the FQDN */
ret = gf_get_hostname_from_ip((char *)ip, &fqdn);
if (ret != 0) {
gf_msg("resolver", GF_LOG_INFO, errno, LG_MSG_RESOLVE_HOSTNAME_FAILED,
"could not resolve "
"hostname for %s",
ip);
}
out:
return fqdn;
}
/**
* gf_resolve_path_parent -- Given a path, returns an allocated string
* containing the parent's path.
* @path: Path to parse
* @return: The parent path if found, NULL otherwise
*/
char *
gf_resolve_path_parent(const char *path)
{
char *parent = NULL;
char *tmp = NULL;
char *pathc = NULL;
GF_VALIDATE_OR_GOTO(THIS->name, path, out);
if (strlen(path) <= 0) {
gf_msg_callingfn(THIS->name, GF_LOG_DEBUG, 0, LG_MSG_INVALID_STRING,
"invalid string for 'path'");
goto out;
}
/* dup the parameter, we don't want to modify it */
pathc = strdupa(path);
if (!pathc) {
goto out;
}
/* Get the parent directory */
tmp = dirname(pathc);
if (strcmp(tmp, "/") == 0)
goto out;
parent = gf_strdup(tmp);
out:
return parent;
}
int32_t
gf_resolve_ip6(const char *hostname, uint16_t port, int family, void **dnscache,
struct addrinfo **addr_info)
{
int32_t ret = 0;
struct addrinfo hints;
struct dnscache6 *cache = NULL;
char service[NI_MAXSERV], host[NI_MAXHOST];
if (!hostname) {
gf_msg_callingfn("resolver", GF_LOG_WARNING, 0, LG_MSG_HOSTNAME_NULL,
"hostname is NULL");
return -1;
}
if (!*dnscache) {
*dnscache = GF_CALLOC(1, sizeof(struct dnscache6),
gf_common_mt_dnscache6);
if (!*dnscache)
return -1;
}
cache = *dnscache;
if (cache->first && !cache->next) {
freeaddrinfo(cache->first);
cache->first = cache->next = NULL;
gf_msg_trace("resolver", 0, "flushing DNS cache");
}
if (!cache->first) {
char *port_str = NULL;
gf_msg_trace("resolver", 0,
"DNS cache not present, freshly "
"probing hostname: %s",
hostname);
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
ret = gf_asprintf(&port_str, "%d", port);
if (-1 == ret) {
return -1;
}
if ((ret = getaddrinfo(hostname, port_str, &hints, &cache->first)) !=
0) {
gf_msg("resolver", GF_LOG_ERROR, 0, LG_MSG_GETADDRINFO_FAILED,
"getaddrinfo failed (family:%d) (%s)", family,
gai_strerror(ret));
GF_FREE(*dnscache);
*dnscache = NULL;
GF_FREE(port_str);
return -1;
}
GF_FREE(port_str);
cache->next = cache->first;
}
if (cache->next) {
ret = getnameinfo((struct sockaddr *)cache->next->ai_addr,
cache->next->ai_addrlen, host, sizeof(host), service,
sizeof(service), NI_NUMERICHOST);
if (ret != 0) {
gf_msg("resolver", GF_LOG_ERROR, 0, LG_MSG_GETNAMEINFO_FAILED,
"getnameinfo failed"
" (%s)",
gai_strerror(ret));
goto err;
}
gf_msg_debug("resolver", 0,
"returning ip-%s (port-%s) for "
"hostname: %s and port: %d",
host, service, hostname, port);
*addr_info = cache->next;
}
if (cache->next)
cache->next = cache->next->ai_next;
if (cache->next) {
ret = getnameinfo((struct sockaddr *)cache->next->ai_addr,
cache->next->ai_addrlen, host, sizeof(host), service,
sizeof(service), NI_NUMERICHOST);
if (ret != 0) {
gf_msg("resolver", GF_LOG_ERROR, 0, LG_MSG_GETNAMEINFO_FAILED,
"getnameinfo failed"
" (%s)",
gai_strerror(ret));
goto err;
}
gf_msg_debug("resolver", 0,
"next DNS query will return: "
"ip-%s port-%s",
host, service);
}
return 0;
err:
freeaddrinfo(cache->first);
cache->first = cache->next = NULL;
GF_FREE(cache);
*dnscache = NULL;
return -1;
}
/**
* gf_dnscache_init -- Initializes a dnscache struct and sets the ttl
* to the specified value in the parameter.
*
* @ttl: the TTL in seconds
* @return: SUCCESS: Pointer to an allocated dnscache struct
* FAILURE: NULL
*/
struct dnscache *
gf_dnscache_init(time_t ttl)
{
struct dnscache *cache = GF_MALLOC(sizeof(*cache), gf_common_mt_dnscache);
if (cache) {
cache->cache_dict = NULL;
cache->ttl = ttl;
}
return cache;
}
/**
* gf_dnscache_entry_init -- Initialize a dnscache entry
*
* @return: SUCCESS: Pointer to an allocated dnscache entry struct
* FAILURE: NULL
*/
struct dnscache_entry *
gf_dnscache_entry_init()
{
struct dnscache_entry *entry = GF_CALLOC(1, sizeof(*entry),
gf_common_mt_dnscache_entry);
return entry;
}
/**
* gf_dnscache_entry_deinit -- Free memory used by a dnscache entry
*
* @entry: Pointer to deallocate
*/
void
gf_dnscache_entry_deinit(struct dnscache_entry *entry)
{
GF_FREE(entry->ip);
GF_FREE(entry->fqdn);
GF_FREE(entry);
}
/**
* gf_rev_dns_lookup -- Perform a reverse DNS lookup on the IP address.
*
* @ip: The IP address to perform a reverse lookup on
*
* @return: success: Allocated string containing the hostname
* failure: NULL
*/
char *
gf_rev_dns_lookup_cached(const char *ip, struct dnscache *dnscache)
{
char *fqdn = NULL;
int ret = 0;
dict_t *cache = NULL;
data_t *entrydata = NULL;
struct dnscache_entry *dnsentry = NULL;
gf_boolean_t from_cache = _gf_false;
if (!dnscache)
goto out;
if (!dnscache->cache_dict) {
dnscache->cache_dict = dict_new();
if (!dnscache->cache_dict) {
goto out;
}
}
cache = dnscache->cache_dict;
/* Quick cache lookup to see if we already hold it */
entrydata = dict_get(cache, (char *)ip);
if (entrydata) {
dnsentry = (struct dnscache_entry *)entrydata->data;
/* First check the TTL & timestamp */
if (time(NULL) - dnsentry->timestamp > dnscache->ttl) {
gf_dnscache_entry_deinit(dnsentry);
entrydata->data = NULL; /* Mark this as 'null' so
* dict_del () doesn't try free
* this after we've already
* freed it.
*/
dict_del(cache, (char *)ip); /* Remove this entry */
} else {
/* Cache entry is valid, get the FQDN and return */
fqdn = dnsentry->fqdn;
from_cache = _gf_true; /* Mark this as from cache */
goto out;
}
}
/* Get the FQDN */
ret = gf_get_hostname_from_ip((char *)ip, &fqdn);
if (ret != 0)
goto out;
if (!fqdn) {
gf_log_callingfn("resolver", GF_LOG_CRITICAL,
"Allocation failed for the host address");
goto out;
}
from_cache = _gf_false;
out:
/* Insert into the cache */
if (fqdn && !from_cache) {
struct dnscache_entry *entry = gf_dnscache_entry_init();
if (!entry) {
goto out;
}
entry->fqdn = fqdn;
if (!ip) {
gf_dnscache_entry_deinit(entry);
goto out;
}
entry->ip = gf_strdup(ip);
entry->timestamp = time(NULL);
entrydata = bin_to_data(entry, sizeof(*entry));
dict_set(cache, (char *)ip, entrydata);
}
return fqdn;
}
struct xldump {
int lineno;
};
/* to catch any format discrepencies that may arise in code */
static int
nprintf(struct xldump *dump, const char *fmt, ...)
__attribute__((__format__(__printf__, 2, 3)));
static int
nprintf(struct xldump *dump, const char *fmt, ...)
{
va_list ap;
char *msg = NULL;
char header[32];
int ret = 0;
ret = snprintf(header, 32, "%3d:", ++dump->lineno);
if (ret < 0)
goto out;
va_start(ap, fmt);
ret = vasprintf(&msg, fmt, ap);
va_end(ap);
if (-1 == ret)
goto out;
/* NOTE: No ret value from gf_msg_plain, so unable to compute printed
* characters. The return value from nprintf is not used, so for now
* living with it */
gf_msg_plain(GF_LOG_WARNING, "%s %s", header, msg);
out:
FREE(msg);
return 0;
}
static int
xldump_options(dict_t *this, char *key, data_t *value, void *d)
{
nprintf(d, " option %s %s", key, value->data);
return 0;
}
static void
xldump_subvolumes(xlator_t *this, void *d)
{
xlator_list_t *subv = NULL;
int len = 0;
char *subvstr = NULL;
if (!this->children)
return;
for (subv = this->children; subv; subv = subv->next)
len += (strlen(subv->xlator->name) + 1);
subvstr = GF_MALLOC(len, gf_common_mt_strdup);
len = 0;
for (subv = this->children; subv; subv = subv->next)
len += sprintf(subvstr + len, "%s%s", subv->xlator->name,
subv->next ? " " : "");
nprintf(d, " subvolumes %s", subvstr);
GF_FREE(subvstr);
}
static void
xldump(xlator_t *each, void *d)
{
nprintf(d, "volume %s", each->name);
nprintf(d, " type %s", each->type);
dict_foreach(each->options, xldump_options, d);
xldump_subvolumes(each, d);
nprintf(d, "end-volume");
nprintf(d, " ");
}
void
gf_log_dump_graph(FILE *specfp, glusterfs_graph_t *graph)
{
struct xldump xld = {
0,
};
gf_msg_plain(GF_LOG_WARNING, "Final graph:");
gf_msg_plain(GF_LOG_WARNING,
"+---------------------------------------"
"---------------------------------------+");
xlator_foreach_depth_first(graph->top, xldump, &xld);
gf_msg_plain(GF_LOG_WARNING,
"+---------------------------------------"
"---------------------------------------+");
}
static void
gf_dump_config_flags()
{
gf_msg_plain_nomem(GF_LOG_ALERT, "configuration details:");
/* have argp */
#ifdef HAVE_ARGP
gf_msg_plain_nomem(GF_LOG_ALERT, "argp 1");
#endif
/* ifdef if found backtrace */
#ifdef HAVE_BACKTRACE
gf_msg_plain_nomem(GF_LOG_ALERT, "backtrace 1");
#endif
/* Berkeley-DB version has cursor->get() */
#ifdef HAVE_BDB_CURSOR_GET
gf_msg_plain_nomem(GF_LOG_ALERT, "bdb->cursor->get 1");
#endif
/* Define to 1 if you have the <db.h> header file. */
#ifdef HAVE_DB_H
gf_msg_plain_nomem(GF_LOG_ALERT, "db.h 1");
#endif
/* Define to 1 if you have the <dlfcn.h> header file. */
#ifdef HAVE_DLFCN_H
gf_msg_plain_nomem(GF_LOG_ALERT, "dlfcn 1");
#endif
/* define if fdatasync exists */
#ifdef HAVE_FDATASYNC
gf_msg_plain_nomem(GF_LOG_ALERT, "fdatasync 1");
#endif
/* Define to 1 if you have the `pthread' library (-lpthread). */
#ifdef HAVE_LIBPTHREAD
gf_msg_plain_nomem(GF_LOG_ALERT, "libpthread 1");
#endif
/* define if llistxattr exists */
#ifdef HAVE_LLISTXATTR
gf_msg_plain_nomem(GF_LOG_ALERT, "llistxattr 1");
#endif
/* define if found setfsuid setfsgid */
#ifdef HAVE_SET_FSID
gf_msg_plain_nomem(GF_LOG_ALERT, "setfsid 1");
#endif
/* define if found spinlock */
#ifdef HAVE_SPINLOCK
gf_msg_plain_nomem(GF_LOG_ALERT, "spinlock 1");
#endif
/* Define to 1 if you have the <sys/epoll.h> header file. */
#ifdef HAVE_SYS_EPOLL_H
gf_msg_plain_nomem(GF_LOG_ALERT, "epoll.h 1");
#endif
/* Define to 1 if you have the <sys/extattr.h> header file. */
#ifdef HAVE_SYS_EXTATTR_H
gf_msg_plain_nomem(GF_LOG_ALERT, "extattr.h 1");
#endif
/* Define to 1 if you have the <sys/xattr.h> header file. */
#ifdef HAVE_SYS_XATTR_H
gf_msg_plain_nomem(GF_LOG_ALERT, "xattr.h 1");
#endif
/* define if found st_atim.tv_nsec */
#ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
gf_msg_plain_nomem(GF_LOG_ALERT, "st_atim.tv_nsec 1");
#endif
/* define if found st_atimespec.tv_nsec */
#ifdef HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
gf_msg_plain_nomem(GF_LOG_ALERT, "st_atimespec.tv_nsec 1");
#endif
/* Define to the full name and version of this package. */
#ifdef PACKAGE_STRING
{
char *msg = NULL;
int ret = -1;
ret = gf_asprintf(&msg, "package-string: %s", PACKAGE_STRING);
if (ret >= 0) {
gf_msg_plain_nomem(GF_LOG_ALERT, msg);
GF_FREE(msg);
}
}
#endif
return;
}
/* Obtain a backtrace and print it to the log */
void
gf_print_trace(int32_t signum, glusterfs_ctx_t *ctx)
{
char msg[1024] = {
0,
};
char timestr[64] = {
0,
};
call_stack_t *stack = NULL;
/* Now every gf_log call will just write to a buffer and when the
* buffer becomes full, its written to the log-file. Suppose the process
* crashes and prints the backtrace in the log-file, then the previous
* log information will still be in the buffer itself. So flush the
* contents of the buffer to the log file before printing the backtrace
* which helps in debugging.
*/
gf_log_flush();
gf_log_disable_suppression_before_exit(ctx);
/* Pending frames, (if any), list them in order */
gf_msg_plain_nomem(GF_LOG_ALERT, "pending frames:");
{
/* FIXME: traversing stacks outside pool->lock */
list_for_each_entry(stack, &ctx->pool->all_frames, all_frames)
{
if (stack->type == GF_OP_TYPE_FOP)
sprintf(msg, "frame : type(%d) op(%s)", stack->type,
gf_fop_list[stack->op]);
else
sprintf(msg, "frame : type(%d) op(%d)", stack->type, stack->op);
gf_msg_plain_nomem(GF_LOG_ALERT, msg);
}
}
sprintf(msg, "patchset: %s", GLUSTERFS_REPOSITORY_REVISION);
gf_msg_plain_nomem(GF_LOG_ALERT, msg);
sprintf(msg, "signal received: %d", signum);
gf_msg_plain_nomem(GF_LOG_ALERT, msg);
{
/* Dump the timestamp of the crash too, so the previous logs
can be related */
gf_time_fmt(timestr, sizeof timestr, time(NULL), gf_timefmt_FT);
gf_msg_plain_nomem(GF_LOG_ALERT, "time of crash: ");
gf_msg_plain_nomem(GF_LOG_ALERT, timestr);
}
gf_dump_config_flags();
gf_msg_backtrace_nomem(GF_LOG_ALERT, 200);
sprintf(msg, "---------");
gf_msg_plain_nomem(GF_LOG_ALERT, msg);
/* Send a signal to terminate the process */
signal(signum, SIG_DFL);
raise(signum);
}
void
trap(void)
{
}
char *
gf_trim(char *string)
{
register char *s, *t;
if (string == NULL) {
return NULL;
}
for (s = string; isspace(*s); s++)
;
if (*s == 0)
return s;
t = s + strlen(s) - 1;
while (t > s && isspace(*t))
t--;
*++t = '\0';
return s;
}
int
gf_strstr(const char *str, const char *delim, const char *match)
{
char *tmp = NULL;
char *save_ptr = NULL;
char *tmp_str = NULL;
int ret = 0;
tmp_str = strdup(str);
if (str == NULL || delim == NULL || match == NULL || tmp_str == NULL) {
gf_msg_callingfn(THIS->name, GF_LOG_WARNING, EINVAL, LG_MSG_INVALID_ARG,
"argument invalid");