 /****************************************************************************

   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 <unistd.h>
#include <ctype.h>
#include <math.h>

#include <httpd.h>
#include <http_config.h>
#include <http_protocol.h>
#include <http_log.h>
#include <http_core.h>      /* for ap_construct_url */
#include <http_request.h>

#include <apr_strings.h>
#include <apr_signal.h>

#include "dav_repos.h"
#include "dbms.h"
#include "util.h"
#include "share_common.h"
#include "share_account.h"
#include "share_anonymous.h"
#include "share_fhs_stat.h"
#include "share_statistic.h"

/************************/
/* initialize functions */
void *dav_shared_get_new_memory(struct shm_mgnt *mgnt, server_rec *s, apr_pool_t *p, bool block)
{
	apr_status_t sts;
	void *mem = NULL;

#ifndef __OPENDAV_SHARED_NOT_USE
	/* create and check shared memory */
	sts = apr_shm_attach(&(mgnt->shm), mgnt->shm_key, p);
	if (sts != APR_SUCCESS) {
		if (sts == APR_EEXIST) {
		}
		ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, "OPENDAV: initialize_shared_memory: "
			"cannot attach shared memory [%s]", mgnt->shm_key);
		return NULL;
	}
	if (apr_shm_size_get(mgnt->shm) != mgnt->size) {
		ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, "OPENDAV: initialize_shared_memory: "
			"size not match. request size %d alloced size %d [%s]", 
			(int)mgnt->size,
			(int)apr_shm_size_get(mgnt->shm),
			mgnt->shm_key);
		return NULL;
	}
	mem = apr_shm_baseaddr_get(mgnt->shm);
	if (mem == NULL) {
		ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "OPENDAV: initialize_shared_memory: "
			"cannot find shared memory address(attach) [%s]", mgnt->shm_key);
		return NULL;
	}

	ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "OPENDAV: initialize_shared_memory: "
		"realm '%s', shared memory %d byte allocate success [%s]",
		mgnt->shm_key, mgnt->size, mgnt->shm_key);

	apr_lockmech_e  	mech;
	if (block)
		mech = APR_LOCK_DEFAULT;
	else 
		mech = APR_LOCK_FCNTL;

	apr_file_remove(mgnt->lock_key, p);
	sts = apr_global_mutex_create(&(mgnt->lock), mgnt->lock_key, mech, p);
	if (sts != APR_SUCCESS) {
		ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, "OPENDAV: initialize_shared_memory: "
			"cannot create global mutex [%s]", mgnt->shm_key);
		return NULL;
	}

#ifdef __OPENDAV_DEBUG_SHARED__
	DBG1("dav_shared_get_new_memory: pass mem:%x", (int)mem);
#endif // __OPENDAV_DEBUG_SHARED__
#endif // !__OPENDAV_SHARED_NOT_USE

	return mem;
}

int dav_initialize_shared_memory(server_rec *s, apr_pool_t *p)
{

#ifndef __OPENDAV_SHARED_NOT_USE
	dav_repos_server_conf *dsc;

	/* get dav_repos_server_conf structure */
	dsc = dav_repos_get_server_conf(s);
	if (!dsc) {
		ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "OPENDAV: initialize_shared_memory: "
			"fail to read config file");
		return !OK;
	}

	/* check configuration */
	if (dav_initialize_shared_fhs_stat(dsc, s, p) != OK)
		return !OK;

#ifndef __OPENDISK_SOLUTION_NOT_USE_TS__
	if (dav_initialize_shared_statistic(dsc, s, p) != OK)
		return !OK;
#endif // !__OPENDISK_SOLUTION_NOT_USE_TS__ 

	if (dav_initialize_shared_anonymous(dsc, s, p) != OK)
		return !OK;
	
	if (dav_initialize_shared_account_cache(dsc, s, p) != OK)
		return !OK;

	ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "OPENDAV: initialize_shared_memory: initialize success");

#endif // !__OPENDAV_SHARED_NOT_USE

	return OK;
}

apr_status_t dav_cleanup_shared_memory(void *data)
{
	//server_rec *s = (server_rec *)data;

#ifndef __OPENDAV_SHARED_NOT_USE


	dav_cleanup_shared_fhs_stat();

#ifndef __OPENDISK_SOLUTION_NOT_USE_TS__
	dav_cleanup_shared_statistic();
#endif // !__OPENDISK_SOLUTION_NOT_USE_TS__
		
	dav_cleanup_shared_anonymous();

	dav_cleanup_shared_account_cache();

	ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "OPENDAV: dav_cleanup_shared_memory: Detach Passed");

#endif // !__OPENDAV_SHARED_NOT_USE

	return 0;
}

// 2014.06.04 dadamin
// 메타 정보 테이블
char* dav_get_target_table(const char *tranid, char * buf)
{
	if (!buf)
		return NULL;
	sprintf(buf, "t_meta_%s", tranid);

	return buf;
}

// uri 값 대신 display name 사용 유무
// 0: uri 사용, 1: dispaly name 사용, -1: error
int dav_use_display_name()
{
	int r = 1;
	return r;
}
