CUBRID Engine  latest
util_common.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_common.c - utility common functions
21  */
22 
23 #ident "$Id$"
24 
25 #include <ctype.h>
26 #include <assert.h>
27 #if !defined(WINDOWS)
28 #include <fcntl.h>
29 #endif /* !defined(WINDOWS) */
30 
31 #include "config.h"
32 
33 #include "utility.h"
34 #include "util_func.h"
35 #include "porting.h"
36 #include "message_catalog.h"
37 #include "log_common_impl.h"
38 #include "log_writer.h"
39 #include "mprec.h"
40 #include "system_parameter.h"
41 #include "environment_variable.h"
42 #include "heartbeat.h"
43 #if defined (WINDOWS)
44 #include "wintcp.h"
45 #else
46 #include "tcp.h"
47 #endif
48 
49 typedef enum
50 {
54 
55 static int utility_get_option_index (UTIL_ARG_MAP * arg_map, int arg_ch);
56 static int check_database_name_local (const char *name, int existing_or_new_db);
57 static char **util_split_ha_node (const char *str);
58 static char **util_split_ha_db (const char *str);
59 static char **util_split_ha_sync (const char *str);
60 static int util_get_ha_parameters (char **ha_node_list_p, char **ha_db_list_p, char **ha_sync_mode_p,
61  const char **ha_copy_log_base_p, int *ha_max_mem_size_p);
62 static bool util_is_replica_node (void);
63 
64 /*
65  * utility_initialize() - initialize cubrid library
66  * return: 0 if success, otherwise -1
67  */
68 int
70 {
72 
73  if (msgcat_init () != NO_ERROR)
74  {
75  PRINT_AND_LOG_ERR_MSG ("Unable to access system message catalog.\n");
77  }
78 
79  return NO_ERROR;
80 }
81 
82 /*
83  * utility_get_generic_message() - get a string of the generic-utility from the catalog
84  * return: message string
85  * message_index(in): an index of the message string
86  */
87 const char *
88 utility_get_generic_message (int message_index)
89 {
91 }
92 
93 /*
94  * check_database_name() - check validation of the name of a database for existing db
95  * return: error code
96  * name(in): the name of a database
97  */
98 int
99 check_database_name (const char *name)
100 {
102 }
103 
104 /*
105  * check_database_name() - check validation of the name of a database for new db
106  * return: error code
107  * name(in): the name of a database
108  */
109 int
110 check_new_database_name (const char *name)
111 {
113 }
114 
115 /*
116  * check_database_name_local() - check validation of the name of a database
117  * return: error code
118  * name(in): the name of a database
119  * existing_or_new_db(in): whether db is existing or new one
120  */
121 static int
122 check_database_name_local (const char *name, int existing_or_new_db)
123 {
124  int status = NO_ERROR;
125  int i = 0;
126 
127  if (name[0] == '#')
128  {
129  status = ER_GENERIC_ERROR;
130  }
131  else
132  {
133  for (i = 0; name[i] != 0; i++)
134  {
135  if (isspace (name[i]) || name[i] == '/' || name[i] == '\\' || !isprint (name[i])
136  || (existing_or_new_db == NEW_DATABASE && name[i] == '@'))
137  {
138  status = ER_GENERIC_ERROR;
139  break;
140  }
141  }
142  }
143 
144  if (status == ER_GENERIC_ERROR)
145  {
147  if (message != NULL)
148  {
149  PRINT_AND_LOG_ERR_MSG (message, name[i], name);
150  }
151  }
152  return status;
153 }
154 
155 /*
156  * check_volume_name() - check validation of the name of a volume
157  * return: error code
158  * name(in): the name of a volume
159  */
160 int
161 check_volume_name (const char *name)
162 {
163  int status = NO_ERROR;
164  int i = 0;
165 
166  if (name == NULL)
167  {
168  return NO_ERROR;
169  }
170 
171  if (name[0] == '#')
172  {
173  status = ER_GENERIC_ERROR;
174  }
175  else
176  {
177  for (i = 0; name[i] != 0; i++)
178  {
179  if (isspace (name[i]) || name[i] == '/' || name[i] == '\\' || !isprint (name[i]))
180  {
181  status = ER_GENERIC_ERROR;
182  break;
183  }
184  }
185  }
186 
187  if (status == ER_GENERIC_ERROR)
188  {
190  if (message != NULL)
191  {
192  PRINT_AND_LOG_ERR_MSG (message, name[i], name);
193  }
194  }
195  return status;
196 }
197 
198 /*
199  * utility_get_option_index() - search an option in the map of arguments
200  * return: an index of a founded option
201  * arg_map(in): the map of arguments
202  * arg_ch(in): the value of an argument
203  */
204 static int
205 utility_get_option_index (UTIL_ARG_MAP * arg_map, int arg_ch)
206 {
207  int i;
208 
209  for (i = 0; arg_map[i].arg_ch; i++)
210  {
211  if (arg_map[i].arg_ch == arg_ch)
212  {
213  return i;
214  }
215  }
216  return -1;
217 }
218 
219 /*
220  * utility_get_option_int_value() - search an option in the map of arguments
221  * and return that value
222  * return: the value of a searched argument
223  * arg_map(in): the map of arguments
224  * arg_ch(in): the value of an argument
225  */
226 int
228 {
229  int index = utility_get_option_index (arg_map, arg_ch);
230  if (index != -1)
231  {
232  return arg_map[index].arg_value.i;
233  }
234  return 0;
235 }
236 
237 /*
238  * get_option_bool_value() - search an option in the map of arguments
239  * and return that value
240  * return: the value of a searched argument
241  * arg_map(in): the map of arguments
242  * arg_ch(in): the value of an argument
243  */
244 bool
246 {
247  int index = utility_get_option_index (arg_map, arg_ch);
248  if (index != -1)
249  {
250  if (arg_map[index].arg_value.i == 1)
251  {
252  return true;
253  }
254  }
255  return false;
256 }
257 
258 /*
259  * get_option_string_value() - search an option in the map of arguments
260  * and return that value
261  * return: the value of a searched argument
262  * arg_map(in): the map of arguments
263  * arg_ch(in): the value of an argument
264  */
265 char *
267 {
268  int arg_index = utility_get_option_index (arg_map, arg_ch);
269  if (arg_index != -1)
270  {
271  if (arg_ch == OPTION_STRING_TABLE)
272  {
273  if (index < arg_map[arg_index].value_info.num_strings)
274  {
275  return (((char **) arg_map[arg_index].arg_value.p)[index]);
276  }
277  }
278  else
279  {
280  return ((char *) arg_map[arg_index].arg_value.p);
281  }
282  }
283  return NULL;
284 }
285 
286 /*
287  * utility_get_option_bigint_value() - search an option in the map of arguments
288  * and return that value
289  * return: the value of a searched argument
290  * arg_map(in): the map of arguments
291  * arg_ch(in): the value of an argument
292  */
293 INT64
295 {
296  int index = utility_get_option_index (arg_map, arg_ch);
297  if (index != -1)
298  {
299  return arg_map[index].arg_value.l;
300  }
301  return 0;
302 }
303 
304 int
306 {
307  int arg_index = utility_get_option_index (arg_map, OPTION_STRING_TABLE);
308  if (arg_index != -1)
309  {
310  return arg_map[arg_index].value_info.num_strings;
311  }
312  return 0;
313 }
314 
315 /*
316  * fopen_ex - open a file for variable architecture
317  * return: FILE *
318  * filename(in): path to the file to open
319  * type(in): open type
320  */
321 FILE *
322 fopen_ex (const char *filename, const char *type)
323 {
324 #if defined(SOLARIS)
325  size_t r = 0;
326  char buf[1024];
327 
328  extern size_t confstr (int, char *, size_t);
329  r = confstr (_CS_LFS_CFLAGS, buf, 1024);
330 
331  if (r > 0)
332  return fopen64 (filename, type);
333  else
334  return fopen (filename, type);
335 #elif defined(HPUX)
336 #if _LFS64_LARGEFILE == 1
337  return fopen64 (filename, type);
338 #else
339  return fopen (filename, type);
340 #endif
341 #elif defined(AIX) || (defined(I386) && defined(LINUX))
342  return fopen64 (filename, type);
343 #else /* NT, ALPHA_OSF, and the others */
344  return fopen (filename, type);
345 #endif
346 }
347 
348 /*
349  * utility_keyword_search
350  */
351 int
352 utility_keyword_search (UTIL_KEYWORD * keywords, int *keyval_p, char **keystr_p)
353 {
354  UTIL_KEYWORD *keyp;
355 
356  if (*keyval_p >= 0 && *keystr_p == NULL)
357  {
358  /* get keyword string from keyword value */
359  for (keyp = keywords; keyp->keyval >= 0; keyp++)
360  {
361  if (*keyval_p == keyp->keyval)
362  {
363  *keystr_p = const_cast < char *>(keyp->keystr);
364  return NO_ERROR;
365  }
366  }
367  }
368  else if (*keyval_p < 0 && *keystr_p != NULL)
369  {
370  /* get keyword value from keyword string */
371  for (keyp = keywords; keyp->keystr != NULL; keyp++)
372  {
373  if (!strcasecmp (*keystr_p, keyp->keystr))
374  {
375  *keyval_p = keyp->keyval;
376  return NO_ERROR;
377  }
378  }
379  }
380  return ER_FAILED;
381 }
382 
383 /*
384  * utility_localtime - transform date and time to broken-down time
385  * return: 0 if success, otherwise -1
386  * ts(in): pointer of time_t data value
387  * result(out): pointer of struct tm which will store the broken-down time
388  */
389 int
390 utility_localtime (const time_t * ts, struct tm *result)
391 {
392  struct tm *tm_p, tm_val;
393 
394  if (result == NULL)
395  {
396  return -1;
397  }
398 
399  tm_p = localtime_r (ts, &tm_val);
400  if (tm_p == NULL)
401  {
402  memset (result, 0, sizeof (struct tm));
403  return -1;
404  }
405 
406  memcpy (result, tm_p, sizeof (struct tm));
407  return 0;
408 }
409 
410 /*
411  * util_is_localhost -
412  *
413  * return:
414  *
415  * NOTE:
416  */
417 bool
419 {
420  char localhost[CUB_MAXHOSTNAMELEN];
421  GETHOSTNAME (localhost, CUB_MAXHOSTNAMELEN);
422 
423  return are_hostnames_equal (host, localhost);
424 }
425 
448 bool
449 are_hostnames_equal (const char *hostname_a, const char *hostname_b)
450 {
451  const char *a;
452  const char *b;
453 
454  for (a = hostname_a, b = hostname_b; *a && *b && (*a == *b); ++a, ++b)
455  ;
456 
457  if (*a == '\0' && *b != '\0')
458  {
459  return *b == '.';
460  }
461  else if (*a != '\0' && *b == '\0')
462  {
463  return *a == '.';
464  }
465  else
466  {
467  return *a == *b;
468  }
469 }
470 
471 /*
472  * util_get_num_of_ha_nodes - counter the number of nodes
473  * in either ha_node_list or ha_replica_list
474  * return: the number of nodes in a node list
475  * node_list(in): ha_node_list or ha_replica_list
476  */
477 int
478 util_get_num_of_ha_nodes (const char *node_list)
479 {
480  char **ha_node_list_pp = NULL;
481  int num_of_nodes = 0;
482 
483  if (node_list == NULL)
484  {
485  return 0;
486  }
487  if ((ha_node_list_pp = util_split_ha_node (node_list)) != NULL)
488  {
489  for (num_of_nodes = 0; ha_node_list_pp[num_of_nodes] != NULL;)
490  {
491  num_of_nodes++;
492  }
493  }
494 
495  if (ha_node_list_pp)
496  {
497  util_free_string_array (ha_node_list_pp);
498  }
499 
500  return num_of_nodes;
501 }
502 
503 static char **
504 util_split_ha_node (const char *str)
505 {
506  char *start_node;
507 
508  start_node = (char *) strchr (str, '@');
509  if (start_node == NULL || str == start_node)
510  {
511  return NULL;
512  }
513 
514  return util_split_string (start_node + 1, " ,:");
515 }
516 
517 static char **
518 util_split_ha_db (const char *str)
519 {
520  return util_split_string (str, " ,:");
521 }
522 
523 static char **
524 util_split_ha_sync (const char *str)
525 {
526  return util_split_string (str, " ,:");
527 }
528 
529 /*
530  * copylogdb_keyword() - get keyword value or string of the copylogdb mode
531  * return: NO_ERROR or ER_FAILED
532  * keyval_p(in/out): keyword value
533  * keystr_p(in/out): keyword string
534  */
535 int
536 copylogdb_keyword (int *keyval_p, char **keystr_p)
537 {
538  static UTIL_KEYWORD keywords[] = {
539  {LOGWR_MODE_ASYNC, "async"},
540  {LOGWR_MODE_SEMISYNC, "semisync"},
541  {LOGWR_MODE_SYNC, "sync"},
542  {-1, NULL}
543  };
544 
545  return utility_keyword_search (keywords, keyval_p, keystr_p);
546 }
547 
548 /*
549  * changemode_keyword() - get keyword value or string of the server mode
550  * return: NO_ERROR or ER_FAILED
551  * keyval_p(in/out): keyword value
552  * keystr_p(in/out): keyword string
553  */
554 int
555 changemode_keyword (int *keyval_p, char **keystr_p)
556 {
557  static UTIL_KEYWORD keywords[] = {
565  {-1, NULL}
566  };
567 
568  return utility_keyword_search (keywords, keyval_p, keystr_p);
569 }
570 
571 static int
572 util_get_ha_parameters (char **ha_node_list_p, char **ha_db_list_p, char **ha_sync_mode_p,
573  const char **ha_copy_log_base_p, int *ha_max_mem_size_p)
574 {
575  int error = NO_ERROR;
576 
577  *(ha_db_list_p) = prm_get_string_value (PRM_ID_HA_DB_LIST);
578  if (*(ha_db_list_p) == NULL || **(ha_db_list_p) == '\0')
579  {
581  fprintf (stderr, message, prm_get_name (PRM_ID_HA_DB_LIST), "");
582  return ER_GENERIC_ERROR;
583  }
584 
585  *(ha_node_list_p) = prm_get_string_value (PRM_ID_HA_NODE_LIST);
586  if (*(ha_node_list_p) == NULL || **(ha_node_list_p) == '\0')
587  {
589  fprintf (stderr, message, prm_get_name (PRM_ID_HA_NODE_LIST), "");
590  return ER_GENERIC_ERROR;
591  }
592 
593  *(ha_sync_mode_p) = prm_get_string_value (PRM_ID_HA_COPY_SYNC_MODE);
594  *(ha_max_mem_size_p) = prm_get_integer_value (PRM_ID_HA_APPLY_MAX_MEM_SIZE);
595 
596  *(ha_copy_log_base_p) = prm_get_string_value (PRM_ID_HA_COPY_LOG_BASE);
597  if (*(ha_copy_log_base_p) == NULL || **(ha_copy_log_base_p) == '\0')
598  {
599  *(ha_copy_log_base_p) = (char *) envvar_get ("DATABASES");
600  if (*(ha_copy_log_base_p) == NULL)
601  {
602  *(ha_copy_log_base_p) = ".";
603  }
604  }
605 
606  return error;
607 }
608 
609 static bool
611 {
612  bool is_replica_node = false;
613  int i;
614  char local_host_name[CUB_MAXHOSTNAMELEN];
615  char *ha_replica_list_p, **ha_replica_list_pp = NULL;
616 
617  ha_replica_list_p = prm_get_string_value (PRM_ID_HA_REPLICA_LIST);
618  if (ha_replica_list_p != NULL && *(ha_replica_list_p) != '\0')
619  {
620  ha_replica_list_pp = util_split_ha_node (ha_replica_list_p);
621  if (ha_replica_list_pp != NULL && (GETHOSTNAME (local_host_name, sizeof (local_host_name)) == 0))
622  {
623  for (i = 0; ha_replica_list_pp[i] != NULL; i++)
624  {
625  if (strcmp (ha_replica_list_pp[i], local_host_name) == 0)
626  {
627  is_replica_node = true;
628  break;
629  }
630  }
631 
632  }
633  }
634 
635  if (ha_replica_list_pp)
636  {
637  util_free_string_array (ha_replica_list_pp);
638  }
639 
640  return is_replica_node;
641 }
642 
643 /*
644  * util_free_ha_conf -
645  *
646  * return:
647  *
648  * NOTE:
649  */
650 void
652 {
653  int i;
654  HA_NODE_CONF *nc;
655 
656  for (i = 0, nc = ha_conf->node_conf; i < ha_conf->num_node_conf; i++)
657  {
658  if (nc[i].node_name)
659  {
660  free_and_init (nc[i].node_name);
661  }
662 
663  if (nc[i].copy_log_base)
664  {
665  free_and_init (nc[i].copy_log_base);
666  }
667 
668  if (nc[i].copy_sync_mode)
669  {
670  free_and_init (nc[i].copy_sync_mode);
671  }
672  }
673  free_and_init (ha_conf->node_conf);
674  ha_conf->num_node_conf = 0;
675  ha_conf->node_conf = NULL;
676 
677  if (ha_conf->db_names)
678  {
679  util_free_string_array (ha_conf->db_names);
680  ha_conf->db_names = NULL;
681  }
682 
683  return;
684 }
685 
686 /*
687  * util_make_ha_conf -
688  *
689  * return:
690  *
691  * NOTE:
692  */
693 int
695 {
696  int error = NO_ERROR;
697  int i, num_ha_nodes;
698  char *ha_db_list_p = NULL;
699  char *ha_node_list_p = NULL, **ha_node_list_pp = NULL;
700  char *ha_sync_mode_p = NULL, **ha_sync_mode_pp = NULL;
701  const char *ha_copy_log_base_p;
702  int ha_max_mem_size;
703  bool is_replica_node;
704 
705  error =
706  util_get_ha_parameters (&ha_node_list_p, &ha_db_list_p, &ha_sync_mode_p, &ha_copy_log_base_p, &ha_max_mem_size);
707  if (error != NO_ERROR)
708  {
709  return error;
710  }
711 
712  ha_conf->db_names = util_split_ha_db (ha_db_list_p);
713  if (ha_conf->db_names == NULL)
714  {
716  fprintf (stderr, message);
717 
718  error = ER_GENERIC_ERROR;
719  goto ret;
720  }
721 
722  ha_node_list_pp = util_split_ha_node (ha_node_list_p);
723  if (ha_node_list_pp == NULL)
724  {
726  fprintf (stderr, message);
727 
728  error = ER_GENERIC_ERROR;
729  goto ret;
730  }
731 
732  for (i = 0; ha_node_list_pp[i] != NULL;)
733  {
734  i++;
735  }
736  num_ha_nodes = i;
737 
738  ha_conf->node_conf = (HA_NODE_CONF *) malloc (sizeof (HA_NODE_CONF) * num_ha_nodes);
739  if (ha_conf->node_conf == NULL)
740  {
742  fprintf (stderr, message);
743 
744  error = ER_GENERIC_ERROR;
745  goto ret;
746  }
747  memset ((void *) ha_conf->node_conf, 0, sizeof (HA_NODE_CONF) * num_ha_nodes);
748  ha_conf->num_node_conf = num_ha_nodes;
749 
750  /* set ha_sync_mode */
751  is_replica_node = util_is_replica_node ();
752  if (is_replica_node == true)
753  {
754  for (i = 0; i < num_ha_nodes; i++)
755  {
756  ha_conf->node_conf[i].copy_sync_mode = strdup ("async");
757  if (ha_conf->node_conf[i].copy_sync_mode == NULL)
758  {
760  fprintf (stderr, message);
761 
762  error = ER_GENERIC_ERROR;
763  goto ret;
764  }
765  }
766  }
767  else
768  {
769  if (ha_sync_mode_p == NULL || *(ha_sync_mode_p) == '\0')
770  {
771  for (i = 0; i < num_ha_nodes; i++)
772  {
773  ha_conf->node_conf[i].copy_sync_mode = strdup ("sync");
774  if (ha_conf->node_conf[i].copy_sync_mode == NULL)
775  {
777  fprintf (stderr, message);
778 
779  error = ER_GENERIC_ERROR;
780  goto ret;
781  }
782  }
783  }
784  else
785  {
786  int mode;
787 
788  ha_sync_mode_pp = util_split_ha_sync (ha_sync_mode_p);
789  if (ha_sync_mode_pp == NULL)
790  {
792  fprintf (stderr, message);
793 
794  error = ER_GENERIC_ERROR;
795  goto ret;
796  }
797 
798  for (i = 0; i < num_ha_nodes; i++)
799  {
800  mode = -1;
801  if (ha_sync_mode_pp[i] == NULL || copylogdb_keyword (&mode, &ha_sync_mode_pp[i]) == -1)
802  {
804 
805  fprintf (stderr, message, prm_get_name (PRM_ID_HA_COPY_SYNC_MODE),
806  (ha_sync_mode_pp[i]) ? ha_sync_mode_pp[i] : "");
807 
808  error = ER_GENERIC_ERROR;
809  goto ret;
810  }
811 
812  ha_conf->node_conf[i].copy_sync_mode = strdup (ha_sync_mode_pp[i]);
813  if (ha_conf->node_conf[i].copy_sync_mode == NULL)
814  {
816  fprintf (stderr, message);
817 
818  error = ER_GENERIC_ERROR;
819  goto ret;
820  }
821  }
822  }
823  }
824 
825  for (i = 0; i < num_ha_nodes; i++)
826  {
827  assert_release (ha_node_list_pp[i] != NULL);
828 
829  ha_conf->node_conf[i].node_name = strdup (ha_node_list_pp[i]);
830  ha_conf->node_conf[i].copy_log_base = strdup (ha_copy_log_base_p);
831  ha_conf->node_conf[i].apply_max_mem_size = ha_max_mem_size;
832 
833  if (ha_conf->node_conf[i].node_name == NULL || ha_conf->node_conf[i].copy_log_base == NULL)
834  {
836  fprintf (stderr, message);
837 
838  error = ER_GENERIC_ERROR;
839  goto ret;
840  }
841  }
842 
843 ret:
844  if (ha_node_list_pp)
845  {
846  util_free_string_array (ha_node_list_pp);
847  ha_node_list_pp = NULL;
848  }
849 
850  if (ha_sync_mode_pp)
851  {
852  util_free_string_array (ha_sync_mode_pp);
853  ha_sync_mode_pp = NULL;
854  }
855 
856  if (error != NO_ERROR)
857  {
858  util_free_ha_conf (ha_conf);
859  }
860 
861  return error;
862 }
863 
864 /*
865  * util_get_ha_mode_for_sa_utils -
866  *
867  * return:
868  *
869  * NOTE:
870  */
871 int
873 {
875 }
876 
877 #if !defined(WINDOWS)
878 /*
879  * util_redirect_stdout_to_null - redirect stdout/stderr to /dev/null
880  *
881  * return:
882  *
883  */
884 void
886 {
887  const char *null_dev = "/dev/null";
888  int fd;
889 
890  fd = open (null_dev, O_WRONLY);
891  if (fd != -1)
892  {
893  close (1);
894  close (2);
895  dup2 (fd, 1);
896  dup2 (fd, 2);
897  close (fd);
898  }
899 }
900 #endif /* !defined(WINDOWS) */
901 
902 /*
903  * util_size_to_byte -
904  *
905  * return:
906  *
907  */
908 static int
909 util_size_to_byte (double *pre, const char *post)
910 {
911  if (strcasecmp (post, "b") == 0)
912  {
913  /* bytes */
914  }
915  else if ((strcasecmp (post, "k") == 0) || (strcasecmp (post, "kb") == 0))
916  {
917  /* kilo */
918  *pre = *pre * ONE_K;
919  }
920  else if ((strcasecmp (post, "m") == 0) || (strcasecmp (post, "mb") == 0))
921  {
922  /* mega */
923  *pre = *pre * ONE_M;
924  }
925  else if ((strcasecmp (post, "g") == 0) || (strcasecmp (post, "gb") == 0))
926  {
927  /* giga */
928  *pre = *pre * ONE_G;
929  }
930  else if ((strcasecmp (post, "t") == 0) || (strcasecmp (post, "tb") == 0))
931  {
932  /* tera */
933  *pre = *pre * ONE_T;
934  }
935  else if ((strcasecmp (post, "p") == 0) || (strcasecmp (post, "pb") == 0))
936  {
937  /* peta */
938  *pre = *pre * ONE_P;
939  }
940  else
941  {
942  return ER_FAILED;
943  }
944 
945  return NO_ERROR;
946 }
947 
948 /*
949  * util_byte_to_size_string -
950  *
951  * return:
952  *
953  */
954 int
955 util_byte_to_size_string (char *buf, size_t len, UINT64 size_num)
956 {
957  char num_str[100];
958  const char *ss = "BKMGTP";
959  double v = (double) size_num;
960  int pow = 0;
961  int i, decpt, sign, num_len;
962  char *rve;
963 
964  if (buf == NULL)
965  {
966  return ER_FAILED;
967  }
968  buf[0] = '\0';
969 
970  while (pow < 6 && v >= ONE_K)
971  {
972  pow++;
973  v /= ONE_K;
974  }
975 
976  _dtoa (v, 3, 1, &decpt, &sign, &rve, num_str, 0);
977  num_str[99] = '\0';
978  num_len = (int) strlen (num_str);
979 
980  if (len < (size_t) (decpt + 4))
981  {
982  return ER_FAILED;
983  }
984 
985  for (i = 0; i <= decpt + 1; i++)
986  {
987  if (i == decpt)
988  {
989  buf[i] = '.';
990  }
991  else if (i == decpt + 1)
992  {
993  if (num_len > decpt)
994  {
995  buf[i] = num_str[num_len - 1];
996  }
997  else
998  {
999  buf[i] = '0';
1000  }
1001  buf[i + 1] = ss[pow];
1002  buf[i + 2] = '\0';
1003  }
1004  else
1005  {
1006  if (num_len < decpt && i >= num_len)
1007  {
1008  buf[i] = '0';
1009  }
1010  else
1011  {
1012  buf[i] = num_str[i];
1013  }
1014  }
1015  }
1016 
1017  return NO_ERROR;
1018 }
1019 
1020 /*
1021  * util_size_string_to_byte -
1022  *
1023  * return:
1024  *
1025  */
1026 int
1027 util_size_string_to_byte (UINT64 * size_num, const char *size_str)
1028 {
1029  double val;
1030  const char *default_unit = "B";
1031  char *end;
1032  const char *size_unit;
1033 
1034  if (size_str == NULL || size_num == NULL)
1035  {
1036  return ER_FAILED;
1037  }
1038  *size_num = 0;
1039 
1040  val = strtod (size_str, &end);
1041  if (end == size_str)
1042  {
1043  return ER_FAILED;
1044  }
1045 
1046  if (val < 0)
1047  {
1048  return ER_FAILED;
1049  }
1050 
1051  if (*end != '\0')
1052  {
1053  size_unit = end;
1054  }
1055  else
1056  {
1057  size_unit = default_unit;
1058  }
1059 
1060  if (util_size_to_byte (&val, size_unit) != NO_ERROR)
1061  {
1062  return ER_FAILED;
1063  }
1064 
1065  *size_num = (UINT64) val;
1066  return NO_ERROR;
1067 }
1068 
1069 /*
1070  * util_time_to_byte -
1071  *
1072  * return:
1073  *
1074  */
1075 static int
1076 util_time_to_msec (double *pre, const char *post)
1077 {
1078  if ((strcasecmp (post, "ms") == 0) || (strcasecmp (post, "msec") == 0))
1079  {
1080  /* millisecond */
1081  }
1082  else if ((strcasecmp (post, "s") == 0) || (strcasecmp (post, "sec") == 0))
1083  {
1084  /* second */
1085  *pre = *pre * ONE_SEC;
1086  }
1087  else if (strcasecmp (post, "min") == 0)
1088  {
1089  /* minute */
1090  *pre = *pre * ONE_MIN;
1091  }
1092  else if (strcasecmp (post, "h") == 0)
1093  {
1094  /* hours */
1095  *pre = *pre * ONE_HOUR;
1096  }
1097  else
1098  {
1099  return ER_FAILED;
1100  }
1101 
1102  return NO_ERROR;
1103 }
1104 
1105 /*
1106  * util_msec_to_time_string -
1107  *
1108  * return:
1109  *
1110  */
1111 int
1112 util_msec_to_time_string (char *buf, size_t len, INT64 msec_num)
1113 {
1114  INT64 v = msec_num;
1115  INT64 sec, msec;
1116  int error = 0;
1117 
1118  if (buf == NULL)
1119  {
1120  return ER_FAILED;
1121  }
1122  buf[0] = '\0';
1123 
1124  sec = v / ONE_SEC;
1125 
1126  if (sec > 0)
1127  {
1128  msec = v % ONE_SEC;
1129  error = snprintf (buf, len, "%lld.%03lld sec", (long long) sec, (long long) msec);
1130  }
1131  else if (v < 0)
1132  {
1133  error = snprintf (buf, len, "%lld", (long long) v);
1134  }
1135  else
1136  {
1137  error = snprintf (buf, len, "%lld msec", (long long) v);
1138  }
1139 
1140  if (error < 0)
1141  {
1142  return ER_FAILED;
1143  }
1144 
1145  return NO_ERROR;
1146 }
1147 
1148 /*
1149  * util_time_string_to_msec -
1150  *
1151  * return:
1152  *
1153  */
1154 int
1155 util_time_string_to_msec (INT64 * msec_num, char *time_str)
1156 {
1157  double val;
1158  const char *default_unit = "ms";
1159  char *end;
1160  const char *time_unit;
1161 
1162  if (time_str == NULL || msec_num == NULL)
1163  {
1164  return ER_FAILED;
1165  }
1166  *msec_num = 0;
1167 
1168  val = strtod (time_str, &end);
1169  if (end == time_str)
1170  {
1171  return ER_FAILED;
1172  }
1173 
1174  if (val < 0)
1175  {
1176  *msec_num = (INT64) val;
1177  return NO_ERROR;
1178  }
1179 
1180  if (*end != '\0')
1181  {
1182  time_unit = end;
1183  }
1184  else
1185  {
1186  time_unit = default_unit;
1187  }
1188 
1189  if (util_time_to_msec (&val, time_unit) != NO_ERROR)
1190  {
1191  return ER_FAILED;
1192  }
1193 
1194  *msec_num = (INT64) val;
1195  return NO_ERROR;
1196 }
1197 
1198 /*
1199  * util_print_deprecated -
1200  *
1201  * return:
1202  *
1203  */
1204 void
1206 {
1207  int cat = MSGCAT_CATALOG_UTILS;
1208  int set = MSGCAT_UTIL_SET_GENERIC;
1210  const char *fmt = msgcat_message (cat, set, msg);
1211  if (fmt == NULL)
1212  {
1213  fprintf (stderr, "error: msgcat_message");
1214  }
1215  else
1216  {
1217  fprintf (stderr, fmt, option);
1218  }
1219 }
1220 
1221 /*
1222  * util_get_table_list_from_file() -
1223  * return: NO_ERROR/ER_GENERIC_ERROR
1224  */
1225 int
1227 {
1228  int c, i, p;
1229  char name[SM_MAX_IDENTIFIER_LENGTH];
1230  FILE *fp = fopen (fname, "r");
1231 
1232  if (fp == NULL)
1233  {
1235  return ER_GENERIC_ERROR;
1236  }
1237 
1238  i = p = 0;
1239  while (1)
1240  {
1241  c = fgetc (fp);
1242  if (c == ' ' || c == '\t' || c == ',' || c == '\n' || c == EOF)
1243  {
1244  if (p != 0)
1245  {
1246  name[p] = '\0';
1247  if (da_add (darray, name) != NO_ERROR)
1248  {
1249  fclose (fp);
1251  return ER_GENERIC_ERROR;
1252  }
1253  i++;
1254  p = 0;
1255  }
1256  if (c == EOF)
1257  {
1258  break;
1259  }
1260  continue;
1261  }
1262  name[p++] = c;
1263  if (p == SM_MAX_IDENTIFIER_LENGTH)
1264  {
1265  /* too long table name */
1267  fclose (fp);
1268  return ER_GENERIC_ERROR;
1269  }
1270  }
1271  fclose (fp);
1272 
1273  return NO_ERROR;
1274 }
bool util_is_localhost(char *host)
Definition: util_common.c:418
static int util_get_ha_parameters(char **ha_node_list_p, char **ha_db_list_p, char **ha_sync_mode_p, const char **ha_copy_log_base_p, int *ha_max_mem_size_p)
Definition: util_common.c:572
#define NO_ERROR
Definition: error_code.h:46
static bool util_is_replica_node(void)
Definition: util_common.c:610
#define HA_SERVER_STATE_IDLE_STR
Definition: boot.h:127
#define PRINT_AND_LOG_ERR_MSG(...)
Definition: util_func.h:49
void util_redirect_stdout_to_null(void)
Definition: util_common.c:885
int util_size_string_to_byte(UINT64 *size_num, const char *size_str)
Definition: util_common.c:1027
union UTIL_ARG_MAP::@81 value_info
static int check_database_name_local(const char *name, int existing_or_new_db)
Definition: util_common.c:122
#define ONE_P
Definition: porting.h:66
#define ER_FAILED
Definition: error_code.h:47
bool are_hostnames_equal(const char *hostname_a, const char *hostname_b)
Definition: util_common.c:449
union UTIL_ARG_MAP::@82 arg_value
#define assert_release(e)
Definition: error_manager.h:96
int util_get_table_list_from_file(char *fname, dynamic_array *darray)
Definition: util_common.c:1226
#define SM_MAX_IDENTIFIER_LENGTH
int util_byte_to_size_string(char *buf, size_t len, UINT64 size_num)
Definition: util_common.c:955
char * node_name
Definition: utility.h:799
int check_volume_name(const char *name)
Definition: util_common.c:161
int apply_max_mem_size
Definition: utility.h:802
int check_database_name(const char *name)
Definition: util_common.c:99
DATABASE_NAME
Definition: util_common.c:49
INT64 l
Definition: utility.h:782
int util_time_string_to_msec(INT64 *msec_num, char *time_str)
Definition: util_common.c:1155
int util_msec_to_time_string(char *buf, size_t len, INT64 msec_num)
Definition: util_common.c:1112
int util_log_write_errid(int message_id,...)
Definition: util_func.c:468
int er_init(const char *msglog_filename, int exit_ask)
int utility_get_option_int_value(UTIL_ARG_MAP *arg_map, int arg_ch)
Definition: util_common.c:227
static KEYWORD_RECORD keywords[]
Definition: keyword.c:41
int utility_keyword_search(UTIL_KEYWORD *keywords, int *keyval_p, char **keystr_p)
Definition: util_common.c:352
void util_print_deprecated(const char *option)
Definition: util_common.c:1205
static char * node_name
Definition: cas_runner.c:174
INT64 utility_get_option_bigint_value(UTIL_ARG_MAP *arg_map, int arg_ch)
Definition: util_common.c:294
bool utility_get_option_bool_value(UTIL_ARG_MAP *arg_map, int arg_ch)
Definition: util_common.c:245
int prm_get_integer_value(PARAM_ID prm_id)
#define ER_GENERIC_ERROR
Definition: error_code.h:49
#define ONE_HOUR
Definition: porting.h:70
#define HA_SERVER_STATE_DEAD_STR
Definition: boot.h:133
int utility_localtime(const time_t *ts, struct tm *result)
Definition: util_common.c:390
#define HA_SERVER_STATE_MAINTENANCE_STR
Definition: boot.h:132
#define _dtoa
Definition: mprec.h:362
#define HA_SERVER_STATE_TO_BE_STANDBY_STR
Definition: boot.h:131
static enum scanner_mode mode
int check_new_database_name(const char *name)
Definition: util_common.c:110
static int util_size_to_byte(double *pre, const char *post)
Definition: util_common.c:909
#define ONE_G
Definition: porting.h:64
static char ** util_split_ha_node(const char *str)
Definition: util_common.c:504
int utility_initialize()
Definition: util_common.c:69
void util_free_ha_conf(HA_CONF *ha_conf)
Definition: util_common.c:651
#define NULL
Definition: freelistheap.h:34
static char ** util_split_ha_db(const char *str)
Definition: util_common.c:518
static char ** util_split_ha_sync(const char *str)
Definition: util_common.c:524
char * copy_log_base
Definition: utility.h:800
char * copy_sync_mode
Definition: utility.h:801
void util_free_string_array(char **array)
Definition: util_func.c:292
static int utility_get_option_index(UTIL_ARG_MAP *arg_map, int arg_ch)
Definition: util_common.c:205
const char * keystr
Definition: utility.h:1684
#define ONE_M
Definition: porting.h:63
int msgcat_init(void)
int utility_get_option_string_table_size(UTIL_ARG_MAP *arg_map)
Definition: util_common.c:305
#define ONE_SEC
Definition: porting.h:68
FILE * fopen_ex(const char *filename, const char *type)
Definition: util_common.c:322
char ** util_split_string(const char *str, const char *delim)
Definition: util_func.c:247
const char * utility_get_generic_message(int message_index)
Definition: util_common.c:88
const char * envvar_get(const char *name)
static void error(const char *msg)
Definition: gencat.c:331
#define HA_SERVER_STATE_STANDBY_STR
Definition: boot.h:130
int changemode_keyword(int *keyval_p, char **keystr_p)
Definition: util_common.c:555
char ** db_names
Definition: utility.h:807
int da_add(dynamic_array *da, void *data)
Definition: dynamic_array.c:82
#define free_and_init(ptr)
Definition: memory_alloc.h:147
#define strlen(s1)
Definition: intl_support.c:43
char * prm_get_string_value(PARAM_ID prm_id)
#define HA_SERVER_STATE_TO_BE_ACTIVE_STR
Definition: boot.h:129
HA_NODE_CONF * node_conf
Definition: utility.h:810
#define ONE_K
Definition: porting.h:62
int i
Definition: dynamic_load.c:954
void * p
Definition: utility.h:780
char * msgcat_message(int cat_id, int set_id, int msg_id)
int util_get_num_of_ha_nodes(const char *node_list)
Definition: util_common.c:478
const char * prm_get_name(PARAM_ID prm_id)
char * strdup(const char *str)
Definition: porting.c:901
int util_get_ha_mode_for_sa_utils(void)
Definition: util_common.c:872
#define OPTION_STRING_TABLE
Definition: utility.h:813
#define ER_BO_CANNOT_ACCESS_MESSAGE_CATALOG
Definition: error_code.h:183
int copylogdb_keyword(int *keyval_p, char **keystr_p)
Definition: util_common.c:536
#define HA_SERVER_STATE_ACTIVE_STR
Definition: boot.h:128
#define CUB_MAXHOSTNAMELEN
Definition: porting.h:379
static char * host
int util_make_ha_conf(HA_CONF *ha_conf)
Definition: util_common.c:694
int arg_ch
Definition: utility.h:772
#define ONE_T
Definition: porting.h:65
int num_strings
Definition: utility.h:776
#define ONE_MIN
Definition: porting.h:69
#define GETHOSTNAME(p, l)
Definition: porting.h:381
const char ** p
Definition: dynamic_load.c:945
char * utility_get_option_string_value(UTIL_ARG_MAP *arg_map, int arg_ch, int index)
Definition: util_common.c:266
int num_node_conf
Definition: utility.h:809
static int util_time_to_msec(double *pre, const char *post)
Definition: util_common.c:1076
#define MSGCAT_CATALOG_UTILS