📄 jk_shm.c
字号:
/* ========================================================================= * * * * The Apache Software License, Version 1.1 * * * * Copyright (c) 1999-2002 The Apache Software Foundation. * * All rights reserved. * * * * ========================================================================= * * * * Redistribution and use in source and binary forms, with or without modi- * * fication, are permitted provided that the following conditions are met: * * * * 1. Redistributions of source code must retain the above copyright notice * * notice, this list of conditions and the following disclaimer. * * * * 2. Redistributions in binary form must reproduce the above copyright * * notice, this list of conditions and the following disclaimer in the * * documentation and/or other materials provided with the distribution. * * * * 3. The end-user documentation included with the redistribution, if any, * * must include the following acknowlegement: * * * * "This product includes software developed by the Apache Software * * Foundation <http://www.apache.org/>." * * * * Alternately, this acknowlegement may appear in the software itself, if * * and wherever such third-party acknowlegements normally appear. * * * * 4. The names "The Jakarta Project", "Jk", and "Apache Software * * Foundation" must not be used to endorse or promote products derived * * from this software without prior written permission. For written * * permission, please contact <apache@apache.org>. * * * * 5. Products derived from this software may not be called "Apache" nor may * * "Apache" appear in their names without prior written permission of the * * Apache Software Foundation. * * * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES * * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * * THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY * * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * * POSSIBILITY OF SUCH DAMAGE. * * * * ========================================================================= * * * * This software consists of voluntary contributions made by many indivi- * * duals on behalf of the Apache Software Foundation. For more information * * on the Apache Software Foundation, please see <http://www.apache.org/>. * * * * ========================================================================= *//** * Scoreboard - used for communication on multi-process servers. * * This is an optional component - will be enabled only if APR is used. * The code is cut&pasted from apache and mod_jserv. * * * * @author Jserv and Apache people */#include "jk_global.h"#include "jk_map.h"#include "jk_pool.h"#include "jk_shm.h"/* global.h will include apr.h. If APR and mmap is compiled in, we'll use it. If APR is not availalbe, we use mmap directly - the code worked fine for jserv.*/#if APR_HAS_MMAP#include "apr_mmap.h"#include "apr_file_io.h"#include "apr_file_info.h"#include "apr_general.h"#elif defined(HAVE_MMAP) && !defined(WIN32)#include <sys/mman.h>#include <fcntl.h>#endif#define SHM_WRITE_SLOT 2#define SHM_RESET 5#define SHM_DUMP 6#ifdef APR_HAS_MMAP static int JK_METHOD jk2_shm_destroy(jk_env_t *env, jk_shm_t *shm){ apr_mmap_t *aprShm=(apr_mmap_t *)shm->privateData; if( aprShm==NULL ) return JK_OK; return apr_mmap_delete(aprShm);}static int jk2_shm_create(jk_env_t *env, jk_shm_t *shm){ int rc; apr_file_t *file; apr_finfo_t finfo; apr_mmap_t *aprMmap; apr_pool_t *globalShmPool; globalShmPool= (apr_pool_t *)env->getAprPool( env ); if( globalShmPool==NULL ) return JK_FALSE; /* Check if the scoreboard is in a note. That's the only way we can get HP-UX to work */ apr_pool_userdata_get( & shm->image, "mod_jk_shm",globalShmPool ); if( shm->image!=NULL ) { shm->head = (jk_shm_head_t *)shm->image; if( shm->mbean->debug > 0 ) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "shm.create(): GLOBAL_SHM %#lx\n", shm->image ); return JK_OK; } else { if( shm->mbean->debug > 0 ) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "shm.create(): NO GLOBAL_SHM %#lx\n", shm->image ); } /* First make sure the file exists and is big enough */ rc=apr_file_open( &file, shm->fname, APR_READ | APR_WRITE | APR_CREATE | APR_BINARY, APR_OS_DEFAULT, globalShmPool); if (rc!=JK_OK) { char error[256]; apr_strerror( rc, error, 256 ); env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): error opening file %s %d %s\n", shm->fname, rc, error ); shm->privateData=NULL; return rc; } rc=apr_file_info_get(&finfo, APR_FINFO_SIZE, file); if( shm->mbean->debug > 0 ) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "shm.create(): file open %s %d %d\n", shm->fname, shm->size, finfo.size ); if( finfo.size < shm->size ) { char bytes[1024]; apr_size_t toWrite = (apr_size_t)(shm->size-finfo.size); apr_off_t off=0; memset( bytes, 0, 1024 ); apr_file_seek( file, APR_END, &off); while( toWrite > 0 ) { apr_size_t written; rc=apr_file_write_full(file, bytes, 1024, &written); if( rc!=APR_SUCCESS ) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): Can't write %s %d %s\n", shm->fname, errno, strerror( errno )); return JK_ERR; } if( toWrite < written ){ toWrite=0; }else{ toWrite-=written; } } rc=apr_file_info_get(&finfo, APR_FINFO_SIZE, file); } /* Now mmap it */ rc=apr_mmap_create( &aprMmap, file, (apr_off_t)0, (apr_size_t)finfo.size, APR_MMAP_READ | APR_MMAP_WRITE, globalShmPool ); if( rc!=JK_OK ) { char error[256]; apr_strerror( rc, error, 256 ); env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): error creating %s %d %d %#lx %s\n", shm->fname, finfo.size, rc, globalShmPool, error ); shm->privateData=NULL; return rc; } shm->privateData=aprMmap; apr_mmap_offset(& shm->image, aprMmap, (apr_off_t)0); apr_pool_userdata_set( shm->image, "mod_jk_shm", NULL, globalShmPool ); shm->head = (jk_shm_head_t *)shm->image; if( shm->image==NULL ) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): No base memory %s\n", shm->fname); return JK_ERR; } return JK_OK;}#elif defined(HAVE_MMAP) && !defined(WIN32)static int JK_METHOD jk2_shm_destroy(jk_env_t *env, jk_shm_t *shm){ caddr_t shmf=(caddr_t)shm->privateData; munmap(shmf, shm->size); return JK_OK;}static int jk2_shm_create(jk_env_t *env, jk_shm_t *shm){ int rc; struct stat filestat; int fd = open(shm->fname, O_RDWR|O_CREAT, 0777); if (fd == -1) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): Can't open %s %d %s\n", shm->fname, errno, strerror( errno )); return JK_ERR; } rc=stat( shm->fname, &filestat); if ( rc == -1) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): Can't stat %s %d %s\n", shm->fname, errno, strerror( errno )); return JK_ERR; } if( shm->mbean->debug > 0 ) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "shm.create(): file open %s %d %d\n", shm->fname, shm->size, filestat.st_size ); if (filestat.st_size < shm->size ) { char bytes[1024]; int toWrite=shm->size - filestat.st_size; memset( bytes, 0, 1024 ); lseek(fd, 0, SEEK_END); while( toWrite > 0 ) { int written; written=write(fd, bytes, 1024); if( written == -1 ) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): Can't write %s %d %s\n", shm->fname, errno, strerror( errno )); return JK_ERR; } toWrite-=written; } rc=stat( shm->fname, &filestat); if ( rc == -1) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): Can't stat2 %s %d %s\n", shm->fname, errno, strerror( errno )); return JK_ERR; } } shm->privateData = mmap(NULL, filestat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shm->privateData == (caddr_t)-1 || shm->privateData == NULL ) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "shm.create(): Can't mmap %s %d %s\n", shm->fname, errno, strerror( errno )); close(fd); return JK_ERR; } shm->image = (void *)shm->privateData; shm->head = (jk_shm_head_t *)shm->image; close(fd); return JK_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -