summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorliuwentan <[email protected]>2022-08-26 17:39:50 +0800
committerliuwentan <[email protected]>2022-08-31 22:06:41 +0800
commit223fc15aae7e81ab320b0c7534ae71979e64e820 (patch)
tree60f927a8ddd73aa2545a1c69aca5dbeed1c5fc6e
parent9cdc57eaa7612129e631a6a080892ee9d9b1a03a (diff)
[PACKET_IO]fix pcap_compile thread unsafe bug
-rw-r--r--src/packet_io/pcap_file_mode/pio_pcap_file.cpp12
-rw-r--r--src/packet_io/pcap_file_mode/pio_pcap_file.h2
2 files changed, 11 insertions, 3 deletions
diff --git a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp
index 7fec9a4..a882fcc 100644
--- a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp
+++ b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp
@@ -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.");
diff --git a/src/packet_io/pcap_file_mode/pio_pcap_file.h b/src/packet_io/pcap_file_mode/pio_pcap_file.h
index 24641e4..55e1854 100644
--- a/src/packet_io/pcap_file_mode/pio_pcap_file.h
+++ b/src/packet_io/pcap_file_mode/pio_pcap_file.h
@@ -99,6 +99,8 @@ struct pio_pcap_file_device_context {
/* point to packet_io device it belongs to */
struct packet_io_device *pdev;
+
+ pthread_mutex_t ctx_mutex;
};
/**