CUBRID Engine  latest
broker_shm.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 /*
21  * broker_shm.c -
22  */
23 
24 #ident "$Id$"
25 
26 #if defined(WINDOWS)
27 #include <winsock2.h>
28 #include <windows.h>
29 #include <aclapi.h>
30 #endif
31 
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <errno.h>
35 #include <assert.h>
36 
37 #if defined(WINDOWS)
38 #include <windows.h>
39 #else
40 #include <sys/ipc.h>
41 #include <sys/shm.h>
42 #include <netdb.h>
43 #endif
44 
45 #include "cas_common.h"
46 #include "broker_shm.h"
47 #include "broker_error.h"
48 #include "broker_filename.h"
49 #include "broker_util.h"
50 
51 #if defined(WINDOWS)
52 #include "broker_list.h"
53 #endif
54 
55 #define SHMODE 0644
56 
57 
58 #if defined(WINDOWS)
59 static int shm_id_cmp_func (void *key1, void *key2);
60 static int shm_info_assign_func (T_LIST * node, void *key, void *value);
61 static char *shm_id_to_name (int shm_key);
62 #endif
63 static int get_host_ip (unsigned char *ip_addr);
64 
65 #if defined(WINDOWS)
66 T_LIST *shm_id_list_header = NULL;
67 #endif
68 
70  T_BROKER_INFO * br_info_p, int as_index);
72 
73 static void get_access_log_file_name (char *access_log_file, char *access_log_path, char *broker_name, int len);
74 static void get_error_log_file_name (char *access_log_file, char *error_log_path, char *broker_name, int len);
75 
76 static const char *get_appl_server_name (int appl_server_type);
77 
78 /*
79  * name: uw_shm_open
80  *
81  * arguments:
82  * key - caller module defined shmared memory key.
83  * if 'key' value is 0, this module set key value
84  * from shm key file.
85  *
86  * returns/side-effects:
87  * attached shared memory pointer if no error
88  * NULL if error shm open error.
89  *
90  * description: get and attach shared memory
91  *
92  */
93 #if defined(WINDOWS)
94 void *
95 uw_shm_open (int shm_key, int which_shm, T_SHM_MODE shm_mode)
96 {
97  LPVOID lpvMem = NULL; /* address of shared memory */
98  HANDLE hMapObject = NULL;
99  DWORD dwAccessRight;
100  char *shm_name;
101 
102  shm_name = shm_id_to_name (shm_key);
103 
104  if (shm_mode == SHM_MODE_MONITOR)
105  {
106  dwAccessRight = FILE_MAP_READ;
107  }
108  else
109  {
110  dwAccessRight = FILE_MAP_WRITE;
111  }
112 
113  hMapObject = OpenFileMapping (dwAccessRight, FALSE, /* inherit flag */
114  shm_name); /* name of map object */
115 
116  if (hMapObject == NULL)
117  {
118  return NULL;
119  }
120 
121  /* Get a pointer to the file-mapped shared memory. */
122  lpvMem = MapViewOfFile (hMapObject, /* object to map view of */
123  dwAccessRight, 0, /* high offset: map from */
124  0, /* low offset: beginning */
125  0); /* default: map entire file */
126 
127  if (lpvMem == NULL)
128  {
129  CloseHandle (hMapObject);
130  return NULL;
131  }
132 
133  link_list_add (&shm_id_list_header, (void *) lpvMem, (void *) hMapObject, shm_info_assign_func);
134 
135  return lpvMem;
136 }
137 #else
138 void *
139 uw_shm_open (int shm_key, int which_shm, T_SHM_MODE shm_mode)
140 {
141  int mid;
142  void *p;
143 
144  if (shm_key < 0)
145  {
147  return NULL;
148  }
149  mid = shmget (shm_key, 0, SHMODE);
150 
151  if (mid == -1)
152  {
154  return NULL;
155  }
156  p = shmat (mid, (char *) 0, ((shm_mode == SHM_MODE_ADMIN) ? 0 : SHM_RDONLY));
157  if (p == (void *) -1)
158  {
160  return NULL;
161  }
162  if (which_shm == SHM_APPL_SERVER)
163  {
164  if (((T_SHM_APPL_SERVER *) p)->magic == MAGIC_NUMBER)
165  {
166  return p;
167  }
168  }
169  else if (which_shm == SHM_BROKER)
170  {
171  if (((T_SHM_BROKER *) p)->magic == MAGIC_NUMBER)
172  {
173  return p;
174  }
175  }
176  else if (which_shm == SHM_PROXY)
177  {
178  if (((T_SHM_PROXY *) p)->magic == MAGIC_NUMBER)
179  {
180  return p;
181  }
182  }
184  return NULL;
185 }
186 #endif
187 
188 /*
189  * name: uw_shm_create
190  *
191  * arguments:
192  * NONE
193  *
194  * returns/side-effects:
195  * created shared memory ptr if no error
196  * NULL if error
197  *
198  * description: create and attach shared memory
199  * unless shared memory is already created with same key
200  *
201  */
202 
203 #if defined(WINDOWS)
204 void *
205 uw_shm_create (int shm_key, int size, int which_shm)
206 {
207  LPVOID lpvMem = NULL;
208  HANDLE hMapObject = NULL;
209  char *shm_name;
210  DWORD dwRes;
211  PSID pEveryoneSID = NULL;
212  PSID pAdminSID = NULL;
213  PACL pACL = NULL;
214  PSECURITY_DESCRIPTOR pSD = NULL;
215  EXPLICIT_ACCESS ea[2];
216  SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
217  SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
218  SECURITY_ATTRIBUTES sa;
219 
220  /* Create a well-known SID for the Everyone group. */
221  if (!AllocateAndInitializeSid (&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID))
222  {
223  goto error_exit;
224  }
225 
226  /* Initialize an EXPLICIT_ACCESS structure for an ACE. The ACE will allow Everyone read access to the shared memory. */
227  memset (ea, '\0', 2 * sizeof (EXPLICIT_ACCESS));
228  ea[0].grfAccessPermissions = GENERIC_READ;
229  ea[0].grfAccessMode = SET_ACCESS;
230  ea[0].grfInheritance = NO_INHERITANCE;
231  ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
232  ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
233  ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;
234 
235  /* Create a SID for the BUILTIN\Administrators group. */
236  if (!AllocateAndInitializeSid
237  (&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pAdminSID))
238  {
239  goto error_exit;
240  }
241 
242  /* Initialize an EXPLICIT_ACCESS structure for an ACE. The ACE will allow the Administrators group full access to the
243  * shared memory */
244  ea[1].grfAccessPermissions = GENERIC_ALL;
245  ea[1].grfAccessMode = SET_ACCESS;
246  ea[1].grfInheritance = NO_INHERITANCE;
247  ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
248  ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
249  ea[1].Trustee.ptstrName = (LPTSTR) pAdminSID;
250 
251  /* Create a new ACL that contains the new ACEs. */
252  dwRes = SetEntriesInAcl (2, ea, NULL, &pACL);
253  if (ERROR_SUCCESS != dwRes)
254  {
255  goto error_exit;
256  }
257 
258  /* Initialize a security descriptor. */
259  pSD = (PSECURITY_DESCRIPTOR) LocalAlloc (LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
260 
261  if (NULL == pSD)
262  {
263  goto error_exit;
264  }
265 
266  if (!InitializeSecurityDescriptor (pSD, SECURITY_DESCRIPTOR_REVISION))
267  {
268  goto error_exit;
269  }
270 
271  /* Add the ACL to the security descriptor. */
272  if (!SetSecurityDescriptorDacl (pSD, TRUE, pACL, FALSE))
273  {
274  goto error_exit;
275  }
276 
277  /* Initialize a security attributes structure. */
278  sa.nLength = sizeof (SECURITY_ATTRIBUTES);
279  sa.lpSecurityDescriptor = pSD;
280  sa.bInheritHandle = FALSE;
281 
282  shm_name = shm_id_to_name (shm_key);
283 
284  hMapObject = CreateFileMapping (INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, size, shm_name);
285 
286  if (hMapObject == NULL)
287  {
288  goto error_exit;
289  }
290 
291  if (GetLastError () == ERROR_ALREADY_EXISTS)
292  {
293  CloseHandle (hMapObject);
294  goto error_exit;
295  }
296 
297  lpvMem = MapViewOfFile (hMapObject, FILE_MAP_WRITE, 0, 0, 0);
298 
299  if (lpvMem == NULL)
300  {
301  CloseHandle (hMapObject);
302  goto error_exit;
303  }
304 
305  link_list_add (&shm_id_list_header, (void *) lpvMem, (void *) hMapObject, shm_info_assign_func);
306 
307  return lpvMem;
308 
309 error_exit:
310  if (pEveryoneSID)
311  {
312  FreeSid (pEveryoneSID);
313  }
314 
315  if (pAdminSID)
316  {
317  FreeSid (pAdminSID);
318  }
319 
320  if (pACL)
321  {
322  LocalFree (pACL);
323  }
324 
325  if (pSD)
326  {
327  LocalFree (pSD);
328  }
329 
330  return NULL;
331 }
332 #else
333 void *
334 uw_shm_create (int shm_key, int size, int which_shm)
335 {
336  int mid;
337  void *p;
338 
339  if (size <= 0 || shm_key <= 0)
340  return NULL;
341 
342  mid = shmget (shm_key, size, IPC_CREAT | IPC_EXCL | SHMODE);
343 
344  if (mid == -1)
345  return NULL;
346  p = shmat (mid, (char *) 0, 0);
347 
348  if (p == (void *) -1)
349  return NULL;
350  else
351  {
352  if (which_shm == SHM_APPL_SERVER)
353  {
354 #ifdef USE_MUTEX
355  me_reset_sv (&(((T_SHM_APPL_SERVER *) p)->lock));
356 #endif /* USE_MUTEX */
357  ((T_SHM_APPL_SERVER *) p)->magic = MAGIC_NUMBER;
358  }
359  else if (which_shm == SHM_BROKER)
360  {
361 #ifdef USE_MUTEX
362  me_reset_sv (&(((T_SHM_BROKER *) p)->lock));
363 #endif
364  ((T_SHM_BROKER *) p)->magic = MAGIC_NUMBER;
365  }
366  else if (which_shm == SHM_PROXY)
367  {
368 #ifdef USE_MUTEX
369  me_reset_sv (&(((T_SHM_PROXY *) p)->lock));
370 #endif
371  ((T_SHM_PROXY *) p)->magic = MAGIC_NUMBER;
372  }
373  }
374  return p;
375 }
376 #endif
377 
378 /*
379  * name: uw_shm_destroy
380  *
381  * arguments:
382  * NONE
383  *
384  * returns/side-effects:
385  * void
386  *
387  * description: destroy shared memory
388  *
389  */
390 int
391 uw_shm_destroy (int shm_key)
392 {
393 #if defined(WINDOWS)
394  return 0;
395 #else
396  int mid;
397 
398  mid = shmget (shm_key, 0, SHMODE);
399 
400  if (mid == -1)
401  {
402  return -1;
403  }
404 
405  if (shmctl (mid, IPC_RMID, 0) == -1)
406  {
407  return -1;
408  }
409 
410  return 0;
411 
412 #endif
413 }
414 
415 T_SHM_BROKER *
416 broker_shm_initialize_shm_broker (int master_shm_id, T_BROKER_INFO * br_info, int br_num, int acl_flag, char *acl_file)
417 {
418  int i, shm_size;
420  unsigned char ip_addr[4];
421 
422  if (get_host_ip (ip_addr) < 0)
423  {
424  return NULL;
425  }
426 
427  shm_size = sizeof (T_SHM_BROKER) + (br_num - 1) * sizeof (T_BROKER_INFO);
428 
429  shm_br = (T_SHM_BROKER *) uw_shm_create (master_shm_id, shm_size, SHM_BROKER);
430 
431  if (shm_br == NULL)
432  {
433  return NULL;
434  }
435 
436  memcpy (shm_br->my_ip_addr, ip_addr, 4);
437 #if defined(WINDOWS)
438  shm_br->magic = uw_shm_get_magic_number ();
439 #else /* WINDOWS */
440  shm_br->owner_uid = getuid ();
441 
442  /* create a new session */
443  setsid ();
444 #endif /* WINDOWS */
445 
446  shm_br->num_broker = br_num;
447  shm_br->access_control = acl_flag;
448 
449  if (acl_file != NULL)
450  {
451  strncpy (shm_br->access_control_file, acl_file, sizeof (shm_br->access_control_file) - 1);
452  }
453 
454  for (i = 0; i < br_num; i++)
455  {
456  shm_br->br_info[i] = br_info[i];
457 
458  if (br_info[i].shard_flag == OFF)
459  {
460  get_access_log_file_name (shm_br->br_info[i].access_log_file, br_info[i].access_log_file, br_info[i].name,
462  }
463  get_error_log_file_name (shm_br->br_info[i].error_log_file, br_info[i].error_log_file, br_info[i].name,
465  }
466 
467  return shm_br;
468 }
469 
472 {
473  int as_index;
475  T_APPL_SERVER_INFO *as_info_p = NULL;
476 
478  T_SHARD_INFO *shard_info_p = NULL;
479  T_SHARD_CONN_INFO *shard_conn_info_p = NULL;
480  short proxy_id, shard_id, shard_cas_id;
481 
482  shm_as_p =
484 
485  if (shm_as_p == NULL)
486  {
487  return NULL;
488  }
489 
490  shm_as_p->cci_default_autocommit = br_info_p->cci_default_autocommit;
491  shm_as_p->job_queue_size = br_info_p->job_queue_size;
492  shm_as_p->job_queue[0].id = 0; /* initialize max heap */
493  shm_as_p->max_prepared_stmt_count = br_info_p->max_prepared_stmt_count;
494 
495  shm_as_p->monitor_hang_flag = br_info_p->monitor_hang_flag;
496  shm_as_p->monitor_server_flag = br_info_p->monitor_server_flag;
497  memset (shm_as_p->unusable_databases_cnt, 0, sizeof (shm_as_p->unusable_databases_cnt));
498 
499  strcpy (shm_as_p->log_dir, br_info_p->log_dir);
500  strcpy (shm_as_p->slow_log_dir, br_info_p->slow_log_dir);
501  strcpy (shm_as_p->err_log_dir, br_info_p->err_log_dir);
502  strcpy (shm_as_p->broker_name, br_info_p->name);
503 
504  shm_as_p->broker_port = br_info_p->port;
505  shm_as_p->num_appl_server = br_info_p->appl_server_num;
506  shm_as_p->sql_log_mode = br_info_p->sql_log_mode;
507  shm_as_p->sql_log_max_size = br_info_p->sql_log_max_size;
508  shm_as_p->long_query_time = br_info_p->long_query_time;
509  shm_as_p->long_transaction_time = br_info_p->long_transaction_time;
510  shm_as_p->appl_server_max_size = br_info_p->appl_server_max_size;
511  shm_as_p->appl_server_hard_limit = br_info_p->appl_server_hard_limit;
512  shm_as_p->session_timeout = br_info_p->session_timeout;
513  shm_as_p->sql_log2 = br_info_p->sql_log2;
514  shm_as_p->slow_log_mode = br_info_p->slow_log_mode;
515 #if defined(WINDOWS)
516  shm_as_p->as_port = br_info_p->appl_server_port;
517 #endif /* WINDOWS */
518  shm_as_p->query_timeout = br_info_p->query_timeout;
519  shm_as_p->mysql_read_timeout = br_info_p->mysql_read_timeout;
520  shm_as_p->mysql_keepalive_interval = br_info_p->mysql_keepalive_interval;
521  shm_as_p->max_string_length = br_info_p->max_string_length;
522  shm_as_p->stripped_column_name = br_info_p->stripped_column_name;
523  shm_as_p->keep_connection = br_info_p->keep_connection;
524  shm_as_p->cache_user_info = br_info_p->cache_user_info;
525  shm_as_p->statement_pooling = br_info_p->statement_pooling;
526  shm_as_p->access_mode = br_info_p->access_mode;
527  shm_as_p->cci_pconnect = br_info_p->cci_pconnect;
528  shm_as_p->access_log = br_info_p->access_log;
529  shm_as_p->access_log_max_size = br_info_p->access_log_max_size;
530  shm_as_p->jdbc_cache = br_info_p->jdbc_cache;
531  shm_as_p->jdbc_cache_only_hint = br_info_p->jdbc_cache_only_hint;
532  shm_as_p->jdbc_cache_life_time = br_info_p->jdbc_cache_life_time;
533 
534  shm_as_p->connect_order = br_info_p->connect_order;
535  shm_as_p->replica_only_flag = br_info_p->replica_only_flag;
536 
538 
539  shm_as_p->trigger_action_flag = br_info_p->trigger_action_flag;
540 
541  shm_as_p->cas_rctime = br_info_p->cas_rctime;
542 
543  strcpy (shm_as_p->preferred_hosts, br_info_p->preferred_hosts);
544  strcpy (shm_as_p->error_log_file, br_info_p->error_log_file);
545  strcpy (shm_as_p->access_log_file, br_info_p->access_log_file);
546  strcpy (shm_as_p->appl_server_name, get_appl_server_name (br_info_p->appl_server));
547  ut_get_broker_port_name (shm_as_p->port_name, br_info_p->name, SHM_PROXY_NAME_MAX);
548  strcpy (shm_as_p->db_connection_file, br_info_p->db_connection_file);
549 
550  for (as_index = 0; as_index < br_info_p->appl_server_max_num; as_index++)
551  {
552  as_info_p = &(shm_as_p->as_info[as_index]);
553  broker_shm_set_as_info (shm_as_p, as_info_p, br_info_p, as_index);
554  }
555 
556  shm_as_p->shard_flag = br_info_p->shard_flag;
557 
558  if (shm_as_p->shard_flag == OFF)
559  {
560  assert (shm_proxy_p == NULL);
561  return shm_as_p;
562  }
563 
564  strncpy_bufsize (shm_as_p->proxy_log_dir, br_info_p->proxy_log_dir);
565 
566  shm_as_p->proxy_log_max_size = br_info_p->proxy_log_max_size;
567 
568  shard_shm_set_shard_conn_info (shm_as_p, shm_proxy_p);
569 
570  for (proxy_id = 0; proxy_id < shm_proxy_p->num_proxy; proxy_id++)
571  {
572  proxy_info_p = &shm_proxy_p->proxy_info[proxy_id];
573 
574  for (shard_id = 0; shard_id < proxy_info_p->num_shard_conn; shard_id++)
575  {
576  shard_info_p = &proxy_info_p->shard_info[shard_id];
577  shard_conn_info_p = &shm_as_p->shard_conn_info[shard_id];
578 
579  proxy_info_p->fixed_shard_user = (shard_conn_info_p->db_user[0] != '\0') ? true : false;
580 
581  for (shard_cas_id = 0; shard_cas_id < shard_info_p->max_appl_server; shard_cas_id++)
582  {
583  as_info_p = &(shm_as_p->as_info[shard_cas_id + shard_info_p->as_info_index_base]);
584 
585  as_info_p->proxy_id = proxy_id;
586  as_info_p->shard_id = shard_id;
587  as_info_p->shard_cas_id = shard_cas_id;
588  as_info_p->fixed_shard_user = proxy_info_p->fixed_shard_user;
589  if (proxy_info_p->fixed_shard_user == true)
590  {
591  strcpy (as_info_p->database_user, shard_conn_info_p->db_user);
592  strcpy (as_info_p->database_passwd, shard_conn_info_p->db_password);
593  }
594 
595  if (shard_cas_id < shard_info_p->min_appl_server)
596  {
597  as_info_p->advance_activate_flag = 1;
598  }
599  else
600  {
601  as_info_p->advance_activate_flag = 0;
602  }
603 
604  as_info_p->proxy_conn_wait_timeout = br_info_p->proxy_conn_wait_timeout;
605  as_info_p->force_reconnect = false;
606  }
607  }
608  }
609 
610  return shm_as_p;
611 }
612 
613 static void
615  int as_index)
616 {
617  as_info_p->service_flag = SERVICE_OFF;
618  as_info_p->last_access_time = time (NULL);
619  as_info_p->transaction_start_time = (time_t) 0;
620 
621  as_info_p->mutex_flag[SHM_MUTEX_BROKER] = FALSE;
622  as_info_p->mutex_flag[SHM_MUTEX_ADMIN] = FALSE;
623  as_info_p->mutex_turn = SHM_MUTEX_BROKER;
624 
625  as_info_p->num_request = 0;
626  as_info_p->num_requests_received = 0;
627  as_info_p->num_transactions_processed = 0;
628  as_info_p->num_queries_processed = 0;
629  as_info_p->num_long_queries = 0;
630  as_info_p->num_long_transactions = 0;
631  as_info_p->num_error_queries = 0;
632  as_info_p->num_interrupts = 0;
633  as_info_p->num_connect_requests = 0;
634  as_info_p->num_connect_rejected = 0;
635  as_info_p->num_restarts = 0;
636  as_info_p->auto_commit_mode = FALSE;
637  as_info_p->database_name[0] = '\0';
638  as_info_p->database_host[0] = '\0';
639  as_info_p->database_user[0] = '\0';
640  as_info_p->database_passwd[0] = '\0';
641  as_info_p->last_connect_time = 0;
642  as_info_p->num_holdable_results = 0;
644  as_info_p->cur_sql_log_mode = br_info_p->sql_log_mode;
645  as_info_p->cur_slow_log_mode = br_info_p->slow_log_mode;
646 
647  as_info_p->as_id = as_index;
648 
649  as_info_p->fn_status = -1;
650  as_info_p->session_id = 0;
651  return;
652 }
653 
654 static void
656 {
657  T_SHARD_CONN_INFO *shard_conn_info_p;
660  T_SHARD_CONN *conn_p = NULL;
661  T_SHARD_USER *user_p = NULL;
662  int i;
663 
664 
665  shm_user_p = &shm_proxy_p->shm_shard_user;
666  shm_conn_p = &shm_proxy_p->shm_shard_conn;
667 
668  user_p = &shm_user_p->shard_user[0];
669 
670  for (i = 0; i < shm_conn_p->num_shard_conn; i++)
671  {
672  conn_p = &shm_conn_p->shard_conn[i];
673  shard_conn_info_p = &shm_as_p->shard_conn_info[i];
674 
675  strncpy_bufsize (shard_conn_info_p->db_user, user_p->db_user);
676  strncpy_bufsize (shard_conn_info_p->db_name, conn_p->db_name);
677  strncpy_bufsize (shard_conn_info_p->db_host, conn_p->db_conn_info);
678  strncpy_bufsize (shard_conn_info_p->db_password, user_p->db_password);
679  }
680 }
681 
682 
683 #if defined(WINDOWS)
684 void
685 uw_shm_detach (void *p)
686 {
687  HANDLE hMapObject;
688 
689  SLEEP_MILISEC (0, 10);
690  LINK_LIST_FIND_VALUE (hMapObject, shm_id_list_header, p, shm_id_cmp_func);
691  if (hMapObject == NULL)
692  return;
693 
694  UnmapViewOfFile (p);
695  CloseHandle (hMapObject);
696  link_list_node_delete (&shm_id_list_header, p, shm_id_cmp_func, NULL);
697 }
698 #else
699 void
700 uw_shm_detach (void *p)
701 {
702  shmdt (p);
703 }
704 #endif
705 
706 #if defined(WINDOWS)
707 int
708 uw_shm_get_magic_number ()
709 {
710  return MAGIC_NUMBER;
711 }
712 #endif
713 
714 #if defined(WINDOWS)
715 static int
716 shm_id_cmp_func (void *key1, void *key2)
717 {
718  if (key1 == key2)
719  return 1;
720  return 0;
721 }
722 
723 static int
724 shm_info_assign_func (T_LIST * node, void *key, void *value)
725 {
726  node->key = key;
727  node->value = value;
728  return 0;
729 }
730 
731 static char *
732 shm_id_to_name (int shm_key)
733 {
734  static char shm_name[32];
735 
736  sprintf (shm_name, "Global\\v3mapfile%d", shm_key);
737  return shm_name;
738 }
739 #endif /* WINDOWS */
740 
741 static int
742 get_host_ip (unsigned char *ip_addr)
743 {
744  char hostname[CUB_MAXHOSTNAMELEN];
745  struct hostent *hp;
746 
747  if (gethostname (hostname, sizeof (hostname)) < 0)
748  {
749  fprintf (stderr, "gethostname error\n");
750  return -1;
751  }
752  if ((hp = gethostbyname (hostname)) == NULL)
753  {
754  fprintf (stderr, "unknown host : %s\n", hostname);
755  return -1;
756  }
757  memcpy ((void *) ip_addr, (void *) hp->h_addr_list[0], 4);
758 
759  return 0;
760 }
761 
762 
763 #if 0
764 static void
765 bs_log_msg (LPTSTR lpszMsg)
766 {
767  char lpDisplayBuf[4096];
768  sprintf (lpDisplayBuf, "%s", lpszMsg);
769  MessageBox (NULL, (LPCTSTR) lpDisplayBuf, TEXT ("Error"), MB_OK);
770 }
771 
772 static void
773 bs_last_error_msg (LPTSTR lpszFunction)
774 {
775  LPVOID lpDisplayBuf;
776  LPVOID lpMsgBuf;
777  DWORD dw;
778 
779  dw = GetLastError ();
780  FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
781  MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
782  lpDisplayBuf =
783  (LPVOID) LocalAlloc (LMEM_ZEROINIT,
784  (lstrlen ((LPCTSTR) lpMsgBuf) + lstrlen ((LPCTSTR) lpszFunction) + 40) * sizeof (TCHAR));
785  sprintf ((LPTSTR) lpDisplayBuf, "%s failed with error %d: %s", lpszFunction, dw, lpMsgBuf);
786  MessageBox (NULL, (LPCTSTR) lpDisplayBuf, TEXT ("Error"), MB_OK);
787  LocalFree (lpMsgBuf);
788  LocalFree (lpDisplayBuf);
789 }
790 #endif
791 
792 #if defined (WINDOWS)
793 static int
794 uw_sem_open (char *sem_name, HANDLE * sem_handle)
795 {
796  HANDLE new_handle;
797 
798  new_handle = OpenMutex (SYNCHRONIZE, FALSE, sem_name);
799  if (new_handle == NULL)
800  {
801  return -1;
802  }
803 
804  if (sem_handle)
805  {
806  *sem_handle = new_handle;
807  }
808 
809  return 0;
810 }
811 
812 int
813 uw_sem_init (char *sem_name)
814 {
815  HANDLE sem_handle;
816 
817  sem_handle = CreateMutex (NULL, FALSE, sem_name);
818  if (sem_handle == NULL && GetLastError () == ERROR_ALREADY_EXISTS)
819  {
820  sem_handle = OpenMutex (SYNCHRONIZE, FALSE, sem_name);
821  }
822 
823  if (sem_handle == NULL)
824  {
825  return -1;
826  }
827  else
828  {
829  return 0;
830  }
831 }
832 #else
833 int
834 uw_sem_init (sem_t * sem)
835 {
836  return sem_init (sem, 1, 1);
837 }
838 #endif
839 
840 #if defined (WINDOWS)
841 int
842 uw_sem_wait (char *sem_name)
843 {
844  DWORD dwWaitResult;
845  HANDLE sem_handle;
846 
847  if (uw_sem_open (sem_name, &sem_handle) != 0)
848  {
849  return -1;
850  }
851 
852  dwWaitResult = WaitForSingleObject (sem_handle, INFINITE);
853  switch (dwWaitResult)
854  {
855  case WAIT_OBJECT_0:
856  return 0;
857  case WAIT_TIMEOUT:
858  case WAIT_FAILED:
859  case WAIT_ABANDONED:
860  return -1;
861  }
862 
863  return -1;
864 }
865 #else
866 int
867 uw_sem_wait (sem_t * sem)
868 {
869  return sem_wait (sem);
870 }
871 #endif
872 
873 #if defined (WINDOWS)
874 int
875 uw_sem_post (char *sem_name)
876 {
877  HANDLE sem_handle;
878 
879  if (uw_sem_open (sem_name, &sem_handle) != 0)
880  {
881  return -1;
882  }
883 
884  if (ReleaseMutex (sem_handle) != 0)
885  {
886  return 0;
887  }
888 
889  return -1;
890 }
891 #else
892 int
893 uw_sem_post (sem_t * sem)
894 {
895  return sem_post (sem);
896 }
897 #endif
898 
899 #if defined (WINDOWS)
900 int
901 uw_sem_destroy (char *sem_name)
902 {
903  HANDLE sem_handle;
904 
905  if (uw_sem_open (sem_name, &sem_handle) != 0)
906  {
907  return -1;
908  }
909 
910  if (sem_handle != NULL && CloseHandle (sem_handle) != 0)
911  {
912  return 0;
913  }
914 
915  return -1;
916 }
917 #else
918 int
919 uw_sem_destroy (sem_t * sem)
920 {
921  return sem_destroy (sem);
922 }
923 #endif
924 
925 static void
926 get_access_log_file_name (char *access_log_file, char *access_log_path, char *broker_name, int len)
927 {
928  snprintf (access_log_file, len, "%s/%s.access", access_log_path, broker_name);
929 }
930 
931 static void
932 get_error_log_file_name (char *access_log_file, char *error_log_path, char *broker_name, int len)
933 {
934  snprintf (access_log_file, len, "%s/%s.err", error_log_path, broker_name);
935 }
936 
937 static const char *
938 get_appl_server_name (int appl_server_type)
939 {
940  if (appl_server_type == APPL_SERVER_CAS_ORACLE)
941  {
943  }
944  else if (appl_server_type == APPL_SERVER_CAS_MYSQL51)
945  {
947  }
948  else if (appl_server_type == APPL_SERVER_CAS_MYSQL)
949  {
951  }
952 
953  return APPL_SERVER_CAS_NAME;
954 }
#define SLEEP_MILISEC(sec, msec)
Definition: util_func.h:40
char trigger_action_flag
int long_transaction_time
char monitor_hang_flag
#define MAGIC_NUMBER
Definition: broker_shm.h:176
INT64 num_long_transactions
Definition: broker_shm.h:347
char jdbc_cache_only_hint
#define SHMODE
Definition: broker_shm.c:55
char database_name[SRV_CON_DBNAME_SIZE]
Definition: broker_shm.h:352
int link_list_node_delete(T_LIST **cur_head, void *key, int(*cmp_func)(void *, void *), void(*node_dealloc)(T_LIST *))
Definition: broker_list.c:156
#define TRUE
Definition: broker_admin.c:49
struct t_broker_info T_BROKER_INFO
#define APPL_SERVER_CAS_MYSQL_NAME
T_BROKER_INFO br_info[1]
Definition: broker_shm.h:661
T_MAX_HEAP_NODE job_queue[JOB_QUEUE_MAX_SIZE+1]
Definition: broker_shm.h:637
int max_prepared_stmt_count
Definition: broker_shm.h:623
char proxy_log_dir[CONF_LOG_FILE_LEN]
Definition: broker_shm.h:600
T_PROXY_INFO proxy_info[MAX_PROXY_NUM]
Definition: broker_shm.h:545
char db_name[MAX_DBNAME_LENGTH]
Definition: broker_shm.h:219
T_SHARD_CONN shard_conn[MAX_SHARD_CONN]
Definition: broker_shm.h:286
#define SHM_BROKER
Definition: broker_shm.h:64
char error_log_file[CONF_LOG_FILE_LEN]
Definition: broker_shm.h:599
void * value
Definition: broker_list.h:49
char port_name[SHM_APPL_SERVER_NAME_MAX]
Definition: broker_shm.h:601
#define APPL_SERVER_CAS_MYSQL51_NAME
int link_list_add(T_LIST **cur_head, void *add_key, void *add_val, int(*assign_func)(T_LIST *, void *, void *))
Definition: broker_list.c:63
time_t last_access_time
Definition: broker_shm.h:317
int max_num_delayed_hosts_lookup
char broker_name[BROKER_NAME_LEN]
Definition: cas.c:148
static T_BROKER_INFO * br_info_p
Definition: broker.c:312
int uw_sem_post(sem_t *sem)
Definition: broker_shm.c:893
#define UW_SET_ERROR_CODE(code, os_errno)
Definition: broker_error.h:87
#define SHM_PROXY
Definition: broker_shm.h:65
int proxy_conn_wait_timeout
static T_SHM_PROXY * shm_proxy_p
Definition: broker.c:313
char slow_log_dir[CONF_LOG_FILE_LEN]
char proxy_log_dir[CONF_LOG_FILE_LEN]
int unusable_databases_cnt[PAIR_LIST]
Definition: broker_shm.h:627
int mysql_keepalive_interval
int uw_sem_init(sem_t *sem)
Definition: broker_shm.c:834
char database_user[SRV_CON_DBUSER_SIZE]
Definition: broker_shm.h:354
#define SHM_MUTEX_BROKER
Definition: broker_shm.h:68
#define LINK_LIST_FIND_VALUE(VALUE, HEAD, KEY, KEY_CMP_FUNC)
Definition: broker_list.h:34
void ut_get_broker_port_name(char *port_name, char *broker_name, int len)
Definition: broker_util.c:542
INT64 num_queries_processed
Definition: broker_shm.h:345
char db_host[MAX_CONN_INFO_LENGTH]
Definition: broker_shm.h:220
T_SHARD_INFO shard_info[SHARD_INFO_SIZE_LIMIT]
Definition: broker_shm.h:520
unsigned char my_ip_addr[4]
Definition: broker_shm.h:654
int appl_server_hard_limit
char broker_name[BROKER_NAME_LEN]
Definition: broker_shm.h:588
int max_appl_server
Definition: broker_shm.h:416
int appl_server_hard_limit
Definition: broker_shm.h:612
char db_user[SRV_CON_DBUSER_SIZE]
Definition: broker_shm.h:221
void * key
Definition: broker_list.h:48
char jdbc_cache_only_hint
Definition: broker_shm.h:570
char db_password[SRV_CON_DBPASSWD_SIZE]
Definition: broker_shm.h:236
char cci_default_autocommit
Definition: broker_shm.h:572
#define APPL_SERVER_CAS_MYSQL
Definition: broker_config.h:37
#define APPL_SERVER_CAS_ORACLE
Definition: broker_config.h:35
static void broker_shm_set_as_info(T_SHM_APPL_SERVER *shm_appl, T_APPL_SERVER_INFO *as_info_p, T_BROKER_INFO *br_info_p, int as_index)
Definition: broker_shm.c:614
#define APPL_SERVER_CAS_ORACLE_NAME
int jdbc_cache_life_time
static const char * get_appl_server_name(int appl_server_type)
Definition: broker_shm.c:938
char db_password[SRV_CON_DBPASSWD_SIZE]
Definition: broker_shm.h:222
#define assert(x)
T_SHM_SHARD_USER shm_shard_user
Definition: broker_shm.h:541
char log_dir[CONF_LOG_FILE_LEN]
int mysql_keepalive_interval
Definition: broker_shm.h:616
char database_passwd[SRV_CON_DBPASSWD_SIZE]
Definition: broker_shm.h:355
INT64 num_connect_requests
Definition: broker_shm.h:358
int proxy_id
Definition: shard_proxy.c:45
int as_info_index_base
Definition: broker_shm.h:422
char cci_default_autocommit
int num_shard_conn
Definition: broker_shm.h:514
char monitor_server_flag
static void get_access_log_file_name(char *access_log_file, char *access_log_path, char *broker_name, int len)
Definition: broker_shm.c:926
void uw_shm_detach(void *p)
Definition: broker_shm.c:700
char stripped_column_name
enum t_shm_mode T_SHM_MODE
Definition: broker_shm.h:669
int uw_shm_destroy(int shm_key)
Definition: broker_shm.c:391
time_t transaction_start_time
Definition: broker_shm.h:318
bool fixed_shard_user
Definition: broker_shm.h:489
int max_prepared_stmt_count
T_SHM_SHARD_USER * shm_user_p
Definition: shard_proxy.c:50
#define NULL
Definition: freelistheap.h:34
#define strncpy_bufsize(buf, str)
Definition: porting.h:340
#define SHM_PROXY_NAME_MAX
Definition: broker_shm.h:140
char db_conn_info[MAX_CONN_INFO_LENGTH]
Definition: broker_shm.h:279
char trigger_action_flag
Definition: broker_shm.h:579
INT64 num_transactions_processed
Definition: broker_shm.h:344
uid_t owner_uid
Definition: broker_shm.h:656
#define UW_ER_SHM_OPEN
Definition: broker_error.h:44
static T_SHM_APPL_SERVER * shm_appl
Definition: broker.c:311
char err_log_dir[CONF_LOG_FILE_LEN]
Definition: broker_shm.h:587
char access_log_file[CONF_LOG_FILE_LEN]
T_SHM_SHARD_CONN shm_shard_conn
Definition: broker_shm.h:542
char database_host[CUB_MAXHOSTNAMELEN]
Definition: broker_shm.h:353
#define APPL_SERVER_CAS_NAME
#define SHM_MUTEX_ADMIN
Definition: broker_shm.h:69
unsigned int session_id
Definition: broker_shm.h:377
char preferred_hosts[BROKER_INFO_NAME_MAX]
static T_SHM_BROKER * shm_br
Definition: broker.c:310
T_PROXY_INFO * proxy_info_p
Definition: shard_proxy.c:48
int appl_server_max_size
int max_num_delayed_hosts_lookup
Definition: broker_shm.h:578
time_t last_connect_time
Definition: broker_shm.h:357
bool monitor_server_flag
Definition: broker_shm.h:630
INT64 num_requests_received
Definition: broker_shm.h:343
char db_connection_file[BROKER_INFO_PATH_MAX]
Definition: broker_shm.h:593
char preferred_hosts[SHM_APPL_SERVER_NAME_MAX]
Definition: broker_shm.h:590
T_SHARD_USER shard_user[1]
Definition: broker_shm.h:243
char name[BROKER_NAME_LEN]
#define FALSE
Definition: broker_admin.c:50
char error_log_file[CONF_LOG_FILE_LEN]
char appl_server_name[APPL_SERVER_NAME_MAX_SIZE]
Definition: broker_shm.h:589
T_SHM_APPL_SERVER * broker_shm_initialize_shm_as(T_BROKER_INFO *br_info_p, T_SHM_PROXY *shm_proxy_p)
Definition: broker_shm.c:471
T_APPL_SERVER_INFO as_info[APPL_SERVER_NUM_LIMIT]
Definition: broker_shm.h:641
struct t_shm_broker T_SHM_BROKER
Definition: broker_shm.h:647
char statement_pooling
static void get_error_log_file_name(char *access_log_file, char *error_log_path, char *broker_name, int len)
Definition: broker_shm.c:932
int i
Definition: dynamic_load.c:954
#define CONF_LOG_FILE_LEN
Definition: broker_config.h:52
T_SHM_BROKER * broker_shm_initialize_shm_broker(int master_shm_id, T_BROKER_INFO *br_info, int br_num, int acl_flag, char *acl_file)
Definition: broker_shm.c:416
char access_log_file[CONF_LOG_FILE_LEN]
Definition: broker_shm.h:592
int uw_sem_wait(sem_t *sem)
Definition: broker_shm.c:867
#define APPL_SERVER_CAS_MYSQL51
Definition: broker_config.h:36
static void shard_shm_set_shard_conn_info(T_SHM_APPL_SERVER *shm_as_p, T_SHM_PROXY *shm_proxy_p)
Definition: broker_shm.c:655
char stripped_column_name
Definition: broker_shm.h:563
char db_user[SRV_CON_DBUSER_SIZE]
Definition: broker_shm.h:235
bool access_control
Definition: broker_shm.h:660
T_SHM_APPL_SERVER * shm_as_p
Definition: shard_proxy.c:43
void * uw_shm_open(int shm_key, int which_shm, T_SHM_MODE shm_mode)
Definition: broker_shm.c:139
char slow_log_dir[CONF_LOG_FILE_LEN]
Definition: broker_shm.h:586
#define CUB_MAXHOSTNAMELEN
Definition: porting.h:379
T_SHM_SHARD_CONN * shm_conn_p
Definition: shard_proxy.c:52
char log_dir[CONF_LOG_FILE_LEN]
Definition: broker_shm.h:585
T_SHARD_CONN_INFO shard_conn_info[SHARD_INFO_SIZE_LIMIT]
Definition: broker_shm.h:639
char err_log_dir[CONF_LOG_FILE_LEN]
char access_control_file[SHM_BROKER_PATH_MAX]
Definition: broker_shm.h:659
static int get_host_ip(unsigned char *ip_addr)
Definition: broker_shm.c:742
INT64 num_connect_rejected
Definition: broker_shm.h:359
const char ** p
Definition: dynamic_load.c:945
char db_connection_file[BROKER_INFO_PATH_MAX]
#define UW_ER_SHM_OPEN_MAGIC
Definition: broker_error.h:82
int uw_sem_destroy(sem_t *sem)
Definition: broker_shm.c:919
char db_name[MAX_DBNAME_LENGTH]
Definition: broker_shm.h:278
void * uw_shm_create(int shm_key, int size, int which_shm)
Definition: broker_shm.c:334
#define SHM_APPL_SERVER
Definition: broker_shm.h:63