apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r1561260 - in /apr/apr/trunk: CHANGES configure.in shmem/unix/shm.c
Date Sat, 25 Jan 2014 05:51:13 GMT
Author: jim
Date: Sat Jan 25 05:51:13 2014
New Revision: 1561260

URL: http://svn.apache.org/r1561260
Log:
  *) Fix POSIX shared memory (shm_open) use for named shared memory.
     PR 55928. [Jozef Hatala <jh-asf skrt org>]

Modified:
    apr/apr/trunk/CHANGES
    apr/apr/trunk/configure.in
    apr/apr/trunk/shmem/unix/shm.c

Modified: apr/apr/trunk/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/trunk/CHANGES?rev=1561260&r1=1561259&r2=1561260&view=diff
==============================================================================
--- apr/apr/trunk/CHANGES [utf-8] (original)
+++ apr/apr/trunk/CHANGES [utf-8] Sat Jan 25 05:51:13 2014
@@ -1,6 +1,9 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) Fix POSIX shared memory (shm_open) use for named shared memory.
+     PR 55928. [Jozef Hatala <jh-asf skrt org>]
+
   *) Windows platform: Remove support for Windows 9x.
 
   *) Added signed apr_intptr_t.  Changed ODBC dbd driver to use this.

Modified: apr/apr/trunk/configure.in
URL: http://svn.apache.org/viewvc/apr/apr/trunk/configure.in?rev=1561260&r1=1561259&r2=1561260&view=diff
==============================================================================
--- apr/apr/trunk/configure.in (original)
+++ apr/apr/trunk/configure.in Sat Jan 25 05:51:13 2014
@@ -1150,11 +1150,6 @@ APR_IFALLYES(header:sys/mman.h func:mmap
              [havemmaptmp="1"
               APR_DECIDE(USE_SHMEM_MMAP_TMP, 
                   [Classical mmap() on temporary file])])
