You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Phil Endecott <sp...@chezphil.org> on 2005/03/23 19:38:02 UTC

[users@httpd] Shared memory difficulties

Dear Apache Experts,

Has anyone any experience with Apache modules and shared memory?  I am 
having difficulty attaching to a shared memory block from within a child 
process.

Basically, I'm creating a shared memory region from the post_config hook 
and then attaching to it in each child from the child_init hook.  The 
apr_shm_create call succeeds, but the apr_shm_attach calls fail with "No 
such file or directory".  This is Apache 2.0 on Linux.

I would be happy to use anonymous shared memory, but I don't see how to 
do it: if I pass NULL as the filename to apr_shm_create, what do I pass 
to apr_shm_attach to identify the region to attach to?  Since I don't 
understand that I'm using named regions with /tmp/foo as the filename.

This is my first attempt at writing an Apache module so my mistake is 
probably something basic.


Here is my test program:

#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_script.h"
#include "http_connection.h"
#include "apr_strings.h"

#include <unistd.h>


module AP_MODULE_DECLARE_DATA shm_test;


static int already_init(server_rec* s)
{
   void* data;
   const char* userdata_key="shm_test_init_flag";

   apr_pool_userdata_get(&data, userdata_key, s->process->pool);
   if (!data) {
     apr_pool_userdata_set((const void *) 1, userdata_key,
			  apr_pool_cleanup_null, s->process->pool);
   }
   return !!data;
}


apr_shm_t* shm_test_shm;


static int shm_test_post_config(apr_pool_t *pconf, apr_pool_t *plog,
			       apr_pool_t *ptemp, server_rec *s)
{
   if (already_init(s)) {
     return OK;
   }

   int sz = 1024;

   int rc = apr_shm_create(&shm_test_shm, sz, "/tmp/foo", pconf);
   if (rc != APR_SUCCESS) {
     ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
		 "Failed to create anonymous shared memory segment");
     return HTTP_INTERNAL_SERVER_ERROR;
   }

   return OK;
}


static void shm_test_child_init(apr_pool_t *p, server_rec *s)
{
   int rc = apr_shm_attach(&shm_test_shm, "/tmp/foo", p);
   if (rc != APR_SUCCESS) {
     ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
		 "Failed to attach to anonymous shared memory segment in process 
%d",getpid());
     return;
   }
}


static void shm_test_register_hooks(apr_pool_t *p)
{
   ap_hook_post_config(shm_test_post_config, NULL, NULL, APR_HOOK_MIDDLE);
   ap_hook_child_init(shm_test_child_init, NULL, NULL, APR_HOOK_MIDDLE);
}



module AP_MODULE_DECLARE_DATA shm_test = {
   STANDARD20_MODULE_STUFF,
   NULL, /* per-directory config creator */
   NULL, /* dir config merger */
   NULL, /* server config creator */
   NULL, /* server config merger */
   NULL, /* command table */
   shm_test_register_hooks,  /* set up other request processing hooks */
};


I compile and install this using apxs:

$ apxs2 -c -Wc,-std=c99 shm_test.c
/usr/bin/libtool --silent --mode=compile gcc -prefer-pic -pipe 
-I/usr/include/xmltok -I/usr/include/openssl -Wall -O2 
-DAP_HAVE_DESIGNATED_INITIALIZER -DLINUX=2 -D_REENTRANT 
-D_XOPEN_SOURCE=500 -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE -pipe 
-I/usr/include/xmltok -I/usr/include/openssl -Wall -O2 -pthread 
-I/usr/include/apache2  -I/usr/include/apr-0   -I/usr/include/apr-0 
-I/usr/include -std=c99  -c -o shm_test.lo shm_test.c && touch shm_test.slo
/usr/bin/libtool --silent --mode=link gcc -o shm_test.la  -rpath 
/usr/lib/apache2/modules -module -avoid-version    shm_test.lo

# apxs2 -n shm_test -i shm_test.la
[snip]

I have this in httpd.conf:

LoadModule shm_test /usr/lib/apache2/modules/shm_test.so


When I restart Apache, I get this:

[Wed Mar 23 18:20:37 2005] [error] (2)No such file or directory: Failed 
to attach to anonymous shared memory segment in process 28548
[Wed Mar 23 18:20:37 2005] [error] (2)No such file or directory: Failed 
to attach to anonymous shared memory segment in process 28549
[Wed Mar 23 18:20:37 2005] [error] (2)No such file or directory: Failed 
to attach to anonymous shared memory segment in process 28550
[Wed Mar 23 18:20:37 2005] [error] (2)No such file or directory: Failed 
to attach to anonymous shared memory segment in process 28551
[Wed Mar 23 18:20:37 2005] [error] (2)No such file or directory: Failed 
to attach to anonymous shared memory segment in process 28552


Any suggestions, debugging hints, or pointers to documentation or 
example code would be much appreciated.

Thanks,

--Phil.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org