[PACKET_IO]fix pcap_compile thread unsafe bug

This commit is contained in:
liuwentan
2022-08-26 17:39:50 +08:00
parent 9cdc57eaa7
commit 223fc15aae
2 changed files with 11 additions and 3 deletions

View File

@@ -82,7 +82,6 @@ static ssize_t init_pcap_file(struct pcap_plain_file_info *pfile_info)
}
pthread_mutex_init(&pfile_info->handle_mutex, nullptr);
#if 0
if (pfile_info->shared != nullptr && pfile_info->shared->bpf_string != nullptr) {
if (pcap_compile(pfile_info->pcap_handle, &pfile_info->filter,
pfile_info->shared->bpf_string, 1, 0) < 0) {
@@ -99,7 +98,7 @@ static ssize_t init_pcap_file(struct pcap_plain_file_info *pfile_info)
}
pcap_freecode(&pfile_info->filter);
}
#endif
pfile_info->data_link = pcap_datalink(pfile_info->pcap_handle);
return 0;
@@ -169,6 +168,9 @@ static ssize_t pcap_directory_file_init(struct pio_pcap_file_device_context *pfi
TAILQ_INIT(&g_pending_file_queue.file_queue_head);
pthread_mutex_init(&g_pending_file_queue.queue_mutex, nullptr);
/* pcap file device context mutex init */
pthread_mutex_init(&pfile_dev_ctx->ctx_mutex, nullptr);
return 0;
}
@@ -792,7 +794,11 @@ static ssize_t pcap_directory_files_dispatch(struct pio_pcap_file_device_context
pfile_info->shared = &pfile_dev_ctx->shared;
if (init_pcap_file(pfile_info) < 0) {
/* init_pcap_file is thread unsafe, because it calls pcap_compile(thread unsafe) */
pthread_mutex_lock(&pfile_dev_ctx->ctx_mutex);
res = init_pcap_file(pfile_info);
pthread_mutex_unlock(&pfile_dev_ctx->ctx_mutex);
if (res < 0) {
FREE(pfile_info);
FREE(pfile_dev_ctx->entity.dir->pending_file[rxq_id]);
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "init_pcap_file failed.");