Author: mturk Date: Thu Aug 6 08:10:29 2009 New Revision: 801535 URL: http://svn.apache.org/viewvc?rev=801535&view=rev Log: Add shared memory and proc mutex. POSIX only
Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_procmutex.h (with props) commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c (with props) commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c (with props) Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in commons/sandbox/runtime/trunk/src/main/native/configure commons/sandbox/runtime/trunk/src/main/native/include/acr_openssl.h commons/sandbox/runtime/trunk/src/main/native/include/arch/darwin/acr_arch_private.h commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h commons/sandbox/runtime/trunk/src/main/native/os/unix/ios.c commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c commons/sandbox/runtime/trunk/src/main/native/test/testcase.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Thu Aug 6 08:10:29 2009 @@ -101,6 +101,8 @@ $(SRCDIR)/os/unix/ios.$(OBJ) \ $(SRCDIR)/os/unix/group.$(OBJ) \ $(SRCDIR)/os/unix/user.$(OBJ) \ + $(SRCDIR)/os/unix/pmutex.$(OBJ) \ + $(SRCDIR)/os/unix/shm.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ @@ -118,6 +120,8 @@ $(SRCDIR)/os/unix/ios.$(OBJ) \ $(SRCDIR)/os/unix/group.$(OBJ) \ $(SRCDIR)/os/unix/user.$(OBJ) \ + $(SRCDIR)/os/unix/pmutex.$(OBJ) \ + $(SRCDIR)/os/unix/shm.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ @@ -133,6 +137,7 @@ $(SRCDIR)/os/unix/ios.$(OBJ) \ $(SRCDIR)/os/unix/group.$(OBJ) \ $(SRCDIR)/os/unix/user.$(OBJ) \ + $(SRCDIR)/os/unix/shm.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ @@ -148,6 +153,8 @@ $(SRCDIR)/os/unix/ios.$(OBJ) \ $(SRCDIR)/os/unix/group.$(OBJ) \ $(SRCDIR)/os/unix/user.$(OBJ) \ + $(SRCDIR)/os/unix/pmutex.$(OBJ) \ + $(SRCDIR)/os/unix/shm.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/uuid.$(OBJ) \ $(SRCDIR)/os/unix/uutils.$(OBJ) \ @@ -170,13 +177,13 @@ $(STATICLIB): $(COMMON_OBJS) $(@platf...@_objs) @testobjs@ $(AR) $(ARFLAGS) $@ $(COMMON_OBJS) $(@platf...@_objs) @testobjs@ - -...@$(RANLIB) $@ + $(RANLIB) $@ $(SHAREDLIB): $(COMMON_OBJS) $(@platf...@_objs) @testobjs@ $(STATICLIB) - $(CC) $(SHFLAGS) -o $@ $(COMMON_OBJS) $(@platf...@_objs) @testobjs@ $(LDFLAGS) + $(CC) $(SHFLAGS) $(COMMON_OBJS) $(@platf...@_objs) @testobjs@ $(LDFLAGS) -o $@ $(SSLMODLIB): $(SHAREDLIB) $(OPENSSL_OBJS) - $(CC) $(SHFLAGS) -o $@ $(OPENSSL_OBJS) $(LDFLAGS) $(SSLFLAGS) -L. -l$(NAME) + $(CC) $(SHFLAGS) $(OPENSSL_OBJS) $(LDFLAGS) $(SSLFLAGS) -L. -l$(NAME) -o $@ clean: for i in $(SRCDIR) $(ARCH_DIRS) ; do rm -f $$i/*.$(OBJ) ; done Modified: commons/sandbox/runtime/trunk/src/main/native/configure URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/configure (original) +++ commons/sandbox/runtime/trunk/src/main/native/configure Thu Aug 6 08:10:29 2009 @@ -797,7 +797,9 @@ fi have_openssl=1 modules="\$(SSLMODLIB)" + have_ocsp=`have_include openssl/ocsp` else + have_ocsp=0 have_openssl=0 fi @@ -852,6 +854,7 @@ #define HAVE_LIBAIO_H $have_libaio #define HAVE_UUID_UUID_H $have_uuid_uuid #define HAVE_OPENSSL $have_openssl +#define HAVE_OCSP $have_ocsp #define HAVE_ZLIB $have_zlib #define HAVE_SELINUX_H $have_selinux Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_openssl.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_openssl.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_openssl.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_openssl.h Thu Aug 6 08:10:29 2009 @@ -48,6 +48,12 @@ #include <openssl/rand.h> #include <openssl/x509v3.h> #include <openssl/md5.h> + +#ifdef HAVE_OCSP +#include <openssl/x509_vfy.h> +#include <openssl/ocsp.h> +#endif + /* Avoid tripping over an engine build installed globally and detected * when the user points at an explicit non-engine flavor of OpenSSL */ Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_procmutex.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_procmutex.h?rev=801535&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_procmutex.h (added) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_procmutex.h Thu Aug 6 08:10:29 2009 @@ -0,0 +1,97 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _ACR_PROCMUTEX_H +#define _ACR_PROCMUTEX_H + +#include "acr.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file acr_procmutex.h + * @brief + * + * ACR Process mutex functions + * + */ + +/** + * Private, platform-specific data struture representing a mutex. + */ +typedef struct acr_pmutex_t acr_pmutex_t; + + +/** + * Create and initialize a mutex that can be used to synchronize processes. + * @param env JNI environment to use. + * @param fname A file name to use if the lock mechanism requires one. This + * argument should always be provided. The lock code itself will + * determine if it should be used. + */ +ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *env, const acr_pchar_t *fname); + +/** + * Acquire the lock for the given mutex. If the mutex is already locked, + * the current thread will be put to sleep until the lock becomes available. + * @param env JNI environment to use. + * @param mutex the mutex on which to acquire the lock. + */ +ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *env, int mutex); + +/** + * Attempt to acquire the lock for the given mutex. If the mutex has already + * been acquired, the call returns immediately with APR_EBUSY. Note: it + * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine + * if the return value was APR_EBUSY, for portability reasons. + * @param env JNI environment to use. + * @param mutex the mutex on which to attempt the lock acquiring. + */ +ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *env, int mutex); + +/** + * Release the lock for the given mutex. + * @param env JNI environment to use. + * @param mutex the mutex from which to release the lock. + */ +ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *env, int mutex); + +/** + * Set mutex permissions. + * @param env JNI environment to use. + * @param mutex the mutex for which to set the permissions. + * @param perms Permissions to set. See ACR_FPROT_ flags. + * @param uid User that will own the mutex. + * @param gid Group that will own the mutex. + */ +ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *env, int mutex, int perms, + acr_uid_t uid, acr_uid_t gid); + +/** + * Destroy the mutex and free the memory associated with the lock. + * @param env JNI environment to use. + * @param mutex the mutex to destroy. + */ +ACR_DECLARE(int) ACR_ProcMutexClose(JNIEnv *env, int mutex); + +#ifdef __cplusplus +} +#endif + +#endif /* _ACR_PROCMUTEX_H */ + Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr_procmutex.h ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h?rev=801535&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h (added) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h Thu Aug 6 08:10:29 2009 @@ -0,0 +1,144 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _ACR_SHM_H +#define _ACR_SHM_H + +#include "acr.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file acr_shm.h + * @brief + * + * ACR Shared memory functions + * + */ + +/** + * Private, platform-specific data struture representing a shared memory + * segment. + */ +typedef struct acr_shm_t acr_shm_t; + + +/** + * Create and make accessable a shared memory segment. + * @param env JNI environment to use. + * @param reqsize The desired size of the segment. + * @param filename The file to use for shared memory on platforms that + * require it. + * @remark A note about Anonymous vs. Named shared memory segments: + * Not all plaforms support anonymous shared memory segments, but in + * some cases it is prefered over other types of shared memory + * implementations. Passing a NULL 'file' parameter to this function + * will cause the subsystem to use anonymous shared memory segments. + * If such a system is not available, APR_ENOTIMPL is returned. + * @remark A note about allocation sizes: + * On some platforms it is necessary to store some metainformation + * about the segment within the actual segment. In order to supply + * the caller with the requested size it may be necessary for the + * implementation to request a slightly greater segment length + * from the subsystem. In all cases, the apr_shm_baseaddr_get() + * function will return the first usable byte of memory. + * @return The shared memory structure to create. + * + */ +ACR_DECLARE(int) ACR_ShmCreate(JNIEnv *env, acr_size_t reqsize, + const acr_pchar_t *filename); + +/** + * Attach to a shared memory segment that was created + * by another process. + * @param env JNI environment to use. + * @param filename The file used to create the original segment. + * (This MUST match the original filename.) + * @return The shared memory structure to create. + */ +ACR_DECLARE(int) ACR_ShmAttach(JNIEnv *env, + const acr_pchar_t *filename); + +/** + * Destroy a shared memory segment and associated memory. + * @param env JNI environment to use. + * @param m The shared memory segment structure to destroy. + */ +ACR_DECLARE(int) ACR_ShmDestroy(JNIEnv *env, int m); + +/** + * Detach from a shared memory segment without destroying it. + * @param env JNI environment to use. + * @param m The shared memory structure representing the segment + * to detach from. + */ +ACR_DECLARE(int) ACR_ShmDetach(JNIEnv *env, int m); + +/** + * Remove named resource associated with a shared memory segment, + * preventing attachments to the resource, but not destroying it. + * @param env JNI environment to use. + * @param filename The filename associated with shared-memory segment which + * needs to be removed + * @remark This function is only supported on platforms which support + * name-based shared memory segments, and will return APR_ENOTIMPL on + * platforms without such support. Removing the file while the shm + * is in use is not entirely portable, caller may use this to enhance + * obscurity of the resource, but be prepared for the the call to fail, + * and for concurrent attempts to create a resource of the same name + * to also fail. The pool cleanup of apr_shm_create (apr_shm_destroy) + * also removes the named resource. + */ +ACR_DECLARE(int) ACR_ShmRemove(JNIEnv *env, const acr_pchar_t *filename); + +/** + * Retrieve the base address of the shared memory segment. + * NOTE: This address is only usable within the callers address + * space, since this API does not guarantee that other attaching + * processes will maintain the same address mapping. + * @param m The shared memory segment from which to retrieve + * the base address. + * @return address, aligned by APR_ALIGN_DEFAULT. + */ +ACR_DECLARE(void *) ACR_ShmGetBaseAddr(int m); + +/** + * Retrieve the length of a shared memory segment in bytes. + * @param m The shared memory segment from which to retrieve + * the segment length. + */ +ACR_DECLARE(acr_size_t) ACR_ShmGetSize(int m); + +/** + * Set shared memory permissions. + * @param env JNI environment to use. + * @param shm the shared for which to set the permissions. + * @param perms Permissions to set. See ACR_FPROT_ flags. + * @param uid User that will own the mutex. + * @param gid Group that will own the mutex. + */ +ACR_DECLARE(int) ACR_ShmPermSet(JNIEnv *env, int shm, int perms, + acr_uid_t uid, acr_uid_t gid); + + +#ifdef __cplusplus +} +#endif + +#endif /* _ACR_SHM_H */ + Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/darwin/acr_arch_private.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/darwin/acr_arch_private.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/arch/darwin/acr_arch_private.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/arch/darwin/acr_arch_private.h Thu Aug 6 08:10:29 2009 @@ -41,8 +41,31 @@ #define ACR_JNI_PLATFORM_DECLARE(RT, CL, FN) \ JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_platform_darwin_##CL##_##FN +/** + * Temporary APR flags + */ +#define APR_USE_SHMEM_SHMGET 1 +#define APR_USE_SHMEM_MMAP_ANON 1 +#define APR_USE_FCNTL_SERIALIZE 1 +#define APR_USE_PTHREAD_SERIALIZE 1 +#define APR_PROCESS_LOCK_IS_GLOBAL 0 +#define APR_HAVE_CORKABLE_TCP 0 +#define APR_HAVE_SA_STORAGE 0 +#define APR_HAVE_UNION_SEMUN 0 +#define APR_HAVE_SCTP 0 +#define APR_HAS_SO_ACCEPTFILTER 0 +#define APR_HAS_UNICODE_FS 0 +#define APR_HAS_PROC_INVOKED 0 +#define APR_HAS_XTHREAD_FILES 0 +#define APR_HAS_OS_UUID 0 +#define APR_TCP_NOPUSH_FLAG 0 +#define APR_TCP_NODELAY_INHERITED 1 +#define APR_O_NONBLOCK_INHERITED 1 +#define APR_HAS_LARGE_FILES 1 + #ifdef __cplusplus } #endif #endif /* ACR_ARCH_PRIVATE_H */ + Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h Thu Aug 6 08:10:29 2009 @@ -37,8 +37,31 @@ #define ACR_JNI_PLATFORM_DECLARE(RT, CL, FN) \ JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_platform_hpux_##CL##_##FN +/** + * Temporary APR flags + */ +#define APR_USE_SHMEM_SHMGET 0 +#define APR_USE_SHMEM_MMAP_ANON 1 +#define APR_USE_SYSVSEM_SERIALIZE 1 +#define APR_USE_PTHREAD_SERIALIZE 1 +#define APR_PROCESS_LOCK_IS_GLOBAL 0 +#define APR_HAVE_CORKABLE_TCP 0 +#define APR_HAVE_SA_STORAGE 0 +#define APR_HAVE_UNION_SEMUN 0 +#define APR_HAVE_SCTP 0 +#define APR_HAS_SO_ACCEPTFILTER 0 +#define APR_HAS_UNICODE_FS 0 +#define APR_HAS_PROC_INVOKED 0 +#define APR_HAS_XTHREAD_FILES 0 +#define APR_HAS_OS_UUID 0 +#define APR_TCP_NOPUSH_FLAG 0 +#define APR_TCP_NODELAY_INHERITED 0 +#define APR_O_NONBLOCK_INHERITED 0 +#define APR_HAS_LARGE_FILES 0 + #ifdef __cplusplus } #endif #endif /* ACR_ARCH_PRIVATE_H */ + Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h Thu Aug 6 08:10:29 2009 @@ -33,14 +33,35 @@ * ACR Linux platform private macros and defines * */ - #define ACR_PLATFORM_CLASS_PATH ACR_CLASS_PATH "platform/linux/" #define ACR_JNI_PLATFORM_DECLARE(RT, CL, FN) \ JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_platform_linux_##CL##_##FN +/** + * Temporary APR flags + */ +#define APR_USE_SHMEM_SHMGET 1 +#define APR_USE_SHMEM_MMAP_ANON 1 +#define APR_USE_SYSVSEM_SERIALIZE 1 +#define APR_USE_PTHREAD_SERIALIZE 1 +#define APR_PROCESS_LOCK_IS_GLOBAL 0 +#define APR_HAVE_CORKABLE_TCP 1 +#define APR_HAVE_SA_STORAGE 1 +#define APR_HAVE_UNION_SEMUN 0 +#define APR_HAVE_SCTP 0 +#define APR_HAS_SO_ACCEPTFILTER 0 +#define APR_HAS_UNICODE_FS 0 +#define APR_HAS_PROC_INVOKED 0 +#define APR_HAS_XTHREAD_FILES 0 +#define APR_HAS_OS_UUID 1 +#define APR_TCP_NOPUSH_FLAG TCP_CORK +#define APR_TCP_NODELAY_INHERITED 1 +#define APR_O_NONBLOCK_INHERITED 0 +#define APR_HAS_LARGE_FILES 0 #ifdef __cplusplus } #endif #endif /* ACR_ARCH_PRIVATE_H */ + Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h Thu Aug 6 08:10:29 2009 @@ -37,8 +37,31 @@ #define ACR_JNI_PLATFORM_DECLARE(RT, CL, FN) \ JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_platform_solaris_##CL##_##FN +/** + * Temporary APR flags + */ +#define APR_USE_SHMEM_SHMGET 1 +#define APR_USE_SHMEM_MMAP_ANON 1 +#define APR_USE_SYSVSEM_SERIALIZE 1 +#define APR_USE_PTHREAD_SERIALIZE 1 +#define APR_PROCESS_LOCK_IS_GLOBAL 0 +#define APR_HAVE_CORKABLE_TCP 1 +#define APR_HAVE_SA_STORAGE 1 +#define APR_HAVE_UNION_SEMUN 1 +#define APR_HAVE_SCTP 0 +#define APR_HAS_SO_ACCEPTFILTER 0 +#define APR_HAS_UNICODE_FS 0 +#define APR_HAS_PROC_INVOKED 0 +#define APR_HAS_XTHREAD_FILES 0 +#define APR_HAS_OS_UUID 1 +#define APR_TCP_NOPUSH_FLAG TCP_NOPUSH +#define APR_TCP_NODELAY_INHERITED 1 +#define APR_O_NONBLOCK_INHERITED 1 +#define APR_HAS_LARGE_FILES 1 + #ifdef __cplusplus } #endif #endif /* ACR_ARCH_PRIVATE_H */ + Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h Thu Aug 6 08:10:29 2009 @@ -38,19 +38,27 @@ #define INVALID_HANDLE_VALUE ((void *)(ptrdiff_t)-1) #define IS_INVALID_HANDLE(h) (((h) == NULL || (h) == INVALID_HANDLE_VALUE)) -typedef struct acr_ioh { - ACR_SP_ALIGN(void *, h); - int type; - unsigned int flags; -} acr_ioh; +typedef struct acr_ioh acr_ioh; +typedef int (acr_ioh_cleanup_fn_t)(void *, int, unsigned int); + +struct acr_ioh { + void *h; + acr_ioh_cleanup_fn_t *c; + int type; + unsigned int flags; +}; extern acr_ioh *acr_ioh_tab; extern int acr_ioh_mask; extern int acr_ioh_init(int); extern int acr_ioh_alloc(void *, int, unsigned int); +extern int acr_ioh_open(void *, int, unsigned int, acr_ioh_cleanup_fn_t *); extern int acr_ioh_free(int); +extern int acr_ioh_close(int); extern void acr_ioh_cleanup(void); +extern mode_t ACR_UnixPermsToMode(int); + /** Default number of IO slots */ #define ACR_IOH_SLOTS (64 * 1024) Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h Thu Aug 6 08:10:29 2009 @@ -274,17 +274,23 @@ size_t iov_len; /* Number of bytes */ }; -typedef struct acr_ioh { - ACR_SP_ALIGN(void *, h); - int type; - unsigned int flags; -} acr_ioh; +typedef struct acr_ioh acr_ioh; +typedef int (acr_ioh_cleanup_fn_t)(void *, int, unsigned int); + +struct acr_ioh { + void *h; + acr_ioh_cleanup_fn_t *c; + int type; + unsigned int flags; +}; extern acr_ioh *acr_ioh_tab; extern int acr_ioh_mask; extern int acr_ioh_init(int); extern int acr_ioh_alloc(void *, int, unsigned int); +extern int acr_ioh_open(void *, int, unsigned int, acr_ioh_cleanup_fn_t *); extern int acr_ioh_free(int); +extern int acr_ioh_close(int); extern void acr_ioh_cleanup(void); /** Default number of IO slots Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/ios.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/ios.c?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/ios.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/ios.c Thu Aug 6 08:10:29 2009 @@ -173,6 +173,12 @@ int acr_ioh_alloc(void *h, int type, unsigned int flags) { + return acr_ioh_open(h, type, flags, NULL); +} + +int acr_ioh_open(void *h, int type, unsigned int flags, + acr_ioh_cleanup_fn_t *c) +{ int i, x, e; if (!__bitmap) { @@ -185,6 +191,7 @@ if (__bitmap[i] != 0xFF) { x = (i << 3) + __sbit_mask[__bitmap[i]]; acr_ioh_tab[x].h = h; + acr_ioh_tab[x].c = c; acr_ioh_tab[x].type = type; acr_ioh_tab[x].flags = flags; __SET_BMP(i, x); @@ -197,6 +204,7 @@ if (__bitmap[i] != 0xFF) { x = (i << 3) + __sbit_mask[__bitmap[i]]; acr_ioh_tab[x].h = h; + acr_ioh_tab[x].c = c; acr_ioh_tab[x].type = type; acr_ioh_tab[x].flags = flags; __SET_BMP(i, x); @@ -214,6 +222,7 @@ if (__bitmap[i] != 0xFF) { x = (i << 3) + __sbit_mask[__bitmap[i]]; acr_ioh_tab[x].h = h; + acr_ioh_tab[x].c = c; acr_ioh_tab[x].type = type; acr_ioh_tab[x].flags = flags; __SET_BMP(i, x); @@ -230,12 +239,13 @@ int acr_ioh_free(int i) { if (i < 0 || i >= acr_ioh_mask) - return ACR_EINVAL; + return ACR_EBADF; if (!__bitmap) return ACR_ENOMEM; pthread_mutex_lock(&ios_lock); acr_ioh_tab[i].h = INVALID_HANDLE_VALUE; + acr_ioh_tab[i].c = NULL; acr_ioh_tab[i].type = ACR_DT_UNKNOWN; acr_ioh_tab[i].flags = 0; __CLR_BIT(__bitmap, i); @@ -244,6 +254,30 @@ return 0; } +int acr_ioh_close(int i) +{ + int rc = 0; + + if (i < 0 || i >= acr_ioh_mask) + return ACR_EBADF; + if (!__bitmap) + return ACR_ENOMEM; + if (!IS_INVALID_HANDLE(acr_ioh_tab[i].h) && acr_ioh_tab[i].c) { + /* Run the cleanup */ + rc = (*acr_ioh_tab[i].c)(acr_ioh_tab[i].h, acr_ioh_tab[i].type, + acr_ioh_tab[i].flags); + } + pthread_mutex_lock(&ios_lock); + acr_ioh_tab[i].h = INVALID_HANDLE_VALUE; + acr_ioh_tab[i].c = NULL; + acr_ioh_tab[i].type = ACR_DT_UNKNOWN; + acr_ioh_tab[i].flags = 0; + __CLR_BIT(__bitmap, i); + + pthread_mutex_unlock(&ios_lock); + return rc; +} + void acr_ioh_cleanup() { int i; @@ -252,9 +286,10 @@ return; pthread_mutex_lock(&ios_lock); for (i = 0; i < acr_ioh_mask; i++) { - if (!IS_INVALID_HANDLE(acr_ioh_tab[i].h)) { - /* TODO: Run the cleanup? */ - + if (!IS_INVALID_HANDLE(acr_ioh_tab[i].h) && acr_ioh_tab[i].c) { + /* Run the cleanups */ + (*acr_ioh_tab[i].c)(acr_ioh_tab[i].h, acr_ioh_tab[i].type, + acr_ioh_tab[i].flags); } } free(acr_ioh_tab); Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c?rev=801535&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c Thu Aug 6 08:10:29 2009 @@ -0,0 +1,217 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "acr.h" +#include "acr_private.h" +#include "acr_arch.h" +#include "acr_error.h" +#include "acr_memory.h" +#include "acr_string.h" +#include "acr_descriptor.h" +#include "acr_procmutex.h" + +#include <sys/ipc.h> +#include <sys/sem.h> +#include <sys/file.h> + +union semun { + int val; + struct semid_ds *buf; + unsigned short *array; +}; + +struct acr_pmutex_t { + int filedes; + int locked; + char *fname; +}; + +static int mutex_cleanup(void *mutex, int type, unsigned int flags) +{ + if (type != ACR_DT_MUTEX) { + acr_pmutex_t *m = (acr_pmutex_t *)mutex; + if (m->filedes > 0) { + union semun ick; + ick.val = 0; + semctl(m->filedes, 0, IPC_RMID, ick); + } + free(m->fname); + free(m); + return ACR_SUCCESS; + } + return ACR_EINVAL; +} + +ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *fname) +{ + union semun ick; + int rc = 0; + acr_pmutex_t *m; + + m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t)); + if (!m) + return -1; + m->fname = ACR_StrdupA(_E, THROW_FMARK, fname); + if (!m->fname) { + rc = ACR_GET_OS_ERROR(); + free(m); + ACR_SET_OS_ERROR(rc); + return -1; + } + + m->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); + + if (m->filedes < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + ick.val = 1; + if (semctl(m->filedes, 0, SETVAL, ick) < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + m->locked = 0; + +cleanup: + if (rc) { + if (m->filedes > 0) { + ick.val = 0; + semctl(m->filedes, 0, IPC_RMID, ick); + } + free(m->fname); + free(m); + if (!IS_INVALID_HANDLE(_E)) { + if (rc == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + } + ACR_SET_OS_ERROR(rc); + return -1; + } + else { + rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup); + return rc; + } +} + +ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex) +{ + int rc; + struct sembuf op; + acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + ACR_SET_OS_ERROR(ACR_EINVAL); + return -1; + } + op.sem_num = 0; + op.sem_op = -1; + op.sem_flg = SEM_UNDO; + + do { + rc = semop(m->filedes, &op, 1); + } while (rc < 0 && errno == EINTR); + + if (rc < 0) + return ACR_GET_OS_ERROR(); + m->locked = 1; + + return ACR_SUCCESS; +} + +ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex) +{ + int rc; + struct sembuf op; + acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + return ACR_EINVAL; + } + op.sem_num = 0; + op.sem_op = -1; + op.sem_flg = SEM_UNDO | IPC_NOWAIT; + + do { + rc = semop(m->filedes, &op, 1); + } while (rc < 0 && errno == EINTR); + + if (rc < 0) { + if (errno == EAGAIN) { + return ACR_EBUSY; + } + } + else + m->locked = 1; + + return ACR_SUCCESS;; +} + +ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex) +{ + int rc; + struct sembuf op; + acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + return ACR_EINVAL; + } + + m->locked = 0; + op.sem_num = 0; + op.sem_op = 1; + op.sem_flg = SEM_UNDO; + do { + rc = semop(m->filedes, &op, 1); + } while (rc < 0 && errno == EINTR); + + if (rc < 0) + return ACR_GET_OS_ERROR(); + else + return ACR_SUCCESS; +} + +ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms, + acr_uid_t uid, acr_uid_t gid) +{ + union semun ick; + struct semid_ds buf; + acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + return ACR_EINVAL; + } + buf.sem_perm.uid = uid; + buf.sem_perm.gid = gid; + buf.sem_perm.mode = ACR_UnixPermsToMode(perms); + ick.buf = &buf; + + if (semctl(m->filedes, 0, IPC_SET, ick) < 0) { + return ACR_GET_OS_ERROR(); + } + else + return ACR_SUCCESS; +} + +ACR_DECLARE(int) ACR_ProcMutexClose(JNIEnv *_E, int mutex) +{ + + /* Close will call the cleanup function + */ + return acr_ioh_close(mutex); +} + Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c?rev=801535&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c Thu Aug 6 08:10:29 2009 @@ -0,0 +1,475 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "acr.h" +#include "acr_private.h" +#include "acr_arch.h" +#include "acr_error.h" +#include "acr_memory.h" +#include "acr_string.h" +#include "acr_descriptor.h" +#include "acr_shm.h" + +#include <sys/mman.h> +#include <sys/ipc.h> +#include <sys/shm.h> +#include <sys/file.h> + +#if !defined(SHM_R) +#define SHM_R 0400 +#endif +#if !defined(SHM_W) +#define SHM_W 0200 +#endif + +#define ACR_SHM_OWNER 0 +#define ACR_SHM_CHILD 1 + +struct acr_shm_t { + void *base; /* base real address */ + void *usable; /* base usable address */ + acr_size_t reqsize; /* requested segment size */ + acr_size_t realsize; /* actual segment size */ + const char *filename; /* NULL if anonymous */ + int shmid; /* shmem ID returned from shmget() */ + key_t shmkey; +}; + +static int shm_owner_cleanup(void *shm, int type, unsigned int flags) +{ + int rc = 0; + acr_shm_t *m = (acr_shm_t *)shm; + + if (type != ACR_DT_SHM) { + return ACR_EINVAL; + } + /* anonymous shared memory */ + if (m->filename == NULL) { + if (munmap(m->base, m->realsize) == -1) { + rc = ACR_GET_OS_ERROR(); + } + } + /* name-based shared memory */ + else { + /* Indicate that the segment is to be destroyed as soon + * as all processes have detached. This also disallows any + * new attachments to the segment. + */ + if (shmctl(m->shmid, IPC_RMID, NULL) == -1 && errno != EINVAL) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + if (shmdt(m->base) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + if (access(m->filename, F_OK)) { + rc = ACR_SUCCESS; + } + else { + if (unlink(m->filename)) + rc = ACR_GET_OS_ERROR(); + } + } +cleanup: + if (m->filename) + free((void *)(m->filename)); + free(m); + return rc; +} + +static int shm_child_cleanup(void *shm, int type, unsigned int flags) +{ + int rc = 0; + acr_shm_t *m = (acr_shm_t *)shm; + + if (type != ACR_DT_SHM) { + return ACR_EINVAL; + } + + if (m->filename == NULL) { + /* It doesn't make sense to detach from an + * anonymous memory segment. + */ + rc = ACR_EINVAL; + } + else { + if (shmdt(m->base) == -1) { + rc = ACR_GET_OS_ERROR(); + } + } + if (m->filename) + free((void *)(m->filename)); + free(m); + return rc; +} + +ACR_DECLARE(int) ACR_ShmDestroy(JNIEnv *_E, int shm) +{ + int rv; + + rv = acr_ioh_close(shm); + if (rv && !IS_INVALID_HANDLE(_E)) { + if (rv == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rv); + } + return rv; +} + +ACR_DECLARE(int) ACR_ShmDetach(JNIEnv *_E, int shm) +{ + int rv; + + rv = acr_ioh_close(shm); + if (rv && !IS_INVALID_HANDLE(_E)) { + if (rv == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rv); + } + return rv; +} + +ACR_DECLARE(int) ACR_ShmClose(JNIEnv *_E, int shm) +{ + int rv; + + rv = acr_ioh_close(shm); + if (rv && !IS_INVALID_HANDLE(_E)) { + if (rv == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rv); + } + return rv; +} + +ACR_DECLARE(int) ACR_ShmRemove(JNIEnv *_E, const acr_pchar_t *filename) +{ + int rc = 0; + int file; + key_t shmkey; + int shmid; + + /* Presume that the file already exists; just open for writing. + * Mode is not needed cause we don't use O_CREAT flag. + */ + file = open(filename, O_WRONLY); + if (file < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + /* ftok() (on solaris at least) requires that the file actually + * exist before calling ftok(). + */ + shmkey = ftok(filename, 1); + if (shmkey == (key_t)-1) { + rc = ACR_GET_OS_ERROR(); + close(file); + goto cleanup; + } + close(file); + + if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + /* Indicate that the segment is to be destroyed as soon + * as all processes have detached. This also disallows any + * new attachments to the segment. + */ + if (shmctl(shmid, IPC_RMID, NULL) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + +cleanup: + unlink(filename); + if (rc && !IS_INVALID_HANDLE(_E)) { + if (rc == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + } + return rc; +} + +ACR_DECLARE(int) ACR_ShmCreate(JNIEnv *_E, acr_size_t reqsize, + const acr_pchar_t *filename) +{ + acr_shm_t *shm = NULL; + struct shmid_ds shmbuf; + int file; /* file where metadata is stored */ + int rc; + acr_size_t nbytes; + + shm = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_shm_t)); + if (!shm) + return -1; + /* Check if they want anonymous or name-based shared memory */ + if (filename == NULL) { + shm->filename = NULL; + shm->reqsize = reqsize; + shm->realsize = reqsize + + ACR_ALIGN_DEFAULT(sizeof(acr_size_t)); /* room for metadata */ + shm->base = mmap(NULL, shm->realsize, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0); + if (shm->base == (void *)MAP_FAILED) { + rc = ACR_GET_OS_ERROR(); + free(shm); + if (!IS_INVALID_HANDLE(_E)) { + if (rc == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + } + ACR_SET_OS_ERROR(rc); + return -1; + } + + /* store the real size in the metadata */ + *(acr_size_t*)(shm->base) = shm->realsize; + /* metadata isn't usable */ + shm->usable = (char *)shm->base + ACR_ALIGN_DEFAULT(sizeof(acr_size_t)); + } + /* Name-based shared memory */ + else { + shm->reqsize = reqsize; + shm->filename = ACR_StrdupA(_E, THROW_FMARK, filename); + if (!shm->filename) { + rc = ACR_GET_OS_ERROR(); + free(shm); + ACR_SET_OS_ERROR(rc); + return -1; + } + + shm->realsize = reqsize; + + /* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. + * Unlike APR we use 0660 which is less permissive + */ + file = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0660); + if (file < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + /* ftok() (on solaris at least) requires that the file actually + * exist before calling ftok(). */ + shm->shmkey = ftok(filename, 1); + if (shm->shmkey == (key_t)-1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + if ((shm->shmid = shmget(shm->shmkey, shm->realsize, + SHM_R | SHM_W | IPC_CREAT | IPC_EXCL)) < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + if ((shm->base = shmat(shm->shmid, NULL, 0)) == (void *)-1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + shm->usable = shm->base; + + if (shmctl(shm->shmid, IPC_STAT, &shmbuf) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + shmbuf.shm_perm.uid = getuid(); + shmbuf.shm_perm.gid = getgid(); + if (shmctl(shm->shmid, IPC_SET, &shmbuf) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + nbytes = sizeof(reqsize); + do { + rc = write(file,(const void *)&reqsize, nbytes); + } while (rc == (acr_size_t)-1 && errno == EINTR); + if (rc == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } +cleanup: + if (file > 0) + rc = close(file); + if (rc) { + free((void *)(shm->filename)); + free(shm); + if (!IS_INVALID_HANDLE(_E)) { + if (rc == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + } + ACR_SET_OS_ERROR(rc); + shm = NULL; + } + } + if (shm) { + rc = acr_ioh_open(shm, ACR_DT_SHM, ACR_SHM_OWNER, shm_owner_cleanup); + return rc; + } + else + return -1; +} + +ACR_DECLARE(int) ACR_ShmAttach(JNIEnv *_E, + const acr_pchar_t *filename) +{ + acr_shm_t *shm = NULL; + int file; /* file where metadata is stored */ + int rc; + acr_size_t nbytes; + + if (!filename) { + ACR_SET_OS_ERROR(ACR_EINVAL); + return -1; + } + shm = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_shm_t)); + if (!shm) + return -1; + shm->filename = ACR_StrdupA(_E, THROW_FMARK, filename); + if (!shm->filename) { + rc = ACR_GET_OS_ERROR(); + free(shm); + ACR_SET_OS_ERROR(rc); + return -1; + } + + file = open(filename, O_RDONLY); + if (file < 0) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + + nbytes = sizeof(shm->reqsize); + + do { + rc = read(file, (void *)&(shm->reqsize), nbytes); + } while (rc == -1 && errno == EINTR); + if (rc != nbytes) { + /* Wrong format */ + rc = EBADF; + close(file); + goto cleanup; + } + rc = close(file); + if (rc) { + goto cleanup; + } + + shm->shmkey = ftok(filename, 1); + if (shm->shmkey == (key_t)-1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + if ((shm->shmid = shmget(shm->shmkey, 0, SHM_R | SHM_W)) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + if ((shm->base = shmat(shm->shmid, NULL, 0)) == (void *)-1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + shm->usable = shm->base; + shm->realsize = shm->reqsize; + +cleanup: + if (rc) { + free((void *)(shm->filename)); + free(shm); + if (!IS_INVALID_HANDLE(_E)) { + if (rc == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + } + ACR_SET_OS_ERROR(rc); + shm = NULL; + } + if (shm) { + rc = acr_ioh_open(shm, ACR_DT_SHM, ACR_SHM_CHILD, shm_child_cleanup); + return rc; + } + else + return -1; +} + +ACR_DECLARE(int) ACR_ShmPermSet(JNIEnv *_E, int shm, int perms, + acr_uid_t uid, acr_uid_t gid) +{ + int rc = 0; + struct shmid_ds shmbuf; + int shmid; + acr_shm_t *m = (acr_shm_t *)ACR_IOH(shm); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) { + rc = ACR_EINVAL; + goto cleanup; + } + if ((shmid = shmget(m->shmkey, 0, SHM_R | SHM_W)) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + shmbuf.shm_perm.uid = uid; + shmbuf.shm_perm.gid = gid; + shmbuf.shm_perm.mode = ACR_UnixPermsToMode(perms); + if (shmctl(shmid, IPC_SET, &shmbuf) == -1) { + rc = ACR_GET_OS_ERROR(); + goto cleanup; + } + +cleanup: + if (rc && !IS_INVALID_HANDLE(_E)) { + if (rc == EACCES) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0); + else + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc); + } + return rc; +} + +ACR_DECLARE(void *) ACR_ShmGetBaseAddr(int shm) +{ + acr_shm_t *m = (acr_shm_t *)ACR_IOH(shm); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) { + ACR_SET_OS_ERROR(ACR_EINVAL); + return NULL; + } + else + return m->usable; +} + +ACR_DECLARE(acr_size_t) ACR_ShmGetSize(int shm) +{ + acr_shm_t *m = (acr_shm_t *)ACR_IOH(shm); + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) { + ACR_SET_OS_ERROR(ACR_EINVAL); + return 0; + } + else + return m->reqsize; +} + Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/ios.c Thu Aug 6 08:10:29 2009 @@ -150,6 +150,12 @@ int acr_ioh_alloc(void *h, int type, unsigned int flags) { + return acr_ioh_open(h, type, flags, NULL); +} + +int acr_ioh_open(void *h, int type, unsigned int flags, + acr_ioh_cleanup_fn_t *c) +{ int i, x, e; if (!__bitmap) { @@ -162,6 +168,7 @@ if (__bitmap[i] != 0xFF) { x = (i << 3) + __sbit_mask[__bitmap[i]]; acr_ioh_tab[x].h = h; + acr_ioh_tab[x].c = c; acr_ioh_tab[x].type = type; acr_ioh_tab[x].flags = flags; __SET_BMP(i, x); @@ -174,6 +181,7 @@ if (__bitmap[i] != 0xFF) { x = (i << 3) + __sbit_mask[__bitmap[i]]; acr_ioh_tab[x].h = h; + acr_ioh_tab[x].c = c; acr_ioh_tab[x].type = type; acr_ioh_tab[x].flags = flags; __SET_BMP(i, x); @@ -191,6 +199,7 @@ if (__bitmap[i] != 0xFF) { x = (i << 3) + __sbit_mask[__bitmap[i]]; acr_ioh_tab[x].h = h; + acr_ioh_tab[x].c = c; acr_ioh_tab[x].type = type; acr_ioh_tab[x].flags = flags; __SET_BMP(i, x); @@ -207,12 +216,13 @@ int acr_ioh_free(int i) { if (i < 0 || i >= acr_ioh_mask) - return ACR_EINVAL; + return ACR_EBADF; if (!__bitmap) return ACR_ENOMEM; EnterCriticalSection(&ios_lock); acr_ioh_tab[i].h = INVALID_HANDLE_VALUE; + acr_ioh_tab[i].c = NULL; acr_ioh_tab[i].type = ACR_DT_UNKNOWN; acr_ioh_tab[i].flags = 0; __CLR_BIT(__bitmap, i); @@ -221,6 +231,30 @@ return 0; } +int acr_ioh_close(int i) +{ + int rc = 0; + + if (i < 0 || i >= acr_ioh_mask) + return ACR_EBADF; + if (!__bitmap) + return ACR_ENOMEM; + if (!IS_INVALID_HANDLE(acr_ioh_tab[i].h) && acr_ioh_tab[i].c) { + /* Run the cleanup */ + rc = (*acr_ioh_tab[i].c)(acr_ioh_tab[i].h, acr_ioh_tab[i].type, + acr_ioh_tab[i].flags); + } + EnterCriticalSection(&ios_lock); + acr_ioh_tab[i].h = INVALID_HANDLE_VALUE; + acr_ioh_tab[i].c = NULL; + acr_ioh_tab[i].type = ACR_DT_UNKNOWN; + acr_ioh_tab[i].flags = 0; + __CLR_BIT(__bitmap, i); + LeaveCriticalSection(&ios_lock); + + return rc; +} + void acr_ioh_cleanup() { int i; @@ -229,9 +263,10 @@ return; EnterCriticalSection(&ios_lock); for (i = 0; i < acr_ioh_mask; i++) { - if (acr_ioh_tab[i].h) { - /* TODO: Run the cleanup */ - + if (!IS_INVALID_HANDLE(acr_ioh_tab[i].h) && acr_ioh_tab[i].c) { + /* Run the cleanups */ + (*acr_ioh_tab[i].c)(acr_ioh_tab[i].h, acr_ioh_tab[i].type, + acr_ioh_tab[i].flags); } } free(acr_ioh_tab); Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Thu Aug 6 08:10:29 2009 @@ -28,6 +28,7 @@ #include "acr_descriptor.h" #include "acr_file.h" #include "acr_xdr.h" +#include "acr_shm.h" #if defined (WIN32) #include <io.h> @@ -637,3 +638,40 @@ return r; } +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test060)(ACR_JNISTDARGS, jint d) +{ + + return ACR_ShmCreate(_E, d, NULL); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test061)(ACR_JNISTDARGS, jint d) +{ + + return ACR_ShmCreate(_E, d, "shared"); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test062)(ACR_JNISTDARGS, jint s) +{ + return ACR_ShmDestroy(_E, s); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test063)(ACR_JNISTDARGS, jint d) +{ + return ACR_ShmRemove(_E, "shared"); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test064)(ACR_JNISTDARGS, jint d) +{ + return ACR_ShmAttach(_E, "shared"); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test065)(ACR_JNISTDARGS, jint s) +{ + return ACR_ShmDetach(_E, s); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test066)(ACR_JNISTDARGS, jint s) +{ + return ACR_ShmGetSize(s); +} + Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=801535&r1=801534&r2=801535&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Thu Aug 6 08:10:29 2009 @@ -104,6 +104,14 @@ private static native int test050(int i); private static native int test051(int i); + private static native int test060(int l); + private static native int test061(int l); + private static native int test062(int s); + private static native int test063(int d); + private static native int test064(int l); + private static native int test065(int s); + private static native int test066(int s); + private static native String test100(String s); @@ -890,5 +898,53 @@ assertEquals("Result", 0.0, test044(23456789.5678)); } + public void testShmCreate() + throws Throwable + { + try { + // Remove any previous shared memory + test063(0); + } catch (Throwable e) { + // Ignore + } + int sa = test061(1024); + assertTrue("Shared memory", sa > 0); + + try { + // Second invocation must throw exists. + int sb = test061(1024); + fail("Exception not thrown"); + } catch (Throwable e) { + // Ignore + } + assertEquals("Shared memory size", 1024, test066(sa)); + + int rv = test062(sa); + assertEquals("Destroy Shared memory", 0, rv); + } + + public void testShmAttach() + throws Throwable + { + int rv; + try { + // Remove any previous shared memory + test063(0); + } catch (Throwable e) { + // Ignore + } + int sa = test061(1024); + assertTrue("Shared memory", sa > 0); + + int sb = test064(0); + assertTrue("Shared memory", sb > 0); + + assertEquals("Shared memory size", 1024, test066(sb)); + rv = test062(sa); + assertEquals("Destroy Shared memory", 0, rv); + rv = test065(sb); + assertEquals("Destroy Attached memory", 0, rv); + } + }