Compare commits
2 Commits
e4c8ec9621
...
v0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 743fe964fb | |||
| 6e9ac56a3c |
2
Makefile
2
Makefile
@@ -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
|
||||||
17
configs/ncsambawatcher.service
Normal file
17
configs/ncsambawatcher.service
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=A service which scan Nextcloud folders
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/bin/ncsambawatcher
|
||||||
|
Restart=always
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
WorkingDirectory=/usr/bin/
|
||||||
|
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||||
|
StandardOutput=journal
|
||||||
|
StandardError=journal
|
||||||
|
SyslogIdentifier=ncsambawatcher
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
12
init.sh
Executable file
12
init.sh
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
make
|
||||||
|
|
||||||
|
sudo cp ncsambawatcher /usr/bin/
|
||||||
|
sudo chmod +x /usr/bin/ncsambawatcher
|
||||||
|
|
||||||
|
sudo cp configs/ncsambawatcher.service /etc/systemd/system
|
||||||
|
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
sudo systemctl enable ncsambawatcher.service
|
||||||
|
sudo systemctl start ncsambawatcher.service
|
||||||
1
src/guarder.cpp
Normal file
1
src/guarder.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "guarder.h"
|
||||||
50
src/guarder.h
Normal file
50
src/guarder.h
Normal 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
|
||||||
77
src/main.cpp
77
src/main.cpp
@@ -1,71 +1,64 @@
|
|||||||
#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)
|
int p1[2];
|
||||||
|
guarder guard;
|
||||||
|
userManager manager;
|
||||||
|
|
||||||
|
void flushManagerToPipe()
|
||||||
{
|
{
|
||||||
bool *init = static_cast<bool *>(shmat(shmid, nullptr, 0));
|
if (!guard.isFlagOn())
|
||||||
*init = false;
|
{
|
||||||
shmdt(init);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readRunningFlag(int shmid)
|
void handler(int sig)
|
||||||
{
|
{
|
||||||
bool *init = static_cast<bool *>(shmat(shmid, nullptr, 0));
|
if (sig == SCAN_DONE_SIG)
|
||||||
*init = false;
|
{
|
||||||
shmdt(init);
|
flushManagerToPipe();
|
||||||
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()
|
int main()
|
||||||
{
|
{
|
||||||
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
||||||
|
|
||||||
int p1[2];
|
|
||||||
pipe(p1);
|
pipe(p1);
|
||||||
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 parent = getpid();
|
||||||
pid_t child = fork();
|
pid_t child = fork();
|
||||||
|
|
||||||
if (child > 0) // parent
|
if (child > 0) // parent
|
||||||
{
|
{
|
||||||
|
signal(SCAN_DONE_SIG, handler);
|
||||||
close(p1[0]); // read
|
close(p1[0]); // read
|
||||||
|
|
||||||
FILE *logpipe = popen(LOGFILE, "r");
|
FILE *logpipe = popen(LOGFILE, "r");
|
||||||
std::array<char, 256> buffer;
|
std::array<char, 256> buffer;
|
||||||
userManager manager;
|
|
||||||
|
|
||||||
while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
|
while (fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
|
||||||
{
|
{
|
||||||
@@ -74,20 +67,14 @@ int main()
|
|||||||
if (line.find('|') == std::string::npos)
|
if (line.find('|') == std::string::npos)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
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));
|
||||||
manager.addUser(user);
|
manager.addUser(user);
|
||||||
manager.setUserFlagged(user);
|
manager.setUserFlagged(user);
|
||||||
|
|
||||||
std::vector<std::string> users = manager.getFlaggedUsers();
|
std::cout << "User find: " << user << std::endl;
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it)
|
flushManagerToPipe();
|
||||||
{
|
|
||||||
int size = it->size();
|
|
||||||
write(p1[1], &size, sizeof(int));
|
|
||||||
write(p1[1], it->data(), size * sizeof(char));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(logpipe);
|
fclose(logpipe);
|
||||||
@@ -108,20 +95,24 @@ int main()
|
|||||||
read(p1[0], buffer, size * sizeof(char));
|
read(p1[0], buffer, size * sizeof(char));
|
||||||
std::string name(buffer);
|
std::string name(buffer);
|
||||||
|
|
||||||
|
std::cout << "Scan received for: " << name << std::endl;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user