Skip to content

Commit cae096b

Browse files
committed
Improve closing files using pclose() which are opened by popen().
Pavan Deolasee provided the fix.
1 parent f42d65e commit cae096b

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

contrib/pgxc_ctl/bash_handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ read_config_file(char *path, char *conf)
7676
sprintf(cmd, "bash %s/pgxc_ctl_bash print_values", path);
7777
vars = popen(cmd, "r");
7878
read_vars(vars);
79-
fclose(vars);
79+
pclose(vars);
8080
}

contrib/pgxc_ctl/coord_cmd.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ add_coordinatorMaster(char *name, char *host, int port, int pooler, char *dir)
980980
"# End of Additon\n",
981981
timeStampString(date, MAXTOKEN+1),
982982
port, pooler, gtmHost, atoi(gtmPort));
983-
fclose(f);
983+
pclose(f);
984984
}
985985
CleanArray(confFiles);
986986
jj = coordIdx(name);
@@ -996,7 +996,7 @@ add_coordinatorMaster(char *name, char *host, int port, int pooler, char *dir)
996996
sval(VAR_pgxcOwner), aval(VAR_coordPgHbaEntries)[kk]);
997997
}
998998
fprintf(f, "# End of addition\n");
999-
fclose(f);
999+
pclose(f);
10001000
}
10011001

10021002
/* Lock ddl */
@@ -1040,20 +1040,20 @@ add_coordinatorMaster(char *name, char *host, int port, int pooler, char *dir)
10401040
}
10411041
fprintf(f, "CREATE NODE %s WITH (TYPE = 'coordinator', host='%s', PORT=%d);\n", name, host, port);
10421042
fprintf(f, "\\q\n");
1043-
fclose(f);
1043+
pclose(f);
10441044
}
10451045
}
10461046
/* Quit DDL lokkup session */
10471047
fprintf(lockf, "\\q\n");
1048-
fclose(lockf);
1048+
pclose(lockf);
10491049
if ((f = pgxc_popen_wRaw("psql -h %s -p %d %s", host, port, sval(VAR_defaultDatabase))) == NULL)
10501050
elog(ERROR, "ERROR: cannot connect to the coordinator master %s.\n", name);
10511051
else
10521052
{
10531053
fprintf(f, "ALTER NODE %s WITH (host='%s', PORT=%d);\n", name, host, port);
10541054
fprintf(f, "select pgxc_pool_reload();\n");
10551055
fprintf(f, "\\q\n");
1056-
fclose(f);
1056+
pclose(f);
10571057
}
10581058
return 0;
10591059
}
@@ -1124,7 +1124,7 @@ add_coordinatorSlave(char *name, char *host, char *dir, char *archDir)
11241124
timeStampString(date, MAXPATH),
11251125
sval(VAR_pgxcUser), host, archDir,
11261126
getDefaultWalSender(TRUE));
1127-
fclose(f);
1127+
pclose(f);
11281128
/* pg_hba.conf for replication */
11291129
if ((f = pgxc_popen_w(aval(VAR_coordMasterServers)[idx], "cat >> %s/pg_hba.conf", aval(VAR_coordMasterDirs)[idx])) == NULL)
11301130
{
@@ -1139,7 +1139,7 @@ add_coordinatorSlave(char *name, char *host, char *dir, char *archDir)
11391139
"# End of addition ===============================\n",
11401140
timeStampString(date, MAXPATH),
11411141
sval(VAR_pgxcOwner), getIpAddress(host));
1142-
fclose(f);
1142+
pclose(f);
11431143
/* Reconfigure pgxc_ctl configuration with the new slave */
11441144
/* Need an API to expand the array to desired size */
11451145
if ((extendVar(VAR_coordSlaveServers, idx, "none") != 0) ||
@@ -1257,7 +1257,7 @@ add_coordinatorSlave(char *name, char *host, char *dir, char *archDir)
12571257
/* max_wal_senders to be taken here. */
12581258
"# End of Addition\n",
12591259
timeStampString(date, MAXTOKEN), atoi(aval(VAR_coordPorts)[idx]));
1260-
fclose(f);
1260+
pclose(f);
12611261
/* Update the slave recovery.conf */
12621262
if ((f = pgxc_popen_w(host, "cat >> %s/recovery.conf", dir)) == NULL)
12631263
{
@@ -1276,7 +1276,7 @@ add_coordinatorSlave(char *name, char *host, char *dir, char *archDir)
12761276
timeStampString(date, MAXTOKEN), aval(VAR_coordMasterServers)[idx], aval(VAR_coordPorts)[idx],
12771277
sval(VAR_pgxcOwner), aval(VAR_coordNames)[idx],
12781278
aval(VAR_coordArchLogDirs)[idx], aval(VAR_coordArchLogDirs)[idx]);
1279-
fclose(f);
1279+
pclose(f);
12801280