-APR_IFALLYES(header:sys/mman.h func:mmap func:munmap func:shm_open dnl
-             func:shm_unlink,
-             [havemmapshm="1"
-              APR_DECIDE(USE_SHMEM_MMAP_SHM, 
-                  [mmap() via POSIX.1 shm_open() on temporary file])])
 APR_IFALLYES(header:sys/ipc.h header:sys/shm.h header:sys/file.h dnl
              func:shmget func:shmat func:shmdt func:shmctl,
              [haveshmget="1"
@@ -1168,6 +1163,15 @@ APR_IFALLYES(header:os2.h,
 APR_IFALLYES(header:windows.h,
              [havewin32shm="1"
               APR_DECIDE(USE_SHMEM_WIN32, [Windows shared memory])])
+AC_ARG_ENABLE(posix-shm,
+[  --enable-posix-shm      Use POSIX shared memory (shm_open) if available],
+[
+APR_IFALLYES(header:sys/mman.h func:mmap func:munmap func:shm_open dnl
+             func:shm_unlink,
+             [havemmapshm="1"
+              APR_DECIDE(USE_SHMEM_MMAP_SHM, 
+                  [mmap() via POSIX.1 shm_open() on temporary file])])
+])
 case $host in
     *linux* ) 
         # Linux pre-2.4 had problems with MM_SHMT_MMANON even though

Modified: apr/apr/trunk/shmem/unix/shm.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/shmem/unix/shm.c?rev=1561260&r1=1561259&r2=1561260&view=diff
==============================================================================
--- apr/apr/trunk/shmem/unix/shm.c (original)
+++ apr/apr/trunk/shmem/unix/shm.c Sat Jan 25 05:51:13 2014
@@ -23,6 +23,61 @@
 #include "apr_strings.h"
 #include "apr_hash.h"
 
+#if APR_USE_SHMEM_MMAP_SHM
+/* 
+ *   For portable use, a shared memory object should be identified by a name of
+ *   the form /somename; that is, a null-terminated string of up to NAME_MAX
+ *   (i.e., 255) characters consisting of an initial slash, followed by one or
+ *   more characters, none of which are slashes.
+ */
+#ifndef NAME_MAX
+#define NAME_MAX 255
+#endif
+static const char *make_shm_open_safe_name(const char *filename,
+                                           apr_pool_t *pool)
+{
+    const char *in;
+    char *result;
+    char *out;
+    apr_size_t len = 0;
+
+    if (filename == NULL)
+        return NULL;
+
+    len = strlen(filename);
+    if (filename[0] != '/') {
+        ++len;
+    }
+    if (len > NAME_MAX) {
+        len = NAME_MAX;
+    }
+    /* Allocate the required string */
+    result = apr_palloc(pool, len+1);
+
+    in = filename;
+    if (*in == '/') {
+        ++in;
+    }
+
+    out = result;
+    *out++ = '/';
+
+    for ( ; --len; ++in, ++out) {
+        if (*in == '/') {
+            /* '/' becomes '|' */
+            *out = '|';
+        } else {
+            /* Everything else remains unchanged */
+            *out = *in;
+        }
+    }
+
+    *out = '\0';
+
+    return result;
+}
+#endif
+
 static apr_status_t shm_cleanup_owner(void *m_)
 {
     apr_shm_t *m = (apr_shm_t *)m_;
@@ -60,7 +115,7 @@ static apr_status_t shm_cleanup_owner(vo
         if (munmap(m->base, m->realsize) == -1) {
             return errno;
         }
-        if (shm_unlink(m->filename) == -1) {
+        if (shm_unlink(make_shm_open_safe_name(m->filename, m->pool)) == -1 &&
errno != ENOENT) {
             return errno;
         }
         return APR_SUCCESS;
@@ -221,7 +276,9 @@ APR_DECLARE(apr_status_t) apr_shm_create
         new_m->pool = pool;
         new_m->reqsize = reqsize;
         new_m->filename = apr_pstrdup(pool, filename);
-
+#if APR_USE_SHMEM_MMAP_SHM
+        const char *shm_name = make_shm_open_safe_name(filename, pool);
+#endif
 #if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
         new_m->realsize = reqsize + 
             APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */
@@ -262,7 +319,7 @@ APR_DECLARE(apr_status_t) apr_shm_create
         }
 #endif /* APR_USE_SHMEM_MMAP_TMP */
 #if APR_USE_SHMEM_MMAP_SHM
-        tmpfd = shm_open(filename, O_RDWR | O_CREAT | O_EXCL, 0644);
+        tmpfd = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, 0644);
         if (tmpfd == -1) {
             return errno;
         }
@@ -276,10 +333,10 @@ APR_DECLARE(apr_status_t) apr_shm_create
 
         status = apr_file_trunc(file, new_m->realsize);
         if (status != APR_SUCCESS) {
-            shm_unlink(filename); /* we're failing, remove the object */
+            shm_unlink(shm_name); /* we're failing, remove the object */
             return status;
         }
-        new_m->base = mmap(NULL, reqsize, PROT_READ | PROT_WRITE,
+        new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
                            MAP_SHARED, tmpfd, 0);
 
         /* FIXME: check for errors */
@@ -388,7 +445,8 @@ APR_DECLARE(apr_status_t) apr_shm_remove
 #if APR_USE_SHMEM_MMAP_TMP
     return apr_file_remove(filename, pool);
 #elif APR_USE_SHMEM_MMAP_SHM
-    if (shm_unlink(filename) == -1) {
+    const char *shm_name = make_shm_open_safe_name(filename, pool);
+    if (shm_unlink(shm_name) == -1) {
         return errno;
     }
     return APR_SUCCESS;
@@ -483,7 +541,22 @@ APR_DECLARE(apr_status_t) apr_shm_attach
         new_m = apr_palloc(pool, sizeof(apr_shm_t));
         new_m->pool = pool;
         new_m->filename = apr_pstrdup(pool, filename);
+#if APR_USE_SHMEM_MMAP_SHM
+        const char *shm_name = make_shm_open_safe_name(filename, pool);
+
+        tmpfd = shm_open(shm_name, O_RDWR, 0644);
+        if (tmpfd == -1) {
+            return errno;
+        }
 
+        status = apr_os_file_put(&file, &tmpfd,
+                                 APR_READ | APR_WRITE,
+                                 pool); 
+        if (status != APR_SUCCESS) {
+            return status;
+        }
+
+#elif APR_USE_SHMEM_MMAP_TMP
         status = apr_file_open(&file, filename, 
                                APR_FOPEN_READ | APR_FOPEN_WRITE,
                                APR_FPROT_OS_DEFAULT, pool);
@@ -494,6 +567,9 @@ APR_DECLARE(apr_status_t) apr_shm_attach
         if (status != APR_SUCCESS) {
             return status;
         }
+#else
+        return APR_ENOTIMPL;
+#endif
 
         nbytes = sizeof(new_m->realsize);
         status = apr_file_read(file, (void *)&(new_m->realsize),



Mime
View raw message