summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/commands.cc
blob: bb3763ce8c5dd95028abe32389060a54d1789c69 (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
/* 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; version 2 of the License.

   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 */

#include "commands.h"

#include "instance_map.h"
#include "messages.h"
#include "mysqld_error.h"
#include "mysql_manager_error.h"
#include "protocol.h"
#include "buffer.h"
#include "options.h"

#include <m_string.h>
#include <mysql.h>
#include <my_dir.h>


/*
  Add a string to a buffer

  SYNOPSYS
    put_to_buff()
    buff              buffer to add the string
    str               string to add
    uint              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)
{
  size_t len= strlen(str);
  if (buff->append(*position, str, (uint) len))
    return 1;

  *position+= (uint) len;
  return 0;
}


/* implementation for Show_instances: */


/*
  The method sends a list of instances in the instance map to the client.

  SYNOPSYS
    Show_instances::execute()
    net                    The network connection to the client.
    connection_id          Client connection ID

  RETURN
    0 - ok
    1 - error occured
*/

int Show_instances::execute(struct st_net *net, ulong connection_id)
{
  Buffer send_buff;  /* buffer for packets */
  LIST name, status;
  NAME_WITH_LENGTH name_field, status_field;
  LIST *field_list;
  uint position=0;

  name_field.name= (char*) "instance_name";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;
  status_field.name= (char*) "status";
  status_field.length= DEFAULT_FIELD_LENGTH;
  status.data= &status_field;
  field_list= list_add(NULL, &status);
  field_list= list_add(field_list, &name);

  send_fields(net, field_list);

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

    instance_map->lock();
    while ((instance= iterator.next()))
    {
      position= 0;
      store_to_protocol_packet(&send_buff, instance->options.instance_name,
                               &position);
      if (instance->is_running())
        store_to_protocol_packet(&send_buff, (char*) "online", &position);
      else
        store_to_protocol_packet(&send_buff, (char*) "offline", &position);
      if (my_net_write(net, send_buff.buffer, (uint) position))
        goto err;
    }
    instance_map->unlock();
  }
  if (send_eof(net))
    goto err;
  if (net_flush(net))
    goto err;

  return 0;
err:
  return ER_OUT_OF_RESOURCES;
}


/* implementation for Flush_instances: */

int Flush_instances::execute(struct st_net *net, ulong connection_id)
{
  if (instance_map->flush_instances() ||
      net_send_ok(net, connection_id, NULL))
    return ER_OUT_OF_RESOURCES;

  return 0;
}


/* implementation for Show_instance_status: */

Show_instance_status::Show_instance_status(Instance_map *instance_map_arg,
                                           const char *name, uint len)
  :Command(instance_map_arg)
{
  Instance *instance;

  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
    instance_name= instance->options.instance_name;
  else
    instance_name= NULL;
}


/*
  The method sends a table with a status of requested instance to the client.

  SYNOPSYS
    Show_instance_status::do_command()
    net               The network connection to the client.
    instance_name     The name of the instance.

  RETURN
    0 - ok
    1 - error occured
*/


int Show_instance_status::execute(struct st_net *net,
                                  ulong connection_id)
{
  enum { MAX_VERSION_LENGTH= 40 };
  Buffer send_buff;  /* buffer for packets */
  LIST name, status, version;
  LIST *field_list;
  NAME_WITH_LENGTH name_field, status_field, version_field;
  uint position=0;

  if (!instance_name)
    return ER_BAD_INSTANCE_NAME;

  /* create list of the fileds to be passed to send_fields */
  name_field.name= (char*) "instance_name";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;
  status_field.name= (char*) "status";
  status_field.length= DEFAULT_FIELD_LENGTH;
  status.data= &status_field;
  version_field.name= (char*) "version";
  version_field.length= MAX_VERSION_LENGTH;
  version.data= &version_field;
  field_list= list_add(NULL, &version);
  field_list= list_add(field_list, &status);
  field_list= list_add(field_list, &name);

  send_fields(net, field_list);

  {
    Instance *instance;

    store_to_protocol_packet(&send_buff, (char*) instance_name, &position);
    if (!(instance= instance_map->find(instance_name, (uint) strlen(instance_name))))
      goto err;
    if (instance->is_running())
      store_to_protocol_packet(&send_buff, (char*) "online", &position);
    else
      store_to_protocol_packet(&send_buff, (char*) "offline", &position);

    if (instance->options.mysqld_version)
      store_to_protocol_packet(&send_buff, instance->options.mysqld_version,
                               &position);
    else
      store_to_protocol_packet(&send_buff, (char*) "unknown", &position);


    if (send_buff.is_error() ||
        my_net_write(net, send_buff.buffer, (uint) position))
      goto err;
  }

  if (send_eof(net) || net_flush(net))
    goto err;

  return 0;

err:
  return ER_OUT_OF_RESOURCES;
}


