Create guard for flush

This commit is contained in:
2025-05-26 14:04:15 +02:00
parent e4c8ec9621
commit 6e9ac56a3c
4 changed files with 84 additions and 44 deletions

View File

@@ -1,2 +1,2 @@
all: all:
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp src/guarder.cpp -o ncsambawatcher

1
src/guarder.cpp Normal file
View File

@@ -0,0 +1 @@
#include "guarder.h"

50
src/guarder.h Normal file
View File

@@ -0,0 +1,50 @@
#ifndef _GUARDER_H
#define _GUARDER_H
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
class guarder{
private:
const int shmid;
void setFlag(bool value)
{
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
*flag = value;
shmdt(flag);
}
public:
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT))
{
setFlagOff();
}
void setFlagOff()
{
setFlag(false);
}
void setFlagOn()
{
setFlag(true);
}
bool isFlagOn()
{
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
bool ret = *flag;
shmdt(flag);
return ret;
}
~guarder()
{
shmctl(shmid, IPC_RMID, nullptr);
}
};
#endif // _GUARDER_H

View File

@@ -1,85 +1,28 @@
#include <iostream> #include <iostream>
#include <array> #include <array>
#include <string> #include <string>
#include <map>
#include <vector> #include <vector>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <syslog.h> #include <syslog.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h>
#include "locations.h" #include "locations.h"
#include "usermanager.h" #include "usermanager.h"
#include "guarder.h"
#define MAXNAMESIZE 255 #define MAXNAMESIZE 255
#define SCAN_DONE_SIG SIGRTMIN #define SCAN_DONE_SIG SIGRTMIN
#define SCAN_CMD_USR "docker exec -ti --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="
void initRunningFlag(int shmid)
{
bool *init = static_cast<bool *>(shmat(shmid, nullptr, 0));
*init = false;
shmdt(init);
}
bool readRunningFlag(int shmid)
{
bool *init = static_cast<bool *>(shmat(shmid, nullptr, 0));
*init = false;
shmdt(init);
return *init;
}
void setRunningFlag(int shmid, bool data)
{
bool *init = static_cast<bool *>(shmat(shmid, nullptr, 0));
*init = data;
shmdt(init);
}
void handler(int sig) {}
int main()
{
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
int p1[2]; int p1[2];
pipe(p1); guarder guard;
sigset_t ss;
sigfillset(&ss);
sigdelset(&ss, SCAN_DONE_SIG);
int shmid = shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT);
initRunningFlag(shmid);
pid_t parent = getpid();
pid_t child = fork();
if (child > 0) // parent
{
close(p1[0]); // read
FILE *logpipe = popen(LOGFILE, "r");
std::array<char, 256> buffer;
userManager manager; userManager manager;
while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr) void flushManagerToPipe()
{
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));
manager.addUser(user);
manager.setUserFlagged(user);
std::vector<std::string> users = manager.getFlaggedUsers(); std::vector<std::string> users = manager.getFlaggedUsers();
for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it) for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it)
@@ -88,6 +31,50 @@ int main()
write(p1[1], &size, sizeof(int)); write(p1[1], &size, sizeof(int));
write(p1[1], it->data(), size * sizeof(char)); 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;
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);
perror("");
flushManagerToPipe();
} }
fclose(logpipe); fclose(logpipe);
@@ -108,20 +95,22 @@ int main()
read(p1[0], buffer, size * sizeof(char)); read(p1[0], buffer, size * sizeof(char));
std::string name(buffer); std::string name(buffer);
guard.setFlagOn();
system((std::string(SCAN_CMD_USR) + name).data()); system((std::string(SCAN_CMD_USR) + name).data());
guard.setFlagOff();
kill(parent, SCAN_DONE_SIG);
if (buffer != nullptr) if (buffer != nullptr)
{ {
delete[] buffer; delete[] buffer;
buffer = nullptr; buffer = nullptr;
} }
} }
close(p1[0]); // read close(p1[0]); // read
} }
shmctl(shmid, IPC_RMID, nullptr);
closelog(); closelog();
return EXIT_SUCCESS; return EXIT_SUCCESS;