53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#include "sigcat.h"
|
|
|
|
void setup_default_handlers(void) {
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = default_signal_handler;
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
for (int i = SIGMIN; i <= SIGMAX; i++) {
|
|
// Cannot process SIGKILL or SIGSTOP
|
|
if (i == SIGKILL || i == SIGSTOP) {
|
|
continue;
|
|
}
|
|
|
|
sigaction(i, &sa, 0);
|
|
}
|
|
}
|
|
|
|
void default_signal_handler(int s) {
|
|
fprintf(outputStream, "sigcat received %s\n", strsignal(s));
|
|
fflush(outputStream);
|
|
|
|
if (s == SIGUSR1 || s == SIGUSR2) {
|
|
usr_signal_handler(s);
|
|
}
|
|
}
|
|
|
|
void usr_signal_handler(int s) {
|
|
if (s == SIGUSR1) {
|
|
outputStream = stdout;
|
|
} else if (s == SIGUSR2) {
|
|
outputStream = stderr;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
// Defined in header
|
|
outputStream = stdout;
|
|
|
|
setup_default_handlers();
|
|
|
|
// Forever loop input until recieve EOF
|
|
char *line;
|
|
while ((line = read_line(stdin)) != NULL) {
|
|
strcat(line, "\n");
|
|
fprintf(outputStream, line);
|
|
fflush(outputStream);
|
|
free(line);
|
|
}
|
|
|
|
return 0;
|
|
}
|