Sistemato programma FifoReporter.c
[gapil.git] / listati / FifoReporter-main.c
1     /* Main body: wait something to report */
2     while (1) {
3         if ((n=epoll_wait(epfd, events, MAX_EPOLL_EV, -1)) < 0) 
4             die("error on epoll_wait");
5         for (i=0; i<n; i++) {    // loop on ready file descriptors             
6             if (events[i].data.fd == sigfd) {  // look if signalfd ready 
7                 printf("Signal received:\n");
8                 while(nread=read(sigfd, &siginf, sizeof(siginf))) {
9                     if (nread < 0) {
10                         if (errno != EAGAIN) 
11                             die("signalfd read error");
12                         else 
13                             break;
14                     }
15                     if (nread != sizeof(siginf)) {
16                         printf("Error on signal data read, '\n");
17                         continue;
18                     }
19                     printf("Got %s\n", sig_names[siginf.ssi_signo]);
20                     if(siginf.ssi_signo == SIGINT) { // SIGINT stop program
21                         unlink(fifoname);
22                         exit(0);
23                     }
24                 }
25             } else if (events[i].data.fd == fifofd) { // look if fifofd ready 
26                 printf("Message from fifo:\n");
27                 while ((nread = read(fifofd, buffer, 5000))) {
28                     if (nread < 0) {
29                         if (errno != EAGAIN)
30                             die("fifo read error");
31                         else 
32                             printf("end message\n");
33                         break;
34                     }
35                     buffer[nread] = 0;
36                     if (fputs(buffer, stdout) == EOF)
37                         die("Errore in scrittura su terminale");
38                 }
39             } else {   // anything else is an error
40                 printf("epoll activity on unknown %i file descriptor\n", 
41                        epev.data.fd);
42                 exit(-1);
43             }
44         }
45     }