4 Commits

3 changed files with 87 additions and 93 deletions

View File

@@ -4,20 +4,33 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
class guarder{ class guarder{
private: private:
const int shmid; const int shmid;
const int semid;
void semaphoreOp(int op) {
struct sembuf sb{};
sb.sem_num = 0;
sb.sem_op = op;
sb.sem_flg = 0;
semop(semid, &sb, 1);
}
void setFlag(bool value) void setFlag(bool value)
{ {
semaphoreOp(-1);
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0)); bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
*flag = value; *flag = value;
shmdt(flag); shmdt(flag);
semaphoreOp(1);
} }
public: public:
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT)) guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT)), semid(semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT))
{ {
setFlagOff(); setFlagOff();
} }
@@ -34,15 +47,18 @@ public:
bool isFlagOn() bool isFlagOn()
{ {
semaphoreOp(-1);
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0)); bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
bool ret = *flag; bool ret = *flag;
shmdt(flag); shmdt(flag);
semaphoreOp(1);
return ret; return ret;
} }
~guarder() ~guarder()
{ {
shmctl(shmid, IPC_RMID, nullptr); shmctl(shmid, IPC_RMID, nullptr);
semctl(semid, 0, IPC_RMID);
} }
}; };

View File

@@ -1,62 +1,22 @@
#include <stdio.h>
#include <iostream> #include <iostream>
#include <thread>
#include <vector>
#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())
{
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"); FILE *logpipe = popen(LOGFILE, "r");
std::array<char, 256> buffer; std::array<char, 256> buffer;
@@ -69,51 +29,44 @@ int main()
std::vector<std::string> x = splitLogFile(line, '|'); std::vector<std::string> x = splitLogFile(line, '|');
std::string user(x.at(USER_LOG_LOCATION)); std::string user(x.at(USER_LOG_LOCATION));
{
std::lock_guard<std::mutex> lock(mtx);
manager.addUser(user); manager.addUser(user);
manager.setUserFlagged(user); manager.setUserFlagged(user);
}
cv.notify_one();
std::cout << "User find: " << user << std::endl; std::cout << "User find: " << user << std::endl;
flushManagerToPipe();
} }
}
fclose(logpipe);
close(p1[1]); // write void scannerThreadFunc()
} {
else // child std::unique_lock<std::mutex> lock(mtx);
{ while (true)
close(p1[1]); // write {
cv.wait(lock, []
int size; { return manager.isAnybodyInQueue(); });
char *buffer = nullptr;
std::vector<std::string> scanUsers = manager.getFlaggedUsers();
while (read(p1[0], &size, sizeof(int))) manager.unflagAllUsers();
{
if (buffer == nullptr) for (std::vector<std::string>::iterator it = scanUsers.begin(); it != scanUsers.end(); it++)
buffer = new char[size]; {
system((std::string(SCAN_CMD_USR) + *it).c_str());
read(p1[0], buffer, size * sizeof(char)); }
std::string name(buffer); }
}
std::cout << "Scan received for: " << name << std::endl;
int main()
guard.setFlagOn(); {
system((std::string(SCAN_CMD_USR) + name).data()); std::thread readingThread(readingThreadFunc);
guard.setFlagOff(); std::thread scannerThread(scannerThreadFunc);
kill(parent, SCAN_DONE_SIG); readingThread.join();
scannerThread.join();
if (buffer != nullptr)
{ return 0;
delete[] buffer;
buffer = nullptr;
}
}
close(p1[0]); // read
}
closelog();
return EXIT_SUCCESS;
} }

View File

@@ -5,6 +5,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
#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 +14,7 @@ class userManager
{ {
private: private:
std::map<std::string, bool> users; std::map<std::string, bool> users;
std::mutex mtx;
public: public:
@@ -23,6 +25,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 +34,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 +55,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,6 +64,7 @@ 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;
@@ -67,6 +75,8 @@ public:
{ {
std::vector<std::string> ret; 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) for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{ {
ret.push_back(it->first); ret.push_back(it->first);
@@ -79,6 +89,8 @@ public:
{ {
std::vector<std::string> ret; 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) for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{ {
if (it->second) if (it->second)
@@ -89,6 +101,19 @@ public:
return ret; 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 #endif // _USERMAN_H