-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_proc_lab_ring.h
More file actions
121 lines (97 loc) · 3.06 KB
/
Copy pathkernel_proc_lab_ring.h
File metadata and controls
121 lines (97 loc) · 3.06 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef KERNEL_PROC_LAB_RING_H
#define KERNEL_PROC_LAB_RING_H
#include <linux/types.h>
#ifdef __KERNEL__
#include <linux/string.h>
#else
#include <stdbool.h>
#include <string.h>
typedef __u32 u32;
typedef __u64 u64;
static inline void kernel_proc_lab_strscpy(char *dest, const char *src,
size_t size)
{
size_t len;
if (size == 0)
return;
len = strnlen(src, size - 1);
memcpy(dest, src, len);
dest[len] = '\0';
}
#define strscpy(dest, src, size) kernel_proc_lab_strscpy(dest, src, size)
#endif
#include "kernel_proc_lab_ioctl.h"
static inline u32 kernel_proc_lab_ring_index(u64 sequence, u32 capacity)
{
return (u32)((sequence - 1) % capacity);
}
static inline u64 kernel_proc_lab_ring_retained_start(u64 latest_sequence,
u32 count)
{
if (count == 0)
return latest_sequence + 1;
return latest_sequence - count + 1;
}
static inline void kernel_proc_lab_ring_snapshot(
const struct kernel_proc_lab_log_entry *entries, u32 capacity, u32 count,
u64 latest_sequence, struct kernel_proc_lab_log_snapshot *snapshot)
{
u64 first_sequence;
u32 snapshot_count;
u32 index;
memset(snapshot, 0, sizeof(*snapshot));
snapshot_count = count;
if (snapshot_count > KERNEL_PROC_LAB_USER_LOG_CAPACITY)
snapshot_count = KERNEL_PROC_LAB_USER_LOG_CAPACITY;
snapshot->count = snapshot_count;
if (snapshot_count == 0)
return;
first_sequence = latest_sequence - snapshot_count + 1;
for (index = 0; index < snapshot_count; index += 1) {
u64 sequence = first_sequence + index;
u32 source = kernel_proc_lab_ring_index(sequence, capacity);
snapshot->entries[index] = entries[source];
}
}
static inline bool kernel_proc_lab_ring_copy_entry(
const struct kernel_proc_lab_log_entry *entries, u32 capacity, u32 count,
u64 latest_sequence, u64 sequence,
struct kernel_proc_lab_log_entry *entry)
{
u64 first_sequence;
u32 source_index;
if (count == 0)
return false;
first_sequence = kernel_proc_lab_ring_retained_start(latest_sequence, count);
if (sequence < first_sequence || sequence > latest_sequence)
return false;
source_index = kernel_proc_lab_ring_index(sequence, capacity);
if (entries[source_index].seq != sequence)
return false;
*entry = entries[source_index];
return true;
}
static inline void kernel_proc_lab_ring_fill_entry(
struct kernel_proc_lab_log_entry *entries, u32 capacity, u64 sequence,
u64 timestamp_ns, u32 type, u32 pid, u32 uid, const char *comm,
const char *message)
{
u32 index = kernel_proc_lab_ring_index(sequence, capacity);
entries[index].seq = sequence;
entries[index].timestamp_ns = timestamp_ns;
entries[index].type = type;
entries[index].pid = pid;
entries[index].uid = uid;
entries[index].reserved = 0;
strscpy(entries[index].comm, comm, KERNEL_PROC_LAB_COMM_MAX);
strscpy(entries[index].message, message, KERNEL_PROC_LAB_MESSAGE_MAX);
}
static inline void kernel_proc_lab_ring_fill_message(
struct kernel_proc_lab_log_entry *entries, u32 capacity, u64 sequence,
const char *message)
{
kernel_proc_lab_ring_fill_entry(entries, capacity, sequence, 0,
KERNEL_PROC_LAB_EVENT_MESSAGE, 0, 0,
"", message);
}
#endif