summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/commands.cc
blob: f45b230171e0c54311fef509e2b48ab81554cb01 (plain)
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
/* Copyright (C) 2004 MySQL AB

   This program 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 2 of the License, or
   (at your option) any later version.

   This program 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
#pragma implementation
#endif

#include "commands.h"

#include <my_global.h>
#include <m_ctype.h>
#include <mysql.h>
#include <my_dir.h>

#include "buffer.h"
#include "guardian.h"
#include "instance_map.h"
#include "log.h"
#include "manager.h"
#include "messages.h"
#include "mysqld_error.h"
#include "mysql_manager_error.h"
#include "options.h"
#include "priv.h"
#include "protocol.h"


/*
  modify_defaults_to_im_error -- a map of error codes of
  mysys::modify_defaults_file() into Instance Manager error codes.
*/

static const int modify_defaults_to_im_error[]= { 0, ER_OUT_OF_RESOURCES,
                                                  ER_ACCESS_OPTION_FILE };


/*
  Add a string to a buffer.

  SYNOPSYS
    put_to_buff()
    buff              buffer to add the string
    str               string to add
    position          offset in the buff to add a string

  DESCRIPTION

  Function to add a string to the buffer. It is different from
  store_to_protocol_packet, which is used in the protocol.cc.
  The last one also stores the length of the string in a special way.
  This is required for MySQL client/server protocol support only.

  RETURN
    0 - ok
    1 - error occured
*/

static inline int put_to_buff(Buffer *buff, const char *str, uint *position)
{
  uint len= strlen(str);
  if (buff->append(*position, str, len))
    return 1;

  *position+= len;
  return 0;
}


static int parse_version_number(const char *version_str, char *version,
                                uint version_size)
{
  const char *start= version_str;
  const char *end;

  // skip garbage
  while (!my_isdigit(default_charset_info, *start))
    start++;

  end= start;
  // skip digits and dots
  while (my_isdigit(default_charset_info, *end) || *end == '.')
    end++;

  if ((uint)(end - start) >= version_size)
    return -1;

  strncpy(version, start, end-start);
  version[end-start]= '\0';

  return 0;
}


/**************************************************************************
 Implementation of Instance_name.
**************************************************************************/

Instance_name::Instance_name(const LEX_STRING *name)
{
  str.str= str_buffer;
  str.length= name->length;

  if (str.length > MAX_INSTANCE_NAME_SIZE - 1)
    str.length= MAX_INSTANCE_NAME_SIZE - 1;

  strmake(str.str, name->str, str.length);
}

/**************************************************************************
 Implementation of Show_instances.
**************************************************************************/

/*
  Implementation of SHOW INSTANCES statement.

  Possible error codes:
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Show_instances::execute(st_net *net, ulong connection_id)
{
  int err_code;

  if ((err_code= write_header(net)) ||
      (err_code= write_data(net)))
    return err_code;

  if (send_eof(net) || net_flush(net))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


int Show_instances::write_header(st_net *net)
{
  LIST name, state;
  LEX_STRING name_field, state_field;
  LIST *field_list;

  name_field.str= (char *) "instance_name";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;

  state_field.str= (char *) "state";
  state_field.length= DEFAULT_FIELD_LENGTH;
  state.data= &state_field;

  field_list= list_add(NULL, &state);
  field_list= list_add(field_list, &name);

  return send_fields(net, field_list) ? ER_OUT_OF_RESOURCES : 0;
}


int Show_instances::write_data(st_net *net)
{
  my_bool err_status= FALSE;

  Instance *instance;
  Instance_map::Iterator iterator(instance_map);

  instance_map->guardian->lock();
  instance_map->lock();

  while ((instance= iterator.next()))
  {
    Buffer send_buf;  /* buffer for packets */
    uint pos= 0;

    const char *instance_name= instance->options.instance_name.str;
    const char *state_name= instance_map->get_instance_state_name(instance);

    if (store_to_protocol_packet(&send_buf, instance_name, &pos) ||
        store_to_protocol_packet(&send_buf, state_name, &pos) ||
        my_net_write(net, send_buf.buffer, pos))
    {
      err_status= TRUE;
      break;
    }
  }

  instance_map->unlock();
  instance_map->guardian->unlock();

  return err_status ? ER_OUT_OF_RESOURCES : 0;
}


/**************************************************************************
 Implementation of Flush_instances.
**************************************************************************/

/*
  Implementation of FLUSH INSTANCES statement.

  Possible error codes:
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
    ER_THERE_IS_ACTIVE_INSTACE  If there is an active instance
*/

