JobCreatePipe: do fcntl() *after* any fiddling of fd's

to avoid leaking descriptors.
Job_ServerStart: set closed on exec for jp_0 and jp_1.
This commit is contained in:
sjg 2013-06-05 03:59:43 +00:00
parent b01ef1be52
commit c2bb7dcfaf
1 changed files with 14 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: job.c,v 1.172 2013/03/05 22:01:43 christos Exp $ */
/* $NetBSD: job.c,v 1.173 2013/06/05 03:59:43 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -70,14 +70,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: job.c,v 1.172 2013/03/05 22:01:43 christos Exp $";
static char rcsid[] = "$NetBSD: job.c,v 1.173 2013/06/05 03:59:43 sjg Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: job.c,v 1.172 2013/03/05 22:01:43 christos Exp $");
__RCSID("$NetBSD: job.c,v 1.173 2013/06/05 03:59:43 sjg Exp $");
#endif
#endif /* not lint */
#endif
@ -401,6 +401,15 @@ JobCreatePipe(Job *job, int minfd)
if (pipe(job->jobPipe) == -1)
Punt("Cannot create pipe: %s", strerror(errno));
for (i = 0; i < 2; i++) {
/* Avoid using low numbered fds */
fd = fcntl(job->jobPipe[i], F_DUPFD, minfd);
if (fd != -1) {
close(job->jobPipe[i]);
job->jobPipe[i] = fd;
}
}
/* Set close-on-exec flag for both */
(void)fcntl(job->jobPipe[0], F_SETFD, 1);
(void)fcntl(job->jobPipe[1], F_SETFD, 1);
@ -413,15 +422,6 @@ JobCreatePipe(Job *job, int minfd)
*/
fcntl(job->jobPipe[0], F_SETFL,
fcntl(job->jobPipe[0], F_GETFL, 0) | O_NONBLOCK);
for (i = 0; i < 2; i++) {
/* Avoid using low numbered fds */
fd = fcntl(job->jobPipe[i], F_DUPFD, minfd);
if (fd != -1) {
close(job->jobPipe[i]);
job->jobPipe[i] = fd;
}
}
}
/*-
@ -2812,6 +2812,8 @@ Job_ServerStart(int max_tokens, int jp_0, int jp_1)
/* Pipe passed in from parent */
tokenWaitJob.inPipe = jp_0;
tokenWaitJob.outPipe = jp_1;
(void)fcntl(jp_0, F_SETFD, 1);
(void)fcntl(jp_1, F_SETFD, 1);
return;
}