2 Commits

Author SHA1 Message Date
f886dc7162 Multi-process logic changed to multi-thread logic 2025-05-31 15:00:38 +02:00
23d0216a27 Usermanager protected with lock_guards 2025-05-31 13:26:44 +02:00
2 changed files with 70 additions and 92 deletions

View File

@@ -1,62 +1,22 @@
#include <stdio.h>
#include <iostream>
#include <thread>
#include <vector>
#include <array>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <syslog.h>
#include <fcntl.h>
#include <signal.h>
#include "locations.h"
#include <mutex>
#include <condition_variable>
#include <cstdio>
#include "usermanager.h"
#include "guarder.h"
#define MAXNAMESIZE 255
#define SCAN_DONE_SIG SIGRTMIN
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
int p1[2];
guarder guard;
userManager manager;
std::condition_variable cv;
std::mutex mtx;
void flushManagerToPipe()
void readingThreadFunc()
{
if (!guard.isFlagOn())
{
std::vector<std::string> users = manager.getFlaggedUsers();
for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it)
{
int size = it->size();
write(p1[1], &size, sizeof(int));
write(p1[1], it->data(), size * sizeof(char));
}
manager.unflagAllUsers();
}
}
void handler(int sig)
{
if (sig == SCAN_DONE_SIG)
{
flushManagerToPipe();
}
}
int main()
{
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
pipe(p1);
pid_t parent = getpid();
pid_t child = fork();
if (child > 0) // parent
{
signal(SCAN_DONE_SIG, handler);
close(p1[0]); // read
FILE *logpipe = popen(LOGFILE, "r");
std::array<char, 256> buffer;
@@ -69,51 +29,44 @@ int main()
std::vector<std::string> x = splitLogFile(line, '|');
std::string user(x.at(USER_LOG_LOCATION));
{
std::lock_guard<std::mutex> lock(mtx);
manager.addUser(user);
manager.setUserFlagged(user);
}
cv.notify_one();
std::cout << "User find: " << user << std::endl;
flushManagerToPipe();
}
}
fclose(logpipe);
close(p1[1]); // write
}
else // child
void scannerThreadFunc()
{
close(p1[1]); // write
int size;
char *buffer = nullptr;
while (read(p1[0], &size, sizeof(int)))
std::unique_lock<std::mutex> lock(mtx);
while (true)
{
if (buffer == nullptr)
buffer = new char[size];
cv.wait(lock, []
{ return manager.isAnybodyInQueue(); });
read(p1[0], buffer, size * sizeof(char));
std::string name(buffer);
std::vector<std::string> scanUsers = manager.getFlaggedUsers();
manager.unflagAllUsers();
std::cout << "Scan received for: " << name << std::endl;
guard.setFlagOn();
system((std::string(SCAN_CMD_USR) + name).data());
guard.setFlagOff();
kill(parent, SCAN_DONE_SIG);
if (buffer != nullptr)
for (std::vector<std::string>::iterator it = scanUsers.begin(); it != scanUsers.end(); it++)
{
delete[] buffer;
buffer = nullptr;
system((std::string(SCAN_CMD_USR) + *it).c_str());
}
}
}
close(p1[0]); // read
}
int main()
{
std::thread readingThread(readingThreadFunc);
std::thread scannerThread(scannerThreadFunc);
closelog();
readingThread.join();
scannerThread.join();
return EXIT_SUCCESS;
return 0;
}

View File

@@ -5,6 +5,7 @@
#include <map>
#include <vector>
#include <sstream>
#include <mutex>
#include "locations.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
@@ -13,6 +14,7 @@ class userManager
{
private:
std::map<std::string, bool> users;
std::mutex mtx;
public:
@@ -23,6 +25,7 @@ public:
void addUser(std::string &user)
{
std::lock_guard<std::mutex> lock(mtx);
if (users.count(user) == 0)
{
users[user] = false;
@@ -31,16 +34,19 @@ public:
void removeUser(std::string &user)
{
std::lock_guard<std::mutex> lock(mtx);
users.erase(user);
}
bool isContains(std::string &user)
{
std::lock_guard<std::mutex> lock(mtx);
return users.count(user) == 1;
}
void setUserFlagged(std::string &user)
{
std::lock_guard<std::mutex> lock(mtx);
if (users.count(user) == 1)
{
users[user] = true;
@@ -49,6 +55,7 @@ public:
void setUserUnflagged(std::string &user)
{
std::lock_guard<std::mutex> lock(mtx);
if (users.count(user) == 1)
{
users[user] = false;
@@ -57,6 +64,7 @@ public:
void unflagAllUsers()
{
std::lock_guard<std::mutex> lock(mtx);
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
it->second = false;
@@ -67,6 +75,8 @@ public:
{
std::vector<std::string> ret;
std::lock_guard<std::mutex> lock(mtx);
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
ret.push_back(it->first);
@@ -79,6 +89,8 @@ public:
{
std::vector<std::string> ret;
std::lock_guard<std::mutex> lock(mtx);
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
if (it->second)
@@ -89,6 +101,19 @@ public:
return ret;
}
bool isAnybodyInQueue()
{
std::lock_guard<std::mutex> lock(mtx);
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
if (it->second)
return true;
}
return false;
}
};
#endif // _USERMAN_H