Skip to content

Commit 5294816

Browse files
committed
Code review for postmaster.pid contents changes.
Fix broken test for pre-existing postmaster, caused by wrong code for appending lines to the lockfile; don't write a failed listen_address setting into the lockfile; don't arbitrarily change the location of the data directory in the lockfile compared to previous releases; provide more consistent and useful definitions of the socket path and listen_address entries; avoid assuming that pg_ctl has the same DEFAULT_PGSOCKET_DIR as the postmaster; assorted code style improvements.
1 parent f0f3604 commit 5294816

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(int target_line, const char *str)
107+
AddToDataDirLockFile(int target_line, const char *str)
108108
{
109109
}
110110

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

136136
bool
137137
errstart(int elevel, const char *filename, int lineno,
138-
const char *funcname)
138+
const char *funcname, const char *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-
char line[32];
207+
char line[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
return memAddress;

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
int opt;
483483
int status;
484484
char *userDoption = NULL;
485+
bool listen_addr_saved = false;
485486
int i;
486-
bool connection_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-
char line[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
#ifdef USE_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
#define DIRECTORY_LOCK_FILE "postmaster.pid"
49-
/*
50-
* The lock file contents are:
51-
*
52-
* line #
53-
* 1 pid
54-
* 2 postmaster start time
55-
* 3 data directory
56-
* 4 port #
57-
* 5 user-specified socket directory
58-
* (the lines below are added later)
59-
* 6 first valid listen_address
60-
* 7 shared memory key
61-
*/
62-
49+
6350
ProcessingMode Mode = InitProcessing;
6451

6552
/* Note: we rely on this to initialize as zeroes */
@@ -244,7 +231,6 @@ static int SecurityRestrictionContext = 0;
244231
static bool SetRoleIsActive = 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
bool isDDLock, const char *refName)
692678
{
693679
int fd;
694-
char buffer[MAXPGPATH * 3 + 256];
680+
char buffer[MAXPGPATH * 2 + 256];
695681
int ntries;
696682
int len;
697683
int encoded_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 long id1, id2;
862-
int lineno;
848+
unsigned long id1,
849+
id2;
850+
int lineno;
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+
#ifdef HAVE_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(int target_line, const char *str)
1026+
AddToDataDirLockFile(int target_line, const char *str)
10271027
{
10281028
int fd;
10291029
int len;
10301030
int lineno;
10311031
char *ptr;
1032-
char buffer[MAXPGPATH * 3 + 256];
1032+
char buffer[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 over first four lines (PID, pgdata, portnum, socketdir).
1056+
* Skip over lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy