1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*-------------------------------------------------------------------------
*
* utils.c
*
* Utility module of Postgres-XC configuration and operation tool.
*
* Copyright (c) 2013 Postgres-XC Development Group
*
*-------------------------------------------------------------------------
*/
/*
* Variable useful tools/small routines.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include "../../src/interfaces/libpq/libpq-fe.h"
#include "utils.h"
#include "pgxc_ctl.h"
#include "pgxc_ctl_log.h"
#include "do_shell.h"
#include "config.h"
#include "variables.h"
#include "varnames.h"
#include "c.h"
static int Malloc_ed = 0;
static int Strdup_ed = 0;
static int Freed = 0;
void *Malloc(size_t size)
{
void *rv = malloc(size);
Malloc_ed++;
if (rv == NULL)
{
elog(PANIC, "PANIC: No more memory. See core file for details.\n");
abort();
}
return(rv);
}
char **addToList(char **List, char *val)
{
char **rv;
int ii;
for (ii = 0; List[ii]; ii++);
rv = Realloc(List, sizeof(char *) * ii);
rv[ii - 1] = NULL;
return rv;
}
void *Malloc0(size_t size)
{
void *rv = malloc(size);
Malloc_ed++;
if (rv == NULL)
{
elog(PANIC, "PANIC: No more memory. See core file for details.\n");
abort();
}
memset(rv, 0, size);
return(rv);
}
void *Realloc(void *ptr, size_t size)
{
void *rv = realloc(ptr, size);
if (rv == NULL)
{
elog(PANIC, "PANIC: No more memory. See core file for details.\n");
abort();
}
return(rv);
}
void Free(void *ptr)
{
Freed++;
if (ptr)
free(ptr);
}
/*
* If flag is TRUE and chdir fails, then exit(1)
*/
int Chdir(char *path, int flag)
{
if (chdir(path))
{
elog(ERROR, "ERROR: Could not change work directory to \"%s\". %s%s\n",
path,
flag == TRUE ? "Exiting. " : "",
strerror(errno));
if (flag == TRUE)
exit(1);
else
return -1;
}
return 0;
}
FILE *Fopen(char *path, char *mode)
{
FILE *rv;
if ((rv = fopen(path, mode)) == NULL)
elog(ERROR, "ERROR: Could not open the file \"%s\" in \"%s\", %s\n", path, mode, strerror(errno));
return(rv);
}
char *Strdup(const char *s)
{
char *rv;
Strdup_ed++;
rv = strdup(s);
if (rv == NULL)
{
elog(PANIC, "PANIC: No more memory. See core file for details.\n");
abort();
}
return(rv);
}
void appendFiles(FILE *f, char **fileList)
{
FILE *src;
int ii;
char buf[MAXLINE+1];
if (fileList)
for (ii = 0; fileList[ii]; ii++)
{
if (!is_none(fileList[ii]))
{
if ((src = fopen(fileList[ii], "r")) == 0)
{
elog(ERROR, "ERROR: could not open file %s for read, %s\n", fileList[ii], strerror(errno));
continue;
}
while (fgets(buf, MAXLINE, src))
fputs(buf, f);
fclose(src);
}
}
}
FILE *prepareLocalStdin(char *buf, int len, char **fileList)
{
FILE *f;
if ((f = fopen(createLocalFileName(STDIN, buf, len), "w")) == NULL)
{
elog(ERROR, "ERROR: could not open file %s for write, %s\n", buf, strerror(errno));
return(NULL);
}
appendFiles(f, fileList);
return(f);
}
char *timeStampString(char *buf, int len)
{
time_t nowTime;
struct tm nowTm;
nowTime = time(NULL);
localtime_r(&nowTime, &nowTm);
snprintf(buf, len, "%04d%02d%02d_%02d:%02d:%02d",
nowTm.tm_year+1900, nowTm.tm_mon+1, nowTm.tm_mday,
nowTm.tm_hour, nowTm.tm_min, nowTm.tm_sec);
return(buf);
}
char **makeActualNodeList(char **nodeList)
{
char **actualNodeList;
int ii, jj;
for (ii = 0, jj = 0; nodeList[ii]; ii++)
{
if (!is_none(nodeList[ii]))
jj++;
}
actualNodeList = Malloc0(sizeof(char *) * (jj + 1));
for (ii = 0, jj = 0; nodeList[ii]; ii++)
{
if (!is_none(nodeList[ii]))
{
actualNodeList[jj] = Strdup(nodeList[ii]);
jj++;
}
}
return actualNodeList;
}
int gtmProxyIdx(char *gtmProxyName)
{
int ii;
for (ii = 0; aval(VAR_gtmProxyNames)[ii]; ii++)
{
if (strcmp(aval(VAR_gtmProxyNames)[ii], gtmProxyName) == 0)
return ii;
}
return -1;
}
int coordIdx(char *coordName)
{
int ii;
if (is_none(coordName))
return -1;
for (ii = 0; aval(VAR_coordNames)[ii]; ii++)
{
if (strcmp(aval(VAR_coordNames)[ii], coordName) == 0)
return ii;
}
return -1;
}
int datanodeIdx(char *datanodeName)
{
int ii;
if (is_none(datanodeName))
return -1;
for (ii = 0; aval(VAR_datanodeNames)[ii]; ii++)
{
if (strcmp(aval(VAR_datanodeNames)[ii], datanodeName) == 0)
return ii;
}
return -1;
}
int getEffectiveGtmProxyIdxFromServerName(char *serverName)
{
int ii;
if (serverName == NULL)
return (-1);
for (ii = 0; aval(VAR_gtmProxyNames)[ii]; ii++)
{
if (strcmp(aval(VAR_gtmProxyServers)[ii], serverName) == 0)
return ii;
}
return -1;
}
/*
* 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 *pidfile, char *dir)
{
char cmd[MAXLINE+1];
char pid_s[MAXLINE+1];
FILE *wkf;
snprintf(cmd, MAXLINE,
"ssh %s@%s "
"\"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 server process, host \"%s\" dir \"%s\", %s\n",
host, dir, strerror(errno));
return(-1);
}
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);
return(atoi(pid_s));
}
int pingNode(char *host, char *port)
{
PGPing status;
char conninfo[MAXLINE+1];
char editBuf[MAXPATH+1];
conninfo[0] = 0;
if (host)
{
snprintf(editBuf, MAXPATH, "host = '%s' ", host);
strncat(conninfo, editBuf, MAXLINE);
}
if (port)
{
snprintf(editBuf, MAXPATH, "port = %d ", atoi(port));
strncat(conninfo, editBuf, MAXLINE);
}
strncat(conninfo, "dbname = postgres ", MAXLINE);
if (conninfo[0])
{
status = PQping(conninfo);
if (status == PQPING_OK)
return 0;
else
return 1;
}
else
return -1;
}
/*
* A different mechanism to ping datanode and coordinator slaves since these
* nodes currently do not accept connections and hence won't respond to PQping
* requests. Instead we rely on "pg_ctl status", which must be run via ssh on
* the remote machine
*/
int pingNodeSlave(char *host, char *datadir)
{
FILE *wkf;
char cmd[MAXLINE+1];
char line[MAXLINE+1];
int rv;
snprintf(cmd, MAXLINE, "ssh %s@%s pg_ctl -D %s status > /dev/null 2>&1; echo $?",
sval(VAR_pgxcUser), host, datadir);
wkf = popen(cmd, "r");
if (wkf == NULL)
return -1;
if (fgets(line, MAXLINE, wkf))
{
trimNl(line);
rv = atoi(line);
}
else
rv = -1;
pclose(wkf);
return rv;
}
void trimNl(char *s)
{
for (;*s && *s != '\n'; s++);
*s = 0;
}
char *getChPidList(char *host, pid_t ppid)
{
FILE *wkf;
char cmd[MAXLINE+1];
char line[MAXLINE+1];
char *rv = Malloc(MAXLINE+1);
rv[0] = 0;
snprintf(cmd, MAXLINE, "ssh %s@%s pgrep -P %d",
sval(VAR_pgxcUser), host, ppid);
wkf = popen(cmd, "r");
if (wkf == NULL)
return NULL;
while (fgets(line, MAXLINE, wkf))
{
trimNl(line);
strncat(rv, line, MAXLINE);
strncat(rv, " ", MAXLINE);
}
pclose(wkf);
return rv;
}
char *getIpAddress(char *hostName)
{
char command[MAXLINE+1];
char *ipAddr;
FILE *f;
snprintf(command, MAXLINE, "ping -c1 %s | head -n 1 | sed 's/^[^(]*(\\([^)]*\\).*$/\\1/'", hostName);
if ((f = popen(command, "r")) == NULL)
{
elog(ERROR, "ERROR: could not open the command, \"%s\", %s\n", command, strerror(errno));
return NULL;
}
ipAddr = Malloc(MAXTOKEN+1);
fgets(ipAddr, MAXTOKEN, f);
pclose(f);
trimNl(ipAddr);
return ipAddr;
}
/*
* Test to see if a directory exists and is empty or not.
*
* Returns:
* 0 if nonexistent
* 1 if exists and empty
* 2 if exists and not empty
* -1 if trouble accessing directory (errno reflects the error)
*/
int
pgxc_check_dir(const char *dir)
{
int result = 1;
DIR *chkdir;
struct dirent *file;
errno = 0;
chkdir = opendir(dir);
if (chkdir == NULL)
return (errno == ENOENT) ? 0 : -1;
while ((file = readdir(chkdir)) != NULL)
{
if (strcmp(".", file->d_name) == 0 ||
strcmp("..", file->d_name) == 0)
{
/* skip this and parent directory */
continue;
}
else
{
result = 2; /* not empty */
break;
}
}
#ifdef WIN32
/*
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
closedir(chkdir);
if (errno != 0)
result = -1; /* some kind of I/O error? */
return result;
}
|