/**************************************************************************** mod_dav_pgsql (DASL) for apache 2.X CopyRight(C) 2005 SolutionBox Inc. All Rights reserved. CopyRight(C) 2006 Netomi Inc. All Rights reserved. Author : elenoa lazyfake (elenoa@solutionbox.co.kr) $Id: share.c,v 1.8 2007/02/22 08:22:13 elenoa Exp $ Redistribution and use in source and binary forms, with or with out modification, are not permitted in outside of SolutionBox Inc. ****************************************************************************/ #include #include #include #include #include #include #include #include /* for ap_construct_url */ #include #include #include #include "dav_repos.h" #include "dbms.h" #include "util.h" #include "share_common.h" #include "share_fhs_stat.h" /* for fhs */ static struct shm_mgnt fhs_mgnt = { NULL, NULL, SHM_FHS, SHM_FHS_LOCK, 0, 0 }; static struct shm_mgnt fhscnt_mgnt = { NULL, NULL, SHM_FHSCNT, SHM_FHSCNT_LOCK, 0, 0 }; struct fhs_stat *g_fhs = NULL; int *g_fhscnt = NULL; static int maxfhs = 0; int dav_initialize_shared_fhs_stat(dav_repos_server_conf *dsc, server_rec *s, apr_pool_t *p) { if (!dsc->max_fhs || (maxfhs = atoi(dsc->max_fhs)) <= 0) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "OPENDAV: initialize_shared_memory: " "MaxFHS <= 0 (%d)", maxfhs); return !OK; } /* fhs management: calculate shared memory size from max_fhs value */ fhs_mgnt.size = (sizeof(struct fhs_stat) * maxfhs); g_fhs = (struct fhs_stat *)dav_shared_get_new_memory(&fhs_mgnt, s, p, false); if (!g_fhs) return !OK; fhscnt_mgnt.size = (sizeof(int)); g_fhscnt = (int *)dav_shared_get_new_memory(&fhscnt_mgnt, s, p, false); if (!g_fhscnt) return !OK; return OK; } void dav_cleanup_shared_fhs_stat() { if (fhs_mgnt.shm) { apr_shm_detach(fhs_mgnt.shm); } if (fhs_mgnt.lock) { apr_global_mutex_destroy(fhs_mgnt.lock); } } int dav_shared_du_is_alive(const char *my_domain) { int i = 0; if (!g_fhs) return 0; for (i = 0; i < maxfhs; i++) { if (*((g_fhs + i)->szHostname) == '\0') break; if (!strcmp((g_fhs + i)->szHostname, my_domain)) { int r = 0; switch ((g_fhs+i)->nActionCode) { case FHS_OK: case READ_ONLY: r = 1; break; case DELETED: case MANUAL_STOP: case SERVICE_STOP: r = 0; ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_is_alive:: FHS[%s] => Action Code [%d] Service Disabled", (g_fhs + i)->szHostname, (g_fhs + i)->nActionCode); break; default: r = -1; ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_is_alive:: FHS[%s] => Action Code [%d] Unknown", (g_fhs + i)->szHostname, (g_fhs + i)->nActionCode); break; } return r; } } ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_is_alive:: Not found alive FHS.[%s]", my_domain); return 0; } char *dav_shared_du_put_target(apr_pool_t *p, const char *my_domain) { struct fhs_stat *du = NULL; char *domain = NULL; if (!g_fhs) return NULL; if (!g_fhscnt) return NULL; int fhscnt = *g_fhscnt; if (fhscnt <= 0) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_put_target:: Empty FHS.[%s]", my_domain); return NULL; } // ·£´ý ¼±Åà unsigned int selfhs = 0; int i = 0; for (i = 0; i < 10; i++) { apr_generate_random_bytes((unsigned char *)&selfhs, sizeof(unsigned int)); selfhs = selfhs % fhscnt; if ((g_fhs + selfhs)->nActionCode != FHS_OK) continue; du = (g_fhs + selfhs); break; } if (du == NULL) { int j = 0; // ·¥´ýÀ¸·Î fhs ¼±Á¤µÇÁö ¾Ê¾ÒÀ» °æ¿ì // ƯÁ¤ fhs ºÎÅÍ Àüü ½ºÄµ, ¾÷·Îµå °¡´ÉÇÑ ¼­¹ö·Î // óÀ½ ¹ß°ßµÈ fhs¸¦ ¾÷·Îµå ¼­¹ö·Î ¼±Á¤ÇÔ // 2017-03-08 BUG huibong // #30515 - ¾÷·Îµå ´ë»ó ¼±Á¤½Ã 1Â÷ ·£´ý ¼±Åÿ¡¼­ ¼±ÅÃÀÌ ½ÇÆÐÇÑ °æ¿ì.. // - 2Â÷ ¼øÂ÷ °Ë»ö¿¡¼­ ¼±Á¤ÇØ¾ß Çϴµ¥ ·çÇÁ ¹ö±×·Î ¼±ÅõÇÁö ¾Ê´Â »óȲ ¹ß»ý. À̸¦ ¼öÁ¤ ó¸®ÇÔ. // ¸¶Áö¸· ·£´ý ¼±ÅÃÇÑ FHS °¡ À¯È¿ÇÏÁö ¾ÊÀ¸¹Ç·Î ´Ù½Ã random ¼±ÅÃÇÑ ÈÄ ÀÛ¾÷À» ÁøÇàÇÑ´Ù. apr_generate_random_bytes((unsigned char *)&selfhs, sizeof(unsigned int)); selfhs = selfhs % fhscnt; for (j = selfhs; j < fhscnt; ++j) { if ((g_fhs + j)->nActionCode != FHS_OK) { //ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_put_target:: 2nd check FHS readonly. [%s]", (g_fhs+j)->szHostname ); if( selfhs > 0 && (j+1) >= fhscnt ) { // ÃÖÃÊ 0¹ø ÀåºñºÎÅÍ ½ÃÀÛÇÏÁö ¾Ê°í - Áï Áß°£ ÀåºñºÎÅÍ ½ÃÀÛÇØ¼­ // ¸¶Áö¸·±îÁö °Ë»öÇߴµ¥µµ ¾ø´Â °æ¿ì... // 0¹ø ÀåºñºÎÅÍ ´Ù½Ã °Ë»öÇϵµ·Ï Á¶Á¤ ó¸®. j = -1; // 0¹ø ÀåºñºÎÅÍ ´Ù½Ã ½ÃÀÛÇϵµ·Ï index Á¶Á¤. fhscnt = selfhs; // ·çÇÁ Ãʱ⿡ ¼³Á¤ÇÑ Àåºñ±îÁö °Ë»ö ¹üÀ§ Á¶Á¤. selfhs = 0; // º» ·ÎÁ÷ÀÌ ´Ù½Ã ¼öÇàµÇÁö ¾Êµµ·Ï flag ¼³Á¤ ó¸®. //ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_put_target:: 2nd select index reset" ); } continue; } du = (g_fhs + j); //ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_put_target:: 2nd select FHS. [%s]", du->szHostname ); break; } } if (du != NULL) { domain = apr_pstrdup(p, du->szHostname); //ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_put_target:: writable FHS. [%s]", domain); } else { ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "dav_shared_du_put_target:: No writable FHS. [%s]", my_domain); } return domain; }