-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmystat.c
More file actions
95 lines (89 loc) · 1.97 KB
/
mystat.c
File metadata and controls
95 lines (89 loc) · 1.97 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
91
92
93
94
95
#include "common.h"
#include "mystat.h"
#include "counter.h"
int main(int argc, const char *argv[])
{
daemonize();
init();
listenpck();
return 0;
}
void daemonize(void)
{
int i, lfp;
char str[10];
if(getppid()==1){
return;
}
i=fork();
if(i<0){
perror("fork");
exit(EXIT_FAILURE);
}
if(i>0){
exit(EXIT_SUCCESS);
}
setsid();
for(i=getdtablesize();i>=0;--i){
close(i);
}
i=open("/dev/null", O_RDWR);
dup(i);
dup(i);
umask(027);
if(chdir(RUNNING_DIR) < 0){
perror("chdir(/)");
snprintf(errorstring, 512, "directory change to / failed.exiting.");
printe(PT_Error);
exit(EXIT_FAILURE);
}
lfp=open(LOCK_FILE, O_RDWR|O_CREAT, 0640);
if(lfp<0){
perror("LOCK_FILE");
snprintf(errorstring, 512, 'LOCK_FILE failed, exiting');
printe(PT_Error);
exit(EXIT_FAILURE);
}
if(lockf(lfp, F_TLOCK, 0)<0){
perror("LOCK_FILE lock");
snprintf(errorstring, 512, "LOCKFILE lock failed ,exiting.");
printe(PT_Error);
exit(EXIT_FAILURE);
}
/*the only instance continues*/
sprintf(str, "%d\n", getpid());
/*record pid to lockfile*/
if(write(lfp, str, strlen(str)) < 0){
perror("write(LOCK_FILE");
snprintf(errorstring, 512, "writing to lockfile failed, exiting");
printe(PT_Error);
exit(EXIT_FAILURE);
}
signal(SIGCHLD, SIG_IGN); /*ignore child*/
signal(SIGTSTP, SIG_IGN); /*Ignore tty signals*/
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGHUP, signal_handler);/*catch hangup signal*/
signal(SIGTERM, signal_handler);/*catch kill signal*/
}
void log_message(char *filename, char *message)
{
FILE *logfile;
logfile=fopen(filename, "a");
if(!logfile)
return;
fprintf(logfile, "%s\n", message);
fclose(logfile);
}
void signal_handler(int sig)
{
switch(sig){
case SIGHUP:
log_message(LOG_FILE, "hangup signal catched");
break;
case SIGTERM:
log_message(LOG_FILE, "terminate signal catched");
exit(0);
break;
}
}