apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r813589 - in /apr/apr/branches/1.3.x: CHANGES locks/unix/proc_mutex.c
Date Thu, 10 Sep 2009 21:07:37 GMT
Author: jim
Date: Thu Sep 10 21:07:36 2009
New Revision: 813589

URL: http://svn.apache.org/viewvc?rev=813589&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.3.x/CHANGES
    apr/apr/branches/1.3.x/locks/unix/proc_mutex.c

Modified: apr/apr/branches/1.3.x/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.3.x/CHANGES?rev=813589&r1=813588&r2=813589&view=diff
==============================================================================
--- apr/apr/branches/1.3.x/CHANGES [utf-8] (original)
+++ apr/apr/branches/1.3.x/CHANGES [utf-8] Thu Sep 10 21:07:36 2009
@@ -1,8 +1,11 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 1.3.9
 
+  *) 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.3.x/locks/unix/proc_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.3.x/locks/unix/proc_mutex.c?rev=813589&r1=813588&r2=813589&view=diff
==============================================================================
--- apr/apr/branches/1.3.x/locks/unix/proc_mutex.c (original)
+++ apr/apr/branches/1.3.x/locks/unix/proc_mutex.c Thu Sep 10 21:07:36 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;
         }



Mime
View raw message