Set f_file to -1/F_UNUSED when after closing when possible

This will help ensure we don't trash file descriptors that get used later on
in the daemon

Found via internal Coverity scan

MFC after: 2 weeks
Discussed with: cem, ed, markj
Differential Revision: https://reviews.freebsd.org/D3081
Submitted by: Miles Ohlrich <miles.ohlrich@isilon.com>
Sponsored by: EMC / Isilon Storage Division
This commit is contained in:
ngie 2015-08-05 03:17:06 +00:00
parent b62835a630
commit 15c1eedd90

View File

@ -349,6 +349,18 @@ static int waitdaemon(int, int, int);
static void timedout(int); static void timedout(int);
static void increase_rcvbuf(int); static void increase_rcvbuf(int);
static void
close_filed(struct filed *f)
{
if (f == NULL || f->f_file == -1)
return;
(void)close(f->f_file);
f->f_file = -1;
f->f_type = F_UNUSED;
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -1024,7 +1036,8 @@ logmsg(int pri, const char *msg, const char *from, int flags)
(void)strlcpy(f->f_lasttime, timestamp, (void)strlcpy(f->f_lasttime, timestamp,
sizeof(f->f_lasttime)); sizeof(f->f_lasttime));
fprintlog(f, flags, msg); fprintlog(f, flags, msg);
(void)close(f->f_file); close(f->f_file);
f->f_file = -1;
} }
(void)sigsetmask(omask); (void)sigsetmask(omask);
return; return;
@ -1313,8 +1326,7 @@ fprintlog(struct filed *f, int flags, const char *msg)
*/ */
if (errno != ENOSPC) { if (errno != ENOSPC) {
int e = errno; int e = errno;
(void)close(f->f_file); close_filed(f);
f->f_type = F_UNUSED;
errno = e; errno = e;
logerror(f->f_un.f_fname); logerror(f->f_un.f_fname);
} }
@ -1338,7 +1350,7 @@ fprintlog(struct filed *f, int flags, const char *msg)
} }
if (writev(f->f_file, iov, IOV_SIZE) < 0) { if (writev(f->f_file, iov, IOV_SIZE) < 0) {
int e = errno; int e = errno;
(void)close(f->f_file); close_filed(f);
if (f->f_un.f_pipe.f_pid > 0) if (f->f_un.f_pipe.f_pid > 0)
deadq_enter(f->f_un.f_pipe.f_pid, deadq_enter(f->f_un.f_pipe.f_pid,
f->f_un.f_pipe.f_pname); f->f_un.f_pipe.f_pname);
@ -1446,7 +1458,7 @@ reapchild(int signo __unused)
for (f = Files; f; f = f->f_next) for (f = Files; f; f = f->f_next)
if (f->f_type == F_PIPE && if (f->f_type == F_PIPE &&
f->f_un.f_pipe.f_pid == pid) { f->f_un.f_pipe.f_pid == pid) {
(void)close(f->f_file); close_filed(f);
f->f_un.f_pipe.f_pid = 0; f->f_un.f_pipe.f_pid = 0;
log_deadchild(pid, status, log_deadchild(pid, status,
f->f_un.f_pipe.f_pname); f->f_un.f_pipe.f_pname);
@ -1550,7 +1562,7 @@ die(int signo)
if (f->f_prevcount) if (f->f_prevcount)
fprintlog(f, 0, (char *)NULL); fprintlog(f, 0, (char *)NULL);
if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) { if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) {
(void)close(f->f_file); close_filed(f);
f->f_un.f_pipe.f_pid = 0; f->f_un.f_pipe.f_pid = 0;
} }
} }
@ -1634,11 +1646,11 @@ init(int signo)
case F_FORW: case F_FORW:
case F_CONSOLE: case F_CONSOLE:
case F_TTY: case F_TTY:
(void)close(f->f_file); close_filed(f);
break; break;
case F_PIPE: case F_PIPE:
if (f->f_un.f_pipe.f_pid > 0) { if (f->f_un.f_pipe.f_pid > 0) {
(void)close(f->f_file); close_filed(f);
deadq_enter(f->f_un.f_pipe.f_pid, deadq_enter(f->f_un.f_pipe.f_pid,
f->f_un.f_pipe.f_pname); f->f_un.f_pipe.f_pname);
} }