X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;f=sources%2FFifoReporter.c;h=0ce8c37df0313cb39d07267077e424558c7adee1;hb=265547995607b3ec2c04f9b8b035b416e892920b;hp=631c56e459c5ad437f1bb2493519a8b3c70dd03c;hpb=9562f11ab8163e83cbd02c6dffa342629e230ca5;p=gapil.git diff --git a/sources/FifoReporter.c b/sources/FifoReporter.c index 631c56e..0ce8c37 100644 --- a/sources/FifoReporter.c +++ b/sources/FifoReporter.c @@ -41,24 +41,63 @@ #include /* signal constants, types and functions */ #include /* file control functions */ #include /* Linux epoll interface */ +#include /* Linux signalfd interface */ #include "macros.h" #include "Gapil.h" /* Subroutines declaration */ void usage(void); +void die(char *); /* default name for the input fifo */ char *fifoname = "/tmp/reporter.fifo"; +#define MAX_EPOLL_EV 10 + int main(int argc, char *argv[]) { /* Variables definition */ - int i, t = 1; + int i, n, nread, t = 10; char buffer[4096]; - int fifofd, epfd; - struct epoll_event epev; - int nread; + int fifofd, epfd, sigfd; + sigset_t sigmask; + struct epoll_event epev, events[MAX_EPOLL_EV]; + struct signalfd_siginfo siginf; + char *sig_names[] = { // signal name declaration from bits/signum.h + "------- ", /*0 Filler for signal names array */ + "SIGHUP ", /*1 Hangup (POSIX). */ + "SIGINT ", /*2 Interrupt (ANSI). */ + "SIGQUIT ", /*3 Quit (POSIX). */ + "SIGILL ", /*4 Illegal instruction (ANSI). */ + "SIGTRAP ", /*5 Trace trap (POSIX). */ + "SIGABRT ", /*6 Abort (ANSI). */ + "SIGBUS ", /*7 BUS error (4.2 BSD). */ + "SIGFPE ", /*8 Floating-point exception (ANSI). */ + "SIGKILL ", /*9 Kill, unblockable (POSIX). */ + "SIGUSR1 ", /*10 User-defined signal 1 (POSIX). */ + "SIGSEGV ", /*11 Segmentation violation (ANSI). */ + "SIGUSR2 ", /*12 User-defined signal 2 (POSIX). */ + "SIGPIPE ", /*13 Broken pipe (POSIX). */ + "SIGALRM ", /*14 Alarm clock (POSIX). */ + "SIGTERM ", /*15 Termination (ANSI). */ + "SIGSTKFLT ", /*16 Stack fault. */ + "SIGCHLD ", /*17 Child status has changed (POSIX). */ + "SIGCONT ", /*18 Continue (POSIX). */ + "SIGSTOP ", /*19 Stop, unblockable (POSIX). */ + "SIGTSTP ", /*20 Keyboard stop (POSIX). */ + "SIGTTIN ", /*21 Background read from tty (POSIX). */ + "SIGTTOU ", /*22 Background write to tty (POSIX). */ + "SIGURG ", /*23 Urgent condition on socket (4.2 BSD). */ + "SIGXCPU ", /*24 CPU limit exceeded (4.2 BSD). */ + "SIGXFSZ ", /*25 File size limit exceeded (4.2 BSD). */ + "SIGVTALRM ", /*26 Virtual alarm clock (4.2 BSD). */ + "SIGPROF ", /*27 Profiling alarm clock (4.2 BSD). */ + "SIGWINCH ", /*28 Window size change (4.3 BSD, Sun). */ + "SIGIO ", /*29 I/O now possible (4.2 BSD). */ + "SIGPWR ", /*30 Power failure restart (System V). */ + "SIGSYS " /*31 Bad system call. */ + }; /* * Input section: decode parameters passed in the calling * Use getopt function @@ -97,24 +136,85 @@ int main(int argc, char *argv[]) /* * Initial setup */ + if ((epfd=epoll_create(5)) < 0) // epoll init + die("Failing on epoll_create"); + /* Signal setup for signalfd and epoll use */ + sigemptyset(&sigmask); + sigaddset(&sigmask, SIGINT); + sigaddset(&sigmask, SIGQUIT); + sigaddset(&sigmask, SIGTERM); + if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) // block signals + die("Failing in signalfd"); + if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) // take a signalfd + die("Failing in signalfd"); + epev.data.fd = sigfd; // add fd to epoll + epev.events = EPOLLIN; + if (epoll_ctl(epfd, EPOLL_CTL_ADD, sigfd, &epev)) + die("Failing in signal epoll_ctl"); + /* Fifo setup for epoll use */ if (mkfifo(fifoname, 0622)) { // create well known fifo if does't exist - if (errno!=EEXIST) { - perror("Cannot create well known fifo"); - exit(1); - } - } - if ((fifo = open(fifoname, O_RDONLY)) < 0) { // open fifo - perror("Cannot open read only well known fifo"); - exit(1); - } - epfd = epoll_create(5); // initialize epoll - if (epfd < 0) { - perror("Failing on epoll_create"); - exit(1); + if (errno!=EEXIST) + die("Cannot create well known fifo"); } - /* Main body: loop over requests */ + if ((fifofd = open(fifoname, O_RDWR|O_NONBLOCK)) < 0) // open fifo + die("Cannot open well known fifo"); + epev.data.fd = fifofd; // add fd to epoll + epev.events = EPOLLIN; + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fifofd, &epev)) + die("Failing in fifo epoll_ctl"); + /* + * Main body: wait something to report + */ + printf("FifoReporter starting, pid %i\n", getpid()); while (1) { - + if ((n=epoll_wait(epfd, events, MAX_EPOLL_EV, -1)) < 0) + die("error on epoll_wait"); + debug("Got %i events\n", n); + /* loop on epoll events */ + for (i=0; i