Compare commits
1 Commits
1113a21b2d
...
multi-proc
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c441cde92 |
@@ -1,7 +1,6 @@
|
||||
[Unit]
|
||||
Description=A service which scan Nextcloud folders
|
||||
After=network.target docker.service
|
||||
Requires=docker.service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/bin/ncsambawatcher
|
||||
|
||||
@@ -4,37 +4,26 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/sem.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class guarder{
|
||||
private:
|
||||
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)
|
||||
{
|
||||
semaphoreOp(-1);
|
||||
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
|
||||
*flag = value;
|
||||
shmdt(flag);
|
||||
semaphoreOp(1);
|
||||
}
|
||||
|
||||
public:
|
||||
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT)), semid(semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT))
|
||||
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT))
|
||||
{
|
||||
setFlagOff();
|
||||
}
|
||||
|
||||
guarder(guarder &g) : shmid(g.shmid) {}
|
||||
|
||||
void setFlagOff()
|
||||
{
|
||||
setFlag(false);
|
||||
@@ -47,18 +36,15 @@ public:
|
||||
|
||||
bool isFlagOn()
|
||||
{
|
||||
semaphoreOp(-1);
|
||||
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
|
||||
bool ret = *flag;
|
||||
shmdt(flag);
|
||||
semaphoreOp(1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
~guarder()
|
||||
{
|
||||
shmctl(shmid, IPC_RMID, nullptr);
|
||||
semctl(semid, 0, IPC_RMID);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
166
src/main.cpp
166
src/main.cpp
@@ -1,102 +1,120 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <syslog.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include "locations.h"
|
||||
#include "usermanager.h"
|
||||
#include "guarder.h"
|
||||
#include "processManager.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 readingThreadFunc()
|
||||
void flushManagerToPipe()
|
||||
{
|
||||
FILE *logpipe = popen(LOGFILE, "r");
|
||||
std::array<char, 256> buffer;
|
||||
|
||||
while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
|
||||
if (!guard.isFlagOn())
|
||||
{
|
||||
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));
|
||||
std::vector<std::string> users = manager.getFlaggedUsers();
|
||||
|
||||
for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
manager.addUser(user);
|
||||
manager.setUserFlagged(user);
|
||||
int size = it->size();
|
||||
write(p1[1], &size, sizeof(int));
|
||||
write(p1[1], it->data(), size * sizeof(char));
|
||||
}
|
||||
|
||||
cv.notify_one();
|
||||
|
||||
std::cout << "User find: " << user << std::endl;
|
||||
manager.unflagAllUsers();
|
||||
}
|
||||
}
|
||||
|
||||
void scannerThreadFunc()
|
||||
void handler(int sig)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
std::vector<pid_t> childrens;
|
||||
while (true)
|
||||
if (sig == SCAN_DONE_SIG)
|
||||
{
|
||||
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();
|
||||
flushManagerToPipe();
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::thread readingThread(readingThreadFunc);
|
||||
std::thread scannerThread(scannerThreadFunc);
|
||||
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
||||
pipe(p1);
|
||||
|
||||
readingThread.join();
|
||||
scannerThread.join();
|
||||
pid_t parent = getpid();
|
||||
pid_t child = fork();
|
||||
|
||||
return 0;
|
||||
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;
|
||||
processManager pm;
|
||||
|
||||
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::string cmd = std::string(SCAN_CMD_USR) + name;
|
||||
|
||||
std::cout << "Scan received for: " << name << std::endl;
|
||||
|
||||
pm.runTask(name, cmd);
|
||||
|
||||
kill(parent, SCAN_DONE_SIG);
|
||||
|
||||
if (buffer != nullptr)
|
||||
{
|
||||
delete[] buffer;
|
||||
buffer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
close(p1[0]); // read
|
||||
}
|
||||
|
||||
closelog();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
41
src/processManager.h
Normal file
41
src/processManager.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef _PROCCESSMANAGER_H
|
||||
#define _PROCCESSMANAGER_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include "guarder.h"
|
||||
#include "usermanager.h"
|
||||
|
||||
class processManager
|
||||
{
|
||||
private:
|
||||
std::map<std::string, guarder> running;
|
||||
|
||||
bool getIdTag(std::string &id)
|
||||
{
|
||||
return running[id].isFlagOn();
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
void runTask(std::string &id, std::string &cmdCommand)
|
||||
{
|
||||
if (!getIdTag(id))
|
||||
{
|
||||
running[id].setFlagOn();
|
||||
pid_t child = fork();
|
||||
|
||||
if (child == 0) // child
|
||||
{
|
||||
system(cmdCommand.c_str());
|
||||
running[id].setFlagOff();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _PROCCESSMANAGER_H
|
||||
@@ -4,9 +4,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <mutex>
|
||||
#include "locations.h"
|
||||
|
||||
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
|
||||
@@ -15,7 +13,6 @@ class userManager
|
||||
{
|
||||
private:
|
||||
std::map<std::string, bool> users;
|
||||
std::mutex mtx;
|
||||
|
||||
public:
|
||||
|
||||
@@ -26,7 +23,6 @@ public:
|
||||
|
||||
void addUser(std::string &user)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
if (users.count(user) == 0)
|
||||
{
|
||||
users[user] = false;
|
||||
@@ -35,19 +31,16 @@ 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;
|
||||
@@ -56,7 +49,6 @@ public:
|
||||
|
||||
void setUserUnflagged(std::string &user)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
if (users.count(user) == 1)
|
||||
{
|
||||
users[user] = false;
|
||||
@@ -65,56 +57,38 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
std::set<std::string> getUsers()
|
||||
std::vector<std::string> getUsers()
|
||||
{
|
||||
std::set<std::string> ret;
|
||||
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
std::vector<std::string> ret;
|
||||
|
||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||
{
|
||||
ret.insert(it->first);
|
||||
ret.push_back(it->first);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::set<std::string> getFlaggedUsers()
|
||||
std::vector<std::string> getFlaggedUsers()
|
||||
{
|
||||
std::set<std::string> ret;
|
||||
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
std::vector<std::string> ret;
|
||||
|
||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||
{
|
||||
if (it->second)
|
||||
{
|
||||
ret.insert(it->first);
|
||||
ret.push_back(it->first);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user