static
memcached_st *s_init_mod_data (const memcached_server_list_st servers, zend_bool is_persistent)
{
	void *buffer;
	php_memcached_user_data *user_data;
	memcached_st *memc;

	buffer = pecalloc(1, sizeof(memcached_st), is_persistent);
	memc   = memcached_create (buffer);

	if (!memc) {
		php_error_docref(NULL, E_ERROR, "failed to allocate memcached structure");
		/* not reached */
	}

	memcached_set_memory_allocators(memc, s_pemalloc_fn, s_pefree_fn, s_perealloc_fn, s_pecalloc_fn, NULL);

	user_data                = pecalloc(1, sizeof(php_memcached_user_data), is_persistent);
	user_data->is_persistent = is_persistent;
	user_data->has_sasl_data = 0;
	user_data->lock_key      = NULL;
	user_data->is_locked     = 0;

	memcached_set_user_data(memc, user_data);
	memcached_server_push (memc, servers);
	memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
	return memc;
}
Esempio n. 2
0
void _PG_init(void)
{
  MemoryContext old_ctxt;

  globals.pg_ctxt = AllocSetContextCreate(TopMemoryContext,
					  "pgmemcache global context",
					  ALLOCSET_SMALL_MINSIZE,
					  ALLOCSET_SMALL_INITSIZE,
					  ALLOCSET_SMALL_MAXSIZE);

  old_ctxt = MemoryContextSwitchTo(globals.pg_ctxt);
  globals.mc = memcached_create(NULL);

  if (memcached_set_memory_allocators(globals.mc,
				      (memcached_malloc_fn) pgmemcache_malloc,
				      (memcached_free_fn) pgmemcache_free,
				      (memcached_realloc_fn) pgmemcache_realloc,
				      (memcached_calloc_fn) pgmemcache_calloc,
				      NULL) != MEMCACHED_SUCCESS) {
    elog(ERROR, "pgmemcache: unable to set memory allocators");
  }

  MemoryContextSwitchTo(old_ctxt);

  DefineCustomStringVariable("pgmemcache.default_servers",
			     "Comma-separated list of memcached servers to connect to.",
			     "Specified as a comma-separated list of host:port (port is optional).",
			     &memcache_default_servers,
			     NULL,
			     PGC_USERSET,
			     GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			     (GucStringCheckHook) check_default_guc,
#endif
			     (GucStringAssignHook) assign_default_servers_guc,
			     (GucShowHook) show_default_servers_guc);

  DefineCustomStringVariable ("pgmemcache.default_behavior",
			      "Comma-separated list of memcached behavior (optional).",
			      "Specified as a comma-separated list of behavior_flag:behavior_data.",
			      &memcache_default_behavior,
			      NULL,
			      PGC_USERSET,
			      GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			      (GucStringCheckHook) check_default_guc,
#endif
			      (GucStringAssignHook) assign_default_behavior_guc,
			      (GucShowHook) show_default_behavior_guc);

  DefineCustomStringVariable ("pgmemcache.sasl_authentication_username",
			      "pgmemcache SASL user authentication username",
			      "Simple string pgmemcache.sasl_authentication_username = '******'",
			      &memcache_sasl_authentication_username,
			      NULL,
			      PGC_USERSET,
			      GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			      (GucStringCheckHook) check_default_guc,
#endif
			      NULL,
			      (GucShowHook) show_memcache_sasl_authentication_username_guc);

  DefineCustomStringVariable ("pgmemcache.sasl_authentication_password",
			      "pgmemcache SASL user authentication username",
			      "Simple string pgmemcache.sasl_authentication_password = '******'",
			      &memcache_sasl_authentication_password,
			      NULL,
			      PGC_USERSET,
			      GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			      (GucStringCheckHook) check_default_guc,
#endif
			      NULL,
			      (GucShowHook) show_memcache_sasl_authentication_password_guc);
#if 0
  if ((memcache_sasl_authentication_username != "" && memcache_sasl_authentication_password != "") || (memcache_sasl_authentication_username != NULL && memcache_sasl_authentication_password != NULL)) {
    if  (sasl_client_init(NULL) != SASL_OK)
	elog(ERROR, "Failed to initialize SASL library!");
    memcached_set_sasl_callbacks(globals.mc, sasl_callbacks);
  }
#endif
}
Esempio n. 3
0
/*!
 * \brief Module initialization function
 * \return 0 on success, -1 on failure
 */
