You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by tr...@apache.org on 2013/10/21 01:40:35 UTC
svn commit: r1534011 - in /apr/apr/branches/1.5.x: ./ CHANGES
include/apr_shm.h shmem/beos/shm.c shmem/os2/shm.c shmem/unix/shm.c
shmem/win32/shm.c
Author: trawick
Date: Sun Oct 20 23:40:35 2013
New Revision: 1534011
URL: http://svn.apache.org/r1534011
Log:
Merge r1531768 and r1533979 from trunk:
r1531768:
Windows: Create named shared memory segments under the "Local"
namespace if the caller is unprivileged, fixing an inability of
unprivileged callers to use apr_shm_create() with named shared
memory segments under recent Windows.
r1533979:
Add apr_shm_create_ex() and apr_shm_attach_ex(), which
provide the ability to select the Global or Local
namespace on Windows.
Modified:
apr/apr/branches/1.5.x/ (props changed)
apr/apr/branches/1.5.x/CHANGES
apr/apr/branches/1.5.x/include/apr_shm.h
apr/apr/branches/1.5.x/shmem/beos/shm.c
apr/apr/branches/1.5.x/shmem/os2/shm.c
apr/apr/branches/1.5.x/shmem/unix/shm.c
apr/apr/branches/1.5.x/shmem/win32/shm.c
Propchange: apr/apr/branches/1.5.x/
------------------------------------------------------------------------------
Merged /apr/apr/trunk:r1531768,1533979
Modified: apr/apr/branches/1.5.x/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/CHANGES?rev=1534011&r1=1534010&r2=1534011&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/CHANGES [utf-8] (original)
+++ apr/apr/branches/1.5.x/CHANGES [utf-8] Sun Oct 20 23:40:35 2013
@@ -1,6 +1,15 @@
-*- coding: utf-8 -*-
Changes for APR 1.5.0
+ *) Windows: Create named shared memory segments under the "Local"
+ namespace if the caller is unprivileged, fixing an inability of
+ unprivileged callers to use apr_shm_create() with named shared
+ memory segments under recent Windows. As before, shared memory
+ segments are created under the "Global" namespace for privileged
+ callers. Add apr_shm_create_ex() and apr_shm_attach_ex(), which
+ provide the ability to override the normal namespace selection.
+ [Jeff Trawick]
+
*) Update compile settings for MINT OS. PR 47181. [Alan Hourihane
<alanh fairlite.co.uk>]
Modified: apr/apr/branches/1.5.x/include/apr_shm.h
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/include/apr_shm.h?rev=1534011&r1=1534010&r2=1534011&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/include/apr_shm.h (original)
+++ apr/apr/branches/1.5.x/include/apr_shm.h Sun Oct 20 23:40:35 2013
@@ -43,7 +43,8 @@ extern "C" {
typedef struct apr_shm_t apr_shm_t;
/**
- * Create and make accessable a shared memory segment.
+ * Create and make accessible a shared memory segment with default
+ * properties.
* @param m The shared memory structure to create.
* @param reqsize The desired size of the segment.
* @param filename The file to use for shared memory on platforms that
@@ -71,6 +72,52 @@ APR_DECLARE(apr_status_t) apr_shm_create
apr_pool_t *pool);
/**
+ * Special processing flags for apr_shm_create_ex() and apr_shm_attach_ex().
+ */
+#define APR_SHM_NS_LOCAL 1 /* Create or attach to named shared memory
+ * segment in the "Local" namespace on
+ * Windows. (Ignored on other platforms.)
+ * By default, the "Global" namespace is
+ * used for privileged processes and the
+ * "Local" namespace is used otherwise.
+ */
+#define APR_SHM_NS_GLOBAL 2 /* Create or attach to named shared memory
+ * segment in the "Global" namespace on
+ * Windows. (Ignored on other platforms.)
+ */
+
+/**
+ * Create and make accessible a shared memory segment with platform-
+ * specific processing.
+ * @param m The shared memory structure to create.
+ * @param reqsize The desired size of the segment.
+ * @param filename The file to use for shared memory on platforms that
+ * require it.
+ * @param pool the pool from which to allocate the shared memory
+ * structure.
+ * @param flags mask of APR_SHM_* (defined above)
+ * @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.
+ *
+ */
+APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *filename,
+ apr_pool_t *pool,
+ apr_int32_t flags);
+
+/**
* Remove named resource associated with a shared memory segment,
* preventing attachments to the resource, but not destroying it.
* @param filename The filename associated with shared-memory segment which
@@ -108,6 +155,21 @@ APR_DECLARE(apr_status_t) apr_shm_attach
apr_pool_t *pool);
/**
+ * Attach to a shared memory segment that was created
+ * by another process, with platform-specific processing.
+ * @param m The shared memory structure to create.
+ * @param filename The file used to create the original segment.
+ * (This MUST match the original filename.)
+ * @param pool the pool from which to allocate the shared memory
+ * structure for this process.
+ * @param flags mask of APR_SHM_* (defined above)
+ */
+APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool,
+ apr_int32_t flags);
+
+/**
* Detach from a shared memory segment without destroying it.
* @param m The shared memory structure representing the segment
* to detach from.
Modified: apr/apr/branches/1.5.x/shmem/beos/shm.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/shmem/beos/shm.c?rev=1534011&r1=1534010&r2=1534011&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/shmem/beos/shm.c (original)
+++ apr/apr/branches/1.5.x/shmem/beos/shm.c Sun Oct 20 23:40:35 2013
@@ -71,6 +71,15 @@ APR_DECLARE(apr_status_t) apr_shm_create
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *filename,
+ apr_pool_t *p,
+ apr_int32_t flags)
+{
+ return apr_shm_create(m, reqsize, filename, p);
+}
+
APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
{
delete_area(m->aid);
@@ -133,6 +142,14 @@ APR_DECLARE(apr_status_t) apr_shm_attach
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool,
+ apr_int32_t flags)
+{
+ return apr_shm_attach(m, filename, pool);
+}
+
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
{
delete_area(m->aid);
Modified: apr/apr/branches/1.5.x/shmem/os2/shm.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/shmem/os2/shm.c?rev=1534011&r1=1534010&r2=1534011&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/shmem/os2/shm.c (original)
+++ apr/apr/branches/1.5.x/shmem/os2/shm.c Sun Oct 20 23:40:35 2013
@@ -56,6 +56,15 @@ APR_DECLARE(apr_status_t) apr_shm_create
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *filename,
+ apr_pool_t *p,
+ apr_int32_t flags)
+{
+ return apr_shm_create(m, reqsize, filename, p);
+}
+
APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
{
DosFreeMem(m->memblock);
@@ -90,6 +99,14 @@ APR_DECLARE(apr_status_t) apr_shm_attach
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool,
+ apr_int32_t flags)
+{
+ return apr_shm_attach(m, filename, pool);
+}
+
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
{
int rc = 0;
Modified: apr/apr/branches/1.5.x/shmem/unix/shm.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/shmem/unix/shm.c?rev=1534011&r1=1534010&r2=1534011&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/shmem/unix/shm.c (original)
+++ apr/apr/branches/1.5.x/shmem/unix/shm.c Sun Oct 20 23:40:35 2013
@@ -365,6 +365,15 @@ APR_DECLARE(apr_status_t) apr_shm_create
}
}
+APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *filename,
+ apr_pool_t *p,
+ apr_int32_t flags)
+{
+ return apr_shm_create(m, reqsize, filename, p);
+}
+
APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
apr_pool_t *pool)
{
@@ -570,6 +579,14 @@ APR_DECLARE(apr_status_t) apr_shm_attach
}
}
+APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
+ const char *filename,
+ apr_pool_t *pool,
+ apr_int32_t flags)
+{
+ return apr_shm_attach(m, filename, pool);
+}
+
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
{
apr_status_t rv = shm_cleanup_attach(m);
Modified: apr/apr/branches/1.5.x/shmem/win32/shm.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/shmem/win32/shm.c?rev=1534011&r1=1534010&r2=1534011&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/shmem/win32/shm.c (original)
+++ apr/apr/branches/1.5.x/shmem/win32/shm.c Sun Oct 20 23:40:35 2013
@@ -56,10 +56,69 @@ static apr_status_t shm_cleanup(void* sh
return rv;
}
-APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
- apr_size_t reqsize,
- const char *file,
- apr_pool_t *pool)
+/* See if the caller is able to create a map in the global namespace by
+ * checking if the SE_CREATE_GLOBAL_NAME privilege is enabled.
+ *
+ * Prior to APR 1.5.0, named shared memory segments were always created
+ * in the global segment. However, with recent versions of Windows this
+ * fails for unprivileged processes. Thus, with older APR, named shared
+ * memory segments can't be created by unprivileged processes on newer
+ * Windows.
+ *
+ * By checking if the caller has the privilege, shm APIs can decide
+ * whether to use the Global or Local namespace.
+ *
+ * If running on an SDK without the required API definitions *OR*
+ * some processing failure occurs trying to check the privilege, fall
+ * back to earlier behavior -- always try to use the Global namespace.
+ */
+#ifdef SE_CREATE_GLOBAL_NAME
+static int can_create_global_maps(void)
+{
+ BOOL ok, has_priv;
+ LUID priv_id;
+ PRIVILEGE_SET privs;
+ HANDLE hToken;
+
+ ok = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken);
+ if (!ok && GetLastError() == ERROR_NO_TOKEN) {
+ /* no thread-specific access token, so try to get process access token
+ */
+ ok = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken);
+ }
+
+ if (ok) {
+ ok = LookupPrivilegeValue(NULL, SE_CREATE_GLOBAL_NAME, &priv_id);
+ }
+
+ if (ok) {
+ privs.PrivilegeCount = 1;
+ privs.Control = PRIVILEGE_SET_ALL_NECESSARY;
+ privs.Privilege[0].Luid = priv_id;
+ privs.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
+ ok = PrivilegeCheck(hToken, &privs, &has_priv);
+ }
+
+ if (ok && !has_priv) {
+ return 0;
+ }
+ else {
+ return 1;
+ }
+}
+#else /* SE_CREATE_GLOBAL_NAME */
+/* SDK definitions missing */
+static int can_create_global_maps(void)
+{
+ return 1;
+}
+#endif /* SE_CREATE_GLOBAL_NAME */
+
+APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *file,
+ apr_pool_t *pool,
+ apr_int32_t flags)
{
static apr_size_t memblock = 0;
HANDLE hMap, hFile;
@@ -96,6 +155,8 @@ APR_DECLARE(apr_status_t) apr_shm_create
mapkey = NULL;
}
else {
+ int global;
+
/* Do file backed, which is not an inherited handle
* While we could open APR_EXCL, it doesn't seem that Unix
* ever did. Ignore that error here, but fail later when
@@ -112,9 +173,18 @@ APR_DECLARE(apr_status_t) apr_shm_create
/* res_name_from_filename turns file into a pseudo-name
* without slashes or backslashes, and prepends the \global
- * prefix on Win2K and later
+ * or \local prefix on Win2K and later
*/
- mapkey = res_name_from_filename(file, 1, pool);
+ if (flags & APR_SHM_NS_GLOBAL) {
+ global = 1;
+ }
+ else if (flags & APR_SHM_NS_LOCAL) {
+ global = 0;
+ }
+ else {
+ global = can_create_global_maps();
+ }
+ mapkey = res_name_from_filename(file, global, pool);
}
#if APR_HAS_UNICODE_FS
@@ -170,6 +240,14 @@ APR_DECLARE(apr_status_t) apr_shm_create
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
+ apr_size_t reqsize,
+ const char *file,
+ apr_pool_t *pool)
+{
+ return apr_shm_create_ex(m, reqsize, file, pool, 0);
+}
+
APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
{
apr_status_t rv = shm_cleanup(m);
@@ -183,24 +261,20 @@ APR_DECLARE(apr_status_t) apr_shm_remove
return apr_file_remove(filename, pool);
}
-APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
- const char *file,
- apr_pool_t *pool)
+static apr_status_t shm_attach_internal(apr_shm_t **m,
+ const char *file,
+ apr_pool_t *pool,
+ int global)
{
HANDLE hMap;
void *mapkey;
void *base;
- if (!file) {
- return APR_EINVAL;
- }
- else {
- /* res_name_from_filename turns file into a pseudo-name
- * without slashes or backslashes, and prepends the \global
- * prefix on Win2K and later
- */
- mapkey = res_name_from_filename(file, 1, pool);
- }
+ /* res_name_from_filename turns file into a pseudo-name
+ * without slashes or backslashes, and prepends the \global
+ * or local prefix on Win2K and later
+ */
+ mapkey = res_name_from_filename(file, global, pool);
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
@@ -264,6 +338,55 @@ APR_DECLARE(apr_status_t) apr_shm_attach
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
+ const char *file,
+ apr_pool_t *pool,
+ apr_int32_t flags)
+{
+ apr_status_t rv;
+ int can_create_global;
+ int try_global_local[3] = {-1, -1, -1};
+ int cur;
+
+ if (!file) {
+ return APR_EINVAL;
+ }
+
+ if (flags & APR_SHM_NS_LOCAL) {
+ try_global_local[0] = 0; /* only search local */
+ }
+ else if (flags & APR_SHM_NS_GLOBAL) {
+ try_global_local[0] = 1; /* only search global */
+ }
+ else {
+ can_create_global = can_create_global_maps();
+ if (!can_create_global) { /* unprivileged process */
+ try_global_local[0] = 0; /* search local before global */
+ try_global_local[1] = 1;
+ }
+ else {
+ try_global_local[0] = 1; /* search global before local */
+ try_global_local[1] = 0;
+ }
+ }
+
+ for (cur = 0; try_global_local[cur] != -1; cur++) {
+ rv = shm_attach_internal(m, file, pool, try_global_local[cur]);
+ if (!APR_STATUS_IS_ENOENT(rv)) {
+ break;
+ }
+ }
+
+ return rv;
+}
+
+APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
+ const char *file,
+ apr_pool_t *pool)
+{
+ return apr_shm_attach_ex(m, file, pool, 0);
+}
+
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
{
apr_status_t rv = shm_cleanup(m);