12811281
/* Start the slave */
12821282
doImmediate(host, NULL, "pg_ctl start -Z coordinator -D %s -w", dir);
@@ -1472,7 +1472,7 @@ remove_coordinatorSlave(char *name, int clean_opt)
14721472
"archive_command = ''\n"
14731473
"# End of the update\n",
14741474
timeStampString(date, MAXTOKEN));
1475-
fclose(f);
1475+
pclose(f);
14761476
}
14771477
doImmediate(aval(VAR_coordMasterServers)[idx], NULL, "pg_ctl restart -Z coordinator -D %s", aval(VAR_coordMasterDirs)[idx]);
14781478
if (clean_opt)
@@ -1918,7 +1918,7 @@ failover_oneCoordinator(int coordIdx)
19181918
"# End of addition\n",
19191919
timeStampString(timestamp, MAXTOKEN),
19201920
gtmHost, gtmPort);
1921-
fclose(f);
1921+
pclose(f);
19221922

19231923
/* Restart coord Slave Server */
19241924
rc_local = doImmediate(aval(VAR_coordSlaveServers)[coordIdx], NULL,
@@ -2000,7 +2000,7 @@ failover_oneCoordinator(int coordIdx)
20002000
aval(VAR_coordNames)[coordIdx],
20012001
aval(VAR_coordNames)[coordIdx], aval(VAR_coordMasterServers)[coordIdx], aval(VAR_coordPorts)[coordIdx]);
20022002
#endif
2003-
fclose(f);
2003+
pclose(f);
20042004
}
20052005
}
20062006
/* Now update myself */
@@ -2018,7 +2018,7 @@ failover_oneCoordinator(int coordIdx)
20182018
"select pgxc_pool_reload();\n"
20192019
"\\q\n",
20202020
aval(VAR_coordNames)[coordIdx], aval(VAR_coordMasterServers)[coordIdx], aval(VAR_coordPorts)[coordIdx]);
2021-
fclose(f);
2021+
pclose(f);
20222022
return(rc);
20232023

20242024
# undef checkRc

contrib/pgxc_ctl/datanode_cmd.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ failover_oneDatanode(int datanodeIdx)
763763
"# End of addition\n",
764764
timeStampString(timestamp, MAXTOKEN),
765765
gtmHost, gtmPort);
766-
fclose(f);
766+
pclose(f);
767767

768768
/* Restart datanode slave (as the new master) */
769769
rc_local = doImmediate(aval(VAR_datanodeSlaveServers)[datanodeIdx], NULL,
@@ -845,7 +845,7 @@ failover_oneDatanode(int datanodeIdx)
845845
aval(VAR_datanodeNames)[datanodeIdx],
846846
#endif
847847
aval(VAR_datanodeNames)[datanodeIdx], aval(VAR_datanodeMasterServers)[datanodeIdx], aval(VAR_datanodePorts)[datanodeIdx]);
848-
fclose(f);
848+
pclose(f);
849849
}
850850
return rc;
851851

@@ -985,7 +985,7 @@ add_datanodeMaster(char *name, char *host, int port, char *dir)
985985
"# End of Additon\n",
986986
timeStampString(date, MAXTOKEN+1),
987987
port, gtmHost, gtmPort);
988-
fclose(f);
988+
pclose(f);
989989
}
990990
CleanArray(confFiles);
991991
jj = datanodeIdx(name);
@@ -1001,7 +1001,7 @@ add_datanodeMaster(char *name, char *host, int port, char *dir)
10011001
sval(VAR_pgxcOwner), aval(VAR_datanodePgHbaEntries)[kk]);
10021002
}
10031003
fprintf(f, "# End of addition\n");
1004-
fclose(f);
1004+
pclose(f);
10051005
}
10061006

10071007
/* Lock ddl */
@@ -1045,13 +1045,13 @@ add_datanodeMaster(char *name, char *host, int port, char *dir)
10451045
}
10461046
fprintf(f, "CREATE NODE %s WITH (TYPE = 'datanode', host='%s', PORT=%d);\n", name, host, port);
10471047
fprintf(f, "\\q\n");
1048-
fclose(f);
1048+
pclose(f);
10491049
}
10501050
}
10511051

