Skip to content

Commit

Permalink
openrc-shutdown: broadcast: simplify non-blocking logic
Browse files Browse the repository at this point in the history
The sigsetjump/siglongjmp logic originates from sysvinit, and was
added before the fork() call was there.

Instead of playing games with signal handlers, just let the kernel kill
the process with SIGALARM if write() blocks for some unknown reason.

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 9236460
Showing 1 changed file with 13 additions and 40 deletions.
53 changes: 13 additions & 40 deletions src/openrc-shutdown/broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <fcntl.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -37,15 +35,6 @@
# define _PATH_DEV "/dev/"
#endif

static sigjmp_buf jbuf;

/*
* Alarm handler
*/
RC_NORETURN static void handler(int arg RC_UNUSED)
{
siglongjmp(jbuf, 1);
}

static void getuidtty(char **userp, char **ttyp)
{
Expand Down Expand Up @@ -123,16 +112,11 @@ void broadcast(char *text)
char *date;
char *p;
char *line = NULL;
struct sigaction sa;
int flags;
char *term = NULL;
struct utmpx *utmp;
/*
* 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;
int fd;
FILE *tp;

getuidtty(&user, &tty);

Expand All @@ -159,11 +143,6 @@ void broadcast(char *text)
if (fork() != 0)
return;

memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);

setutxent();

while ((utmp = getutxent()) != NULL) {
Expand All @@ -178,25 +157,19 @@ void broadcast(char *text)
continue;
}

/*
* Open it non-delay
*/
if (sigsetjmp(jbuf, 1) == 0) {
alarm(2);
flags = O_WRONLY|O_NDELAY|O_NOCTTY;
if (file_isatty(term) && (fd = open(term, flags)) >= 0) {
if (isatty(fd) && (tp = fdopen(fd, "w")) != NULL) {
fputs(line, tp);
fputs(text, tp);
fflush(tp);
}
/* Request non-blocking I/O */
flags = O_WRONLY|O_NONBLOCK|O_NOCTTY;
if (file_isatty(term) && (fd = open(term, flags)) >= 0) {
if (isatty(fd) && (tp = fdopen(fd, "w")) != NULL) {
alarm(2); /* Let the kernel kill us if write() blocks */
fputs(line, tp);
fputs(text, tp);
fclose(tp);
alarm(0);
}
else
close(fd);
}
alarm(0);
if (fd >= 0)
close(fd);
if (tp != NULL)
fclose(tp);
free(term);
}
endutxent();
Expand Down

0 comments on commit 9236460

Please sign in to comment.