int Flush_instances::execute(st_net *net, ulong connection_id)
{
  instance_map->guardian->lock();
  instance_map->lock();

  if (instance_map->is_there_active_instance())
  {
    instance_map->unlock();
    instance_map->guardian->unlock();
    return ER_THERE_IS_ACTIVE_INSTACE;
  }

  if (instance_map->flush_instances())
  {
    instance_map->unlock();
    instance_map->guardian->unlock();
    return ER_OUT_OF_RESOURCES;
  }

  instance_map->unlock();
  instance_map->guardian->unlock();

  return net_send_ok(net, connection_id, NULL) ? ER_OUT_OF_RESOURCES : 0;
}


/**************************************************************************
 Implementation of Abstract_instance_cmd.
**************************************************************************/

Abstract_instance_cmd::Abstract_instance_cmd(
  Instance_map *instance_map_arg, const LEX_STRING *instance_name_arg)
  :Command(instance_map_arg),
  instance_name(instance_name_arg)
{
  /*
    MT-NOTE: we can not make a search for Instance object here,
    because it can dissappear after releasing the lock.
  */
}


int Abstract_instance_cmd::execute(st_net *net, ulong connection_id)
{
  int err_code;

  instance_map->lock();

  {
    Instance *instance= instance_map->find(get_instance_name());

    if (!instance)
    {
      instance_map->unlock();
      return ER_BAD_INSTANCE_NAME;
    }

    err_code= execute_impl(net, instance);
  }

  instance_map->unlock();

  if (!err_code)
    err_code= send_ok_response(net, connection_id);

  return err_code;
}


/**************************************************************************
 Implementation of Show_instance_status.
**************************************************************************/

Show_instance_status::Show_instance_status(Instance_map *instance_map_arg,
                                           const LEX_STRING *instance_name_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg)
{
}


/*
  Implementation of SHOW INSTANCE STATUS statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Show_instance_status::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  if ((err_code= write_header(net)) ||
      (err_code= write_data(net, instance)))
    return err_code;

  return 0;
}


int Show_instance_status::send_ok_response(st_net *net, ulong connection_id)
{
  if (send_eof(net) || net_flush(net))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


int Show_instance_status::write_header(st_net *net)
{
  LIST name, state, version, version_number, mysqld_compatible;
  LIST *field_list;
  LEX_STRING name_field, state_field, version_field,
                   version_number_field, mysqld_compatible_field;

  /* Create list of the fileds to be passed to send_fields(). */

  name_field.str= (char *) "instance_name";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;

  state_field.str= (char *) "state";
  state_field.length= DEFAULT_FIELD_LENGTH;
  state.data= &state_field;

  version_field.str= (char *) "version";
  version_field.length= MAX_VERSION_LENGTH;
  version.data= &version_field;

  version_number_field.str= (char *) "version_number";
  version_number_field.length= MAX_VERSION_LENGTH;
  version_number.data= &version_number_field;

  mysqld_compatible_field.str= (char *) "mysqld_compatible";
  mysqld_compatible_field.length= DEFAULT_FIELD_LENGTH;
  mysqld_compatible.data= &mysqld_compatible_field;

  field_list= list_add(NULL, &mysqld_compatible);
  field_list= list_add(field_list, &version);
  field_list= list_add(field_list, &version_number);
  field_list= list_add(field_list, &state);
  field_list= list_add(field_list, &name);

  return send_fields(net, field_list) ? ER_OUT_OF_RESOURCES : 0;
}


int Show_instance_status::write_data(st_net *net, Instance *instance)
{
  Buffer send_buf;  /* buffer for packets */
  char version_num_buf[MAX_VERSION_LENGTH];
  uint pos= 0;

  const char *state_name;
  const char *version_tag= "unknown";
  const char *version_num= "unknown";
  const char *mysqld_compatible_status;

  instance_map->guardian->lock();
  state_name= instance_map->get_instance_state_name(instance);
  mysqld_compatible_status= instance->is_mysqld_compatible() ? "yes" : "no";
  instance_map->guardian->unlock();

  if (instance->options.mysqld_version)
  {

    if (parse_version_number(instance->options.mysqld_version, version_num_buf,
                             sizeof(version_num_buf)))
      return ER_OUT_OF_RESOURCES;

    version_num= version_num_buf;
    version_tag= instance->options.mysqld_version;
  }

  if (store_to_protocol_packet(&send_buf, get_instance_name()->str, &pos) ||
      store_to_protocol_packet(&send_buf, state_name, &pos) ||
      store_to_protocol_packet(&send_buf, version_num, &pos) ||
      store_to_protocol_packet(&send_buf, version_tag, &pos) ||
      store_to_protocol_packet(&send_buf, mysqld_compatible_status, &pos) ||
      my_net_write(net, send_buf.buffer, (uint) pos))
  {
    return ER_OUT_OF_RESOURCES;
  }

  return 0;
}


/**************************************************************************
 Implementation of Show_instance_options.
**************************************************************************/

