summaryrefslogtreecommitdiff
path: root/src/sapp_dev/sapp_plug.c
blob: b323eee4d27e257b1922a20c31713ee153562bc3 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "sapp_api.h"
#include "sapp_private_api.h"


#ifdef __cplusplus
extern "C" {
#endif

static const unsigned char MESA_art_log[] = 
{
	0x50, 0x72, 0x6F, 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x62, 0x79, 0x0D, 0x0A, 0x20, 0x20, 0x20,
	0x20, 0x5F, 0x5F, 0x20, 0x20, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 
	0x5F, 0x5F, 0x5F, 0x20, 0x5F, 0x5F, 0x5F, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x2F, 0x20, 0x20, 0x7C, 
	0x2F, 0x20, 0x20, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x5F, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x2F, 0x2F, 
	0x20, 0x20, 0x20, 0x7C, 0x0D, 0x0A, 0x20, 0x20, 0x2F, 0x20, 0x2F, 0x7C, 0x5F, 0x2F, 0x20, 0x2F, 
	0x20, 0x5F, 0x5F, 0x2F, 0x20, 0x20, 0x5C, 0x5F, 0x5F, 0x20, 0x5C, 0x2F, 0x20, 0x2F, 0x7C, 0x20, 
	0x7C, 0x0D, 0x0A, 0x20, 0x2F, 0x20, 0x2F, 0x20, 0x20, 0x2F, 0x20, 0x2F, 0x20, 0x2F, 0x5F, 0x5F, 
	0x5F, 0x20, 0x5F, 0x5F, 0x5F, 0x2F, 0x20, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x20, 0x7C, 0x0D, 0x0A, 
	0x2F, 0x5F, 0x2F, 0x20, 0x20, 0x2F, 0x5F, 0x2F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x2F ,0x2F, 0x5F, 
	0x5F, 0x5F, 0x5F, 0x2F, 0x5F, 0x2F, 0x20, 0x20, 0x7C, 0x5F, 0x7C, 0x0D, 0x0A, 0x00/* EOF */ 
};

int MESA_platform_init(int argc, char *argv[]);
void MESA_platform_run(void);
extern int sapp_args_v;
int dpdk_init(int argc, char **argv);

/*
	��Щ�������ܷ���sapp_global_val, �����ж�destroy����״̬��, destroy��������Ҫfree sapp_global_val������!
	0:init or done; >= 1:just doing in any thread;
*/

volatile unsigned long g_sapp_destory_env_running_state;
volatile unsigned long g_sapp_destory_env_done_state;

static volatile unsigned long g_destory_env_per_thread_running_state[MAX_THREAD_NUM];


static void signal_user_handler(int signo)
{
	//time_t last_time = time(NULL);
	//while(time(NULL) < last_time + 10); /* wait 10 second, for DPDK IO module detect this process is not running */
	signal(signo, SIG_DFL);
	kill(getpid(), signo);
}

static void signal_hup_handler(int signo)
{
	printf("SIGHUP recviced!\n");
	MESA_handle_runtime_log_reconstruction(NULL);
}

static void signal_take_over(void)
{
	sapp_config_t *pconfig = &sapp_global_val->config;
	signal(SIGHUP, signal_hup_handler);
	if (0 == sapp_global_single.signal_take_over_sw && 0 == pconfig->tools.signal_handler.signal)
	{
		return;
	}

	signal(SIGUSR1, signal_user_handler);
	signal(SIGUSR2, signal_user_handler);
	//signal(pconfig->tools.signal_handler.signal, signal_handler);
}


static void show_mesa_log(void)
{
	int i;

	for(i = 0; (i < sizeof(MESA_art_log)) && (MESA_art_log[i] != 0); i ++){
		//putchar(MESA_log[i]);
		printf("%c", MESA_art_log[i]);
	}
	printf("\n");
	sapp_runtime_log(30, "\n\n%s", MESA_art_log);	
}

static void sapp_wait_for_required_thread_starting(void)
{
	int tseq;

	for(tseq = 0; tseq < g_packet_io_thread_num; tseq++){
		while(sapp_global_val->individual_fixed.thread_obtain_id[tseq] == 0){
			sapp_usleep(10);
		}
	}

	while(sapp_global_val->individual_fixed.thread_timer_event_id == 0){
		sapp_usleep(10);
	}
}


int libsapp_setup_env(int argc, char *argv[])
{
	int ret;

	MESA_ATOMIC_SET(g_sapp_destory_env_running_state, 0);
	MESA_ATOMIC_SET(g_sapp_destory_env_done_state, 0);
	memset((void *)g_destory_env_per_thread_running_state, 0, sizeof(g_destory_env_per_thread_running_state));
	
	sapp_gval_init();
	sapp_set_current_state(SAPP_STATE_JUST_START);
	sapp_set_current_state(SAPP_STATE_CONFIG_PARSE);
	
	if(argc >= 2){
		/* ����������в���, �Ƚ���cla, ��Ϊ����ʹ��-h, -v�Ȱ�����Ϣ		 */
		if(sapp_parse_cmd_args(argc, argv) < 0){
			return -1;
		}

		ret = sapp_parse_config();
	}else{
		/* û�������в���, ʹ��Ĭ��ֵ��ʼ�����·�� */
		sapp_update_main_config_file(NULL);
		sapp_get_secondary_file_path();
		sapp_update_config_root_dir(NULL);
		sapp_update_data_root_dir(NULL);
		ret = sapp_parse_config();
	}
	if(ret < 0){
		return -1;
	}

	sapp_cla_override_cfg_file();
	
	show_mesa_log();

#if SAPP_INSECTICIDE
	printf("\033[33m[Warning]This sapp is a temp version for solve confounded bug!\033[0m\n");
#endif
	
#if COMPAT_PAPP_FOR_BENCHMARK
	printf("\033[33m[Warning]This sapp is a emasculate version for compare with papp, in other word, it's papp!\033[0m\n");
	sleep(1);
#endif

	sapp_init_breakpad_mini();
	signal_take_over();
	
	if(MESA_platform_init(argc, argv) < 0){
		return -1;
	}


	MESA_platform_run();

	sapp_wait_for_required_thread_starting();

	//TODO: dumpfileģʽ���������Զ��˳�, ����һֱ����
	//�������ź�, �����������߳�ǿ�е���sapp_destroy_env()
#if 0	
	while((SAPP_STATE_PROCESSING == sapp_global_val->individual_volatile->current_state)
	 && (sapp_global_val->config.packet_io.depolyment_mode_bin)){
		usleep(1);
	}
#endif 
	return 0;
}


static void wait_for_all_io_threads(void)
{
	int tseq;

	for(tseq = 0; tseq < g_packet_io_thread_num; tseq++){
		pthread_join(sapp_global_val->individual_fixed.thread_obtain_id[tseq], NULL);
	}

	pthread_join(sapp_global_val->individual_fixed.thread_timer_event_id, NULL);

	sapp_timer_destroy(sapp_global_val->individual_fixed.sapp_standalone_timer);
}



void libsapp_destroy_env_per_thread(int tseq)
{
	if(MESA_ATOMIC_INC(g_destory_env_per_thread_running_state[tseq]) != 1){ 
		/* ��ʼ��ʱ��0, INC����1, ˵����������һ���߳�������destroy����, �����ȴ�, ֱ������Ϊֹ */
		MESA_ATOMIC_DEC(g_destory_env_per_thread_running_state[tseq]);
		while(MESA_ATOMIC_READ(g_destory_env_per_thread_running_state[tseq]) != 0){
			sapp_usleep(1000);
		}
		return;
	}

	packet_io_clean_thread_context(tseq);
	MESA_ATOMIC_DEC(g_destory_env_per_thread_running_state[tseq]);
}




void libsapp_destroy_env(void)
{
	int exit_process = 0;

	/* 
		ע��! һ���������: 
		sapp -d������Ϻ�, ���Զ�����libsapp_destroy_env(),
		���Dz��Ҳ���ܻ����ⲿ��������libsapp_destroy_env(),
		�������ͬ������, ��double free��!!
		�˴������ü�������һ���Ⱥ��߼�˳��.
	*/
	
	if(MESA_ATOMIC_INC(g_sapp_destory_env_running_state) != 1){ 
		/* ��ʼ��ʱ��0, INC����1, ˵����������һ���߳�������destroy����, �����ȴ�, ֱ������Ϊֹ */
		MESA_ATOMIC_DEC(g_sapp_destory_env_running_state);
		while(MESA_ATOMIC_READ(g_sapp_destory_env_running_state) != 0){
			sapp_usleep(1000);
		}
		return;
	}

	if(NULL == sapp_global_val){
		/* �����Ѿ����ù�libsapp_destroy_env(), ����libsapp_setup_env()ʧ���� */
		return;
	}

	if(CAP_MODEL_PCAP_DUMPFILE == g_packet_io_cap_mode){
		exit_process = 1;
	}

	sapp_set_current_state(SAPP_STATE_READY_TO_EXIT);
	packet_io_stop();
	wait_for_all_io_threads();
	packet_io_exit();

	/* ������Դ��free��, ��ȥ���ü��� */
	MESA_ATOMIC_DEC(g_sapp_destory_env_running_state);

	MESA_ATOMIC_SET(g_sapp_destory_env_done_state, SAPP_DESTROY_DONE_FLAG);	
	if(exit_process){
		exit(0);
	}	
}


#ifdef __cplusplus
}
#endif