6
6
#include <stdio.h>
7
7
#include <unistd.h>
8
8
#include <pwd.h>
9
+ #include <time.h>
10
+ #include <locale.h>
11
+ #include <signal.h>
9
12
#include <bpf/bpf.h>
10
13
#include "xcapture.h"
11
14
#include "xcapture.skel.h"
12
15
#include <syscall_names.h>
13
- #include <time.h>
14
- #include <locale.h>
16
+
17
+
18
+ // handle CTRL+C and sigpipe etc
19
+ static volatile bool exiting = false;
20
+
21
+ static void sig_handler (int sig )
22
+ {
23
+ exiting = true;
24
+ }
25
+
15
26
16
27
// translate uid to user name
17
28
const char * getusername (uid_t uid )
@@ -91,6 +102,12 @@ int main(int argc, char **argv)
91
102
ssize_t ret = 0 ;
92
103
int err = 0 ;
93
104
105
+ /* Signal handling */
106
+ signal (SIGINT , sig_handler );
107
+ signal (SIGTERM , sig_handler );
108
+ signal (SIGPIPE , sig_handler );
109
+
110
+
94
111
/* For number formatting for readability */
95
112
setlocale (LC_ALL ,"en_US.UTF-8" );
96
113
@@ -115,7 +132,7 @@ int main(int argc, char **argv)
115
132
bool header_printed = false;
116
133
117
134
// sample and print every second
118
- while (true ) {
135
+ while (! exiting ) {
119
136
clock_gettime (CLOCK_REALTIME , & sample_ts );
120
137
// clock_gettime(CLOCK_MONOTONIC, &ktime); // TODO check twice and pick lowest diff in case of an interrupt/inv ctx switch
121
138
@@ -225,10 +242,10 @@ int main(int argc, char **argv)
225
242
226
243
// sleep for 1 second for now (even if prev sample took some time)
227
244
// TODO sleep N microseconds less, based on how long the last sample took (like in original v1 xcapture.c)
228
- usleep (1000000 );
245
+ if (!exiting )
246
+ usleep (1000000 );
229
247
}
230
248
231
-
232
249
cleanup :
233
250
/* Clean up */
234
251
fflush (stdout );
0 commit comments