Show_instance_options::Show_instance_options(
  Instance_map *instance_map_arg, const LEX_STRING *instance_name_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg)
{
}


/*
  Implementation of SHOW INSTANCE OPTIONS statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Show_instance_options::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  if ((err_code= write_header(net)) ||
      (err_code= write_data(net, instance)))
    return err_code;

  return 0;
}


int Show_instance_options::send_ok_response(st_net *net, ulong connection_id)
{
  if (send_eof(net) || net_flush(net))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


int Show_instance_options::write_header(st_net *net)
{
  LIST name, option;
  LIST *field_list;
  LEX_STRING name_field, option_field;

  /* Create list of the fileds to be passed to send_fields(). */

  name_field.str= (char *) "option_name";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;

  option_field.str= (char *) "value";
  option_field.length= DEFAULT_FIELD_LENGTH;
  option.data= &option_field;

  field_list= list_add(NULL, &option);
  field_list= list_add(field_list, &name);

  return send_fields(net, field_list) ? ER_OUT_OF_RESOURCES : 0;
}


int Show_instance_options::write_data(st_net *net, Instance *instance)
{
  Buffer send_buff;  /* buffer for packets */
  uint pos= 0;

  if (store_to_protocol_packet(&send_buff, "instance_name", &pos) ||
      store_to_protocol_packet(&send_buff, get_instance_name()->str, &pos) ||
      my_net_write(net, send_buff.buffer, pos))
  {
    return ER_OUT_OF_RESOURCES;
  }

  /* Loop through the options. */

  for (int i= 0; i < instance->options.get_num_options(); i++)
  {
    Named_value option= instance->options.get_option(i);
    const char *option_value= option.get_value()[0] ? option.get_value() : "";

    pos= 0;

    if (store_to_protocol_packet(&send_buff, option.get_name(), &pos) ||
        store_to_protocol_packet(&send_buff, option_value, &pos) ||
        my_net_write(net, send_buff.buffer, pos))
    {
      return ER_OUT_OF_RESOURCES;
    }
  }

  return 0;
}


/**************************************************************************
 Implementation of Start_instance.
**************************************************************************/

Start_instance::Start_instance(Instance_map *instance_map_arg,
                               const LEX_STRING *instance_name_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg)
{
}


/*
  Implementation of START INSTANCE statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Start_instance::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  if ((err_code= instance->start()))
    return err_code;

  if (!(instance->options.nonguarded))
    instance_map->guardian->guard(instance);

  return 0;
}


int Start_instance::send_ok_response(st_net *net, ulong connection_id)
{
  if (net_send_ok(net, connection_id, "Instance started"))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


/**************************************************************************
 Implementation of Stop_instance.
**************************************************************************/

Stop_instance::Stop_instance(Instance_map *instance_map_arg,
                             const LEX_STRING *instance_name_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg)
{
}


/*
  Implementation of STOP INSTANCE statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Stop_instance::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  if (!(instance->options.nonguarded))
    instance_map->guardian->stop_guard(instance);

  if ((err_code= instance->stop()))
    return err_code;

  return 0;
}


int Stop_instance::send_ok_response(st_net *net, ulong connection_id)
{
  if (net_send_ok(net, connection_id, NULL))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


/**************************************************************************
 Implementation for Create_instance.
**************************************************************************/

Create_instance::Create_instance(Instance_map *instance_map_arg,
                                 const LEX_STRING *instance_name_arg)
  :Command(instance_map_arg),
  instance_name(instance_name_arg)
{
}


/*
  This operation initializes Create_instance object.

  SYNOPSYS
    text            [IN/OUT] a pointer to the text containing instance options.

  RETURN
    FALSE           On success.
    TRUE            On error.
*/

bool Create_instance::init(const char **text)
{
  return options.init() || parse_args(text);
}


/*
  This operation parses CREATE INSTANCE options.

  SYNOPSYS
    text            [IN/OUT] a pointer to the text containing instance options.

  RETURN
    FALSE           On success.
    TRUE            On syntax error.
*/

