Improvements on the SharedFiles class

examples/multiprocess_c/multi
This commit is contained in:
Felipe Zimmerle 2017-02-23 22:11:30 -03:00 committed by Felipe Zimmerle
parent 01c13da510
commit 7f9cd76619
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
2 changed files with 177 additions and 51 deletions

View File

@ -27,7 +27,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <utility>
#include <fstream> #include <fstream>
#include <string> #include <string>
@ -35,22 +35,25 @@ namespace modsecurity {
namespace utils { namespace utils {
msc_file_handler_t *SharedFiles::find_handler( std::pair<msc_file_handler *, FILE *> SharedFiles::find_handler(
const std::string &fileName) { const std::string &fileName) {
for (const auto &i: m_handlers) { for (const auto &i : m_handlers) {
if (i.first == fileName) { if (i.first == fileName) {
return i.second; return i.second;
} }
} }
return NULL; return std::pair<modsecurity::utils::msc_file_handler*,
_IO_FILE*>(NULL, NULL);
} }
msc_file_handler_t *SharedFiles::add_new_handler( std::pair<msc_file_handler *, FILE *> SharedFiles::add_new_handler(
const std::string &fileName, std::string *error) { const std::string &fileName, std::string *error) {
int shm_id; int shm_id;
int ret;
key_t mem_key_structure; key_t mem_key_structure;
msc_file_handler_t *new_debug_log; msc_file_handler_t *new_debug_log;
struct shmid_ds shared_mem_info;
FILE *fp; FILE *fp;
bool toBeCreated = true; bool toBeCreated = true;
@ -67,8 +70,8 @@ msc_file_handler_t *SharedFiles::add_new_handler(
goto err_mem_key; goto err_mem_key;
} }
shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t) + fileName.size() + 1, shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t) \
IPC_CREAT | IPC_EXCL | 0666); + fileName.size() + 1, IPC_CREAT | IPC_EXCL | 0666);
if (shm_id < 0) { if (shm_id < 0) {
shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t) shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t)
+ fileName.size() + 1, IPC_CREAT | 0666); + fileName.size() + 1, IPC_CREAT | 0666);
@ -80,6 +83,13 @@ msc_file_handler_t *SharedFiles::add_new_handler(
} }
} }
ret = shmctl(shm_id, IPC_STAT, &shared_mem_info);
if (ret < 0) {
error->assign("Failed to get information on shared memory (1): ");
error->append(strerror(errno));
goto err_shmctl1;
}
new_debug_log = reinterpret_cast<msc_file_handler_t *>( new_debug_log = reinterpret_cast<msc_file_handler_t *>(
shmat(shm_id, NULL, 0)); shmat(shm_id, NULL, 0));
if ((reinterpret_cast<char *>(new_debug_log)[0]) == -1) { if ((reinterpret_cast<char *>(new_debug_log)[0]) == -1) {
@ -88,103 +98,143 @@ msc_file_handler_t *SharedFiles::add_new_handler(
goto err_shmat1; goto err_shmat1;
} }
if (toBeCreated == false && shared_mem_info.shm_nattch == 0) {
toBeCreated = true;
}
if (toBeCreated) { if (toBeCreated) {
memset(new_debug_log, '\0', sizeof(msc_file_handler_t)); memset(new_debug_log, '\0', sizeof(msc_file_handler_t));
pthread_mutex_init(&new_debug_log->lock, NULL); pthread_mutex_init(&new_debug_log->lock, NULL);
new_debug_log->fp = fp;
new_debug_log->file_handler = fileno(new_debug_log->fp);
new_debug_log->shm_id_structure = shm_id; new_debug_log->shm_id_structure = shm_id;
memcpy(new_debug_log->file_name, fileName.c_str(), fileName.size()); memcpy(new_debug_log->file_name, fileName.c_str(), fileName.size());
new_debug_log->file_name[fileName.size()] = '\0'; new_debug_log->file_name[fileName.size()] = '\0';
} }
m_handlers.push_back(std::make_pair(fileName, new_debug_log)); m_handlers.push_back(std::make_pair(fileName,
std::make_pair(new_debug_log, fp)));
return new_debug_log; return std::make_pair(new_debug_log, fp);
err_shmget1: err_shmget1:
err_shmctl1:
err_shmat1: err_shmat1:
shmdt(new_debug_log); shmdt(new_debug_log);
err_mem_key: err_mem_key:
fclose(fp); fclose(fp);
err_fh: err_fh:
return NULL; return std::pair<modsecurity::utils::msc_file_handler*,
_IO_FILE*>(NULL, NULL);
} }
bool SharedFiles::open(const std::string& fileName, std::string *error) { bool SharedFiles::open(const std::string& fileName, std::string *error) {
msc_file_handler_t *a = find_handler(fileName); std::pair<msc_file_handler *, FILE *> a;
if (a == NULL) {
#if MODSEC_USE_GENERAL_LOCK
pthread_mutex_lock(m_generalLock);
#endif
a = find_handler(fileName);
if (a.first == NULL) {
a = add_new_handler(fileName, error); a = add_new_handler(fileName, error);
if (error->size() > 0) { if (error->size() > 0) {
return false; goto out;
} }
} }
if (a == NULL) { if (a.first == NULL) {
error->assign("Not able to open: " + fileName); error->assign("Not able to open: " + fileName);
return false; goto out;
} }
a->using_it++; out:
#if MODSEC_USE_GENERAL_LOCK
pthread_mutex_unlock(m_generalLock);
#endif
return true; return true;
} }
void SharedFiles::close(const std::string& fileName) { void SharedFiles::close(const std::string& fileName) {
msc_file_handler_t *a; std::pair<msc_file_handler *, FILE *> a;
int j = 0; /* int ret; */
/* int shm_id; */
/* struct shmid_ds shared_mem_info; */
/* int j = 0; */
#if MODSEC_USE_GENERAL_LOCK
pthread_mutex_lock(m_generalLock);
#endif
if (fileName.empty()) { if (fileName.empty()) {
return; goto out;
} }
a = find_handler(fileName); a = find_handler(fileName);
if (a == NULL) { if (a.first == NULL || a.second == NULL) {
return; goto out;
} }
a->using_it--; /* fclose(a.second); */
a.second = 0;
if (a->using_it == 0) { /*
int shm_id1 = a->shm_id_structure; * Delete the file structure will be welcomed, but we cannot delay
msc_file_handler_t *p , *n; * while the process is being killed.
pthread_mutex_lock(&a->lock); *
fclose(a->fp); for (std::pair<std::string,
pthread_mutex_unlock(&a->lock); std::pair<msc_file_handler *, FILE *>> i : m_handlers) {
pthread_mutex_destroy(&a->lock);
shmdt(a);
shmctl(shm_id1, IPC_RMID, NULL);
}
for (const auto &i: m_handlers) {
if (i.first == fileName) { if (i.first == fileName) {
j++; j++;
} }
} }
m_handlers.erase(m_handlers.begin() + j, m_handlers.begin() + j + 1); m_handlers.erase(m_handlers.begin()+j);
*/
/* hmdt(a.second); */
shmctl(a.first->shm_id_structure, IPC_RMID, NULL);
/*
*
* We could check to see how many process attached to the shared memory
* we have, prior to the deletion of the shared memory.
*
ret = shmctl(a.first->shm_id_structure, IPC_STAT, &shared_mem_info);
if (ret < 0) {
goto out;
}
ret = shared_mem_info.shm_nattch;
shm_id = a.first->shm_id_structure;
*/
out:
#if MODSEC_USE_GENERAL_LOCK
pthread_mutex_unlock(m_generalLock);
#endif
return;
} }
bool SharedFiles::write(const std::string& fileName, bool SharedFiles::write(const std::string& fileName,
const std::string &msg, std::string *error) { const std::string &msg, std::string *error) {
std::pair<msc_file_handler *, FILE *> a;
std::string lmsg = msg; std::string lmsg = msg;
size_t wrote; size_t wrote;
bool ret = true; bool ret = true;
msc_file_handler_t *a = find_handler(fileName); a = find_handler(fileName);
if (a == NULL) { if (a.first == NULL) {
error->assign("file is not open: " + fileName); error->assign("file is not open: " + fileName);
return false; return false;
} }
pthread_mutex_lock(&a->lock); pthread_mutex_lock(&a.first->lock);
wrote = fwrite(reinterpret_cast<const char *>(lmsg.c_str()), 1, wrote = fwrite(reinterpret_cast<const char *>(lmsg.c_str()), 1,
lmsg.size(), a->fp); lmsg.size(), a.second);
if (wrote < msg.size()) { if (wrote < msg.size()) {
error->assign("failed to write: " + fileName); error->assign("failed to write: " + fileName);
ret = false; ret = false;
} }
pthread_mutex_unlock(&a->lock); pthread_mutex_unlock(&a.first->lock);
return ret; return ret;
} }

View File

@ -17,26 +17,39 @@
#define SRC_UTILS_SHARED_FILES_H_ #define SRC_UTILS_SHARED_FILES_H_
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include <utility>
#include <vector>
#include <fstream>
#include <string> #include <string>
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#include "modsecurity/audit_log.h" #include "modsecurity/audit_log.h"
/**
* Not using this critical section yet.
*
*/
/* #define MODSEC_USE_GENERAL_LOCK */
namespace modsecurity { namespace modsecurity {
namespace utils { namespace utils {
typedef struct msc_file_handler { typedef struct msc_file_handler {
FILE *fp;
int file_handler;
int shm_id_structure; int shm_id_structure;
int using_it;
pthread_mutex_t lock; pthread_mutex_t lock;
char file_name[]; char file_name[];
} msc_file_handler_t; } msc_file_handler_t;
@ -48,19 +61,77 @@ class SharedFiles {
void close(const std::string& fileName); void close(const std::string& fileName);
bool write(const std::string& fileName, const std::string &msg, bool write(const std::string& fileName, const std::string &msg,
std::string *error); std::string *error);
static SharedFiles& getInstance() { static SharedFiles& getInstance() {
static SharedFiles instance; static SharedFiles instance;
return instance; return instance;
} }
protected: protected:
msc_file_handler_t *find_handler(const std::string &fileName); std::pair<msc_file_handler *, FILE *> find_handler(
msc_file_handler_t *add_new_handler(const std::string &fileName, const std::string &fileName);
std::string *error); std::pair<msc_file_handler *, FILE *> add_new_handler(
const std::string &fileName, std::string *error);
private: private:
SharedFiles() { } SharedFiles()
~SharedFiles() { } #ifdef MODSEC_USE_GENERAL_LOCK
: m_generalLock(NULL)
#endif
{
#ifdef MODSEC_USE_GENERAL_LOCK
int shm_id;
bool toBeCreated = true;
bool err = false;
m_memKeyStructure = ftok(".", 1);
if (m_memKeyStructure < 0) {
err = true;
goto err_mem_key;
}
shm_id = shmget(m_memKeyStructure, sizeof(pthread_mutex_t),
IPC_CREAT | IPC_EXCL | 0666);
if (shm_id < 0) {
shm_id = shmget(m_memKeyStructure, sizeof(pthread_mutex_t),
IPC_CREAT | 0666);
toBeCreated = false;
if (shm_id < 0) {
err = true;
goto err_shmget1;
}
}
m_generalLock = reinterpret_cast<pthread_mutex_t *>(
shmat(shm_id, NULL, 0));
if ((reinterpret_cast<char *>(m_generalLock)[0]) == -1) {
err = true;
goto err_shmat1;
}
if (toBeCreated) {
memset(m_generalLock, '\0', sizeof(pthread_mutex_t));
pthread_mutex_init(m_generalLock, NULL);
pthread_mutex_unlock(m_generalLock);
}
if (err) {
err_mem_key:
std::cerr << strerror(errno) << std::endl;
err_shmget1:
std::cerr << "err_shmget1" << std::endl;
err_shmat1:
std::cerr << "err_shmat1" << std::endl;
}
toBeCreated = false;
#endif
}
~SharedFiles() {
#if MODSEC_USE_GENERAL_LOCK
shmdt(m_generalLock);
shmctl(m_memKeyStructure, IPC_RMID, NULL);
#endif
}
// C++ 03 // C++ 03
// ======== // ========
@ -70,7 +141,12 @@ class SharedFiles {
SharedFiles(SharedFiles const&); SharedFiles(SharedFiles const&);
void operator=(SharedFiles const&); void operator=(SharedFiles const&);
std::vector<std::pair<std::string, msc_file_handler *>> m_handlers; std::vector<std::pair<std::string,
std::pair<msc_file_handler *, FILE *>>> m_handlers;
#if MODSEC_USE_GENERAL_LOCK
pthread_mutex_t *m_generalLock;
key_t m_memKeyStructure;
#endif
}; };