-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
77 lines (67 loc) · 1.34 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
gcc -c -Wall -Wshadow logger.c && gcc -o logger logger.o && ./logger
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <signal.h>
#define fifo "/tmp/fifo"
#define timeout 5
char*
date(){
time_t ttime =time(0);
char* c_time;
/* Convert to local time format. */
c_time = ctime(&ttime);
if (c_time != NULL){
return (c_time);/* Print to stdout. */
}
}
static void
hndlr(int no){
alarm(5);
fprintf(stderr,"%s",date());
fprintf(stderr, "time out,no events\n");
}
int
main(int argc,char* argv[]){
char buf[1024];
int fd, nr;
/* Remove any previous FIFO*/
unlink(fifo);
if(mkfifo(fifo, 0666) < 0){// rw rw --
err(1, "mkfifo");
}else{
fprintf(stderr,"%s",date());
fprintf(stderr, "waiting for a client...\n");
}
fd = open(fifo, O_RDONLY);
if(fd < 0) {
err(1, "open");
}
if(fd>0){
fprintf(stderr,"%s",date());
fprintf(stderr, "ready to read events...\n");
}
signal(SIGALRM, hndlr);//para el handler
alarm(5);
while(1){
while ((nr = read(fd, buf, sizeof(buf))) > 0){
alarm(5);
if(strcmp(buf,"end\n") == 0){
write(2, buf, nr);
close(fd);
exit(0);
}
fprintf(stderr,"%s",date());
write(2, buf, nr);
}
}
exit(0);
}