Skip to content

Commit

Permalink
openrc-shutdown: broadcast: avoid double fclose
Browse files Browse the repository at this point in the history
Initialize fd and tp to known invalid values, and reset them after
calling close/fclose. This is necessary since these are decleared as
static variables.

Note the close will will likely still be called twice: we close fd
before tp to avoid any buffered writes.

Bug: https://bugs.gentoo.org/923326
Signed-off-by: Mike Gilbert <[email protected]>
  • Loading branch information
floppym committed May 23, 2024
1 parent 3d30b6f commit 5d6c5e1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/openrc-shutdown/broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ void broadcast(char *text)
* These are set across the sigsetjmp call, so they can't be stored on
* the stack, otherwise they might be clobbered.
*/
static int fd;
static FILE *tp;
static int fd = -1;
static FILE *tp = NULL;

getuidtty(&user, &tty);

Expand Down Expand Up @@ -193,10 +193,19 @@ void broadcast(char *text)
}
}
alarm(0);
if (fd >= 0)
/*
* It is possible we got here because fputs or fflush got
* interrupted by SIGALARM. Close the file descriptor first to
* avoid blocking in fclose.
*/
if (fd >= 0) {
close(fd);
if (tp != NULL)
fd = -1;
}
if (tp != NULL) {
fclose(tp);
tp = NULL;
}
free(term);
}
endutxent();
Expand Down

0 comments on commit 5d6c5e1

Please sign in to comment.