From 1e249de228edbe33f890bb7a635aa06bb3ad635c Mon Sep 17 00:00:00 2001 From: Mike Gilbert Date: Tue, 21 May 2024 14:33:45 -0400 Subject: [PATCH] openrc-shutdown: broadcast: avoid double close Initialize fd and tp to known invalid values, and reset them after calling close/fclose. Bug: https://bugs.gentoo.org/923326 Signed-off-by: Mike Gilbert --- src/openrc-shutdown/broadcast.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/openrc-shutdown/broadcast.c b/src/openrc-shutdown/broadcast.c index f21ee95c4..ce9a079de 100644 --- a/src/openrc-shutdown/broadcast.c +++ b/src/openrc-shutdown/broadcast.c @@ -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); @@ -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();