CUBRID Engine  latest
commdb.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * commdb.c - commdb main
21  */
22 
23 #ident "$Id$"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <assert.h>
30 
31 #if defined(WINDOWS)
32 #include <winsock2.h>
33 #else /* ! WINDOWS */
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <unistd.h>
37 #endif /* ! WINDOWS */
38 
39 #if defined(SOLARIS) || defined(LINUX)
40 #include <netdb.h>
41 #endif /* SOLARIS || LINUX */
42 
43 #include "connection_defs.h"
44 #include "connection_cl.h"
45 #if defined(WINDOWS)
46 #include "wintcp.h"
47 #endif /* WINDOWS */
48 #include "error_manager.h"
49 #include "porting.h"
50 #include "heartbeat.h"
51 #include "master_util.h"
52 #include "message_catalog.h"
53 #include "utility.h"
54 #include "util_support.h"
55 #include "porting.h"
56 #include "cubrid_getopt.h"
57 
58 #define COMMDB_CMD_ALLOWED_ON_REMOTE() \
59  ((commdb_Arg_deact_stop_all == true) \
60  || (commdb_Arg_deact_confirm_stop_all == true) \
61  || (commdb_Arg_deact_confirm_no_server == true) \
62  || (commdb_Arg_deactivate_heartbeat == true) \
63  || (commdb_Arg_is_registered == true) \
64  || (commdb_Arg_ha_deregister_by_args == true) \
65  || (commdb_Arg_print_ha_node_info == true) \
66  || (commdb_Arg_print_ha_process_info == true) \
67  || (commdb_Arg_print_ha_ping_hosts_info == true) \
68  || (commdb_Arg_print_ha_admin_info == true) \
69  || (commdb_Arg_ha_start_util_process == true) \
70  )
71 
72 typedef enum
73 {
75  COMM_ALL = 99
77 
78 static int send_for_server_stats (CSS_CONN_ENTRY * conn);
79 static int send_for_all_stats (CSS_CONN_ENTRY * conn);
80 #if defined (ENABLE_UNUSED_FUNCTION)
81 static int send_for_server_downtime (CSS_CONN_ENTRY * conn);
82 #endif
83 static int return_integer_data (CSS_CONN_ENTRY * conn, unsigned short request_id);
84 static int send_for_request_count (CSS_CONN_ENTRY * conn);
85 static void process_status_query (CSS_CONN_ENTRY * conn, int server_type, char **server_info);
86 static void process_master_kill (CSS_CONN_ENTRY * conn);
87 static void process_master_stop_shutdown (CSS_CONN_ENTRY * conn);
88 static void process_master_shutdown (CSS_CONN_ENTRY * conn, int minutes);
89 static void process_slave_kill (CSS_CONN_ENTRY * conn, char *slave_name, int minutes, int pid);
90 static int process_server_info_pid (CSS_CONN_ENTRY * conn, const char *server, int server_type);
91 static void process_ha_server_mode (CSS_CONN_ENTRY * conn, char *server_name);
92 static void process_ha_node_info_query (CSS_CONN_ENTRY * conn, int verbose_yn);
93 static void process_ha_process_info_query (CSS_CONN_ENTRY * conn, int verbose_yn);
95 static int process_ha_deregister_by_pid (CSS_CONN_ENTRY * conn, char *pid_string);
96 static int process_ha_deregister_by_args (CSS_CONN_ENTRY * conn, char *args);
97 
98 static int process_reconfig_heartbeat (CSS_CONN_ENTRY * conn);
102 static int process_deact_stop_all (CSS_CONN_ENTRY * conn);
103 static int process_activate_heartbeat (CSS_CONN_ENTRY * conn);
104 static int process_ha_start_util_process (CSS_CONN_ENTRY * conn, char *args);
105 
106 static int process_batch_command (CSS_CONN_ENTRY * conn);
107 
109 static bool commdb_Arg_halt_shutdown = false;
111 static bool commdb_Arg_kill_all = false;
112 static bool commdb_Arg_print_info = false;
113 static bool commdb_Arg_print_all_info = false;
114 static bool commdb_Arg_ha_mode_server_info = false;
116 static bool commdb_Arg_print_ha_node_info = false;
119 static bool commdb_Arg_print_ha_admin_info = false;
124 static bool commdb_Arg_kill_all_ha_utils = false;
125 static bool commdb_Arg_is_registered = false;
127 static bool commdb_Arg_reconfig_heartbeat = false;
129 static bool commdb_Arg_deact_immediately = false;
130 static bool commdb_Arg_activate_heartbeat = false;
131 static bool commdb_Arg_verbose_output = false;
132 static bool commdb_Arg_deact_stop_all = false;
135 static char *commdb_Arg_host_name = NULL;
138 
139 /*
140  * send_request_no_args() - send request without argument
141  * return: request id if success, otherwise 0
142  * conn(in): connection entry pointer
143  * command(in): request command
144  */
145 static unsigned short
147 {
148  unsigned short request_id;
149 
150  if (css_send_request (conn, command, &request_id, NULL, 0) == NO_ERRORS)
151  return (request_id);
152  else
153  return (0);
154 }
155 
156 /*
157  * send_request_one_arg() - send request with one argument
158  * return: request id if success, otherwise 0
159  * conn(in): connection info
160  * command(in): request command
161  * buffer(in): buffer pointer of request argument
162  * size(in): size of argument
163  */
164 static unsigned short
165 send_request_one_arg (CSS_CONN_ENTRY * conn, int command, char *buffer, int size)
166 {
167  unsigned short request_id;
168 
169  if (css_send_request (conn, command, &request_id, buffer, size) == NO_ERRORS)
170  return (request_id);
171  else
172  return (0);
173 }
174 
175 /*
176  * send_request_two_args() - send request with two arguments
177  * return: request id if success, otherwise 0
178  * conn(in): connection info
179  * command(in): request command
180  * buffer1(in): buffer pointer of first request argument
181  * size1(in): size of first argument
182  * buffer2(in): buffer pointer of first request argument
183  * size2(in): size of first argument
184  */
185 static unsigned short
186 send_request_two_args (CSS_CONN_ENTRY * conn, int command, char *buffer1, int size1, char *buffer2, int size2)
187 {
188  unsigned short request_id;
189 
190  if (css_send_request (conn, command, &request_id, buffer1, size1) == NO_ERRORS)
191  if (css_send_data (conn, request_id, buffer2, size2) == NO_ERRORS)
192  return (request_id);
193  return (0);
194 }
195 
196 /*
197  * send_for_start_time() - send request for master start time
198  * return: request id if success, otherwise 0
199  * conn(in): connection info
200  */
201 static unsigned short
203 {
204  return (send_request_no_args (conn, GET_START_TIME));
205 }
206 
207 /*
208  * return_string() - receive string data response
209  * return: none
210  * conn(in): connection info
211  * request_id(in): request id
212  * buffer(out): response output buffer
213  * buffer_size(out): size of data received
214  */
215 static void
216 return_string (CSS_CONN_ENTRY * conn, unsigned short request_id, char **buffer, int *buffer_size)
217 {
218  css_receive_data (conn, request_id, buffer, buffer_size, -1);
219 }
220 
221 /*
222  * send_for_server_count() - send request for database server count
223  * return: request id
224  * conn(in): connection info
225  */
226 static int
228 {
229  return (send_request_no_args (conn, GET_SERVER_COUNT));
230 }
231 
232 /*
233  * send_for_all_count() - send request for all processes count
234  * return: request id
235  * conn(in): connection info
236  */
237 static int
239 {
240  return (send_request_no_args (conn, GET_ALL_COUNT));
241 }
242 
243 /*
244  * send_for_server_stats() - send request for server processes info
245  * return: request id
246  * conn(in): connection info
247  */
248 static int
250 {
251  return (send_request_no_args (conn, GET_SERVER_LIST));
252 }
253 
254 /*
255  * send_for_all_stats() - send request for all processes info
256  * return: request id
257  * conn(in): connection info
258  */
259 static int
261 {
262  return (send_request_no_args (conn, GET_ALL_LIST));
263 }
264 
265 #if defined (ENABLE_UNUSED_FUNCTION)
266 /*
267  * send_for_server_downtime() - send request for master shutdown time or
268  * release string
269  * return: request id
270  * conn(in): connection info
271  */
272 static int
273 send_for_server_downtime (CSS_CONN_ENTRY * conn)
274 {
275  return (send_request_no_args (conn, GET_SHUTDOWN_TIME));
276 }
277 #endif /* ENABLE_UNUSED_FUNCTION */
278 
279 /*
280  * return_integer_data() - receive integer data response
281  * return: response value
282  * conn(in): connection info
283  * request_id(in): request id
284  */
285 static int
286 return_integer_data (CSS_CONN_ENTRY * conn, unsigned short request_id)
287 {
288  int size;
289  int *buffer = NULL;
290 
291  if (css_receive_data (conn, request_id, (char **) &buffer, &size, -1) == NO_ERRORS)
292  {
293  if (size == sizeof (int))
294  {
295  size = ntohl (*buffer);
296  free_and_init (buffer);
297  return (size);
298  }
299  }
300  if (buffer != NULL)
301  {
302  free_and_init (buffer);
303  }
304  return (0);
305 }
306 
307 /*
308  * send_for_request_count() - send request for serviced master request count
309  * return: request id
310  * conn(in): connection info
311  */
312 static int
314 {
315  return (send_request_no_args (conn, GET_REQUEST_COUNT));
316 }
317 
318 /*
319  * process_status_query() - print or get process status
320  * return: none
321  * conn(in): connection info
322  * server_type(in): COMM_SERVER_TYPE
323  * server_info(out): process info output pointer. If server_info is NULL,
324  * process status is displayed to stdout
325  */
326 static void
327 process_status_query (CSS_CONN_ENTRY * conn, int server_type, char **server_info)
328 {
329  int buffer_size;
330  int server_count, requests_serviced;
331  char *buffer1 = NULL, *buffer2 = NULL;
332  unsigned short rid1, rid2, rid3, rid4;
333 
334  if (server_info != NULL)
335  *server_info = NULL;
336 
337  rid2 = send_for_request_count (conn);
338  switch (server_type)
339  {
340  case COMM_SERVER:
341  rid3 = send_for_server_count (conn);
342  rid1 = send_for_start_time (conn);
343  rid4 = send_for_server_stats (conn);
344  break;
345  case COMM_ALL:
346  rid3 = send_for_all_count (conn);
347  rid1 = send_for_start_time (conn);
348  rid4 = send_for_all_stats (conn);
349  break;
350  default:
351  rid1 = rid3 = rid4 = 0;
352  break;
353  }
354 
355  /* check for errors on the read */
356  if (!rid1 || !rid2 || !rid3 || !rid4)
357  return;
358 
359  return_string (conn, rid1, &buffer1, &buffer_size);
360  requests_serviced = return_integer_data (conn, rid2);
361  server_count = return_integer_data (conn, rid3);
362 
363  if (server_count)
364  {
365  return_string (conn, rid4, &buffer2, &buffer_size);
366  if (server_info == NULL)
367  {
369  }
370  else
371  {
372  *server_info = buffer2;
373  buffer2 = NULL;
374  }
375  }
376 
377  free_and_init (buffer1);
378  free_and_init (buffer2);
379 }
380 
381 /*
382  * process_master_kill() - send request to kill master
383  * return: none
384  * conn(in): connection info
385  */
386 static void
388 {
389  while (send_request_no_args (conn, KILL_MASTER_SERVER) != 0)
390  {
391  ; /* wait to master kill */
392  }
393 }
394 
395 /*
396  * process_master_stop_shutdown() - send request to cancel shutdown
397  * return: none
398  * conn(in): connection info
399  */
400 static void
402 {
404 }
405 
406 /*
407  * process_master_shutdown() - send request to shut down master
408  * return: none
409  * conn(in): connection info
410  * minutes(in): shutdown timeout in minutes
411  */
412 static void
414 {
415  int down;
416 
417  down = htonl (minutes);
418  while (send_request_one_arg (conn, START_SHUTDOWN, (char *) &down, sizeof (int)) != 0)
419  {
420  ; /* wait to master shutdown */
421  }
422 }
423 
424 /*
425  * process_slave_kill() - process request to kill server process
426  * return: none
427  * conn(in): connection info
428  * slave_name(in): target process name
429  * minutes(in): shutdown timeout in minutes
430  * pid(in): process id
431  */
432 static void
433 process_slave_kill (CSS_CONN_ENTRY * conn, char *slave_name, int minutes, int pid)
434 {
435  int net_minutes;
436  char *reply_buffer = NULL;
437  int size = 0;
438  unsigned short rid;
439 
440  net_minutes = htonl (minutes);
441  rid =
442  send_request_two_args (conn, KILL_SLAVE_SERVER, slave_name, (int) strlen (slave_name) + 1, (char *) &net_minutes,
443  sizeof (int));
444  return_string (conn, rid, &reply_buffer, &size);
445  if (size)
446  {
447  printf ("\n%s\n", reply_buffer);
448 
449  if (pid > 0)
450  {
452  }
453  }
454  if (reply_buffer != NULL)
455  {
456  free_and_init (reply_buffer);
457  }
458 }
459 
460 /*
461  * process_ha_server_mode() - process ha server mode
462  * return: none
463  * conn(in): connection info
464  * server_name(in): target process name
465  */
466 static void
467 process_ha_server_mode (CSS_CONN_ENTRY * conn, char *server_name)
468 {
469  char *reply_buffer = NULL;
470  int size = 0;
471  unsigned short rid;
472 
473  rid = send_request_one_arg (conn, GET_SERVER_HA_MODE, server_name, (int) strlen (server_name) + 1);
474  return_string (conn, rid, &reply_buffer, &size);
475 
476  if (size)
477  {
478  printf ("\n%s\n", reply_buffer);
479  }
480 
481  if (reply_buffer != NULL)
482  {
483  free_and_init (reply_buffer);
484  }
485 }
486 
487 
488 /*
489  * process_server_info_pid() - find process id from server status
490  * return: process id. 0 if server not found.
491  * conn(in): connection info
492  * server(in): database name
493  * server_type(in): COMM_SERVER_TYPE
494  */
495 static int
496 process_server_info_pid (CSS_CONN_ENTRY * conn, const char *server, int server_type)
497 {
498  char search_pattern[256];
499  char *p = NULL;
500  int pid = 0;
501  char *server_info = NULL;
502 
503  if (server == NULL)
504  return 0;
505 
506  process_status_query (conn, server_type, &server_info);
507 
508  if (server_info)
509  {
510  switch (server_type)
511  {
512  default:
513  sprintf (search_pattern, "Server %s (", server);
514  break;
515  }
516  p = strstr (server_info, search_pattern);
517  if (p)
518  {
519  p = strstr (p + strlen (search_pattern), "pid");
520  if (p)
521  {
522  p += 4;
523  }
524  }
525  if (p)
526  pid = atoi (p);
527 
528  free_and_init (server_info);
529  }
530 
531  return pid;
532 }
533 
534 /*
535  * process_ha_node_info_query() - process heartbeat node list
536  * return: none
537  * conn(in): connection info
538  * verbose_yn(in):
539  */
540 static void
542 {
543  char *reply_buffer = NULL;
544  int size = 0;
545 #if !defined(WINDOWS)
546  unsigned short rid;
547 #endif /* !WINDOWS */
548 
549 #if !defined(WINDOWS)
550  rid = send_request_no_args (conn, (verbose_yn) ? GET_HA_NODE_LIST_VERBOSE : GET_HA_NODE_LIST);
551  return_string (conn, rid, &reply_buffer, &size);
552 #endif
553 
554  if (size)
555  {
556  printf ("\n%s\n", reply_buffer);
557  }
558 
559  if (reply_buffer != NULL)
560  {
561  free_and_init (reply_buffer);
562  }
563 }
564 
565 /*
566  process_ha_process_info_query() - process heartbeat process list
567  * return: none
568  * conn(in): connection info
569  * verbose_yn(in):
570  */
571 static void
573 {
574  char *reply_buffer = NULL;
575  int size = 0;
576 #if !defined(WINDOWS)
577  unsigned short rid;
578 #endif /* !WINDOWS */
579 
580 #if !defined(WINDOWS)
582  return_string (conn, rid, &reply_buffer, &size);
583 #endif
584 
585  if (size)
586  {
587  printf ("\n%s\n", reply_buffer);
588  }
589 
590  if (reply_buffer != NULL)
591  {
592  free_and_init (reply_buffer);
593  }
594 }
595 
596 /*
597  * process_ha_ping_host_info_query() - process heartbeat ping hosts list
598  * return: none
599  * conn(in): connection info
600  */
601 static void
603 {
604  char *reply_buffer = NULL;
605  int size = 0;
606 #if !defined(WINDOWS)
607  unsigned short rid;
608 #endif /* !WINDOWS */
609 
610 #if !defined(WINDOWS)
612  return_string (conn, rid, &reply_buffer, &size);
613 #endif
614 
615  if (size > 0 && reply_buffer[0] != '\0')
616  {
617  printf ("\n%s\n", reply_buffer);
618  }
619 
620  if (reply_buffer != NULL)
621  {
622  free_and_init (reply_buffer);
623  }
624 }
625 
626 /*
627  * process_ha_admin_info_query() - request administrative info
628  * return: none
629  * conn(in): connection info
630  */
631 static void
633 {
634  char *reply_buffer = NULL;
635  int size = 0;
636 #if !defined(WINDOWS)
637  unsigned short rid;
638 #endif /* !WINDOWS */
639 
640 #if !defined(WINDOWS)
642  return_string (conn, rid, &reply_buffer, &size);
643 #endif
644 
645  if (size > 0 && reply_buffer[0] != '\0')
646  {
647  printf ("\n%s\n", reply_buffer);
648  }
649 
650  if (reply_buffer != NULL)
651  {
652  free_and_init (reply_buffer);
653  }
654 }
655 
656 /*
657  * process_kill_all_ha_utils() - kill all copylogdb and applylogdb process
658  * return: none
659  * conn(in): connection info
660  */
661 static void
663 {
664  char *reply_buffer = NULL;
665  int size = 0;
666 #if !defined(WINDOWS)
667  unsigned short rid;
668 #endif /* !WINDOWS */
669 
670 #if !defined(WINDOWS)
672  return_string (conn, rid, &reply_buffer, &size);
673 #endif
674 
675  if (size)
676  {
677  printf ("\n%s\n", reply_buffer);
678  }
679 
680  if (reply_buffer != NULL)
681  {
682  free_and_init (reply_buffer);
683  }
684 }
685 
686 /*
687  * process_is_registered_proc () - check registerd copylogdb and applylogdb
688  * return: none
689  * conn(in): connection info
690  */
691 static int
693 {
694  int error = NO_ERROR;
695 #if !defined(WINDOWS)
696  char *reply_buffer = NULL;
697  int size = 0;
698  char buffer[HB_MAX_SZ_PROC_ARGS];
699  int len;
700  unsigned short rid;
701 
702  strncpy (buffer, args, sizeof (buffer) - 1);
703  len = strlen (buffer) + 1;
704  rid = send_request_one_arg (conn, IS_REGISTERED_HA_PROC, (char *) buffer, len);
705  return_string (conn, rid, &reply_buffer, &size);
706 
707  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
708  {
709  error = NO_ERROR;
710  }
711  else
712  {
713  error = ER_FAILED;
714  }
715 
716  if (reply_buffer != NULL)
717  {
718  free_and_init (reply_buffer);
719  }
720 
721 #endif /* !WINDOWS */
722  return error;
723 }
724 
725 /*
726  * process_ha_deregister_by_pid() - deregister heartbeat process by pid
727  * return: NO_ERROR if successful, error code otherwise
728  * conn(in): connection info
729  * pid_string(in):
730  */
731 static int
733 {
734  int error = NO_ERROR;
735 #if !defined(WINDOWS)
736  char *reply_buffer = NULL;
737  int size = 0;
738  unsigned short rid;
739 
740  pid_t pid;
741 
742  pid = htonl (atoi (pid_string));
743 
744  rid = send_request_one_arg (conn, DEREGISTER_HA_PROCESS_BY_PID, (char *) &pid, sizeof (pid));
745  return_string (conn, rid, &reply_buffer, &size);
746 
747  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
748  {
749  error = NO_ERROR;
750  }
751  else
752  {
753  error = ER_FAILED;
754  }
755 
756  if (reply_buffer != NULL)
757  {
758  free_and_init (reply_buffer);
759  }
760 #endif /* !WINDOWS */
761 
762  return error;
763 }
764 
765 /*
766  * process_ha_deregister_by_args () - deregister heartbeat process by args
767  * return: NO_ERROR if successful, error code otherwise
768  * conn(in): connection info
769  * args(in): process arguments
770  */
771 static int
773 {
774  int error = NO_ERROR;
775 #if !defined(WINDOWS)
776  char *reply_buffer = NULL;
777  int size = 0;
778  char buffer[HB_MAX_SZ_PROC_ARGS];
779  int len;
780  unsigned short rid;
781 
782  strncpy (buffer, args, sizeof (buffer) - 1);
783  len = strlen (buffer) + 1;
784  rid = send_request_one_arg (conn, DEREGISTER_HA_PROCESS_BY_ARGS, (char *) buffer, len);
785  return_string (conn, rid, &reply_buffer, &size);
786 
787  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
788  {
789  error = NO_ERROR;
790  }
791  else
792  {
793  error = ER_FAILED;
794  }
795 
796  if (reply_buffer != NULL)
797  {
798  free_and_init (reply_buffer);
799  }
800 
801 #endif /* !WINDOWS */
802 
803  return error;
804 }
805 
806 /*
807  * process_reconfig_heartbeat() - reconfigure heartbeat node
808  * return: none
809  * conn(in): connection info
810  */
811 static int
813 {
814  char *reply_buffer = NULL;
815  int size = 0;
816  int error = NO_ERROR;
817 #if !defined(WINDOWS)
818  unsigned short rid;
819 #endif /* !WINDOWS */
820 
821 #if !defined(WINDOWS)
823  return_string (conn, rid, &reply_buffer, &size);
824 #endif
825 
826  if (size > 0 && reply_buffer[0] != '\0')
827  {
828  printf ("\n%s\n", reply_buffer);
829  error = NO_ERROR;
830  }
831  else
832  {
833  error = ER_FAILED;
834  }
835 
836  if (reply_buffer != NULL)
837  {
838  free_and_init (reply_buffer);
839  }
840 
841  return error;
842 }
843 
844 /*
845  * process_deactivate_heartbeat() - deactivate heartbeat
846  * return: none
847  * conn(in): connection info
848  */
849 static int
851 {
852  int error = NO_ERROR;
853  char *msg_reply_buffer = NULL;
854  char *result_reply_buffer = NULL;
855  int size = 0;
856 #if !defined(WINDOWS)
857  unsigned short rid;
858 #endif /* !WINDOWS */
859 
860 #if !defined(WINDOWS)
862 
863  return_string (conn, rid, &msg_reply_buffer, &size);
864  if (size > 0 && msg_reply_buffer[0] != '\0')
865  {
866  printf ("\n%s\n", msg_reply_buffer);
867  }
868 
869  return_string (conn, rid, &result_reply_buffer, &size);
870 #endif
871  if (size > 0 && strcmp (result_reply_buffer, HA_REQUEST_SUCCESS) == 0)
872  {
873  error = NO_ERROR;
874  }
875  else
876  {
877  error = ER_FAILED;
878  }
879 
880  if (msg_reply_buffer != NULL)
881  {
882  free_and_init (msg_reply_buffer);
883  }
884 
885  if (result_reply_buffer != NULL)
886  {
887  free_and_init (result_reply_buffer);
888  }
889  return error;
890 }
891 
892 static int
894 {
895  int error = NO_ERROR;
896  char *reply_buffer = NULL;
897  int size = 0;
898 #if !defined(WINDOWS)
899  unsigned short rid;
900 #endif /* !WINDOWS */
901 
902 #if !defined(WINDOWS)
904  return_string (conn, rid, &reply_buffer, &size);
905 #endif
906 
907  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
908  {
909  error = NO_ERROR;
910  }
911  else
912  {
913  error = ER_FAILED;
914  }
915 
916  if (reply_buffer != NULL)
917  {
918  free_and_init (reply_buffer);
919  }
920 
921  return error;
922 }
923 
924 static int
926 {
927  int error = NO_ERROR;
928  char *reply_buffer = NULL;
929  int size = 0;
930 #if !defined(WINDOWS)
931  unsigned short rid;
932 #endif /* !WINDOWS */
933 
934 #if !defined(WINDOWS)
936  return_string (conn, rid, &reply_buffer, &size);
937 #endif
938 
939  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
940  {
941  error = NO_ERROR;
942  }
943  else
944  {
945  error = ER_FAILED;
946  }
947 
948  if (reply_buffer != NULL)
949  {
950  free_and_init (reply_buffer);
951  }
952 
953  return error;
954 }
955 
956 static int
958 {
959  int error = NO_ERROR;
960  char *reply_buffer = NULL;
961  int size = 0;
962 #if !defined(WINDOWS)
963  unsigned short rid;
964 #endif /* !WINDOWS */
965 
966 #if !defined(WINDOWS)
967  rid = send_request_one_arg (conn, DEACT_STOP_ALL, (char *) &commdb_Arg_deact_immediately, sizeof (bool));
968 
969  return_string (conn, rid, &reply_buffer, &size);
970 #endif /* !WINDOWS */
971 
972  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
973  {
974  error = NO_ERROR;
975  }
976  else
977  {
978  error = ER_FAILED;
979  }
980 
981  if (reply_buffer != NULL)
982  {
983  free_and_init (reply_buffer);
984  }
985 
986  return error;
987 }
988 
989 /*
990  * process_activate_heartbeat() - activate heartbeat
991  * return: none
992  * conn(in): connection info
993  */
994 static int
996 {
997  int error = NO_ERROR;
998  char *reply_buffer = NULL;
999  int size = 0;
1000 #if !defined(WINDOWS)
1001  unsigned short rid;
1002 #endif /* !WINDOWS */
1003 
1004 #if !defined(WINDOWS)
1006  return_string (conn, rid, &reply_buffer, &size);
1007 #endif
1008 
1009  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
1010  {
1011  error = NO_ERROR;
1012  }
1013  else
1014  {
1015  error = ER_FAILED;
1016  }
1017 
1018  if (reply_buffer != NULL)
1019  {
1020  free_and_init (reply_buffer);
1021  }
1022 
1023  return error;
1024 }
1025 
1026 /*
1027  * process_ha_start_util_process () - start ha utility process
1028  * return: NO_ERROR if successful, error code otherwise
1029  * conn(in): connection info
1030  * args(in): process arguments
1031  */
1032 static int
1034 {
1035  int error = NO_ERROR;
1036 #if !defined(WINDOWS)
1037  char *reply_buffer = NULL;
1038  int size = 0;
1039  char buffer[HB_MAX_SZ_PROC_ARGS];
1040  int len;
1041  unsigned short rid;
1042 
1043  strncpy (buffer, args, sizeof (buffer) - 1);
1044  len = strlen (buffer) + 1;
1045  rid = send_request_one_arg (conn, START_HA_UTIL_PROCESS, (char *) buffer, len);
1046  return_string (conn, rid, &reply_buffer, &size);
1047 
1048  if (size > 0 && strcmp (reply_buffer, HA_REQUEST_SUCCESS) == 0)
1049  {
1050  error = NO_ERROR;
1051  }
1052  else
1053  {
1054  error = ER_FAILED;
1055  }
1056 
1057  if (reply_buffer != NULL)
1058  {
1059  free_and_init (reply_buffer);
1060  }
1061 
1062 #endif /* !WINDOWS */
1063  return error;
1064 }
1065 
1066 /*
1067  * process_batch_command() - process user command in batch mode
1068  * return: none
1069  * conn(in): connection info
1070  */
1071 static int
1073 {
1074  int pid;
1075 
1077  {
1080  }
1081 
1082  if (commdb_Arg_kill_all)
1083  {
1085  }
1086 
1088  {
1090  }
1091 
1093  {
1095  }
1096 
1098  {
1100  }
1101 
1103  {
1105  }
1106 
1108  {
1110  }
1111 
1113  {
1115  }
1116 
1118  {
1120  }
1121 
1123  {
1125  }
1126 
1128  {
1130  }
1131 
1133  {
1135  }
1136 
1138  {
1140  }
1141 
1143  {
1145  }
1146 
1148  {
1149  return process_reconfig_heartbeat (conn);
1150  }
1151 
1153  {
1154  return process_deactivate_heartbeat (conn);
1155  }
1156 
1158  {
1159  return process_deact_stop_all (conn);
1160  }
1161 
1163  {
1164  return process_deact_confirm_stop_all (conn);
1165  }
1166 
1168  {
1169  return process_deact_confirm_no_server (conn);
1170  }
1171 
1173  {
1174  return process_activate_heartbeat (conn);
1175  }
1176 
1178  {
1180  }
1181 
1182  return NO_ERROR;
1183 }
1184 
1185 /*
1186  * main() - commdb main function
1187  * return: EXIT_SUCCESS/EXIT_FAILURE
1188  */
1189 int
1190 main (int argc, char **argv)
1191 {
1192  int status = EXIT_SUCCESS;
1193  int port_id;
1194  unsigned short rid;
1195  const char *hostname = "localhost";
1196  CSS_CONN_ENTRY *conn;
1197 
1198  static struct option commdb_options[] = {
1221  {COMMDB_HOST_L, 1, 0, COMMDB_HOST_S},
1224  {0, 0, 0, 0}
1225  };
1226 
1227 #if defined(WINDOWS)
1228  if (css_windows_startup () < 0)
1229  {
1230  return EXIT_FAILURE;
1231  }
1232 #endif /* WINDOWS */
1233 
1234 #if !defined(WINDOWS)
1235  if (os_set_signal_handler (SIGPIPE, SIG_IGN) == SIG_ERR)
1236  {
1237  return EXIT_FAILURE;
1238  }
1239 #endif /* ! WINDOWS */
1240 
1241  /* initialize message catalog for argument parsing and usage() */
1242  if (utility_initialize () != NO_ERROR)
1243  {
1244  return EXIT_FAILURE;
1245  }
1246 
1247  ER_SAFE_INIT (NULL, ER_NEVER_EXIT);
1248 
1249  if (argc == 1)
1250  {
1252  goto usage;
1253  }
1254 
1255  while (1)
1256  {
1257  int option_index = 0;
1258  int option_key;
1259  char optstring[64];
1260 
1261  utility_make_getopt_optstring (commdb_options, optstring);
1262  option_key = getopt_long (argc, argv, optstring, commdb_options, &option_index);
1263  if (option_key == -1)
1264  {
1265  break;
1266  }
1267 
1268  switch (option_key)
1269  {
1270  case 'P':
1271  commdb_Arg_print_info = true;
1272  break;
1273  case 'O':
1275  break;
1276  case 'A':
1277  commdb_Arg_kill_all = true;
1278  break;
1279  case 'S':
1281  {
1283  }
1285  break;
1286  case 'c':
1288  {
1290  }
1293  break;
1294  case 'N':
1296  break;
1297  case 'L':
1299  break;
1300  case 'p':
1302  break;
1303  case 'D':
1305  {
1307  }
1310  break;
1311  case 'R':
1313  {
1315  }
1318  break;
1319  case 'd':
1321  break;
1322  case 'C':
1324  {
1326  }
1328  commdb_Arg_is_registered = true;
1329  break;
1330  case 'F':
1332  break;
1335  break;
1338  break;
1339  case 'V':
1341  break;
1342  case 'i':
1344  break;
1347  break;
1350  break;
1353  break;
1354  case COMMDB_HOST_S:
1355  if (commdb_Arg_host_name != NULL)
1356  {
1357  free (commdb_Arg_host_name);
1358  }
1360  break;
1363  break;
1366  {
1368  }
1371  break;
1372  default:
1374  goto usage;
1375  }
1376  }
1377 
1379  {
1380  hostname = commdb_Arg_host_name;
1381  }
1382 
1383  if (master_util_config_startup ((argc > 1) ? argv[1] : NULL, &port_id) == false)
1384  {
1386  status = EXIT_FAILURE;
1387  goto error;
1388  }
1389 
1390  conn = css_connect_to_master_for_info (hostname, port_id, &rid);
1391  if (conn == NULL)
1392  {
1394  status = EXIT_FAILURE;
1395  goto error;
1396  }
1397 
1398  /* command mode */
1399  return process_batch_command (conn);
1400 
1401 error:
1402 #if defined(WINDOWS)
1404 #endif /* WINDOWS */
1405  msgcat_final ();
1406  goto end;
1407 
1408 usage:
1410  msgcat_final ();
1411  status = EXIT_FAILURE;
1412 
1413 end:
1414  if (commdb_Arg_server_name != NULL)
1415  {
1417  }
1418  if (commdb_Arg_ha_mode_server_name != NULL)
1419  {
1421  }
1422  if (commdb_Arg_ha_deregister_pid != NULL)
1423  {
1425  }
1426  if (commdb_Arg_ha_deregister_args != NULL)
1427  {
1429  }
1430  if (commdb_Arg_is_registered_id != NULL)
1431  {
1433  }
1434  if (commdb_Arg_host_name != NULL)
1435  {
1437  }
1438  if (commdb_Arg_ha_util_process_args != NULL)
1439  {
1441  }
1442 
1443  return status;
1444 }
static bool commdb_Arg_print_all_info
Definition: commdb.c:113
#define COMMDB_HA_START_UTIL_PROCESS_L
Definition: utility.h:1434
static void process_slave_kill(CSS_CONN_ENTRY *conn, char *slave_name, int minutes, int pid)
Definition: commdb.c:433
CSS_CONN_ENTRY * css_connect_to_master_for_info(const char *host_name, int port_id, unsigned short *rid)
static int process_is_registered_proc(CSS_CONN_ENTRY *conn, char *args)
Definition: commdb.c:692
#define HA_REQUEST_SUCCESS
#define COMMDB_ACTIVATE_HEARTBEAT_S
Definition: utility.h:1425
#define NO_ERROR
Definition: error_code.h:46
DllImport char * optarg
#define COMMDB_HB_DEACT_IMMEDIATELY_S
Definition: utility.h:1429
COMM_SERVER_TYPE
Definition: commdb.c:72
static bool commdb_Arg_deact_confirm_stop_all
Definition: commdb.c:133
#define PRINT_AND_LOG_ERR_MSG(...)
Definition: util_func.h:49
#define COMMDB_HB_DEACT_IMMEDIATELY_L
Definition: utility.h:1430
static char * commdb_Arg_ha_mode_server_name
Definition: commdb.c:115
#define COMMDB_DEACT_STOP_ALL_S
Definition: utility.h:1419
#define COMMDB_IS_REGISTERED_PROC_S
Definition: utility.h:1413
#define COMMDB_ALL_LIST_S
Definition: utility.h:1391
int argc
Definition: dynamic_load.c:951
static bool commdb_Arg_kill_all_ha_utils
Definition: commdb.c:124
unsigned int htonl(unsigned int from)
void master_util_wait_proc_terminate(int pid)
Definition: master_util.c:72
#define ER_FAILED
Definition: error_code.h:47
static int process_deact_stop_all(CSS_CONN_ENTRY *conn)
Definition: commdb.c:957
#define COMMDB_HA_NODE_LIST_S
Definition: utility.h:1401
static int send_for_all_count(CSS_CONN_ENTRY *conn)
Definition: commdb.c:238
#define COMMDB_DEACT_CONFIRM_STOP_ALL_S
Definition: utility.h:1421
#define COMMDB_DEACT_CONFIRM_STOP_ALL_L
Definition: utility.h:1422
static int process_server_info_pid(CSS_CONN_ENTRY *conn, const char *server, int server_type)
Definition: commdb.c:496
static int process_ha_deregister_by_pid(CSS_CONN_ENTRY *conn, char *pid_string)
Definition: commdb.c:732
#define COMMDB_DEACT_CONFIRM_NO_SERVER_L
Definition: utility.h:1424
#define COMMDB_DEREG_HA_BY_ARGS_L
Definition: utility.h:1410
#define COMMDB_CMD_ALLOWED_ON_REMOTE()
Definition: commdb.c:58
int getopt_long(int, char *const *, const char *, const struct option *, int *)
static char * commdb_Arg_host_name
Definition: commdb.c:135
#define COMMDB_HA_ADMIN_INFO_L
Definition: utility.h:1432
static void process_kill_all_ha_utils(CSS_CONN_ENTRY *conn)
Definition: commdb.c:662
#define COMMDB_ALL_LIST_L
Definition: utility.h:1392
#define COMMDB_DEREG_HA_BY_PID_L
Definition: utility.h:1408
static int send_for_server_stats(CSS_CONN_ENTRY *conn)
Definition: commdb.c:249
static unsigned short send_request_one_arg(CSS_CONN_ENTRY *conn, int command, char *buffer, int size)
Definition: commdb.c:165
static int process_ha_deregister_by_args(CSS_CONN_ENTRY *conn, char *args)
Definition: commdb.c:772
int util_log_write_errid(int message_id,...)
Definition: util_func.c:468
static int send_for_request_count(CSS_CONN_ENTRY *conn)
Definition: commdb.c:313
static int send_for_all_stats(CSS_CONN_ENTRY *conn)
Definition: commdb.c:260
static bool commdb_Arg_is_registered
Definition: commdb.c:125
static int process_deact_confirm_no_server(CSS_CONN_ENTRY *conn)
Definition: commdb.c:893
static void process_ha_server_mode(CSS_CONN_ENTRY *conn, char *server_name)
Definition: commdb.c:467
static bool commdb_Arg_ha_mode_server_info
Definition: commdb.c:114
#define COMMDB_VERBOSE_OUTPUT_S
Definition: utility.h:1427
#define COMMDB_KILL_ALL_HA_PROCESS_L
Definition: utility.h:1412
static int process_batch_command(CSS_CONN_ENTRY *conn)
Definition: commdb.c:1072
static bool commdb_Arg_ha_deregister_by_args
Definition: commdb.c:122
#define COMMDB_HA_ADMIN_INFO_S
Definition: utility.h:1431
#define COMMDB_SERVER_LIST_L
Definition: utility.h:1390
static bool commdb_Arg_deact_stop_all
Definition: commdb.c:132
#define COMMDB_ACTIVATE_HEARTBEAT_L
Definition: utility.h:1426
int css_send_request(CSS_CONN_ENTRY *conn, int command, unsigned short *request_id, const char *arg_buffer, int arg_buffer_size)
#define COMMDB_RECONFIG_HEARTBEAT_S
Definition: utility.h:1415
static bool commdb_Arg_print_ha_process_info
Definition: commdb.c:117
static void return_string(CSS_CONN_ENTRY *conn, unsigned short request_id, char **buffer, int *buffer_size)
Definition: commdb.c:216
static int process_activate_heartbeat(CSS_CONN_ENTRY *conn)
Definition: commdb.c:995
#define HB_MAX_SZ_PROC_ARGS
Definition: heartbeat.h:83
static bool commdb_Arg_print_ha_admin_info
Definition: commdb.c:119
#define COMMDB_HA_START_UTIL_PROCESS_S
Definition: utility.h:1433
#define COMMDB_IS_REGISTERED_PROC_L
Definition: utility.h:1414
static void process_master_stop_shutdown(CSS_CONN_ENTRY *conn)
Definition: commdb.c:401
static unsigned short send_for_start_time(CSS_CONN_ENTRY *conn)
Definition: commdb.c:202
#define COMMDB_SHUTDOWN_ALL_L
Definition: utility.h:1396
static int process_deactivate_heartbeat(CSS_CONN_ENTRY *conn)
Definition: commdb.c:850
static bool commdb_Arg_print_ha_ping_hosts_info
Definition: commdb.c:118
static char * commdb_Arg_is_registered_id
Definition: commdb.c:126
static unsigned short send_request_no_args(CSS_CONN_ENTRY *conn, int command)
Definition: commdb.c:146
#define COMMDB_HOST_S
Definition: utility.h:1397
#define COMMDB_DEACT_STOP_ALL_L
Definition: utility.h:1420
int utility_initialize()
Definition: util_common.c:69
#define NULL
Definition: freelistheap.h:34
bool master_util_config_startup(const char *db_name, int *port_id)
Definition: master_util.c:42
#define COMMDB_DEACTIVATE_HEARTBEAT_L
Definition: utility.h:1418
pid_t pid
Definition: dynamic_load.c:955
#define COMMDB_DEREG_HA_BY_PID_S
Definition: utility.h:1407
char * utility_make_getopt_optstring(const GETOPT_LONG *opt_array, char *buf)
Definition: util_support.c:49
#define COMMDB_VERBOSE_OUTPUT_L
Definition: utility.h:1428
static void process_master_shutdown(CSS_CONN_ENTRY *conn, int minutes)
Definition: commdb.c:413
#define COMMDB_DEREG_HA_BY_ARGS_S
Definition: utility.h:1409
static void process_status_query(CSS_CONN_ENTRY *conn, int server_type, char **server_info)
Definition: commdb.c:327
static bool commdb_Arg_deactivate_heartbeat
Definition: commdb.c:128
static bool commdb_Arg_reconfig_heartbeat
Definition: commdb.c:127
int main(int argc, char **argv)
Definition: commdb.c:1190
#define COMMDB_HA_PING_HOST_LIST_L
Definition: utility.h:1406
static bool commdb_Arg_verbose_output
Definition: commdb.c:131
#define COMMDB_SERVER_LIST_S
Definition: utility.h:1389
static unsigned short send_request_two_args(CSS_CONN_ENTRY *conn, int command, char *buffer1, int size1, char *buffer2, int size2)
Definition: commdb.c:186
static void process_ha_node_info_query(CSS_CONN_ENTRY *conn, int verbose_yn)
Definition: commdb.c:541
static char * commdb_Arg_ha_util_process_args
Definition: commdb.c:137
static int commdb_Arg_shutdown_time
Definition: commdb.c:110
static void error(const char *msg)
Definition: gencat.c:331
#define COMMDB_HA_PROCESS_LIST_L
Definition: utility.h:1404
#define COMMDB_HA_PING_HOST_LIST_S
Definition: utility.h:1405
int css_receive_data(CSS_CONN_ENTRY *conn, unsigned short req_id, char **buffer, int *buffer_size, int timeout)
#define COMMDB_RECONFIG_HEARTBEAT_L
Definition: utility.h:1416
static int process_deact_confirm_stop_all(CSS_CONN_ENTRY *conn)
Definition: commdb.c:925
int css_windows_startup(void)
Definition: wintcp.c:94
static bool commdb_Arg_print_ha_node_info
Definition: commdb.c:116
#define COMMDB_KILL_ALL_HA_PROCESS_S
Definition: utility.h:1411
static bool commdb_Arg_kill_all
Definition: commdb.c:111
const char ** argv
Definition: dynamic_load.c:952
int msgcat_final(void)
#define free_and_init(ptr)
Definition: memory_alloc.h:147
static void process_ha_admin_info_query(CSS_CONN_ENTRY *conn)
Definition: commdb.c:632
static char * commdb_Arg_server_name
Definition: commdb.c:108
#define COMMDB_DEACTIVATE_HEARTBEAT_S
Definition: utility.h:1417
#define COMMDB_SHUTDOWN_ALL_S
Definition: utility.h:1395
#define strlen(s1)
Definition: intl_support.c:43
#define COMMDB_HOST_L
Definition: utility.h:1398
#define COMMDB_SERVER_MODE_S
Definition: utility.h:1399
void css_windows_shutdown(void)
Definition: wintcp.c:140
static bool commdb_Arg_ha_deregister_by_pid
Definition: commdb.c:120
static bool commdb_Arg_print_info
Definition: commdb.c:112
#define COMMDB_SERVER_MODE_L
Definition: utility.h:1400
char * msgcat_message(int cat_id, int set_id, int msg_id)
static void process_master_kill(CSS_CONN_ENTRY *conn)
Definition: commdb.c:387
static int send_for_server_count(CSS_CONN_ENTRY *conn)
Definition: commdb.c:227
char * strdup(const char *str)
Definition: porting.c:901
static bool commdb_Arg_activate_heartbeat
Definition: commdb.c:130
unsigned int ntohl(unsigned int from)
int css_send_data(CSS_CONN_ENTRY *conn, unsigned short rid, const char *buffer, int buffer_size)
static int process_ha_start_util_process(CSS_CONN_ENTRY *conn, char *args)
Definition: commdb.c:1033
#define COMMDB_HA_NODE_LIST_L
Definition: utility.h:1402
void usage(void)
static char * commdb_Arg_ha_deregister_pid
Definition: commdb.c:121
static bool commdb_Arg_ha_start_util_process
Definition: commdb.c:136
#define COMMDB_SHUTDOWN_SERVER_S
Definition: utility.h:1393
static bool commdb_Arg_deact_confirm_no_server
Definition: commdb.c:134
#define COMMDB_DEACT_CONFIRM_NO_SERVER_S
Definition: utility.h:1423
#define COMMDB_HA_PROCESS_LIST_S
Definition: utility.h:1403
static void process_ha_ping_host_info_query(CSS_CONN_ENTRY *conn)
Definition: commdb.c:602
static bool commdb_Arg_halt_shutdown
Definition: commdb.c:109
static bool commdb_Arg_deact_immediately
Definition: commdb.c:129
SIGNAL_HANDLER_FUNCTION os_set_signal_handler(const int sig_no, SIGNAL_HANDLER_FUNCTION sig_handler)
Definition: porting.c:1333
static void process_ha_process_info_query(CSS_CONN_ENTRY *conn, int verbose_yn)
Definition: commdb.c:572
const char ** p
Definition: dynamic_load.c:945
static int process_reconfig_heartbeat(CSS_CONN_ENTRY *conn)
Definition: commdb.c:812
static int return_integer_data(CSS_CONN_ENTRY *conn, unsigned short request_id)
Definition: commdb.c:286
static char * commdb_Arg_ha_deregister_args
Definition: commdb.c:123
#define COMMDB_SHUTDOWN_SERVER_L
Definition: utility.h:1394
#define MSGCAT_CATALOG_UTILS