mturk 2005/02/13 04:08:05 Modified: jk/native/common jk_shm.c jk_shm.h Log: Add logging and global lock file to shared memory. Revision Changes Path 1.10 +157 -16 jakarta-tomcat-connectors/jk/native/common/jk_shm.c Index: jk_shm.c =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_shm.c,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- jk_shm.c 12 Feb 2005 17:01:00 -0000 1.9 +++ jk_shm.c 13 Feb 2005 12:08:05 -0000 1.10 @@ -31,7 +31,10 @@ char magic[8]; size_t size; size_t pos; - int childs; + unsigned int childs; + unsigned int workers; + + unsigned int urimaps; time_t modified; char buf[1]; }; @@ -44,50 +47,71 @@ size_t size; const char *filename; int fd; + int fd_lock; int attached; jk_shm_header_t *hdr; - CRITICAL_SECTION cs; + JK_CRIT_SEC cs; }; typedef struct jk_shm jk_shm_t; static const char shm_signature[] = { JK_SHM_MAGIC }; -static jk_shm_t jk_shmem = { 0, NULL, -1, 0, NULL}; +static jk_shm_t jk_shmem = { 0, NULL, -1, -1, 0, NULL, 0}; static time_t jk_workers_modified_time = 0; #if defined (WIN32) || defined(NETWARE) /* Use plain memory */ -int jk_shm_open(const char *fname) +int jk_shm_open(const char *fname, jk_logger_t *l) { int rc; + JK_TRACE_ENTER(l); if (jk_shmem.hdr) { + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, "Shared memory is already opened"); + JK_TRACE_EXIT(l); return 0; } jk_shmem.size = JK_SHM_ALIGN(sizeof(jk_shm_header_t) + JK_SHM_SIZE); jk_shmem.hdr = (jk_shm_header_t *)calloc(1, jk_shmem.size); - if (!jk_shmem.hdr) + if (!jk_shmem.hdr) { + JK_TRACE_EXIT(l); return -1; + } jk_shmem.filename = "memory"; jk_shmem.fd = 0; jk_shmem.attached = 0; memcpy(jk_shmem.hdr->magic, shm_signature, 8); jk_shmem.hdr->size = JK_SHM_SIZE; JK_INIT_CS(&(jk_shmem.cs), rc); + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Initialized shared memory size=%u free=%u", + jk_shmem.size, jk_shmem.hdr->size); + JK_TRACE_EXIT(l); return 0; } -int jk_shm_attach(const char *fname) +int jk_shm_attach(const char *fname, jk_logger_t *l) { - if (!jk_shm_open(fname)) { + JK_TRACE_ENTER(l); + if (!jk_shm_open(fname, l)) { jk_shmem.attached = 1; jk_shmem.hdr->childs++; + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Attached shared memory [%d] size=%u free=%u", + jk_shmem.hdr->childs, jk_shmem.hdr->size, + jk_shmem.hdr->size - jk_shmem.hdr->pos); + JK_TRACE_EXIT(l); return 0; } - else + else { + JK_TRACE_EXIT(l); return -1; + } } void jk_shm_close() @@ -117,15 +141,63 @@ #define MAP_FILE (0) #endif -static int do_shm_open(const char *fname, int attached) +static int do_shm_open_lock(const char *fname, int attached, jk_logger_t *l) +{ + int rc; + int fd; + int flags = O_RDWR; + char flkname[256]; + JK_TRACE_ENTER(l); + + jk_shmem.fd_lock = -1; + strcpy(flkname, fname); + strcat(flkname, ".lock"); + if (!attached) + flags |= (O_CREAT|O_TRUNC); + fd = open(flkname, flags, 0666); + if (fd == -1) { + JK_TRACE_EXIT(l); + return errno; + } + + if (!attached) { + if (ftruncate(fd, 1)) { + rc = errno; + close(fd); + JK_TRACE_EXIT(l); + return rc; + } + } + if (lseek(fd, 0, SEEK_SET) != 0) { + rc = errno; + close(fd); + JK_TRACE_EXIT(l); + return rc; + } + jk_shmem.fd_lock = fd; + + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Opened shared memory lock %s", flkname); + JK_TRACE_EXIT(l); + return 0; +} + +static int do_shm_open(const char *fname, int attached, jk_logger_t *l) { int rc; int fd; int flags = O_RDWR; void *base; - if (jk_shmem.hdr) - jk_shm_close(); + JK_TRACE_ENTER(l); + if (jk_shmem.hdr) { + /* Probably a call from vhost */ + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Shared memory is already open"); + return 0; + } jk_shmem.filename = fname; jk_shmem.attached = attached; @@ -134,6 +206,10 @@ /* Use plain memory in case there is no file name */ if (!fname) { jk_shmem.filename = "memory"; + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Using process memory as shared memory"); + JK_TRACE_EXIT(l); return 0; } @@ -142,6 +218,7 @@ fd = open(fname, flags, 0666); if (fd == -1) { jk_shmem.size = 0; + JK_TRACE_EXIT(l); return errno; } @@ -153,14 +230,19 @@ rc = errno; close(fd); jk_shmem.size = 0; + JK_TRACE_EXIT(l); return rc; } + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Truncated shared memory to %u", size); } } if (lseek(fd, 0, SEEK_SET) != 0) { rc = errno; close(fd); jk_shmem.size = 0; + JK_TRACE_EXIT(l); return rc; } @@ -172,6 +254,7 @@ rc = errno; close(fd); jk_shmem.size = 0; + JK_TRACE_EXIT(l); return rc; } jk_shmem.hdr = base; @@ -182,33 +265,55 @@ memset(jk_shmem.hdr, 0, jk_shmem.size); memcpy(jk_shmem.hdr->magic, shm_signature, 8); jk_shmem.hdr->size = JK_SHM_SIZE; + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_DEBUG, + "Initialized shared memory size=%u free=%u", + jk_shmem.size, jk_shmem.hdr->size); } else { jk_shmem.hdr->childs++; + if (JK_IS_DEBUG_LEVEL(l)) + jk_log(l, JK_LOG_INFO, + "Attached shared memory [%d] size=%u free=%u", + jk_shmem.hdr->childs, jk_shmem.hdr->size, + jk_shmem.hdr->size - jk_shmem.hdr->pos); /* TODO: check header magic */ } JK_INIT_CS(&(jk_shmem.cs), rc); + if ((rc = do_shm_open_lock(fname, attached, l))) { + munmap((void *)jk_shmem.hdr, jk_shmem.size); + close(jk_shmem.fd); + jk_shmem.hdr = NULL; + jk_shmem.fd = -1; + JK_TRACE_EXIT(l); + return rc; + } + JK_TRACE_EXIT(l); return 0; } -int jk_shm_open(const char *fname) +int jk_shm_open(const char *fname, jk_logger_t *l) { - return do_shm_open(fname, 0); + return do_shm_open(fname, 0, l); } -int jk_shm_attach(const char *fname) +int jk_shm_attach(const char *fname, jk_logger_t *l) { - return do_shm_open(fname, 1); + return do_shm_open(fname, 1, l); } void jk_shm_close() { int rc; if (jk_shmem.hdr) { + if (jk_shmem.fd_lock >= 0) { + close(jk_shmem.fd_lock); + } if (jk_shmem.fd >= 0) { munmap((void *)jk_shmem.hdr, jk_shmem.size); close(jk_shmem.fd); } + jk_shmem.fd_lock = -1; } if (jk_shmem.size) JK_DELETE_CS(&(jk_shmem.cs), rc); @@ -263,6 +368,9 @@ { int rc; JK_ENTER_CS(&(jk_shmem.cs), rc); + if (rc == JK_TRUE && jk_shmem.fd_lock != -1) { + JK_ENTER_LOCK(jk_shmem.fd_lock, rc); + } return rc; } @@ -270,5 +378,38 @@ { int rc; JK_LEAVE_CS(&(jk_shmem.cs), rc); + if (rc == JK_TRUE && jk_shmem.fd_lock != -1) { + JK_LEAVE_LOCK(jk_shmem.fd_lock, rc); + } return rc; } + +jk_shm_worker_t *jk_shm_alloc_worker(jk_pool_t *p) +{ + jk_shm_worker_t *w = (jk_shm_worker_t *)jk_shm_alloc(p, sizeof(jk_shm_worker_t)); + if (w) { + memset(w, 0, sizeof(jk_shm_worker_t)); + if (jk_shmem.hdr) { + jk_shmem.hdr->workers++; + w->id = jk_shmem.hdr->workers; + } + else + w->id = -1; + } + return w; +} + +jk_shm_urimap_t *jk_shm_alloc_urimap(jk_pool_t *p) +{ + jk_shm_urimap_t *u = (jk_shm_urimap_t *)jk_shm_alloc(p, sizeof(jk_shm_urimap_t)); + if (u) { + memset(u, 0, sizeof(jk_shm_urimap_t)); + if (jk_shmem.hdr) { + jk_shmem.hdr->urimaps++; + u->id = jk_shmem.hdr->urimaps; + } + else + u->id = -1; + } + return u; +} 1.8 +27 -3 jakarta-tomcat-connectors/jk/native/common/jk_shm.h Index: jk_shm.h =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native/common/jk_shm.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- jk_shm.h 12 Feb 2005 17:01:00 -0000 1.7 +++ jk_shm.h 13 Feb 2005 12:08:05 -0000 1.8 @@ -24,6 +24,7 @@ #include "jk_global.h" #include "jk_pool.h" +#include "jk_util.h" #ifdef __cplusplus extern "C" @@ -40,6 +41,7 @@ #define JK_SHM_MAJOR '1' #define JK_SHM_MINOR '0' #define JK_SHM_STR_SIZ 63 +#define JK_SHM_URI_SIZ 127 #define JK_SHM_DYNAMIC 16 #define JK_SHM_MAGIC '!', 'J', 'K', 'S', 'H', 'M', JK_SHM_MAJOR, JK_SHM_MINOR @@ -90,11 +92,23 @@ }; typedef struct jk_shm_worker jk_shm_worker_t; +/** jk shm uri worker map record structure */ +struct jk_shm_urimap +{ + int id; + unsigned int match_type; + size_t ctxt_len; + char context[JK_SHM_URI_SIZ+1]; + char worker[JK_SHM_STR_SIZ+1]; + char suffix[JK_SHM_STR_SIZ+1]; +}; +typedef struct jk_shm_urimap jk_shm_urimap_t; + const char *jk_shm_name(); /* Open the shared memory creating file if needed */ -int jk_shm_open(const char *fname); +int jk_shm_open(const char *fname, jk_logger_t *l); /* Close the shared memory */ @@ -103,13 +117,23 @@ /* Attach the shared memory in child process. * File has to be opened in parent. */ -int jk_shm_attach(const char *fname); +int jk_shm_attach(const char *fname, jk_logger_t *l); /* allocate shm memory * If there is no shm present the pool will be used instead */ void *jk_shm_alloc(jk_pool_t *p, size_t size); +/* allocate shm worker record + * If there is no shm present the pool will be used instead + */ +jk_shm_worker_t *jk_shm_alloc_worker(jk_pool_t *p); + +/* allocate shm uri worker map record + * If there is no shm present the pool will be used instead + */ +jk_shm_urimap_t *jk_shm_alloc_urimap(jk_pool_t *p); + /* Return workers.properties last modified time */ time_t jk_shm_get_workers_time();
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]