use of fprintf() is not async-signal safe; call write() instead

This commit is contained in:
Sean McGovern 2022-11-29 01:38:45 -05:00
parent ee35b241ad
commit 8ac1d97556
1 changed files with 25 additions and 1 deletions

26
main.c
View File

@ -14,6 +14,7 @@
#include <errno.h>
#include <sys/errno.h>
#include <assert.h>
#include <syslog.h>
/* I have to redefined this because obsdfreqd is compiled with
-Wall and I don't know of a compiler agnostic way to disable
@ -168,7 +169,30 @@ void set_policy(const char* policy) {
/* restore policy auto upon exit */
void quit_gracefully(int signum) {
fprintf(stderr, "Caught signal %d, set auto policy\n", signum);
char message[48];
strncpy(message, "Caught signal ", 14);
switch(signum) {
case SIGINT:
strncat(message, "SIGINT", 6);
break;
case SIGTERM:
strncat(message, "SIGTERM", 7);
break;
default:
break;
}
strncat(message, ", set auto policy\n", 18);
if (isatty(STDERR_FILENO)) {
write(STDERR_FILENO, message, strlen(message));
} else {
struct syslog_data data = SYSLOG_DATA_INIT;
syslog_r(LOG_DAEMON|LOG_INFO, &data, "%s", message);
}
set_policy("auto");
exit(0);
}