Module Name: src Committed By: riastradh Date: Sun Apr 16 20:57:14 UTC 2017
Modified Files: src/usr.bin/make: job.c Log Message: Check fcntl return values out of paranoia. CID 975277 CID 975278 To generate a diff of this commit: cvs rdiff -u -r1.188 -r1.189 src/usr.bin/make/job.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/job.c diff -u src/usr.bin/make/job.c:1.188 src/usr.bin/make/job.c:1.189 --- src/usr.bin/make/job.c:1.188 Fri Aug 26 23:28:39 2016 +++ src/usr.bin/make/job.c Sun Apr 16 20:57:14 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: job.c,v 1.188 2016/08/26 23:28:39 dholland Exp $ */ +/* $NetBSD: job.c,v 1.189 2017/04/16 20:57:14 riastradh 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.188 2016/08/26 23:28:39 dholland Exp $"; +static char rcsid[] = "$NetBSD: job.c,v 1.189 2017/04/16 20:57:14 riastradh 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.188 2016/08/26 23:28:39 dholland Exp $"); +__RCSID("$NetBSD: job.c,v 1.189 2017/04/16 20:57:14 riastradh Exp $"); #endif #endif /* not lint */ #endif @@ -411,7 +411,7 @@ static void JobSigUnlock(sigset_t *omask static void JobCreatePipe(Job *job, int minfd) { - int i, fd; + int i, fd, flags; if (pipe(job->jobPipe) == -1) Punt("Cannot create pipe: %s", strerror(errno)); @@ -426,8 +426,10 @@ JobCreatePipe(Job *job, int minfd) } /* Set close-on-exec flag for both */ - (void)fcntl(job->jobPipe[0], F_SETFD, FD_CLOEXEC); - (void)fcntl(job->jobPipe[1], F_SETFD, FD_CLOEXEC); + if (fcntl(job->jobPipe[0], F_SETFD, FD_CLOEXEC) == -1) + Punt("Cannot set close-on-exec: %s", strerror(errno)); + if (fcntl(job->jobPipe[1], F_SETFD, FD_CLOEXEC) == -1) + Punt("Cannot set close-on-exec: %s", strerror(errno)); /* * We mark the input side of the pipe non-blocking; we poll(2) the @@ -435,8 +437,12 @@ JobCreatePipe(Job *job, int minfd) * race for the token when a new one becomes available, so the read * from the pipe should not block. */ - fcntl(job->jobPipe[0], F_SETFL, - fcntl(job->jobPipe[0], F_GETFL, 0) | O_NONBLOCK); + flags = fcntl(job->jobPipe[0], F_GETFL, 0); + if (flags == -1) + Punt("Cannot get flags: %s", strerror(errno)); + flags |= O_NONBLOCK; + if (fcntl(job->jobPipe[0], F_SETFL, flags) == -1) + Punt("Cannot set flags: %s", strerror(errno)); } /*- @@ -1365,15 +1371,27 @@ JobExec(Job *job, char **argv) execError("dup2", "job->cmdFILE"); _exit(1); } - (void)fcntl(0, F_SETFD, 0); - (void)lseek(0, (off_t)0, SEEK_SET); + if (fcntl(0, F_SETFD, 0) == -1) { + execError("fcntl clear close-on-exec", "stdin"); + _exit(1); + } + if (lseek(0, (off_t)0, SEEK_SET) == -1) { + execError("lseek to 0", "stdin"); + _exit(1); + } if (job->node->type & (OP_MAKE | OP_SUBMAKE)) { /* * Pass job token pipe to submakes. */ - fcntl(tokenWaitJob.inPipe, F_SETFD, 0); - fcntl(tokenWaitJob.outPipe, F_SETFD, 0); + if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1) { + execError("clear close-on-exec", "tokenWaitJob.inPipe"); + _exit(1); + } + if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1) { + execError("clear close-on-exec", "tokenWaitJob.outPipe"); + _exit(1); + } } /* @@ -1390,7 +1408,10 @@ JobExec(Job *job, char **argv) * it before routing the shell's error output to the same place as * its standard output. */ - (void)fcntl(1, F_SETFD, 0); + if (fcntl(1, F_SETFD, 0) == -1) { + execError("clear close-on-exec", "stdout"); + _exit(1); + } if (dup2(1, 2) == -1) { execError("dup2", "1, 2"); _exit(1);