-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.c
More file actions
90 lines (80 loc) · 1.28 KB
/
logger.c
File metadata and controls
90 lines (80 loc) · 1.28 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
#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>
static void
printdate(char *buf)
{
time_t t;
char *ct;
int size_time;
t = time(&t);
ct = ctime(&t);
size_time = strlen(ct);
ct[size_time-1] = 0;
printf("%s: %s", ct, buf);
fflush(stdin);
}
static void
tout(int no)
{
char *msg = "time out, no events\n";
printdate(msg);
signal(SIGALRM, tout);
siginterrupt(SIGALRM, 0);
alarm(5);
}
static void
readfifo(int fd)
{
char buf[1024];
int nr;
for(;;){
signal(SIGALRM, tout);
siginterrupt(SIGALRM, 0);
alarm(5);
nr = read(fd, buf, sizeof buf - 1);
if(nr < 0){
err(1, "read");
}else if(nr == 0){
break;
}
buf[nr] = 0;
alarm(0);
printdate(buf);
if(strcmp(buf, "bye\n") == 0){
close(fd);
exit(0);
}
}
}
int
main(int argc, char* argv[])
{
int fd;
if(!access("/tmp/logger", F_OK)){
if(unlink("/tmp/logger") < 0){
err(1,"unlink");
}
}
if(mkfifo("/tmp/logger", 0664) < 0)
err(1, "mkfifo");
for(;;){
printf("waiting for clients\n");
fd = open("/tmp/logger", O_RDONLY);
if(fd < 0) {
err(1, "open");
}
printf("ready to read events\n");
readfifo(fd);
alarm(0);
close(fd);
}
exit(0);
}