You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by ji...@apache.org on 2009/09/10 22:58:35 UTC

svn commit: r813586 - in /apr/apr/branches/1.4.x: CHANGES STATUS locks/unix/proc_mutex.c

Author: jim
Date: Thu Sep 10 20:58:34 2009
New Revision: 813586

URL: http://svn.apache.org/viewvc?rev=813586&view=rev
Log:
Merge r811455, r813063 from trunk:

Allow for passed locknames to be honored with posix-sems.
Still maintain posix-sem naming conventions tho!


just use it :)

Reviewed/backported by: jim

Modified:
    apr/apr/branches/1.4.x/CHANGES
    apr/apr/branches/1.4.x/STATUS
    apr/apr/branches/1.4.x/locks/unix/proc_mutex.c

Modified: apr/apr/branches/1.4.x/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.4.x/CHANGES?rev=813586&r1=813585&r2=813586&view=diff
==============================================================================
--- apr/apr/branches/1.4.x/CHANGES [utf-8] (original)
+++ apr/apr/branches/1.4.x/CHANGES [utf-8] Thu Sep 10 20:58:34 2009
@@ -5,8 +5,11 @@
      Fix overflow in pools and rmm, where size alignment was taking place.
      [Matt Lewis <ma...@google.com>, Sander Striker]
 
+  *) Posix semaphores can now be named and used as named semaphores.
+     [Jim Jagielski]
+
   *) Better handling of APR_OFF_T_FMT for Darwin 10 depending on -arch
-     setting of compiler [Jim Jagielski]
+     setting of compiler. [Jim Jagielski]
 
   *) Add comments describing the thread-safety properties of apr_pool_t.
      [Neil Conway nrc cs.berkeley.edu]

Modified: apr/apr/branches/1.4.x/STATUS
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.4.x/STATUS?rev=813586&r1=813585&r2=813586&view=diff
==============================================================================
--- apr/apr/branches/1.4.x/STATUS [utf-8] (original)
+++ apr/apr/branches/1.4.x/STATUS [utf-8] Thu Sep 10 20:58:34 2009
@@ -64,13 +64,6 @@
 
 CURRENT VOTES:
 
-  * unix/proc_mutex.c: Allow for Posix-sems to be actually named.
-      Trunk Version:
-        http://svn.apache.org/viewvc?view=rev&revision=811455
-        http://svn.apache.org/viewvc?view=rev&revision=813063
-      1.4 Version:
-        trunk works
-      +1: jim
 
 
 CURRENT test/testall -v EXCEPTIONS:

Modified: apr/apr/branches/1.4.x/locks/unix/proc_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.4.x/locks/unix/proc_mutex.c?rev=813586&r1=813585&r2=813586&view=diff
==============================================================================
--- apr/apr/branches/1.4.x/locks/unix/proc_mutex.c (original)
+++ apr/apr/branches/1.4.x/locks/unix/proc_mutex.c Thu Sep 10 20:58:34 2009
@@ -18,6 +18,7 @@
 #include "apr_strings.h"
 #include "apr_arch_proc_mutex.h"
 #include "apr_arch_file_io.h" /* for apr_mkstemp() */
+#include "apr_md5.h" /* for apr_md5() */
 
 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
 {
@@ -54,11 +55,10 @@
 static apr_status_t proc_mutex_posix_create(apr_proc_mutex_t *new_mutex,
                                             const char *fname)
 {
+    #define APR_POSIXSEM_NAME_MAX 30
+    #define APR_POSIXSEM_NAME_MIN 13
     sem_t *psem;
-    char semname[31];
-    apr_time_t now;
-    unsigned long sec;
-    unsigned long usec;
+    char semname[APR_MD5_DIGESTSIZE * 2 + 2];
     
     new_mutex->interproc = apr_palloc(new_mutex->pool,
                                       sizeof(*new_mutex->interproc));
@@ -69,28 +69,46 @@
      *   - be at most 14 chars
      *   - be unique and not match anything on the filesystem
      *
-     * Because of this, we ignore fname, and try our
-     * own naming system. We tuck the name away, since it might
-     * be useful for debugging. to  make this as robust as possible,
-     * we initially try something larger (and hopefully more unique)
-     * and gracefully fail down to the LCD above.
+     * Because of this, we use fname to generate an md5 hex checksum
+     * and use that as the name of the semaphore. If no filename was
+     * given, we create one based on the time. We tuck the name
+     * away, since it might be useful for debugging.
+     *
+     * To  make this as robust as possible, we initially try something
+     * larger (and hopefully more unique) and gracefully fail down to the
+     * LCD above.
      *
      * NOTE: Darwin (Mac OS X) seems to be the most restrictive
      * implementation. Versions previous to Darwin 6.2 had the 14
      * char limit, but later rev's allow up to 31 characters.
      *
      */
-    now = apr_time_now();
-    sec = apr_time_sec(now);
-    usec = apr_time_usec(now);
-    apr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", sec, usec);
+    if (fname) {
+        unsigned char digest[APR_MD5_DIGESTSIZE];   /* note dependency on semname here */
+        const char *hex = "0123456789abcdef";
+        char *p = semname;
+        int i;
+        apr_md5(digest, fname, strlen(fname));
+        *p++ = '/';     /* must start with /, right? */
+        for (i = 0; i < sizeof(digest); i++) {
+            *p++ = hex[digest[i] >> 4];
+            *p++ = hex[digest[i] & 0xF];
+        }
+        semname[APR_POSIXSEM_NAME_MAX] = '\0';
+    } else {
+        apr_time_t now;
+        unsigned long sec;
+        unsigned long usec;
+        now = apr_time_now();
+        sec = apr_time_sec(now);
+        usec = apr_time_usec(now);
+        apr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", sec, usec);
+    }
     psem = sem_open(semname, O_CREAT | O_EXCL, 0644, 1);
     if (psem == (sem_t *)SEM_FAILED) {
         if (errno == ENAMETOOLONG) {
             /* Oh well, good try */
-            semname[13] = '\0';
-        } else if (errno == EEXIST) {
-            apr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", usec, sec);
+            semname[APR_POSIXSEM_NAME_MIN] = '\0';
         } else {
             return errno;
         }