blob: 8424271ec579c541c0275badb581026ce7230602 [file] [log] [blame]
Lucas De Marchi8f923be2011-12-03 04:28:49 -02001/*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2008 Alan Jenkins <alan.christopher.jenkins@googlemail.com>
5 * Copyright (C) 2011 ProFUSION embedded systems
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation version 2.1.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
Lucas De Marchie8847fd2011-11-30 15:23:28 -020020
21#include <arpa/inet.h> /* htonl */
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26#include <fnmatch.h>
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -020027#include <assert.h>
Lucas De Marchie8847fd2011-11-30 15:23:28 -020028
29#include "libkmod-private.h"
30#include "libkmod-index.h"
31#include "macro.h"
32
Lucas De Marchi8f923be2011-12-03 04:28:49 -020033/* index.c: module index file shared functions for modprobe and depmod */
34
Gustavo Sverzut Barbieri148226e2011-12-10 11:53:51 -020035#define INDEX_CHILDMAX 128
36
37/* Disk format:
38
39 uint32_t magic = INDEX_MAGIC;
40 uint32_t version = INDEX_VERSION;
41 uint32_t root_offset;
42
43 (node_offset & INDEX_NODE_MASK) specifies the file offset of nodes:
44
45 char[] prefix; // nul terminated
46
47 char first;
48 char last;
49 uint32_t children[last - first + 1];
50
51 uint32_t value_count;
52 struct {
53 uint32_t priority;
54 char[] value; // nul terminated
55 } values[value_count];
56
57 (node_offset & INDEX_NODE_FLAGS) indicates which fields are present.
58 Empty prefixes are omitted, leaf nodes omit the three child-related fields.
59
60 This could be optimised further by adding a sparse child format
61 (indicated using a new flag).
62 */
63
64/* Format of node offsets within index file */
65enum node_offset {
66 INDEX_NODE_FLAGS = 0xF0000000, /* Flags in high nibble */
67 INDEX_NODE_PREFIX = 0x80000000,
68 INDEX_NODE_VALUES = 0x40000000,
69 INDEX_NODE_CHILDS = 0x20000000,
70
71 INDEX_NODE_MASK = 0x0FFFFFFF, /* Offset value */
72};
73
Lucas De Marchie8847fd2011-11-30 15:23:28 -020074void index_values_free(struct index_value *values)
75{
76 while (values) {
77 struct index_value *value = values;
78
79 values = value->next;
80 free(value);
81 }
82}
83
Lucas De Marchie8847fd2011-11-30 15:23:28 -020084static int add_value(struct index_value **values,
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -020085 const char *value, unsigned len, unsigned int priority)
Lucas De Marchie8847fd2011-11-30 15:23:28 -020086{
87 struct index_value *v;
Lucas De Marchie8847fd2011-11-30 15:23:28 -020088
89 /* find position to insert value */
90 while (*values && (*values)->priority < priority)
91 values = &(*values)->next;
92
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -020093 v = malloc(sizeof(struct index_value) + len + 1);
94 if (!v)
95 return -1;
Lucas De Marchie8847fd2011-11-30 15:23:28 -020096 v->next = *values;
97 v->priority = priority;
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -020098 v->len = len;
99 memcpy(v->value, value, len);
100 v->value[len] = '\0';
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200101 *values = v;
102
Gustavo Sverzut Barbieri558b0202011-12-08 16:36:48 -0200103 return 0;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200104}
105
Lucas De Marchi93688882011-12-02 10:05:31 -0200106static void read_error(void)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200107{
108 fatal("Module index: unexpected error: %s\n"
109 "Try re-running depmod\n", errno ? strerror(errno) : "EOF");
110}
111
112static int read_char(FILE *in)
113{
114 int ch;
115
116 errno = 0;
117 ch = getc_unlocked(in);
118 if (ch == EOF)
119 read_error();
120 return ch;
121}
122
123static uint32_t read_long(FILE *in)
124{
125 uint32_t l;
126
127 errno = 0;
128 if (fread(&l, sizeof(uint32_t), 1, in) <= 0)
129 read_error();
130 return ntohl(l);
131}
132
133/*
134 * Buffer abstract data type
135 *
136 * Used internally to store the current path during tree traversal.
137 * They help build wildcard key strings to pass to fnmatch(),
138 * as well as building values of matching keys.
139 */
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200140struct buffer {
141 char *bytes;
142 unsigned size;
143 unsigned used;
144};
145
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200146#define BUF_STEP (2048)
147static bool buf_grow(struct buffer *buf, size_t newsize)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200148{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200149 void *tmp;
150 size_t sz;
151
152 if (newsize % BUF_STEP == 0)
153 sz = newsize;
154 else
155 sz = ((newsize / BUF_STEP) + 1) * BUF_STEP;
156
Gustavo Sverzut Barbieri435ad782011-12-08 16:35:08 -0200157 if (buf->size == sz)
158 return true;
159
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200160 tmp = realloc(buf->bytes, sz);
161 if (sz > 0 && tmp == NULL)
162 return false;
163 buf->bytes = tmp;
164 buf->size = sz;
165 return true;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200166}
167
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200168static void buf_init(struct buffer *buf)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200169{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200170 buf->bytes = NULL;
171 buf->size = 0;
172 buf->used = 0;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200173}
174
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200175static void buf_release(struct buffer *buf)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200176{
177 free(buf->bytes);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200178}
179
180/* Destroy buffer and return a copy as a C string */
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200181static char *buf_steal(struct buffer *buf)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200182{
183 char *bytes;
184
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200185 bytes = realloc(buf->bytes, buf->used + 1);
186 if (!bytes) {
187 free(buf->bytes);
188 return NULL;
189 }
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200190 bytes[buf->used] = '\0';
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200191 return bytes;
192}
193
194/* Return a C string owned by the buffer
195 (invalidated if the buffer is changed).
196 */
197static const char *buf_str(struct buffer *buf)
198{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200199 if (!buf_grow(buf, buf->used + 1))
200 return NULL;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200201 buf->bytes[buf->used] = '\0';
202 return buf->bytes;
203}
204
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200205static bool buf_pushchar(struct buffer *buf, char ch)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200206{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200207 if (!buf_grow(buf, buf->used + 1))
208 return false;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200209 buf->bytes[buf->used] = ch;
210 buf->used++;
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200211 return true;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200212}
213
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200214static unsigned buf_freadchars(struct buffer *buf, FILE *in)
215{
216 unsigned i = 0;
217 int ch;
218
219 while ((ch = read_char(in))) {
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200220 if (!buf_pushchar(buf, ch))
221 break;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200222 i++;
223 }
224
225 return i;
226}
227
228static void buf_popchar(struct buffer *buf)
229{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200230 assert(buf->used > 0);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200231 buf->used--;
232}
233
234static void buf_popchars(struct buffer *buf, unsigned n)
235{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200236 assert(buf->used >= n);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200237 buf->used -= n;
238}
239
240static void buf_clear(struct buffer *buf)
241{
242 buf->used = 0;
243}
244
245/*
Lucas De Marchieb8bb322011-12-02 10:23:02 -0200246 * Index file searching
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200247 */
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200248struct index_node_f {
249 FILE *file;
250 char *prefix; /* path compression */
251 struct index_value *values;
252 unsigned char first; /* range of child nodes */
253 unsigned char last;
254 uint32_t children[0];
255};
256
257static struct index_node_f *index_read(FILE *in, uint32_t offset)
258{
259 struct index_node_f *node;
260 char *prefix;
261 int i, child_count = 0;
262
263 if ((offset & INDEX_NODE_MASK) == 0)
264 return NULL;
265
266 fseek(in, offset & INDEX_NODE_MASK, SEEK_SET);
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200267
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200268 if (offset & INDEX_NODE_PREFIX) {
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200269 struct buffer buf;
270 buf_init(&buf);
271 buf_freadchars(&buf, in);
272 prefix = buf_steal(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200273 } else
274 prefix = NOFAIL(strdup(""));
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200275
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200276 if (offset & INDEX_NODE_CHILDS) {
277 char first = read_char(in);
278 char last = read_char(in);
279 child_count = last - first + 1;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200280
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200281 node = NOFAIL(malloc(sizeof(struct index_node_f) +
282 sizeof(uint32_t) * child_count));
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200283
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200284 node->first = first;
285 node->last = last;
286
287 for (i = 0; i < child_count; i++)
288 node->children[i] = read_long(in);
289 } else {
290 node = NOFAIL(malloc(sizeof(struct index_node_f)));
291 node->first = INDEX_CHILDMAX;
292 node->last = 0;
293 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200294
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200295 node->values = NULL;
296 if (offset & INDEX_NODE_VALUES) {
297 int value_count;
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200298 struct buffer buf;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200299 const char *value;
300 unsigned int priority;
301
302 value_count = read_long(in);
303
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200304 buf_init(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200305 while (value_count--) {
306 priority = read_long(in);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200307 buf_freadchars(&buf, in);
308 value = buf_str(&buf);
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -0200309 add_value(&node->values, value, buf.used, priority);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200310 buf_clear(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200311 }
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200312 buf_release(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200313 }
314
315 node->prefix = prefix;
316 node->file = in;
317 return node;
318}
319
320static void index_close(struct index_node_f *node)
321{
322 free(node->prefix);
323 index_values_free(node->values);
324 free(node);
325}
326
327struct index_file {
328 FILE *file;
329 uint32_t root_offset;
330};
331
332/* Failures are silent; modprobe will fall back to text files */
333struct index_file *index_file_open(const char *filename)
334{
335 FILE *file;
336 uint32_t magic, version;
337 struct index_file *new;
338
339 file = fopen(filename, "r");
340 if (!file)
341 return NULL;
342 errno = EINVAL;
343
344 magic = read_long(file);
345 if (magic != INDEX_MAGIC)
346 return NULL;
347
348 version = read_long(file);
349 if (version >> 16 != INDEX_VERSION_MAJOR)
350 return NULL;
351
352 new = NOFAIL(malloc(sizeof(struct index_file)));
353 new->file = file;
354 new->root_offset = read_long(new->file);
355
356 errno = 0;
357 return new;
358}
359
Lucas De Marchi0fbdfef2011-12-02 09:56:22 -0200360void index_file_close(struct index_file *idx)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200361{
Lucas De Marchi0fbdfef2011-12-02 09:56:22 -0200362 fclose(idx->file);
363 free(idx);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200364}
365
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200366static struct index_node_f *index_readroot(struct index_file *in)
367{
368 return index_read(in->file, in->root_offset);
369}
370
371static struct index_node_f *index_readchild(const struct index_node_f *parent,
372 int ch)
373{
Lucas De Marchie71970a2011-12-02 10:27:15 -0200374 if (parent->first <= ch && ch <= parent->last) {
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200375 return index_read(parent->file,
376 parent->children[ch - parent->first]);
Lucas De Marchie71970a2011-12-02 10:27:15 -0200377 }
378
379 return NULL;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200380}
381
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200382static char *index_search__node(struct index_node_f *node, const char *key, int i)
383{
384 char *value;
385 struct index_node_f *child;
386 int ch;
387 int j;
388
389 while(node) {
390 for (j = 0; node->prefix[j]; j++) {
391 ch = node->prefix[j];
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200392
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200393 if (ch != key[i+j]) {
394 index_close(node);
395 return NULL;
396 }
397 }
Lucas De Marchie71970a2011-12-02 10:27:15 -0200398
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200399 i += j;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200400
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200401 if (key[i] == '\0') {
402 if (node->values) {
403 value = strdup(node->values[0].value);
404 index_close(node);
405 return value;
406 } else {
407 return NULL;
408 }
409 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200410
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200411 child = index_readchild(node, key[i]);
412 index_close(node);
413 node = child;
414 i++;
415 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200416
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200417 return NULL;
418}
419
420/*
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200421 * Search the index for a key
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200422 *
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200423 * Returns the value of the first match
424 *
425 * The recursive functions free their node argument (using index_close).
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200426 */
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200427char *index_search(struct index_file *in, const char *key)
428{
429 struct index_node_f *root;
430 char *value;
431
432 root = index_readroot(in);
433 value = index_search__node(root, key, 0);
434
435 return value;
436}
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200437
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200438
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200439
440/* Level 4: add all the values from a matching node */
441static void index_searchwild__allvalues(struct index_node_f *node,
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200442 struct index_value **out)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200443{
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200444 struct index_value *v;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200445
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200446 for (v = node->values; v != NULL; v = v->next)
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -0200447 add_value(out, v->value, v->len, v->priority);
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200448
449 index_close(node);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200450}
451
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200452/*
453 * Level 3: traverse a sub-keyspace which starts with a wildcard,
454 * looking for matches.
455 */
456static void index_searchwild__all(struct index_node_f *node, int j,
457 struct buffer *buf,
458 const char *subkey,
459 struct index_value **out)
460{
461 int pushed = 0;
462 int ch;
463
464 while (node->prefix[j]) {
465 ch = node->prefix[j];
466
467 buf_pushchar(buf, ch);
468 pushed++;
469 j++;
470 }
471
472 for (ch = node->first; ch <= node->last; ch++) {
473 struct index_node_f *child = index_readchild(node, ch);
474
475 if (!child)
476 continue;
477
478 buf_pushchar(buf, ch);
479 index_searchwild__all(child, 0, buf, subkey, out);
480 buf_popchar(buf);
481 }
482
483 if (node->values) {
484 if (fnmatch(buf_str(buf), subkey, 0) == 0)
485 index_searchwild__allvalues(node, out);
486 } else {
487 index_close(node);
488 }
489
490 buf_popchars(buf, pushed);
491}
492
493/* Level 2: descend the tree (until we hit a wildcard) */
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200494static void index_searchwild__node(struct index_node_f *node,
495 struct buffer *buf,
496 const char *key, int i,
497 struct index_value **out)
498{
499 struct index_node_f *child;
500 int j;
501 int ch;
502
503 while(node) {
504 for (j = 0; node->prefix[j]; j++) {
505 ch = node->prefix[j];
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200506
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200507 if (ch == '*' || ch == '?' || ch == '[') {
508 index_searchwild__all(node, j, buf,
509 &key[i+j], out);
510 return;
511 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200512
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200513 if (ch != key[i+j]) {
514 index_close(node);
515 return;
516 }
517 }
Lucas De Marchie71970a2011-12-02 10:27:15 -0200518
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200519 i += j;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200520
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200521 child = index_readchild(node, '*');
522 if (child) {
523 buf_pushchar(buf, '*');
524 index_searchwild__all(child, 0, buf, &key[i], out);
525 buf_popchar(buf);
526 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200527
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200528 child = index_readchild(node, '?');
529 if (child) {
530 buf_pushchar(buf, '?');
531 index_searchwild__all(child, 0, buf, &key[i], out);
532 buf_popchar(buf);
533 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200534
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200535 child = index_readchild(node, '[');
536 if (child) {
537 buf_pushchar(buf, '[');
538 index_searchwild__all(child, 0, buf, &key[i], out);
539 buf_popchar(buf);
540 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200541
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200542 if (key[i] == '\0') {
543 index_searchwild__allvalues(node, out);
544
545 return;
546 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200547
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200548 child = index_readchild(node, key[i]);
549 index_close(node);
550 node = child;
551 i++;
552 }
553}
554
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200555/*
556 * Search the index for a key. The index may contain wildcards.
557 *
558 * Returns a list of all the values of matching keys.
559 */
560struct index_value *index_searchwild(struct index_file *in, const char *key)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200561{
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200562 struct index_node_f *root = index_readroot(in);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200563 struct buffer buf;
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200564 struct index_value *out = NULL;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200565
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200566 buf_init(&buf);
567 index_searchwild__node(root, &buf, key, 0, &out);
568 buf_release(&buf);
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200569 return out;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200570}
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200571
572#include <sys/mman.h>
573#include <sys/stat.h>
574#include <unistd.h>
575
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200576static const char _idx_empty_str[] = "";
577
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200578/**************************************************************************/
579/*
580 * Alternative implementation, using mmap to map all the file to memory when
581 * starting
582 */
583struct index_mm {
584 struct kmod_ctx *ctx;
585 void *mm;
586 uint32_t root_offset;
587 size_t size;
588};
589
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200590struct index_mm_value {
591 unsigned int priority;
592 unsigned int len;
593 const char *value;
594};
595
596struct index_mm_value_array {
597 struct index_mm_value *values;
598 unsigned int len;
599};
600
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200601struct index_mm_node {
602 struct index_mm *idx;
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200603 const char *prefix; /* mmape'd value */
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200604 struct index_mm_value_array values;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200605 unsigned char first;
606 unsigned char last;
607 uint32_t children[];
608};
609
610static inline uint32_t read_long_mm(void **p)
611{
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200612 uint8_t *addr = *(uint8_t **)p;
613 uint32_t v;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200614
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200615 /* addr may be unalined to uint32_t */
616 memcpy(&v, addr, sizeof(uint32_t));
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200617
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200618 *p = addr + sizeof(uint32_t);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200619 return ntohl(v);
620}
621
622static inline uint8_t read_char_mm(void **p)
623{
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200624 uint8_t *addr = *(uint8_t **)p;
625 uint8_t v = *addr;
626 *p = addr + sizeof(uint8_t);
627 return v;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200628}
629
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -0200630static inline char *read_chars_mm(void **p, unsigned *rlen)
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200631{
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200632 char *addr = *(char **)p;
633 size_t len = *rlen = strlen(addr);
634 *p = addr + len + 1;
635 return addr;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200636}
637
638static struct index_mm_node *index_mm_read_node(struct index_mm *idx,
639 uint32_t offset) {
640 void *p = idx->mm;
641 struct index_mm_node *node;
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200642 const char *prefix;
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200643 int i, child_count, value_count, children_padding;
644 uint32_t children[INDEX_CHILDMAX];
645 char first, last;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200646
647 if ((offset & INDEX_NODE_MASK) == 0)
648 return NULL;
649
650 p = (char *)p + (offset & INDEX_NODE_MASK);
651
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200652 if (offset & INDEX_NODE_PREFIX) {
653 unsigned len;
654 prefix = read_chars_mm(&p, &len);
655 } else
656 prefix = _idx_empty_str;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200657
658 if (offset & INDEX_NODE_CHILDS) {
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200659 first = read_char_mm(&p);
660 last = read_char_mm(&p);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200661 child_count = last - first + 1;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200662 for (i = 0; i < child_count; i++)
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200663 children[i] = read_long_mm(&p);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200664 } else {
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200665 first = INDEX_CHILDMAX;
666 last = 0;
667 child_count = 0;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200668 }
669
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200670 children_padding = (offsetof(struct index_mm_node, children) +
671 (sizeof(uint32_t) * child_count)) % sizeof(void *);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200672
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200673 if (offset & INDEX_NODE_VALUES)
674 value_count = read_long_mm(&p);
675 else
676 value_count = 0;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200677
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200678 node = malloc(sizeof(struct index_mm_node)
679 + sizeof(uint32_t) * child_count + children_padding
680 + sizeof(struct index_mm_value) * value_count);
681 if (node == NULL)
682 return NULL;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200683
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200684 node->idx = idx;
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200685 node->prefix = prefix;
686 if (value_count == 0)
687 node->values.values = NULL;
688 else {
689 node->values.values = (struct index_mm_value *)
690 ((char *)node + sizeof(struct index_mm_node) +
691 sizeof(uint32_t) * child_count + children_padding);
692 }
693 node->values.len = value_count;
694 node->first = first;
695 node->last = last;
696 memcpy(node->children, children, sizeof(uint32_t) * child_count);
697
698 for (i = 0; i < value_count; i++) {
699 struct index_mm_value *v = node->values.values + i;
700 v->priority = read_long_mm(&p);
701 v->value = read_chars_mm(&p, &v->len);
702 }
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200703
704 return node;
705}
706
707static void index_mm_free_node(struct index_mm_node *node)
708{
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200709 free(node);
710}
711
Lucas De Marchi5109f2b2011-12-08 15:10:55 -0200712struct index_mm *index_mm_open(struct kmod_ctx *ctx, const char *filename,
713 bool populate)
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200714{
715 int fd;
Lucas De Marchi5109f2b2011-12-08 15:10:55 -0200716 int flags;
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200717 struct stat st;
718 struct index_mm *idx;
719 struct {
720 uint32_t magic;
721 uint32_t version;
722 uint32_t root_offset;
723 } hdr;
724 void *p;
725
726 DBG(ctx, "file=%s\n", filename);
727
728 if ((fd = open(filename, O_RDONLY)) < 0) {
729 ERR(ctx, "%m\n");
730 return NULL;
731 }
732
733 fstat(fd, &st);
734
735 idx = malloc(sizeof(*idx));
736 if (idx == NULL) {
737 ERR(ctx, "%m\n");
738 goto fail;
739 }
740
Lucas De Marchi5109f2b2011-12-08 15:10:55 -0200741 flags = MAP_PRIVATE;
742 if (populate)
743 flags |= MAP_POPULATE;
744
745 if ((idx->mm = mmap(0, st.st_size, PROT_READ, flags, fd, 0))
746 == MAP_FAILED) {
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200747 ERR(ctx, "%m\n");
748 goto fail;
749 }
750
751 p = idx->mm;
752 hdr.magic = read_long_mm(&p);
753 hdr.version = read_long_mm(&p);
754 hdr.root_offset = read_long_mm(&p);
755
756 if (hdr.magic != INDEX_MAGIC) {
757 ERR(ctx, "magic check fail: %x instead of %x\n", hdr.magic,
758 INDEX_MAGIC);
759 goto fail;
760 }
761
762 if (hdr.version >> 16 != INDEX_VERSION_MAJOR) {
763 ERR(ctx, "major version check fail: %u instead of %u\n",
764 hdr.version, INDEX_MAGIC);
765 goto fail;
766 }
767
768 idx->root_offset = hdr.root_offset;
769 idx->size = st.st_size;
770 idx->ctx = ctx;
771 close(fd);
772
773 return idx;
774
775fail:
776 close(fd);
777 if (idx->mm)
778 munmap(idx->mm, st.st_size);
779 free(idx);
780 return NULL;
781}
782
783void index_mm_close(struct index_mm *idx)
784{
785 munmap(idx->mm, idx->size);
786 free(idx);
787}
Lucas De Marchi77bf9362011-12-02 17:41:30 -0200788
789static struct index_mm_node *index_mm_readroot(struct index_mm *idx)
790{
791 return index_mm_read_node(idx, idx->root_offset);
792}
Lucas De Marchi91298dc2011-12-02 17:41:46 -0200793
794static struct index_mm_node *index_mm_readchild(const struct index_mm_node *parent,
795 int ch)
796{
797 if (parent->first <= ch && ch <= parent->last) {
798 return index_mm_read_node(parent->idx,
799 parent->children[ch - parent->first]);
800 }
801
802 return NULL;
803}
Lucas De Marchie33bb872011-12-02 17:45:01 -0200804
805static char *index_mm_search_node(struct index_mm_node *node, const char *key,
806 int i)
807{
808 char *value;
809 struct index_mm_node *child;
810 int ch;
811 int j;
812
813 while(node) {
814 for (j = 0; node->prefix[j]; j++) {
815 ch = node->prefix[j];
816
817 if (ch != key[i+j]) {
818 index_mm_free_node(node);
819 return NULL;
820 }
821 }
822
823 i += j;
824
825 if (key[i] == '\0') {
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200826 if (node->values.len > 0) {
827 value = strdup(node->values.values[0].value);
Lucas De Marchie33bb872011-12-02 17:45:01 -0200828 index_mm_free_node(node);
829 return value;
830 } else {
831 return NULL;
832 }
833 }
834
835 child = index_mm_readchild(node, key[i]);
836 index_mm_free_node(node);
837 node = child;
838 i++;
839 }
840
841 return NULL;
842}
Lucas De Marchib797b792011-12-02 17:49:03 -0200843
844/*
845 * Search the index for a key
846 *
847 * Returns the value of the first match
848 *
849 * The recursive functions free their node argument (using index_close).
850 */
851char *index_mm_search(struct index_mm *idx, const char *key)
852{
853 struct index_mm_node *root;
854 char *value;
855
856 root = index_mm_readroot(idx);
857 value = index_mm_search_node(root, key, 0);
858
859 return value;
860}
Lucas De Marchibf89f702011-12-02 18:23:36 -0200861
862/* Level 4: add all the values from a matching node */
863static void index_mm_searchwild_allvalues(struct index_mm_node *node,
864 struct index_value **out)
865{
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200866 struct index_mm_value *itr, *itr_end;
Lucas De Marchibf89f702011-12-02 18:23:36 -0200867
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200868 itr = node->values.values;
869 itr_end = itr + node->values.len;
870 for (; itr < itr_end; itr++)
871 add_value(out, itr->value, itr->len, itr->priority);
Lucas De Marchibf89f702011-12-02 18:23:36 -0200872
873 index_mm_free_node(node);
874}
875
876/*
877 * Level 3: traverse a sub-keyspace which starts with a wildcard,
878 * looking for matches.
879 */
880static void index_mm_searchwild_all(struct index_mm_node *node, int j,
881 struct buffer *buf,
882 const char *subkey,
883 struct index_value **out)
884{
885 int pushed = 0;
886 int ch;
887
888 while (node->prefix[j]) {
889 ch = node->prefix[j];
890
891 buf_pushchar(buf, ch);
892 pushed++;
893 j++;
894 }
895
896 for (ch = node->first; ch <= node->last; ch++) {
897 struct index_mm_node *child = index_mm_readchild(node, ch);
898
899 if (!child)
900 continue;
901
902 buf_pushchar(buf, ch);
903 index_mm_searchwild_all(child, 0, buf, subkey, out);
904 buf_popchar(buf);
905 }
906
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200907 if (node->values.len > 0) {
Lucas De Marchibf89f702011-12-02 18:23:36 -0200908 if (fnmatch(buf_str(buf), subkey, 0) == 0)
909 index_mm_searchwild_allvalues(node, out);
910 } else {
911 index_mm_free_node(node);
912 }
913
914 buf_popchars(buf, pushed);
915}
916
917/* Level 2: descend the tree (until we hit a wildcard) */
918static void index_mm_searchwild_node(struct index_mm_node *node,
919 struct buffer *buf,
920 const char *key, int i,
921 struct index_value **out)
922{
923 struct index_mm_node *child;
924 int j;
925 int ch;
926
927 while(node) {
928 for (j = 0; node->prefix[j]; j++) {
929 ch = node->prefix[j];
930
931 if (ch == '*' || ch == '?' || ch == '[') {
932 index_mm_searchwild_all(node, j, buf,
933 &key[i+j], out);
934 return;
935 }
936
937 if (ch != key[i+j]) {
938 index_mm_free_node(node);
939 return;
940 }
941 }
942
943 i += j;
944
945 child = index_mm_readchild(node, '*');
946 if (child) {
947 buf_pushchar(buf, '*');
948 index_mm_searchwild_all(child, 0, buf, &key[i], out);
949 buf_popchar(buf);
950 }
951
952 child = index_mm_readchild(node, '?');
953 if (child) {
954 buf_pushchar(buf, '?');
955 index_mm_searchwild_all(child, 0, buf, &key[i], out);
956 buf_popchar(buf);
957 }
958
959 child = index_mm_readchild(node, '[');
960 if (child) {
961 buf_pushchar(buf, '[');
962 index_mm_searchwild_all(child, 0, buf, &key[i], out);
963 buf_popchar(buf);
964 }
965
966 if (key[i] == '\0') {
967 index_mm_searchwild_allvalues(node, out);
968
969 return;
970 }
971
972 child = index_mm_readchild(node, key[i]);
973 index_mm_free_node(node);
974 node = child;
975 i++;
976 }
977}
978
979/*
980 * Search the index for a key. The index may contain wildcards.
981 *
982 * Returns a list of all the values of matching keys.
983 */
984struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key)
985{
986 struct index_mm_node *root = index_mm_readroot(idx);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200987 struct buffer buf;
Lucas De Marchibf89f702011-12-02 18:23:36 -0200988 struct index_value *out = NULL;
989
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200990 buf_init(&buf);
991 index_mm_searchwild_node(root, &buf, key, 0, &out);
992 buf_release(&buf);
Lucas De Marchibf89f702011-12-02 18:23:36 -0200993 return out;
994}