bool Create_instance::parse_args(const char **text)
{
  uint len;

  /* Check if we have something (and trim leading spaces). */

  get_word(text, &len, NONSPACE);

  if (len == 0)
    return FALSE; /* OK: no option. */

  /* Main parsing loop. */

  while (TRUE)
  {
    LEX_STRING option_name;
    char *option_name_str;
    char *option_value_str= NULL;

    /* Looking for option name. */

    get_word(text, &option_name.length, OPTION_NAME);

    if (option_name.length == 0)
      return TRUE; /* Syntax error: option name expected. */

    option_name.str= (char *) *text;
    *text+= option_name.length;

    /* Looking for equal sign. */

    skip_spaces(text);

    if (**text == '=')
    {
      ++(*text); /* Skip an equal sign. */

      /* Looking for option value. */

      skip_spaces(text);

      if (!**text)
        return TRUE; /* Syntax error: EOS when option value expected. */

      if (**text != '\'' && **text != '"')
      {
        /* Option value is a simple token. */

        LEX_STRING option_value;

        get_word(text, &option_value.length, ALPHANUM);

        if (option_value.length == 0)
          return TRUE; /* internal parser error. */

        option_value.str= (char *) *text;
        *text+= option_value.length;

        if (!(option_value_str= Named_value::alloc_str(&option_value)))
          return TRUE; /* out of memory during parsing. */
      }
      else
      {
        /* Option value is a string. */

        if (parse_option_value(*text, &len, &option_value_str))
          return TRUE; /* Syntax error: invalid string specification. */

        *text+= len;
      }
    }

    if (!option_value_str)
    {
      LEX_STRING empty_str= { C_STRING_WITH_LEN("") };

      if (!(option_value_str= Named_value::alloc_str(&empty_str)))
        return TRUE; /* out of memory during parsing. */
    }

    if (!(option_name_str= Named_value::alloc_str(&option_name)))
    {
      Named_value::free_str(&option_value_str);
      return TRUE; /* out of memory during parsing. */
    }

    {
      Named_value option(option_name_str, option_value_str);

      if (options.add_element(&option))
      {
        option.free();
        return TRUE; /* out of memory during parsing. */
      }
    }

    skip_spaces(text);

    if (!**text)
      return FALSE; /* OK: end of options. */

    if (**text != ',')
      return TRUE; /* Syntax error: comma expected. */

    ++(*text);
  }
}


/*
  Implementation of CREATE INSTANCE statement.

  Possible error codes:
    ER_MALFORMED_INSTANCE_NAME  Instance name is malformed
    ER_CREATE_EXISTING_INSTANCE There is an instance with the given name
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Create_instance::execute(st_net *net, ulong connection_id)
{
  int err_code;

  /* Check that the name is valid and there is no instance with such name. */

  if (!Instance::is_name_valid(get_instance_name()))
    return ER_MALFORMED_INSTANCE_NAME;

  /*
    NOTE: In order to prevent race condition, we should perform all operations
    on under acquired lock.
  */

  instance_map->lock();

  if (instance_map->find(get_instance_name()))
  {
    instance_map->unlock();
    return ER_CREATE_EXISTING_INSTANCE;
  }

  if ((err_code= instance_map->create_instance(get_instance_name(), &options)))
  {
    instance_map->unlock();
    return err_code;
  }

  if ((err_code= create_instance_in_file(get_instance_name(), &options)))
  {
    Instance *instance= instance_map->find(get_instance_name());

    if (instance)
      instance_map->remove_instance(instance); /* instance is deleted here. */

    instance_map->unlock();
    return err_code;
  }

  /* That's all. */

  instance_map->unlock();

  /* Send the result. */

  if (net_send_ok(net, connection_id, NULL))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


/**************************************************************************
 Implementation for Drop_instance.
**************************************************************************/

Drop_instance::Drop_instance(Instance_map *instance_map_arg,
                             const LEX_STRING *instance_name_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg)
{
}


/*
  Implementation of DROP INSTANCE statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_DROP_ACTIVE_INSTANCE     The specified instance is active
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Drop_instance::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  /* Check that the instance is offline. */

  if (instance_map->guardian->is_active(instance))
    return ER_DROP_ACTIVE_INSTANCE;

  err_code= modify_defaults_file(Options::Main::config_file, NULL, NULL,
                                 get_instance_name()->str, MY_REMOVE_SECTION);
  DBUG_ASSERT(err_code >= 0 && err_code <= 2);

  if (err_code)
  {
    log_error("Can not remove instance '%s' from defaults file (%s). "
              "Original error code: %d.",
              (const char *) get_instance_name()->str,
              (const char *) Options::Main::config_file,
              (int) err_code);
  }

  if (err_code)
    return modify_defaults_to_im_error[err_code];

  /* Remove instance from the instance map hash and Guardian's list. */

  if (!instance->options.nonguarded)
    instance_map->guardian->stop_guard(instance);

  if ((err_code= instance->stop()))
    return err_code;

  instance_map->remove_instance(instance);

  return 0;
}


