forked from Streptoco/EmbeddedProgrammingInLinux
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsignal_handler.cpp
More file actions
54 lines (43 loc) · 1.37 KB
/
signal_handler.cpp
File metadata and controls
54 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <csignal>
#include <unistd.h>
#include "signal_handler.h"
#include "pipe_handler.h"
#include "zip.h"
using namespace std;
pid_t signal_handler::childPid = 0;
void signal_handler::handleSignalChild(int signal)
{
if (signal == SIGINT)
{
cout << "zipping database upon closure" << endl;
zip_t *archive = zip_open("archive.zip", ZIP_CREATE | ZIP_TRUNCATE, NULL);
filesystem::path currentPath = filesystem::current_path().parent_path();
cout << "current path: " << currentPath << endl;
if (!archive) {
throw runtime_error("Failed to open a zip file. Demolishing proccess!");
}
addFilesToArchive(archive, currentPath.string(), "");
zip_close(archive);
exit(0);
}
exit(signal);
}
void signal_handler::handleSignalParent(int signal) {
cout << "Parent process received interrupt signal: " << signal << endl;
if(signal == SIGINT && childPid != 0) {
kill(childPid, signal);
int status;
waitpid(childPid, &status, 0);
if (WIFEXITED(status)) {
std::cout << "Child process ended normally.\n";
exit(0);
} else {
std::cout << "Child process ended abnormally.\n";
}
}
exit(signal);
}
void signal_handler::setChildPid(int childPid) {
signal_handler::childPid = childPid;
}