-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.cpp
98 lines (81 loc) · 3.02 KB
/
signals.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <csignal>
#include "signals.h"
#include "Commands.h"
using namespace std;
void ctrlZHandler(int sig_num) {
cout << "smash: got ctrl-Z" << endl;
// if CURR_FORK_CHILD_RUNNING == 0 do nothing
if (CURR_FORK_CHILD_RUNNING == 0) return;
// send SIGSTOP to CURR_FORK_CHILD_RUNNING
pid_t gpid = getpgid(CURR_FORK_CHILD_RUNNING);
if (gpid < 0) {
perror("smash error: getgpid failed");
return;
}
// send signal, print message
if (killpg(gpid, SIGSTOP) < 0) {
perror("smash error: killpg failed");
} else {
cout << "smash: process " << CURR_FORK_CHILD_RUNNING << " was stopped" << endl;
}
}
void ctrlCHandler(int sig_num) {
cout << "smash: got ctrl-C" << endl;
// if CURR_FORK_CHILD_RUNNING == 0 do nothing
if (CURR_FORK_CHILD_RUNNING == 0) return;
// send SIGSTOP to CURR_FORK_CHILD_RUNNING
pid_t gpid = getpgid(CURR_FORK_CHILD_RUNNING);
if (gpid < 0) {
perror("smash error: getgpid failed");
return;
}
// send signal, print message
if (killpg(gpid, SIGKILL) < 0) {
perror("smash error: killpg failed");
} else {
cout << "smash: process " << CURR_FORK_CHILD_RUNNING << " was killed" << endl;
}
}
void alarmHandler(int sig_num) {
// print message (it's ok to print once because multiple alarm in the same time won't be tested)
cout << "smash: got an alarm" << endl;
// clean jobs list
GLOBAL_JOBS_POINTER->removeFinishedJobs();
// get curr time
time_t curr_time = time(nullptr);
if (curr_time == (time_t)(-1)) {
perror("smash error: time failed");
return;
}
// iterate the job list and find timeout commands
TIME_UNTIL_NEXT_ALARM = numeric_limits<double>::max();
map<JobID,JobEntry>& jobs = GLOBAL_JOBS_POINTER->jobs;
for (auto& job : jobs) {
if (!job.second.is_timeout || job.second.pid == 0) continue;
double time_remain = (double)job.second.time_limit - difftime(curr_time, job.second.original_start_time);
if (time_remain < 1.0) {
// get group pid
pid_t gpid = getpgid(job.second.pid);
if (gpid < 0) {
perror("smash error: getgpid failed");
continue;
}
// send signal, print message
if (killpg(gpid, SIGKILL) < 0) {
perror("smash error: killpg failed");
} else {
cout << "smash: " << job.second.cmd_str << " timed out!" << endl;
job.second.is_timeout = false; // make sure that we don't SIGKILL a job twice
}
} else if ((unsigned int)time_remain < TIME_UNTIL_NEXT_ALARM) {
// TIME_UNTIL_NEXT_ALARM = minimum timeout leftover duration
TIME_UNTIL_NEXT_ALARM = time_remain;
}
}
// send another alarm for the next timeout command
if (TIME_UNTIL_NEXT_ALARM < numeric_limits<double>::max()) {
alarm((unsigned int)TIME_UNTIL_NEXT_ALARM);
TIME_AT_LAST_UPDATE = curr_time;
}
}