10521052
/* Quit DDL lokkup session */
10531053
fprintf(lockf, "\\q\n");
1054-
fclose(lockf);
1054+
pclose(lockf);
10551055
return 0;
10561056

10571057
}
@@ -1123,7 +1123,7 @@ add_datanodeSlave(char *name, char *host, char *dir, char *archDir)
11231123
timeStampString(date, MAXPATH),
11241124
sval(VAR_pgxcUser), host, archDir,
11251125
getDefaultWalSender(FALSE));
1126-
fclose(f);
1126+
pclose(f);
11271127
/* pg_hba.conf for replication */
11281128
if ((f = pgxc_popen_w(aval(VAR_datanodeMasterServers)[idx], "cat >> %s/pg_hba.conf", aval(VAR_datanodeMasterDirs)[idx])) == NULL)
11291129
{
@@ -1138,7 +1138,7 @@ add_datanodeSlave(char *name, char *host, char *dir, char *archDir)
11381138
"# End of addition ===============================\n",
11391139
timeStampString(date, MAXPATH),
11401140
sval(VAR_pgxcOwner), getIpAddress(host));
1141-
fclose(f);
1141+
pclose(f);
11421142
/* Reconfigure pgxc_ctl configuration with the new slave */
11431143
#if 0
11441144
/* Need an API to expand the array to desired size */
@@ -1208,7 +1208,7 @@ add_datanodeSlave(char *name, char *host, char *dir, char *archDir)
12081208
"archive_command = ''\n" /* No archive mode */
12091209
"# End of Addition\n",
12101210
timeStampString(date, MAXTOKEN), aval(VAR_datanodePorts)[idx]);
1211-
fclose(f);
1211+
pclose(f);
12121212
/* Update the slave recovery.conf */
12131213
if ((f = pgxc_popen_w(host, "cat >> %s/recovery.conf", dir)) == NULL)
12141214
{
@@ -1227,7 +1227,7 @@ add_datanodeSlave(char *name, char *host, char *dir, char *archDir)
12271227
timeStampString(date, MAXTOKEN), aval(VAR_datanodeMasterServers)[idx], aval(VAR_datanodePorts)[idx],
12281228
sval(VAR_pgxcOwner), aval(VAR_datanodeNames)[idx],
12291229
aval(VAR_datanodeArchLogDirs)[idx], aval(VAR_datanodeArchLogDirs)[idx]);
1230-
fclose(f);
1230+
pclose(f);
12311231
/* Start the slave */
12321232
doImmediate(host, NULL, "pg_ctl start -Z datanode -D %s -w", dir);
12331233
return 0;
@@ -1333,7 +1333,7 @@ remove_datanodeMaster(char *name, int clean_opt)
13331333
}
13341334
fprintf(f, "DROP NODE %s;\n", name);
13351335
fprintf(f, "\\q");
1336-
fclose(f);
1336+
pclose(f);
13371337
}
13381338
}
13391339
#if 1
@@ -1428,7 +1428,7 @@ remove_datanodeSlave(char *name, int clean_opt)
14281428
"archive_command = ''\n"
14291429
"# End of the update\n",
14301430
timeStampString(date, MAXTOKEN));
1431-
fclose(f);
1431+
pclose(f);
14321432
}
14331433
doImmediate(aval(VAR_datanodeMasterServers)[idx], NULL, "pg_ctl restart -Z datanode -D %s", aval(VAR_datanodeMasterDirs)[idx]);
14341434

contrib/pgxc_ctl/pgxc_ctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ read_configuration(void)
259259
return;
260260
}
261261
read_vars(conf);
262-
fclose(conf);
262+
pclose(conf);
263263
uninstall_pgxc_ctl_bash(pgxc_ctl_bash_path);
264264
elog(INFO, "Finished to read configuration.\n");
265265
}

contrib/pgxc_ctl/utils.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ getChPidList(char *host, pid_t ppid)
386386
strncat(rv, line, MAXLINE);
387387
strncat(rv, " ", MAXLINE);
388388
}
389+
pclose(wkf);
389390
return rv;
390391
}
391392

@@ -405,7 +406,7 @@ getIpAddress(char *hostName)
405406
ipAddr = Malloc(MAXTOKEN+1);
406407
if (fgets(ipAddr, MAXTOKEN, f) != ipAddr)
407408
ipAddr[0] = '\0';
408-
fclose(f);
409+
pclose(f);
409410
trimNl(ipAddr);
410411
return ipAddr;
411412
}

0 commit comments

Comments
 (0)