Skip to content

Commit dde36bf

Browse files
committed
Create postmaster.pid and postmaster.opts under $PGDATA
1 parent 9de156f commit dde36bf

File tree

1 file changed

+172
-3
lines changed

1 file changed

+172
-3
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 172 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.127 1999/10/25 03:07:45 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.128 1999/12/03 06:26:34 ishii Exp $
1414
*
1515
* NOTES
1616
*
@@ -92,6 +92,23 @@
9292
#include "utils/trace.h"
9393
#include "version.h"
9494

95+
/*
96+
* "postmaster.pid" is a file containing postmaster's pid, being
97+
* created uder $PGDATA upon postmaster's starting up. When postmaster
98+
* shuts down, it will be unlinked. The purpose of the file includes:
99+
*
100+
* (1) supplying neccessary information to stop/restart postmaster
101+
* (2) preventing another postmaster process starting while it has
102+
* already started
103+
*/
104+
#define PIDFNAME "postmaster.pid"
105+
106+
/*
107+
* "postmaster.opts" is a file containing options for postmaser.
108+
* pg_ctl will use it to restart postmaster.
109+
*/
110+
#define OPTSFNAME "postmaster.opts"
111+
95112
#if !defined(MAXINT)
96113
#define MAXINT INT_MAX
97114
#endif
@@ -261,6 +278,15 @@ static void RandomSalt(char *salt);
261278
static void SignalChildren(SIGNAL_ARGS);
262279
static int CountChildren(void);
263280

281+
static int SetPidFile(pid_t pid, char *progname, int port, char *datadir,
282+
int assert, int nbuf, char *execfile,
283+
int debuglvl, int netserver,
284+
#ifdef USE_SSL
285+
int securenetserver,
286+
#endif
287+
int maxbackends, int reinit,
288+
int silent, int sendstop, char *extraoptions);
289+
264290
extern int BootstrapMain(int argc, char *argv[]);
265291
static pid_t SSDataBase(bool startup);
266292
#define StartupDataBase() SSDataBase(true)
@@ -272,11 +298,11 @@ static void InitSSL(void);
272298

273299
#ifdef CYR_RECODE
274300
void GetCharSetByHost(char *, int, char *);
275-
276301
#endif
277302

278303
#ifdef USE_ASSERT_CHECKING
279-
int assert_enabled = 1;
304+
305+
int assert_enabled = 1;
280306

281307
#endif
282308

@@ -351,6 +377,9 @@ PostmasterMain(int argc, char *argv[])
351377
bool DataDirOK; /* We have a usable PGDATA value */
352378
char hostbuf[MAXHOSTNAMELEN];
353379
int nonblank_argc;
380+
char original_extraoptions[MAXPGPATH];
381+
382+
*original_extraoptions = '\0';
354383

355384
/*
356385
* We need three params so we can display status. If we don't get
@@ -509,6 +538,7 @@ PostmasterMain(int argc, char *argv[])
509538
*/
510539
strcat(ExtraOptions, " ");
511540
strcat(ExtraOptions, optarg);
541+
strcpy(original_extraoptions, optarg);
512542
break;
513543
case 'p':
514544
/* Set PGPORT by hand. */
@@ -618,6 +648,33 @@ PostmasterMain(int argc, char *argv[])
618648
if (silentflag)
619649
pmdaemonize();
620650

651+
/*
652+
* create pid file. if the file has already existed, exits.
653+
*/
654+
if (SetPidFile(
655+
getpid(), /* postmaster process id */
656+
progname, /* postmaster executable file */
657+
PostPortName, /* port number */
658+
DataDir, /* PGDATA */
659+
assert_enabled, /* whether -A is specified or not */
660+
NBuffers, /* -B: number of shared buffers */
661+
Execfile, /* -b: postgres executable file */
662+
DebugLvl, /* -d: debug level */
663+
NetServer, /* -i: accept connection from INET */
664+
#ifdef USE_SSL
665+
SecureNetServer, /* -l: use SSL */
666+
#endif
667+
MaxBackends, /* -N: max number of backends */
668+
Reinit, /* -n: reinit shared mem after failure */
669+
silentflag, /* -S: detach tty */
670+
SendStop, /* -s: send SIGSTOP */
671+
original_extraoptions /* options for backend */
672+
)
673+
) {
674+
ExitPostmaster(1);
675+
return 0;
676+
}
677+
621678
/*
622679
* Set up signal handlers for the postmaster process.
623680
*/
@@ -2068,3 +2125,115 @@ SSDataBase(bool startup)
20682125

