Send and receive usernames
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,2 +1,2 @@
|
|||||||
all:
|
all:
|
||||||
g++ -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher
|
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher
|
||||||
71
src/main.cpp
71
src/main.cpp
@@ -2,8 +2,11 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#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 <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -13,13 +16,46 @@
|
|||||||
#include "locations.h"
|
#include "locations.h"
|
||||||
#include "usermanager.h"
|
#include "usermanager.h"
|
||||||
|
|
||||||
|
#define MAXNAMESIZE 255
|
||||||
|
#define SCAN_DONE_SIG SIGRTMIN
|
||||||
|
|
||||||
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
||||||
|
|
||||||
int p1[2];
|
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();
|
||||||
|
|
||||||
@@ -27,26 +63,51 @@ int main()
|
|||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
std::string line(buffer.data());
|
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();
|
||||||
|
|
||||||
|
for (auto it = users.begin(); it != users.end(); ++it)
|
||||||
|
{
|
||||||
|
write(p1[1], it->data(), it->size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose(logpipe);
|
||||||
close(p1[1]); // write
|
close(p1[1]); // write
|
||||||
}
|
}
|
||||||
else // child
|
else // child
|
||||||
{
|
{
|
||||||
close(p1[1]); // write
|
close(p1[1]); // write
|
||||||
|
|
||||||
|
std::array<char, MAXNAMESIZE> received;
|
||||||
|
|
||||||
|
while (read(p1[0], received.data(), sizeof(char) * MAXNAMESIZE))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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