Massimo Dal Zotto
authorMarc G. Fournier
Tue, 25 Aug 1998 21:32:10 +0000 (21:32 +0000)
committerMarc G. Fournier
Tue, 25 Aug 1998 21:32:10 +0000 (21:32 +0000)
> socket-flock.patch
>
>       use advisory locks to check if the unix socket can be deleted.
>       A running postmaster keeps a lock on that file. A starting
>       postmaster exits if the file exists and is locked, otherwise
>       it deletes the sockets and proceeds.
>       This avoid the need to remove manually the file after a postmaster
>       or system crash.
>       I don't know if flock is available on any system. If not we could
>       define a HAVE_FLOCK set by configure.

src/backend/libpq/pqcomm.c

index 56f0fd658e99a40969c90891c09e56c3fda7f14c..1a67903959dc05453a34f7a6e94108094b0896ef 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.50 1998/07/26 04:30:28 scrappy Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.51 1998/08/25 21:32:10 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -55,6 +55,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #if defined(linux)
 #ifndef SOMAXCONN
@@ -70,6 +71,7 @@
 #ifdef MULTIBYTE
 #include "mb/pg_wchar.h"
 #endif
+#include "utils/trace.h"
 
 /* ----------------
  *     declarations
@@ -522,6 +524,7 @@ StreamServerPort(char *hostName, short portName, int *fdP)
                family;
    size_t      len;
    int         one = 1;
+   int         lock_fd;
 
    family = ((hostName != NULL) ? AF_INET : AF_UNIX);
 
@@ -550,6 +553,20 @@ StreamServerPort(char *hostName, short portName, int *fdP)
    {
        len = UNIXSOCK_PATH(saddr.un, portName);
        strcpy(sock_path, saddr.un.sun_path);
+
+       /*
+        * If the socket exists but nobody has an advisory lock on it
+        * we can safely delete the file.
+        */
+       if ((lock_fd = open(sock_path, O_RDONLY|O_NONBLOCK, 0666)) >= 0) {
+           if (flock(lock_fd, LOCK_EX|LOCK_NB) == 0) {
+               TPRINTF(TRACE_VERBOSE, "flock on %s, deleting", sock_path);
+               unlink(sock_path);
+           } else {
+               TPRINTF(TRACE_VERBOSE, "flock failed for %s", sock_path);
+           }
+           close(lock_fd);
+       }
    }
    else
    {
@@ -564,18 +581,32 @@ StreamServerPort(char *hostName, short portName, int *fdP)
                "FATAL: StreamServerPort: bind() failed: errno=%d\n",
                errno);
        pqdebug("%s", PQerrormsg);
-       strcat(PQerrormsg, "\tIs another postmaster already running on that port?\n");
+       strcat(PQerrormsg,
+              "\tIs another postmaster already running on that port?\n");
        if (family == AF_UNIX)
-           strcat(PQerrormsg, "\tIf not, remove socket node (/tmp/.s.PGSQL.)and retry.\n");
+           sprintf(PQerrormsg+strlen(PQerrormsg),
+                  "\tIf not, remove socket node (%s) and retry.\n",
+                   sock_path);
        else
            strcat(PQerrormsg, "\tIf not, wait a few seconds and retry.\n");
        fputs(PQerrormsg, stderr);
        return (STATUS_ERROR);
    }
 
-   if (family == AF_UNIX)
+   if (family == AF_UNIX) {
        on_proc_exit(StreamDoUnlink, NULL);
 
+       /*
+        * Open the socket file and get an advisory lock on it.
+        * The lock_fd is left open to keep the lock.
+        */
+       if ((lock_fd = open(sock_path, O_RDONLY|O_NONBLOCK, 0666)) >= 0) {
+           if (flock(lock_fd, LOCK_EX|LOCK_NB) != 0) {
+               TPRINTF(TRACE_VERBOSE, "flock error for %s", sock_path);
+           }
+       }
+   }
+
    listen(fd, SOMAXCONN);
 
    /*