Skip to content

Commit 0463484

Browse files
committed
[Macros] 'close' unnecessary file descriptors in plugin process
Close all the pipe file descriptors in the child process after duping them to the standard I/O. This is not necessary but it's a good thing to do anyway.
1 parent be25ea2 commit 0463484

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/Basic/Program.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ swift::ExecuteWithPipe(llvm::StringRef program,
123123
posix_spawn_file_actions_t FileActions;
124124
posix_spawn_file_actions_init(&FileActions);
125125

126+
// Redirect file descriptors...
126127
posix_spawn_file_actions_adddup2(&FileActions, p1.read, STDIN_FILENO);
127-
posix_spawn_file_actions_addclose(&FileActions, p1.write);
128-
129128
posix_spawn_file_actions_adddup2(&FileActions, p2.write, STDOUT_FILENO);
129+
130+
// Close all file descriptors, not needed as we duped them to the stdio.
131+
posix_spawn_file_actions_addclose(&FileActions, p1.read);
132+
posix_spawn_file_actions_addclose(&FileActions, p1.write);
130133
posix_spawn_file_actions_addclose(&FileActions, p2.read);
134+
posix_spawn_file_actions_addclose(&FileActions, p2.write);
131135

132136
// Spawn the subtask.
133137
int error = posix_spawn(&pid, progCStr, &FileActions, nullptr,
@@ -156,13 +160,16 @@ swift::ExecuteWithPipe(llvm::StringRef program,
156160

157161
// Child process.
158162
case 0:
159-
close(p1.write);
160-
close(p2.read);
161-
162163
// Redirect file descriptors...
163164
dup2(p1.read, STDIN_FILENO);
164165
dup2(p2.write, STDOUT_FILENO);
165166

167+
// Close all file descriptors, not needed as we duped them to the stdio.
168+
close(p1.read);
169+
close(p1.write);
170+
close(p2.read);
171+
close(p2.write);
172+
166173
// Execute the program.
167174
if (envp) {
168175
execve(progCStr, const_cast<char **>(argv), const_cast<char **>(envp));
@@ -180,11 +187,11 @@ swift::ExecuteWithPipe(llvm::StringRef program,
180187

181188
// Parent process.
182189
default:
183-
break;
190+
close(p1.read);
191+
close(p2.write);
184192
}
185193
#endif
186-
close(p1.read);
187-
close(p2.write);
194+
188195
llvm::sys::ProcessInfo proc;
189196
proc.Pid = pid;
190197
proc.Process = pid;

0 commit comments

Comments
 (0)