/* Implementation for Show_instance_options */

Show_instance_options::Show_instance_options(Instance_map *instance_map_arg,
                                             const char *name, uint len):
  Command(instance_map_arg)
{
  Instance *instance;

  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
    instance_name= instance->options.instance_name;
  else
    instance_name= NULL;
}


int Show_instance_options::execute(struct st_net *net, ulong connection_id)
{
  Buffer send_buff;  /* buffer for packets */
  LIST name, option;
  LIST *field_list;
  NAME_WITH_LENGTH name_field, option_field;
  uint position=0;

  if (!instance_name)
    return ER_BAD_INSTANCE_NAME;

  /* create list of the fileds to be passed to send_fields */
  name_field.name= (char*) "option_name";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;
  option_field.name= (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);

  send_fields(net, field_list);

  {
    Instance *instance;

    if (!(instance= instance_map->find(instance_name, (uint) strlen(instance_name))))
      goto err;
    store_to_protocol_packet(&send_buff, (char*) "instance_name", &position);
    store_to_protocol_packet(&send_buff, (char*) instance_name, &position);
    if (my_net_write(net, send_buff.buffer, (uint) position))
      goto err;
    if ((instance->options.mysqld_path))
    {
      position= 0;
      store_to_protocol_packet(&send_buff, (char*) "mysqld-path", &position);
      store_to_protocol_packet(&send_buff,
                               (char*) instance->options.mysqld_path,
                               &position);
      if (send_buff.is_error() ||
          my_net_write(net, send_buff.buffer, (uint) position))
        goto err;
    }

    if ((instance->options.nonguarded))
    {
      position= 0;
      store_to_protocol_packet(&send_buff, (char*) "nonguarded", &position);
      store_to_protocol_packet(&send_buff, "", &position);
      if (send_buff.is_error() ||
          my_net_write(net, send_buff.buffer, (uint) position))
        goto err;
    }

    /* loop through the options stored in DYNAMIC_ARRAY */
    for (uint i= 0; i < instance->options.options_array.elements; i++)
    {
      char *tmp_option, *option_value;
      get_dynamic(&(instance->options.options_array), (gptr) &tmp_option, i);
      option_value= strchr(tmp_option, '=');
      /* split the option string into two parts if it has a value */

      position= 0;
      if (option_value != NULL)
      {
        *option_value= 0;
        store_to_protocol_packet(&send_buff, tmp_option + 2, &position);
        store_to_protocol_packet(&send_buff, option_value + 1, &position);
        /* join name and the value into the same option again */
        *option_value= '=';
      }
      else
      {
        store_to_protocol_packet(&send_buff, tmp_option + 2, &position);
        store_to_protocol_packet(&send_buff, "", &position);
      }

      if (send_buff.is_error() ||
          my_net_write(net, send_buff.buffer, (uint) position))
        goto err;
    }
  }

  if (send_eof(net) || net_flush(net))
    goto err;

  return 0;

err:
  return ER_OUT_OF_RESOURCES;
}


/* Implementation for Start_instance */

Start_instance::Start_instance(Instance_map *instance_map_arg,
                               const char *name, uint len)
  :Command(instance_map_arg)
{
  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
    instance_name= instance->options.instance_name;
}


int Start_instance::execute(struct st_net *net, ulong connection_id)
{
  uint err_code;
  if (instance == 0)
    return ER_BAD_INSTANCE_NAME; /* haven't found an instance */
  else
  {
    if ((err_code= instance->start()))
      return err_code;

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

    net_send_ok(net, connection_id, "Instance started");
    return 0;
  }
}


/* implementation for Show_instance_log: */