20692126
return(pid);
20702127
}
2128+
2129+
static char PidFile[MAXPGPATH];
2130+
2131+
static void UnlinkPidFile(void)
2132+
{
2133+
unlink(PidFile);
2134+
}
2135+
2136+
static int SetPidFile(pid_t pid, char *progname, int port, char *datadir,
2137+
int assert, int nbuf, char *execfile,
2138+
int debuglvl, int netserver,
2139+
#ifdef USE_SSL
2140+
int securenetserver,
2141+
#endif
2142+
int maxbackends, int reinit,
2143+
int silent, int sendstop, char *extraoptions)
2144+
{
2145+
int fd;
2146+
char optsfile[MAXPGPATH];
2147+
char pidstr[32];
2148+
char opts[1024];
2149+
char buf[1024];
2150+
2151+
/*
2152+
* Creating pid file
2153+
*/
2154+
sprintf(PidFile,"%s/%s", datadir, PIDFNAME);
2155+
fd = open(PidFile, O_RDWR | O_CREAT | O_EXCL, 0600);
2156+
if (fd < 0) {
2157+
fprintf(stderr, "Can't create pidfile: %s\n", PidFile);
2158+
fprintf(stderr, "Is another postmaser running?\n");
2159+
return(-1);
2160+
}
2161+
sprintf(pidstr, "%d", pid);
2162+
if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
2163+
fprintf(stderr,"Write to pid file failed\n");
2164+
close(fd);
2165+
unlink(PidFile);
2166+
return(-1);
2167+
}
2168+
close(fd);
2169+
2170+
/*
2171+
* Creating opts file
2172+
*/
2173+
sprintf(optsfile,"%s/%s", datadir, OPTSFNAME);
2174+
fd = open(optsfile, O_RDWR | O_TRUNC | O_CREAT, 0600);
2175+
if (fd < 0) {
2176+
fprintf(stderr, "Can't create optsfile:%s", optsfile);
2177+
unlink(PidFile);
2178+
return(-1);
2179+
}
2180+
sprintf(opts, "%s\n-p %d\n-D %s\n",progname, port, datadir);
2181+
if (assert) {
2182+
sprintf(buf, "-A %d\n", assert);
2183+
strcat(opts, buf);
2184+
}
2185+
2186+
sprintf(buf, "-B %d\n-b %s\n",nbuf, execfile);
2187+
strcat(opts, buf);
2188+
2189+
if (debuglvl) {
2190+
sprintf(buf, "-d %d\n", debuglvl);
2191+
strcat(opts, buf);
2192+
}
2193+
2194+
if (netserver) {
2195+
strcat(opts, "-i\n");
2196+
}
2197+
2198+
#ifdef USE_SSL
2199+
if (securenetserver) {
2200+
strcat(opts, "-l\n");
2201+
}
2202+
#endif
2203+
2204+
sprintf(buf, "-N %d\n", maxbackends);
2205+
strcat(opts, buf);
2206+
2207+
if (!reinit) {
2208+
strcat(opts, "-n\n");
2209+
}
2210+
2211+
if (silent) {
2212+
strcat(opts, "-S\n");
2213+
}
2214+
2215+
if (sendstop) {
2216+
strcat(opts, "-s\n");
2217+
}
2218+
2219+
if (strlen(extraoptions) > 0) {
2220+
strcat(opts, "-o '");
2221+
strcat(opts, extraoptions);
2222+
strcat(opts, "'");
2223+
}
2224+
2225+
if (write(fd, opts, strlen(opts)) != strlen(opts)) {
2226+
perror("Writing to opts file failed");
2227+
unlink(PidFile);
2228+
close(fd);
2229+
return(-1);
2230+
}
2231+
close(fd);
2232+
2233+
/*
2234+
* register clean up proc
2235+
*/
2236+
on_proc_exit(UnlinkPidFile, NULL);
2237+
2238+
return(0);
2239+
}

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