Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit51e8e18

Browse files
committed
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.
1 parent53d7d47 commit51e8e18

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

‎src/backend/libpq/pqcomm.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.50 1998/07/26 04:30:28 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.51 1998/08/25 21:32:10 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -55,6 +55,7 @@
5555
#include<netinet/in.h>
5656
#include<netinet/tcp.h>
5757
#include<arpa/inet.h>
58+
#include<sys/file.h>
5859

5960
#if defined(linux)
6061
#ifndefSOMAXCONN
@@ -70,6 +71,7 @@
7071
#ifdefMULTIBYTE
7172
#include"mb/pg_wchar.h"
7273
#endif
74+
#include"utils/trace.h"
7375

7476
/* ----------------
7577
*declarations
@@ -522,6 +524,7 @@ StreamServerPort(char *hostName, short portName, int *fdP)
522524
family;
523525
size_tlen;
524526
intone=1;
527+
intlock_fd;
525528

526529
family= ((hostName!=NULL) ?AF_INET :AF_UNIX);
527530

@@ -550,6 +553,20 @@ StreamServerPort(char *hostName, short portName, int *fdP)
550553
{
551554
len=UNIXSOCK_PATH(saddr.un,portName);
552555
strcpy(sock_path,saddr.un.sun_path);
556+
557+
/*
558+
* If the socket exists but nobody has an advisory lock on it
559+
* we can safely delete the file.
560+
*/
561+
if ((lock_fd=open(sock_path,O_RDONLY|O_NONBLOCK,0666)) >=0) {
562+
if (flock(lock_fd,LOCK_EX|LOCK_NB)==0) {
563+
TPRINTF(TRACE_VERBOSE,"flock on %s, deleting",sock_path);
564+
unlink(sock_path);
565+
}else {
566+
TPRINTF(TRACE_VERBOSE,"flock failed for %s",sock_path);
567+
}
568+
close(lock_fd);
569+
}
553570
}
554571
else
555572
{
@@ -564,18 +581,32 @@ StreamServerPort(char *hostName, short portName, int *fdP)
564581
"FATAL: StreamServerPort: bind() failed: errno=%d\n",
565582
errno);
566583
pqdebug("%s",PQerrormsg);
567-
strcat(PQerrormsg,"\tIs another postmaster already running on that port?\n");
584+
strcat(PQerrormsg,
585+
"\tIs another postmaster already running on that port?\n");
568586
if (family==AF_UNIX)
569-
strcat(PQerrormsg,"\tIf not, remove socket node (/tmp/.s.PGSQL.<portnumber>)and retry.\n");
587+
sprintf(PQerrormsg+strlen(PQerrormsg),
588+
"\tIf not, remove socket node (%s) and retry.\n",
589+
sock_path);
570590
else
571591
strcat(PQerrormsg,"\tIf not, wait a few seconds and retry.\n");
572592
fputs(PQerrormsg,stderr);
573593
return (STATUS_ERROR);
574594
}
575595

576-
if (family==AF_UNIX)
596+
if (family==AF_UNIX) {
577597
on_proc_exit(StreamDoUnlink,NULL);
578598

599+
/*
600+
* Open the socket file and get an advisory lock on it.
601+
* The lock_fd is left open to keep the lock.
602+
*/
603+
if ((lock_fd=open(sock_path,O_RDONLY|O_NONBLOCK,0666)) >=0) {
604+
if (flock(lock_fd,LOCK_EX|LOCK_NB)!=0) {
605+
TPRINTF(TRACE_VERBOSE,"flock error for %s",sock_path);
606+
}
607+
}
608+
}
609+
579610
listen(fd,SOMAXCONN);
580611

581612
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp