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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
|
/*
Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "common-utils.h"
#include "cli1-xdr.h"
#include "xdr-generic.h"
#include "glusterd.h"
#include "glusterd-op-sm.h"
#include "glusterd-store.h"
#include "glusterd-utils.h"
#include "glusterd-volgen.h"
#include "run.h"
#include <signal.h>
static char *gsync_reserved_opts[] = {
"gluster-command",
"pid-file",
"state-file",
"session-owner",
NULL
};
int
glusterd_handle_gsync_set (rpcsvc_request_t *req)
{
int32_t ret = 0;
dict_t *dict = NULL;
gf_cli_req cli_req = {{0},};
glusterd_op_t cli_op = GD_OP_GSYNC_SET;
char *master = NULL;
char *slave = NULL;
char operation[256] = {0,};
int type = 0;
glusterd_conf_t *priv = NULL;
char *host_uuid = NULL;
GF_ASSERT (req);
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
if (!xdr_to_generic (req->msg[0], &cli_req,
(xdrproc_t)xdr_gf_cli_req)) {
req->rpc_err = GARBAGE_ARGS;
goto out;
}
if (cli_req.dict.dict_len) {
dict = dict_new ();
if (!dict)
goto out;
ret = dict_unserialize (cli_req.dict.dict_val,
cli_req.dict.dict_len,
&dict);
if (ret < 0) {
gf_log ("glusterd", GF_LOG_ERROR, "failed to "
"unserialize req-buffer to dictionary");
goto out;
} else {
dict->extra_stdfree = cli_req.dict.dict_val;
}
host_uuid = gf_strdup (uuid_utoa(priv->uuid));
if (host_uuid == NULL) {
gf_log ("glusterd", GF_LOG_ERROR, "failed to get"
"the uuid of the host machine");
ret = -1;
goto out;
}
ret = dict_set_dynstr (dict, "host-uuid", host_uuid);
if (ret)
goto out;
}
ret = dict_get_str (dict, "master", &master);
if (ret < 0) {
gf_log ("", GF_LOG_INFO, "master not found, while handling"
GEOREP" options");
master = "(No Master)";
}
ret = dict_get_str (dict, "slave", &slave);
if (ret < 0) {
gf_log ("", GF_LOG_INFO, "slave not not found, while"
"handling "GEOREP" options");
slave = "(No Slave)";
}
ret = dict_get_int32 (dict, "type", &type);
if (ret < 0) {
gf_log ("", GF_LOG_WARNING, "command type not found, while"
"handling "GEOREP" options");
goto out;
}
switch (type) {
case GF_GSYNC_OPTION_TYPE_START:
strncpy (operation, "start", sizeof (operation));
break;
case GF_GSYNC_OPTION_TYPE_STOP:
strncpy (operation, "stop", sizeof (operation));
break;
case GF_GSYNC_OPTION_TYPE_CONFIG:
strncpy (operation, "config", sizeof (operation));
break;
case GF_GSYNC_OPTION_TYPE_STATUS:
strncpy (operation, "status", sizeof (operation));
break;
case GF_GSYNC_OPTION_TYPE_ROTATE:
strncpy (operation, "rotate", sizeof(operation));
break;
}
gf_cmd_log ("volume "GEOREP, " %s command on %s,%s", operation, master,
slave);
ret = glusterd_op_begin (req, GD_OP_GSYNC_SET, dict);
gf_cmd_log ("volume "GEOREP, " %s command on %s,%s %s ", operation,
master, slave, (ret != 0)? "FAILED" : "SUCCEEDED");
out:
glusterd_friend_sm ();
glusterd_op_sm ();
if (ret) {
if (dict)
dict_unref (dict);
ret = glusterd_op_send_cli_response (cli_op, ret, 0, req,
NULL, "operation failed");
}
return ret;
}
/*****
*
* glusterd_urltransform* internal API
*
*****/
static void
glusterd_urltransform_init (runner_t *runner, const char *transname)
{
runinit (runner);
runner_add_arg (runner, GSYNCD_PREFIX"/gsyncd");
runner_argprintf (runner, "--%s-url", transname);
}
static void
glusterd_urltransform_add (runner_t *runner, const char *url)
{
runner_add_arg (runner, url);
}
static void
_glusterd_urltransform_add_iter (dict_t *dict, char *key, data_t *value, void *data)
{
runner_t *runner = (runner_t *)data;
char *slave = NULL;
slave = strchr (value->data, ':');
GF_ASSERT (slave);
slave++;
runner_add_arg (runner, slave);
}
static void
glusterd_urltransform_free (char **linearr, unsigned n)
{
int i = 0;
for (; i < n; i++)
GF_FREE (linearr[i]);
GF_FREE (linearr);
}
static int
glusterd_urltransform (runner_t *runner, char ***linearrp)
{
char **linearr = NULL;
char *line = NULL;
unsigned arr_len = 32;
unsigned arr_idx = 0;
gf_boolean_t error = _gf_false;
linearr = GF_CALLOC (arr_len, sizeof (char *), gf_gld_mt_linearr);
if (!linearr) {
error = _gf_true;
goto out;
}
runner_redir (runner, STDOUT_FILENO, RUN_PIPE);
if (runner_start (runner) != 0) {
gf_log ("", GF_LOG_ERROR, "spawning child failed");
error = _gf_true;
goto out;
}
arr_idx = 0;
for (;;) {
line = GF_MALLOC (1024, gf_gld_mt_linebuf);
if (!line) {
error = _gf_true;
goto out;
}
if (fgets (line, 1024, runner_chio (runner, STDOUT_FILENO)) ==
NULL)
break;
if (line[strlen (line) - 1] != '\n') {
GF_FREE (line);
error = _gf_true;
goto out;
}
line[strlen (line) - 1] = '\0';
if (arr_idx == arr_len) {
arr_len <<= 1;
linearr = GF_REALLOC (linearr, arr_len);
if (!linearr) {
GF_FREE (line);
error = _gf_true;
goto out;
}
}
linearr[arr_idx] = line;
arr_idx++;
}
out:
/* XXX chpid field is not exported by run API
* but runner_end() does not abort the invoked
* process (ie. it might block in waitpid(2))
* so we resort to a manual kill a the private field
*/
if (error && runner->chpid > 0)
kill (runner->chpid, SIGKILL);
if (runner_end (runner) != 0)
error = _gf_true;
if (error) {
gf_log ("", GF_LOG_ERROR, "reading data from child failed");
glusterd_urltransform_free (linearr, arr_idx);
return -1;
}
*linearrp = linearr;
return arr_idx;
}
static int
glusterd_urltransform_single (const char *url, const char *transname,
char ***linearrp)
{
runner_t runner = {0,};
glusterd_urltransform_init (&runner, transname);
glusterd_urltransform_add (&runner, url);
return glusterd_urltransform (&runner, linearrp);
}
struct dictidxmark {
unsigned isrch;
unsigned ithis;
char *ikey;
};
static void
_dict_mark_atindex (dict_t *dict, char *key, data_t *value, void *data)
{
struct dictidxmark *dim = data;
if (dim->isrch == dim->ithis)
dim->ikey = key;
dim->ithis++;
}
static char *
dict_get_by_index (dict_t *dict, unsigned i)
{
struct dictidxmark dim = {0,};
dim.isrch = i;
dict_foreach (dict, _dict_mark_atindex, &dim);
return dim.ikey;
}
static int
glusterd_get_slave (glusterd_volinfo_t *vol, const char *slaveurl, char **slavekey)
{
runner_t runner = {0,};
int n = 0;
int i = 0;
char **linearr = NULL;
glusterd_urltransform_init (&runner, "canonicalize");
dict_foreach (vol->gsync_slaves, _glusterd_urltransform_add_iter, &runner);
glusterd_urltransform_add (&runner, slaveurl);
n = glusterd_urltransform (&runner, &linearr);
if (n == -1)
return -2;
for (i = 0; i < n - 1; i++) {
if (strcmp (linearr[i], linearr[n - 1]) == 0)
break;
}
glusterd_urltransform_free (linearr, i);
if (i < n - 1)
*slavekey = dict_get_by_index (vol->gsync_slaves, i);
else
i = -1;
return i;
}
static int
glusterd_query_extutil (char *resbuf, runner_t *runner)
{
char *ptr = NULL;
int ret = 0;
runner_redir (runner, STDOUT_FILENO, RUN_PIPE);
if (runner_start (runner) != 0) {
gf_log ("", GF_LOG_ERROR, "spawning child failed");
return -1;
}
ptr = fgets(resbuf, PATH_MAX, runner_chio (runner, STDOUT_FILENO));
if (ptr)
resbuf[strlen(resbuf)-1] = '\0'; //strip off \n
ret = runner_end (runner);
if (ret)
gf_log ("", GF_LOG_ERROR, "reading data from child failed");
return ret ? -1 : 0;
}
int
glusterd_gsync_get_param_file (char *prmfile, const char *param, char *master,
char *slave, char *gl_workdir)
{
runner_t runner = {0,};
runinit (&runner);
runner_add_args (&runner, GSYNCD_PREFIX"/gsyncd", "-c", NULL);
runner_argprintf (&runner, "%s/"GSYNC_CONF, gl_workdir);
runner_argprintf (&runner, ":%s", master);
runner_add_args (&runner, slave, "--config-get", NULL);
runner_argprintf (&runner, "%s-file", param);
return glusterd_query_extutil (prmfile, &runner);
}
int
glusterd_gsync_get_session_owner (char *master, char *slave, char *session_owner,
char *gl_workdir)
{
runner_t runner = {0,};
runinit(&runner);
runner_add_args (&runner, GSYNCD_PREFIX"/gsyncd", "-c", NULL);
runner_argprintf (&runner, "%s/"GSYNC_CONF, gl_workdir);
runner_argprintf (&runner, ":%s", master);
runner_add_args (&runner, slave, "--config-get", "session-owner",
NULL);
return glusterd_query_extutil (session_owner, &runner);
}
int
glusterd_gsync_get_slave_log_file (char *master, char *slave, char *log_file)
{
int ret = -1;
runner_t runner = {0,};
char uuid_str[64] = {0,};
glusterd_conf_t *priv = NULL;
char *gl_workdir = NULL;
GF_ASSERT(THIS);
GF_ASSERT(THIS->private);
priv = THIS->private;
GF_VALIDATE_OR_GOTO("gsyncd", master, out);
GF_VALIDATE_OR_GOTO("gsyncd", slave, out);
gl_workdir = priv->workdir;
/* get the session owner for the master-slave session */
ret = glusterd_gsync_get_session_owner (master, slave, uuid_str,
gl_workdir);
if (ret)
goto out;
/* get the log file for the slave */
runinit(&runner);
runner_add_args (&runner, GSYNCD_PREFIX"/gsyncd", "-c", NULL);
runner_argprintf (&runner, "%s/"GSYNC_CONF, gl_workdir);
runner_argprintf (&runner, "--session-owner=%s", uuid_str);
runner_add_args (&runner, slave, "--config-get", "log-file", NULL);
ret = glusterd_query_extutil (log_file, &runner);
out:
return ret;
}
static int
gsyncd_getpidfile (char *master, char *slave, char *pidfile)
{
int ret = -1;
glusterd_conf_t *priv = NULL;
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
GF_VALIDATE_OR_GOTO ("gsync", master, out);
GF_VALIDATE_OR_GOTO ("gsync", slave, out);
ret = glusterd_gsync_get_param_file (pidfile, "pid", master,
slave, priv->workdir);
if (ret == -1) {
ret = -2;
gf_log ("", GF_LOG_WARNING, "failed to create the pidfile string");
goto out;
}
ret = open (pidfile, O_RDWR);
out:
return ret;
}
static int
glusterd_gsyncd_getlogfile (char *master, char *slave, char *log_file)
{
int ret = -1;
glusterd_conf_t *priv = NULL;
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
GF_VALIDATE_OR_GOTO ("gsync", master, out);
GF_VALIDATE_OR_GOTO ("gsync", slave, out);
ret = glusterd_gsync_get_param_file (log_file, "log", master,
slave, priv->workdir);
if (ret == -1) {
ret = -2;
gf_log ("", GF_LOG_WARNING, "failed to gsyncd logfile");
goto out;
}
out:
return ret;
}
static int
gsync_status_byfd (int fd)
{
GF_ASSERT (fd >= -1);
if (lockf (fd, F_TEST, 0) == -1 &&
(errno == EAGAIN || errno == EACCES))
/* gsyncd keeps the pidfile locked */
return 0;
return -1;
}
/* status: return 0 when gsync is running
* return -1 when not running
*/
int
gsync_status (char *master, char *slave, int *status)
{
char pidfile[PATH_MAX] = {0,};
int fd = -1;
fd = gsyncd_getpidfile (master, slave, pidfile);
if (fd == -2)
return -1;
*status = gsync_status_byfd (fd);
close (fd);
return 0;
}
int32_t
glusterd_gsync_volinfo_dict_set (glusterd_volinfo_t *volinfo,
char *key, char *value)
{
int32_t ret = -1;
char *gsync_status = NULL;
gsync_status = gf_strdup (value);
if (!gsync_status) {
gf_log ("", GF_LOG_ERROR, "Unable to allocate memory");
goto out;
}
ret = dict_set_dynstr (volinfo->dict, key, gsync_status);
if (ret) {
gf_log ("", GF_LOG_ERROR, "Unable to set dict");
goto out;
}
ret = 0;
out:
return 0;
}
int
gsync_verify_config_options (dict_t *dict, char **op_errstr)
{
char **resopt = NULL;
int i = 0;
char *subop = NULL;
char *slave = NULL;
char *op_name = NULL;
char *op_value = NULL;
char *t = NULL;
gf_boolean_t banned = _gf_true;
if (dict_get_str (dict, "subop", &subop) != 0) {
gf_log ("", GF_LOG_WARNING, "missing subop");
*op_errstr = gf_strdup ("Invalid config request");
return -1;
}
if (dict_get_str (dict, "slave", &slave) != 0) {
gf_log ("", GF_LOG_WARNING, GEOREP" CONFIG: no slave given");
*op_errstr = gf_strdup ("Slave required");
return -1;
}
if (strcmp (subop, "get-all") == 0)
return 0;
if (dict_get_str (dict, "op_name", &op_name) != 0) {
gf_log ("", GF_LOG_WARNING, "option name missing");
*op_errstr = gf_strdup ("Option name missing");
return -1;
}
if (runcmd (GSYNCD_PREFIX"/gsyncd", "--config-check", op_name, NULL)) {
gf_log ("", GF_LOG_WARNING, "Invalid option %s", op_name);
*op_errstr = gf_strdup ("Invalid option");
return -1;
}
if (strcmp (subop, "get") == 0)
return 0;
t = strtail (subop, "set");
if (!t)
t = strtail (subop, "del");
if (!t || (t[0] && strcmp (t, "-glob") != 0)) {
gf_log ("", GF_LOG_WARNING, "unknown subop %s", subop);
*op_errstr = gf_strdup ("Invalid config request");
return -1;
}
if (strtail (subop, "set") &&
dict_get_str (dict, "op_value", &op_value) != 0) {
gf_log ("", GF_LOG_WARNING, "missing value for set");
*op_errstr = gf_strdup ("missing value");
}
/* match option name against reserved options, modulo -/_
* difference
*/
for (resopt = gsync_reserved_opts; *resopt; resopt++) {
banned = _gf_true;
for (i = 0; (*resopt)[i] && op_name[i]; i++) {
if ((*resopt)[i] == op_name[i] ||
((*resopt)[i] == '-' && op_name[i] == '_'))
continue;
banned = _gf_false;
}
if (banned) {
gf_log ("", GF_LOG_WARNING, "Reserved option %s", op_name);
*op_errstr = gf_strdup ("Reserved option");
return -1;
break;
}
}
return 0;
}
static void
_get_status_mst_slv (dict_t *this, char *key, data_t *value, void *data)
{
glusterd_gsync_status_temp_t *param = NULL;
char *slave = NULL;
int ret = 0;
param = (glusterd_gsync_status_temp_t *)data;
GF_ASSERT (param);
GF_ASSERT (param->volinfo);
slave = strchr(value->data, ':');
if (slave)
slave ++;
else
return;
ret = glusterd_get_gsync_status_mst_slv(param->volinfo,
slave, param->rsp_dict);
}
static void
_get_max_gsync_slave_num (dict_t *this, char *key, data_t *value, void *data)
{
int tmp_slvnum = 0;
int *slvnum = (int *)data;
sscanf (key, "slave%d", &tmp_slvnum);
if (tmp_slvnum > *slvnum)
*slvnum = tmp_slvnum;
}
static int
glusterd_remove_slave_in_info (glusterd_volinfo_t *volinfo, char *slave,
char **op_errstr)
{
int ret = 0;
char *slavekey = NULL;
GF_ASSERT (volinfo);
GF_ASSERT (slave);
ret = glusterd_get_slave (volinfo, slave, &slavekey);
if (ret < 0) {
ret++;
goto out;
}
dict_del (volinfo->gsync_slaves, slavekey);
ret = glusterd_store_volinfo (volinfo,
GLUSTERD_VOLINFO_VER_AC_INCREMENT);
if (ret) {
*op_errstr = gf_strdup ("Failed to store the Volume"
"information");
goto out;
}
out:
gf_log ("", GF_LOG_DEBUG, "returning %d", ret);
return ret;
}
static int
glusterd_gsync_get_uuid (char *slave, glusterd_volinfo_t *vol,
uuid_t uuid)
{
int ret = 0;
char *slavekey = NULL;
char *slaveentry = NULL;
char *t = NULL;
GF_ASSERT (vol);
GF_ASSERT (slave);
ret = glusterd_get_slave (vol, slave, &slavekey);
if (ret < 0) {
/* XXX colliding cases of failure and non-extant
* slave... now just doing this as callers of this
* function can make sense only of -1 and 0 as retvals;
* getting at the proper semanticals will involve
* fixing callers as well.
*/
ret = -1;
goto out;
}
ret = dict_get_str (vol->gsync_slaves, slavekey, &slaveentry);
GF_ASSERT (ret == 0);
t = strchr (slaveentry, ':');
GF_ASSERT (t);
*t = '\0';
ret = uuid_parse (slaveentry, uuid);
*t = ':';
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
static int
glusterd_check_gsync_running_local (char *master, char *slave,
gf_boolean_t *is_run)
{
int ret = -1;
int ret_status = 0;
GF_ASSERT (master);
GF_ASSERT (slave);
GF_ASSERT (is_run);
*is_run = _gf_false;
ret = gsync_status (master, slave, &ret_status);
if (ret == 0 && ret_status == 0) {
*is_run = _gf_true;
} else if (ret == -1) {
gf_log ("", GF_LOG_WARNING, GEOREP" validation "
" failed");
goto out;
}
ret = 0;
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
static int
glusterd_store_slave_in_info (glusterd_volinfo_t *volinfo, char *slave,
char *host_uuid, char **op_errstr)
{
int ret = 0;
int maxslv = 0;
char **linearr = NULL;
char *value = NULL;
char *slavekey = NULL;
char *slaveentry = NULL;
char key[512] = {0, };
char *t = NULL;
GF_ASSERT (volinfo);
GF_ASSERT (slave);
GF_ASSERT (host_uuid);
ret = glusterd_get_slave (volinfo, slave, &slavekey);
switch (ret) {
case -2:
ret = -1;
goto out;
case -1:
break;
default:
GF_ASSERT (ret > 0);
ret = dict_get_str (volinfo->gsync_slaves, slavekey, &slaveentry);
GF_ASSERT (ret == 0);
/* same-name + same-uuid slave entries should have been filtered
* out in glusterd_op_verify_gsync_start_options(), so we can
* assert an uuid mismatch
*/
t = strtail (slaveentry, host_uuid);
GF_ASSERT (!t || *t != ':')
gf_log ("", GF_LOG_ERROR, GEOREP" has already been invoked for "
"the %s (master) and %s (slave) "
"from a different machine",
volinfo->volname, slave);
*op_errstr = gf_strdup (GEOREP" already running in an an"
"another machine");
ret = -1;
goto out;
}
ret = glusterd_urltransform_single (slave, "normalize", &linearr);
if (ret == -1)
goto out;
ret = gf_asprintf (&value, "%s:%s", host_uuid, linearr[0]);
glusterd_urltransform_free (linearr, 1);
if (ret == -1)
goto out;
dict_foreach (volinfo->gsync_slaves, _get_max_gsync_slave_num, &maxslv);
snprintf (key, 512, "slave%d", maxslv + 1);
ret = dict_set_dynstr (volinfo->gsync_slaves, key, value);
if (ret)
goto out;
ret = glusterd_store_volinfo (volinfo,
GLUSTERD_VOLINFO_VER_AC_INCREMENT);
if (ret) {
*op_errstr = gf_strdup ("Failed to store the Volume "
"information");
goto out;
}
ret = 0;
out:
return ret;
}
static int
glusterd_op_verify_gsync_start_options (glusterd_volinfo_t *volinfo,
char *slave, char **op_errstr)
{
int ret = -1;
gf_boolean_t is_running = _gf_false;
char msg[2048] = {0};
uuid_t uuid = {0};
glusterd_conf_t *priv = NULL;
xlator_t *this = NULL;
this = THIS;
GF_ASSERT (volinfo);
GF_ASSERT (slave);
GF_ASSERT (op_errstr);
GF_ASSERT (this && this->private);
priv = this->private;
if (GLUSTERD_STATUS_STARTED != volinfo->status) {
snprintf (msg, sizeof (msg), "Volume %s needs to be started "
"before "GEOREP" start", volinfo->volname);
goto out;
}
/*Check if the gsync is already started in cmd. inited host
* If so initiate add it into the glusterd's priv*/
ret = glusterd_gsync_get_uuid (slave, volinfo, uuid);
if ((ret == 0) && (uuid_compare (priv->uuid, uuid) == 0)) {
ret = glusterd_check_gsync_running_local (volinfo->volname,
slave, &is_running);
if (ret) {
snprintf (msg, sizeof (msg), GEOREP" start option "
"validation failed ");
goto out;
}
if (_gf_true == is_running) {
snprintf (msg, sizeof (msg), GEOREP " session between"
" %s & %s already started", volinfo->volname,
slave);
ret = -1;
goto out;
}
}
ret = 0;
out:
if (ret && (msg[0] != '\0')) {
*op_errstr = gf_strdup (msg);
}
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
int
glusterd_check_gsync_running (glusterd_volinfo_t *volinfo, gf_boolean_t *flag)
{
GF_ASSERT (volinfo);
GF_ASSERT (flag);
if (volinfo->gsync_slaves->count)
*flag = _gf_true;
else
*flag = _gf_false;
return 0;
}
static int
glusterd_op_verify_gsync_running (glusterd_volinfo_t *volinfo,
char *slave, char **op_errstr)
{
int ret = -1;
char msg[2048] = {0};
uuid_t uuid = {0};
GF_ASSERT (THIS && THIS->private);
GF_ASSERT (volinfo);
GF_ASSERT (slave);
GF_ASSERT (op_errstr);
if (GLUSTERD_STATUS_STARTED != volinfo->status) {
snprintf (msg, sizeof (msg), "Volume %s needs to be started "
"before "GEOREP" start", volinfo->volname);
goto out;
}
ret = glusterd_gsync_get_uuid (slave, volinfo, uuid);
if (ret == -1) {
snprintf (msg, sizeof (msg), GEOREP" session between %s & %s"
" not active", volinfo->volname, slave);
goto out;
}
ret = 0;
out:
if (ret && (msg[0] != '\0')) {
*op_errstr = gf_strdup (msg);
}
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
static int
glusterd_verify_gsync_status_opts (dict_t *dict, char **op_errstr)
{
char *slave = NULL;
char *volname = NULL;
char errmsg[PATH_MAX] = {0, };
gf_boolean_t exists = _gf_false;
glusterd_volinfo_t *volinfo = NULL;
int ret = 0;
ret = dict_get_str (dict, "master", &volname);
if (ret < 0) {
ret = 0;
goto out;
}
exists = glusterd_check_volume_exists (volname);
ret = glusterd_volinfo_find (volname, &volinfo);
if ((ret) || (!exists)) {
gf_log ("", GF_LOG_WARNING, "volume name does not exist");
snprintf (errmsg, sizeof(errmsg), "Volume name %s does not"
" exist", volname);
*op_errstr = gf_strdup (errmsg);
ret = -1;
goto out;
}
ret = dict_get_str (dict, "slave", &slave);
if (ret < 0) {
ret = 0;
goto out;
}
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
static int
glusterd_op_gsync_args_get (dict_t *dict, char **op_errstr,
char **master, char **slave)
{
int ret = -1;
GF_ASSERT (dict);
GF_ASSERT (op_errstr);
GF_ASSERT (master);
GF_ASSERT (slave);
ret = dict_get_str (dict, "master", master);
if (ret < 0) {
gf_log ("", GF_LOG_WARNING, "master not found");
*op_errstr = gf_strdup ("master not found");
goto out;
}
ret = dict_get_str (dict, "slave", slave);
if (ret < 0) {
gf_log ("", GF_LOG_WARNING, "slave not found");
*op_errstr = gf_strdup ("slave not found");
goto out;
}
ret = 0;
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
int
glusterd_op_stage_gsync_set (dict_t *dict, char **op_errstr)
{
int ret = 0;
int type = 0;
char *volname = NULL;
char *slave = NULL;
gf_boolean_t exists = _gf_false;
glusterd_volinfo_t *volinfo = NULL;
char errmsg[PATH_MAX] = {0,};
dict_t *ctx = NULL;
ret = dict_get_int32 (dict, "type", &type);
if (ret < 0) {
gf_log ("", GF_LOG_WARNING, "command type not found");
*op_errstr = gf_strdup ("command unsuccessful");
goto out;
}
switch (type) {
case GF_GSYNC_OPTION_TYPE_STATUS:
ret = glusterd_verify_gsync_status_opts (dict, op_errstr);
goto out;
case GF_GSYNC_OPTION_TYPE_CONFIG:
ret = gsync_verify_config_options (dict, op_errstr);
goto out;
case GF_GSYNC_OPTION_TYPE_ROTATE:
/* checks same as status mode */
ret = glusterd_verify_gsync_status_opts(dict, op_errstr);
goto out;
}
ret = glusterd_op_gsync_args_get (dict, op_errstr, &volname, &slave);
if (ret)
goto out;
exists = glusterd_check_volume_exists (volname);
ret = glusterd_volinfo_find (volname, &volinfo);
if ((ret) || (!exists)) {
gf_log ("", GF_LOG_WARNING, "volume name does not exist");
snprintf (errmsg, sizeof(errmsg), "Volume name %s does not"
" exist", volname);
*op_errstr = gf_strdup (errmsg);
ret = -1;
goto out;
}
switch (type) {
case GF_GSYNC_OPTION_TYPE_START:
ret = glusterd_op_verify_gsync_start_options (volinfo, slave,
op_errstr);
if (ret)
goto out;
ctx = glusterd_op_get_ctx();
if (ctx) {
/*gsyncd does a fuse mount to start the geo-rep session*/
if (!glusterd_is_fuse_available ()) {
gf_log ("glusterd", GF_LOG_ERROR, "Unable to open"
" /dev/fuse (%s), geo-replication start"
" failed", strerror (errno));
snprintf (errmsg, sizeof(errmsg),
"fuse unvailable");
*op_errstr = gf_strdup (errmsg);
ret = -1;
goto out;
}
}
break;
case GF_GSYNC_OPTION_TYPE_STOP:
ret = glusterd_op_verify_gsync_running (volinfo, slave,
op_errstr);
break;
}
out:
return ret;
}
int
stop_gsync (char *master, char *slave, char **msg)
{
int32_t ret = 0;
int pfd = -1;
pid_t pid = 0;
char pidfile[PATH_MAX] = {0,};
char buf [1024] = {0,};
int i = 0;
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
pfd = gsyncd_getpidfile (master, slave, pidfile);
if (pfd == -2) {
gf_log ("", GF_LOG_ERROR, GEOREP" stop validation "
" failed for %s & %s", master, slave);
ret = -1;
goto out;
}
if (gsync_status_byfd (pfd) == -1) {
gf_log ("", GF_LOG_ERROR, "gsyncd b/w %s & %s is not"
" running", master, slave);
if (msg)
*msg = gf_strdup ("Warning: "GEOREP" session was in "
"corrupt state");
/* monitor gsyncd already dead */
goto out;
}
ret = read (pfd, buf, 1024);
if (ret > 0) {
pid = strtol (buf, NULL, 10);
ret = kill (-pid, SIGTERM);
if (ret) {
gf_log ("", GF_LOG_WARNING,
"failed to kill gsyncd");
goto out;
}
for (i = 0; i < 20; i++) {
if (gsync_status_byfd (pfd) == -1) {
/* monitor gsyncd is dead but worker may
* still be alive, give some more time
* before SIGKILL (hack)
*/
usleep (50000);
break;
}
usleep (50000);
}
kill (-pid, SIGKILL);
unlink (pidfile);
}
ret = 0;
out:
close (pfd);
return ret;
}
int
glusterd_check_restart_gsync_session (glusterd_volinfo_t *volinfo, char *slave,
dict_t *resp_dict);
int
glusterd_gsync_configure (glusterd_volinfo_t *volinfo, char *slave,
dict_t *dict, dict_t *resp_dict, char **op_errstr)
{
int32_t ret = -1;
char *op_name = NULL;
char *op_value = NULL;
runner_t runner = {0,};
glusterd_conf_t *priv = NULL;
char *subop = NULL;
char *master = NULL;
GF_ASSERT (slave);
GF_ASSERT (op_errstr);
GF_ASSERT (dict);
GF_ASSERT (resp_dict);
ret = dict_get_str (dict, "subop", &subop);
if (ret != 0)
goto out;
if (strcmp (subop, "get") == 0 || strcmp (subop, "get-all") == 0) {
/* deferred to cli */
gf_log ("", GF_LOG_DEBUG, "Returning 0");
return 0;
}
ret = dict_get_str (dict, "op_name", &op_name);
if (ret != 0)
goto out;
if (strtail (subop, "set")) {
ret = dict_get_str (dict, "op_value", &op_value);
if (ret != 0)
goto out;
}
if (THIS)
priv = THIS->private;
if (priv == NULL) {
gf_log ("", GF_LOG_ERROR, "priv of glusterd not present");
*op_errstr = gf_strdup ("glusterd defunct");
goto out;
}
master = "";
runinit (&runner);
runner_add_args (&runner, GSYNCD_PREFIX"/gsyncd", "-c", NULL);
runner_argprintf (&runner, "%s/"GSYNC_CONF, priv->workdir);
if (volinfo) {
master = volinfo->volname;
runner_argprintf (&runner, ":%s", master);
}
runner_add_arg (&runner, slave);
runner_argprintf (&runner, "--config-%s", subop);
runner_add_arg (&runner, op_name);
if (op_value)
runner_add_arg (&runner, op_value);
ret = runner_run (&runner);
if (ret) {
gf_log ("", GF_LOG_WARNING, "gsyncd failed to "
"%s %s option for %s %s peers",
subop, op_name, master, slave);
gf_asprintf (op_errstr, GEOREP" config-%s failed for %s %s",
subop, master, slave);
goto out;
}
ret = 0;
gf_asprintf (op_errstr, "config-%s successful", subop);
out:
if (!ret && volinfo) {
ret = glusterd_check_restart_gsync_session (volinfo, slave,
resp_dict);
if (ret)
*op_errstr = gf_strdup ("internal error");
}
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
int
glusterd_gsync_read_frm_status (char *path, char *data)
{
int ret = 0;
FILE *status_file = NULL;
GF_ASSERT (path);
GF_ASSERT (data);
status_file = fopen (path, "r");
if (status_file == NULL) {
gf_log ("", GF_LOG_WARNING, "Unable to read gsyncd status"
" file");
return -1;
}
ret = fread (data, PATH_MAX, 1, status_file);
if (ret < 0) {
gf_log ("", GF_LOG_WARNING, "Status file of gsyncd is corrupt");
return -1;
}
data[strlen(data)-1] = '\0';
return 0;
}
int
glusterd_read_status_file (char *master, char *slave,
dict_t *dict)
{
glusterd_conf_t *priv = NULL;
int ret = 0;
char statusfile[PATH_MAX] = {0, };
char buff[PATH_MAX] = {0, };
char mst[PATH_MAX] = {0, };
char slv[PATH_MAX] = {0, };
char sts[PATH_MAX] = {0, };
int gsync_count = 0;
int status = 0;
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
ret = glusterd_gsync_get_param_file (statusfile, "state", master,
slave, priv->workdir);
if (ret) {
gf_log ("", GF_LOG_WARNING, "Unable to get the name of status"
"file for %s(master), %s(slave)", master, slave);
goto out;
}
ret = gsync_status (master, slave, &status);
if (ret == 0 && status == -1) {
strncpy (buff, "corrupt", sizeof (buff));
goto done;
} else if (ret == -1)
goto out;
ret = glusterd_gsync_read_frm_status (statusfile, buff);
if (ret) {
gf_log ("", GF_LOG_WARNING, "Unable to read the status"
"file for %s(master), %s(slave)", master, slave);
goto out;
}
done:
ret = dict_get_int32 (dict, "gsync-count", &gsync_count);
if (ret)
gsync_count = 1;
else
gsync_count++;
snprintf (mst, sizeof (mst), "master%d", gsync_count);
ret = dict_set_dynstr (dict, mst, gf_strdup (master));
if (ret)
goto out;
snprintf (slv, sizeof (slv), "slave%d", gsync_count);
ret = dict_set_dynstr (dict, slv, gf_strdup (slave));
if (ret)
goto out;
snprintf (sts, sizeof (slv), "status%d", gsync_count);
ret = dict_set_dynstr (dict, sts, gf_strdup (buff));
if (ret)
goto out;
ret = dict_set_int32 (dict, "gsync-count", gsync_count);
if (ret)
goto out;
ret = 0;
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d ", ret);
return ret;
}
int
glusterd_check_restart_gsync_session (glusterd_volinfo_t *volinfo, char *slave,
dict_t *resp_dict)
{
int ret = 0;
uuid_t uuid = {0, };
glusterd_conf_t *priv = NULL;
char *status_msg = NULL;
GF_ASSERT (volinfo);
GF_ASSERT (slave);
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
if (glusterd_gsync_get_uuid (slave, volinfo, uuid))
/* session does not exist, nothing to do */
goto out;
if (uuid_compare (priv->uuid, uuid) == 0) {
ret = stop_gsync (volinfo->volname, slave, &status_msg);
if (ret == 0 && status_msg)
ret = dict_set_str (resp_dict, "gsync-status",
status_msg);
if (ret == 0)
ret = glusterd_start_gsync (volinfo, slave,
uuid_utoa(priv->uuid), NULL);
}
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
int32_t
glusterd_marker_create_volfile (glusterd_volinfo_t *volinfo)
{
int32_t ret = 0;
ret = glusterd_create_volfiles_and_notify_services (volinfo);
if (ret) {
gf_log ("", GF_LOG_ERROR, "Unable to create volfile"
" for setting of marker while '"GEOREP" start'");
ret = -1;
goto out;
}
ret = glusterd_store_volinfo (volinfo, GLUSTERD_VOLINFO_VER_AC_INCREMENT);
if (ret)
goto out;
if (GLUSTERD_STATUS_STARTED == volinfo->status)
ret = glusterd_nodesvcs_handle_graph_change (volinfo);
ret = 0;
out:
return ret;
}
int
glusterd_set_marker_gsync (glusterd_volinfo_t *volinfo)
{
int ret = -1;
int marker_set = _gf_false;
char *gsync_status = NULL;
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
marker_set = glusterd_volinfo_get_boolean (volinfo, VKEY_MARKER_XTIME);
if (marker_set == -1) {
gf_log ("", GF_LOG_ERROR, "failed to get the marker status");
ret = -1;
goto out;
}
if (marker_set == _gf_false) {
gsync_status = gf_strdup ("on");
if (gsync_status == NULL) {
ret = -1;
goto out;
}
ret = glusterd_gsync_volinfo_dict_set (volinfo,
VKEY_MARKER_XTIME, gsync_status);
if (ret < 0)
goto out;
ret = glusterd_marker_create_volfile (volinfo);
if (ret) {
gf_log ("", GF_LOG_ERROR, "Setting dict failed");
goto out;
}
}
ret = 0;
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
int
glusterd_get_gsync_status_mst_slv( glusterd_volinfo_t *volinfo,
char *slave, dict_t *rsp_dict)
{
uuid_t uuid = {0, };
glusterd_conf_t *priv = NULL;
int ret = 0;
GF_ASSERT (volinfo);
GF_ASSERT (slave);
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
ret = glusterd_gsync_get_uuid (slave, volinfo, uuid);
if ((ret == 0) && (uuid_compare (priv->uuid, uuid) != 0))
goto out;
if (ret) {
ret = 0;
gf_log ("", GF_LOG_INFO, "geo-replication status %s %s :"
"session is not active", volinfo->volname, slave);
goto out;
}
ret = glusterd_read_status_file (volinfo->volname, slave, rsp_dict);
out:
gf_log ("", GF_LOG_DEBUG, "Returning with %d", ret);
return ret;
}
static int
glusterd_get_gsync_status_mst (glusterd_volinfo_t *volinfo, dict_t *rsp_dict)
{
glusterd_gsync_status_temp_t param = {0, };
GF_ASSERT (volinfo);
param.rsp_dict = rsp_dict;
param.volinfo = volinfo;
dict_foreach (volinfo->gsync_slaves, _get_status_mst_slv, ¶m);
return 0;
}
static int
glusterd_get_gsync_status_all ( dict_t *rsp_dict)
{
int32_t ret = 0;
glusterd_conf_t *priv = NULL;
glusterd_volinfo_t *volinfo = NULL;
GF_ASSERT (THIS);
priv = THIS->private;
GF_ASSERT (priv);
list_for_each_entry (volinfo, &priv->volumes, vol_list) {
ret = glusterd_get_gsync_status_mst (volinfo, rsp_dict);
if (ret)
goto out;
}
out:
gf_log ("", GF_LOG_DEBUG, "Returning with %d", ret);
return ret;
}
static int
glusterd_get_gsync_status (dict_t *dict, char **op_errstr, dict_t *rsp_dict)
{
char *slave = NULL;
char *volname = NULL;
char errmsg[PATH_MAX] = {0, };
gf_boolean_t exists = _gf_false;
glusterd_volinfo_t *volinfo = NULL;
int ret = 0;
ret = dict_get_str (dict, "master", &volname);
if (ret < 0){
ret = glusterd_get_gsync_status_all (rsp_dict);
goto out;
}
exists = glusterd_check_volume_exists (volname);
ret = glusterd_volinfo_find (volname, &volinfo);
if ((ret) || (!exists)) {
gf_log ("", GF_LOG_WARNING, "volume name does not exist");
snprintf (errmsg, sizeof(errmsg), "Volume name %s does not"
" exist", volname);
*op_errstr = gf_strdup (errmsg);
ret = -1;
goto out;
}
ret = dict_get_str (dict, "slave", &slave);
if (ret < 0) {
ret = glusterd_get_gsync_status_mst (volinfo, rsp_dict);
goto out;
}
ret = glusterd_get_gsync_status_mst_slv (volinfo, slave, rsp_dict);
out:
gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
return ret;
}
int
glusterd_send_sigstop (pid_t pid)
{
int ret = 0;
ret = kill (pid, SIGSTOP);
if (ret)
gf_log ("", GF_LOG_ERROR, GEOREP"failed to send SIGSTOP signal");
return ret;
}
int
glusterd_send_sigcont (pid_t pid)
{
int ret = 0;
ret = kill (pid, SIGCONT);
if (ret)
gf_log ("", GF_LOG_ERROR, GEOREP"failed to send SIGCONT signal");
return ret;
}
/*
* Log rotations flow is something like this:
* - Send SIGSTOP to process group (this will stop monitor/worker process
* and also the slave if it's local)
* - Rotate log file for monitor/worker
* - Rotate log file for slave if it's local
* - Send SIGCONT to the process group. Monitor wakes up, kills the worker
* (this is done in the SIGCONT handler), which results in the termination
* of the slave (local/remote). After returning from signal handler,
* monitor detects absence of worker and starts it again, which in-turn
* starts the slave.
*/
int
glusterd_send_log_rotate_signal (pid_t pid, char *logfile1, char *logfile2)
{
int ret = 0;
struct stat stbuf = {0,};
char rlogfile[PATH_MAX] = {0,};
time_t rottime = 0;
ret = glusterd_send_sigstop (-pid);
rottime = time (NULL);
snprintf (rlogfile, sizeof (rlogfile), "%s.%"PRIu64, logfile1,
(uint64_t) rottime);
ret = rename (logfile1, rlogfile);
if (ret)
gf_log ("", GF_LOG_ERROR, "rename failed for geo-rep log file");
snprintf (rlogfile, sizeof (rlogfile), "%s.%"PRIu64, logfile2,
(uint64_t) rottime);
ret = stat (logfile2, &stbuf);
if (ret) {
if (errno != ENOENT)
gf_log("", GF_LOG_ERROR, "stat failed for slave log"
" file: %s", logfile2);
else {
gf_log ("", GF_LOG_DEBUG, "Slave is not local, skipping rotation");
ret = 0;
}
goto out;
}
ret = rename (logfile2, rlogfile);
if (ret)
gf_log ("", GF_LOG_ERROR, "rename failed for geo-rep slave log file");
out:
ret = glusterd_send_sigcont (-pid);
return ret;
}
int
glusterd_get_pid_from_file (char *master, char *slave, pid_t *pid)
{
int ret = -1;
int pfd = 0;
char pidfile[PATH_MAX] = {0,};
char buff[1024] = {0,};
pfd = gsyncd_getpidfile (master, slave, pidfile);
if (pfd == -2) {
gf_log ("", GF_LOG_ERROR, GEOREP" log-rotate validation "
" failed for %s & %s", master, slave);
goto out;
}
if (gsync_status_byfd (pfd) == -1) {
gf_log ("", GF_LOG_ERROR, "gsyncd b/w %s & %s is not"
" running", master, slave);
goto out;
}
ret = read (pfd, buff, 1024);
if (ret < 0) {
gf_log ("", GF_LOG_ERROR, GEOREP" cannot read pid from pid-file");
goto out;
}
close(pfd);
*pid = strtol (buff, NULL, 10);
ret = 0;
out:
return ret;
}
int
glusterd_do_gsync_log_rotate (char *master, char *slave, uuid_t *uuid, char **op_errstr)
{
int ret = 0;
glusterd_conf_t *priv = NULL;
pid_t pid = 0;
char log_file1[PATH_MAX] = {0,};
char log_file2[PATH_MAX] = {0,};
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
priv = THIS->private;
ret = glusterd_get_pid_from_file (master, slave, &pid);
if (ret)
goto out;
/* log file */
ret = glusterd_gsyncd_getlogfile (master, slave, log_file1);
if (ret)
goto out;
/* slave log file */
ret = glusterd_gsync_get_slave_log_file (master, slave, log_file2);
if (ret)
goto out;
ret = glusterd_send_log_rotate_signal (pid, log_file1, log_file2);
out:
if (ret && op_errstr)
*op_errstr = gf_strdup("Error rotating log file");
return ret;
}
int
glusterd_do_gsync_log_rotation_mst_slv (glusterd_volinfo_t *volinfo, char *slave,
char **op_errstr)
{
uuid_t uuid = {0, };
glusterd_conf_t *priv = NULL;
int ret = 0;
char errmsg[1024] = {0,};
xlator_t *this = NULL;
GF_ASSERT (volinfo);
GF_ASSERT (slave);
GF_ASSERT (THIS);
this = THIS;
GF_ASSERT (this->private);
priv = this->private;
ret = glusterd_gsync_get_uuid (slave, volinfo, uuid);
if ((ret == 0) && (uuid_compare (priv->uuid, uuid) != 0))
goto out;
if (ret) {
snprintf(errmsg, sizeof(errmsg), "geo-replication session b/w %s %s not active",
volinfo->volname, slave);
gf_log (this->name, GF_LOG_WARNING, "%s", errmsg);
if (op_errstr)
*op_errstr = gf_strdup(errmsg);
goto out;
}
ret = glusterd_do_gsync_log_rotate (volinfo->volname, slave, &uuid, op_errstr);
out:
gf_log (this->name, GF_LOG_DEBUG, "Returning with %d", ret);
return ret;
}
static void
_iterate_log_rotate_mst_slv (dict_t *this, char *key, data_t *value, void *data)
{
glusterd_gsync_status_temp_t *param = NULL;
char *slave = NULL;
param = (glusterd_gsync_status_temp_t *) data;
GF_ASSERT (param);
GF_ASSERT (param->volinfo);
slave = strchr (value->data, ':');
if (slave)
slave++;
else {
gf_log ("", GF_LOG_ERROR, "geo-replication log-rotate: slave (%s) "
"not conforming to format", slave);
return;
}
(void) glusterd_do_gsync_log_rotation_mst_slv (param->volinfo, slave, NULL);
}
int
glusterd_do_gsync_log_rotation_mst (glusterd_volinfo_t *volinfo)
{
glusterd_gsync_status_temp_t param = {0, };
GF_ASSERT (volinfo);
param.volinfo = volinfo;
dict_foreach (volinfo->gsync_slaves, _iterate_log_rotate_mst_slv, ¶m);
return 0;
}
static int
glusterd_rotate_gsync_all ()
{
int32_t ret = 0;
glusterd_conf_t *priv = NULL;
glusterd_volinfo_t *volinfo = NULL;
GF_ASSERT (THIS);
priv = THIS->private;
GF_ASSERT (priv);
list_for_each_entry (volinfo, &priv->volumes, vol_list) {
ret = glusterd_do_gsync_log_rotation_mst (volinfo);
if (ret)
goto out;
}
out:
gf_log ("", GF_LOG_DEBUG, "Returning with %d", ret);
return ret;
}
static int
glusterd_rotate_gsync_logs (dict_t *dict, char **op_errstr, dict_t *rsp_dict)
{
char *slave = NULL;
char *volname = NULL;
char errmsg[1024] = {0,};
gf_boolean_t exists = _gf_false;
glusterd_volinfo_t *volinfo = NULL;
int ret = 0;
ret = dict_get_str (dict, "master", &volname);
if (ret < 0) {
ret = glusterd_rotate_gsync_all ();
goto out;
}
exists = glusterd_check_volume_exists (volname);
ret = glusterd_volinfo_find (volname, &volinfo);
if ((ret) || (!exists)) {
snprintf (errmsg, sizeof(errmsg), "Volume %s does not"
" exist", volname);
gf_log ("", GF_LOG_WARNING, "%s", errmsg);
*op_errstr = gf_strdup (errmsg);
ret = -1;
goto out;
}
ret = dict_get_str (dict, "slave", &slave);
if (ret < 0) {
ret = glusterd_do_gsync_log_rotation_mst (volinfo);
goto out;
}
ret = glusterd_do_gsync_log_rotation_mst_slv (volinfo, slave, op_errstr);
out:
return ret;
}
int
glusterd_op_gsync_set (dict_t *dict, char **op_errstr, dict_t *rsp_dict)
{
int32_t ret = -1;
int32_t type = -1;
dict_t *ctx = NULL;
dict_t *resp_dict = NULL;
char *host_uuid = NULL;
char *slave = NULL;
char *volname = NULL;
glusterd_volinfo_t *volinfo = NULL;
glusterd_conf_t *priv = NULL;
char *status_msg = NULL;
uuid_t uuid = {0, };
GF_ASSERT (THIS);
GF_ASSERT (THIS->private);
GF_ASSERT (dict);
GF_ASSERT (op_errstr);
priv = THIS->private;
ret = dict_get_int32 (dict, "type", &type);
if (ret < 0)
goto out;
ret = dict_get_str (dict, "host-uuid", &host_uuid);
if (ret < 0)
goto out;
ctx = glusterd_op_get_ctx ();
resp_dict = ctx ? ctx : rsp_dict;
GF_ASSERT (resp_dict);
if (type == GF_GSYNC_OPTION_TYPE_STATUS) {
ret = glusterd_get_gsync_status (dict, op_errstr, resp_dict);
goto out;
}
if (type == GF_GSYNC_OPTION_TYPE_ROTATE) {
ret = glusterd_rotate_gsync_logs (dict, op_errstr, resp_dict);
goto out;
}
ret = dict_get_str (dict, "slave", &slave);
if (ret < 0)
goto out;
if (dict_get_str (dict, "master", &volname) == 0) {
ret = glusterd_volinfo_find (volname, &volinfo);
if (ret) {
gf_log ("", GF_LOG_WARNING, "Volinfo for %s (master) not found",
volname);
goto out;
}
}
if (type == GF_GSYNC_OPTION_TYPE_CONFIG) {
ret = glusterd_gsync_configure (volinfo, slave, dict, resp_dict,
op_errstr);
goto out;
}
if (!volinfo) {
ret = -1;
goto out;
}
if (type == GF_GSYNC_OPTION_TYPE_START) {
ret = glusterd_set_marker_gsync (volinfo);
if (ret != 0) {
gf_log ("", GF_LOG_WARNING, "marker start failed");
*op_errstr = gf_strdup ("failed to initialize indexing");
ret = -1;
goto out;
}
ret = glusterd_store_slave_in_info(volinfo, slave,
host_uuid, op_errstr);
if (ret)
goto out;
ret = glusterd_start_gsync (volinfo, slave, host_uuid,
op_errstr);
}
if (type == GF_GSYNC_OPTION_TYPE_STOP) {
ret = glusterd_gsync_get_uuid (slave, volinfo, uuid);
if (ret) {
gf_log ("", GF_LOG_WARNING, GEOREP" is not set up for"
"%s(master) and %s(slave)", volname, slave);
*op_errstr = strdup (GEOREP" is not set up");
goto out;
}
ret = glusterd_remove_slave_in_info(volinfo, slave, op_errstr);
if (ret)
goto out;
if (uuid_compare (priv->uuid, uuid) != 0) {
goto out;
}
ret = stop_gsync (volname, slave, &status_msg);
if (ret == 0 && status_msg)
ret = dict_set_str (resp_dict, "gsync-status",
status_msg);
if (ret != 0)
*op_errstr = gf_strdup ("internal error");
}
out:
gf_log ("", GF_LOG_DEBUG,"Returning %d", ret);
return ret;
}
|