Skip to content

Commit 0451859

Browse files
macdiceThomas Munro
andcommitted
Clean up test_cloexec.c and Makefile.
An unused variable caused a compiler warning on BF animal fairywren, an snprintf() call was redundant, and some buffer sizes were inconsistent. Per code review from Tom Lane. The Makefile's test ifeq ($(PORTNAME), win32) never succeeded due to a circularity, so only Meson builds were actually compiling the new test code, partially explaining why CI didn't tell us about the warning sooner (the other problem being that CompilerWarnings only makes world-bin, a problem for another commit). Simplify. Backpatch-through: 16, like commit c507ba5 Author: Bryan Green <dbryan.green@gmail.com> Co-authored-by: Thomas Munro <tmunro@gmail.com> Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/1086088.1765593851%40sss.pgh.pa.us
1 parent 699293d commit 0451859

File tree

2 files changed

+23
-54
lines changed

2 files changed

+23
-54
lines changed

src/test/modules/test_cloexec/Makefile

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
# src/test/modules/test_cloexec/Makefile
22

3-
# This module is for Windows only
4-
ifeq ($(PORTNAME),win32)
5-
6-
MODULE_big = test_cloexec
7-
OBJS = \
8-
$(WIN32RES) \
9-
test_cloexec.o
10-
113
PGFILEDESC = "test_cloexec - test O_CLOEXEC flag handling"
4+
PGAPPICON = win32
125

13-
# Build as a standalone program, not a shared library
14-
PROGRAM = test_cloexec
15-
override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
6+
PROGRAM += test_cloexec
7+
OBJS += $(WIN32RES) test_cloexec.o
168

9+
NO_INSTALLCHECK = 1
1710
TAP_TESTS = 1
1811

19-
endif
20-
2112
ifdef USE_PGXS
2213
PG_CONFIG = pg_config
2314
PGXS := $(shell $(PG_CONFIG) --pgxs)

src/test/modules/test_cloexec/test_cloexec.c

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@
2424
#include "common/file_utils.h"
2525
#include "port.h"
2626

27+
#ifdef WIN32
2728
static void run_parent_tests(const char *testfile1, const char *testfile2);
2829
static void run_child_tests(const char *handle1_str, const char *handle2_str);
2930
static bool try_write_to_handle(HANDLE h, const char *label);
31+
#endif
3032

3133
int
3234
main(int argc, char *argv[])
3335
{
34-
char testfile1[MAXPGPATH];
35-
char testfile2[MAXPGPATH];
36-
3736
/* Windows-only test */
3837
#ifndef WIN32
3938
fprintf(stderr, "This test only runs on Windows\n");
4039
return 0;
41-
#endif
40+
#else
41+
char testfile1[MAXPGPATH];
42+
char testfile2[MAXPGPATH];
4243

4344
if (argc == 3)
4445
{
@@ -68,36 +69,34 @@ main(int argc, char *argv[])
6869
fprintf(stderr, "Usage: %s [handle1_hex handle2_hex]\n", argv[0]);
6970
return 1;
7071
}
72+
#endif
7173
}
7274

75+
#ifdef WIN32
7376
static void
7477
run_parent_tests(const char *testfile1, const char *testfile2)
7578
{
76-
#ifdef WIN32
7779
int fd1,
7880
fd2;
7981
HANDLE h1,
8082
h2;
81-
char cmdline[1024];
82-
STARTUPINFO si;
83-
PROCESS_INFORMATION pi;
83+
char exe_path[MAXPGPATH];
84+
char cmdline[MAXPGPATH + 100];
85+
STARTUPINFO si = {.cb = sizeof(si)};
86+
PROCESS_INFORMATION pi = {0};
8487
DWORD exit_code;
8588

8689
printf("Parent: Opening test files...\n");
8790

88-
/*
89-
* Open first file WITH O_CLOEXEC - should NOT be inherited
90-
*/
91+
/* Open first file WITH O_CLOEXEC - should NOT be inherited */
9192
fd1 = open(testfile1, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
9293
if (fd1 < 0)
9394
{
9495
fprintf(stderr, "Failed to open %s: %s\n", testfile1, strerror(errno));
9596
exit(1);
9697
}
9798

98-
/*
99-
* Open second file WITHOUT O_CLOEXEC - should be inherited
100-
*/
99+
/* Open second file WITHOUT O_CLOEXEC - should be inherited */
101100
fd2 = open(testfile2, O_RDWR | O_CREAT | O_TRUNC, 0600);
102101
if (fd2 < 0)
103102
{
@@ -121,29 +120,12 @@ run_parent_tests(const char *testfile1, const char *testfile2)
121120
printf("Parent: fd1=%d (O_CLOEXEC) -> HANDLE=%p\n", fd1, h1);
122121
printf("Parent: fd2=%d (no O_CLOEXEC) -> HANDLE=%p\n", fd2, h2);
123122

124-
/*
125-
* Spawn child process with bInheritHandles=TRUE, passing handle values as
126-
* hex strings
127-
*/
128-
snprintf(cmdline, sizeof(cmdline), "\"%s\" %p %p",
129-
GetCommandLine(), h1, h2);
130-
131123
/*
132124
* Find the actual executable path by removing any arguments from
133-
* GetCommandLine().
125+
* GetCommandLine(), and add our new arguments.
134126
*/
135-
{
136-
char exe_path[MAX_PATH];
137-
char *space_pos;
138-
139-
GetModuleFileName(NULL, exe_path, sizeof(exe_path));
140-
snprintf(cmdline, sizeof(cmdline), "\"%s\" %p %p",
141-
exe_path, h1, h2);
142-
}
143-
144-
memset(&si, 0, sizeof(si));
145-
si.cb = sizeof(si);
146-
memset(&pi, 0, sizeof(pi));
127+
GetModuleFileName(NULL, exe_path, sizeof(exe_path));
128+
snprintf(cmdline, sizeof(cmdline), "\"%s\" %p %p", exe_path, h1, h2);
147129

148130
printf("Parent: Spawning child process...\n");
149131
printf("Parent: Command line: %s\n", cmdline);
@@ -180,19 +162,19 @@ run_parent_tests(const char *testfile1, const char *testfile2)
180162
printf("Parent: Child exit code: %lu\n", exit_code);
181163

182164
if (exit_code == 0)
165+
{
183166
printf("Parent: SUCCESS - O_CLOEXEC behavior verified\n");
167+
}
184168
else
185169
{
186170
printf("Parent: FAILURE - O_CLOEXEC not working correctly\n");
187171
exit(1);
188172
}
189-
#endif
190173
}
191174

192175
static void
193176
run_child_tests(const char *handle1_str, const char *handle2_str)
194177
{
195-
#ifdef WIN32
196178
HANDLE h1,
197179
h2;
198180
bool h1_worked,
@@ -232,13 +214,11 @@ run_child_tests(const char *handle1_str, const char *handle2_str)
232214
printf("Child: Test FAILED - O_CLOEXEC not working correctly\n");
233215
exit(1);
234216
}
235-
#endif
236217
}
237218

238219
static bool
239220
try_write_to_handle(HANDLE h, const char *label)
240221
{
241-
#ifdef WIN32
242222
const char *test_data = "test\n";
243223
DWORD bytes_written;
244224
BOOL result;
@@ -256,7 +236,5 @@ try_write_to_handle(HANDLE h, const char *label)
256236
label, GetLastError());
257237
return false;
258238
}
259-
#else
260-
return false;
261-
#endif
262239
}
240+
#endif

0 commit comments

Comments
 (0)