int Drop_instance::send_ok_response(st_net *net, ulong connection_id)
{
  if (net_send_ok(net, connection_id, "Instance dropped"))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


/**************************************************************************
 Implementation for Show_instance_log.
**************************************************************************/

Show_instance_log::Show_instance_log(Instance_map *instance_map_arg,
                                     const LEX_STRING *instance_name_arg,
                                     Log_type log_type_arg,
                                     uint size_arg, uint offset_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg),
  log_type(log_type_arg),
  size(size_arg),
  offset(offset_arg)
{
}


/*
  Implementation of SHOW INSTANCE LOG statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_OFFSET_ERROR             We were requested to read negative number of
                                bytes from the log
    ER_NO_SUCH_LOG              The specified type of log is not available for
                                the given instance
    ER_GUESS_LOGFILE            IM wasn't able to figure out the log
                                placement, while it is enabled. Probably user
                                should specify the path to the logfile
                                explicitly.
    ER_OPEN_LOGFILE             Cannot open the logfile
    ER_READ_FILE                Cannot read the logfile
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Show_instance_log::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  if ((err_code= check_params(instance)))
    return err_code;

  if ((err_code= write_header(net)) ||
      (err_code= write_data(net, instance)))
    return err_code;

  return 0;
}


int Show_instance_log::send_ok_response(st_net *net, ulong connection_id)
{
  if (send_eof(net) || net_flush(net))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


int Show_instance_log::check_params(Instance *instance)
{
  const char *logpath= instance->options.logs[log_type];

  /* Cannot read negative number of bytes. */

  if (offset > size)
    return ER_OFFSET_ERROR;

  /* Instance has no such log. */

  if (logpath == NULL)
    return ER_NO_SUCH_LOG;

  if (*logpath == '\0')
    return ER_GUESS_LOGFILE;

  return 0;
}


int Show_instance_log::write_header(st_net *net)
{
  LIST name;
  LIST *field_list;
  LEX_STRING name_field;

  /* Create list of the fields to be passed to send_fields(). */

  name_field.str= (char *) "Log";
  name_field.length= DEFAULT_FIELD_LENGTH;

  name.data= &name_field;

  field_list= list_add(NULL, &name);

  return send_fields(net, field_list) ? ER_OUT_OF_RESOURCES : 0;
}


int Show_instance_log::write_data(st_net *net, Instance *instance)
{
  Buffer send_buff;  /* buffer for packets */
  uint pos= 0;

  const char *logpath= instance->options.logs[log_type];
  File fd;

  size_t buff_size;
  int read_len;

  MY_STAT file_stat;
  Buffer read_buff;

  if ((fd= my_open(logpath, O_RDONLY | O_BINARY,  MYF(MY_WME))) <= 0)
    return ER_OPEN_LOGFILE;

  /* my_fstat doesn't use the flag parameter */
  if (my_fstat(fd, &file_stat, MYF(0)))
  {
    close(fd);
    return ER_OUT_OF_RESOURCES;
  }

  /* calculate buffer size */
  buff_size= (size - offset);

  read_buff.reserve(0, buff_size);

  /* read in one chunk */
  read_len= (int)my_seek(fd, file_stat.st_size - size, MY_SEEK_SET, MYF(0));

  if ((read_len= my_read(fd, (byte*) read_buff.buffer,
                         buff_size, MYF(0))) < 0)
  {
    close(fd);
    return ER_READ_FILE;
  }

  close(fd);

  if (store_to_protocol_packet(&send_buff, read_buff.buffer, &pos, read_len) ||
      my_net_write(net, send_buff.buffer, pos))
  {
    return ER_OUT_OF_RESOURCES;
  }

  return 0;
}


/**************************************************************************
 Implementation of Show_instance_log_files.
**************************************************************************/

Show_instance_log_files::Show_instance_log_files
              (Instance_map *instance_map_arg,
               const LEX_STRING *instance_name_arg)
  :Abstract_instance_cmd(instance_map_arg, instance_name_arg)
{
}


/*
  Implementation of SHOW INSTANCE LOG FILES statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Show_instance_log_files::execute_impl(st_net *net, Instance *instance)
{
  int err_code;

  if ((err_code= write_header(net)) ||
      (err_code= write_data(net, instance)))
    return err_code;

  return 0;
}


int Show_instance_log_files::send_ok_response(st_net *net, ulong connection_id)
{
  if (send_eof(net) || net_flush(net))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


int Show_instance_log_files::write_header(st_net *net)
{
  LIST name, path, size;
  LIST *field_list;
  LEX_STRING name_field, path_field, size_field;

  /* Create list of the fileds to be passed to send_fields(). */

  name_field.str= (char *) "Logfile";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;

  path_field.str= (char *) "Path";
  path_field.length= DEFAULT_FIELD_LENGTH;
  path.data= &path_field;

  size_field.str= (char *) "File size";
  size_field.length= DEFAULT_FIELD_LENGTH;
  size.data= &size_field;

  field_list= list_add(NULL, &size);
  field_list= list_add(field_list, &path);
  field_list= list_add(field_list, &name);

  return send_fields(net, field_list) ? ER_OUT_OF_RESOURCES : 0;
}


int Show_instance_log_files::write_data(st_net *net, Instance *instance)
{
  Buffer send_buff;  /* buffer for packets */

  /*
    We have alike structure in instance_options.cc. We use such to be able
    to loop through the options, which we need to handle in some common way.
  */
  struct log_files_st
  {
    const char *name;
    const char *value;
  } logs[]=
  {
    {"ERROR LOG", instance->options.logs[IM_LOG_ERROR]},
    {"GENERAL LOG", instance->options.logs[IM_LOG_GENERAL]},
    {"SLOW LOG", instance->options.logs[IM_LOG_SLOW]},
    {NULL, NULL}
  };
  struct log_files_st *log_files;

  for (log_files= logs; log_files->name; log_files++)
  {
    if (!log_files->value)
      continue;

    struct stat file_stat;
    /*
      Save some more space for the log file names. In fact all
      we need is strlen("GENERAL_LOG") + 1
    */
    enum { LOG_NAME_BUFFER_SIZE= 20 };
    char buff[LOG_NAME_BUFFER_SIZE];

    uint pos= 0;

    const char *log_path= "";
    const char *log_size= "0";

    if (!stat(log_files->value, &file_stat) &&
        MY_S_ISREG(file_stat.st_mode))
    {
      int10_to_str(file_stat.st_size, buff, 10);

      log_path= log_files->value;
      log_size= buff;
    }

    if (store_to_protocol_packet(&send_buff, log_files->name, &pos) ||
        store_to_protocol_packet(&send_buff, log_path, &pos) ||
        store_to_protocol_packet(&send_buff, log_size, &pos) ||
        my_net_write(net, send_buff.buffer, pos))
      return ER_OUT_OF_RESOURCES;
  }

  return 0;
}


/**************************************************************************
 Implementation of Abstract_option_cmd.
**************************************************************************/

/*
  Instance_options_list -- a data class representing a list of options for
  some instance.
*/

class Instance_options_list
{
public:
  Instance_options_list(const LEX_STRING *instance_name_arg);

public:
  bool init();

  const LEX_STRING *get_instance_name() const
  {
    return instance_name.get_str();
  }

public:
  /*
    This member is set and used only in Abstract_option_cmd::execute_impl().
    Normally it is not used (and should not).

    The problem is that construction and execution of commands are made not
    in one transaction (not under one lock session). So, we can not initialize
    instance in constructor and use it in execution.
  */
  Instance *instance;

  Named_value_arr options;

private:
  Instance_name instance_name;
};


/**************************************************************************/

Instance_options_list::Instance_options_list(
  const LEX_STRING *instance_name_arg)
  :instance(NULL),
  instance_name(instance_name_arg)
{
}


bool Instance_options_list::init()
{
  return options.init();
}


/**************************************************************************/

C_MODE_START

static byte* get_item_key(const byte* item, uint* len,
                          my_bool __attribute__((unused)) t)
{
  const Instance_options_list *lst= (const Instance_options_list *) item;
  *len= lst->get_instance_name()->length;
  return (byte *) lst->get_instance_name()->str;
}

static void delete_item(void *item)
{
  delete (Instance_options_list *) item;
}

C_MODE_END


/**************************************************************************/

Abstract_option_cmd::Abstract_option_cmd(Instance_map *instance_map_arg)
  :Command(instance_map_arg),
  initialized(FALSE)
{
}


Abstract_option_cmd::~Abstract_option_cmd()
{
  if (initialized)
    hash_free(&instance_options_map);
}


bool Abstract_option_cmd::add_option(const LEX_STRING *instance_name,
                                     Named_value *option)
{
  Instance_options_list *lst= get_instance_options_list(instance_name);

  if (!lst)
    return TRUE;

  lst->options.add_element(option);

  return FALSE;
}


bool Abstract_option_cmd::init(const char **text)
{
  static const int INITIAL_HASH_SIZE= 16;

  if (hash_init(&instance_options_map, default_charset_info,
                INITIAL_HASH_SIZE, 0, 0, get_item_key, delete_item, 0))
    return TRUE;

  if (parse_args(text))
    return TRUE;

  initialized= TRUE;

  return FALSE;
}


/*
  Correct the option file. The "skip" option is used to remove the found
  option.

  SYNOPSYS
  Abstract_option_cmd::correct_file()
    skip     Skip the option, being searched while writing the result file.
             That is, to delete it.

  RETURN
    0                       Success
    ER_OUT_OF_RESOURCES     Not enough resources to complete the operation
    ER_ACCESS_OPTION_FILE   Cannot access the option file
*/

int Abstract_option_cmd::correct_file(Instance *instance, Named_value *option,
                                      bool skip)
{
  int err_code= modify_defaults_file(Options::Main::config_file,
                                     option->get_name(),
                                     option->get_value(),
                                     instance->get_name()->str,
                                     skip);

  DBUG_ASSERT(err_code >= 0 && err_code <= 2);

  if (err_code)
  {
    log_error("Can not modify option (%s) in defaults file (%s). "
              "Original error code: %d.",
              (const char *) option->get_name(),
              (const char *) Options::Main::config_file,
              (int) err_code);
  }

  return modify_defaults_to_im_error[err_code];
}


