Commit 51e8e187 authored by Marc G. Fournier's avatar Marc G. Fournier

Massimo Dal Zotto <dz@cs.unitn.it>

> 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.
parent 53d7d473
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * 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 @@ ...@@ -55,6 +55,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/file.h>
#if defined(linux) #if defined(linux)
#ifndef SOMAXCONN #ifndef SOMAXCONN
...@@ -70,6 +71,7 @@ ...@@ -70,6 +71,7 @@
#ifdef MULTIBYTE #ifdef MULTIBYTE
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#endif #endif
#include "utils/trace.h"
/* ---------------- /* ----------------
* declarations * declarations
...@@ -522,6 +524,7 @@ StreamServerPort(char *hostName, short portName, int *fdP) ...@@ -522,6 +524,7 @@ StreamServerPort(char *hostName, short portName, int *fdP)
family; family;
size_t len; size_t len;
int one = 1; int one = 1;
int lock_fd;
family = ((hostName != NULL) ? AF_INET : AF_UNIX); family = ((hostName != NULL) ? AF_INET : AF_UNIX);
...@@ -550,6 +553,20 @@ StreamServerPort(char *hostName, short portName, int *fdP) ...@@ -550,6 +553,20 @@ StreamServerPort(char *hostName, short portName, int *fdP)
{ {
len = UNIXSOCK_PATH(saddr.un, portName); len = UNIXSOCK_PATH(saddr.un, portName);
strcpy(sock_path, saddr.un.sun_path); 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 else
{ {
...@@ -564,18 +581,32 @@ StreamServerPort(char *hostName, short portName, int *fdP) ...@@ -564,18 +581,32 @@ StreamServerPort(char *hostName, short portName, int *fdP)
"FATAL: StreamServerPort: bind() failed: errno=%d\n", "FATAL: StreamServerPort: bind() failed: errno=%d\n",
errno); errno);
pqdebug("%s", PQerrormsg); 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) if (family == AF_UNIX)
strcat(PQerrormsg, "\tIf not, remove socket node (/tmp/.s.PGSQL.<portnumber>)and retry.\n"); sprintf(PQerrormsg+strlen(PQerrormsg),
"\tIf not, remove socket node (%s) and retry.\n",
sock_path);
else else
strcat(PQerrormsg, "\tIf not, wait a few seconds and retry.\n"); strcat(PQerrormsg, "\tIf not, wait a few seconds and retry.\n");
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
return (STATUS_ERROR); return (STATUS_ERROR);
} }
if (family == AF_UNIX) if (family == AF_UNIX) {
on_proc_exit(StreamDoUnlink, NULL); 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); listen(fd, SOMAXCONN);
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment