-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.c
executable file
·166 lines (147 loc) · 3.8 KB
/
temp.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <cutils/sockets.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <ctype.h>
#include <termios.h>
#include <unistd.h>
#include <sys/resource.h>
void socket_test();
void coredump_test();
static void handle_signal();
static void SIGCHLD_handler(int k);
void register_epoll_handler(int fd, void (*fn)());
static int signal_write_fd = -1;
static int signal_read_fd = -1;
static int epoll_fd = -1;
static int quit = 1;
int main(int argc, char ** argv )
{
int i = 1;
struct rlimit rl;
rl.rlim_cur = RLIM_INFINITY;
rl.rlim_max= RLIM_INFINITY;
if(setrlimit(RLIMIT_CORE, &rl)== -1){
printf("set RLIMIT failed\n");
}else
printf("set RLIMIT successful \n");
for(i; i< argc; i++)
{
if(!strcmp(argv[i], "-s"))
{
socket_test();
return 1;
}else if(!strcmp(argv[i], "-f")){
fork_test();
return 1;
}else if(!strcmp(argv[i], "-c")){
coredump_test();
return 1;
}else if(!strcmp(argv[i], "-n"))
fork_nu_test();
return 1;
}
//printf("I'm in main %d\n", getpid());
return 1;
}
void fork_test()
{
int count = 1;
printf("before fork, PID is %d\n", getpid());
int result = fork();
if(result < 0)
printf("fork error");
else if(result==0)
{
count ++ ;
printf("in the child process, PID is %d count is %d\n", getpid(), count);
}
else if(result > 0)
{
count ++ ;
printf("in the parent process PID is %d result is %d count is %d \n", getpid(),result, count);
}
}
void fork_nu_test()
{
//int count = 1;
fork();
fork();
fork();
fork();
sleep(10);
printf("fork, PPID is %d PID is %d\n", getppid(), getpid());
sleep(3);
exit(1);
}
void socket_test()
{
int s[2];
char buf[32];
int signal;
epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if(socketpair(AF_UNIX, SOCK_STREAM,0, s)==-1){
printf("socket pair failed\n");
exit(1);
}
signal_write_fd = s[0];
signal_read_fd = s[1];
// Write to signal_write_fd if we catch SIGCHLD.
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = SIGCHLD_handler;
act.sa_flags = SA_RESTART | SA_SIGINFO;//SA_NOCLDSTOP;
sigaction(SIGABRT, &act, 0);
sigaction(SIGSEGV, &act, 0);
//reap_any_outstanding_children();
// register_epoll_handler(signal_read_fd, handle_signal);
if(fork()==0)
{
printf("waiting for read \n");
read(signal_read_fd, &signal, sizeof(signal));
printf("read data from socket %d, %d\n", signal_read_fd, signal);
printf("process PPID = %d PID = %d\n", getppid(), getpid());
close(s[0]);
close(s[1]);
}else{
close(s[0]);
close(s[1]);
}
/* while(quit)
{
sleep(3000);
}*/
exit(1);
}
static void handle_signal() {
// Clear outstanding requests.
char buf[32];
read(signal_read_fd, buf, sizeof(buf));
printf("handle_signal, read data from socket %d, %s\n", signal_read_fd, buf);
quit =0;
//reap_any_outstanding_children();
}
static void SIGCHLD_handler(int k) {
printf("INIT#, child process dead k= %d\n", k);
if (TEMP_FAILURE_RETRY(write(signal_write_fd, &k, sizeof(k))) == -1) {
printf("write(signal_write_fd) failed: %s\n", strerror(errno));
}
}
/*
void register_epoll_handler(int fd, void (*fn)()) {
epoll_event ev;
ev.events = EPOLLIN;
ev.data.ptr = reinterpret_cast<void*>(fn);
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
printf("epoll_ctl failed: %s\n", strerror(errno));
}
}*/
void coredump_test()
{
char *a = NULL ;
*a = 0x323;
}