Multi-process logic changed to multi-thread logic

This commit is contained in:
2025-05-31 15:00:38 +02:00
parent 23d0216a27
commit f886dc7162
2 changed files with 58 additions and 92 deletions

View File

@@ -1,119 +1,72 @@
#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();
FILE *logpipe = popen(LOGFILE, "r");
std::array<char, 256> buffer;
while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
{
std::string line(buffer.data());
if (line.find('|') == std::string::npos)
continue;
std::vector<std::string> x = splitLogFile(line, '|');
std::string user(x.at(USER_LOG_LOCATION));
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));
std::lock_guard<std::mutex> lock(mtx);
manager.addUser(user);
manager.setUserFlagged(user);
}
manager.unflagAllUsers();
cv.notify_one();
std::cout << "User find: " << user << std::endl;
}
}
void handler(int sig)
void scannerThreadFunc()
{
if (sig == SCAN_DONE_SIG)
std::unique_lock<std::mutex> lock(mtx);
while (true)
{
flushManagerToPipe();
cv.wait(lock, []
{ return manager.isAnybodyInQueue(); });
std::vector<std::string> scanUsers = manager.getFlaggedUsers();
manager.unflagAllUsers();
for (std::vector<std::string>::iterator it = scanUsers.begin(); it != scanUsers.end(); it++)
{
system((std::string(SCAN_CMD_USR) + *it).c_str());
}
}
}
int main()
{
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
pipe(p1);
std::thread readingThread(readingThreadFunc);
std::thread scannerThread(scannerThreadFunc);
pid_t parent = getpid();
pid_t child = fork();
readingThread.join();
scannerThread.join();
if (child > 0) // parent
{
signal(SCAN_DONE_SIG, handler);
close(p1[0]); // read
FILE *logpipe = popen(LOGFILE, "r");
std::array<char, 256> buffer;
while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
{
std::string line(buffer.data());
if (line.find('|') == std::string::npos)
continue;
std::vector<std::string> x = splitLogFile(line, '|');
std::string user(x.at(USER_LOG_LOCATION));
manager.addUser(user);
manager.setUserFlagged(user);
std::cout << "User find: " << user << std::endl;
flushManagerToPipe();
}
fclose(logpipe);
close(p1[1]); // write
}
else // child
{
close(p1[1]); // write
int size;
char *buffer = nullptr;
while (read(p1[0], &size, sizeof(int)))
{
if (buffer == nullptr)
buffer = new char[size];
read(p1[0], buffer, size * sizeof(char));
std::string name(buffer);
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)
{
delete[] buffer;
buffer = nullptr;
}
}
close(p1[0]); // read
}
closelog();
return EXIT_SUCCESS;
return 0;
}

View File

@@ -101,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