Krolyxon
Commit 51854c6

Branches

Refs →

nsxiv51854c6

Fixed leakage of pipe descriptors in case of failing fork

Author
Bert Münnich
Date
2014-11-27 22:25 +0100
Parent
ed030fe
Commit
51854c614873f59571e80da79f0dc0e8446cbf21

+10 −8

1 file changed

  1. M main.c +10 −8
M main.c View
@@ -262,20 +262,22 @@ void open_info(void)
262262
263263 if (pipe(pfd) < 0)
264264 return;
265− pid = fork();
266− if (pid > 0) {
267− close(pfd[1]);
268− fcntl(pfd[0], F_SETFL, O_NONBLOCK);
269− info.fd = pfd[0];
270− info.i = info.lastsep = 0;
271− info.open = true;
272− } else if (pid == 0) {
265+ if ((pid = fork()) == 0) {
273266 close(pfd[0]);
274267 dup2(pfd[1], 1);
275268 execl(info.cmd, info.cmd, files[fileidx].name, NULL);
276269 warn("could not exec: %s", info.cmd);
277270 exit(EXIT_FAILURE);
278271 }
272+ close(pfd[1]);
273+ if (pid < 0) {
274+ close(pfd[0]);
275+ } else {
276+ fcntl(pfd[0], F_SETFL, O_NONBLOCK);
277+ info.fd = pfd[0];
278+ info.i = info.lastsep = 0;
279+ info.open = true;
280+ }
279281 }
280282
281283 void read_info(void)