static int mod_init(void) {
	char *server, *port;
	unsigned int len = 0;
	memcached_return rc;
	struct memcached_server_st *svt;

	if ((port = strchr(mcd_srv_str, ':')) != NULL) {
		port = port + 1;
		len = strlen(mcd_srv_str) - strlen(port) - 1;
	} else {
		LM_DBG("no port definition, using default port\n");
		port = "11211";
		len = strlen(mcd_srv_str) ;
	}

	server = pkg_malloc(len+1);
	if (server == NULL) {
		PKG_MEM_ERROR;
		return -1;
	}

	strncpy(server, mcd_srv_str, len);
	server[len] = '\0';

	memcached_h = memcached_create(NULL);
	if (memcached_h == NULL) {
		LM_ERR("could not create memcached structure\n");
		pkg_free(server);
		return -1;
	}
	LM_DBG("allocated new server handle at %p", memcached_h);

        if (mcd_memory == 1) {
                LM_INFO("Use internal kamailio memory manager for memcached client library\n");

#if LIBMEMCACHED_VERSION_HEX >= 0x00038000
                rc = memcached_set_memory_allocators(memcached_h, (memcached_malloc_fn)mcd_malloc,
                                             (memcached_free_fn)mcd_free, (memcached_realloc_fn)mcd_realloc,
                                             (memcached_calloc_fn)mcd_calloc, NULL);
#else
                rc = memcached_set_memory_allocators(memcached_h, (memcached_malloc_function)mcd_malloc_compat,
                                             (memcached_free_function)mcd_free_compat, (memcached_realloc_function)mcd_realloc_compat,
                                             (memcached_calloc_function)mcd_calloc_compat);
#endif

		if (rc == MEMCACHED_SUCCESS) {
			LM_DBG("memory manager callbacks set\n");
		} else {
			LM_ERR("memory manager callbacks not set, returned %s.\n", memcached_strerror(memcached_h, rc));
			pkg_free(server);
			return -1;
		}
	} else {
		LM_INFO("Use system memory manager for memcached client library\n");
	}

	svt = memcached_server_list_append(servers, server, atoi(port), &rc);
	if(svt==NULL) {
		LM_ERR("failed to append server\n");
		if(servers) {
			memcached_server_list_free(servers);
			servers = NULL;
		}
		pkg_free(server);
		return -1;
	}

	servers = svt;
	if (memcached_behavior_set(memcached_h, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, mcd_timeout) != MEMCACHED_SUCCESS) {
		LM_ERR("could not set server connection timeout\n");
		pkg_free(server);
		return -1;
	}
	rc = memcached_server_push(memcached_h, servers);
	if (rc == MEMCACHED_SUCCESS) {
		LM_DBG("added server list to structure\n");
	} else {
		LM_ERR("attempt to add server list to structure returned %s.\n", memcached_strerror(memcached_h, rc));
		pkg_free(server);
		return -1;
	}

	pkg_free(server);

	/** \todo FIXME logic to handle connection errors on startup
	memcached_server_cursor(memcached_h, (const memcached_server_fn*) &mcd_check_connection, NULL, 1);
	*/

	LM_INFO("libmemcached version is %s\n", memcached_lib_version());
	return 0;
}
Esempio n. 4
0
void _PG_init(void)
{
  MemoryContext old_ctxt;

  globals.pg_ctxt = AllocSetContextCreate(TopMemoryContext,
					  "pgmemcache global context",
					  ALLOCSET_SMALL_MINSIZE,
					  ALLOCSET_SMALL_INITSIZE,
					  ALLOCSET_SMALL_MAXSIZE);

  old_ctxt = MemoryContextSwitchTo(globals.pg_ctxt);
  globals.mc = memcached_create(NULL);

  if (memcached_set_memory_allocators(globals.mc,
				      (memcached_malloc_fn) pgmemcache_malloc,
				      (memcached_free_fn) pgmemcache_free,
				      (memcached_realloc_fn) pgmemcache_realloc,
				      (memcached_calloc_fn) pgmemcache_calloc,
				      NULL) != MEMCACHED_SUCCESS) {
    elog(ERROR, "pgmemcache: unable to set memory allocators");
  }

  MemoryContextSwitchTo(old_ctxt);

  DefineCustomStringVariable("pgmemcache.default_servers",
			     "Comma-separated list of memcached servers to connect to.",
			     "Specified as a comma-separated list of host:port (port is optional).",
			     &memcache_default_servers,
			     NULL,
			     PGC_USERSET,
			     GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			     NULL,
#endif
			     assign_default_servers_guc,
			     show_default_servers_guc);

  DefineCustomStringVariable ("pgmemcache.default_behavior",
			      "Comma-separated list of memcached behavior (optional).",
			      "Specified as a comma-separated list of behavior_flag:behavior_data.",
			      &memcache_default_behavior,
			      NULL,
			      PGC_USERSET,
			      GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			      NULL,
#endif
			      assign_default_behavior_guc,
			      show_default_behavior_guc);

  DefineCustomStringVariable ("pgmemcache.sasl_authentication_username",
			      "pgmemcache SASL user authentication username",
			      "Simple string pgmemcache.sasl_authentication_username = '******'",
			      &memcache_sasl_authentication_username,
			      NULL,
			      PGC_USERSET,
			      GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			      NULL,
#endif
			      NULL,
			      show_memcache_sasl_authentication_username_guc);

  DefineCustomStringVariable ("pgmemcache.sasl_authentication_password",
			      "pgmemcache SASL user authentication password",
			      "Simple string pgmemcache.sasl_authentication_password = '******'",
			      &memcache_sasl_authentication_password,
			      NULL,
			      PGC_USERSET,
			      GUC_LIST_INPUT,
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 90100)
			      NULL,
#endif
			      NULL,
			      show_memcache_sasl_authentication_password_guc);
#if LIBMEMCACHED_WITH_SASL_SUPPORT
  if ((strlen(memcache_sasl_authentication_username) > 0 && strlen(memcache_sasl_authentication_password) > 0) || (memcache_sasl_authentication_username != NULL && memcache_sasl_authentication_password != NULL)) {

        rc = memcached_set_sasl_auth_data(globals.mc, memcache_sasl_authentication_username, memcache_sasl_authentication_password);
        if (rc != MEMCACHED_SUCCESS) {
	    elog(ERROR, "%s ", memcached_strerror(globals.mc, rc));
        }
        _init_sasl();
      }
#endif
}