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

Commit5294816

Browse files
committed
Code review for postmaster.pid contents changes.
Fix broken test for pre-existing postmaster, caused by wrong code forappending lines to the lockfile; don't write a failed listen_addresssetting into the lockfile; don't arbitrarily change the location of thedata directory in the lockfile compared to previous releases; provide moreconsistent and useful definitions of the socket path and listen_addressentries; avoid assuming that pg_ctl has the same DEFAULT_PGSOCKET_DIR asthe postmaster; assorted code style improvements.
1 parentf0f3604 commit5294816

File tree

7 files changed

+194
-159
lines changed

7 files changed

+194
-159
lines changed

‎doc/src/sgml/storage.sgml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,14 @@ last started with</entry>
117117
<row>
118118
<entry><filename>postmaster.pid</></entry>
119119
<entry>A lock file recording the current postmaster process id (PID),
120-
postmaster start time, cluster data directory, port number, user-specified
121-
Unix domain socket directory, first valid listen_address host, and
122-
shared memory segment ID</entry>
120+
cluster data directory path,
121+
postmaster start timestamp,
122+
port number,
123+
Unix-domain socket directory path (empty on Windows),
124+
first valid listen_address (IP address or <literal>*</>, or empty if
125+
not listening on TCP),
126+
and shared memory segment ID
127+
(this file is not present after server shutdown)</entry>
123128
</row>
124129

125130
</tbody>

‎src/backend/port/ipc_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ on_exit_reset(void)
104104
}
105105

106106
void
107-
AddToLockFile(inttarget_line,constchar*str)
107+
AddToDataDirLockFile(inttarget_line,constchar*str)
108108
{
109109
}
110110

@@ -135,7 +135,7 @@ errcode_for_file_access(void)
135135

136136
bool
137137
errstart(intelevel,constchar*filename,intlineno,
138-
constchar*funcname)
138+
constchar*funcname,constchar*domain)
139139
{
140140
return (elevel >=ERROR);
141141
}

‎src/backend/port/sysv_shmem.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,16 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
199199
on_shmem_exit(IpcMemoryDetach,PointerGetDatum(memAddress));
200200

201201
/*
202-
* Append record key and ID in lockfile for data directory. Format
203-
* to try to keep it the same length.
202+
* Store shmem key and ID in data directory lockfile. Format to try to
203+
* keep it the same length always (trailing junk in the lockfile won't
204+
* hurt, but might confuse humans).
204205
*/
205206
{
206-
charline[32];
207+
charline[64];
207208

208-
sprintf(line,"%9lu %9lu\n", (unsigned long)memKey,
209-
(unsigned long)shmid);
210-
AddToLockFile(LOCK_FILE_LINES,line);
209+
sprintf(line,"%9lu %9lu",
210+
(unsigned long)memKey, (unsigned long)shmid);
211+
AddToDataDirLockFile(LOCK_FILE_LINE_SHMEM_KEY,line);
211212
}
212213

213214
returnmemAddress;

‎src/backend/postmaster/postmaster.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ PostmasterMain(int argc, char *argv[])
482482
intopt;
483483
intstatus;
484484
char*userDoption=NULL;
485+
boollisten_addr_saved= false;
485486
inti;
486-
boolconnection_line_output= false;
487-
487+
488488
MyProcPid=PostmasterPid=getpid();
489489