Show_instance_log::Show_instance_log(Instance_map *instance_map_arg,
                                     const char *name, uint len,
                                     Log_type log_type_arg,
                                     const char *size_arg,
                                     const char *offset_arg)
  :Command(instance_map_arg)
{
  Instance *instance;

  if (offset_arg != NULL)
    offset= atoi(offset_arg);
  else
    offset= 0;
  size= atoi(size_arg);
  log_type= log_type_arg;

  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
    instance_name= instance->options.instance_name;
  else
    instance_name= NULL;
}



/*
  Open the logfile, read requested part of the log and send the info
  to the client.

  SYNOPSYS
    Show_instance_log::execute()
    net                 The network connection to the client.
    connection_id       Client connection ID

  DESCRIPTION

    Send a table with the content of the log requested. The function also
    deals with errro handling, to be verbose.

  RETURN
   ER_OFFSET_ERROR      We were requested to read negative number of bytes
                        from the log
   ER_NO_SUCH_LOG       The kind log being read is not enabled in the 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  We weren't able to allocate some resources
*/

int Show_instance_log::execute(struct st_net *net, ulong connection_id)
{
  Buffer send_buff;  /* buffer for packets */
  LIST name;
  LIST *field_list;
  NAME_WITH_LENGTH name_field;
  uint position= 0;

  /* create list of the fileds to be passed to send_fields */
  name_field.name= (char*) "Log";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;
  field_list= list_add(NULL, &name);

  if (!instance_name)
    return ER_BAD_INSTANCE_NAME;

  /* cannot read negative number of bytes */
  if (offset > size)
    return ER_OFFSET_ERROR;

  send_fields(net, field_list);

  {
    Instance *instance;
    const char *logpath;
    File fd;

    if ((instance= instance_map->find(instance_name,
                                      (uint) strlen(instance_name))) == NULL)
      goto err;

    logpath= instance->options.logs[log_type];

    /* Instance has no such log */
    if (logpath == NULL)
      return ER_NO_SUCH_LOG;

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


    if ((fd= my_open(logpath, O_RDONLY | O_BINARY,  MYF(MY_WME))) >= 0)
    {
      size_t buff_size;
      int read_len;
      /* calculate buffer size */
      MY_STAT file_stat;
      Buffer read_buff;

      /* my_fstat doesn't use the flag parameter */
      if (my_fstat(fd, &file_stat, MYF(0)))
        goto err;

      buff_size= (size - offset);

      read_buff.reserve(0, (uint) 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,
                             (uint) buff_size, MYF(0))) < 0)
        return ER_READ_FILE;
      store_to_protocol_packet(&send_buff, read_buff.buffer,
                               &position, read_len);
      close(fd);
    }
    else
      return ER_OPEN_LOGFILE;

    if (my_net_write(net, send_buff.buffer, (uint) position))
      goto err;
  }

  if (send_eof(net) ||  net_flush(net))
    goto err;

  return 0;

err:
  return ER_OUT_OF_RESOURCES;
}


/* implementation for Show_instance_log_files: */

Show_instance_log_files::Show_instance_log_files
              (Instance_map *instance_map_arg, const char *name, uint len)
  :Command(instance_map_arg)
{
  Instance *instance;

  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
    instance_name= instance->options.instance_name;
  else
    instance_name= NULL;
}


/*
  The method sends a table with a list of log files
  used by the instance.

  SYNOPSYS
    Show_instance_log_files::execute()
    net               The network connection to the client.
    connection_id     The ID of the client connection

  RETURN
    ER_BAD_INSTANCE_NAME  The instance name specified is not valid
    ER_OUT_OF_RESOURCES   some error occured
    0 - ok
*/