/*
  Implementation of SET statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance with the given name does not exist
    ER_INCOMPATIBLE_OPTION      The specified option can not be set for
                                mysqld-compatible instance
    ER_INSTANCE_IS_ACTIVE       The specified instance is active
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Abstract_option_cmd::execute(st_net *net, ulong connection_id)
{
  int err_code;

  instance_map->lock();

  err_code= execute_impl(net, connection_id);

  instance_map->unlock();

  return err_code;
}


Instance_options_list *
Abstract_option_cmd::get_instance_options_list(const LEX_STRING *instance_name)
{
  Instance_options_list *lst=
    (Instance_options_list *) hash_search(&instance_options_map,
                                        (byte *) instance_name->str,
                                        instance_name->length);

  if (!lst)
  {
    lst= new Instance_options_list(instance_name);

    if (!lst)
      return NULL;

    if (lst->init() || my_hash_insert(&instance_options_map, (byte *) lst))
    {
      delete lst;
      return NULL;
    }
  }

  return lst;
}


int Abstract_option_cmd::execute_impl(st_net *net, ulong connection_id)
{
  int err_code;

  /* Check that all the specified instances exist and are offline. */

  for (uint i= 0; i < instance_options_map.records; ++i)
  {
    Instance_options_list *lst=
      (Instance_options_list *) hash_element(&instance_options_map, i);

    lst->instance= instance_map->find(lst->get_instance_name());

    if (!lst->instance)
      return ER_BAD_INSTANCE_NAME;

    if (instance_map->guardian->is_active(lst->instance))
      return ER_INSTANCE_IS_ACTIVE;
  }

  /* Perform command-specific (SET/UNSET) actions. */

  for (uint i= 0; i < instance_options_map.records; ++i)
  {
    Instance_options_list *lst=
      (Instance_options_list *) hash_element(&instance_options_map, i);

    for (int j= 0; j < lst->options.get_size(); ++j)
    {
      Named_value option= lst->options.get_element(j);
      err_code= process_option(lst->instance, &option);

      if (err_code)
        break;
    }

    if (err_code)
      break;
  }

  if (err_code == 0)
    net_send_ok(net, connection_id, NULL);

  return err_code;
}


/**************************************************************************
 Implementation of Set_option.
**************************************************************************/

Set_option::Set_option(Instance_map *instance_map_arg)
  :Abstract_option_cmd(instance_map_arg)
{
}


/*
  This operation parses SET options.

  SYNOPSYS
    text            [IN/OUT] a pointer to the text containing options.

  RETURN
    FALSE           On success.
    TRUE            On syntax error.
*/

bool Set_option::parse_args(const char **text)
{
  uint len;

  /* Check if we have something (and trim leading spaces). */

  get_word(text, &len, NONSPACE);

  if (len == 0)
    return TRUE; /* Syntax error: no option. */

  /* Main parsing loop. */

  while (TRUE)
  {
    LEX_STRING instance_name;
    LEX_STRING option_name;
    char *option_name_str;
    char *option_value_str= NULL;

    /* Looking for instance name. */

    get_word(text, &instance_name.length, ALPHANUM);

    if (instance_name.length == 0)
      return TRUE; /* Syntax error: instance name expected. */

    instance_name.str= (char *) *text;
    *text+= instance_name.length;

    skip_spaces(text);

    /* Check the the delimiter is a dot. */

    if (**text != '.')
      return TRUE; /* Syntax error: dot expected. */

    ++(*text);

    /* Looking for option name. */

    get_word(text, &option_name.length, OPTION_NAME);

    if (option_name.length == 0)
      return TRUE; /* Syntax error: option name expected. */

    option_name.str= (char *) *text;
    *text+= option_name.length;

    /* Looking for equal sign. */

    skip_spaces(text);

    if (**text == '=')
    {
      ++(*text); /* Skip an equal sign. */

      /* Looking for option value. */

      skip_spaces(text);

      if (!**text)
        return TRUE; /* Syntax error: EOS when option value expected. */

      if (**text != '\'' && **text != '"')
      {
        /* Option value is a simple token. */

        LEX_STRING option_value;

        get_word(text, &option_value.length, ALPHANUM);

        if (option_value.length == 0)
          return TRUE; /* internal parser error. */

        option_value.str= (char *) *text;
        *text+= option_value.length;

        if (!(option_value_str= Named_value::alloc_str(&option_value)))
          return TRUE; /* out of memory during parsing. */
      }
      else
      {
        /* Option value is a string. */

        if (parse_option_value(*text, &len, &option_value_str))
          return TRUE; /* Syntax error: invalid string specification. */

        *text+= len;
      }
    }

    if (!option_value_str)
    {
      LEX_STRING empty_str= { C_STRING_WITH_LEN("") };

      if (!(option_value_str= Named_value::alloc_str(&empty_str)))
        return TRUE; /* out of memory during parsing. */
    }

    if (!(option_name_str= Named_value::alloc_str(&option_name)))
    {
      Named_value::free_str(&option_name_str);
      return TRUE; /* out of memory during parsing. */
    }

    {
      Named_value option(option_name_str, option_value_str);

      if (add_option(&instance_name, &option))
      {
        option.free();
        return TRUE; /* out of memory during parsing. */
      }
    }

    skip_spaces(text);

    if (!**text)
      return FALSE; /* OK: end of options. */

    if (**text != ',')
      return TRUE; /* Syntax error: comma expected. */

    ++(*text); /* Skip a comma. */
  }
}


