75{
76#ifdef WIN32
77 int fd1,
78 fd2;
79 HANDLE h1,
80 h2;
81 char cmdline[1024];
82 STARTUPINFO si;
83 PROCESS_INFORMATION pi;
84 DWORD exit_code;
85
86 printf(
"Parent: Opening test files...\n");
87
88
89
90
91 fd1 = open(testfile1, O_RDWR | O_CREAT | O_TRUNC |
O_CLOEXEC, 0600);
92 if (fd1 < 0)
93 {
95 exit(1);
96 }
97
98
99
100
101 fd2 = open(testfile2, O_RDWR | O_CREAT | O_TRUNC, 0600);
102 if (fd2 < 0)
103 {
104 fprintf(stderr,
"Failed to open %s: %s\n", testfile2,
strerror(errno));
106 exit(1);
107 }
108
109
110 h1 = (HANDLE) _get_osfhandle(fd1);
111 h2 = (HANDLE) _get_osfhandle(fd2);
112
113 if (h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
114 {
115 fprintf(stderr,
"Failed to get OS handles\n");
118 exit(1);
119 }
120
121 printf(
"Parent: fd1=%d (O_CLOEXEC) -> HANDLE=%p\n", fd1, h1);
122 printf(
"Parent: fd2=%d (no O_CLOEXEC) -> HANDLE=%p\n", fd2, h2);
123
124
125
126
127
128 snprintf(cmdline,
sizeof(cmdline),
"\"%s\" %p %p",
129 GetCommandLine(), h1, h2);
130
131
132
133
134
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));
147
148 printf(
"Parent: Spawning child process...\n");
149 printf(
"Parent: Command line: %s\n", cmdline);
150
151 if (!CreateProcess(NULL,
152 cmdline,
153 NULL,
154 NULL,
155 TRUE,
156 0,
157 NULL,
158 NULL,
159 &si,
160 &pi))
161 {
162 fprintf(stderr,
"CreateProcess failed: %lu\n", GetLastError());
165 exit(1);
166 }
167
168 printf(
"Parent: Waiting for child process...\n");
169
170
171 WaitForSingleObject(pi.hProcess, INFINITE);
172 GetExitCodeProcess(pi.hProcess, &exit_code);
173
174 CloseHandle(pi.hProcess);
175 CloseHandle(pi.hThread);
176
179
180 printf(
"Parent: Child exit code: %lu\n", exit_code);
181
182 if (exit_code == 0)
183 printf(
"Parent: SUCCESS - O_CLOEXEC behavior verified\n");
184 else
185 {
186 printf(
"Parent: FAILURE - O_CLOEXEC not working correctly\n");
187 exit(1);
188 }
189#endif
190}