-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-13syslog.c
57 lines (51 loc) · 968 Bytes
/
3-13syslog.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <syslog.h>
int main(int argc, const char *argv[])
{
pid_t pid, sid;
int i, fd;
char *buf = "This is a Daemon\n";
pid = fork();
if(pid < 0){
printf("fork error\n");
return -1;
}
else if(pid > 0){
exit(0);
}
else{
openlog("daemon_syslog", LOG_PID, LOG_DAEMON);
if((sid = setsid()) < 0){
syslog(LOG_ERR, "%s\n", "setsid");
exit(1);
}
if((sid = chdir("/")) < 0){
syslog(LOG_ERR, "%s\n", "chdir");
exit(1);
}
umask(0);
for(i = 0; i < getdtablesize(); i++){
close(i);
}
if((fd = open("/tmp/daemon.log",
O_CREAT|O_WRONLY|O_TRUNC, 0600)) < 0){
syslog(LOG_ERR, "open");
exit(1);
}
syslog(LOG_INFO, "%s\n", "open daemon.log");
while(1){
write(fd, buf, strlen(buf));
sleep(5);
}
close(fd);
closelog();
}
return 0;
}