#include "common/file_utils.h"
#include "port.h"
+#ifdef WIN32
static void run_parent_tests(const char *testfile1, const char *testfile2);
static void run_child_tests(const char *handle1_str, const char *handle2_str);
static bool try_write_to_handle(HANDLE h, const char *label);
+#endif
int
main(int argc, char *argv[])
{
- char testfile1[MAXPGPATH];
- char testfile2[MAXPGPATH];
-
/* Windows-only test */
#ifndef WIN32
fprintf(stderr, "This test only runs on Windows\n");
return 0;
-#endif
+#else
+ char testfile1[MAXPGPATH];
+ char testfile2[MAXPGPATH];
if (argc == 3)
{
fprintf(stderr, "Usage: %s [handle1_hex handle2_hex]\n", argv[0]);
return 1;
}
+#endif
}
+#ifdef WIN32
static void
run_parent_tests(const char *testfile1, const char *testfile2)
{
-#ifdef WIN32
int fd1,
fd2;
HANDLE h1,
h2;
- char cmdline[1024];
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
+ char exe_path[MAXPGPATH];
+ char cmdline[MAXPGPATH + 100];
+ STARTUPINFO si = {.cb = sizeof(si)};
+ PROCESS_INFORMATION pi = {0};
DWORD exit_code;
printf("Parent: Opening test files...\n");
- /*
- * Open first file WITH O_CLOEXEC - should NOT be inherited
- */
+ /* Open first file WITH O_CLOEXEC - should NOT be inherited */
fd1 = open(testfile1, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
if (fd1 < 0)
{
exit(1);
}
- /*
- * Open second file WITHOUT O_CLOEXEC - should be inherited
- */
+ /* Open second file WITHOUT O_CLOEXEC - should be inherited */
fd2 = open(testfile2, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd2 < 0)
{
printf("Parent: fd1=%d (O_CLOEXEC) -> HANDLE=%p\n", fd1, h1);
printf("Parent: fd2=%d (no O_CLOEXEC) -> HANDLE=%p\n", fd2, h2);
- /*
- * Spawn child process with bInheritHandles=TRUE, passing handle values as
- * hex strings
- */
- snprintf(cmdline, sizeof(cmdline), "\"%s\" %p %p",
- GetCommandLine(), h1, h2);
-
/*
* Find the actual executable path by removing any arguments from
- * GetCommandLine().
+ * GetCommandLine(), and add our new arguments.
*/
- {
- char exe_path[MAX_PATH];
- char *space_pos;
-
- GetModuleFileName(NULL, exe_path, sizeof(exe_path));
- snprintf(cmdline, sizeof(cmdline), "\"%s\" %p %p",
- exe_path, h1, h2);
- }
-
- memset(&si, 0, sizeof(si));
- si.cb = sizeof(si);
- memset(&pi, 0, sizeof(pi));
+ GetModuleFileName(NULL, exe_path, sizeof(exe_path));
+ snprintf(cmdline, sizeof(cmdline), "\"%s\" %p %p", exe_path, h1, h2);
printf("Parent: Spawning child process...\n");
printf("Parent: Command line: %s\n", cmdline);
printf("Parent: Child exit code: %lu\n", exit_code);
if (exit_code == 0)
+ {
printf("Parent: SUCCESS - O_CLOEXEC behavior verified\n");
+ }
else
{
printf("Parent: FAILURE - O_CLOEXEC not working correctly\n");
exit(1);
}
-#endif
}
static void
run_child_tests(const char *handle1_str, const char *handle2_str)
{
-#ifdef WIN32
HANDLE h1,
h2;
bool h1_worked,
printf("Child: Test FAILED - O_CLOEXEC not working correctly\n");
exit(1);
}
-#endif
}
static bool
try_write_to_handle(HANDLE h, const char *label)
{
-#ifdef WIN32
const char *test_data = "test\n";
DWORD bytes_written;
BOOL result;
label, GetLastError());
return false;
}
-#else
- return false;
-#endif
}
+#endif