490490
MyStartTime=time(NULL);
@@ -861,24 +861,21 @@ PostmasterMain(int argc, char *argv[])
861861
UnixSocketDir,
862862
ListenSocket,MAXLISTEN);
863863
else
864-
{
865864
status=StreamServerPort(AF_UNSPEC,curhost,
866865
(unsigned short)PostPortNumber,
867866
UnixSocketDir,
868867
ListenSocket,MAXLISTEN);
869-
/* must supply a valid listen_address for PQping() */
870-
if (!connection_line_output)
871-
{
872-
charline[MAXPGPATH+2];
873868

874-
sprintf(line,"%s\n",curhost);
875-
AddToLockFile(LOCK_FILE_LINES-1,line);
876-
connection_line_output= true;
877-
}
878-
}
879-
880869
if (status==STATUS_OK)
870+
{
881871
success++;
872+
/* record the first successful host addr in lockfile */
873+
if (!listen_addr_saved)
874+
{
875+
AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR,curhost);
876+
listen_addr_saved= true;
877+
}
878+
}
882879
else
883880
ereport(WARNING,
884881
(errmsg("could not create listen socket for \"%s\"",
@@ -893,10 +890,6 @@ PostmasterMain(int argc, char *argv[])
893890
pfree(rawstring);
894891
}
895892

896-
/* Supply an empty listen_address line for PQping() */
897-
if (!connection_line_output)
898-
AddToLockFile(LOCK_FILE_LINES-1,"\n");
899-
900893
#ifdefUSE_BONJOUR
901894
/* Register for Bonjour only if we opened TCP socket(s) */
902895
if (enable_bonjour&&ListenSocket[0]!=PGINVALID_SOCKET)
@@ -952,6 +945,14 @@ PostmasterMain(int argc, char *argv[])
952945
ereport(FATAL,
953946
(errmsg("no socket created for listening")));
954947

948+
/*
949+
* If no valid TCP ports, write an empty line for listen address,
950+
* indicating the Unix socket must be used. Note that this line is not
951+
* added to the lock file until there is a socket backing it.
952+
*/
953+
if (!listen_addr_saved)
954+
AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR,"");
955+
955956
/*
956957
* Set up shared memory and semaphores.
957958
*/

‎src/backend/utils/init/miscinit.c

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,7 @@
4646

4747

4848
#defineDIRECTORY_LOCK_FILE"postmaster.pid"
49-
/*
50-
*The lock file contents are:
51-
*
52-
* line #
53-
*1pid
54-
*2postmaster start time
55-
*3data directory
56-
*4port #
57-
*5user-specified socket directory
58-
*(the lines below are added later)
59-
*6first valid listen_address
60-
*7shared memory key
61-
*/
62-
49+
6350
ProcessingModeMode=InitProcessing;
6451

6552
/* Note: we rely on this to initialize as zeroes */
@@ -244,7 +231,6 @@ static intSecurityRestrictionContext = 0;
244231
staticboolSetRoleIsActive= false;
245232

246233

247-
248234
/*
249235
* GetUserId - get the current effective user ID.
250236
*
@@ -691,7 +677,7 @@ CreateLockFile(const char *filename, bool amPostmaster,
691677
boolisDDLock,constchar*refName)
692678
{
693679
intfd;
694-
charbuffer[MAXPGPATH*3+256];
680+
charbuffer[MAXPGPATH*2+256];
695681
intntries;
696682
intlen;
697683
intencoded_pid;
@@ -852,26 +838,26 @@ CreateLockFile(const char *filename, bool amPostmaster,
852838
* looking to see if there is an associated shmem segment that is
853839
* still in use.
854840
*
855-
* Note: because postmaster.pid is written in two steps, we might not
856-
* find the shmem ID values in it; we can't treat that as an error.
841+
* Note: because postmaster.pid is written in multiple steps, we might
842+
* not find the shmem ID values in it; we can't treat that as an
843+
* error.
857844
*/
858845
if (isDDLock)
859846
{
860847
char*ptr=buffer;
861-
unsigned longid1,id2;
862-
intlineno;
848+
unsigned longid1,
849+
id2;
850+
intlineno;
863851

864-
for (lineno=1;lineno <=LOCK_FILE_LINES-1;lineno++)
852+
for (lineno=1;lineno<LOCK_FILE_LINE_SHMEM_KEY;lineno++)
865853
{
866854
if ((ptr=strchr(ptr,'\n'))==NULL)
867-
{
868-
elog(LOG,"bogus data in \"%s\"",DIRECTORY_LOCK_FILE);
869855
break;
870-
}
871856
ptr++;
872857
}
873858

874-
if (ptr&&sscanf(ptr,"%lu %lu",&id1,&id2)==2)
859+
if (ptr!=NULL&&
860+
sscanf(ptr,"%lu %lu",&id1,&id2)==2)
875861
{
876862
if (PGSharedMemoryIsInUse(id1,id2))
877863
ereport(FATAL,
@@ -903,12 +889,23 @@ CreateLockFile(const char *filename, bool amPostmaster,
903889
}
904890

905891
/*
906-
* Successfully created the file, now fill it.
892+
* Successfully created the file, now fill it. See comment in miscadmin.h
893+
* about the contents. Note that we write the same info into both datadir
894+
* and socket lockfiles; although more stuff may get added to the datadir
895+
* lockfile later.
907896
*/
908-
snprintf(buffer,sizeof(buffer),"%d\n%ld\n%s\n%d\n%s\n",
897+
snprintf(buffer,sizeof(buffer),"%d\n%s\n%ld\n%d\n%s\n",
909898
amPostmaster ? (int)my_pid :-((int)my_pid),
910-
(long)MyStartTime,DataDir,PostPortNumber,
911-
UnixSocketDir);
899+
DataDir,
900+
(long)MyStartTime,
901+
PostPortNumber,
902+
#ifdefHAVE_UNIX_SOCKETS
903+
(*UnixSocketDir!='\0') ?UnixSocketDir :DEFAULT_PGSOCKET_DIR
904+
#else
905+
""
906+
#endif
907+
);
908+
912909
errno=0;
913910
if (write(fd,buffer,strlen(buffer))!=strlen(buffer))
914911
{
@@ -1019,17 +1016,20 @@ TouchSocketLockFile(void)
10191016

10201017

10211018
/*
1022-
* Add lines to the data directory lock file. This erases all following
1023-
* lines, but that is OK because lines are added in order.
1019+
* Add (or replace) a line in the data directory lock file.
1020+
* The given string should not include a trailing newline.
1021+
*
1022+
* Caution: this erases all following lines. In current usage that is OK
1023+
* because lines are added in order. We could improve it if needed.
10241024
*/
10251025
void
1026-
AddToLockFile(inttarget_line,constchar*str)
1026+
AddToDataDirLockFile(inttarget_line,constchar*str)
10271027
{
10281028
intfd;
10291029
intlen;
10301030
intlineno;
10311031
char*ptr;
1032-
charbuffer[MAXPGPATH*3+256];
1032+
charbuffer[BLCKSZ];
10331033

10341034
fd=open(DIRECTORY_LOCK_FILE,O_RDWR |PG_BINARY,0);
10351035
if (fd<0)
@@ -1040,7 +1040,7 @@ AddToLockFile(int target_line, const char *str)
10401040
DIRECTORY_LOCK_FILE)));
10411041
return;
10421042
}
1043-
len=read(fd,buffer,sizeof(buffer)-100);
1043+
len=read(fd,buffer,sizeof(buffer)-1);
10441044
if (len<0)
10451045
{
10461046
ereport(LOG,
@@ -1053,7 +1053,7 @@ AddToLockFile(int target_line, const char *str)
10531053
buffer[len]='\0';
10541054

10551055
/*
1056-
* Skip overfirst four lines (PID, pgdata, portnum, socketdir).
1056+
* Skip overlines we are not supposed to rewrite.
10571057
*/
10581058
ptr=buffer;
10591059
for (lineno=1;lineno<target_line;lineno++)
@@ -1066,8 +1066,11 @@ AddToLockFile(int target_line, const char *str)
10661066
}
10671067
ptr++;
10681068
}
1069-
1070-
strlcat(buffer,str,sizeof(buffer));
1069+
1070+
/*
1071+
* Write or rewrite the target line.
1072+
*/
1073+
snprintf(ptr,buffer+sizeof(buffer)-ptr,"%s\n",str);
10711074

10721075
/*
10731076
* And rewrite the data. Since we write in a single kernel call, this

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp