Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 293cd21

Browse files
Removed usage of pthreads and replaced with std C++ features
- Replaced pthread_mutex_t in modsecurity::operators::Pm with std::mutex - Replaced pthread's thread usage in reading_logs_via_rule_message example with std::thread. - Simplified and modernized C++ code. - Removed unnecessary includes of pthread.h
1 parent 4e15f9e commit 293cd21

File tree

12 files changed

+27
-70
lines changed

12 files changed

+27
-70
lines changed

‎build/win32/CMakeLists.txt‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/ConfigureChecks.cmake)
110110
configure_file(config.h.cmake ${BASE_DIR}/src/config.h)
111111

112112
find_package(PCRE2 REQUIRED)
113-
find_package(PThreads4W REQUIRED)
114113
find_package(Poco REQUIRED)
115114
find_package(dirent REQUIRED) # used only by tests (check dirent::dirent refernces)
116115

@@ -139,7 +138,7 @@ add_library(libModSecurity SHARED ${libModSecuritySources})
139138

140139
target_compile_definitions(libModSecurity PRIVATE WITH_PCRE2)
141140
target_include_directories(libModSecurity PRIVATE ${BASE_DIR} ${BASE_DIR}/headers ${BASE_DIR}/others ${MBEDTLS_DIR}/include)
142-
target_link_libraries(libModSecurity PRIVATE pcre2::pcre2 pthreads4w::pthreads4w libinjection mbedcrypto Poco::Poco Iphlpapi.lib)
141+
target_link_libraries(libModSecurity PRIVATE pcre2::pcre2 libinjection mbedcrypto Poco::Poco Iphlpapi.lib)
143142

144143
macro(add_package_dependency project compile_definition link_library flag)
145144
if(${flag})
@@ -255,7 +254,6 @@ setExampleTargetProperties(using_bodies_in_chunks)
255254
# reading_logs_via_rule_message
256255
add_executable(reading_logs_via_rule_message ${BASE_DIR}/examples/reading_logs_via_rule_message/simple_request.cc)
257256
setExampleTargetProperties(reading_logs_via_rule_message)
258-
target_link_libraries(reading_logs_via_rule_message PRIVATE libModSecurity pthreads4w::pthreads4w)
259257

260258
# reading_logs_with_offset
261259
add_executable(reading_logs_with_offset ${BASE_DIR}/examples/reading_logs_with_offset/read.cc)

‎build/win32/conanfile.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[requires]
22
yajl/2.1.0
33
pcre2/10.42
4-
pthreads4w/3.0.0
54
libxml2/2.12.6
65
lua/5.4.6
76
libcurl/8.6.0

‎examples/multiprocess_c/Makefile.am‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ multi_LDFLAGS = \
1515
-L$(top_builddir)/src/.libs/ \
1616
$(GEOIP_LDFLAGS) \
1717
-lmodsecurity \
18-
-lpthread \
1918
-lm \
2019
-lstdc++ \
2120
$(LUA_LDFLAGS) \

‎examples/reading_logs_via_rule_message/Makefile.am‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ simple_request_LDFLAGS = \
2121
-L$(top_builddir)/src/.libs/ \
2222
$(GEOIP_LDFLAGS) \
2323
-lmodsecurity \
24-
-lpthread \
2524
-lm \
2625
-lstdc++ \
2726
$(LMDB_LDFLAGS) \

‎examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h‎

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313
*
1414
*/
1515

16+
#ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
17+
#define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
18+
1619
#include <string>
1720
#include <memory>
1821
#include <thread>
22+
#include <array>
1923
#include <chrono>
20-
#include <pthread.h>
24+
25+
#include "modsecurity/rule_message.h"
2126

2227

23-
#defineNUM_THREADS 100
28+
constexprautoNUM_THREADS = 100;
2429

2530

2631
char request_header[] = "" \
@@ -62,59 +67,33 @@ char response_body[] = "" \
6267

6368
char ip[] = "200.249.12.31";
6469

65-
#include "modsecurity/rule_message.h"
66-
67-
#ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
68-
#define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
69-
7070

