aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-01-13 19:01:28 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2011-01-13 19:01:28 -0500
commit52948169bcddf443b76d6ff1806259b153a2ac04 (patch)
tree9582d9be324ed653a88b63688f5e7073d3329ccf /src/backend
parentf0f36045b2e3d037bb7647d84373404fa4ba9588 (diff)
downloadpostgresql-52948169bcddf443b76d6ff1806259b153a2ac04.tar.gz
postgresql-52948169bcddf443b76d6ff1806259b153a2ac04.zip
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.
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/port/ipc_test.c4
-rw-r--r--src/backend/port/sysv_shmem.c13
-rw-r--r--src/backend/postmaster/postmaster.c35
-rw-r--r--src/backend/utils/init/miscinit.c77
4 files changed, 67 insertions, 62 deletions
diff --git a/src/backend/port/ipc_test.c b/src/backend/port/ipc_test.c
index 2518007c4b6..b4bcf40a7ab 100644
--- a/src/backend/port/ipc_test.c
+++ b/src/backend/port/ipc_test.c
@@ -104,7 +104,7 @@ on_exit_reset(void)
}
void
-AddToLockFile(int target_line, const char *str)
+AddToDataDirLockFile(int target_line, const char *str)
{
}
@@ -135,7 +135,7 @@ errcode_for_file_access(void)
bool
errstart(int elevel, const char *filename, int lineno,
- const char *funcname)
+ const char *funcname, const char *domain)
{
return (elevel >= ERROR);
}
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 474c6142ebe..aece026ec64 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -199,15 +199,16 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
on_shmem_exit(IpcMemoryDetach, PointerGetDatum(memAddress));
/*
- * Append record key and ID in lockfile for data directory. Format
- * to try to keep it the same length.
+ * Store shmem key and ID in data directory lockfile. Format to try to
+ * keep it the same length always (trailing junk in the lockfile won't
+ * hurt, but might confuse humans).
*/
{
- char line[32];
+ char line[64];
- sprintf(line, "%9lu %9lu\n", (unsigned long) memKey,
- (unsigned long) shmid);
- AddToLockFile(LOCK_FILE_LINES, line);
+ sprintf(line, "%9lu %9lu",
+ (unsigned long) memKey, (unsigned long) shmid);
+ AddToDataDirLockFile(LOCK_FILE_LINE_SHMEM_KEY, line);
}
return memAddress;
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index d158ded390b..179048f32d1 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -482,9 +482,9 @@ PostmasterMain(int argc, char *argv[])
int opt;
int status;
char *userDoption = NULL;
+ bool listen_addr_saved = false;
int i;
- bool connection_line_output = false;
-
+
MyProcPid = PostmasterPid = getpid();
MyStartTime = time(NULL);
@@ -861,24 +861,21 @@ PostmasterMain(int argc, char *argv[])
UnixSocketDir,
ListenSocket, MAXLISTEN);
else
- {
status = StreamServerPort(AF_UNSPEC, curhost,
(unsigned short) PostPortNumber,
UnixSocketDir,
ListenSocket, MAXLISTEN);
- /* must supply a valid listen_address for PQping() */
- if (!connection_line_output)
- {
- char line[MAXPGPATH + 2];
- sprintf(line, "%s\n", curhost);
- AddToLockFile(LOCK_FILE_LINES - 1, line);
- connection_line_output = true;
- }
- }
-
if (status == STATUS_OK)
+ {
success++;
+ /* record the first successful host addr in lockfile */
+ if (!listen_addr_saved)
+ {
+ AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, curhost);
+ listen_addr_saved = true;
+ }
+ }
else
ereport(WARNING,
(errmsg("could not create listen socket for \"%s\"",
@@ -893,10 +890,6 @@ PostmasterMain(int argc, char *argv[])
pfree(rawstring);
}
- /* Supply an empty listen_address line for PQping() */
- if (!connection_line_output)
- AddToLockFile(LOCK_FILE_LINES - 1, "\n");
-
#ifdef USE_BONJOUR
/* Register for Bonjour only if we opened TCP socket(s) */
if (enable_bonjour && ListenSocket[0] != PGINVALID_SOCKET)
@@ -953,6 +946,14 @@ PostmasterMain(int argc, char *argv[])
(errmsg("no socket created for listening")));
/*
+ * If no valid TCP ports, write an empty line for listen address,
+ * indicating the Unix socket must be used. Note that this line is not
+ * added to the lock file until there is a socket backing it.
+ */
+ if (!listen_addr_saved)
+ AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, "");
+
+ /*
* Set up shared memory and semaphores.
*/
reset_shared(PostPortNumber);
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 4f708352daa..ef6422e75c3 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -46,20 +46,7 @@
#define DIRECTORY_LOCK_FILE "postmaster.pid"
-/*
- * The lock file contents are:
- *
- * line #
- * 1 pid
- * 2 postmaster start time
- * 3 data directory
- * 4 port #
- * 5 user-specified socket directory
- * (the lines below are added later)
- * 6 first valid listen_address
- * 7 shared memory key
- */
-
+
ProcessingMode Mode = InitProcessing;
/* Note: we rely on this to initialize as zeroes */
@@ -244,7 +231,6 @@ static int SecurityRestrictionContext = 0;
static bool SetRoleIsActive = false;
-
/*
* GetUserId - get the current effective user ID.
*
@@ -691,7 +677,7 @@ CreateLockFile(const char *filename, bool amPostmaster,
bool isDDLock, const char *refName)
{
int fd;
- char buffer[MAXPGPATH * 3 + 256];
+ char buffer[MAXPGPATH * 2 + 256];
int ntries;
int len;
int encoded_pid;
@@ -852,26 +838,26 @@ CreateLockFile(const char *filename, bool amPostmaster,
* looking to see if there is an associated shmem segment that is
* still in use.
*
- * Note: because postmaster.pid is written in two steps, we might not
- * find the shmem ID values in it; we can't treat that as an error.
+ * Note: because postmaster.pid is written in multiple steps, we might
+ * not find the shmem ID values in it; we can't treat that as an
+ * error.
*/
if (isDDLock)
{
char *ptr = buffer;
- unsigned long id1, id2;
- int lineno;
+ unsigned long id1,
+ id2;
+ int lineno;
- for (lineno = 1; lineno <= LOCK_FILE_LINES - 1; lineno++)
+ for (lineno = 1; lineno < LOCK_FILE_LINE_SHMEM_KEY; lineno++)
{
if ((ptr = strchr(ptr, '\n')) == NULL)
- {
- elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
break;
- }
ptr++;
}
- if (ptr && sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
+ if (ptr != NULL &&
+ sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
{
if (PGSharedMemoryIsInUse(id1, id2))
ereport(FATAL,
@@ -903,12 +889,23 @@ CreateLockFile(const char *filename, bool amPostmaster,
}
/*
- * Successfully created the file, now fill it.
+ * Successfully created the file, now fill it. See comment in miscadmin.h
+ * about the contents. Note that we write the same info into both datadir
+ * and socket lockfiles; although more stuff may get added to the datadir
+ * lockfile later.
*/
- snprintf(buffer, sizeof(buffer), "%d\n%ld\n%s\n%d\n%s\n",
+ snprintf(buffer, sizeof(buffer), "%d\n%s\n%ld\n%d\n%s\n",
amPostmaster ? (int) my_pid : -((int) my_pid),
- (long) MyStartTime, DataDir, PostPortNumber,
- UnixSocketDir);
+ DataDir,
+ (long) MyStartTime,
+ PostPortNumber,
+#ifdef HAVE_UNIX_SOCKETS
+ (*UnixSocketDir != '\0') ? UnixSocketDir : DEFAULT_PGSOCKET_DIR
+#else
+ ""
+#endif
+ );
+
errno = 0;
if (write(fd, buffer, strlen(buffer)) != strlen(buffer))
{
@@ -1019,17 +1016,20 @@ TouchSocketLockFile(void)
/*
- * Add lines to the data directory lock file. This erases all following
- * lines, but that is OK because lines are added in order.
+ * Add (or replace) a line in the data directory lock file.
+ * The given string should not include a trailing newline.
+ *
+ * Caution: this erases all following lines. In current usage that is OK
+ * because lines are added in order. We could improve it if needed.
*/
void
-AddToLockFile(int target_line, const char *str)
+AddToDataDirLockFile(int target_line, const char *str)
{
int fd;
int len;
int lineno;
char *ptr;
- char buffer[MAXPGPATH * 3 + 256];
+ char buffer[BLCKSZ];
fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
if (fd < 0)
@@ -1040,7 +1040,7 @@ AddToLockFile(int target_line, const char *str)
DIRECTORY_LOCK_FILE)));
return;
}
- len = read(fd, buffer, sizeof(buffer) - 100);
+ len = read(fd, buffer, sizeof(buffer) - 1);
if (len < 0)
{
ereport(LOG,
@@ -1053,7 +1053,7 @@ AddToLockFile(int target_line, const char *str)
buffer[len] = '\0';
/*
- * Skip over first four lines (PID, pgdata, portnum, socketdir).
+ * Skip over lines we are not supposed to rewrite.
*/
ptr = buffer;
for (lineno = 1; lineno < target_line; lineno++)
@@ -1066,8 +1066,11 @@ AddToLockFile(int target_line, const char *str)
}
ptr++;
}
-
- strlcat(buffer, str, sizeof(buffer));
+
+ /*
+ * Write or rewrite the target line.
+ */
+ snprintf(ptr, buffer + sizeof(buffer) - ptr, "%s\n", str);
/*
* And rewrite the data. Since we write in a single kernel call, this