int Show_instance_log_files::execute(struct st_net *net, ulong connection_id)
{
  Buffer send_buff;  /* buffer for packets */
  LIST name, path, size;
  LIST *field_list;
  NAME_WITH_LENGTH name_field, path_field, size_field;
  uint position= 0;

  if (!instance_name)
    return ER_BAD_INSTANCE_NAME;

  /* create list of the fileds to be passed to send_fields */
  name_field.name= (char*) "Logfile";
  name_field.length= DEFAULT_FIELD_LENGTH;
  name.data= &name_field;
  path_field.name= (char*) "Path";
  path_field.length= DEFAULT_FIELD_LENGTH;
  path.data= &path_field;
  size_field.name= (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);

  send_fields(net, field_list);

  Instance *instance;

  if ((instance= instance_map->
                 find(instance_name, (uint) strlen(instance_name))) == NULL)
    goto err;

  {
    /*
      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 != NULL)
      {
        struct stat file_stat;
        /*
          Save some more space for the log file names. In fact all
          we need is srtlen("GENERAL_LOG") + 1
        */
        enum { LOG_NAME_BUFFER_SIZE= 20 };
        char buff[LOG_NAME_BUFFER_SIZE];

        position= 0;
        /* store the type of the log in the send buffer */
        store_to_protocol_packet(&send_buff, log_files->name, &position);
        if (stat(log_files->value, &file_stat))
        {
          store_to_protocol_packet(&send_buff, "", &position);
          store_to_protocol_packet(&send_buff, (char*) "0", &position);
        }
        else if (MY_S_ISREG(file_stat.st_mode))
        {
          store_to_protocol_packet(&send_buff,
                                   (char*) log_files->value,
                                   &position);
          int10_to_str(file_stat.st_size, buff, 10);
          store_to_protocol_packet(&send_buff, (char*) buff, &position);
        }

        if (my_net_write(net, send_buff.buffer, (uint) position))
          goto err;
      }
    }
  }

  if (send_eof(net) || net_flush(net))
    goto err;

  return 0;

err:
  return ER_OUT_OF_RESOURCES;
}


/* implementation for SET instance_name.option=option_value: */

Set_option::Set_option(Instance_map *instance_map_arg,
                       const char *name, uint len,
                       const char *option_arg, uint option_len_arg,
                       const char *option_value_arg, uint option_value_len_arg)
  :Command(instance_map_arg)
{
  Instance *instance;

  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
  {
    instance_name= instance->options.instance_name;

     /* add prefix for add_option */
    if ((option_len_arg < MAX_OPTION_LEN - 1) ||
        (option_value_len_arg < MAX_OPTION_LEN - 1))
    {
      strmake(option, option_arg, option_len_arg);
      strmake(option_value, option_value_arg, option_value_len_arg);
    }
    else
    {
      option[0]= 0;
      option_value[0]= 0;
    }
    instance_name_len= len;
  }
  else
  {
    instance_name= NULL;
    instance_name_len= 0;
  }
}


/*
  The method sends a table with a list of log files
  used by the instance.

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

  DESCRIPTION

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

  RETURN
    ER_OUT_OF_RESOURCES     out of resources
    ER_ACCESS_OPTION_FILE   Cannot access the option file
    0 - ok
*/

int Set_option::correct_file(int skip)
{
  static const int mysys_to_im_error[]= { 0, ER_OUT_OF_RESOURCES,
                                             ER_ACCESS_OPTION_FILE };
  int error;

  error= modify_defaults_file(Options::config_file, option,
                              option_value, instance_name, skip);
  DBUG_ASSERT(error >= 0 && error <= 2);

  return mysys_to_im_error[error];
}


/*
  The method sets an option in the the default config file (/etc/my.cnf).

  SYNOPSYS
    Set_option::do_command()
    net               The network connection to the client.

  RETURN
    0 - ok
    1 - error occured
*/

int Set_option::do_command(struct st_net *net)
{
  int error;

  /* we must hold the instance_map mutex while changing config file */
  instance_map->lock();
  error= correct_file(FALSE);
  instance_map->unlock();

  return error;
}


int Set_option::execute(struct st_net *net, ulong connection_id)
{
  if (instance_name != NULL)
  {
    int val;

    val= do_command(net);

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

    return val;
  }

  return ER_BAD_INSTANCE_NAME;
}


/* the only function from Unset_option we need to Implement */

int Unset_option::do_command(struct st_net *net)
{
  return correct_file(TRUE);
}


/* Implementation for Stop_instance: */

Stop_instance::Stop_instance(Instance_map *instance_map_arg,
                               const char *name, uint len)
  :Command(instance_map_arg)
{
  /* we make a search here, since we don't want to store the name */
  if ((instance= instance_map->find(name, len)))
    instance_name= instance->options.instance_name;
}


int Stop_instance::execute(struct st_net *net, ulong connection_id)
{
  uint err_code;

  if (instance == 0)
    return ER_BAD_INSTANCE_NAME; /* haven't found an instance */

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

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

  net_send_ok(net, connection_id, NULL);
  return 0;
}


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