71-
struct data_ms {
72-
modsecurity::ModSecurity *modsec;
73-
modsecurity::RulesSet *rules;
74-
};
71+
static void process_request(modsecurity::ModSecurity *modsec, modsecurity::RulesSet *rules) {
72+
for (auto z = 0; z < 10000; z++) {
73+
auto modsecTransaction = std::make_unique<modsecurity::Transaction>(modsec, rules, nullptr);
7574

76-
#if defined _MSC_VER
77-
#pragma warning(push)
78-
#pragma warning(disable:4716) // avoid error C4716: 'process_request': must return a value, as MSVC C++ compiler doesn't support [[noreturn]]
79-
#pragma warning(disable:4715) // avoid warning c4715: 'process_request' : not all control paths return a value
80-
#endif
81-
82-
[[noreturn]] static void *process_request(void *data) {
83-
struct data_ms *a = (struct data_ms *)data;
84-
modsecurity::ModSecurity *modsec = a->modsec;
85-
modsecurity::RulesSet *rules = a->rules;
86-
int z = 0;
87-
88-
for (z = 0; z < 10000; z++) {
89-
modsecurity::Transaction *modsecTransaction = \
90-
new modsecurity::Transaction(modsec, rules, NULL);
9175
modsecTransaction->processConnection(ip, 12345, "127.0.0.1", 80);
9276
modsecTransaction->processURI(request_uri, "GET", "1.1");
9377

9478
std::this_thread::sleep_for(std::chrono::microseconds(10));
79+
9580
modsecTransaction->addRequestHeader("Host",
9681
"net.tutsplus.com");
9782
modsecTransaction->processRequestHeaders();
9883
modsecTransaction->processRequestBody();
84+
9985
modsecTransaction->addResponseHeader("HTTP/1.1",
10086
"200 OK");
10187
modsecTransaction->processResponseHeaders(200, "HTTP 1.2");
10288
modsecTransaction->appendResponseBody(
10389
(const unsigned char*)response_body,
10490
strlen((const char*)response_body));
10591
modsecTransaction->processResponseBody();
106-
modsecTransaction->processLogging();
10792

108-
deletemodsecTransaction;
93+
modsecTransaction->processLogging();
10994
}
110-
111-
pthread_exit(nullptr);
11295
}
11396

114-
#if defined _MSC_VER
115-
#pragma warning(pop)
116-
#endif
117-
11897
class ReadingLogsViaRuleMessage {
11998
public:
12099
ReadingLogsViaRuleMessage(char *request_header,
@@ -134,11 +113,6 @@ class ReadingLogsViaRuleMessage {
134113
{ }
135114

136115
int process() const {
137-
pthread_t threads[NUM_THREADS];
138-
int i;
139-
struct data_ms dms;
140-
void *status;
141-
142116
auto modsec = std::make_unique<modsecurity::ModSecurity>();
143117
modsec->setConnectorInformation("ModSecurity-test v0.0.1-alpha" \
144118
" (ModSecurity test)");
@@ -152,18 +126,19 @@ class ReadingLogsViaRuleMessage {
152126
return -1;
153127
}
154128

155-
dms.modsec = modsec.get();
156-
dms.rules = rules.get();
129+
std::array<std::thread, NUM_THREADS> threads;
157130

158-
for (i = 0; i < NUM_THREADS; i++) {
159-
pthread_create(&threads[i], NULL, process_request,
160-
reinterpret_cast<void *>(&dms));
131+
for (auto i = 0; i != threads.size(); ++i) {
132+
threads[i] = std::thread(
133+
[&modsec, &rules]() {
134+
process_request(modsec.get(), rules.get());
135+
});
161136
}
162137

163138
std::this_thread::sleep_for(std::chrono::microseconds(10000));
164139

165-
for (i=0; i < NUM_THREADS; i++) {
166-
pthread_join(threads[i], &status);
140+
for (auto i = 0; i != threads.size(); ++i) {
141+
threads[i].join();
167142
std::cout << "Main: completed thread id :" << i << std::endl;
168143
}
169144

‎examples/reading_logs_with_offset/Makefile.am‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ read_LDFLAGS = \
2121
-L$(top_builddir)/src/.libs/ \
2222
$(GEOIP_LDFLAGS) \
2323
-lmodsecurity \
24-
-lpthread \
2524
-lm \
2625
-lstdc++ \
2726
$(LMDB_LDFLAGS) \

‎examples/using_bodies_in_chunks/Makefile.am‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ simple_request_LDFLAGS = \
2121
-L$(top_builddir)/src/.libs/ \
2222
$(GEOIP_LDFLAGS) \
2323
-lmodsecurity \
24-
-lpthread \
2524
-lm \
2625
-lstdc++ \
2726
$(MAXMIND_LDFLAGS) \
2827
$(LMDB_LDFLAGS) \
29-
-lpthread \
3028
$(LUA_LDFLAGS) \
3129
$(SSDEEP_LDFLAGS) \
3230
$(YAJL_LDFLAGS)

‎src/collection/backend/in_memory-per_process.cc‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <memory>
2626
#endif
2727

28-
#include <pthread.h>
29-
3028
#include "modsecurity/variable_value.h"
3129
#include "src/utils/regex.h"
3230
#include "src/utils/string.h"

‎src/collection/backend/lmdb.cc‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include <string>
2828
#include <memory>
2929

30-
#include <pthread.h>
31-
3230
#include "modsecurity/variable_value.h"
3331
#include "src/utils/regex.h"
3432
#include "src/variables/variable.h"

‎src/collection/backend/lmdb.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727

2828
#ifdef WITH_LMDB
2929
#include <lmdb.h>
30-
#include <semaphore.h>
3130
#endif // WITH_LMDB
3231
#include <sys/stat.h>
3332
#include <sys/types.h>
3433
#include <fcntl.h>
35-
#include <pthread.h>
3634

3735
#include "modsecurity/variable_value.h"
3836
#include "modsecurity/collection/collection.h"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /