Replace multi-process logic to multi-thread logic #2
@@ -1,6 +1,7 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=A service which scan Nextcloud folders
|
Description=A service which scan Nextcloud folders
|
||||||
After=network.target
|
After=network.target docker.service
|
||||||
|
Requires=docker.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/bin/ncsambawatcher
|
ExecStart=/usr/bin/ncsambawatcher
|
||||||
|
|||||||
167
src/main.cpp
167
src/main.cpp
@@ -1,119 +1,102 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <mutex>
|
||||||
#include <unistd.h>
|
#include <condition_variable>
|
||||||
#include <sys/types.h>
|
#include <cstdio>
|
||||||
#include <syslog.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include "locations.h"
|
|
||||||
#include "usermanager.h"
|
#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="
|
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
|
||||||
|
|
||||||
int p1[2];
|
|
||||||
guarder guard;
|
|
||||||
userManager manager;
|
userManager manager;
|
||||||
|
std::condition_variable cv;
|
||||||
|
std::mutex mtx;
|
||||||
|
|
||||||
void flushManagerToPipe()
|
void readingThreadFunc()
|
||||||
{
|
{
|
||||||
if (!guard.isFlagOn())
|
FILE *logpipe = popen(LOGFILE, "r");
|
||||||
{
|
std::array<char, 256> buffer;
|
||||||
std::vector<std::string> users = manager.getFlaggedUsers();
|
|
||||||
|
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();
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
write(p1[1], &size, sizeof(int));
|
manager.addUser(user);
|
||||||
write(p1[1], it->data(), size * sizeof(char));
|
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);
|
||||||
|
std::vector<pid_t> childrens;
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
flushManagerToPipe();
|
cv.wait(lock, []
|
||||||
|
{ return manager.isAnybodyFlagged(); });
|
||||||
|
|
||||||
|
std::set<std::string> scanUsers = manager.getFlaggedUsers();
|
||||||
|
manager.unflagAllUsers();
|
||||||
|
|
||||||
|
lock.unlock();
|
||||||
|
childrens.clear();
|
||||||
|
|
||||||
|
for (const std::string& user : scanUsers)
|
||||||
|
{
|
||||||
|
pid_t child = fork();
|
||||||
|
|
||||||
|
if (child < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Fork failed for: " << user << std::endl;
|
||||||
|
}
|
||||||
|
else if (child == 0) // child
|
||||||
|
{
|
||||||
|
std::string cmd = (std::string(SCAN_CMD_USR) + user);
|
||||||
|
execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast<char *>(nullptr));
|
||||||
|
std::cerr << "Scan failed" << std::endl;
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
else // parent
|
||||||
|
{
|
||||||
|
childrens.push_back(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const pid_t& pid : childrens)
|
||||||
|
{
|
||||||
|
waitpid(pid, nullptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock.lock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
std::thread readingThread(readingThreadFunc);
|
||||||
pipe(p1);
|
std::thread scannerThread(scannerThreadFunc);
|
||||||
|
|
||||||
pid_t parent = getpid();
|
readingThread.join();
|
||||||
pid_t child = fork();
|
scannerThread.join();
|
||||||
|
|
||||||
if (child > 0) // parent
|
return 0;
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <mutex>
|
||||||
#include "locations.h"
|
#include "locations.h"
|
||||||
|
|
||||||
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
|
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
|
||||||
@@ -13,6 +15,7 @@ class userManager
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::map<std::string, bool> users;
|
std::map<std::string, bool> users;
|
||||||
|
std::mutex mtx;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -23,6 +26,7 @@ public:
|
|||||||
|
|
||||||
void addUser(std::string &user)
|
void addUser(std::string &user)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
if (users.count(user) == 0)
|
if (users.count(user) == 0)
|
||||||
{
|
{
|
||||||
users[user] = false;
|
users[user] = false;
|
||||||
@@ -31,16 +35,19 @@ public:
|
|||||||
|
|
||||||
void removeUser(std::string &user)
|
void removeUser(std::string &user)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
users.erase(user);
|
users.erase(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isContains(std::string &user)
|
bool isContains(std::string &user)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
return users.count(user) == 1;
|
return users.count(user) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUserFlagged(std::string &user)
|
void setUserFlagged(std::string &user)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
if (users.count(user) == 1)
|
if (users.count(user) == 1)
|
||||||
{
|
{
|
||||||
users[user] = true;
|
users[user] = true;
|
||||||
@@ -49,6 +56,7 @@ public:
|
|||||||
|
|
||||||
void setUserUnflagged(std::string &user)
|
void setUserUnflagged(std::string &user)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
if (users.count(user) == 1)
|
if (users.count(user) == 1)
|
||||||
{
|
{
|
||||||
users[user] = false;
|
users[user] = false;
|
||||||
@@ -57,38 +65,56 @@ public:
|
|||||||
|
|
||||||
void unflagAllUsers()
|
void unflagAllUsers()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
{
|
{
|
||||||
it->second = false;
|
it->second = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> getUsers()
|
std::set<std::string> getUsers()
|
||||||
{
|
{
|
||||||
std::vector<std::string> ret;
|
std::set<std::string> ret;
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
|
||||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
{
|
{
|
||||||
ret.push_back(it->first);
|
ret.insert(it->first);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> getFlaggedUsers()
|
std::set<std::string> getFlaggedUsers()
|
||||||
{
|
{
|
||||||
std::vector<std::string> ret;
|
std::set<std::string> ret;
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
|
||||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
{
|
{
|
||||||
if (it->second)
|
if (it->second)
|
||||||
{
|
{
|
||||||
ret.push_back(it->first);
|
ret.insert(it->first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isAnybodyFlagged()
|
||||||
|
{
|
||||||
|
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
|
#endif // _USERMAN_H
|
||||||
Reference in New Issue
Block a user