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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#ifndef SIEVE_BINARY_PRIVATE_H
#define SIEVE_BINARY_PRIVATE_H
#include "sieve-common.h"
#include "sieve-binary.h"
#include "sieve-extensions.h"
#include <sys/stat.h>
#define SIEVE_BINARY_FILE_LOCK_TIMEOUT 10
/*
* Binary file
*/
enum SIEVE_BINARY_FLAGS {
SIEVE_BINARY_FLAG_RESOURCE_LIMIT = BIT(0),
};
struct sieve_binary_header {
uint32_t magic;
uint16_t version_major;
uint16_t version_minor;
uint32_t blocks;
uint32_t hdr_size;
uint32_t flags;
struct {
uint64_t update_time;
uint32_t cpu_time_msecs;
} resource_usage;
};
struct sieve_binary_file {
pool_t pool;
const char *path;
struct sieve_binary *sbin;
struct stat st;
int fd;
off_t offset;
};
void sieve_binary_file_close(struct sieve_binary_file **_file);
/*
* Internal structures
*/
/* Extension registration */
struct sieve_binary_extension_reg {
/* The identifier of the extension within this binary */
int index;
/* Global extension object */
const struct sieve_extension *extension;
/* Extension to the binary; typically used to manage extension-specific
blocks in the binary and as a means to get a binary_free notification
to release references held by extensions.
*/
const struct sieve_binary_extension *binext;
/* Context data associated to the binary by this extension */
void *context;
/* Main block for this extension */
unsigned int block_id;
};
/* Block */
struct sieve_binary_block {
struct sieve_binary *sbin;
unsigned int id;
int ext_index;
buffer_t *data;
uoff_t offset;
};
/*
* Binary object
*/
struct sieve_binary {
pool_t pool;
int refcount;
struct sieve_instance *svinst;
struct event *event;
struct sieve_script *script;
struct sieve_binary_file *file;
struct sieve_binary_header header;
struct sieve_resource_usage rusage;
/* When the binary is loaded into memory or when it is being constructed
by the generator, extensions can be associated to the binary. The
extensions array is a sequential list of all linked extensions. The
extension_index array is a mapping ext_id -> binary_extension. This
is used to obtain the index code associated with an extension for
this particular binary. The linked_extensions list all extensions
linked to this binary object other than the preloaded language
features implemented as 'extensions'.
All arrays refer to the same extension registration objects. Upon
loading a binary, the 'require'd extensions will sometimes need to
associate context data to the binary object in memory. This is stored
in these registration objects as well.
*/
ARRAY(struct sieve_binary_extension_reg *) extensions;
ARRAY(struct sieve_binary_extension_reg *) extension_index;
ARRAY(struct sieve_binary_extension_reg *) linked_extensions;
/* Attributes of a loaded binary */
const char *path;
/* Blocks */
ARRAY(struct sieve_binary_block *) blocks;
bool rusage_updated:1;
};
void sieve_binary_update_event(struct sieve_binary *sbin, const char *new_path)
ATTR_NULL(2);
struct sieve_binary *
sieve_binary_create(struct sieve_instance *svinst, struct sieve_script *script);
/* Blocks management */
static inline struct sieve_binary_block *
sieve_binary_block_index(struct sieve_binary *sbin, unsigned int id)
{
struct sieve_binary_block * const *sblock;
if (id >= array_count(&sbin->blocks))
return NULL;
sblock = array_idx(&sbin->blocks, id);
if (*sblock == NULL)
return NULL;
return *sblock;
}
static inline size_t
_sieve_binary_block_get_size(const struct sieve_binary_block *sblock)
{
return buffer_get_used_size(sblock->data);
}
struct sieve_binary_block *
sieve_binary_block_create_id(struct sieve_binary *sbin, unsigned int id);
buffer_t *sieve_binary_block_get_buffer(struct sieve_binary_block *sblock);
/* Extension registration */
static inline struct sieve_binary_extension_reg *
sieve_binary_extension_create_reg(struct sieve_binary *sbin,
const struct sieve_extension *ext)
{
int index = array_count(&sbin->extensions);
struct sieve_binary_extension_reg *ereg;
if (ext->id < 0)
return NULL;
ereg = p_new(sbin->pool, struct sieve_binary_extension_reg, 1);
ereg->index = index;
ereg->extension = ext;
array_idx_set(&sbin->extensions, (unsigned int) index, &ereg);
array_idx_set(&sbin->extension_index, (unsigned int) ext->id, &ereg);
return ereg;
}
static inline struct sieve_binary_extension_reg *
sieve_binary_extension_get_reg(struct sieve_binary *sbin,
const struct sieve_extension *ext,
bool create)
{
struct sieve_binary_extension_reg *reg = NULL;
if (ext->id >= 0 &&
ext->id < (int)array_count(&sbin->extension_index)) {
struct sieve_binary_extension_reg * const *ereg =
array_idx(&sbin->extension_index,
(unsigned int)ext->id);
reg = *ereg;
}
/* Register if not known */
if (reg == NULL && create)
return sieve_binary_extension_create_reg(sbin, ext);
return reg;
}
static inline int
sieve_binary_extension_register(struct sieve_binary *sbin,
const struct sieve_extension *ext,
struct sieve_binary_extension_reg **reg_r)
{
struct sieve_binary_extension_reg *ereg;
if ((ereg = sieve_binary_extension_get_reg(sbin, ext, FALSE)) == NULL) {
ereg = sieve_binary_extension_create_reg(sbin, ext);
if (ereg == NULL)
return -1;
array_append(&sbin->linked_extensions, &ereg, 1);
}
if (reg_r != NULL)
*reg_r = ereg;
return ereg->index;
}
/* Load/Save */
bool sieve_binary_load_block(struct sieve_binary_block *);
/*
* Resource limits
*/
bool sieve_binary_check_resource_usage(struct sieve_binary *sbin);
int sieve_binary_file_update_resource_usage(struct sieve_binary *sbin,
enum sieve_error *error_r)
ATTR_NULL(2);
#endif
|