Aggiornamenti + alcune sockopt di IP
[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                     printf("From pid %i\n", siginf.ssi_pid);
21                     if(siginf.ssi_signo == SIGINT) { // SIGINT stop program
22                         unlink(fifoname);
23                         exit(0);
24                     }
25                 }
26             } else if (events[i].data.fd == fifofd) { // look if fifofd ready 
27                 printf("Message from fifo:\n");
28                 while ((nread = read(fifofd, buffer, 5000))) {
29                     if (nread < 0) {
30                         if (errno != EAGAIN)
31                             die("fifo read error");
32                         else 
33                             printf("end message\n");
34                         break;
35                     }
36                     buffer[nread] = 0;
37                     if (fputs(buffer, stdout) == EOF)
38                         die("Errore in scrittura su terminale");
39                 }
40             } else {   // anything else is an error
41                 printf("epoll activity on unknown %i file descriptor\n", 
42                        epev.data.fd);
43                 exit(-1);
44             }
45         }
46     }