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
|
#include "monitor_user.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define DEVICE "/dev/variable_monitor"
int file_desc = -1;
/// @brief start watch
/// @param w_arg
/// @return 0 means success, other means fail
int start_watch(watch_arg w_arg) {
if (file_desc < 0) {
file_desc = open(DEVICE, 0);
}
if (file_desc < 0) {
printf("Can't open device file: %s\n", DEVICE);
return -1;
}
if (ioctl(file_desc, 0, &w_arg) < 0) {
printf("ioctl failed\n");
close(file_desc);
return -1;
}
return 0;
}
/// @brief cancel watch
/// @return 0 means success, other means fail
int cancel_watch() {
if (file_desc < 0) {
file_desc = open(DEVICE, 0);
}
if (file_desc < 0) {
printf("Device not open: %s,%d \n", DEVICE, file_desc);
return file_desc;
}
close(file_desc);
file_desc = -1;
return 0;
}
void init_watch_arg(watch_arg *wg, char *name, void *ptr,
int length_byte, long long threshold,
unsigned char is_unsigned,
unsigned char above_threshold, unsigned long time_ns){
wg->task_id = getpid();
strncpy(wg->name, name, (MAX_NAME_LEN + 1));
wg->ptr = ptr;
wg->length_byte = length_byte;
wg->threshold = threshold;
wg->is_unsigned = is_unsigned;
wg->above_threshold = above_threshold;
wg->time_ns = time_ns;
}
|