use of fprintf() is not async-signal safe; call write() instead #2
26
main.c
26
main.c
|
@ -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);
|
||||
}
|
||||
|
|
Reference in New Issue