CUBRID Engine  latest
util_service.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  * util_service.c - a front end of service utilities
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <assert.h>
33 #if !defined(WINDOWS)
34 #include <sys/wait.h>
35 #endif
36 #if defined(WINDOWS)
37 #include <io.h>
38 #endif
39 #include "porting.h"
40 #include "utility.h"
41 #include "error_code.h"
42 #include "error_manager.h"
43 #include "system_parameter.h"
44 #include "connection_cl.h"
45 #include "util_func.h"
46 #include "util_support.h"
47 
48 #if defined(WINDOWS)
49 #include "wintcp.h"
50 #endif
51 #include "environment_variable.h"
52 #include "release_string.h"
53 #include "dynamic_array.h"
54 #include "heartbeat.h"
55 
56 #if defined(WINDOWS)
57 typedef int pid_t;
58 #endif
59 
60 typedef enum
61 {
62  SERVICE = 0,
63  SERVER = 1,
64  BROKER = 2,
65  MANAGER = 3,
66  HEARTBEAT = 4,
67  UTIL_HELP = 6,
69  ADMIN = 8,
72 
73 typedef enum
74 {
82  ON,
83  OFF,
93 
94 typedef enum
95 {
104 
105 typedef enum
106 {
110 
111 typedef enum
112 {
117 
118 typedef enum
119 {
124 
125 typedef struct
126 {
128  const char *option_name;
131 
132 typedef struct
133 {
135  const char *property_value;
137 
138 #define UTIL_TYPE_SERVICE "service"
139 #define UTIL_TYPE_SERVER "server"
140 #define UTIL_TYPE_BROKER "broker"
141 #define UTIL_TYPE_MANAGER "manager"
142 #define UTIL_TYPE_HEARTBEAT "heartbeat"
143 #define UTIL_TYPE_HB_SHORT "hb"
144 #define UTIL_TYPE_JAVASP "javasp"
145 
152  {HEARTBEAT, UTIL_TYPE_HB_SHORT, MASK_HEARTBEAT},
154  {UTIL_HELP, "--help", MASK_ALL},
155  {UTIL_VERSION, "--version", MASK_ALL},
157  {ADMIN, UTIL_OPTION_RENAMEDB, MASK_ADMIN},
158  {ADMIN, UTIL_OPTION_COPYDB, MASK_ADMIN},
159  {ADMIN, UTIL_OPTION_DELETEDB, MASK_ADMIN},
160  {ADMIN, UTIL_OPTION_BACKUPDB, MASK_ADMIN},
161  {ADMIN, UTIL_OPTION_RESTOREDB, MASK_ADMIN},
162  {ADMIN, UTIL_OPTION_ADDVOLDB, MASK_ADMIN},
163 #if 0
164  {ADMIN, UTIL_OPTION_DELVOLDB, MASK_ADMIN},
165 #endif /* ` */
166  {ADMIN, UTIL_OPTION_SPACEDB, MASK_ADMIN},
167  {ADMIN, UTIL_OPTION_LOCKDB, MASK_ADMIN},
168  {ADMIN, UTIL_OPTION_KILLTRAN, MASK_ADMIN},
169  {ADMIN, UTIL_OPTION_OPTIMIZEDB, MASK_ADMIN},
170  {ADMIN, UTIL_OPTION_INSTALLDB, MASK_ADMIN},
171  {ADMIN, UTIL_OPTION_DIAGDB, MASK_ADMIN},
172  {ADMIN, UTIL_OPTION_PATCHDB, MASK_ADMIN},
173  {ADMIN, UTIL_OPTION_CHECKDB, MASK_ADMIN},
174  {ADMIN, UTIL_OPTION_ALTERDBHOST, MASK_ADMIN},
175  {ADMIN, UTIL_OPTION_PLANDUMP, MASK_ADMIN},
176  {ADMIN, UTIL_OPTION_ESTIMATE_DATA, MASK_ADMIN},
177  {ADMIN, UTIL_OPTION_ESTIMATE_INDEX, MASK_ADMIN},
178  {ADMIN, UTIL_OPTION_LOADDB, MASK_ADMIN},
179  {ADMIN, UTIL_OPTION_UNLOADDB, MASK_ADMIN},
180  {ADMIN, UTIL_OPTION_COMPACTDB, MASK_ADMIN},
181  {ADMIN, UTIL_OPTION_PARAMDUMP, MASK_ADMIN},
182  {ADMIN, UTIL_OPTION_STATDUMP, MASK_ADMIN},
183  {ADMIN, UTIL_OPTION_CHANGEMODE, MASK_ADMIN},
184  {ADMIN, UTIL_OPTION_APPLYINFO, MASK_ADMIN},
185  {ADMIN, UTIL_OPTION_GENERATE_LOCALE, MASK_ADMIN},
186  {ADMIN, UTIL_OPTION_DUMP_LOCALE, MASK_ADMIN},
187  {ADMIN, UTIL_OPTION_TRANLIST, MASK_ADMIN},
188  {ADMIN, UTIL_OPTION_GEN_TZ, MASK_ADMIN},
189  {ADMIN, UTIL_OPTION_DUMP_TZ, MASK_ADMIN},
190  {ADMIN, UTIL_OPTION_SYNCCOLLDB, MASK_ADMIN},
191  {ADMIN, UTIL_OPTION_RESTORESLAVE, MASK_ADMIN},
192  {ADMIN, UTIL_OPTION_VACUUMDB, MASK_ADMIN},
193  {ADMIN, UTIL_OPTION_CHECKSUMDB, MASK_ADMIN},
194  {ADMIN, UTIL_OPTION_TDE, MASK_ADMIN},
195  {-1, "", MASK_ADMIN}
196 };
197 
198 #define COMMAND_TYPE_START "start"
199 #define COMMAND_TYPE_STOP "stop"
200 #define COMMAND_TYPE_RESTART "restart"
201 #define COMMAND_TYPE_STATUS "status"
202 #define COMMAND_TYPE_DEREG "deregister"
203 #define COMMAND_TYPE_LIST "list"
204 #define COMMAND_TYPE_RELOAD "reload"
205 #define COMMAND_TYPE_ON "on"
206 #define COMMAND_TYPE_OFF "off"
207 #define COMMAND_TYPE_ACL "acl"
208 #define COMMAND_TYPE_RESET "reset"
209 #define COMMAND_TYPE_INFO "info"
210 #define COMMAND_TYPE_COPYLOGDB "copylogdb"
211 #define COMMAND_TYPE_APPLYLOGDB "applylogdb"
212 #define COMMAND_TYPE_GETID "getid"
213 #define COMMAND_TYPE_TEST "test"
214 #define COMMAND_TYPE_REPLICATION "replication"
215 #define COMMAND_TYPE_REPLICATION_SHORT "repl"
216 
219  {STOP, COMMAND_TYPE_STOP, MASK_ALL},
221  {STATUS, COMMAND_TYPE_STATUS, MASK_ALL},
223  {LIST, COMMAND_TYPE_LIST, MASK_HEARTBEAT},
224  {RELOAD, COMMAND_TYPE_RELOAD, MASK_HEARTBEAT},
226  {OFF, COMMAND_TYPE_OFF, MASK_BROKER},
227  {ACCESS_CONTROL, COMMAND_TYPE_ACL, MASK_SERVER | MASK_BROKER},
228  {RESET, COMMAND_TYPE_RESET, MASK_BROKER},
229  {INFO, COMMAND_TYPE_INFO, MASK_BROKER},
230  {SC_COPYLOGDB, COMMAND_TYPE_COPYLOGDB, MASK_HEARTBEAT},
231  {SC_APPLYLOGDB, COMMAND_TYPE_APPLYLOGDB, MASK_HEARTBEAT},
232  {GET_SHARID, COMMAND_TYPE_GETID, MASK_BROKER},
233  {TEST, COMMAND_TYPE_TEST, MASK_BROKER},
234  {REPLICATION, COMMAND_TYPE_REPLICATION, MASK_HEARTBEAT},
235  {REPLICATION, COMMAND_TYPE_REPLICATION_SHORT, MASK_HEARTBEAT},
236  {-1, "", MASK_ALL}
237 };
238 
241  {SERVICE_START_BROKER, NULL},
242  {SERVICE_START_MANAGER, NULL},
243  {SERVER_START_LIST, NULL},
244  {SERVICE_START_HEARTBEAT, NULL},
245  {SERVICE_START_JAVASP, NULL},
246  {-1, NULL}
247 };
248 
249 
250 static const char **Argv;
251 static int ha_mode_in_common;
252 
253 static int util_get_service_option_mask (int util_type);
254 static int util_get_command_option_mask (int command_type);
255 static void util_service_usage (int util_type);
256 static void util_service_version (const char *argv0);
257 static int load_properties (void);
258 static void finalize_properties (void);
259 static const char *get_property (int property_type);
260 static int parse_arg (UTIL_SERVICE_OPTION_MAP_T * option, const char *arg);
261 static int process_service (int command_type, bool process_window_service);
262 static int process_server (int command_type, int argc, char **argv, bool show_usage, bool check_ha_mode,
263  bool process_window_service);
264 static int process_broker (int command_type, int argc, const char **argv, bool process_window_service);
265 static int process_manager (int command_type, bool process_window_service);
266 static int process_javasp (int command_type, int argc, const char **argv, bool process_window_service);
267 static int process_javasp_start (const char *db_name, bool process_window_service);
268 static int process_javasp_stop (const char *db_name, bool process_window_service);
269 static int process_javasp_status (const char *db_name);
270 static int process_heartbeat (int command_type, int argc, const char **argv);
271 static int process_heartbeat_start (HA_CONF * ha_conf, int argc, const char **argv);
272 static int process_heartbeat_stop (HA_CONF * ha_conf, int argc, const char **argv);
273 static int process_heartbeat_deregister (int argc, const char **argv);
274 static int process_heartbeat_status (int argc, const char **argv);
275 static int process_heartbeat_reload (int argc, const char **argv);
276 static int process_heartbeat_util (HA_CONF * ha_conf, int command_type, int argc, const char **argv);
277 static int process_heartbeat_replication (HA_CONF * ha_conf, int argc, const char **argv);
278 
279 static int proc_execute_internal (const char *file, const char *args[], bool wait_child, bool close_output,
280  bool close_err, bool hide_cmd_args, int *pid);
281 static int proc_execute (const char *file, const char *args[], bool wait_child, bool close_output, bool close_err,
282  int *pid);
283 static int proc_execute_hide_cmd_args (const char *file, const char *args[], bool wait_child, bool close_output,
284  bool close_err, int *pid);
285 static void hide_cmd_line_args (char **args);
286 static int process_master (int command_type);
287 static void print_message (FILE * output, int message_id, ...);
288 static void print_result (const char *util_name, int status, int command_type);
289 static bool is_terminated_process (const int pid);
290 static char *make_exec_abspath (char *buf, int buf_len, char *cmd);
291 static const char *command_string (int command_type);
292 static bool is_server_running (const char *type, const char *server_name, int pid);
293 static int is_broker_running (void);
294 static UTIL_MANAGER_SERVER_STATUS_E is_manager_running (unsigned int sleep_time);
295 static UTIL_JAVASP_SERVER_STATUS_E is_javasp_running (const char *server_name);
296 
297 #if defined(WINDOWS)
298 static bool is_windows_service_running (unsigned int sleep_time);
299 #endif
300 static bool are_all_services_running (unsigned int sleep_time, bool check_win_service);
301 static bool are_all_services_stopped (unsigned int sleep_time, bool check_win_service);
302 static bool check_all_services_status (unsigned int sleep_time, UTIL_ALL_SERVICES_STATUS expected_status,
303  bool check_win_service);
304 
305 static bool ha_make_mem_size (char *mem_size, int size, int value);
306 static bool ha_is_registered (const char *args, const char *hostname);
307 
308 #if !defined(WINDOWS)
309 static int us_hb_copylogdb_start (dynamic_array * out_ap, HA_CONF * ha_conf, const char *db_name, const char *node_name,
310  const char *remote_host);
311 static int us_hb_copylogdb_stop (HA_CONF * ha_conf, const char *db_name, const char *node_name,
312  const char *remote_host);
313 
314 static int us_hb_applylogdb_start (dynamic_array * out_ap, HA_CONF * ha_conf, const char *db_name,
315  const char *node_name, const char *remote_host);
316 static int us_hb_applylogdb_stop (HA_CONF * ha_conf, const char *db_name, const char *node_name,
317  const char *remote_host);
318 
319 #if defined (ENABLE_UNUSED_FUNCTION)
320 static int us_hb_utils_start (dynamic_array * pids, HA_CONF * ha_conf, const char *db_name, const char *node_name);
321 static int us_hb_utils_stop (HA_CONF * ha_conf, const char *db_name, const char *node_name);
322 #endif /* ENABLE_UNUSED_FUNCTION */
323 
324 static int us_hb_server_start (HA_CONF * ha_conf, const char *db_name);
325 static int us_hb_server_stop (HA_CONF * ha_conf, const char *db_name);
326 
327 static int us_hb_process_start (HA_CONF * ha_conf, const char *db_name, bool check_result);
328 static int us_hb_process_stop (HA_CONF * ha_conf, const char *db_name);
329 
330 static int us_hb_process_copylogdb (int command_type, HA_CONF * ha_conf, const char *db_name, const char *node_name,
331  const char *remote_host);
332 static int us_hb_process_applylogdb (int command_type, HA_CONF * ha_conf, const char *db_name, const char *node_name,
333  const char *remote_host);
334 #if defined (ENABLE_UNUSED_FUNCTION)
335 static int us_hb_process_server (int command_type, HA_CONF * ha_conf, const char *db_name);
336 #endif /* ENABLE_UNUSED_FUNCTION */
337 
338 static int us_hb_stop_get_options (char *db_name, int db_name_size, char *host_name, int host_name_size,
339  bool * immediate_stop, int argc, const char **argv);
340 static int us_hb_status_get_options (bool * verbose, char *remote_host_name, int remote_host_name_size, int argc,
341  const char **argv);
342 static int us_hb_util_get_options (char *db_name, int db_name_size, char *node_name, int node_name_size,
343  char *remote_host_name, int remote_host_name_size, int argc, const char **argv);
344 
345 
346 #define US_HB_DEREG_WAIT_TIME_IN_SEC 100
347 #endif /* !WINDOWS */
348 
349 static char *
350 make_exec_abspath (char *buf, int buf_len, char *cmd)
351 {
352  buf[0] = '\0';
353 
354  (void) envvar_bindir_file (buf, buf_len, cmd);
355 
356  return buf;
357 }
358 
359 static const char *
360 command_string (int command_type)
361 {
362  const char *command;
363 
364  switch (command_type)
365  {
366  case START:
367  command = PRINT_CMD_START;
368  break;
369  case STATUS:
370  command = PRINT_CMD_STATUS;
371  break;
372  case DEREGISTER:
373  command = PRINT_CMD_DEREG;
374  break;
375  case LIST:
376  command = PRINT_CMD_LIST;
377  break;
378  case RELOAD:
379  command = PRINT_CMD_RELOAD;
380  break;
381  case ACCESS_CONTROL:
382  command = PRINT_CMD_ACL;
383  break;
384  case SC_COPYLOGDB:
385  command = PRINT_CMD_COPYLOGDB;
386  break;
387  case SC_APPLYLOGDB:
388  command = PRINT_CMD_APPLYLOGDB;
389  break;
390  case TEST:
391  command = PRINT_CMD_TEST;
392  break;
393  case REPLICATION:
394  command = PRINT_CMD_REPLICATION;
395  break;
396  case STOP:
397  default:
398  command = PRINT_CMD_STOP;
399  break;
400  }
401 
402  return command;
403 }
404 
405 static void
406 print_result (const char *util_name, int status, int command_type)
407 {
408  const char *result;
409 
410  if (status != NO_ERROR)
411  {
412  result = PRINT_RESULT_FAIL;
413  }
414  else
415  {
416  result = PRINT_RESULT_SUCCESS;
417  }
418 
419  print_message (stdout, MSGCAT_UTIL_GENERIC_RESULT, util_name, command_string (command_type), result);
420 }
421 
422 /*
423  * print_message() -
424  *
425  * return:
426  *
427  */
428 static void
429 print_message (FILE * output, int message_id, ...)
430 {
431  va_list arg_list;
432  const char *format;
433 
434  format = utility_get_generic_message (message_id);
435  va_start (arg_list, message_id);
436  vfprintf (output, format, arg_list);
437  va_end (arg_list);
438 }
439 
440 /*
441  * process_admin() - process admin utility
442  *
443  * return:
444  *
445  */
446 static int
447 process_admin (int argc, char **argv)
448 {
449  char **copy_argv;
450  int status;
451 
452  /* execv expects NULL terminated arguments vector */
453  copy_argv = (char **) malloc (sizeof (char *) * (argc + 1));
454  if (copy_argv == NULL)
455  {
456  return ER_GENERIC_ERROR;
457  }
458 
459  memcpy (copy_argv, argv, sizeof (char *) * argc);
460  copy_argv[argc] = NULL;
461 
462  status = proc_execute_hide_cmd_args (UTIL_ADMIN_NAME, (const char **) copy_argv, true, false, false, NULL);
463 
464  free (copy_argv);
465 
466  return status;
467 }
468 
469 /*
470  * util_get_service_option_mask () -
471  *
472  */
473 static int
475 {
476  int i;
477 
478  assert (util_type != ADMIN);
479 
480  for (i = 0; us_Service_map[i].option_type != -1; i++)
481  {
482  if (us_Service_map[i].option_type == util_type)
483  {
484  return us_Service_map[i].option_mask;
485  }
486  }
487  return 0; /* NULL mask */
488 }
489 
490 /*
491  * util_get_command_option_mask () -
492  *
493  */
494 static int
496 {
497  int i;
498 
499  for (i = 0; us_Command_map[i].option_type != -1; i++)
500  {
501  if (us_Command_map[i].option_type == command_type)
502  {
503  return us_Command_map[i].option_mask;
504  }
505  }
506  return 0; /* NULL mask */
507 }
508 
509 /*
510  * main() - a service utility's entry point
511  *
512  * return:
513  *
514  * NOTE:
515  */
516 int
517 main (int argc, char *argv[])
518 {
519  int util_type, command_type;
520  int status;
521  bool process_window_service = false;
522  pid_t pid = getpid ();
523  char env_buf[16];
524 
525 #if defined (DO_NOT_USE_CUBRIDENV)
526  char *envval;
527  char path[PATH_MAX];
528 
529  envval = getenv (envvar_prefix ());
530  if (envval != NULL)
531  {
532  fprintf (stderr,
533  "CAUTION : " "The environment variable $%s is set to %s.\n"
534  " But, built-in prefix (%s) will be used.\n\n", envvar_prefix (), envval, envvar_root ());
535  }
536 
537  envval = envvar_get ("DATABASES");
538  if (envval != NULL)
539  {
540  fprintf (stderr,
541  "CAUTION : " "The environment variable $%s_%s is set to %s.\n"
542  " But, built-in prefix (%s) will be used.\n\n", envvar_prefix (), "DATABASES", envval,
543  envvar_vardir_file (path, PATH_MAX, ""));
544  }
545 #endif
546 
547  sprintf (env_buf, "%d", pid);
548  envvar_set (UTIL_PID_ENVVAR_NAME, env_buf);
549 
550  ER_SAFE_INIT (NULL, ER_NEVER_EXIT);
551 
552  Argv = (const char **) argv;
553  if (argc == 2)
554  {
555  if (parse_arg (us_Service_map, (char *) argv[1]) == UTIL_VERSION)
556  {
557  util_service_version (argv[0]);
558  return EXIT_SUCCESS;
559  }
560  }
561 
562  /* validate the number of arguments to avoid Klockwork's error message */
563  if (argc < 2 || argc > 1024)
564  {
565  util_type = -1;
566  goto usage;
567  }
568 
569  util_type = parse_arg (us_Service_map, (char *) argv[1]);
570  if (util_type == ER_GENERIC_ERROR)
571  {
572  util_type = parse_arg (us_Service_map, (char *) argv[2]);
573  if (util_type == ER_GENERIC_ERROR)
574  {
576  goto error;
577  }
578  }
579 
580  if (load_properties () != NO_ERROR)
581  {
583 
584  util_log_write_command (argc, argv);
586 
587  return EXIT_FAILURE;
588  }
589 
591 
592  if (util_type == ADMIN)
593  {
594  util_log_write_command (argc, argv);
595  status = process_admin (argc, argv);
596  util_log_write_result (status);
597 
598  return status;
599  }
600 
601  if (util_type == UTIL_HELP)
602  {
603  util_type = -1;
604  goto usage;
605  }
606 
607  if (argc < 3)
608  {
609  goto usage;
610  }
611 
612  command_type = parse_arg (us_Command_map, (char *) argv[2]);
613  if (command_type == ER_GENERIC_ERROR)
614  {
615  command_type = parse_arg (us_Command_map, (char *) argv[1]);
616  if (command_type == ER_GENERIC_ERROR)
617  {
619  goto error;
620  }
621  }
622  else
623  {
624  int util_mask = util_get_service_option_mask (util_type);
625  int command_mask = util_get_command_option_mask (command_type);
626 
627  if ((util_mask & command_mask) == 0)
628  {
630  goto error;
631  }
632  }
633 
634  util_log_write_command (argc, argv);
635 
636 #if defined(WINDOWS)
637  if (css_windows_startup () < 0)
638  {
639  util_log_write_errstr ("Unable to initialize Winsock.\n");
640  goto error;
641  }
642 
643  process_window_service = true;
644 
645  if ((util_type == SERVICE || util_type == BROKER || util_type == MANAGER) && (argc > 3)
646  && strcmp ((char *) argv[3], "--for-windows-service") == 0)
647  {
648  process_window_service = false;
649  }
650  else if ((util_type == SERVER || util_type == BROKER || util_type == JAVASP_UTIL) && (argc > 4)
651  && strcmp ((char *) argv[4], "--for-windows-service") == 0)
652  {
653  process_window_service = false;
654  }
655 #else
656  assert (process_window_service == false);
657 #endif
658 
659  switch (util_type)
660  {
661  case SERVICE:
662  {
663  status = process_service (command_type, process_window_service);
664  }
665  break;
666  case SERVER:
667  status = process_server (command_type, argc - 3, &argv[3], true, true, process_window_service);
668  break;
669  case BROKER:
670  status = process_broker (command_type, argc - 3, (const char **) &argv[3], process_window_service);
671  break;
672  case MANAGER:
673  status = process_manager (command_type, process_window_service);
674  break;
675  case HEARTBEAT:
676 #if defined(WINDOWS)
677  /* TODO : define message catalog for heartbeat */
678  return EXIT_FAILURE;
679 #else
680  status = process_heartbeat (command_type, argc - 3, (const char **) &argv[3]);
681 #endif /* !WINDOWs */
682  break;
683  case JAVASP_UTIL:
684  status = process_javasp (command_type, argc - 3, (const char **) &argv[3], process_window_service);
685  break;
686  default:
687  goto usage;
688  }
689 
690  util_log_write_result (status);
691 
692  return ((status == NO_ERROR) ? EXIT_SUCCESS : EXIT_FAILURE);
693 
694 usage:
695  util_service_usage (util_type);
696 error:
698 #if defined(WINDOWS)
700 #endif /* WINDOWS */
701  return EXIT_FAILURE;
702 }
703 
704 /*
705  * util_service_usage - display an usage of this utility
706  *
707  * return:
708  *
709  * NOTE:
710  */
711 static void
712 util_service_usage (int util_type)
713 {
714  char *exec_name;
715 
716  if (util_type < 0)
717  {
718  util_type = 0;
719  }
720  else
721  {
722  util_type++;
723  }
724 
725  exec_name = basename ((char *) Argv[0]);
726  print_message (stdout, MSGCAT_UTIL_GENERIC_CUBRID_USAGE + util_type, PRODUCT_STRING, exec_name, exec_name, exec_name);
727 }
728 
729 /*
730  * util_service_version - display a version of this utility
731  *
732  * return:
733  *
734  * NOTE:
735  */
736 static void
737 util_service_version (const char *argv0)
738 {
739  const char *exec_name;
740  char buf[REL_MAX_VERSION_LENGTH];
741 
742  exec_name = basename ((char *) argv0);
744  print_message (stdout, MSGCAT_UTIL_GENERIC_VERSION, exec_name, buf);
745 }
746 
747 /*
748  * proc_execute - to fork/exec internal service exes
749  */
750 static int
751 proc_execute (const char *file, const char *args[], bool wait_child, bool close_output, bool close_err, int *out_pid)
752 {
753  return proc_execute_internal (file, args, wait_child, close_output, close_err, false, out_pid);
754 }
755 
756 /*
757  * proc_execute_hide_cmd_args - to fork/exec cub_admin for command line
758  *
759  * It will hide commandline arguments.
760  */
761 static int
762 proc_execute_hide_cmd_args (const char *file, const char *args[], bool wait_child, bool close_output, bool close_err,
763  int *out_pid)
764 {
765  return proc_execute_internal (file, args, wait_child, close_output, close_err, true, out_pid);
766 }
767 
768 #if defined(WINDOWS)
769 static int
770 proc_execute_internal (const char *file, const char *args[], bool wait_child, bool close_output, bool close_err,
771  bool hide_cmd_args, int *out_pid)
772 {
773  STARTUPINFO si;
774  PROCESS_INFORMATION pi;
775  int i, j, k, cmd_arg_len;
776  char cmd_arg[1024];
777  char executable_path[PATH_MAX];
778  int ret_code = NO_ERROR;
779  bool inherited_handle = TRUE;
780  char fixed_arg[1024]; /* replace " with "" */
781 
782  if (out_pid)
783  {
784  *out_pid = 0;
785  }
786 
787  (void) envvar_bindir_file (executable_path, PATH_MAX, file);
788 
789  for (i = 0, cmd_arg_len = 0; args[i]; i++)
790  {
791  if (strchr (args[i], '"') == NULL)
792  {
793  cmd_arg_len += sprintf (cmd_arg + cmd_arg_len, "\"%s\" ", args[i]);
794  }
795  else
796  {
797  k = 0;
798  for (j = 0; j < strlen (args[i]); j++)
799  {
800  if (args[i][j] == '"')
801  {
802  fixed_arg[k++] = '"';
803  }
804  fixed_arg[k++] = args[i][j];
805  }
806  fixed_arg[k] = '\0';
807 
808  cmd_arg_len += sprintf (cmd_arg + cmd_arg_len, "\"%s\" ", fixed_arg);
809  }
810  }
811 
812  GetStartupInfo (&si);
813  si.dwFlags = si.dwFlags | STARTF_USESTDHANDLES;
814  si.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
815  si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
816  si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
817  inherited_handle = TRUE;
818 
819  if (close_output)
820  {
821  si.hStdOutput = NULL;
822  }
823 
824  if (close_err)
825  {
826  si.hStdError = NULL;
827  }
828 
829  if (!CreateProcess (executable_path, cmd_arg, NULL, NULL, inherited_handle, 0, NULL, NULL, &si, &pi))
830  {
831  return ER_FAILED;
832  }
833 
834  if (wait_child)
835  {
836  DWORD status = 0;
837  WaitForSingleObject (pi.hProcess, INFINITE);
838  GetExitCodeProcess (pi.hProcess, &status);
839  ret_code = status;
840  }
841  else
842  {
843  if (out_pid)
844  {
845  *out_pid = pi.dwProcessId;
846  }
847  }
848 
849  CloseHandle (pi.hProcess);
850  CloseHandle (pi.hThread);
851  return ret_code;
852 }
853 
854 #else
855 static int
856 proc_execute_internal (const char *file, const char *args[], bool wait_child, bool close_output, bool close_err,
857  bool hide_cmd_args, int *out_pid)
858 {
859  pid_t pid, tmp;
860  char executable_path[PATH_MAX];
861 
862  if (out_pid)
863  {
864  *out_pid = 0;
865  }
866 
867  (void) envvar_bindir_file (executable_path, PATH_MAX, file);
868 
869  /* do not process SIGCHLD, a child process will be defunct */
870  if (wait_child)
871  {
872  signal (SIGCHLD, SIG_DFL);
873  }
874  else
875  {
876  signal (SIGCHLD, SIG_IGN);
877  }
878 
879  pid = fork ();
880  if (pid == -1)
881  {
882  perror ("fork");
883  return ER_GENERIC_ERROR;
884  }
885  else if (pid == 0)
886  {
887  /* a child process handle SIGCHLD to SIG_DFL */
888  signal (SIGCHLD, SIG_DFL);
889  if (close_output)
890  {
891  fclose (stdout);
892  }
893 
894  if (close_err)
895  {
896  fclose (stderr);
897  }
898 
899  if (execv (executable_path, (char *const *) args) == -1)
900  {
901  perror ("execv");
902  return ER_GENERIC_ERROR;
903  }
904  }
905  else
906  {
907  int status = 0;
908 
909  if (hide_cmd_args == true)
910  {
911  /* for hide password */
912  hide_cmd_line_args ((char **) args);
913  }
914 
915  /* sleep (0); */
916  if (wait_child)
917  {
918  do
919  {
920  tmp = waitpid (-1, &status, 0);
921  if (tmp == -1)
922  {
923  perror ("waitpid");
924  return ER_GENERIC_ERROR;
925  }
926  }
927  while (tmp != pid);
928  }
929  else
930  {
931  /* sleep (3); */
932  if (out_pid)
933  {
934  *out_pid = pid;
935  }
936  return NO_ERROR;
937  }
938 
939  if (WIFEXITED (status))
940  {
941  return WEXITSTATUS (status);
942  }
943  }
944  return ER_GENERIC_ERROR;
945 }
946 #endif
947 
948 /*
949  * hide_cmd_line_args -
950  *
951  * return:
952  *
953  */
954 static void
955 hide_cmd_line_args (char **args)
956 {
957 #if defined (LINUX)
958  int i;
959 
960  assert (args[0] != NULL && args[1] != NULL);
961 
962  for (i = 2; args[i] != NULL; i++)
963  {
964  memset (args[i], '\0', strlen (args[i]));
965  }
966 #endif /* LINUX */
967 }
968 
969 /*
970  * process_master -
971  *
972  * return:
973  *
974  * command_type(in):
975  */
976 static int
977 process_master (int command_type)
978 {
979  int status = NO_ERROR;
980  int pid = 0;
981  int waited_seconds = 0;
982 
983  int master_port = prm_get_master_port_id ();
984 
985  switch (command_type)
986  {
987  case START:
988  {
990  if (!css_does_master_exist (master_port))
991  {
992  const char *args[] = { UTIL_MASTER_NAME, NULL };
993 
994  status = ER_GENERIC_ERROR;
995  while (status != NO_ERROR && waited_seconds < 180)
996  {
997  if (pid == 0 || (pid != 0 && is_terminated_process (pid)))
998  {
999  if (pid != 0)
1000  {
1001  util_log_write_errstr ("Master does not exist. Try to start it again.\n");
1002  }
1003 
1004 #if !defined(WINDOWS)
1005  envvar_set ("NO_DAEMON", "true");
1006 #endif
1007  status = proc_execute (UTIL_MASTER_NAME, args, false, false, false, &pid);
1008  if (status != NO_ERROR)
1009  {
1010  util_log_write_errstr ("Could not start master process.\n");
1011  break;
1012  }
1013  }
1014 
1015  /* The master process needs a few seconds to bind port */
1016  sleep (1);
1017  waited_seconds++;
1018 
1019  status = css_does_master_exist (master_port) ? NO_ERROR : ER_GENERIC_ERROR;
1020  }
1021 
1022  if (status != NO_ERROR)
1023  {
1024  /* The master process failed to start or could not connected within 3 minutes */
1027  }
1028 
1029  print_result (PRINT_MASTER_NAME, status, command_type);
1030  }
1031  else
1032  {
1033  status = ER_GENERIC_ERROR;
1036  }
1037  }
1038  break;
1039  case STOP:
1041  if (css_does_master_exist (master_port))
1042  {
1043  const char *args[] = { UTIL_COMMDB_NAME, COMMDB_ALL_STOP, NULL };
1044  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
1045 
1046  status = css_does_master_exist (master_port) ? ER_GENERIC_ERROR : NO_ERROR;
1047 
1048  print_result (PRINT_MASTER_NAME, status, command_type);
1049  }
1050  else
1051  {
1052  status = ER_GENERIC_ERROR;
1055  }
1056  break;
1057  }
1058  return status;
1059 }
1060 
1061 #if defined(WINDOWS)
1062 /*
1063  * is_windwos_service_running -
1064  *
1065  * return:
1066  *
1067  * sleep_time(in):
1068  *
1069  * NOTE:
1070  */
1071 static bool
1072 is_windows_service_running (unsigned int sleep_time)
1073 {
1074  FILE *input;
1075  char buf[32], cmd[PATH_MAX];
1076 
1077  sleep (sleep_time);
1078 
1079  make_exec_abspath (cmd, PATH_MAX, (char *) UTIL_WIN_SERVICE_CONTROLLER_NAME " " "-status");
1080 
1081  input = popen (cmd, "r");
1082  if (input == NULL)
1083  {
1084  return false;
1085  }
1086 
1087  memset (buf, '\0', sizeof (buf));
1088 
1089  if ((fgets (buf, 32, input) == NULL) || strncmp (buf, "SERVICE_RUNNING", 15) != 0)
1090  {
1091  pclose (input);
1092  return false;
1093  }
1094 
1095  pclose (input);
1096 
1097  return true;
1098 }
1099 #endif
1100 /*
1101  * are_all_services_running - are all of services running
1102  *
1103  * return:
1104  *
1105  * sleep_time(in):
1106  *
1107  * NOTE:
1108  */
1109 static bool
1110 are_all_services_running (unsigned int sleep_time, bool check_win_service)
1111 {
1112  return check_all_services_status (sleep_time, ALL_SERVICES_RUNNING, check_win_service);
1113 }
1114 
1115 /*
1116  * are_all_services_stopped - are all of services stopped
1117  *
1118  * return:
1119  *
1120  * sleep_time(in):
1121  *
1122  * NOTE:
1123  */
1124 static bool
1125 are_all_services_stopped (unsigned int sleep_time, bool check_win_service)
1126 {
1127  return check_all_services_status (sleep_time, ALL_SERVICES_STOPPED, check_win_service);
1128 }
1129 
1130 
1131 /*
1132  * check_all_services_status - check all service status and compare with
1133  expected_status, if not meet return false.
1134  *
1135  * return:
1136  *
1137  * sleep_time(in):
1138  * expected_status(in);
1139  * NOTE:
1140  */
1141 static bool
1142 check_all_services_status (unsigned int sleep_time, UTIL_ALL_SERVICES_STATUS expected_status, bool check_win_service)
1143 {
1144  bool ret;
1145  int master_port;
1146 
1147  if (check_win_service)
1148  {
1149 #if defined (WINDOWS)
1150  /* check whether CUBRIDService is running */
1151  ret = is_windows_service_running (sleep_time);
1152  if ((expected_status == ALL_SERVICES_RUNNING && !ret) || (expected_status == ALL_SERVICES_STOPPED && ret))
1153  {
1154  return false;
1155  }
1156 #else
1157  assert (0);
1158  return false;
1159 #endif
1160  }
1161 
1162  master_port = prm_get_master_port_id ();
1163  /* check whether cub_master is running */
1164  ret = css_does_master_exist (master_port);
1165  if ((expected_status == ALL_SERVICES_RUNNING && !ret) || (expected_status == ALL_SERVICES_STOPPED && ret))
1166  {
1167  return false;
1168  }
1169 
1170  if (strcmp (get_property (SERVICE_START_SERVER), PROPERTY_ON) == 0
1171  && us_Property_map[SERVER_START_LIST].property_value != NULL)
1172  {
1173  char buf[4096];
1174  char *list, *token, *save;
1175  const char *delim = " ,:";
1176 
1177  memset (buf, '\0', sizeof (buf));
1178 
1179  strncpy (buf, us_Property_map[SERVER_START_LIST].property_value, sizeof (buf) - 1);
1180 
1181  for (list = buf;; list = NULL)
1182  {
1183  token = strtok_r (list, delim, &save);
1184  if (token == NULL)
1185  {
1186  break;
1187  }
1188  /* check whether cub_server is running */
1189  ret = is_server_running (CHECK_SERVER, token, 0);
1190  if ((expected_status == ALL_SERVICES_RUNNING && !ret) || (expected_status == ALL_SERVICES_STOPPED && ret))
1191  {
1192  return false;
1193  }
1194  }
1195  }
1196 
1197  if (strcmp (get_property (SERVICE_START_JAVASP), PROPERTY_ON) == 0
1198  && us_Property_map[SERVER_START_LIST].property_value != NULL)
1199  {
1200  char buf[4096];
1201  char *list, *token, *save;
1202  const char *delim = " ,:";
1203 
1204  memset (buf, '\0', sizeof (buf));
1205 
1206  strncpy (buf, us_Property_map[SERVER_START_LIST].property_value, sizeof (buf) - 1);
1207 
1208  for (list = buf;; list = NULL)
1209  {
1210  token = strtok_r (list, delim, &save);
1211  if (token == NULL)
1212  {
1213  break;
1214  }
1215 
1216  /* check whether cub_javasp is running */
1217  UTIL_JAVASP_SERVER_STATUS_E javasp_status = is_javasp_running (token);
1218  if ((expected_status == ALL_SERVICES_RUNNING && javasp_status != JAVASP_SERVER_RUNNING)
1219  || (expected_status == ALL_SERVICES_STOPPED && javasp_status == JAVASP_SERVER_RUNNING))
1220  {
1221  return false;
1222  }
1223  }
1224  }
1225 
1226  /* check whether cub_broker is running */
1227  if (strcmp (get_property (SERVICE_START_BROKER), PROPERTY_ON) == 0)
1228  {
1229  int broker_status;
1230 
1231  /* broker_status may be 0, 1, 2. */
1232  broker_status = is_broker_running ();
1233  if ((expected_status == ALL_SERVICES_RUNNING && broker_status != 0)
1234  || (expected_status == ALL_SERVICES_STOPPED && broker_status != 1))
1235  {
1236  return false;
1237  }
1238  }
1239 
1240  /* check whether cub_manager is running */
1241  if (strcmp (get_property (SERVICE_START_MANAGER), PROPERTY_ON) == 0)
1242  {
1243  UTIL_MANAGER_SERVER_STATUS_E manager_status;
1244  manager_status = is_manager_running (0);
1245 
1246  if ((expected_status == ALL_SERVICES_RUNNING && manager_status != MANAGER_SERVER_RUNNING)
1247  || (expected_status == ALL_SERVICES_STOPPED && manager_status != MANAGER_SERVER_STOPPED))
1248  {
1249  return false;
1250  }
1251  }
1252 
1253  /* do not check heartbeat in windows */
1254 
1255  return true;
1256 }
1257 
1258 
1259 /*
1260  * process_service -
1261  *
1262  * return:
1263  *
1264  * command_type(in):
1265  * process_window_service(in):
1266  *
1267  * NOTE:
1268  */
1269 static int
1270 process_service (int command_type, bool process_window_service)
1271 {
1272  int status = NO_ERROR;
1273 
1274  switch (command_type)
1275  {
1276  case START:
1277  if (process_window_service)
1278  {
1279 #if defined(WINDOWS)
1280  if (!are_all_services_running (0, process_window_service))
1281  {
1282  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_SERVICE,
1284  };
1285 
1286  proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1287  status = are_all_services_running (0, process_window_service) ? NO_ERROR : ER_GENERIC_ERROR;
1288  print_result (PRINT_SERVICE_NAME, status, command_type);
1289  }
1290  else
1291  {
1294  status = ER_GENERIC_ERROR;
1295  }
1296 #endif
1297  }
1298  else
1299  {
1300  if (!are_all_services_running (0, process_window_service))
1301  {
1302  (void) process_master (command_type);
1303 
1304  if (strcmp (get_property (SERVICE_START_SERVER), PROPERTY_ON) == 0
1305  && us_Property_map[SERVER_START_LIST].property_value != NULL
1306  && us_Property_map[SERVER_START_LIST].property_value[0] != '\0')
1307  {
1308  (void) process_server (command_type, 0, NULL, false, true, false);
1309  }
1310  if (strcmp (get_property (SERVICE_START_BROKER), PROPERTY_ON) == 0)
1311  {
1312  (void) process_broker (command_type, 0, NULL, false);
1313  }
1314  if (strcmp (get_property (SERVICE_START_MANAGER), PROPERTY_ON) == 0)
1315  {
1316  (void) process_manager (command_type, false);
1317  }
1318  if (strcmp (get_property (SERVICE_START_HEARTBEAT), PROPERTY_ON) == 0)
1319  {
1320  (void) process_heartbeat (command_type, 0, NULL);
1321  }
1322  if (strcmp (get_property (SERVICE_START_JAVASP), PROPERTY_ON) == 0
1323  && us_Property_map[SERVER_START_LIST].property_value != NULL
1324  && us_Property_map[SERVER_START_LIST].property_value[0] != '\0')
1325  {
1326  (void) process_javasp (command_type, 0, NULL, false);
1327  }
1328  status = are_all_services_running (0, process_window_service) ? NO_ERROR : ER_GENERIC_ERROR;
1329  }
1330  else
1331  {
1334  status = ER_GENERIC_ERROR;
1335  }
1336  }
1337  break;
1338  case STOP:
1339  if (process_window_service)
1340  {
1341 #if defined(WINDOWS)
1342  if (is_windows_service_running (0))
1343  {
1344  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_SERVICE,
1346  };
1347 
1348  proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1349  status = are_all_services_stopped (0, process_window_service) ? NO_ERROR : ER_GENERIC_ERROR;
1350 
1351  print_result (PRINT_SERVICE_NAME, status, command_type);
1352  }
1353  else
1354  {
1357  status = ER_GENERIC_ERROR;
1358  }
1359 #endif
1360  }
1361  else
1362  {
1363  if (!are_all_services_stopped (0, process_window_service))
1364  {
1365  if (strcmp (get_property (SERVICE_START_SERVER), PROPERTY_ON) == 0
1366  && us_Property_map[SERVER_START_LIST].property_value != NULL
1367  && us_Property_map[SERVER_START_LIST].property_value[0] != '\0')
1368  {
1369  (void) process_server (command_type, 0, NULL, false, true, false);
1370  }
1371 
1372  if (strcmp (get_property (SERVICE_START_JAVASP), PROPERTY_ON) == 0
1373  && us_Property_map[SERVER_START_LIST].property_value != NULL
1374  && us_Property_map[SERVER_START_LIST].property_value[0] != '\0')
1375  {
1376  (void) process_javasp (command_type, 0, NULL, false);
1377  }
1378 
1379  if (strcmp (get_property (SERVICE_START_BROKER), PROPERTY_ON) == 0)
1380  {
1381  (void) process_broker (command_type, 0, NULL, false);
1382  }
1383  if (strcmp (get_property (SERVICE_START_MANAGER), PROPERTY_ON) == 0)
1384  {
1385  (void) process_manager (command_type, false);
1386  }
1387 
1388  if (strcmp (get_property (SERVICE_START_HEARTBEAT), PROPERTY_ON) == 0)
1389  {
1390  (void) process_heartbeat (command_type, 0, NULL);
1391  }
1392 
1393  (void) process_master (command_type);
1394 
1395  status = are_all_services_stopped (0, process_window_service) ? NO_ERROR : ER_GENERIC_ERROR;
1396  }
1397  else
1398  {
1401  status = ER_GENERIC_ERROR;
1402  }
1403  }
1404  break;
1405  case RESTART:
1406  status = process_service (STOP, process_window_service);
1407  status = process_service (START, process_window_service);
1408  break;
1409  case STATUS:
1412  {
1414  }
1415  else
1416  {
1418  }
1419 
1420  {
1421  const char *args[] = { "-b" };
1422 
1423  (void) process_server (command_type, 0, NULL, false, true, false);
1424  (void) process_broker (command_type, 1, args, false);
1425  (void) process_manager (command_type, false);
1426  if (strcmp (get_property (SERVICE_START_JAVASP), PROPERTY_ON) == 0
1427  && us_Property_map[SERVER_START_LIST].property_value != NULL
1428  && us_Property_map[SERVER_START_LIST].property_value[0] != '\0')
1429  {
1430  (void) process_javasp (command_type, 0, NULL, false);
1431  }
1432  if (strcmp (get_property (SERVICE_START_HEARTBEAT), PROPERTY_ON) == 0)
1433  {
1434  (void) process_heartbeat (command_type, 0, NULL);
1435  }
1436  }
1437  break;
1438  default:
1439  status = ER_GENERIC_ERROR;
1440  }
1441 
1442  return status;
1443 }
1444 
1445 /*
1446  * check_server -
1447  *
1448  * return:
1449  *
1450  * type(in):
1451  * server_name(in):
1452  */
1453 static bool
1454 check_server (const char *type, const char *server_name)
1455 {
1456  FILE *input;
1457  char buf[4096], *token, *save_ptr, *delim = (char *) " ";
1458  char cmd[PATH_MAX];
1459 
1460  make_exec_abspath (cmd, PATH_MAX, (char *) UTIL_COMMDB_NAME " " COMMDB_ALL_STATUS);
1461  input = popen (cmd, "r");
1462  if (input == NULL)
1463  {
1464  return false;
1465  }
1466 
1467  while (fgets (buf, 4096, input) != NULL)
1468  {
1469  token = strtok_r (buf, delim, &save_ptr);
1470  if (token == NULL)
1471  {
1472  continue;
1473  }
1474 
1475  if (strcmp (type, CHECK_SERVER) == 0)
1476  {
1477  if (strcmp (token, CHECK_SERVER) != 0 && strcmp (token, CHECK_HA_SERVER) != 0)
1478  {
1479  continue;
1480  }
1481  }
1482  else
1483  {
1484  if (strcmp (token, type) != 0)
1485  {
1486  continue;
1487  }
1488  }
1489 
1490  token = strtok_r (NULL, delim, &save_ptr);
1491  if (token != NULL && strcmp (token, server_name) == 0)
1492  {
1493  pclose (input);
1494  return true;
1495  }
1496  }
1497  pclose (input);
1498  return false;
1499 }
1500 
1501 /*
1502  * is_server_running -
1503  *
1504  * return:
1505  *
1506  * type(in):
1507  * server_name(in):
1508  * pid(in):
1509  */
1510 static bool
1511 is_server_running (const char *type, const char *server_name, int pid)
1512 {
1514  {
1515  return false;
1516  }
1517 
1518  if (pid <= 0)
1519  {
1520  return check_server (type, server_name);
1521  }
1522 
1523  while (true)
1524  {
1525  if (!is_terminated_process (pid))
1526  {
1527  if (check_server (type, server_name))
1528  {
1529  return true;
1530  }
1531  sleep (1);
1532 
1533  /* A child process is defunct because the SIGCHLD signal ignores. */
1534  /*
1535  * if (waitpid (pid, &status, WNOHANG) == -1) { perror ("waitpid"); } */
1536  }
1537  else
1538  {
1539  return false;
1540  }
1541  }
1542 }
1543 
1544 /*
1545  * process_server -
1546  *
1547  * return:
1548  *
1549  * command_type(in):
1550  * argc(in):
1551  * argv(in) :
1552  * show_usage(in):
1553  *
1554  * NOTE:
1555  */
1556 static int
1557 process_server (int command_type, int argc, char **argv, bool show_usage, bool check_ha_mode,
1558  bool process_window_service)
1559 {
1560  char buf[4096];
1561  char *list, *token, *save;
1562  const char *delim = " ,:";
1563  int status = NO_ERROR;
1564  int master_port = prm_get_master_port_id ();
1565 
1566  memset (buf, '\0', sizeof (buf));
1567 
1568  /* A string is copyed because strtok_r() modify an original string. */
1569  if (argc == 0)
1570  {
1571  if (us_Property_map[SERVER_START_LIST].property_value != NULL)
1572  {
1573  strncpy (buf, us_Property_map[SERVER_START_LIST].property_value, sizeof (buf) - 1);
1574  }
1575  }
1576  else
1577  {
1578  strncpy (buf, argv[0], sizeof (buf) - 1);
1579  }
1580 
1581  if (command_type != STATUS && strlen (buf) == 0)
1582  {
1583  if (show_usage)
1584  {
1587  }
1588  return ER_GENERIC_ERROR;
1589  }
1590 
1591  switch (command_type)
1592  {
1593  case START:
1594  if (process_window_service)
1595  {
1596 #if defined(WINDOWS)
1597  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_SERVER,
1598  COMMAND_TYPE_START, NULL, NULL
1599  };
1600 
1601  for (list = buf;; list = NULL)
1602  {
1603  token = strtok_r (list, delim, &save);
1604  if (token == NULL)
1605  {
1606  break;
1607  }
1608 
1609  args[3] = token;
1610 
1611  /* load parameters for [@database] section */
1612  if (check_ha_mode == true)
1613  {
1614  status = sysprm_load_and_init (token, NULL, SYSPRM_IGNORE_INTL_PARAMS);
1615  if (status != NO_ERROR)
1616  {
1617  print_result (PRINT_SERVER_NAME, status, command_type);
1618  break;
1619  }
1620 
1622  {
1623  status = ER_GENERIC_ERROR;
1625  print_result (PRINT_SERVER_NAME, status, command_type);
1627  continue;
1628  }
1629  }
1630  if (is_server_running (CHECK_SERVER, token, 0))
1631  {
1632  status = ER_GENERIC_ERROR;
1635  continue;
1636  }
1637  else
1638  {
1639  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1640 
1641  if (status == NO_ERROR && !is_server_running (CHECK_SERVER, token, 0))
1642  {
1643  status = ER_GENERIC_ERROR;
1644  }
1645  print_result (PRINT_SERVER_NAME, status, command_type);
1646  }
1647  }
1648 #endif
1649  }
1650  else
1651  {
1652  if (!css_does_master_exist (master_port))
1653  {
1654  status = process_master (command_type);
1655  if (status != NO_ERROR)
1656  {
1657  break; /* escape switch */
1658  }
1659  }
1660 
1661  for (list = buf;; list = NULL)
1662  {
1663  token = strtok_r (list, delim, &save);
1664  if (token == NULL)
1665  {
1666  break;
1667  }
1669 
1670 #if !defined(WINDOWS)
1671  if (check_ha_mode == true)
1672  {
1674  if (status != NO_ERROR)
1675  {
1677  print_result (PRINT_SERVER_NAME, status, command_type);
1678  break;
1679  }
1680 
1682  {
1683  status = ER_GENERIC_ERROR;
1685  print_result (PRINT_SERVER_NAME, status, command_type);
1687  continue;
1688  }
1689  }
1690 #endif /* !WINDOWS */
1691 
1692  if (is_server_running (CHECK_SERVER, token, 0))
1693  {
1694  status = ER_GENERIC_ERROR;
1697  continue;
1698  }
1699  else
1700  {
1701  int pid;
1702  const char *args[] = { UTIL_CUBRID_NAME, token, NULL };
1703  status = proc_execute (UTIL_CUBRID_NAME, args, false, false, false, &pid);
1704 
1705  if (status == NO_ERROR && !is_server_running (CHECK_SERVER, token, pid))
1706  {
1707  status = ER_GENERIC_ERROR;
1708  }
1709  print_result (PRINT_SERVER_NAME, status, command_type);
1710  }
1711  }
1712  }
1713  break;
1714  case STOP:
1715  for (list = buf;; list = NULL)
1716  {
1717  token = strtok_r (list, delim, &save);
1718  if (token == NULL)
1719  {
1720  break;
1721  }
1723 
1724  if (is_server_running (CHECK_SERVER, token, 0))
1725  {
1726  if (process_window_service)
1727  {
1728 #if defined(WINDOWS)
1729  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_SERVER,
1730  COMMAND_TYPE_STOP, NULL, NULL
1731  };
1732 
1733  args[3] = token;
1734  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1735 #endif
1736  }
1737  else
1738  {
1739  const char *args[] = { UTIL_COMMDB_NAME, COMMDB_SERVER_STOP, token, NULL };
1740 #if !defined(WINDOWS)
1741  if (check_ha_mode)
1742  {
1743  status = sysprm_load_and_init (token, NULL, SYSPRM_IGNORE_INTL_PARAMS);
1744  if (status != NO_ERROR)
1745  {
1746  print_result (PRINT_SERVER_NAME, status, command_type);
1747  break;
1748  }
1749 
1751  {
1752  status = ER_GENERIC_ERROR;
1755  print_result (PRINT_SERVER_NAME, status, command_type);
1756  continue;
1757  }
1758  }
1759 #endif /* !WINDOWS */
1760  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
1761  }
1762  print_result (PRINT_SERVER_NAME, status, command_type);
1763  }
1764  else
1765  {
1766  status = ER_GENERIC_ERROR;
1769  }
1770  }
1771  break;
1772  case RESTART:
1773  status = process_server (STOP, argc, argv, show_usage, check_ha_mode, process_window_service);
1774  status = process_server (START, argc, argv, show_usage, check_ha_mode, process_window_service);
1775  break;
1776  case STATUS:
1778  if (css_does_master_exist (master_port))
1779  {
1780  const char *args[] = { UTIL_COMMDB_NAME, COMMDB_SERVER_STATUS, NULL };
1781  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
1782  }
1783  else
1784  {
1786  }
1787  break;
1788  case ACCESS_CONTROL:
1789  {
1790  if (argc != 2)
1791  {
1792  status = ER_GENERIC_ERROR;
1793 
1794  if (show_usage)
1795  {
1798  }
1799  break;
1800  }
1801 
1802  if (strcasecmp (argv[0], "reload") == 0)
1803  {
1804  const char *args[] = { UTIL_ADMIN_NAME, UTIL_OPTION_ACLDB, ACLDB_RELOAD, argv[1],
1805  NULL
1806  };
1807 
1808  status = proc_execute (UTIL_ADMIN_NAME, args, true, false, false, NULL);
1809  print_result (PRINT_SERVER_NAME, status, command_type);
1810  }
1811  else if (strcasecmp (argv[0], "status") == 0)
1812  {
1813  const char *args[] = { UTIL_ADMIN_NAME, UTIL_OPTION_ACLDB, argv[1], NULL };
1814 
1815  status = proc_execute (UTIL_ADMIN_NAME, args, true, false, false, NULL);
1816  }
1817  else
1818  {
1819  status = ER_GENERIC_ERROR;
1820  if (show_usage)
1821  {
1824  }
1825 
1826  break;
1827  }
1828  }
1829 
1830  break;
1831  default:
1832  status = ER_GENERIC_ERROR;
1833  break;
1834  }
1835 
1836  return status;
1837 }
1838 
1839 /*
1840  * is_broker_running -
1841  *
1842  * return:
1843  *
1844  */
1845 static int
1847 {
1848  const char *args[] = { UTIL_MONITOR_NAME, 0 };
1849  int status = proc_execute (UTIL_MONITOR_NAME, args, true, true, false, NULL);
1850  return status;
1851 }
1852 
1853 /*
1854  * process_broker -
1855  *
1856  * return:
1857  *
1858  * command_type(in):
1859  * name(in):
1860  *
1861  */
1862 static int
1863 process_broker (int command_type, int argc, const char **argv, bool process_window_service)
1864 {
1865  int status = NO_ERROR;
1866 
1867  switch (command_type)
1868  {
1869  case START:
1871  switch (is_broker_running ())
1872  {
1873  case 0: /* no error */
1874  status = ER_GENERIC_ERROR;
1877  break;
1878  case 1: /* shm_open error */
1879  if (process_window_service)
1880  {
1881 #if defined(WINDOWS)
1882  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_BROKER,
1884  };
1885 
1886  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1887  status = (is_broker_running () == 0) ? NO_ERROR : ER_GENERIC_ERROR;
1888 #endif
1889  }
1890  else
1891  {
1892  const char *args[] = { UTIL_BROKER_NAME, COMMAND_TYPE_START, NULL };
1893  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
1894  status = (is_broker_running () == 0) ? NO_ERROR : ER_GENERIC_ERROR;
1895  }
1896  print_result (PRINT_BROKER_NAME, status, command_type);
1897 
1898  break;
1899  case 2: /* no conf file or parameter error */
1900  default:
1901  status = ER_GENERIC_ERROR;
1903  break;
1904  }
1905  break;
1906  case STOP:
1908  switch (is_broker_running ())
1909  {
1910  case 0: /* no error */
1911  if (process_window_service)
1912  {
1913 #if defined(WINDOWS)
1914  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_BROKER,
1916  };
1917 
1918  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1919  status = (is_broker_running () == 0) ? ER_GENERIC_ERROR : NO_ERROR;
1920 #endif
1921  }
1922  else
1923  {
1924  const char *args[] = { UTIL_BROKER_NAME, COMMAND_TYPE_STOP, NULL };
1925  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
1926  status = (is_broker_running () == 0) ? ER_GENERIC_ERROR : NO_ERROR;
1927  }
1928  print_result (PRINT_BROKER_NAME, status, command_type);
1929  break;
1930  case 1: /* shm_open error */
1931  status = ER_GENERIC_ERROR;
1934  break;
1935  case 2: /* no conf file or parameter error */
1936  default: /* other error */
1937  status = ER_GENERIC_ERROR;
1939  break;
1940  }
1941  break;
1942  case RESTART:
1943  status = process_broker (STOP, 0, NULL, process_window_service);
1944 #if defined (WINDOWS)
1945  Sleep (500);
1946 #endif
1947  status = process_broker (START, 0, NULL, process_window_service);
1948  break;
1949  case STATUS:
1950  {
1951  int i;
1952  const char **args;
1953 
1955  switch (is_broker_running ())
1956  {
1957  case 0: /* no error */
1958  args = (const char **) malloc (sizeof (char *) * (argc + 2));
1959  if (args == NULL)
1960  {
1961  status = ER_GENERIC_ERROR;
1963  break;
1964  }
1965 
1966  args[0] = PRINT_BROKER_NAME " " PRINT_CMD_STATUS;
1967  for (i = 0; i < argc; i++)
1968  {
1969  args[i + 1] = argv[i];
1970  }
1971  args[argc + 1] = NULL;
1972  status = proc_execute (UTIL_MONITOR_NAME, args, true, false, false, NULL);
1973 
1974  free (args);
1975  break;
1976 
1977  case 1: /* shm_open error */
1979  break;
1980 
1981  case 2: /* no conf file */
1982  default: /* other error */
1983  status = ER_GENERIC_ERROR;
1985  break;
1986  }
1987  }
1988  break;
1989  case ON:
1990  {
1991  if (process_window_service)
1992  {
1993 #if defined(WINDOWS)
1994  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_BROKER,
1995  COMMAND_TYPE_ON, argv[0], NULL
1996  };
1997 
1998  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
1999 #endif
2000  }
2001  else
2002  {
2003  const char *args[] = { UTIL_BROKER_NAME, COMMAND_TYPE_ON, argv[0], NULL };
2004  if (argc <= 0)
2005  {
2006  status = ER_GENERIC_ERROR;
2009  break;
2010  }
2011  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
2012  }
2013  }
2014  break;
2015  case OFF:
2016  {
2017  if (process_window_service)
2018  {
2019 #if defined(WINDOWS)
2020  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_BROKER,
2021  COMMAND_TYPE_OFF, argv[0], NULL
2022  };
2023 
2024  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
2025 #endif
2026  }
2027  else
2028  {
2029  const char *args[] = { UTIL_BROKER_NAME, COMMAND_TYPE_OFF, argv[0], NULL };
2030  if (argc <= 0)
2031  {
2032  status = ER_GENERIC_ERROR;
2035  break;
2036  }
2037  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
2038  }
2039  }
2040  break;
2041  case ACCESS_CONTROL:
2042  {
2043  const char *args[5];
2044 
2045  args[0] = UTIL_BROKER_NAME;
2046  args[1] = COMMAND_TYPE_ACL;
2047  args[2] = argv[0];
2048 
2049  if (argc == 1)
2050  {
2051  args[3] = NULL;
2052  }
2053  else if (argc == 2)
2054  {
2055  args[3] = argv[1];
2056  args[4] = NULL;
2057  }
2058  else
2059  {
2060  status = ER_GENERIC_ERROR;
2063  break;
2064  }
2065  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
2066  print_result (PRINT_BROKER_NAME, status, command_type);
2067  break;
2068  }
2069  case RESET:
2070  {
2071  if (process_window_service)
2072  {
2073 #if defined(WINDOWS)
2074  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_BROKER,
2075  COMMAND_TYPE_RESET, argv[0], NULL
2076  };
2077 
2078  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
2079 #endif
2080  }
2081  else
2082  {
2083  const char *args[] = { UTIL_BROKER_NAME, COMMAND_TYPE_RESET, argv[0], NULL };
2084  if (argc <= 0)
2085  {
2086  status = ER_GENERIC_ERROR;
2089  break;
2090  }
2091  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
2092  }
2093  }
2094  break;
2095 
2096  case INFO:
2097  {
2098  const char *args[] = { UTIL_BROKER_NAME, COMMAND_TYPE_INFO, NULL };
2099 
2100  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
2101  }
2102  break;
2103 
2104  case GET_SHARID:
2105  {
2106  int i;
2107  const char **args;
2109 
2110  if (is_broker_running ())
2111  {
2114  status = ER_GENERIC_ERROR;
2115  break;
2116  }
2117 
2118  args = (const char **) malloc (sizeof (char *) * (argc + 3));
2119  if (args == NULL)
2120  {
2121  status = ER_GENERIC_ERROR;
2123  break;
2124  }
2125 
2126  args[0] = UTIL_BROKER_NAME;
2127  args[1] = PRINT_CMD_GETID;
2128 
2129  for (i = 0; i < argc; i++)
2130  {
2131  args[i + 2] = argv[i];
2132  }
2133  args[argc + 2] = NULL;
2134 
2135  status = proc_execute (UTIL_BROKER_NAME, args, true, false, false, NULL);
2136 
2137  free (args);
2138  }
2139  break;
2140 
2141  case TEST:
2142  {
2143  int i;
2144  const char **args;
2145 
2147  switch (is_broker_running ())
2148  {
2149  case 0: /* no error */
2150  args = (const char **) malloc (sizeof (char *) * (argc + 2));
2151  if (args == NULL)
2152  {
2153  status = ER_GENERIC_ERROR;
2155  break;
2156  }
2157 
2158  args[0] = PRINT_BROKER_NAME " " PRINT_CMD_TEST;
2159  for (i = 0; i < argc; i++)
2160  {
2161  args[i + 1] = argv[i];
2162  }
2163  args[argc + 1] = NULL;
2164  status = proc_execute (UTIL_TESTER_NAME, args, true, false, false, NULL);
2165 
2166  free (args);
2167  break;
2168 
2169  case 1: /* shm_open error */
2171  break;
2172 
2173  case 2: /* no conf file */
2174  default: /* other error */
2175  status = ER_GENERIC_ERROR;
2177  break;
2178  }
2179  }
2180  break;
2181 
2182  default:
2183  status = ER_GENERIC_ERROR;
2184  break;
2185  }
2186 
2187  return status;
2188 }
2189 
2191 is_manager_running (unsigned int sleep_time)
2192 {
2193  FILE *input;
2194  char buf[16], cmd[PATH_MAX];
2195  struct stat stbuf;
2196 
2197  sleep (sleep_time);
2198 
2199  /* check cub_manager */
2200  (void) envvar_bindir_file (cmd, PATH_MAX, UTIL_CUB_MANAGER_NAME);
2201  if (stat (cmd, &stbuf) == -1)
2202  {
2203  /* Not install manager server */
2205  }
2206 
2207  strcat (cmd, " getpid");
2208  input = popen (cmd, "r");
2209  if (input == NULL)
2210  {
2211  /* Failed to run cub_manager process */
2213  }
2214 
2215  memset (buf, '\0', sizeof (buf));
2216  if (fgets (buf, 16, input) == NULL)
2217  {
2218  pclose (input);
2219  return MANAGER_SERVER_STOPPED;
2220  }
2221  if (atoi (buf) > 0)
2222  {
2223  pclose (input);
2224  return MANAGER_SERVER_RUNNING;
2225  }
2226  else
2227  {
2228  pclose (input);
2230  }
2231 }
2232 
2233 /*
2234  * process_manager -
2235  *
2236  * return:
2237  *
2238  * command_type(in):
2239  * cms_port(in):
2240  *
2241  */
2242 static int
2243 process_manager (int command_type, bool process_window_service)
2244 {
2245  int cub_manager, status = NO_ERROR;
2246  char cub_manager_path[PATH_MAX];
2247  UTIL_MANAGER_SERVER_STATUS_E manager_status;
2248  struct stat stbuf;
2249 
2250  cub_manager_path[0] = '\0';
2251 
2253 
2254  (void) envvar_bindir_file (cub_manager_path, PATH_MAX, UTIL_CUB_MANAGER_NAME);
2255  if (stat (cub_manager_path, &stbuf) == -1)
2256  {
2259  return ER_GENERIC_ERROR;
2260  }
2261  manager_status = is_manager_running (0);
2262  if (manager_status == MANAGER_SERVER_STATUS_ERROR)
2263  {
2264  status = ER_GENERIC_ERROR;
2265  print_result (PRINT_MANAGER_NAME, status, command_type);
2266  return status;
2267  }
2268 
2269  switch (command_type)
2270  {
2271  case START:
2272  if (manager_status == MANAGER_SERVER_STOPPED)
2273  {
2274  if (process_window_service)
2275  {
2276 #if defined(WINDOWS)
2277  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_MANAGER,
2279  };
2280 
2281  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
2282 #endif
2283  }
2284  else
2285  {
2286  const char *args[] = { UTIL_CUB_MANAGER_NAME, COMMAND_TYPE_START, NULL };
2287  cub_manager = proc_execute (UTIL_CUB_MANAGER_NAME, args, false, false, false, NULL);
2288  }
2289 
2291  print_result (PRINT_MANAGER_NAME, status, command_type);
2292  }
2293  else
2294  {
2297  status = ER_GENERIC_ERROR;
2298  }
2299  break;
2300  case STOP:
2301  if (manager_status == MANAGER_SERVER_RUNNING)
2302  {
2303  if (process_window_service)
2304  {
2305 #if defined(WINDOWS)
2306  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_MANAGER,
2308  };
2309 
2310  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
2311 #endif
2312  }
2313  else
2314  {
2315  const char *args[] = { UTIL_CUB_MANAGER_NAME, COMMAND_TYPE_STOP, NULL };
2316  status = proc_execute (UTIL_CUB_MANAGER_NAME, args, true, false, false, NULL);
2317  }
2319  print_result (PRINT_MANAGER_NAME, status, command_type);
2320  }
2321  else
2322  {
2325  status = ER_GENERIC_ERROR;
2326  }
2327  break;
2328  case STATUS:
2329  {
2330  switch (manager_status)
2331  {
2334  break;
2336  default:
2338  break;
2339  }
2340  }
2341  break;
2342  default:
2343  status = ER_GENERIC_ERROR;
2344  break;
2345  }
2346 
2347  return status;
2348 }
2349 
2350 /*
2351  * is_javasp_running -
2352  *
2353  * return:
2354  *
2355  */
2356 
2358 is_javasp_running (const char *server_name)
2359 {
2360  static const char PING_CMD[] = " ping ";
2361  static const int PING_CMD_LEN = sizeof (PING_CMD);
2362 
2363  FILE *input = NULL;
2364  char buf[PATH_MAX] = { 0 };
2365  char cmd[PATH_MAX + PING_CMD_LEN] = { 0 };
2366 
2367  (void) envvar_bindir_file (cmd, PATH_MAX, UTIL_JAVASP_NAME);
2368 
2369  strcat (cmd, PING_CMD);
2370  strcat (cmd, server_name);
2371 
2372  input = popen (cmd, "r");
2373  if (input == NULL)
2374  {
2376  }
2377 
2378  memset (buf, '\0', sizeof (buf));
2379  if (fgets (buf, PATH_MAX, input) == NULL)
2380  {
2381  pclose (input);
2383  }
2384 
2385  if (strcmp (buf, server_name) == 0)
2386  {
2387  pclose (input);
2388  return JAVASP_SERVER_RUNNING;
2389  }
2390  else if (strcmp (buf, "NO_CONNECTION") == 0)
2391  {
2392  pclose (input);
2393  return JAVASP_SERVER_STOPPED;
2394  }
2395  else
2396  {
2397  pclose (input);
2399  }
2400 }
2401 
2402 static int
2403 process_javasp_start (const char *db_name, bool process_window_service)
2404 {
2405  static const int wait_timeout = 30;
2406  int waited_secs = 0;
2407  int pid = 0;
2408  int status = ER_GENERIC_ERROR;
2409 
2411 
2412  UTIL_JAVASP_SERVER_STATUS_E javasp_status = is_javasp_running (db_name);
2413  if (javasp_status == JAVASP_SERVER_RUNNING)
2414  {
2417  }
2418  else
2419  {
2420  if (process_window_service)
2421  {
2422 #if defined(WINDOWS)
2423  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_JAVASP,
2425  };
2426  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
2427 #endif
2428  }
2429  else
2430  {
2431  const char *args[] = { UTIL_JAVASP_NAME, db_name, NULL };
2432  status = proc_execute (UTIL_JAVASP_NAME, args, false, false, false, &pid);
2433  }
2434 
2435  status = ER_GENERIC_ERROR;
2436  while (status != NO_ERROR && waited_secs < wait_timeout)
2437  {
2438  if (pid != 0 && is_terminated_process (pid))
2439  {
2440  break;
2441  }
2442 
2443  javasp_status = is_javasp_running (db_name);
2444  if (javasp_status == JAVASP_SERVER_RUNNING)
2445  {
2446  status = NO_ERROR;
2447  break;
2448  }
2449  else
2450  {
2451  sleep (1); /* wait to start */
2452  waited_secs++;
2453 
2454  if (javasp_status == JAVASP_SERVER_STOPPED)
2455  {
2456  util_log_write_errstr ("Waiting for javasp server to start... (%d / %d)\n", waited_secs,
2457  wait_timeout);
2458  }
2459  else
2460  {
2461  if (waited_secs > 3)
2462  {
2463  /* invalid database name or failed to open info file, wait upto 3 seconds */
2464  break;
2465  }
2466  }
2467  }
2468  }
2469  }
2470 
2472  return status;
2473 }
2474 
2475 static int
2476 process_javasp_stop (const char *db_name, bool process_window_service)
2477 {
2478  int status = NO_ERROR;
2479  static const int wait_timeout = 5;
2480  int waited_secs = 0;
2481 
2483  UTIL_JAVASP_SERVER_STATUS_E javasp_status = is_javasp_running (db_name);
2484  if (javasp_status == JAVASP_SERVER_RUNNING)
2485  {
2486  if (process_window_service)
2487  {
2488 #if defined(WINDOWS)
2489  const char *args[] = { UTIL_WIN_SERVICE_CONTROLLER_NAME, PRINT_CMD_JAVASP,
2491  };
2492 
2493  status = proc_execute (UTIL_WIN_SERVICE_CONTROLLER_NAME, args, true, false, false, NULL);
2494 #endif
2495  }
2496  else
2497  {
2498  const char *args[] = { UTIL_JAVASP_NAME, COMMAND_TYPE_STOP, db_name, NULL };
2499  status = proc_execute (UTIL_JAVASP_NAME, args, true, false, false, NULL);
2500  do
2501  {
2503  sleep (1); /* wait to stop */
2504  waited_secs++;
2505  }
2506  while (status != NO_ERROR && waited_secs < wait_timeout);
2507  }
2508  }
2509  else
2510  {
2511  status = ER_GENERIC_ERROR;
2514  }
2515 
2516  print_result (PRINT_JAVASP_NAME, status, STOP);
2517  return status;
2518 }
2519 
2520 static int
2522 {
2523  int status = NO_ERROR;
2525 
2526  UTIL_JAVASP_SERVER_STATUS_E javasp_status = is_javasp_running (db_name);
2527  if (javasp_status == JAVASP_SERVER_RUNNING)
2528  {
2529  const char *args[] = { UTIL_JAVASP_NAME, COMMAND_TYPE_STATUS, db_name, NULL };
2530  status = proc_execute (UTIL_JAVASP_NAME, args, true, false, false, NULL);
2531  }
2532  else
2533  {
2534  status = ER_GENERIC_ERROR;
2537  }
2538 
2539  return status;
2540 }
2541 
2542 static int
2543 process_javasp (int command_type, int argc, const char **argv, bool process_window_service)
2544 {
2545  char buf[4096] = { 0 };
2546  char *list = NULL, *save = NULL;
2547  const char *delim = " ,:";
2548  int status = NO_ERROR;
2549  char *db_name = NULL;
2550 
2551  if (argc == 0) /* cubrid service command */
2552  {
2553  if (us_Property_map[SERVER_START_LIST].property_value != NULL)
2554  {
2555  strncpy (buf, us_Property_map[SERVER_START_LIST].property_value, sizeof (buf) - 1);
2556  }
2557  }
2558  else /* cubrid javasp command */
2559  {
2560  strncpy (buf, argv[0], sizeof (buf) - 1);
2561  }
2562 
2563  if (strlen (buf) == 0)
2564  {
2567  status = ER_GENERIC_ERROR;
2568  goto exit;
2569  }
2570 
2571  for (list = buf;; list = NULL)
2572  {
2573  db_name = strtok_r (list, delim, &save);
2574  if (db_name == NULL)
2575  {
2576  break;
2577  }
2578  switch (command_type)
2579  {
2580  case START:
2581  status = process_javasp_start (db_name, process_window_service);
2582  break;
2583  case STOP:
2584  status = process_javasp_stop (db_name, process_window_service);
2585  break;
2586  case RESTART:
2587  status = process_javasp_stop (db_name, process_window_service);
2588  status = process_javasp_start (db_name, process_window_service);
2589  break;
2590  case STATUS:
2591  status = process_javasp_status (db_name);
2592  break;
2593  default:
2594  status = ER_GENERIC_ERROR;
2595  break;
2596  }
2597  }
2598 
2599 exit:
2600  return status;
2601 }
2602 
2603 static bool
2604 ha_make_log_path (char *path, int size, char *base, char *db, char *node)
2605 {
2606  return snprintf (path, size, "%s/%s_%s", base, db, node) >= 0;
2607 }
2608 
2609 static bool
2610 ha_concat_db_and_host (char *db_host, int size, const char *db, const char *host)
2611 {
2612  return snprintf (db_host, size, "%s@%s", db, host) >= 0;
2613 }
2614 
2615 #if defined(WINDOWS)
2616 static bool
2617 ha_mkdir (const char *path, int dummy)
2618 {
2619  return false;
2620 }
2621 #else
2622 static bool
2623 ha_mkdir (const char *path, mode_t mode)
2624 {
2625  char dir[PATH_MAX];
2626  struct stat statbuf;
2627 
2628  if (stat (path, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
2629  {
2630  return true;
2631  }
2632 
2633  cub_dirname_r (path, dir, PATH_MAX);
2634  if (stat (dir, &statbuf) == -1)
2635  {
2636  if (errno == ENOENT && ha_mkdir (dir, mode))
2637  {
2638  return mkdir (path, mode) == 0;
2639  }
2640  else
2641  {
2642  return false;
2643  }
2644  }
2645  else if (S_ISDIR (statbuf.st_mode))
2646  {
2647  return mkdir (path, mode) == 0;
2648  }
2649 
2650  return false;
2651 }
2652 #endif /* WINDOWS */
2653 
2654 static bool
2655 ha_make_mem_size (char *mem_size, int size, int value)
2656 {
2657  return snprintf (mem_size, size, "--max-mem-size=%d", value) >= 0;
2658 }
2659 
2660 static bool
2661 ha_is_registered (const char *args, const char *hostname)
2662 {
2663  int status;
2664  const char *commdb_argv[] = {
2665  UTIL_COMMDB_NAME, /* argv[0] */
2666  COMMDB_IS_REG, /* argv[1] */
2667  args, /* argv[2] : process args */
2668  NULL, NULL, /* argv[3~4] : -h hostname */
2669  NULL /* argv[5] */
2670  };
2671 
2672  if (args == NULL)
2673  {
2674  return false;
2675  }
2676 
2677  if (hostname)
2678  {
2679  commdb_argv[3] = COMMDB_HOST; /* -h */
2680  commdb_argv[4] = hostname; /* hostname */
2681  commdb_argv[5] = NULL;
2682  }
2683 
2684  status = proc_execute (UTIL_COMMDB_NAME, commdb_argv, true, false, false, NULL);
2685  if (status == NO_ERROR)
2686  {
2687  return true;
2688  }
2689 
2690  return false;
2691 }
2692 
2693 static int
2694 ha_argv_to_args (char *args, int size, const char **argv, HB_PROC_TYPE type)
2695 {
2696  int status = NO_ERROR;
2697  int ret = 0;
2698 
2699  if (type == HB_PTYPE_COPYLOGDB)
2700  {
2701  ret = snprintf (args, size, "%s %s %s %s %s %s %s ", /* copylogdb */
2702  argv[0], /* UTIL_ADMIN_NAME : cub_admin */
2703  argv[1], /* UTIL_COPYLOGDB : copylogdb */
2704  argv[2], /* -L, --log-path=PATH */
2705  argv[3], /* PATH */
2706  argv[4], /* -m, --mode=MODE */
2707  argv[5], /* MODE */
2708  argv[6]); /* database-name */
2709  }
2710  else if (type == HB_PTYPE_APPLYLOGDB)
2711  {
2712  ret = snprintf (args, size, "%s %s %s %s %s %s ", /* applylogdb */
2713  argv[0], /* UTIL_ADMIN_NAME : cub_admin */
2714  argv[1], /* UTIL_APPLYLOGDB : applylogdb */
2715  argv[2], /* -L, --log-path=PATH */
2716  argv[3], /* PATH */
2717  argv[4], /* --max-mem-size=SIZE */
2718  argv[5]); /* database-name */
2719  }
2720  else
2721  {
2722  assert (false);
2723  status = ER_GENERIC_ERROR;
2724  }
2725  if (ret < 0)
2726  {
2727  assert (false);
2728  status = ER_GENERIC_ERROR;
2729  }
2730 
2731  return status;
2732 }
2733 
2734 #if !defined(WINDOWS)
2735 static int
2736 us_hb_copylogdb_start (dynamic_array * out_ap, HA_CONF * ha_conf, const char *db_name, const char *node_name,
2737  const char *remote_host)
2738 {
2739  int status = NO_ERROR;
2740  int pid;
2741  int i, j, num_nodes;
2742  int num_db_found = 0, num_node_found = 0;
2743  HA_NODE_CONF *nc;
2744  char db_host[PATH_MAX], log_path[PATH_MAX];
2745  char **dbs;
2746 
2747  num_nodes = ha_conf->num_node_conf;
2748  dbs = ha_conf->db_names;
2749  nc = ha_conf->node_conf;
2750 
2752 
2753  for (i = 0; dbs[i] != NULL; i++)
2754  {
2755  if (db_name != NULL && strcmp (dbs[i], db_name) != 0)
2756  {
2757  continue;
2758  }
2759  num_db_found++;
2760 
2763  if (status != NO_ERROR)
2764  {
2765  goto ret;
2766  }
2767 
2769  {
2770  continue;
2771  }
2772 
2773  for (j = 0; j < num_nodes; j++)
2774  {
2775  if (node_name != NULL && strcmp (nc[j].node_name, node_name) != 0)
2776  {
2777  continue;
2778  }
2779 
2780  if (remote_host == NULL)
2781  {
2782  if (util_is_localhost (nc[j].node_name))
2783  {
2784  continue;
2785  }
2786  }
2787  else
2788  {
2789  if (node_name != NULL && strcmp (node_name, remote_host) == 0)
2790  {
2791  continue;
2792  }
2793  }
2794  num_node_found++;
2795 
2796  db_host[0] = log_path[0] = '\0';
2797  (void) ha_concat_db_and_host (db_host, PATH_MAX, dbs[i], nc[j].node_name);
2798  (void) ha_make_log_path (log_path, PATH_MAX, nc[j].copy_log_base, dbs[i], nc[j].node_name);
2799  if (db_host[0] != '\0' && log_path[0] != '\0')
2800  {
2801  char lw_args[HB_MAX_SZ_PROC_ARGS];
2802  const char *lw_argv[] = { UTIL_ADMIN_NAME, UTIL_COPYLOGDB,
2803  "-L", log_path,
2804  "-m", nc[j].copy_sync_mode,
2805  db_host,
2806  NULL
2807  };
2808 
2809  status = ha_argv_to_args (lw_args, sizeof (lw_args), lw_argv, HB_PTYPE_COPYLOGDB);
2810  if (status != NO_ERROR)
2811  {
2812  goto ret;
2813  }
2814 
2815  if (remote_host == NULL)
2816  {
2817  if (ha_is_registered (lw_args, NULL))
2818  {
2819  continue;
2820  }
2821 
2822  if (ha_mkdir (log_path, 0755))
2823  {
2824  status = proc_execute (UTIL_ADMIN_NAME, lw_argv, false, false, false, &pid);
2825 
2826  if (status != NO_ERROR)
2827  {
2828  goto ret;
2829  }
2830 
2831  if (out_ap && da_add (out_ap, &pid) != NO_ERROR)
2832  {
2833  status = ER_GENERIC_ERROR;
2834  goto ret;
2835  }
2836  }
2837  else
2838  {
2839  status = ER_GENERIC_ERROR;
2840  goto ret;
2841  }
2842  }
2843  else
2844  {
2845  const char *commdb_argv[] = {
2848  COMMDB_HOST, remote_host,
2849  NULL,
2850  };
2851  status = proc_execute (UTIL_COMMDB_NAME, commdb_argv, true, false, false, &pid);
2852  if (status != NO_ERROR)
2853  {
2854  goto ret;
2855  }
2856 
2857  if (out_ap && (da_add (out_ap, lw_args) != NO_ERROR))
2858  {
2859  status = ER_GENERIC_ERROR;
2860  goto ret;
2861  }
2862  }
2863  }
2864  }
2865  }
2866 
2867  if (db_name != NULL && num_db_found == 0)
2868  {
2871  status = ER_GENERIC_ERROR;
2872  goto ret;
2873  }
2874 
2875  if (node_name != NULL && num_node_found == 0)
2876  {
2879  status = ER_GENERIC_ERROR;
2880  }
2881 
2882 ret:
2883  print_result (UTIL_COPYLOGDB, status, START);
2884  return status;
2885 }
2886 
2887 static int
2888 us_hb_copylogdb_stop (HA_CONF * ha_conf, const char *db_name, const char *node_name, const char *remote_host)
2889 {
2890  int status = NO_ERROR;
2891  int i, j, num_nodes;
2892  int num_db_found = 0, num_node_found = 0;
2893  HA_NODE_CONF *nc;
2894  char db_host[PATH_MAX], log_path[PATH_MAX];
2895  char **dbs;
2896  int wait_time = 0;
2897 
2898  num_nodes = ha_conf->num_node_conf;
2899  dbs = ha_conf->db_names;
2900  nc = ha_conf->node_conf;
2901 
2903 
2904  for (i = 0; dbs[i] != NULL; i++)
2905  {
2906  if (db_name != NULL && strcmp (dbs[i], db_name) != 0)
2907  {
2908  continue;
2909  }
2910  num_db_found++;
2911 
2912  for (j = 0; j < num_nodes; j++)
2913  {
2914  if (node_name != NULL && strcmp (nc[j].node_name, node_name) != 0)
2915  {
2916  continue;
2917  }
2918 
2919  if (remote_host == NULL)
2920  {
2921  if (util_is_localhost (nc[j].node_name))
2922  {
2923  continue;
2924  }
2925  }
2926  else
2927  {
2928  if (node_name != NULL && strcmp (node_name, remote_host) == 0)
2929  {
2930  continue;
2931  }
2932  }
2933  num_node_found++;
2934 
2935  db_host[0] = log_path[0] = '\0';
2936  (void) ha_concat_db_and_host (db_host, PATH_MAX, dbs[i], nc[j].node_name);
2937  (void) ha_make_log_path (log_path, PATH_MAX, nc[j].copy_log_base, dbs[i], nc[j].node_name);
2938  if (db_host[0] != '\0' && log_path[0] != '\0')
2939  {
2940  char lw_args[HB_MAX_SZ_PROC_ARGS];
2941  const char *lw_argv[] = {
2943  "-L", log_path,
2944  "-m", nc[j].copy_sync_mode, db_host, NULL
2945  };
2946 
2947  status = ha_argv_to_args (lw_args, sizeof (lw_args), lw_argv, HB_PTYPE_COPYLOGDB);
2948  if (status != NO_ERROR)
2949  {
2950  goto ret;
2951  }
2952 
2953  if (remote_host != NULL || ha_is_registered (lw_args, remote_host))
2954  {
2955  const char *commdb_argv[HB_MAX_SZ_PROC_ARGS] = {
2957  NULL, NULL, /* -h hostname */
2958  NULL
2959  };
2960 
2961  if (remote_host != NULL)
2962  {
2963  commdb_argv[3] = COMMDB_HOST; /* -h */
2964  commdb_argv[4] = remote_host; /* hostname */
2965  commdb_argv[5] = NULL;
2966  }
2967 
2968  status = proc_execute (UTIL_COMMDB_NAME, commdb_argv, true, false, false, NULL);
2969  if (remote_host != NULL && status != NO_ERROR)
2970  {
2971  goto ret;
2972  }
2973 
2974  wait_time = 0;
2975  do
2976  {
2977  if (ha_is_registered (lw_args, remote_host) == false)
2978  {
2979  status = NO_ERROR;
2980  break;
2981  }
2982 
2983  sleep (3);
2984  wait_time += 3;
2985  }
2986  while (wait_time < US_HB_DEREG_WAIT_TIME_IN_SEC);
2987  }
2988  }
2989  else
2990  {
2991  status = ER_GENERIC_ERROR;
2992  goto ret;
2993  }
2994  }
2995  }
2996 
2997  if (db_name != NULL && num_db_found == 0)
2998  {
3001  status = ER_GENERIC_ERROR;
3002  goto ret;
3003  }
3004 
3005  if (node_name != NULL && num_node_found == 0)
3006  {
3009  status = ER_GENERIC_ERROR;
3010  }
3011 
3012 ret:
3013  print_result (UTIL_COPYLOGDB, status, STOP);
3014  return status;
3015 }
3016 
3017 static int
3018 us_hb_applylogdb_start (dynamic_array * out_ap, HA_CONF * ha_conf, const char *db_name, const char *node_name,
3019  const char *remote_host)
3020 {
3021  int status = NO_ERROR;
3022  int pid;
3023  int i, j, num_nodes;
3024  int num_db_found = 0, num_node_found = 0;
3025  HA_NODE_CONF *nc;
3026  char log_path[PATH_MAX], db_host[PATH_MAX], mem_size[PATH_MAX];
3027  char **dbs;
3028 
3029  num_nodes = ha_conf->num_node_conf;
3030  dbs = ha_conf->db_names;
3031  nc = ha_conf->node_conf;
3032 
3034 
3035  for (i = 0; dbs[i] != NULL; i++)
3036  {
3037  if (db_name != NULL && strcmp (dbs[i], db_name) != 0)
3038  {
3039  continue;
3040  }
3041  num_db_found++;
3042 
3045  if (status != NO_ERROR)
3046  {
3047  goto ret;
3048  }
3049 
3051  {
3052  continue;
3053  }
3054 
3055  for (j = 0; j < num_nodes; j++)
3056  {
3057  if (node_name != NULL && strcmp (nc[j].node_name, node_name) != 0)
3058  {
3059  continue;
3060  }
3061 
3062  if (remote_host == NULL)
3063  {
3064  if (util_is_localhost (nc[j].node_name))
3065  {
3066  continue;
3067  }
3068  }
3069  else
3070  {
3071  if (node_name != NULL && strcmp (node_name, remote_host) == 0)
3072  {
3073  continue;
3074  }
3075  }
3076 
3077  num_node_found++;
3078 
3079  log_path[0] = db_host[0] = mem_size[0] = '\0';
3080  (void) ha_concat_db_and_host (db_host, PATH_MAX, dbs[i], "localhost");
3081  (void) ha_make_log_path (log_path, PATH_MAX, nc[j].copy_log_base, dbs[i], nc[j].node_name);
3082  (void) ha_make_mem_size (mem_size, PATH_MAX, nc[j].apply_max_mem_size);
3083 
3084  if (log_path[0] != '\0' && db_host[0] != '\0' && mem_size[0] != '\0')
3085  {
3086  char la_args[HB_MAX_SZ_PROC_ARGS];
3087  const char *la_argv[] = {
3089  "-L", log_path, mem_size, db_host, NULL
3090  };
3091 
3092  status = ha_argv_to_args (la_args, sizeof (la_args), la_argv, HB_PTYPE_APPLYLOGDB);
3093  if (status != NO_ERROR)
3094  {
3095  goto ret;
3096  }
3097 
3098  if (remote_host == NULL)
3099  {
3100  if (ha_is_registered (la_args, NULL))
3101  {
3102  continue;
3103  }
3104 
3105  status = proc_execute (UTIL_ADMIN_NAME, la_argv, false, false, false, &pid);
3106  if (status != NO_ERROR)
3107  {
3108  goto ret;
3109  }
3110 
3111  if (out_ap && da_add (out_ap, &pid) != NO_ERROR)
3112  {
3113  status = ER_GENERIC_ERROR;
3114  goto ret;
3115  }
3116  }
3117  else
3118  {
3119  const char *commdb_argv[] = {
3122  COMMDB_HOST, remote_host,
3123  NULL,
3124  };
3125  status = proc_execute (UTIL_COMMDB_NAME, commdb_argv, true, false, false, &pid);
3126  if (status != NO_ERROR)
3127  {
3128  goto ret;
3129  }
3130 
3131  if (out_ap && (da_add (out_ap, la_args) != NO_ERROR))
3132  {
3133  status = ER_GENERIC_ERROR;
3134  goto ret;
3135  }
3136  }
3137  }
3138  else
3139  {
3140  status = ER_GENERIC_ERROR;
3141  goto ret;
3142  }
3143  }
3144  }
3145 
3146  if (db_name != NULL && num_db_found == 0)
3147  {
3150  status = ER_GENERIC_ERROR;
3151  goto ret;
3152  }
3153 
3154  if (node_name != NULL && num_node_found == 0)
3155  {
3158  status = ER_GENERIC_ERROR;
3159  }
3160 
3161 ret:
3162  print_result (UTIL_APPLYLOGDB, status, START);
3163  return status;
3164 }
3165 
3166 static int
3167 us_hb_applylogdb_stop (HA_CONF * ha_conf, const char *db_name, const char *node_name, const char *remote_host)
3168 {
3169  int status = NO_ERROR;
3170  int i, j, num_nodes;
3171  int num_db_found = 0, num_node_found = 0;
3172  HA_NODE_CONF *nc;
3173  char log_path[PATH_MAX], db_host[PATH_MAX], mem_size[PATH_MAX];
3174  char **dbs;
3175  int wait_time = 0;
3176 
3177  num_nodes = ha_conf->num_node_conf;
3178  dbs = ha_conf->db_names;
3179  nc = ha_conf->node_conf;
3180 
3182 
3183  for (i = 0; dbs[i] != NULL; i++)
3184  {
3185  if (db_name != NULL && strcmp (dbs[i], db_name) != 0)
3186  {
3187  continue;
3188  }
3189  num_db_found++;
3190 
3191  for (j = 0; j < num_nodes; j++)
3192  {
3193  if (node_name != NULL && strcmp (nc[j].node_name, node_name) != 0)
3194  {
3195  continue;
3196  }
3197 
3198  if (remote_host == NULL)
3199  {
3200  if (util_is_localhost (nc[j].node_name))
3201  {
3202  continue;
3203  }
3204  }
3205  else
3206  {
3207  if (node_name != NULL && strcmp (node_name, remote_host) == 0)
3208  {
3209  continue;
3210  }
3211  }
3212  num_node_found++;
3213 
3214  log_path[0] = db_host[0] = mem_size[0] = '\0';
3215  (void) ha_concat_db_and_host (db_host, PATH_MAX, dbs[i], "localhost");
3216  (void) ha_make_log_path (log_path, PATH_MAX, nc[j].copy_log_base, dbs[i], nc[j].node_name);
3217  (void) ha_make_mem_size (mem_size, PATH_MAX, nc[j].apply_max_mem_size);
3218 
3219  if (log_path[0] != '\0' && db_host[0] != '\0' && mem_size[0] != '\0')
3220  {
3221  char la_args[HB_MAX_SZ_PROC_ARGS];
3222  const char *la_argv[] = {
3224  "-L", log_path, mem_size, db_host, NULL
3225  };
3226 
3227  status = ha_argv_to_args (la_args, sizeof (la_args), la_argv, HB_PTYPE_APPLYLOGDB);
3228  if (status != NO_ERROR)
3229  {
3230  goto ret;
3231  }
3232 
3233  if (remote_host != NULL || ha_is_registered (la_args, remote_host))
3234  {
3235  const char *commdb_argv[HB_MAX_SZ_PROC_ARGS] = {
3237  NULL, NULL, /* -h hostname */
3238  NULL
3239  };
3240 
3241  if (remote_host != NULL)
3242  {
3243  commdb_argv[3] = COMMDB_HOST; /* -h */
3244  commdb_argv[4] = remote_host; /* hostname */
3245  commdb_argv[5] = NULL;
3246  }
3247 
3248  status = proc_execute (UTIL_COMMDB_NAME, commdb_argv, true, false, false, NULL);
3249  if (remote_host != NULL && status != NO_ERROR)
3250  {
3251  goto ret;
3252  }
3253 
3254  wait_time = 0;
3255  do
3256  {
3257  if (ha_is_registered (la_args, remote_host) == false)
3258  {
3259  status = NO_ERROR;
3260  break;
3261  }
3262 
3263  sleep (3);
3264  wait_time += 3;
3265  }
3266  while (wait_time < US_HB_DEREG_WAIT_TIME_IN_SEC);
3267  }
3268  }
3269  else
3270  {
3271  status = ER_GENERIC_ERROR;
3272  goto ret;
3273  }
3274  }
3275  }
3276 
3277  if (db_name != NULL && num_db_found == 0)
3278  {
3281  status = ER_GENERIC_ERROR;
3282  goto ret;
3283  }
3284 
3285  if (node_name != NULL && num_node_found == 0)
3286  {
3289  status = ER_GENERIC_ERROR;
3290  }
3291 
3292 ret:
3293  print_result (UTIL_APPLYLOGDB, status, STOP);
3294  return status;
3295 }
3296 
3297 #if defined (ENABLE_UNUSED_FUNCTION)
3298 static int
3299 us_hb_utils_start (dynamic_array * pids, HA_CONF * ha_conf, const char *db_name, const char *node_name)
3300 {
3301  int status = NO_ERROR;
3302 
3303  status = us_hb_copylogdb_start (pids, ha_conf, db_name, node_name, NULL);
3304  if (status != NO_ERROR)
3305  {
3306  return status;
3307  }
3308 
3309  status = us_hb_applylogdb_start (pids, ha_conf, db_name, node_name, NULL);
3310  return status;
3311 }
3312 
3313 static int
3314 us_hb_utils_stop (HA_CONF * ha_conf, const char *db_name, const char *node_name)
3315 {
3316  int status = NO_ERROR;
3317 
3318  status = us_hb_copylogdb_stop (ha_conf, db_name, node_name, NULL);
3319  if (status != NO_ERROR)
3320  {
3321  return status;
3322  }
3323 
3324  status = us_hb_applylogdb_stop (ha_conf, db_name, node_name, NULL);
3325  return status;
3326 }
3327 #endif /* ENABLE_UNUSED_FUNCTION */
3328 
3329 static int
3330 us_hb_server_start (HA_CONF * ha_conf, const char *db_name)
3331 {
3332  int status = NO_ERROR;
3333  int i, num_db_found = 0;
3334  char **dbs;
3335 
3336  dbs = ha_conf->db_names;
3337  for (i = 0; dbs[i] != NULL; i++)
3338  {
3339  if (db_name != NULL && strcmp (db_name, dbs[i]))
3340  {
3341  continue;
3342  }
3343  num_db_found++;
3344 
3345  if (!is_server_running (CHECK_SERVER, dbs[i], 0))
3346  {
3349  if (status != NO_ERROR)
3350  {
3353  break;
3354  }
3355 
3357  {
3361  continue;
3362  }
3363 
3364  status = process_server (START, 1, &(dbs[i]), true, false, false);
3365  if (status != NO_ERROR)
3366  {
3367  return status;
3368  }
3369  }
3370  else
3371  {
3374  }
3375  }
3376 
3377  if (db_name != NULL && num_db_found == 0)
3378  {
3381  status = ER_GENERIC_ERROR;
3382  }
3383 
3384  return status;
3385 }
3386 
3387 static int
3388 us_hb_server_stop (HA_CONF * ha_conf, const char *db_name)
3389 {
3390  int status = NO_ERROR;
3391  int i, num_db_found = 0;
3392  char **dbs;
3393 
3394  dbs = ha_conf->db_names;
3395  for (i = 0; dbs[i] != NULL; i++)
3396  {
3397  if (db_name != NULL && strcmp (db_name, dbs[i]))
3398  {
3399  continue;
3400  }
3401  num_db_found++;
3402 
3403  if (is_server_running (CHECK_SERVER, dbs[i], 0))
3404  {
3405  status = process_server (STOP, 1, &(dbs[i]), true, false, false);
3406  if (status != NO_ERROR)
3407  {
3408  return status;
3409  }
3410  }
3411  else
3412  {
3415  }
3416  }
3417 
3418  if (db_name != NULL && num_db_found == 0)
3419  {
3422  status = ER_GENERIC_ERROR;
3423  }
3424 
3425  return status;
3426 }
3427 
3428 static int
3429 us_hb_process_start (HA_CONF * ha_conf, const char *db_name, bool check_result)
3430 {
3431  int status = NO_ERROR;
3432  int i;
3433  int pid;
3434  dynamic_array *pids = NULL;
3435 
3437 
3438  pids = da_create (100, sizeof (int));
3439  if (pids == NULL)
3440  {
3441  status = ER_GENERIC_ERROR;
3442  goto ret;
3443  }
3444 
3445  status = us_hb_server_start (ha_conf, db_name);
3446  if (status != NO_ERROR)
3447  {
3448  goto ret;
3449  }
3450 
3451  status = us_hb_copylogdb_start (pids, ha_conf, db_name, NULL, NULL);
3452  if (status != NO_ERROR)
3453  {
3454  goto ret;
3455  }
3456 
3457  status = us_hb_applylogdb_start (pids, ha_conf, db_name, NULL, NULL);
3458  if (status != NO_ERROR)
3459  {
3460  goto ret;
3461  }
3462 
3464  if (check_result == true)
3465  {
3466  for (i = 0; i < da_size (pids); i++)
3467  {
3468  da_get (pids, i, &pid);
3469  if (is_terminated_process (pid))
3470  {
3471  status = ER_GENERIC_ERROR;
3472  break;
3473  }
3474  }
3475  }
3476 
3477 ret:
3478  if (pids)
3479  {
3480  da_destroy (pids);
3481  }
3482 
3484  return status;
3485 }
3486 
3487 /*
3488  * us_hb_deactivate - deactivate CUBRID heartbeat
3489  * return:
3490  *
3491  * hostname(in): target hostname
3492  * immediate_stop(in): whether to stop immediately or not
3493  */
3494 static int
3495 us_hb_deactivate (const char *hostname, bool immediate_stop)
3496 {
3497  int status = NO_ERROR;
3498  int opt_idx = 1;
3499 
3500  const char *args[] = {
3501  UTIL_COMMDB_NAME, NULL, NULL, NULL, NULL, NULL
3502  };
3503 
3504  if (hostname != NULL && hostname[0] != '\0')
3505  {
3506  args[opt_idx++] = COMMDB_HOST;
3507  args[opt_idx++] = hostname;
3508  }
3509 
3510  if (immediate_stop == true)
3511  {
3512  args[opt_idx++] = COMMDB_HB_DEACT_IMMEDIATELY;
3513  }
3514 
3515  /* stop all HA processes including cub_server */
3516  args[opt_idx] = COMMDB_HA_DEACT_STOP_ALL;
3517  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
3518  if (status != NO_ERROR)
3519  {
3520  return status;
3521  }
3522 
3523  /* wait until all processes are shutdown */
3524  args[opt_idx] = COMMDB_HA_DEACT_CONFIRM_STOP_ALL;
3525  while ((status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL)) != NO_ERROR)
3526  {
3527  if (status == EXIT_FAILURE)
3528  {
3529  return status;
3530  }
3531  sleep (1);
3532  }
3533 
3534  /* start deactivation */
3535  args[opt_idx] = COMMDB_HA_DEACTIVATE;
3536  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
3537  if (status != NO_ERROR)
3538  {
3539  return status;
3540  }
3541 
3542  /* wait until no cub_server processes are running */
3543  args[opt_idx] = COMMDB_HA_DEACT_CONFIRM_NO_SERVER;
3544  while ((status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL)) != NO_ERROR)
3545  {
3546  if (status == EXIT_FAILURE)
3547  {
3548  return status;
3549  }
3550  sleep (1);
3551  }
3552 
3553  return NO_ERROR;
3554 }
3555 
3556 static int
3557 us_hb_process_stop (HA_CONF * ha_conf, const char *db_name)
3558 {
3559  int status = NO_ERROR;
3560 
3562 
3563  status = us_hb_copylogdb_stop (ha_conf, db_name, NULL, NULL);
3564  if (status != NO_ERROR)
3565  {
3566  goto ret;
3567  }
3568 
3569  status = us_hb_applylogdb_stop (ha_conf, db_name, NULL, NULL);
3570  if (status != NO_ERROR)
3571  {
3572  goto ret;
3573  }
3574 
3575  status = us_hb_server_stop (ha_conf, db_name);
3576 
3577 ret:
3579  return status;
3580 }
3581 
3582 static int
3583 us_hb_process_copylogdb (int command_type, HA_CONF * ha_conf, const char *db_name, const char *node_name,
3584  const char *remote_host)
3585 {
3586  int status = NO_ERROR;
3587  int i;
3588  int pid;
3589  dynamic_array *pids = NULL;
3590  dynamic_array *args = NULL;
3591  char arg[PATH_MAX];
3592 
3593  switch (command_type)
3594  {
3595  case START:
3596  if (remote_host == NULL)
3597  {
3598  pids = da_create (100, sizeof (int));
3599  if (pids == NULL)
3600  {
3601  status = ER_GENERIC_ERROR;
3602  goto ret;
3603  }
3604 
3605  status = us_hb_copylogdb_start (pids, ha_conf, db_name, node_name, remote_host);
3606 
3608  for (i = 0; i < da_size (pids); i++)
3609  {
3610  da_get (pids, i, &pid);
3611  if (is_terminated_process (pid))
3612  {
3613  (void) us_hb_copylogdb_stop (ha_conf, db_name, node_name, remote_host);
3614  status = ER_GENERIC_ERROR;
3615  break;
3616  }
3617  }
3618  }
3619  else
3620  {
3621  args = da_create (100, sizeof (arg));
3622  if (args == NULL)
3623  {
3624  status = ER_GENERIC_ERROR;
3625  goto ret;
3626  }
3627 
3628  status = us_hb_copylogdb_start (args, ha_conf, db_name, node_name, remote_host);
3629 
3631  for (i = 0; i < da_size (args); i++)
3632  {
3633  da_get (args, i, arg);
3634  if (ha_is_registered (arg, remote_host) == false)
3635  {
3636  (void) us_hb_copylogdb_stop (ha_conf, db_name, node_name, remote_host);
3637  status = ER_GENERIC_ERROR;
3638  break;
3639  }
3640  }
3641  }
3642 
3643  break;
3644 
3645  case STOP:
3646  status = us_hb_copylogdb_stop (ha_conf, db_name, node_name, remote_host);
3647 
3648  break;
3649 
3650  default:
3651  status = ER_GENERIC_ERROR;
3652  break;
3653  }
3654 
3655 ret:
3656  if (pids)
3657  {
3658  da_destroy (pids);
3659  }
3660  if (args)
3661  {
3662  da_destroy (args);
3663  }
3664 
3665  return status;
3666 }
3667 
3668 static int
3669 us_hb_process_applylogdb (int command_type, HA_CONF * ha_conf, const char *db_name, const char *node_name,
3670  const char *remote_host)
3671 {
3672  int status = NO_ERROR;
3673  int i, pid;
3674  dynamic_array *pids = NULL;
3675  dynamic_array *args = NULL;
3676  char arg[PATH_MAX];
3677 
3678  switch (command_type)
3679  {
3680  case START:
3681  if (remote_host == NULL)
3682  {
3683  pids = da_create (100, sizeof (int));
3684  if (pids == NULL)
3685  {
3686  status = ER_GENERIC_ERROR;
3687  goto ret;
3688  }
3689 
3690  status = us_hb_applylogdb_start (pids, ha_conf, db_name, node_name, remote_host);
3691 
3693  for (i = 0; i < da_size (pids); i++)
3694  {
3695  da_get (pids, i, &pid);
3696  if (is_terminated_process (pid))
3697  {
3698  (void) us_hb_applylogdb_stop (ha_conf, db_name, node_name, remote_host);
3699  status = ER_GENERIC_ERROR;
3700  break;
3701  }
3702  }
3703  }
3704  else
3705  {
3706  args = da_create (100, sizeof (arg));
3707  if (args == NULL)
3708  {
3709  status = ER_GENERIC_ERROR;
3710  goto ret;
3711  }
3712 
3713  status = us_hb_applylogdb_start (args, ha_conf, db_name, node_name, remote_host);
3714 
3716  for (i = 0; i < da_size (args); i++)
3717  {
3718  da_get (args, i, arg);
3719  if (ha_is_registered (arg, remote_host) == false)
3720  {
3721  (void) us_hb_applylogdb_stop (ha_conf, db_name, node_name, remote_host);
3722  status = ER_GENERIC_ERROR;
3723  break;
3724  }
3725  }
3726  }
3727  break;
3728 
3729  case STOP:
3730  status = us_hb_applylogdb_stop (ha_conf, db_name, node_name, remote_host);
3731  break;
3732 
3733  default:
3734  status = ER_GENERIC_ERROR;
3735  break;
3736  }
3737 
3738 ret:
3739  if (pids)
3740  {
3741  da_destroy (pids);
3742  }
3743  if (args)
3744  {
3745  da_destroy (args);
3746  }
3747 
3748  return status;
3749 }
3750 
3751 #if defined (ENABLE_UNUSED_FUNCTION)
3752 static int
3753 us_hb_process_server (int command_type, HA_CONF * ha_conf, const char *db_name)
3754 {
3755  int status = NO_ERROR;
3756 
3757  switch (command_type)
3758  {
3759  case START:
3760  status = us_hb_server_start (ha_conf, db_name);
3761  break;
3762 
3763  case STOP:
3764  status = us_hb_server_stop (ha_conf, db_name);
3765  break;
3766 
3767  default:
3768  status = ER_GENERIC_ERROR;
3769  break;
3770  }
3771 
3772  return status;
3773 }
3774 #endif /* !ENABLE_UNUSED_FUNCTION */
3775 #endif /* !WINDOWS */
3776 
3777 
3778 /*
3779  * us_hb_stop_get_options -
3780  * return: NO_ERROR or error code
3781  *
3782  * db_name(out):
3783  * db_name_size(in):
3784  * remote_host_name(out):
3785  * remote_host_name_size(in):
3786  * immediate_stop(out):
3787  * argc(in):
3788  * argv(in):
3789  *
3790  */
3791 static int
3792 us_hb_stop_get_options (char *db_name, int db_name_size, char *remote_host_name, int remote_host_name_size,
3793  bool * immediate_stop, int argc, const char **argv)
3794 {
3795  int status = NO_ERROR;
3796  int opt, opt_idx;
3797  char opt_str[64];
3798  int tmp_argc;
3799  const char **tmp_argv = NULL;
3800  size_t copy_len;
3801 
3802  const struct option hb_stop_opts[] = {
3804  {COMMDB_HOST_L, 1, 0, COMMDB_HOST_S},
3805  {0, 0, 0, 0}
3806  };
3807 
3808  *db_name = *remote_host_name = '\0';
3809  *immediate_stop = false;
3810 
3811  /* dummy program name + user given argv */
3812  tmp_argc = argc + 1;
3813 
3814  /* +1 for null termination */
3815  tmp_argv = (const char **) malloc ((tmp_argc + 1) * sizeof (char *));
3816  if (tmp_argv == NULL)
3817  {
3818  status = ER_GENERIC_ERROR;
3821  goto ret;
3822  }
3823  memset (tmp_argv, 0, (tmp_argc + 1) * sizeof (char *));
3824 
3825  tmp_argv[0] = PRINT_HEARTBEAT_NAME " " PRINT_CMD_STOP;
3826  memcpy (&tmp_argv[1], argv, argc * sizeof (char *));
3827 
3828  utility_make_getopt_optstring (hb_stop_opts, opt_str);
3829  while (1)
3830  {
3831  opt = getopt_long (tmp_argc, (char *const *) tmp_argv, opt_str, hb_stop_opts, &opt_idx);
3832  if (opt == -1)
3833  {
3834  break;
3835  }
3836 
3837  switch (opt)
3838  {
3839  case COMMDB_HOST_S:
3840  copy_len = strnlen (optarg, remote_host_name_size - 1);
3841  memcpy (remote_host_name, optarg, copy_len);
3842  remote_host_name[copy_len] = '\0';
3843  break;
3845  *immediate_stop = true;
3846  break;
3847  default:
3848  status = ER_GENERIC_ERROR;
3850  break;
3851  }
3852  }
3853 
3854  /* process non optional arguments */
3855  if (status == NO_ERROR && tmp_argc > optind)
3856  {
3857  if ((tmp_argc - optind) > 1)
3858  {
3859  status = ER_GENERIC_ERROR;
3860  print_message (stderr, MSGCAT_UTIL_GENERIC_ARGS_OVER, tmp_argv[optind + 1]);
3862 
3863  goto ret;
3864  }
3865 
3866  if ((tmp_argc - optind) == 1)
3867  {
3868  copy_len = strnlen (tmp_argv[optind], db_name_size);
3869  memcpy (db_name, tmp_argv[optind], copy_len);
3870  db_name[copy_len] = '\0';
3871  }
3872  }
3873 
3874 ret:
3875  if (tmp_argv)
3876  {
3877  free_and_init (tmp_argv);
3878  }
3879 
3880  return status;
3881 }
3882 
3883 /*
3884  * us_hb_status_get_options -
3885  * return: NO_ERROR or error code
3886  *
3887  * verbose(out):
3888  * remote_host_name(out):
3889  * remote_host_name_size(in):
3890  * argc(in):
3891  * argv(in):
3892  *
3893  */
3894 static int
3895 us_hb_status_get_options (bool * verbose, char *remote_host_name, int remote_host_name_size, int argc,
3896  const char **argv)
3897 {
3898  int status = NO_ERROR;
3899  int opt, opt_idx;
3900  char opt_str[64];
3901  int tmp_argc;
3902  const char **tmp_argv = NULL;
3903  int copy_len;
3904 
3905  const struct option hb_status_opts[] = {
3906  {"verbose", 0, 0, 'v'},
3907  {COMMDB_HOST_L, 1, 0, COMMDB_HOST_S},
3908  {0, 0, 0, 0}
3909  };
3910 
3911  *verbose = false;
3912  *remote_host_name = '\0';
3913 
3914  /* dummy program name + user given argv */
3915  tmp_argc = argc + 1;
3916 
3917  /* +1 for null termination */
3918  tmp_argv = (const char **) malloc ((tmp_argc + 1) * sizeof (char *));
3919  if (tmp_argv == NULL)
3920  {
3921  status = ER_GENERIC_ERROR;
3924  goto ret;
3925  }
3926  memset (tmp_argv, 0, (tmp_argc + 1) * sizeof (char *));
3927 
3928  tmp_argv[0] = PRINT_HEARTBEAT_NAME " " PRINT_CMD_STATUS;
3929  memcpy (&tmp_argv[1], argv, argc * sizeof (char *));
3930 
3931  utility_make_getopt_optstring (hb_status_opts, opt_str);
3932  while (1)
3933  {
3934  opt = getopt_long (tmp_argc, (char *const *) tmp_argv, opt_str, hb_status_opts, &opt_idx);
3935  if (opt == -1)
3936  {
3937  break;
3938  }
3939 
3940  switch (opt)
3941  {
3942  case 'v':
3943  *verbose = true;
3944  break;
3945  case COMMDB_HOST_S:
3946  copy_len = (int) strnlen (optarg, (size_t) (remote_host_name_size - 1));
3947  memcpy (remote_host_name, optarg, copy_len);
3948  remote_host_name[copy_len] = '\0';
3949  break;
3950  default:
3951  status = ER_GENERIC_ERROR;
3953  break;
3954  }
3955  }
3956 
3957  /* process non optional arguments */
3958  if (status == NO_ERROR && tmp_argc > optind)
3959  {
3960  status = ER_GENERIC_ERROR;
3961  print_message (stderr, MSGCAT_UTIL_GENERIC_ARGS_OVER, tmp_argv[optind]);
3963  goto ret;
3964  }
3965 
3966 ret:
3967  if (tmp_argv)
3968  {
3969  free_and_init (tmp_argv);
3970  }
3971 
3972  return status;
3973 }
3974 
3975 /*
3976  * us_hb_util_get_options -
3977  * return: NO_ERROR or error code
3978  *
3979  * db_name(out):
3980  * db_name_size(in):
3981  * node_name(out):
3982  * node_name_size(in):
3983  * remote_host_name(out):
3984  * remote_host_name_size(in):
3985  * argc(in):
3986  * argv(in):
3987  *
3988  */
3989 static int
3990 us_hb_util_get_options (char *db_name, int db_name_size, char *node_name, int node_name_size, char *remote_host_name,
3991  int remote_host_name_size, int argc, const char **argv)
3992 {
3993  int status = NO_ERROR;
3994  int opt, opt_idx;
3995  char opt_str[64];
3996  int tmp_argc;
3997  const char **tmp_argv = NULL;
3998  size_t copy_len;
3999 
4000  const struct option hb_util_opts[] = {
4001  {COMMDB_HOST_L, 1, 0, COMMDB_HOST_S},
4002  {0, 0, 0, 0}
4003  };
4004 
4005  if (argc < 0)
4006  {
4008  return ER_GENERIC_ERROR;
4009  }
4010 
4011  *db_name = *node_name = *remote_host_name = '\0';
4012 
4013  /* dummy program name + user given argv */
4014  tmp_argc = argc + 1;
4015 
4016  /* +1 for null termination */
4017  tmp_argv = (const char **) malloc ((tmp_argc + 1) * sizeof (char *));
4018  if (tmp_argv == NULL)
4019  {
4020  status = ER_GENERIC_ERROR;
4023  goto ret;
4024  }
4025  memset (tmp_argv, 0, (tmp_argc + 1) * sizeof (char *));
4026 
4027  tmp_argv[0] = PRINT_HEARTBEAT_NAME " " "copylogdb/applylogdb/prefetchlogdb";
4028  memcpy (&tmp_argv[1], argv, argc * sizeof (char *));
4029 
4030  utility_make_getopt_optstring (hb_util_opts, opt_str);
4031  while (1)
4032  {
4033  opt = getopt_long (tmp_argc, (char *const *) tmp_argv, opt_str, hb_util_opts, &opt_idx);
4034  if (opt == -1)
4035  {
4036  break;
4037  }
4038 
4039  switch (opt)
4040  {
4041  case COMMDB_HOST_S:
4042  copy_len = strnlen (optarg, remote_host_name_size);
4043  memcpy (remote_host_name, optarg, copy_len);
4044  remote_host_name[copy_len] = '\0';
4045  break;
4046  default:
4047  status = ER_GENERIC_ERROR;
4049  break;
4050  }
4051  }
4052 
4053  /* process non optional arguments */
4054  if (status == NO_ERROR)
4055  {
4056  if ((tmp_argc - optind) > 2)
4057  {
4058  status = ER_GENERIC_ERROR;
4059  print_message (stderr, MSGCAT_UTIL_GENERIC_ARGS_OVER, tmp_argv[tmp_argc - 1]);
4060  util_log_write_errid (MSGCAT_UTIL_GENERIC_ARGS_OVER, tmp_argv[tmp_argc - 1]);
4061  goto ret;
4062  }
4063  else if ((tmp_argc - optind) < 2)
4064  {
4065  status = ER_GENERIC_ERROR;
4068  goto ret;
4069  }
4070 
4071  copy_len = strnlen (tmp_argv[optind], db_name_size - 1);
4072  memcpy (db_name, tmp_argv[optind], copy_len);
4073  db_name[copy_len] = '\0';
4074 
4075  copy_len = strnlen (tmp_argv[optind + 1], node_name_size - 1);
4076  memcpy (node_name, tmp_argv[optind + 1], copy_len);
4077  node_name[copy_len] = '\0';
4078  }
4079 
4080 ret:
4081  if (tmp_argv)
4082  {
4083  free_and_init (tmp_argv);
4084  }
4085 
4086  return status;
4087 }
4088 
4089 
4090 #if !defined(WINDOWS)
4091 
4092 /*
4093  * process_heartbeat_start -
4094  *
4095  * return:
4096  *
4097  * ha_conf(in):
4098  * argc(in):
4099  * argv(in):
4100  *
4101  */
4102 static int
4103 process_heartbeat_start (HA_CONF * ha_conf, int argc, const char **argv)
4104 {
4105  int status = NO_ERROR;
4106  int master_port;
4107  const char *db_name = NULL;
4108 
4110 
4111  master_port = prm_get_master_port_id ();
4112  if (!css_does_master_exist (master_port))
4113  {
4114  status = process_master (START);
4115 
4116  if (status != NO_ERROR)
4117  {
4118  goto ret;
4119  }
4120  }
4121 
4122  if (css_does_master_exist (master_port))
4123  {
4124  const char *args[] = {
4126  };
4127  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
4128  }
4129 
4130  if (status != NO_ERROR)
4131  {
4132  goto ret;
4133  }
4134 
4135  db_name = (argc >= 1) ? argv[0] : NULL;
4136  if (db_name != NULL)
4137  {
4139  if (status != NO_ERROR)
4140  {
4142  goto ret;
4143  }
4144 
4146  {
4147  status = ER_GENERIC_ERROR;
4150  goto ret;
4151  }
4152  }
4153 
4154  status = us_hb_process_start (ha_conf, db_name, true);
4155  if (status != NO_ERROR)
4156  {
4157  if (db_name == NULL)
4158  {
4159  (void) us_hb_deactivate (NULL, false);
4160  }
4161  else
4162  {
4163  (void) us_hb_process_stop (ha_conf, db_name);
4164  }
4165  }
4166 
4167 ret:
4169  return status;
4170 }
4171 
4172 /*
4173  * process_heartbeat_stop -
4174  *
4175  * return:
4176  *
4177  * ha_conf(in):
4178  * argc(in):
4179  * argv(in):
4180  *
4181  */
4182 static int
4183 process_heartbeat_stop (HA_CONF * ha_conf, int argc, const char **argv)
4184 {
4185  int status = NO_ERROR;
4186  int master_port;
4187  char db_name[64] = ""; /* DBNAME_LEN */
4188  char remote_host_name[CUB_MAXHOSTNAMELEN] = "";
4189  bool immediate_stop = false;
4190 
4192 
4193  status =
4194  us_hb_stop_get_options (db_name, sizeof (db_name), remote_host_name, sizeof (remote_host_name), &immediate_stop,
4195  argc, argv);
4196  if (status != NO_ERROR)
4197  {
4198  goto ret;
4199  }
4200 
4201  /* validate options */
4202  if ((db_name[0] != '\0') && (remote_host_name[0] != '\0' || immediate_stop == true))
4203  {
4204  status = ER_GENERIC_ERROR;
4207  goto ret;
4208  }
4209 
4210  master_port = prm_get_master_port_id ();
4211  if (remote_host_name[0] != '\0' || css_does_master_exist (master_port) == true)
4212  {
4213  if (db_name[0] != '\0')
4214  {
4216  if (status != NO_ERROR)
4217  {
4218  goto ret;
4219  }
4220 
4222  {
4223  status = ER_GENERIC_ERROR;
4226  goto ret;
4227  }
4228 
4229  status = us_hb_process_stop (ha_conf, db_name);
4230  }
4231  else
4232  {
4233  status = us_hb_deactivate (remote_host_name, immediate_stop);
4234  }
4235  }
4236  else
4237  {
4238  status = ER_GENERIC_ERROR;
4241  }
4242 
4243  if (status == NO_ERROR)
4244  {
4245  /* wait for cub_master to clean up its internal resources */
4247  }
4248 
4249 ret:
4251  return status;
4252 }
4253 #endif
4254 
4255 /*
4256  * process_heartbeat_deregister -
4257  *
4258  * return:
4259  *
4260  * argc(in):
4261  * argv(in):
4262  *
4263  */
4264 static int
4266 {
4267  int status = NO_ERROR;
4268  int master_port;
4269  const char *pid = NULL;
4270 
4271  if (argc < 1)
4272  {
4273  status = ER_GENERIC_ERROR;
4276  goto ret;
4277  }
4278 
4279  pid = (char *) argv[0];
4280  if (pid == NULL)
4281  {
4282  status = ER_GENERIC_ERROR;
4285  goto ret;
4286  }
4287 
4289 
4290  master_port = prm_get_master_port_id ();
4291  if (css_does_master_exist (master_port))
4292  {
4293  const char *commdb_argv[] = {
4295  };
4296  status = proc_execute (UTIL_COMMDB_NAME, commdb_argv, true, false, false, NULL);
4297  }
4298  else
4299  {
4300  status = ER_GENERIC_ERROR;
4303  }
4304 
4305 ret:
4307  return status;
4308 }
4309 
4310 /*
4311  * process_heartbeat_status -
4312  *
4313  * return:
4314  *
4315  * argc(in):
4316  * argv(in):
4317  *
4318  */
4319 static int
4321 {
4322  int status = NO_ERROR;
4323  int master_port;
4324  bool verbose;
4325  char remote_host_name[CUB_MAXHOSTNAMELEN];
4326  int ext_opt_offset;
4327 
4328  const char *node_list_argv[] = {
4330  NULL, NULL, NULL, /* -v and/or -h hostname */
4331  NULL
4332  };
4333  const char *proc_list_argv[] = {
4335  NULL, NULL, NULL, /* -v and/or -h hostname */
4336  NULL
4337  };
4338  const char *ping_host_list_argv[] = {
4340  NULL, NULL, NULL, /* -v and/or -h hostname */
4341  NULL
4342  };
4343  const char *admin_info_argv[] = {
4345  NULL, NULL, NULL, /* -v and/or -h hostname */
4346  NULL
4347  };
4348 
4350 
4351  verbose = false;
4352  memset (remote_host_name, 0, sizeof (remote_host_name));
4353  status = us_hb_status_get_options (&verbose, remote_host_name, sizeof (remote_host_name), argc, argv);
4354  if (status != NO_ERROR)
4355  {
4356  return status;
4357  }
4358 
4359  ext_opt_offset = 2;
4360 
4361  if (remote_host_name[0] != '\0')
4362  {
4363  node_list_argv[ext_opt_offset] = COMMDB_HOST; /* -h */
4364  proc_list_argv[ext_opt_offset] = COMMDB_HOST; /* -h */
4365  ping_host_list_argv[ext_opt_offset] = COMMDB_HOST; /* -h */
4366  admin_info_argv[ext_opt_offset] = COMMDB_HOST; /* -h */
4367  ext_opt_offset++;
4368 
4369  node_list_argv[ext_opt_offset] = remote_host_name; /* hostname */
4370  proc_list_argv[ext_opt_offset] = remote_host_name; /* hostname */
4371  ping_host_list_argv[ext_opt_offset] = remote_host_name; /* hostname */
4372  admin_info_argv[ext_opt_offset] = remote_host_name; /* hostname */
4373  ext_opt_offset++;
4374  }
4375  else
4376  {
4377  master_port = prm_get_master_port_id ();
4378  if (css_does_master_exist (master_port) == false)
4379  {
4380  status = ER_GENERIC_ERROR;
4382  return status;
4383  }
4384  }
4385 
4386  if (verbose == true)
4387  {
4388  node_list_argv[ext_opt_offset] = COMMDB_VERBOSE_OUTPUT;
4389  proc_list_argv[ext_opt_offset] = COMMDB_VERBOSE_OUTPUT;
4390  }
4391 
4392  status = proc_execute (UTIL_COMMDB_NAME, node_list_argv, true, false, false, NULL);
4393  if (status != NO_ERROR)
4394  {
4395  return status;
4396  }
4397 
4398  status = proc_execute (UTIL_COMMDB_NAME, proc_list_argv, true, false, false, NULL);
4399  if (status != NO_ERROR)
4400  {
4401  return status;
4402  }
4403 
4404  status = proc_execute (UTIL_COMMDB_NAME, ping_host_list_argv, true, false, false, NULL);
4405  if (status != NO_ERROR)
4406  {
4407  return status;
4408  }
4409 
4410  if (verbose == true)
4411  {
4412  status = proc_execute (UTIL_COMMDB_NAME, admin_info_argv, true, false, false, NULL);
4413  }
4414 
4415  return status;
4416 }
4417 
4418 /*
4419  * process_heartbeat_reload -
4420  *
4421  * return:
4422  *
4423  * argc(in):
4424  * argv(in):
4425  *
4426  */
4427 static int
4429 {
4430  int status = NO_ERROR;
4431  int master_port;
4432 
4434 
4435  master_port = prm_get_master_port_id ();
4436  if (css_does_master_exist (master_port))
4437  {
4438  const char *args[] = {
4440  };
4441  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
4442  }
4443  else
4444  {
4445  status = ER_GENERIC_ERROR;
4448  }
4449 
4451  return status;
4452 }
4453 
4454 #if !defined(WINDOWS)
4455 /*
4456  * process_heartbeat_util -
4457  *
4458  * return:
4459  *
4460  * ha_conf(in):
4461  * command_type(in):
4462  * argc(in):
4463  * argv(in):
4464  *
4465  */
4466 static int
4467 process_heartbeat_util (HA_CONF * ha_conf, int command_type, int argc, const char **argv)
4468 {
4469  int status = NO_ERROR;
4470  int sub_command_type;
4471 
4472  char db_name[64];
4474  char host_name[CUB_MAXHOSTNAMELEN];
4475  char *db_name_p, *node_name_p, *host_name_p;
4476 
4478 
4479  sub_command_type = parse_arg (us_Command_map, (char *) argv[0]);
4480 
4481  db_name[0] = node_name[0] = host_name[0] = '\0';
4482  status =
4483  us_hb_util_get_options (db_name, sizeof (db_name), node_name, sizeof (node_name), host_name, sizeof (host_name),
4484  (argc - 1), (argv + 1));
4485  if (status != NO_ERROR)
4486  {
4487  goto ret;
4488  }
4489 
4490  db_name_p = (db_name[0] == '\0') ? NULL : db_name;
4491  node_name_p = (node_name[0] == '\0') ? NULL : node_name;
4492  host_name_p = (host_name[0] == '\0') ? NULL : host_name;
4493 
4494  assert (db_name_p != NULL);
4495 
4496  status = sysprm_load_and_init (db_name_p, NULL, SYSPRM_IGNORE_INTL_PARAMS);
4497  if (status != NO_ERROR)
4498  {
4499  goto ret;
4500  }
4501 
4503  {
4504  status = ER_GENERIC_ERROR;
4506  goto ret;
4507  }
4508 
4509  switch (command_type)
4510  {
4511  case SC_COPYLOGDB:
4512  status = us_hb_process_copylogdb (sub_command_type, ha_conf, db_name_p, node_name_p, host_name_p);
4513  break;
4514  case SC_APPLYLOGDB:
4515  status = us_hb_process_applylogdb (sub_command_type, ha_conf, db_name_p, node_name_p, host_name_p);
4516  break;
4517  }
4518 
4519 ret:
4520  print_result (PRINT_HEARTBEAT_NAME, status, command_type);
4521  return status;
4522 }
4523 
4524 /*
4525  * process_heartbeat_replication -
4526  *
4527  * return:
4528  *
4529  * ha_conf(in):
4530  * argc(in):
4531  * argv(in):
4532  *
4533  */
4534 static int
4535 process_heartbeat_replication (HA_CONF * ha_conf, int argc, const char **argv)
4536 {
4537  int status = NO_ERROR;
4538  int sub_command_type;
4539  int master_port;
4540  const char *node_name = NULL;
4541 
4543 
4544  if (argc < 2)
4545  {
4546  status = ER_GENERIC_ERROR;
4549  goto ret;
4550  }
4551 
4552  sub_command_type = parse_arg (us_Command_map, (char *) argv[0]);
4553  node_name = argv[1];
4554 
4555  if ((sub_command_type != START && sub_command_type != STOP) || node_name == NULL || node_name[0] == '\0')
4556  {
4557  status = ER_GENERIC_ERROR;
4560  goto ret;
4561  }
4562 
4563  master_port = prm_get_master_port_id ();
4564  if (css_does_master_exist (master_port))
4565  {
4566  const char *args[] = {
4568  };
4569  status = proc_execute (UTIL_COMMDB_NAME, args, true, false, false, NULL);
4570  if (status == NO_ERROR)
4571  {
4572  if (sub_command_type == START)
4573  {
4574  status = us_hb_process_copylogdb (START, ha_conf, NULL, node_name, NULL);
4575  if (status == NO_ERROR)
4576  {
4577  status = us_hb_process_applylogdb (START, ha_conf, NULL, node_name, NULL);
4578  if (status != NO_ERROR)
4579  {
4580  (void) us_hb_process_copylogdb (STOP, ha_conf, NULL, node_name, NULL);
4581  }
4582  }
4583  }
4584  else
4585  {
4586  (void) us_hb_process_copylogdb (STOP, ha_conf, NULL, node_name, NULL);
4587  (void) us_hb_process_applylogdb (STOP, ha_conf, NULL, node_name, NULL);
4588  status = NO_ERROR;
4589  }
4590  }
4591  }
4592  else
4593  {
4594  status = ER_GENERIC_ERROR;
4597  }
4598 
4599 ret:
4601  return status;
4602 }
4603 #endif
4604 
4605 /*
4606  * process_heartbeat -
4607  *
4608  * return:
4609  *
4610  * command_type(in):
4611  * argc(in):
4612  * argv(in):
4613  *
4614  */
4615 static int
4616 process_heartbeat (int command_type, int argc, const char **argv)
4617 {
4618  int status = NO_ERROR;
4619 #if !defined(WINDOWS)
4620  HA_CONF ha_conf;
4621 
4623  {
4626  print_result (PRINT_HEARTBEAT_NAME, ER_FAILED, command_type);
4627  return ER_FAILED;
4628  }
4629 
4630  memset ((void *) &ha_conf, 0, sizeof (HA_CONF));
4631  status = util_make_ha_conf (&ha_conf);
4632  if (status != NO_ERROR)
4633  {
4636  print_result (PRINT_HEARTBEAT_NAME, ER_FAILED, command_type);
4637  return ER_FAILED;
4638  }
4639 
4640  switch (command_type)
4641  {
4642  case START:
4643  status = process_heartbeat_start (&ha_conf, argc, argv);
4644  break;
4645  case STOP:
4646  status = process_heartbeat_stop (&ha_conf, argc, argv);
4647  break;
4648  case DEREGISTER:
4649  status = process_heartbeat_deregister (argc, argv);
4650  break;
4651  case STATUS:
4652  case LIST:
4653  status = process_heartbeat_status (argc, argv);
4654  break;
4655  case RELOAD:
4656  status = process_heartbeat_reload (argc, argv);
4657  break;
4658  case SC_COPYLOGDB:
4659  case SC_APPLYLOGDB:
4660  status = process_heartbeat_util (&ha_conf, command_type, argc, argv);
4661  break;
4662  case REPLICATION:
4663  status = process_heartbeat_replication (&ha_conf, argc, argv);
4664  break;
4665  default:
4666  status = ER_GENERIC_ERROR;
4667  break;
4668  }
4669 
4670 ret:
4671  util_free_ha_conf (&ha_conf);
4672  return status;
4673 #else /* !WINDOWS */
4674 
4675  status = ER_FAILED;
4676  print_result (PRINT_HEARTBEAT_NAME, status, command_type);
4677  return status;
4678 #endif /* WINDOWS */
4679 }
4680 
4681 /*
4682  * parse_arg -
4683  *
4684  * return:
4685  *
4686  * option(in):
4687  * arg(in):
4688  *
4689  */
4690 static int
4692 {
4693  int i;
4694 
4695  if (arg == NULL || arg[0] == 0)
4696  {
4697  return ER_GENERIC_ERROR;
4698  }
4699  for (i = 0; option[i].option_type != -1; i++)
4700  {
4701  if (strcasecmp (option[i].option_name, arg) == 0)
4702  {
4703  return option[i].option_type;
4704  }
4705  }
4706  return ER_GENERIC_ERROR;
4707 }
4708 
4709 /*
4710  * load_properties -
4711  *
4712  * return:
4713  *
4714  * NOTE:
4715  */
4716 static int
4718 {
4719  bool server_flag = false;
4720  bool broker_flag = false;
4721  bool manager_flag = false;
4722  bool heartbeat_flag = false;
4723  bool javasp_flag = false;
4724 
4725  char *value = NULL;
4726 
4728  {
4729  return ER_GENERIC_ERROR;
4730  }
4731 
4732  /* get service::service list */
4734  if (value != NULL)
4735  {
4736  const char *error_msg;
4737  char *util = NULL, *save_ptr = NULL;
4738  for (util = value;; util = NULL)
4739  {
4740  util = strtok_r (util, " \t,", &save_ptr);
4741  if (util == NULL)
4742  {
4743  break;
4744  }
4745 
4746  if (strcmp (util, UTIL_TYPE_SERVER) == 0)
4747  {
4748  server_flag = true;
4749  }
4750  else if (strcmp (util, UTIL_TYPE_BROKER) == 0)
4751  {
4752  broker_flag = true;
4753  }
4754  else if (strcmp (util, UTIL_TYPE_MANAGER) == 0)
4755  {
4756  manager_flag = true;
4757  }
4758  else if (strcmp (util, UTIL_TYPE_HEARTBEAT) == 0)
4759  {
4760  heartbeat_flag = true;
4761  }
4762  else if (strcmp (util, UTIL_TYPE_JAVASP) == 0)
4763  {
4764  javasp_flag = true;
4765  }
4766  else
4767  {
4769  fprintf (stderr, error_msg, "service", util);
4770  return ER_GENERIC_ERROR;
4771  }
4772  }
4773  }
4774 
4775  us_Property_map[SERVICE_START_SERVER].property_value = strdup (server_flag ? PROPERTY_ON : PROPERTY_OFF);
4776  us_Property_map[SERVICE_START_BROKER].property_value = strdup (broker_flag ? PROPERTY_ON : PROPERTY_OFF);
4777  us_Property_map[SERVICE_START_MANAGER].property_value = strdup (manager_flag ? PROPERTY_ON : PROPERTY_OFF);
4778  us_Property_map[SERVICE_START_HEARTBEAT].property_value = strdup (heartbeat_flag ? PROPERTY_ON : PROPERTY_OFF);
4779  us_Property_map[SERVICE_START_JAVASP].property_value = strdup (javasp_flag ? PROPERTY_ON : PROPERTY_OFF);
4780 
4781  /* get service::server list */
4783  if (value != NULL)
4784  {
4785  us_Property_map[SERVER_START_LIST].property_value = strdup (value);
4786  if (us_Property_map[SERVER_START_LIST].property_value == NULL)
4787  {
4788  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_OUT_OF_VIRTUAL_MEMORY, 1, (size_t) (strlen (value) + 1));
4789  return ER_OUT_OF_VIRTUAL_MEMORY;
4790  }
4791  }
4792  else
4793  {
4794  us_Property_map[SERVER_START_LIST].property_value = NULL;
4795  }
4796 
4797  return NO_ERROR;
4798 }
4799 
4800 /*
4801  * finalize_properties - free allocated memory by strdup ()
4802  *
4803  * return:
4804  *
4805  */
4806 static void
4808 {
4809  int i;
4810 
4811  for (i = 0; us_Property_map[i].property_index != -1; i++)
4812  {
4813  if (us_Property_map[i].property_value != NULL)
4814  {
4815  free_and_init (us_Property_map[i].property_value);
4816  }
4817  }
4818 }
4819 
4820 /*
4821  * get_property -
4822  *
4823  * return:
4824  *
4825  * property_type(in):
4826  *
4827  * NOTE:
4828  */
4829 static const char *
4830 get_property (int property_type)
4831 {
4832  return us_Property_map[property_type].property_value;
4833 }
4834 
4835 /*
4836  * is_terminated_process() - test if the process is terminated
4837  * return: true if the process is terminated, otherwise false
4838  * pid(in): process id
4839  */
4840 static bool
4842 {
4843 #if defined(WINDOWS)
4844  HANDLE h_process;
4845 
4846  h_process = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, pid);
4847  if (h_process == NULL)
4848  {
4849  return true;
4850  }
4851  else
4852  {
4853  CloseHandle (h_process);
4854  return false;
4855  }
4856 #else /* WINDOWS */
4857  if (kill (pid, 0) == -1)
4858  {
4859  return true;
4860  }
4861  else
4862  {
4863  return false;
4864  }
4865 #endif /* WINDOWS */
4866 }
#define PRINT_HA_PROCS_NAME
Definition: utility.h:853
DllImport int optind
#define UTIL_OPTION_ALTERDBHOST
Definition: utility.h:934
bool util_is_localhost(char *host)
Definition: util_common.c:418
const char * envvar_root(void)
#define UTIL_OPTION_VACUUMDB
Definition: utility.h:955
#define PRINT_CMD_SERVICE
Definition: utility.h:855
#define PRINT_CMD_LIST
Definition: utility.h:865
static UTIL_JAVASP_SERVER_STATUS_E is_javasp_running(const char *server_name)
#define NO_ERROR
Definition: error_code.h:46
DllImport char * optarg
#define UTIL_OPTION_CHANGEMODE
Definition: utility.h:943
#define TRUE
Definition: broker_admin.c:49
#define CHECK_HA_SERVER
Definition: utility.h:878
#define COMMAND_TYPE_STATUS
Definition: util_service.c:201
#define UTIL_CUB_MANAGER_NAME
Definition: utility.h:831
#define COMMDB_HB_DEACT_IMMEDIATELY_S
Definition: utility.h:1429
static int us_hb_applylogdb_stop(HA_CONF *ha_conf, const char *db_name, const char *node_name, const char *remote_host)
#define PRINT_CMD_APPLYLOGDB
Definition: utility.h:869
#define COMMDB_HA_DEREG_BY_ARGS
Definition: utility.h:885
#define COMMDB_HA_START_UTIL_PROCESS
Definition: utility.h:901
#define COMMAND_TYPE_RESTART
Definition: util_service.c:200
#define COMMAND_TYPE_ACL
Definition: util_service.c:207
#define UTIL_OPTION_LOADDB
Definition: utility.h:938
#define PRINT_CMD_TEST
Definition: utility.h:871
#define COMMDB_HB_DEACT_IMMEDIATELY_L
Definition: utility.h:1430
UTIL_SERVICE_INDEX_E
Definition: util_service.c:60
#define COMMAND_TYPE_DEREG
Definition: util_service.c:202
#define PRINT_HEARTBEAT_NAME
Definition: utility.h:851
#define PROPERTY_OFF
Definition: utility.h:843
#define UTIL_APPLYLOGDB
Definition: utility.h:839
#define PRINT_CMD_DEREG
Definition: utility.h:864
#define UTIL_TYPE_SERVER
Definition: util_service.c:139
static void print_result(const char *util_name, int status, int command_type)
Definition: util_service.c:406
#define COMMAND_TYPE_REPLICATION
Definition: util_service.c:214
#define UTIL_ADMIN_NAME
Definition: utility.h:832
int da_destroy(dynamic_array *da)
#define COMMAND_TYPE_REPLICATION_SHORT
Definition: util_service.c:215
int argc
Definition: dynamic_load.c:951
static int us_hb_applylogdb_start(dynamic_array *out_ap, HA_CONF *ha_conf, const char *db_name, const char *node_name, const char *remote_host)
#define UTIL_OPTION_KILLTRAN
Definition: utility.h:928
static int process_heartbeat_replication(HA_CONF *ha_conf, int argc, const char **argv)
#define ER_FAILED
Definition: error_code.h:47
#define UTIL_OPTION_GENERATE_LOCALE
Definition: utility.h:949
#define COMMDB_IS_REG
Definition: utility.h:887
#define COMMDB_HA_DEACT_STOP_ALL
Definition: utility.h:892
static int us_hb_copylogdb_stop(HA_CONF *ha_conf, const char *db_name, const char *node_name, const char *remote_host)
static int process_heartbeat_start(HA_CONF *ha_conf, int argc, const char **argv)
static int us_hb_deactivate(const char *hostname, bool immediate_stop)
static int load_properties(void)
void rel_copy_version_string(char *buf, size_t len)
static void hide_cmd_line_args(char **args)
Definition: util_service.c:955
static int process_heartbeat_deregister(int argc, const char **argv)
#define PRINT_CMD_GETID
Definition: utility.h:870
int util_log_write_command(int argc, char *argv[])
Definition: util_func.c:540
static bool are_all_services_running(unsigned int sleep_time, bool check_win_service)
int envvar_set(const char *name, const char *val)
#define UTIL_OPTION_RESTOREDB
Definition: utility.h:920
#define COMMDB_HA_PROC_LIST
Definition: utility.h:889
static int process_heartbeat(int command_type, int argc, const char **argv)
static const char ** Argv
Definition: util_service.c:250
static bool ha_make_mem_size(char *mem_size, int size, int value)
#define UTIL_MONITOR_NAME
Definition: utility.h:829
#define UTIL_OPTION_PATCHDB
Definition: utility.h:932
int getopt_long(int, char *const *, const char *, const struct option *, int *)
#define HB_STOP_WAITING_TIME_IN_SECS
Definition: heartbeat.h:57
static UTIL_SERVICE_OPTION_MAP_T us_Command_map[]
Definition: util_service.c:217
#define UTIL_OPTION_COPYDB
Definition: utility.h:917
#define UTIL_PID_ENVVAR_NAME
Definition: util_func.h:34
#define COMMAND_TYPE_LIST
Definition: util_service.c:203
#define PRINT_CMD_STATUS
Definition: utility.h:863
#define COMMDB_SERVER_STATUS
Definition: utility.h:881
#define UTIL_OPTION_BACKUPDB
Definition: utility.h:919
#define COMMDB_HA_PING_HOST_LIST
Definition: utility.h:890
#define UTIL_OPTION_DIAGDB
Definition: utility.h:931
static void finalize_properties(void)
#define COMMDB_HB_DEACT_IMMEDIATELY
Definition: utility.h:898
#define COMMDB_HA_DEACT_CONFIRM_STOP_ALL
Definition: utility.h:893
static int process_heartbeat_util(HA_CONF *ha_conf, int command_type, int argc, const char **argv)
#define PRINT_CMD_STOP
Definition: utility.h:862
#define MASK_ALL
Definition: utility.h:905
#define UTIL_JAVASP_NAME
Definition: utility.h:840
#define UTIL_TYPE_BROKER
Definition: util_service.c:140
UTIL_JAVASP_SERVER_STATUS_E
Definition: util_service.c:118
#define PRINT_CMD_RELOAD
Definition: utility.h:866
void prm_set_integer_value(PARAM_ID prm_id, int value)
bool css_does_master_exist(int port_id)
#define COMMDB_HA_NODE_LIST
Definition: utility.h:888
static int us_hb_server_start(HA_CONF *ha_conf, const char *db_name)
int util_log_write_errid(int message_id,...)
Definition: util_func.c:468
static int proc_execute_hide_cmd_args(const char *file, const char *args[], bool wait_child, bool close_output, bool close_err, int *pid)
Definition: util_service.c:762
#define UTIL_OPTION_STATDUMP
Definition: utility.h:942
#define UTIL_OPTION_CHECKDB
Definition: utility.h:933
#define COMMAND_TYPE_START
Definition: util_service.c:198
static int process_javasp_status(const char *db_name)
#define COMMDB_HA_ACTIVATE
Definition: utility.h:896
#define UTIL_OPTION_OPTIMIZEDB
Definition: utility.h:929
static int process_manager(int command_type, bool process_window_service)
#define MASK_ADMIN
Definition: utility.h:910
#define UTIL_MASTER_NAME
Definition: utility.h:825
static int us_hb_server_stop(HA_CONF *ha_conf, const char *db_name)
static int process_heartbeat_stop(HA_CONF *ha_conf, int argc, const char **argv)
#define UTIL_OPTION_PLANDUMP
Definition: utility.h:935
int util_log_write_result(int error)
Definition: util_func.c:448
static char * node_name
Definition: cas_runner.c:174
#define ACLDB_RELOAD
Definition: utility.h:903
#define PRINT_CMD_BROKER
Definition: utility.h:856
static int us_hb_process_start(HA_CONF *ha_conf, const char *db_name, bool check_result)
#define COMMAND_TYPE_INFO
Definition: util_service.c:209
#define REL_MAX_VERSION_LENGTH
#define UTIL_OPTION_CHECKSUMDB
Definition: utility.h:956
#define PRINT_MANAGER_NAME
Definition: utility.h:850
#define COMMDB_HA_ADMIN_INFO
Definition: utility.h:899
static int process_javasp(int command_type, int argc, const char **argv, bool process_window_service)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define UTIL_OPTION_TDE
Definition: utility.h:957
#define PRINT_MASTER_NAME
Definition: utility.h:847
const char * property_value
Definition: util_service.c:135
#define COMMDB_ALL_STATUS
Definition: utility.h:882
#define PRINT_CMD_SERVER
Definition: utility.h:858
#define UTIL_OPTION_APPLYINFO
Definition: utility.h:947
static int process_master(int command_type)
Definition: util_service.c:977
#define assert(x)
static int process_javasp_start(const char *db_name, bool process_window_service)
static void util_service_usage(int util_type)
Definition: util_service.c:712
static int input()
Definition: cnvlex.c:1661
#define MASK_SERVER
Definition: utility.h:907
#define UTIL_BROKER_NAME
Definition: utility.h:828
static int us_hb_status_get_options(bool *verbose, char *remote_host_name, int remote_host_name_size, int argc, const char **argv)
#define UTIL_OPTION_LOCKDB
Definition: utility.h:926
int prm_get_integer_value(PARAM_ID prm_id)
static bool check_all_services_status(unsigned int sleep_time, UTIL_ALL_SERVICES_STATUS expected_status, bool check_win_service)
#define ER_GENERIC_ERROR
Definition: error_code.h:49
int da_get(dynamic_array *da, int pos, void *data)
static int process_admin(int argc, char **argv)
Definition: util_service.c:447
#define COMMDB_VERBOSE_OUTPUT
Definition: utility.h:900
#define PRINT_CMD_START
Definition: utility.h:861
#define UTIL_TYPE_SERVICE
Definition: util_service.c:138
#define ER_OUT_OF_VIRTUAL_MEMORY
Definition: error_code.h:50
#define COMMDB_HA_DEACTIVATE
Definition: utility.h:895
#define HB_MAX_SZ_PROC_ARGS
Definition: heartbeat.h:83
static const char * command_string(int command_type)
Definition: util_service.c:360
#define COMMDB_SERVER_STOP
Definition: utility.h:880
static int us_hb_process_copylogdb(int command_type, HA_CONF *ha_conf, const char *db_name, const char *node_name, const char *remote_host)
static void print_message(FILE *output, int message_id,...)
Definition: util_service.c:429
static char executable_path[PATH_MAX]
Definition: server.c:73
#define COMMAND_TYPE_STOP
Definition: util_service.c:199
static int process_heartbeat_status(int argc, const char **argv)
static bool is_terminated_process(const int pid)
static int process_server(int command_type, int argc, char **argv, bool show_usage, bool check_ha_mode, bool process_window_service)
#define COMMDB_HA_DEREG_BY_PID
Definition: utility.h:884
#define PRINT_CMD_MANAGER
Definition: utility.h:857
const char * envvar_prefix(void)
static int us_hb_stop_get_options(char *db_name, int db_name_size, char *host_name, int host_name_size, bool *immediate_stop, int argc, const char **argv)
int sysprm_load_and_init(const char *db_name, const char *conf_file, const int load_flags)
int prm_get_master_port_id(void)
#define UTIL_TYPE_HB_SHORT
Definition: util_service.c:143
static enum scanner_mode mode
static int process_service(int command_type, bool process_window_service)
int da_size(dynamic_array *da)
#define UTIL_OPTION_RENAMEDB
Definition: utility.h:916
static bool ha_concat_db_and_host(char *db_host, int size, const char *db, const char *host)
static void util_service_version(const char *argv0)
Definition: util_service.c:737
#define UTIL_OPTION_PARAMDUMP
Definition: utility.h:941
#define COMMAND_TYPE_COPYLOGDB
Definition: util_service.c:210
#define UTIL_OPTION_SYNCCOLLDB
Definition: utility.h:951
#define UTIL_TYPE_HEARTBEAT
Definition: util_service.c:142
char * envvar_bindir_file(char *path, size_t size, const char *filename)
#define PRINT_CMD_JAVASP
Definition: utility.h:859
int main(int argc, char *argv[])
Definition: util_service.c:517
#define UTIL_TYPE_MANAGER
Definition: util_service.c:141
int cub_dirname_r(const char *path, char *pathbuf, size_t buflen)
Definition: porting.c:989
#define MASK_JAVASP
Definition: utility.h:912
#define PROPERTY_ON
Definition: utility.h:842
#define UTIL_OPTION_DELETEDB
Definition: utility.h:918
#define COMMDB_HOST_S
Definition: utility.h:1397
dynamic_array * da_create(int count, size_t len)
Definition: dynamic_array.c:30
void util_free_ha_conf(HA_CONF *ha_conf)
Definition: util_common.c:651
enum hb_proc_type HB_PROC_TYPE
Definition: heartbeat.h:67
#define NULL
Definition: freelistheap.h:34
#define MASK_BROKER
Definition: utility.h:908
static int process_javasp_stop(const char *db_name, bool process_window_service)
pid_t pid
Definition: dynamic_load.c:955
#define UTIL_OPTION_CREATEDB
Definition: utility.h:915
char * utility_make_getopt_optstring(const GETOPT_LONG *opt_array, char *buf)
Definition: util_support.c:49
char * copy_sync_mode
Definition: utility.h:801
static int ha_argv_to_args(char *args, int size, const char **argv, HB_PROC_TYPE type)
#define COMMAND_TYPE_RELOAD
Definition: util_service.c:204
UTIL_SERVICE_COMMAND_E
Definition: util_service.c:73
static bool are_all_services_stopped(unsigned int sleep_time, bool check_win_service)
#define UTIL_CUBRID_NAME
Definition: utility.h:827
static bool ha_is_registered(const char *args, const char *hostname)
#define PRINT_RESULT_SUCCESS
Definition: utility.h:874
static int us_hb_copylogdb_start(dynamic_array *out_ap, HA_CONF *ha_conf, const char *db_name, const char *node_name, const char *remote_host)
#define UTIL_OPTION_ACLDB
Definition: utility.h:948
char * db_name
#define HB_START_WAITING_TIME_IN_SECS
Definition: heartbeat.h:56
static char * make_exec_abspath(char *buf, int buf_len, char *cmd)
Definition: util_service.c:350
UTIL_SERVICE_PROPERTY_E
Definition: util_service.c:94
static bool ha_mkdir(const char *path, mode_t mode)
const char * utility_get_generic_message(int message_index)
Definition: util_common.c:88
#define UTIL_OPTION_DUMP_LOCALE
Definition: utility.h:950
#define COMMDB_HOST
Definition: utility.h:897
#define UTIL_OPTION_DUMP_TZ
Definition: utility.h:953
const char * envvar_get(const char *name)
#define UTIL_OPTION_INSTALLDB
Definition: utility.h:930
#define COMMAND_TYPE_TEST
Definition: util_service.c:213
static void error(const char *msg)
Definition: gencat.c:331
#define PRINT_SERVER_NAME
Definition: utility.h:848
#define UTIL_OPTION_COMPACTDB
Definition: utility.h:940
#define UTIL_OPTION_UNLOADDB
Definition: utility.h:939
#define ARG_FILE_LINE
Definition: error_manager.h:44
char ** db_names
Definition: utility.h:807
static int us_hb_process_applylogdb(int command_type, HA_CONF *ha_conf, const char *db_name, const char *node_name, const char *remote_host)
#define PRINT_RESULT_FAIL
Definition: utility.h:875
#define UTIL_COPYLOGDB
Definition: utility.h:838
char * envvar_vardir_file(char *path, size_t size, const char *filename)
UTIL_MANAGER_SERVER_STATUS_E
Definition: util_service.c:111
#define CHECK_SERVER
Definition: utility.h:877
#define COMMDB_ALL_STOP
Definition: utility.h:883
int da_add(dynamic_array *da, void *data)
Definition: dynamic_array.c:82
int css_windows_startup(void)
Definition: wintcp.c:94
#define UTIL_OPTION_GEN_TZ
Definition: utility.h:952
UTIL_ALL_SERVICES_STATUS
Definition: util_service.c:105
const char ** argv
Definition: dynamic_load.c:952
static int process_broker(int command_type, int argc, const char **argv, bool process_window_service)
#define free_and_init(ptr)
Definition: memory_alloc.h:147
#define strlen(s1)
Definition: intl_support.c:43
static bool is_server_running(const char *type, const char *server_name, int pid)
#define UTIL_OPTION_ESTIMATE_INDEX
Definition: utility.h:937
char * prm_get_string_value(PARAM_ID prm_id)
#define COMMDB_HOST_L
Definition: utility.h:1398
#define UTIL_TYPE_JAVASP
Definition: util_service.c:144
#define PRINT_SERVICE_NAME
Definition: utility.h:846
void css_windows_shutdown(void)
Definition: wintcp.c:140
static bool ha_make_log_path(char *path, int size, char *base, char *db, char *node)
#define MASK_SERVICE
Definition: utility.h:906
#define UTIL_OPTION_ESTIMATE_DATA
Definition: utility.h:936
#define PRINT_CMD_REPLICATION
Definition: utility.h:872
#define COMMAND_TYPE_OFF
Definition: util_service.c:206
HA_NODE_CONF * node_conf
Definition: utility.h:810
#define FALSE
Definition: broker_admin.c:50
int util_log_write_errstr(const char *format,...)
Definition: util_func.c:493
char * basename(const char *path)
Definition: porting.c:1132
static int process_heartbeat_reload(int argc, const char **argv)
static bool check_server(const char *type, const char *server_name)
#define MASK_HEARTBEAT
Definition: utility.h:911
#define PRINT_CMD_COPYLOGDB
Definition: utility.h:868
#define UTIL_TESTER_NAME
Definition: utility.h:830
int i
Definition: dynamic_load.c:954
#define UTIL_COMMDB_NAME
Definition: utility.h:826
static int ha_mode_in_common
Definition: util_service.c:251
static int parse_arg(UTIL_SERVICE_OPTION_MAP_T *option, const char *arg)
char * strdup(const char *str)
Definition: porting.c:901
#define UTIL_OPTION_RESTORESLAVE
Definition: utility.h:954
int util_get_ha_mode_for_sa_utils(void)
Definition: util_common.c:872
#define COMMAND_TYPE_GETID
Definition: util_service.c:212
static int us_hb_process_stop(HA_CONF *ha_conf, const char *db_name)
#define PRINT_CMD_ACL
Definition: utility.h:867
#define US_HB_DEREG_WAIT_TIME_IN_SEC
Definition: util_service.c:346
static const char * get_property(int property_type)
#define MASK_MANAGER
Definition: utility.h:909
void usage(void)
#define COMMAND_TYPE_APPLYLOGDB
Definition: util_service.c:211
static UTIL_MANAGER_SERVER_STATUS_E is_manager_running(unsigned int sleep_time)
#define COMMDB_HA_DEACT_CONFIRM_NO_SERVER
Definition: utility.h:894
static UTIL_SERVICE_PROPERTY_T us_Property_map[]
Definition: util_service.c:239
static int proc_execute(const char *file, const char *args[], bool wait_child, bool close_output, bool close_err, int *pid)
Definition: util_service.c:751
#define CUB_MAXHOSTNAMELEN
Definition: porting.h:379
#define COMMAND_TYPE_ON
Definition: util_service.c:205
static int us_hb_util_get_options(char *db_name, int db_name_size, char *node_name, int node_name_size, char *remote_host_name, int remote_host_name_size, int argc, const char **argv)
static char * host
static UTIL_SERVICE_OPTION_MAP_T us_Service_map[]
Definition: util_service.c:146
int util_make_ha_conf(HA_CONF *ha_conf)
Definition: util_common.c:694
#define COMMDB_HA_RELOAD
Definition: utility.h:891
static int util_get_command_option_mask(int command_type)
Definition: util_service.c:495
static int is_broker_running(void)
#define UTIL_OPTION_TRANLIST
Definition: utility.h:927
int num_node_conf
Definition: utility.h:809
#define UTIL_OPTION_SPACEDB
Definition: utility.h:925
static int proc_execute_internal(const char *file, const char *args[], bool wait_child, bool close_output, bool close_err, bool hide_cmd_args, int *pid)
Definition: util_service.c:856
#define COMMAND_TYPE_RESET
Definition: util_service.c:208
static int util_get_service_option_mask(int util_type)
Definition: util_service.c:474
#define PRINT_JAVASP_NAME
Definition: utility.h:852
#define UTIL_OPTION_ADDVOLDB
Definition: utility.h:921
#define PRINT_BROKER_NAME
Definition: utility.h:849