10
10
*
11
11
*
12
12
* 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 $
14
14
*
15
15
* NOTES
16
16
*
92
92
#include "utils/trace.h"
93
93
#include "version.h"
94
94
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
+
95
112
#if !defined(MAXINT )
96
113
#define MAXINT INT_MAX
97
114
#endif
@@ -261,6 +278,15 @@ static void RandomSalt(char *salt);
261
278
static void SignalChildren (SIGNAL_ARGS );
262
279
static int CountChildren (void );
263
280
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
+
264
290
extern int BootstrapMain (int argc ,char * argv []);
265
291
static pid_t SSDataBase (bool startup );
266
292
#define StartupDataBase ()SSDataBase(true)
@@ -272,11 +298,11 @@ static void InitSSL(void);
272
298
273
299
#ifdef CYR_RECODE
274
300
void GetCharSetByHost (char * ,int ,char * );
275
-
276
301
#endif
277
302
278
303
#ifdef USE_ASSERT_CHECKING
279
- int assert_enabled = 1 ;
304
+
305
+ int assert_enabled = 1 ;
280
306
281
307
#endif
282
308
@@ -351,6 +377,9 @@ PostmasterMain(int argc, char *argv[])
351
377
bool DataDirOK ;/* We have a usable PGDATA value */
352
378
char hostbuf [MAXHOSTNAMELEN ];
353
379
int nonblank_argc ;
380
+ char original_extraoptions [MAXPGPATH ];
381
+
382
+ * original_extraoptions = '\0' ;
354
383
355
384
/*
356
385
* We need three params so we can display status. If we don't get
@@ -509,6 +538,7 @@ PostmasterMain(int argc, char *argv[])
509
538
*/
510
539
strcat (ExtraOptions ," " );
511
540
strcat (ExtraOptions ,optarg );
541
+ strcpy (original_extraoptions ,optarg );
512
542
break ;
513
543
case 'p' :
514
544
/* Set PGPORT by hand. */
@@ -618,6 +648,33 @@ PostmasterMain(int argc, char *argv[])
618
648
if (silentflag )
619
649
pmdaemonize ();
620
650
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
+
621
678
/*
622
679
* Set up signal handlers for the postmaster process.
623
680
*/
@@ -2068,3 +2125,115 @@ SSDataBase(bool startup)
2068
2125
2069
2126
return (pid );
2070
2127
}
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
+ }