-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgvm_notify.cpp
More file actions
50 lines (42 loc) · 1.02 KB
/
Copy pathgvm_notify.cpp
File metadata and controls
50 lines (42 loc) · 1.02 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
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <sys/ioctl.h>
#include "gvm_notify.h"
#include "uvm_utils.h"
static gvm_notice_fn g_handler;
static volatile bool g_active;
static pthread_t g_notify_thread;
static void *notify_thread_fn(void *) {
UVM_WAIT_NOTICE_PARAMS params;
while (g_active) {
memset(¶ms, 0, sizeof(params));
if (ioctl(g_uvmfd, UVM_WAIT_NOTICE, ¶ms) != 0) {
if (errno == EINTR)
continue;
break;
}
g_handler(¶ms);
}
return nullptr;
}
int gvm_register_notify(gvm_notice_fn handler) {
if (init_uvmfd() != 0) {
fprintf(stderr, "gvm_register_notify: init_uvmfd failed\n");
return -1;
}
g_handler = handler;
g_active = true;
if (pthread_create(&g_notify_thread, nullptr, notify_thread_fn, nullptr) != 0) {
g_active = false;
perror("gvm_register_notify: thread create failed");
return -1;
}
return 0;
}
void gvm_unregister_notify() {
g_active = false;
pthread_cancel(g_notify_thread);
pthread_join(g_notify_thread, nullptr);
}