-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeamon_read.c
63 lines (59 loc) · 972 Bytes
/
deamon_read.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
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<syslog.h>
#include<string.h>
#define BUFFER_SIZE 2048
int main(int argc, int **argv)
{
pid_t pid, sid;
int i = 0;
long timestamp;
char buf[BUFFER_SIZE];
if(argc < 2)
{
printf("Usage: %s <filename>\n", argv[0]);
exit(1);
}
pid = fork();
if(pid < 0)
{
exit(EXIT_FAILURE);
}
if(pid > 0)
{
exit(EXIT_SUCCESS);
}
umask(0);
/* open a log here */
sid = setsid();
if(sid < 0)
{
exit(EXIT_FAILURE);
}
if((chdir("/opt/c-language/")) < 0)
{
exit(EXIT_FAILURE);
}
while(1)
{
FILE *fin = fopen((char*)argv[1], "r");
if(!fin)
{
printf("child process cannot open file\n");
exit(1);
}
buf[0] = '0';
fread(buf, sizeof(char), BUFFER_SIZE, fin);
fclose(fin);
timestamp = time(NULL);
// printf("%s\n", ctime(×tamp));
printf("%s\n", buf);
sleep(5);
}
exit(EXIT_SUCCESS);
}