-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_app.c
109 lines (63 loc) · 1.9 KB
/
sw_app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#define LOGDIR "./log"
#define LOGFILE "restart.txt"
char *gettime(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
time_t t = (time_t) tv.tv_sec;
struct tm *ptm = localtime(&t);
static char str[1024];
sprintf(str, "%04d.%02d.%02d %02d:%02d:%02d.%03d ",
ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (int) tv.tv_usec / 1000);
return str;
}
int main(int argc, char **argv) {
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
int ran = random();
mkdir(LOGDIR, 0755);
chdir(LOGDIR);
FILE *fp;
fp = fopen(LOGFILE, "a");
fprintf(fp, "process name: %s\t", argv[0]);
for (int k = 1; k < argc; k++)
fprintf(fp, "%s ", argv[k]);
fprintf(fp, "start time: %s\n", gettime());
fclose(fp);
sleep(ran % 20 + 1);
if (ran % 2) //odd => signal
{
int signum = ((ran / 2) % 15 + 1);
FILE *fp;
fp = fopen(LOGFILE, "a");
fprintf(fp, "process name: %s\t", argv[0]);
for (int k = 1; k < argc; k++)
fprintf(fp, "%s ", argv[k]);
fprintf(fp, "crash time: %s\t", gettime());
fprintf(fp, "SIGNAL(%d).%s\n", signum, strsignal(signum));
fclose(fp);
kill(getpid(), signum);
} else // even -> exit
{
int exitval = (ran / 2) % 256;
FILE *fp;
fp = fopen(LOGFILE, "a");
fprintf(fp, "process name: %s\t", argv[0]);
for (int k = 1; k < argc; k++)
fprintf(fp, "%s ", argv[k]);
fprintf(fp, "crash time: %s\t", gettime());
fprintf(fp, "EXIT(%d)\n", exitval);
fclose(fp);
exit(exitval);
}
}