You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Jim Jagielski <ji...@jaguNET.com> on 2003/02/22 17:45:53 UTC

[PATCH] posixsem semaphore name

This has bugged more for awhile. We should be a bit more robust if
we can. I'm almost tempted to 1st try what's passed in fname...

Comments?

Index: locks/unix/proc_mutex.c
===================================================================
RCS file: /home/cvs/apr/locks/unix/proc_mutex.c,v
retrieving revision 1.27
diff -u -r1.27 proc_mutex.c
--- locks/unix/proc_mutex.c	7 Jan 2003 00:52:55 -0000	1.27
+++ locks/unix/proc_mutex.c	22 Feb 2003 16:40:39 -0000
@@ -91,9 +91,10 @@
 {
     sem_t *psem;
     apr_status_t stat;
-    char semname[14];
+    char semname[31];
     apr_time_t now;
-    unsigned long epoch;
+    unsigned long sec;
+    unsigned long usec;
     
     new_mutex->interproc = apr_palloc(new_mutex->pool,
                                       sizeof(*new_mutex->interproc));
@@ -104,20 +105,34 @@
      *   - be at most 14 chars
      *   - be unique and not match anything on the filesystem
      *
-     * Because of this, we ignore fname and craft our own.
+     * Because of this, we ignore fname, and try our
+     * own naming system. We tuck the name away, since it might
+     * be useful for debugging.
+     *
+     * 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. To
+     * make this as robust as possible, we initially try something
+     * larger (and hopefully more unique)
      *
      * FIXME: There is a small window of opportunity where
      * instead of getting a new semaphore descriptor, we get
      * a previously obtained one. This can happen if the requests
-     * are made at the "same time" (within a second, due to the
-     * apr_time_now() call) and in the small span of time between
+     * are made at the "same time" and in the small span of time between
      * the sem_open and the sem_unlink. Use of O_EXCL does not
      * help here however...
+     *
      */
     now = apr_time_now();
-    epoch = apr_time_sec(now);
-    apr_snprintf(semname, sizeof(semname), "/ApR.%lx", epoch);
+    sec = apr_time_sec(now);
+    usec = apr_time_usec(now);
+    apr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", sec, usec);
     psem = sem_open((const char *) semname, O_CREAT, 0644, 1);
+    if ((psem == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
+        /* Oh well, good try */
+        semname[13] = '\0';
+        psem = sem_open((const char *) semname, O_CREAT, 0644, 1);
+    }
 
     if (psem == (sem_t *)SEM_FAILED) {
         stat = errno;
@@ -127,6 +142,7 @@
     /* Ahhh. The joys of Posix sems. Predelete it... */
     sem_unlink((const char *) semname);
     new_mutex->interproc->filedes = (int)psem;	/* Ugg */
+    new_mutex->fname = apr_pstrdup(new_mutex->pool, semname);
     apr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
                               apr_proc_mutex_cleanup, 
                               apr_pool_cleanup_null);

-- 
===========================================================================
   Jim Jagielski   [|]   jim@jaguNET.com   [|]   http://www.jaguNET.com/
      "A society that will trade a little liberty for a little order
             will lose both and deserve neither" - T.Jefferson

Re: [PATCH] posixsem semaphore name

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
At 10:45 AM 2/22/2003, Jim Jagielski wrote:
>This has bugged more for awhile. We should be a bit more robust if
>we can. I'm almost tempted to 1st try what's passed in fname...
>
>Comments?

Sure :-)  If you expose an argument and then ignore the value from the
user, that is sort of bad form.  It's really hard for new users to troubleshoot
when the API doesn't do what you ask.

I'd like to see us use our 'invented' name only when the user passes NULL
for the fname, otherwise trust in what they pass.

It would be cool for us to support some sort of tmpname style pattern,
but we would have to distinguish incomplete patterns from explicit names.
That sounds like a job for a flag.

Either way, I'd say do as the developer asks, not whatever we want :-)
If you have two disjoint processes, it will be very hard for those two
processes to share the same named semaphore when we reinvent
that name in both processes :(

Bill


>Index: locks/unix/proc_mutex.c
>===================================================================
>RCS file: /home/cvs/apr/locks/unix/proc_mutex.c,v
>retrieving revision 1.27
>diff -u -r1.27 proc_mutex.c
>--- locks/unix/proc_mutex.c     7 Jan 2003 00:52:55 -0000       1.27
>+++ locks/unix/proc_mutex.c     22 Feb 2003 16:40:39 -0000
>@@ -91,9 +91,10 @@
> {
>     sem_t *psem;
>     apr_status_t stat;
>-    char semname[14];
>+    char semname[31];
>     apr_time_t now;
>-    unsigned long epoch;
>+    unsigned long sec;
>+    unsigned long usec;
>     
>     new_mutex->interproc = apr_palloc(new_mutex->pool,
>                                       sizeof(*new_mutex->interproc));
>@@ -104,20 +105,34 @@
>      *   - be at most 14 chars
>      *   - be unique and not match anything on the filesystem
>      *
>-     * Because of this, we ignore fname and craft our own.
>+     * Because of this, we ignore fname, and try our
>+     * own naming system. We tuck the name away, since it might
>+     * be useful for debugging.
>+     *
>+     * 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. To
>+     * make this as robust as possible, we initially try something
>+     * larger (and hopefully more unique)
>      *
>      * FIXME: There is a small window of opportunity where
>      * instead of getting a new semaphore descriptor, we get
>      * a previously obtained one. This can happen if the requests
>-     * are made at the "same time" (within a second, due to the
>-     * apr_time_now() call) and in the small span of time between
>+     * are made at the "same time" and in the small span of time between
>      * the sem_open and the sem_unlink. Use of O_EXCL does not
>      * help here however...
>+     *
>      */
>     now = apr_time_now();
>-    epoch = apr_time_sec(now);
>-    apr_snprintf(semname, sizeof(semname), "/ApR.%lx", epoch);
>+    sec = apr_time_sec(now);
>+    usec = apr_time_usec(now);
>+    apr_snprintf(semname, sizeof(semname), "/ApR.%lxZ%lx", sec, usec);
>     psem = sem_open((const char *) semname, O_CREAT, 0644, 1);
>+    if ((psem == (sem_t *)SEM_FAILED) && (errno == ENAMETOOLONG)) {
>+        /* Oh well, good try */
>+        semname[13] = '\0';
>+        psem = sem_open((const char *) semname, O_CREAT, 0644, 1);
>+    }
> 
>     if (psem == (sem_t *)SEM_FAILED) {
>         stat = errno;
>@@ -127,6 +142,7 @@
>     /* Ahhh. The joys of Posix sems. Predelete it... */
>     sem_unlink((const char *) semname);
>     new_mutex->interproc->filedes = (int)psem; /* Ugg */
>+    new_mutex->fname = apr_pstrdup(new_mutex->pool, semname);
>     apr_pool_cleanup_register(new_mutex->pool, (void *)new_mutex,
>                               apr_proc_mutex_cleanup, 
>                               apr_pool_cleanup_null);
>
>-- 
>===========================================================================
>   Jim Jagielski   [|]   jim@jaguNET.com   [|]   http://www.jaguNET.com/
>      "A society that will trade a little liberty for a little order
>             will lose both and deserve neither" - T.Jefferson