-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathshared_files.cc
More file actions
192 lines (155 loc) · 4.64 KB
/
shared_files.cc
File metadata and controls
192 lines (155 loc) · 4.64 KB
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
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include "src/utils/shared_files.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef WIN32
#include <algorithm>
#endif
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
namespace modsecurity {
namespace utils {
SharedFiles::handlers_map::iterator SharedFiles::add_new_handler(
const std::string &fileName, std::string *error) {
FILE *fp = fopen(fileName.c_str(), "a");
if (fp == 0) {
error->assign("Failed to open file: " + fileName);
return m_handlers.end();
}
#ifdef WIN32
// replace invalid characters for a Win32 named object
auto tmp = fileName;
std::replace(tmp.begin(), tmp.end(), '\\', '_');
std::replace(tmp.begin(), tmp.end(), '/', '_');
// use named mutex for multi-process locking support
const auto mutexName = "Global\\ModSecurity_" + tmp;
HANDLE hMutex = CreateMutex(NULL, FALSE, mutexName.c_str());
if (hMutex == NULL) {
error->assign("Failed to create mutex for shared file: " + fileName);
fclose(fp);
return m_handlers.end();
}
#endif
auto handler = handler_info {
fp,
#ifdef WIN32
hMutex,
#endif
0
};
// cppcheck-suppress resourceLeak ; false positive, fp is closed in SharedFiles::close
return m_handlers.insert({ fileName, handler }).first;
}
bool SharedFiles::open(const std::string& fileName, std::string *error) {
auto it = m_handlers.find(fileName);
if (it == m_handlers.end()) {
it = add_new_handler(fileName, error);
if (error->size() > 0)
return false;
}
if (it == m_handlers.end()) {
error->assign("Not able to open: " + fileName);
return false;
}
it->second.cnt++;
return true;
}
bool SharedFiles::reopen(const std::string& fileName, std::string *error) {
auto it = m_handlers.find(fileName);
if (it == m_handlers.end()) {
error->assign("Cannot find open file to reopen: " + fileName);
return false;
}
struct stat target_stat;
struct stat current_stat;
if (
stat(fileName.c_str(), &target_stat) == 0 &&
fstat(fileno(it->second.fp), ¤t_stat) == 0 &&
current_stat.st_dev == target_stat.st_dev &&
current_stat.st_ino == target_stat.st_ino
) {
return true;
}
FILE *fp;
#ifdef WIN32
fopen_s(&fp, fileName.c_str(), "a");
#else
fp = fopen(fileName.c_str(), "a");
#endif
if (fp == nullptr) {
error->assign("Failed to reopen file: " + fileName);
return false;
}
fclose(it->second.fp);
it->second.fp = fp;
return true;
}
void SharedFiles::close(const std::string& fileName) {
if (fileName.empty())
return;
auto it = m_handlers.find(fileName);
if (it == m_handlers.end())
return;
it->second.cnt--;
if (it->second.cnt == 0)
{
fclose(it->second.fp);
#ifdef WIN32
CloseHandle(it->second.hMutex);
#endif
m_handlers.erase(it);
}
}
bool SharedFiles::write(const std::string& fileName,
const std::string &msg, std::string *error) {
bool ret = true;
auto it = m_handlers.find(fileName);
if (it == m_handlers.end()) {
error->assign("file is not open: " + fileName);
return false;
}
//Exclusively lock whole file
#ifndef WIN32
struct flock lock {};
lock.l_start = lock.l_len = lock.l_whence = 0;
lock.l_type = F_WRLCK;
fcntl(fileno(it->second.fp), F_SETLKW, &lock);
#else
DWORD dwWaitResult = WaitForSingleObject(it->second.hMutex, INFINITE);
if (dwWaitResult != WAIT_OBJECT_0) {
error->assign("couldn't lock shared file: " + fileName);
return false;
}
#endif
auto wrote = fwrite(msg.c_str(), 1, msg.size(), it->second.fp);
if (wrote < msg.size()) {
error->assign("failed to write: " + fileName);
ret = false;
}
fflush(it->second.fp);
//Remove exclusive lock
#ifndef WIN32
lock.l_type = F_UNLCK;
fcntl(fileno(it->second.fp), F_SETLKW, &lock);
#else
::ReleaseMutex(it->second.hMutex);
#endif
return ret;
}
} // namespace utils
} // namespace modsecurity