@@ -97,6 +97,7 @@ static bool allow_core_files = false;
97
97
static time_t start_time ;
98
98
99
99
static char postopts_file [MAXPGPATH ];
100
+ static char version_file [MAXPGPATH ];
100
101
static char pid_file [MAXPGPATH ];
101
102
static char backup_file [MAXPGPATH ];
102
103
static char recovery_file [MAXPGPATH ];
@@ -152,7 +153,7 @@ static void pgwin32_doRunAsService(void);
152
153
static int CreateRestrictedProcess (char * cmd , PROCESS_INFORMATION * processInfo , bool as_service );
153
154
#endif
154
155
155
- static pgpid_t get_pgpid (void );
156
+ static pgpid_t get_pgpid (bool is_status_request );
156
157
static char * * readfile (const char * path );
157
158
static void free_readfile (char * * optlines );
158
159
static int start_postmaster (void );
@@ -246,10 +247,34 @@ print_msg(const char *msg)
246
247
}
247
248
248
249
static pgpid_t
249
- get_pgpid (void )
250
+ get_pgpid (bool is_status_request )
250
251
{
251
252
FILE * pidf ;
252
253
long pid ;
254
+ struct stat statbuf ;
255
+
256
+ if (stat (pg_data , & statbuf ) != 0 )
257
+ {
258
+ if (errno == ENOENT )
259
+ printf (_ ("%s: directory \"%s\" does not exist\n" ), progname ,
260
+ pg_data );
261
+ else
262
+ printf (_ ("%s: cannot access directory \"%s\"\n" ), progname ,
263
+ pg_data );
264
+ /*
265
+ * The Linux Standard Base Core Specification 3.1 says this should return
266
+ * '4, program or service status is unknown'
267
+ * https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
268
+ */
269
+ exit (is_status_request ? 4 : 1 );
270
+ }
271
+
272
+ if (stat (version_file , & statbuf ) != 0 && errno == ENOENT )
273
+ {
274
+ printf (_ ("%s: directory \"%s\" is not a database cluster directory\n" ),
275
+ progname , pg_data );
276
+ exit (is_status_request ? 4 : 1 );
277
+ }
253
278
254
279
pidf = fopen (pid_file , "r" );
255
280
if (pidf == NULL )
@@ -810,7 +835,7 @@ do_start(void)
810
835
811
836
if (ctl_command != RESTART_COMMAND )
812
837
{
813
- old_pid = get_pgpid ();
838
+ old_pid = get_pgpid (false );
814
839
if (old_pid != 0 )
815
840
write_stderr (_ ("%s: another server might be running; "
816
841
"trying to start server anyway\n" ),
@@ -894,7 +919,7 @@ do_stop(void)
894
919
pgpid_t pid ;
895
920
struct stat statbuf ;
896
921
897
- pid = get_pgpid ();
922
+ pid = get_pgpid (false );
898
923
899
924
if (pid == 0 ) /* no pid file */
900
925
{
@@ -943,7 +968,7 @@ do_stop(void)
943
968
944
969
for (cnt = 0 ; cnt < wait_seconds ; cnt ++ )
945
970
{
946
- if ((pid = get_pgpid ()) != 0 )
971
+ if ((pid = get_pgpid (false )) != 0 )
947
972
{
948
973
print_msg ("." );
949
974
pg_usleep (1000000 ); /* 1 sec */
@@ -980,7 +1005,7 @@ do_restart(void)
980
1005
pgpid_t pid ;
981
1006
struct stat statbuf ;
982
1007
983
- pid = get_pgpid ();
1008
+ pid = get_pgpid (false );
984
1009
985
1010
if (pid == 0 ) /* no pid file */
986
1011
{
@@ -1033,7 +1058,7 @@ do_restart(void)
1033
1058
1034
1059
for (cnt = 0 ; cnt < wait_seconds ; cnt ++ )
1035
1060
{
1036
- if ((pid = get_pgpid ()) != 0 )
1061
+ if ((pid = get_pgpid (false )) != 0 )
1037
1062
{
1038
1063
print_msg ("." );
1039
1064
pg_usleep (1000000 ); /* 1 sec */
@@ -1071,7 +1096,7 @@ do_reload(void)
1071
1096
{
1072
1097
pgpid_t pid ;
1073
1098
1074
- pid = get_pgpid ();
1099
+ pid = get_pgpid (false );
1075
1100
if (pid == 0 ) /* no pid file */
1076
1101
{
1077
1102
write_stderr (_ ("%s: PID file \"%s\" does not exist\n" ), progname , pid_file );
@@ -1110,7 +1135,7 @@ do_promote(void)
1110
1135
pgpid_t pid ;
1111
1136
struct stat statbuf ;
1112
1137
1113
- pid = get_pgpid ();
1138
+ pid = get_pgpid (false );
1114
1139
1115
1140
if (pid == 0 ) /* no pid file */
1116
1141
{
@@ -1204,7 +1229,7 @@ do_status(void)
1204
1229
{
1205
1230
pgpid_t pid ;
1206
1231
1207
- pid = get_pgpid ();
1232
+ pid = get_pgpid (true );
1208
1233
/* Is there a pid file? */
1209
1234
if (pid != 0 )
1210
1235
{
@@ -1247,7 +1272,7 @@ do_status(void)
1247
1272
1248
1273
/*
1249
1274
* The Linux Standard Base Core Specification 3.1 says this should return
1250
- * '3'
1275
+ * '3, program is not running '
1251
1276
* https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1252
1277
*/
1253
1278
exit (3 );
@@ -2285,6 +2310,7 @@ main(int argc, char **argv)
2285
2310
if (pg_data )
2286
2311
{
2287
2312
snprintf (postopts_file , MAXPGPATH , "%s /postmaster .opts ", pg_data );
2313
+ snprintf (version_file , MAXPGPATH , "%s /PG_VERSION ", pg_data );
2288
2314
snprintf (pid_file , MAXPGPATH , "%s /postmaster .pid ", pg_data );
2289
2315
snprintf (backup_file , MAXPGPATH , "%s /backup_label ", pg_data );
2290
2316
snprintf (recovery_file , MAXPGPATH , "%s /recovery .conf ", pg_data );
0 commit comments