summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavan Deolasee2014-07-24 05:33:09 +0000
committerPavan Deolasee2014-09-01 14:20:29 +0000
commitc22e7ae42377440d12d8eae7ba3c622b89738776 (patch)
tree1cbbc53ae158adc353fe01c6f94d7dc7166aed6b
parentd6c2db5819ca3f9185eb6b5359a8025c7439eb32 (diff)
Fetch server PID from the .pid file
Earlier we were relying on "ps" command to find the PID. But this is not reliable because 1) there could be multiple instances of the server processes running on a server and 2) the command itself is not portable across all platforms with options varying on each
-rw-r--r--contrib/pgxc_ctl/utils.c43
-rw-r--r--contrib/pgxc_ctl/utils.h4
2 files changed, 18 insertions, 29 deletions
diff --git a/contrib/pgxc_ctl/utils.c b/contrib/pgxc_ctl/utils.c
index 18ebef03a6..279ecca5c6 100644
--- a/contrib/pgxc_ctl/utils.c
+++ b/contrib/pgxc_ctl/utils.c
@@ -258,49 +258,38 @@ int getEffectiveGtmProxyIdxFromServerName(char *serverName)
return -1;
}
-
-
/*
- * Please note that this function deeply depend upon
- * the environment.
- *
- * It works find with CentOS/Ubuntu/ReadHat Linux but
- * may need another tweak for other operation systems
- * such as Solaris, FreeBSD, MacOS.
+ * We rely on the PID file created in Postgres/GTM et al data directory to
+ * fetch the PID. The first line in these respective files has the PID.
*/
-pid_t get_prog_pid(char *host, char *progname, char *dir)
+pid_t get_prog_pid(char *host, char *pidfile, char *dir)
{
char cmd[MAXLINE+1];
char pid_s[MAXLINE+1];
- int ii;
FILE *wkf;
- char *token;
- char *line;
snprintf(cmd, MAXLINE,
"ssh %s@%s "
- "\"ps -f -C %s | grep %s\"",
- sval(VAR_pgxcUser), host, progname, dir);
+ "\"cat %s/%s.pid\"",
+ sval(VAR_pgxcUser), host, dir, pidfile);
wkf = popen(cmd, "r");
if (wkf == NULL)
{
- elog(ERROR, "ERROR: cannot obtain pid value of the remote postmaster, host \"%s\" dir \"%s\", %s\n",
+ elog(ERROR, "ERROR: cannot obtain pid value of the remote server process, host \"%s\" dir \"%s\", %s\n",
host, dir, strerror(errno));
return(-1);
}
- fgets(pid_s, MAXLINE, wkf);
+
+ if (fgets(pid_s, MAXLINE, wkf) == NULL)
+ {
+ elog(ERROR, "ERROR: fgets failed to get pid of remote server process, host \"%s\" dir \"%s\", %d\n",
+ host, dir, ferror(wkf));
+ pclose(wkf);
+ return(-1);
+ }
+
pclose(wkf);
- /* Get the second token */
- line = pid_s;
- if ((line = get_word(line, &token)) == NULL)
- return 0;
- get_word(line, &token);
- if (token == NULL)
- return 0;
- for (ii = 0; token[ii]; ii++)
- if (token[ii] < '0' || token[ii] > '9')
- return 0;
- return(atoi(token));
+ return(atoi(pid_s));
}
int pingNode(char *host, char *port)
diff --git a/contrib/pgxc_ctl/utils.h b/contrib/pgxc_ctl/utils.h
index 0f3089354f..976f001c56 100644
--- a/contrib/pgxc_ctl/utils.h
+++ b/contrib/pgxc_ctl/utils.h
@@ -27,13 +27,13 @@ extern int gtmProxyIdx(char *gtmProxyName);
extern int coordIdx(char *coordName);
extern int datanodeIdx(char *datanodeName);
extern int getEffectiveGtmProxyIdxFromServerName(char *serverName);
-extern pid_t get_prog_pid(char *host, char *progname, char *dir);
+extern pid_t get_prog_pid(char *host, char *pidfile, char *dir);
extern int pingNode(char *host, char *port);
extern void trimNl(char *s);
extern char *getChPidList(char *host, pid_t ppid);
extern char *getIpAddress(char *hostName);
-#define get_postmaster_pid(host, dir) get_prog_pid(host, "postgres", dir)
+#define get_postmaster_pid(host, dir) get_prog_pid(host, "postmaster", dir)
#define get_gtm_pid(host, dir) get_prog_pid(host, "gtm", dir)
#define get_gtmProxy_pid(host, dir) get_prog_pid(host, "gtm_proxy", dir)
#define freeAndReset(x) do{Free(x);(x)=NULL;}while(0)