int Set_option::process_option(Instance *instance, Named_value *option)
{
  /* Check that the option is valid. */

  if (instance->is_mysqld_compatible() &&
      Instance_options::is_option_im_specific(option->get_name()))
  {
      log_error("Error: IM-option (%s) can not be used "
                "in the configuration of mysqld-compatible instance (%s).",
                (const char *) option->get_name(),
                (const char *) instance->get_name()->str);
      return ER_INCOMPATIBLE_OPTION;
  }

  /* Update the configuration file. */

  int err_code= correct_file(instance, option, FALSE);

  if (err_code)
    return err_code;

  /* Update the internal cache. */

  if (instance->options.set_option(option))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


/**************************************************************************
 Implementation of Unset_option.
**************************************************************************/

Unset_option::Unset_option(Instance_map *instance_map_arg)
  :Abstract_option_cmd(instance_map_arg)
{
}


/*
  This operation parses UNSET options.

  SYNOPSYS
    text            [IN/OUT] a pointer to the text containing options.

  RETURN
    FALSE           On success.
    TRUE            On syntax error.
*/

bool Unset_option::parse_args(const char **text)
{
  uint len;

  /* Check if we have something (and trim leading spaces). */

  get_word(text, &len, NONSPACE);

  if (len == 0)
    return TRUE; /* Syntax error: no option. */

  /* Main parsing loop. */

  while (TRUE)
  {
    LEX_STRING instance_name;
    LEX_STRING option_name;
    char *option_name_str;
    char *option_value_str;

    /* Looking for instance name. */

    get_word(text, &instance_name.length, ALPHANUM);

    if (instance_name.length == 0)
      return TRUE; /* Syntax error: instance name expected. */

    instance_name.str= (char *) *text;
    *text+= instance_name.length;

    skip_spaces(text);

    /* Check the the delimiter is a dot. */

    if (**text != '.')
      return TRUE; /* Syntax error: dot expected. */

    ++(*text); /* Skip a dot. */

    /* Looking for option name. */

    get_word(text, &option_name.length, OPTION_NAME);

    if (option_name.length == 0)
      return TRUE; /* Syntax error: option name expected. */

    option_name.str= (char *) *text;
    *text+= option_name.length;

    if (!(option_name_str= Named_value::alloc_str(&option_name)))
      return TRUE; /* out of memory during parsing. */

    {
      LEX_STRING empty_str= { C_STRING_WITH_LEN("") };

      if (!(option_value_str= Named_value::alloc_str(&empty_str)))
      {
        Named_value::free_str(&option_name_str);
        return TRUE;
      }
    }

    {
      Named_value option(option_name_str, option_value_str);

      if (add_option(&instance_name, &option))
      {
        option.free();
        return TRUE; /* out of memory during parsing. */
      }
    }

    skip_spaces(text);

    if (!**text)
      return FALSE; /* OK: end of options. */

    if (**text != ',')
      return TRUE; /* Syntax error: comma expected. */

    ++(*text); /* Skip a comma. */
  }
}


/*
  Implementation of UNSET statement.

  Possible error codes:
    ER_BAD_INSTANCE_NAME        The instance name specified is not valid
    ER_INSTANCE_IS_ACTIVE       The specified instance is active
    ER_OUT_OF_RESOURCES         Not enough resources to complete the operation
*/

int Unset_option::process_option(Instance *instance, Named_value *option)
{
  /* Update the configuration file. */

  int err_code= correct_file(instance, option, TRUE);

  if (err_code)
    return err_code;

  /* Update the internal cache. */

  instance->options.unset_option(option->get_name());

  return 0;
}


/**************************************************************************
 Implementation of Syntax_error.
**************************************************************************/

int Syntax_error::execute(st_net *net, ulong connection_id)
{
  return ER_SYNTAX_ERROR;
}