blob: 516240e1b1cdbbc8cb5d316d426366dd52d2bb74 [file] [log] [blame]
Lucas De Marchi8f923be2011-12-03 04:28:49 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchi8f923be2011-12-03 04:28:49 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f923be2011-12-03 04:28:49 -020010 *
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 Marchibfcd31d2012-03-02 21:28:11 -030028#include <inttypes.h>
Lucas De Marchie8847fd2011-11-30 15:23:28 -020029
30#include "libkmod-private.h"
31#include "libkmod-index.h"
32#include "macro.h"
33
Lucas De Marchi8f923be2011-12-03 04:28:49 -020034/* index.c: module index file shared functions for modprobe and depmod */
35
Gustavo Sverzut Barbieri148226e2011-12-10 11:53:51 -020036#define INDEX_CHILDMAX 128
37
38/* Disk format:
39
40 uint32_t magic = INDEX_MAGIC;
41 uint32_t version = INDEX_VERSION;
42 uint32_t root_offset;
43
44 (node_offset & INDEX_NODE_MASK) specifies the file offset of nodes:
45
46 char[] prefix; // nul terminated
47
48 char first;
49 char last;
50 uint32_t children[last - first + 1];
51
52 uint32_t value_count;
53 struct {
54 uint32_t priority;
55 char[] value; // nul terminated
56 } values[value_count];
57
58 (node_offset & INDEX_NODE_FLAGS) indicates which fields are present.
59 Empty prefixes are omitted, leaf nodes omit the three child-related fields.
60
61 This could be optimised further by adding a sparse child format
62 (indicated using a new flag).
63 */
64
65/* Format of node offsets within index file */
66enum node_offset {
67 INDEX_NODE_FLAGS = 0xF0000000, /* Flags in high nibble */
68 INDEX_NODE_PREFIX = 0x80000000,
69 INDEX_NODE_VALUES = 0x40000000,
70 INDEX_NODE_CHILDS = 0x20000000,
71
72 INDEX_NODE_MASK = 0x0FFFFFFF, /* Offset value */
73};
74
Lucas De Marchie8847fd2011-11-30 15:23:28 -020075void index_values_free(struct index_value *values)
76{
77 while (values) {
78 struct index_value *value = values;
79
80 values = value->next;
81 free(value);
82 }
83}
84
Lucas De Marchie8847fd2011-11-30 15:23:28 -020085static int add_value(struct index_value **values,
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -020086 const char *value, unsigned len, unsigned int priority)
Lucas De Marchie8847fd2011-11-30 15:23:28 -020087{
88 struct index_value *v;
Lucas De Marchie8847fd2011-11-30 15:23:28 -020089
90 /* find position to insert value */
91 while (*values && (*values)->priority < priority)
92 values = &(*values)->next;
93
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -020094 v = malloc(sizeof(struct index_value) + len + 1);
95 if (!v)
96 return -1;
Lucas De Marchie8847fd2011-11-30 15:23:28 -020097 v->next = *values;
98 v->priority = priority;
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -020099 v->len = len;
100 memcpy(v->value, value, len);
101 v->value[len] = '\0';
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200102 *values = v;
103
Gustavo Sverzut Barbieri558b0202011-12-08 16:36:48 -0200104 return 0;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200105}
106
Lucas De Marchi93688882011-12-02 10:05:31 -0200107static void read_error(void)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200108{
109 fatal("Module index: unexpected error: %s\n"
110 "Try re-running depmod\n", errno ? strerror(errno) : "EOF");
111}
112
113static int read_char(FILE *in)
114{
115 int ch;
116
117 errno = 0;
118 ch = getc_unlocked(in);
119 if (ch == EOF)
120 read_error();
121 return ch;
122}
123
124static uint32_t read_long(FILE *in)
125{
126 uint32_t l;
127
128 errno = 0;
129 if (fread(&l, sizeof(uint32_t), 1, in) <= 0)
130 read_error();
131 return ntohl(l);
132}
133
134/*
135 * Buffer abstract data type
136 *
137 * Used internally to store the current path during tree traversal.
138 * They help build wildcard key strings to pass to fnmatch(),
139 * as well as building values of matching keys.
140 */
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200141struct buffer {
142 char *bytes;
143 unsigned size;
144 unsigned used;
145};
146
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200147#define BUF_STEP (2048)
148static bool buf_grow(struct buffer *buf, size_t newsize)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200149{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200150 void *tmp;
151 size_t sz;
152
153 if (newsize % BUF_STEP == 0)
154 sz = newsize;
155 else
156 sz = ((newsize / BUF_STEP) + 1) * BUF_STEP;
157
Gustavo Sverzut Barbieri435ad782011-12-08 16:35:08 -0200158 if (buf->size == sz)
159 return true;
160
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200161 tmp = realloc(buf->bytes, sz);
162 if (sz > 0 && tmp == NULL)
163 return false;
164 buf->bytes = tmp;
165 buf->size = sz;
166 return true;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200167}
168
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200169static void buf_init(struct buffer *buf)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200170{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200171 buf->bytes = NULL;
172 buf->size = 0;
173 buf->used = 0;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200174}
175
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200176static void buf_release(struct buffer *buf)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200177{
178 free(buf->bytes);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200179}
180
181/* Destroy buffer and return a copy as a C string */
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200182static char *buf_steal(struct buffer *buf)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200183{
184 char *bytes;
185
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200186 bytes = realloc(buf->bytes, buf->used + 1);
187 if (!bytes) {
188 free(buf->bytes);
189 return NULL;
190 }
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200191 bytes[buf->used] = '\0';
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200192 return bytes;
193}
194
195/* Return a C string owned by the buffer
196 (invalidated if the buffer is changed).
197 */
198static const char *buf_str(struct buffer *buf)
199{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200200 if (!buf_grow(buf, buf->used + 1))
201 return NULL;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200202 buf->bytes[buf->used] = '\0';
203 return buf->bytes;
204}
205
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200206static bool buf_pushchar(struct buffer *buf, char ch)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200207{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200208 if (!buf_grow(buf, buf->used + 1))
209 return false;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200210 buf->bytes[buf->used] = ch;
211 buf->used++;
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200212 return true;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200213}
214
Lucas De Marchi758428a2012-01-16 15:56:17 -0200215static unsigned buf_pushchars(struct buffer *buf, const char *str)
216{
217 unsigned i = 0;
218 int ch;
219
220 while ((ch = str[i])) {
221 buf_pushchar(buf, ch);
222 i++;
223 }
224
225 return i;
226}
227
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200228static unsigned buf_freadchars(struct buffer *buf, FILE *in)
229{
230 unsigned i = 0;
231 int ch;
232
233 while ((ch = read_char(in))) {
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200234 if (!buf_pushchar(buf, ch))
235 break;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200236 i++;
237 }
238
239 return i;
240}
241
242static void buf_popchar(struct buffer *buf)
243{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200244 assert(buf->used > 0);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200245 buf->used--;
246}
247
248static void buf_popchars(struct buffer *buf, unsigned n)
249{
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200250 assert(buf->used >= n);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200251 buf->used -= n;
252}
253
254static void buf_clear(struct buffer *buf)
255{
256 buf->used = 0;
257}
258
259/*
Lucas De Marchieb8bb322011-12-02 10:23:02 -0200260 * Index file searching
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200261 */
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200262struct index_node_f {
263 FILE *file;
264 char *prefix; /* path compression */
265 struct index_value *values;
266 unsigned char first; /* range of child nodes */
267 unsigned char last;
268 uint32_t children[0];
269};
270
271static struct index_node_f *index_read(FILE *in, uint32_t offset)
272{
273 struct index_node_f *node;
274 char *prefix;
275 int i, child_count = 0;
276
277 if ((offset & INDEX_NODE_MASK) == 0)
278 return NULL;
279
280 fseek(in, offset & INDEX_NODE_MASK, SEEK_SET);
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200281
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200282 if (offset & INDEX_NODE_PREFIX) {
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200283 struct buffer buf;
284 buf_init(&buf);
285 buf_freadchars(&buf, in);
286 prefix = buf_steal(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200287 } else
288 prefix = NOFAIL(strdup(""));
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200289
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200290 if (offset & INDEX_NODE_CHILDS) {
291 char first = read_char(in);
292 char last = read_char(in);
293 child_count = last - first + 1;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200294
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200295 node = NOFAIL(malloc(sizeof(struct index_node_f) +
296 sizeof(uint32_t) * child_count));
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200297
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200298 node->first = first;
299 node->last = last;
300
301 for (i = 0; i < child_count; i++)
302 node->children[i] = read_long(in);
303 } else {
304 node = NOFAIL(malloc(sizeof(struct index_node_f)));
305 node->first = INDEX_CHILDMAX;
306 node->last = 0;
307 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200308
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200309 node->values = NULL;
310 if (offset & INDEX_NODE_VALUES) {
311 int value_count;
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200312 struct buffer buf;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200313 const char *value;
314 unsigned int priority;
315
316 value_count = read_long(in);
317
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200318 buf_init(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200319 while (value_count--) {
320 priority = read_long(in);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200321 buf_freadchars(&buf, in);
322 value = buf_str(&buf);
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -0200323 add_value(&node->values, value, buf.used, priority);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200324 buf_clear(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200325 }
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200326 buf_release(&buf);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200327 }
328
329 node->prefix = prefix;
330 node->file = in;
331 return node;
332}
333
334static void index_close(struct index_node_f *node)
335{
336 free(node->prefix);
337 index_values_free(node->values);
338 free(node);
339}
340
341struct index_file {
342 FILE *file;
343 uint32_t root_offset;
344};
345
346/* Failures are silent; modprobe will fall back to text files */
347struct index_file *index_file_open(const char *filename)
348{
349 FILE *file;
350 uint32_t magic, version;
351 struct index_file *new;
352
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -0300353 file = fopen(filename, "re");
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200354 if (!file)
355 return NULL;
356 errno = EINVAL;
357
358 magic = read_long(file);
Cristian Rodríguez67d94ad2011-12-23 03:06:56 -0200359 if (magic != INDEX_MAGIC) {
360 fclose(file);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200361 return NULL;
Cristian Rodríguez67d94ad2011-12-23 03:06:56 -0200362 }
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200363
364 version = read_long(file);
Cristian Rodríguez4088b272011-12-26 01:38:04 -0300365 if (version >> 16 != INDEX_VERSION_MAJOR) {
366 fclose(file);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200367 return NULL;
Cristian Rodríguez4088b272011-12-26 01:38:04 -0300368 }
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200369
370 new = NOFAIL(malloc(sizeof(struct index_file)));
371 new->file = file;
372 new->root_offset = read_long(new->file);
373
374 errno = 0;
375 return new;
376}
377
Lucas De Marchi0fbdfef2011-12-02 09:56:22 -0200378void index_file_close(struct index_file *idx)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200379{
Lucas De Marchi0fbdfef2011-12-02 09:56:22 -0200380 fclose(idx->file);
381 free(idx);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200382}
383
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200384static struct index_node_f *index_readroot(struct index_file *in)
385{
386 return index_read(in->file, in->root_offset);
387}
388
389static struct index_node_f *index_readchild(const struct index_node_f *parent,
390 int ch)
391{
Lucas De Marchie71970a2011-12-02 10:27:15 -0200392 if (parent->first <= ch && ch <= parent->last) {
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200393 return index_read(parent->file,
394 parent->children[ch - parent->first]);
Lucas De Marchie71970a2011-12-02 10:27:15 -0200395 }
396
397 return NULL;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200398}
399
Lucas De Marchi758428a2012-01-16 15:56:17 -0200400static void index_dump_node(struct index_node_f *node, struct buffer *buf,
401 int fd)
402{
403 struct index_value *v;
404 int ch, pushed;
405
406 pushed = buf_pushchars(buf, node->prefix);
407
408 for (v = node->values; v != NULL; v = v->next) {
409 write_str_safe(fd, buf->bytes, buf->used);
410 write_str_safe(fd, " ", 1);
411 write_str_safe(fd, v->value, strlen(v->value));
412 write_str_safe(fd, "\n", 1);
413 }
414
415 for (ch = node->first; ch <= node->last; ch++) {
416 struct index_node_f *child = index_readchild(node, ch);
417
418 if (!child)
419 continue;
420
421 buf_pushchar(buf, ch);
422 index_dump_node(child, buf, fd);
423 buf_popchar(buf);
424 }
425
426 buf_popchars(buf, pushed);
427 index_close(node);
428}
429
430void index_dump(struct index_file *in, int fd, const char *prefix)
431{
432 struct index_node_f *root;
433 struct buffer buf;
434
435 buf_init(&buf);
436 buf_pushchars(&buf, prefix);
437 root = index_readroot(in);
438 index_dump_node(root, &buf, fd);
439 buf_release(&buf);
440}
441
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200442static char *index_search__node(struct index_node_f *node, const char *key, int i)
443{
444 char *value;
445 struct index_node_f *child;
446 int ch;
447 int j;
448
449 while(node) {
450 for (j = 0; node->prefix[j]; j++) {
451 ch = node->prefix[j];
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200452
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200453 if (ch != key[i+j]) {
454 index_close(node);
455 return NULL;
456 }
457 }
Lucas De Marchie71970a2011-12-02 10:27:15 -0200458
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200459 i += j;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200460
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200461 if (key[i] == '\0') {
Lucas De Marchi817f4e32012-02-27 19:54:33 -0300462 value = node->values != NULL
463 ? strdup(node->values[0].value)
464 : NULL;
465
466 index_close(node);
467 return value;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200468 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200469
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200470 child = index_readchild(node, key[i]);
471 index_close(node);
472 node = child;
473 i++;
474 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200475
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200476 return NULL;
477}
478
479/*
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200480 * Search the index for a key
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200481 *
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200482 * Returns the value of the first match
483 *
484 * The recursive functions free their node argument (using index_close).
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200485 */
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200486char *index_search(struct index_file *in, const char *key)
487{
Lucas De Marchiee1d1882012-02-27 18:48:02 -0300488// FIXME: return value by reference instead of strdup
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200489 struct index_node_f *root;
490 char *value;
491
492 root = index_readroot(in);
493 value = index_search__node(root, key, 0);
494
495 return value;
496}
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200497
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200498
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200499
500/* Level 4: add all the values from a matching node */
501static void index_searchwild__allvalues(struct index_node_f *node,
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200502 struct index_value **out)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200503{
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200504 struct index_value *v;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200505
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200506 for (v = node->values; v != NULL; v = v->next)
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -0200507 add_value(out, v->value, v->len, v->priority);
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200508
509 index_close(node);
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200510}
511
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200512/*
513 * Level 3: traverse a sub-keyspace which starts with a wildcard,
514 * looking for matches.
515 */
516static void index_searchwild__all(struct index_node_f *node, int j,
517 struct buffer *buf,
518 const char *subkey,
519 struct index_value **out)
520{
521 int pushed = 0;
522 int ch;
523
524 while (node->prefix[j]) {
525 ch = node->prefix[j];
526
527 buf_pushchar(buf, ch);
528 pushed++;
529 j++;
530 }
531
532 for (ch = node->first; ch <= node->last; ch++) {
533 struct index_node_f *child = index_readchild(node, ch);
534
535 if (!child)
536 continue;
537
538 buf_pushchar(buf, ch);
539 index_searchwild__all(child, 0, buf, subkey, out);
540 buf_popchar(buf);
541 }
542
543 if (node->values) {
544 if (fnmatch(buf_str(buf), subkey, 0) == 0)
545 index_searchwild__allvalues(node, out);
Gustavo Sverzut Barbieri27fdf632011-12-10 13:28:18 -0200546 else
547 index_close(node);
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200548 } else {
549 index_close(node);
550 }
551
552 buf_popchars(buf, pushed);
553}
554
555/* Level 2: descend the tree (until we hit a wildcard) */
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200556static void index_searchwild__node(struct index_node_f *node,
557 struct buffer *buf,
558 const char *key, int i,
559 struct index_value **out)
560{
561 struct index_node_f *child;
562 int j;
563 int ch;
564
565 while(node) {
566 for (j = 0; node->prefix[j]; j++) {
567 ch = node->prefix[j];
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200568
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200569 if (ch == '*' || ch == '?' || ch == '[') {
570 index_searchwild__all(node, j, buf,
571 &key[i+j], out);
572 return;
573 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200574
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200575 if (ch != key[i+j]) {
576 index_close(node);
577 return;
578 }
579 }
Lucas De Marchie71970a2011-12-02 10:27:15 -0200580
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200581 i += j;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200582
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200583 child = index_readchild(node, '*');
584 if (child) {
585 buf_pushchar(buf, '*');
586 index_searchwild__all(child, 0, buf, &key[i], out);
587 buf_popchar(buf);
588 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200589
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200590 child = index_readchild(node, '?');
591 if (child) {
592 buf_pushchar(buf, '?');
593 index_searchwild__all(child, 0, buf, &key[i], out);
594 buf_popchar(buf);
595 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200596
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200597 child = index_readchild(node, '[');
598 if (child) {
599 buf_pushchar(buf, '[');
600 index_searchwild__all(child, 0, buf, &key[i], out);
601 buf_popchar(buf);
602 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200603
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200604 if (key[i] == '\0') {
605 index_searchwild__allvalues(node, out);
606
607 return;
608 }
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200609
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200610 child = index_readchild(node, key[i]);
611 index_close(node);
612 node = child;
613 i++;
614 }
615}
616
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200617/*
618 * Search the index for a key. The index may contain wildcards.
619 *
620 * Returns a list of all the values of matching keys.
621 */
622struct index_value *index_searchwild(struct index_file *in, const char *key)
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200623{
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200624 struct index_node_f *root = index_readroot(in);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200625 struct buffer buf;
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200626 struct index_value *out = NULL;
Lucas De Marchia7be73b2011-11-30 15:59:36 -0200627
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -0200628 buf_init(&buf);
629 index_searchwild__node(root, &buf, key, 0, &out);
630 buf_release(&buf);
Lucas De Marchi1d152ac2011-12-02 10:15:00 -0200631 return out;
Lucas De Marchie8847fd2011-11-30 15:23:28 -0200632}
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200633
634#include <sys/mman.h>
635#include <sys/stat.h>
636#include <unistd.h>
637
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200638static const char _idx_empty_str[] = "";
639
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200640/**************************************************************************/
641/*
642 * Alternative implementation, using mmap to map all the file to memory when
643 * starting
644 */
645struct index_mm {
646 struct kmod_ctx *ctx;
647 void *mm;
648 uint32_t root_offset;
649 size_t size;
650};
651
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200652struct index_mm_value {
653 unsigned int priority;
654 unsigned int len;
655 const char *value;
656};
657
658struct index_mm_value_array {
659 struct index_mm_value *values;
660 unsigned int len;
661};
662
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200663struct index_mm_node {
664 struct index_mm *idx;
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200665 const char *prefix; /* mmape'd value */
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200666 struct index_mm_value_array values;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200667 unsigned char first;
668 unsigned char last;
669 uint32_t children[];
670};
671
672static inline uint32_t read_long_mm(void **p)
673{
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200674 uint8_t *addr = *(uint8_t **)p;
675 uint32_t v;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200676
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200677 /* addr may be unalined to uint32_t */
Lucas De Marchi5bbec8c2012-05-23 19:32:58 -0300678 v = get_unaligned((uint32_t *) addr);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200679
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200680 *p = addr + sizeof(uint32_t);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200681 return ntohl(v);
682}
683
684static inline uint8_t read_char_mm(void **p)
685{
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200686 uint8_t *addr = *(uint8_t **)p;
687 uint8_t v = *addr;
688 *p = addr + sizeof(uint8_t);
689 return v;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200690}
691
Gustavo Sverzut Barbieri1433ba92011-12-08 14:50:29 -0200692static inline char *read_chars_mm(void **p, unsigned *rlen)
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200693{
Gustavo Sverzut Barbierifc2d8352011-12-10 11:36:35 -0200694 char *addr = *(char **)p;
695 size_t len = *rlen = strlen(addr);
696 *p = addr + len + 1;
697 return addr;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200698}
699
700static struct index_mm_node *index_mm_read_node(struct index_mm *idx,
701 uint32_t offset) {
702 void *p = idx->mm;
703 struct index_mm_node *node;
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200704 const char *prefix;
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200705 int i, child_count, value_count, children_padding;
706 uint32_t children[INDEX_CHILDMAX];
707 char first, last;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200708
709 if ((offset & INDEX_NODE_MASK) == 0)
710 return NULL;
711
712 p = (char *)p + (offset & INDEX_NODE_MASK);
713
Gustavo Sverzut Barbieri15c1c142011-12-10 11:44:31 -0200714 if (offset & INDEX_NODE_PREFIX) {
715 unsigned len;
716 prefix = read_chars_mm(&p, &len);
717 } else
718 prefix = _idx_empty_str;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200719
720 if (offset & INDEX_NODE_CHILDS) {
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200721 first = read_char_mm(&p);
722 last = read_char_mm(&p);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200723 child_count = last - first + 1;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200724 for (i = 0; i < child_count; i++)
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200725 children[i] = read_long_mm(&p);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200726 } else {
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200727 first = INDEX_CHILDMAX;
728 last = 0;
729 child_count = 0;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200730 }
731
Gustavo Sverzut Barbieric6824b62011-12-21 18:23:58 -0200732 children_padding = (sizeof(struct index_mm_node) +
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200733 (sizeof(uint32_t) * child_count)) % sizeof(void *);
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200734
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200735 if (offset & INDEX_NODE_VALUES)
736 value_count = read_long_mm(&p);
737 else
738 value_count = 0;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200739
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200740 node = malloc(sizeof(struct index_mm_node)
741 + sizeof(uint32_t) * child_count + children_padding
742 + sizeof(struct index_mm_value) * value_count);
743 if (node == NULL)
744 return NULL;
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200745
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200746 node->idx = idx;
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200747 node->prefix = prefix;
748 if (value_count == 0)
749 node->values.values = NULL;
750 else {
751 node->values.values = (struct index_mm_value *)
752 ((char *)node + sizeof(struct index_mm_node) +
753 sizeof(uint32_t) * child_count + children_padding);
754 }
755 node->values.len = value_count;
756 node->first = first;
757 node->last = last;
758 memcpy(node->children, children, sizeof(uint32_t) * child_count);
759
760 for (i = 0; i < value_count; i++) {
761 struct index_mm_value *v = node->values.values + i;
762 v->priority = read_long_mm(&p);
763 v->value = read_chars_mm(&p, &v->len);
764 }
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200765
766 return node;
767}
768
769static void index_mm_free_node(struct index_mm_node *node)
770{
Lucas De Marchi836be9a2011-12-02 17:27:52 -0200771 free(node);
772}
773
Lucas De Marchi5109f2b2011-12-08 15:10:55 -0200774struct index_mm *index_mm_open(struct kmod_ctx *ctx, const char *filename,
Lucas De Marchi2e2e2522012-03-02 20:33:26 -0300775 unsigned long long *stamp)
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200776{
777 int fd;
778 struct stat st;
779 struct index_mm *idx;
780 struct {
781 uint32_t magic;
782 uint32_t version;
783 uint32_t root_offset;
784 } hdr;
785 void *p;
786
787 DBG(ctx, "file=%s\n", filename);
788
Lucas De Marchi7d51d8b2011-12-12 18:41:57 -0200789 idx = malloc(sizeof(*idx));
790 if (idx == NULL) {
Gustavo Sverzut Barbieridfa96f12012-01-07 19:37:37 -0200791 ERR(ctx, "malloc: %m\n");
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200792 return NULL;
793 }
794
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -0300795 if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) < 0) {
Lucas De Marchi73298172012-02-13 21:58:36 -0200796 DBG(ctx, "open(%s, O_RDONLY|O_CLOEXEC): %m\n", filename);
Lucas De Marchi7d51d8b2011-12-12 18:41:57 -0200797 goto fail_open;
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200798 }
799
Lucas De Marchi7d51d8b2011-12-12 18:41:57 -0200800 fstat(fd, &st);
Lucas De Marchi5b05c322012-06-06 09:36:29 -0300801 if ((size_t) st.st_size < sizeof(hdr))
802 goto fail_nommap;
Lucas De Marchi5109f2b2011-12-08 15:10:55 -0200803
Lucas De Marchi2e2e2522012-03-02 20:33:26 -0300804 if ((idx->mm = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
Lucas De Marchi5109f2b2011-12-08 15:10:55 -0200805 == MAP_FAILED) {
Lucas De Marchibfcd31d2012-03-02 21:28:11 -0300806 ERR(ctx, "mmap(0, %"PRIu64", PROT_READ, %d, MAP_PRIVATE, 0): %m\n",
Lucas De Marchi2e2e2522012-03-02 20:33:26 -0300807 st.st_size, fd);
Lucas De Marchi5b05c322012-06-06 09:36:29 -0300808 goto fail_nommap;
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200809 }
810
811 p = idx->mm;
812 hdr.magic = read_long_mm(&p);
813 hdr.version = read_long_mm(&p);
814 hdr.root_offset = read_long_mm(&p);
815
816 if (hdr.magic != INDEX_MAGIC) {
817 ERR(ctx, "magic check fail: %x instead of %x\n", hdr.magic,
818 INDEX_MAGIC);
819 goto fail;
820 }
821
822 if (hdr.version >> 16 != INDEX_VERSION_MAJOR) {
823 ERR(ctx, "major version check fail: %u instead of %u\n",
824 hdr.version, INDEX_MAGIC);
825 goto fail;
826 }
827
828 idx->root_offset = hdr.root_offset;
829 idx->size = st.st_size;
830 idx->ctx = ctx;
831 close(fd);
832
Lucas De Marchi6068aaa2012-01-17 12:10:42 -0200833 *stamp = stat_mstamp(&st);
Lucas De Marchi9fd58f32011-12-31 18:53:24 -0200834
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200835 return idx;
836
837fail:
Lucas De Marchi5b05c322012-06-06 09:36:29 -0300838 munmap(idx->mm, st.st_size);
839fail_nommap:
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200840 close(fd);
Gustavo Sverzut Barbieria5a92a62011-12-16 16:17:50 -0200841fail_open:
Lucas De Marchib471a6b2011-12-01 23:14:20 -0200842 free(idx);
843 return NULL;
844}
845
846void index_mm_close(struct index_mm *idx)
847{
848 munmap(idx->mm, idx->size);
849 free(idx);
850}
Lucas De Marchi77bf9362011-12-02 17:41:30 -0200851
852static struct index_mm_node *index_mm_readroot(struct index_mm *idx)
853{
854 return index_mm_read_node(idx, idx->root_offset);
855}
Lucas De Marchi91298dc2011-12-02 17:41:46 -0200856
857static struct index_mm_node *index_mm_readchild(const struct index_mm_node *parent,
858 int ch)
859{
860 if (parent->first <= ch && ch <= parent->last) {
861 return index_mm_read_node(parent->idx,
862 parent->children[ch - parent->first]);
863 }
864
865 return NULL;
866}
Lucas De Marchie33bb872011-12-02 17:45:01 -0200867
Lucas De Marchi758428a2012-01-16 15:56:17 -0200868static void index_mm_dump_node(struct index_mm_node *node, struct buffer *buf,
869 int fd)
870{
871 struct index_mm_value *itr, *itr_end;
872 int ch, pushed;
873
874 pushed = buf_pushchars(buf, node->prefix);
875
876 itr = node->values.values;
877 itr_end = itr + node->values.len;
878 for (; itr < itr_end; itr++) {
879 write_str_safe(fd, buf->bytes, buf->used);
880 write_str_safe(fd, " ", 1);
881 write_str_safe(fd, itr->value, itr->len);
882 write_str_safe(fd, "\n", 1);
883 }
884
885 for (ch = node->first; ch <= node->last; ch++) {
886 struct index_mm_node *child = index_mm_readchild(node, ch);
887
888 if (child == NULL)
889 continue;
890
891 buf_pushchar(buf, ch);
892 index_mm_dump_node(child, buf, fd);
893 buf_popchar(buf);
894 }
895
896 buf_popchars(buf, pushed);
897 index_mm_free_node(node);
898}
899
900void index_mm_dump(struct index_mm *idx, int fd, const char *prefix)
901{
902 struct index_mm_node *root;
903 struct buffer buf;
904
905 buf_init(&buf);
906 buf_pushchars(&buf, prefix);
907 root = index_mm_readroot(idx);
908 index_mm_dump_node(root, &buf, fd);
909 buf_release(&buf);
910}
911
Lucas De Marchie33bb872011-12-02 17:45:01 -0200912static char *index_mm_search_node(struct index_mm_node *node, const char *key,
913 int i)
914{
915 char *value;
916 struct index_mm_node *child;
917 int ch;
918 int j;
919
920 while(node) {
921 for (j = 0; node->prefix[j]; j++) {
922 ch = node->prefix[j];
923
924 if (ch != key[i+j]) {
925 index_mm_free_node(node);
926 return NULL;
927 }
928 }
929
930 i += j;
931
932 if (key[i] == '\0') {
Lucas De Marchi817f4e32012-02-27 19:54:33 -0300933 value = node->values.len > 0
934 ? strdup(node->values.values[0].value)
935 : NULL;
936
937 index_mm_free_node(node);
938 return value;
Lucas De Marchie33bb872011-12-02 17:45:01 -0200939 }
940
941 child = index_mm_readchild(node, key[i]);
942 index_mm_free_node(node);
943 node = child;
944 i++;
945 }
946
947 return NULL;
948}
Lucas De Marchib797b792011-12-02 17:49:03 -0200949
950/*
951 * Search the index for a key
952 *
953 * Returns the value of the first match
954 *
955 * The recursive functions free their node argument (using index_close).
956 */
957char *index_mm_search(struct index_mm *idx, const char *key)
958{
Lucas De Marchiee1d1882012-02-27 18:48:02 -0300959// FIXME: return value by reference instead of strdup
Lucas De Marchib797b792011-12-02 17:49:03 -0200960 struct index_mm_node *root;
961 char *value;
962
963 root = index_mm_readroot(idx);
964 value = index_mm_search_node(root, key, 0);
965
966 return value;
967}
Lucas De Marchibf89f702011-12-02 18:23:36 -0200968
969/* Level 4: add all the values from a matching node */
970static void index_mm_searchwild_allvalues(struct index_mm_node *node,
971 struct index_value **out)
972{
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200973 struct index_mm_value *itr, *itr_end;
Lucas De Marchibf89f702011-12-02 18:23:36 -0200974
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -0200975 itr = node->values.values;
976 itr_end = itr + node->values.len;
977 for (; itr < itr_end; itr++)
978 add_value(out, itr->value, itr->len, itr->priority);
Lucas De Marchibf89f702011-12-02 18:23:36 -0200979
980 index_mm_free_node(node);
981}
982
983/*
984 * Level 3: traverse a sub-keyspace which starts with a wildcard,
985 * looking for matches.
986 */
987static void index_mm_searchwild_all(struct index_mm_node *node, int j,
988 struct buffer *buf,
989 const char *subkey,
990 struct index_value **out)
991{
992 int pushed = 0;
993 int ch;
994
995 while (node->prefix[j]) {
996 ch = node->prefix[j];
997
998 buf_pushchar(buf, ch);
999 pushed++;
1000 j++;
1001 }
1002
1003 for (ch = node->first; ch <= node->last; ch++) {
1004 struct index_mm_node *child = index_mm_readchild(node, ch);
1005
1006 if (!child)
1007 continue;
1008
1009 buf_pushchar(buf, ch);
1010 index_mm_searchwild_all(child, 0, buf, subkey, out);
1011 buf_popchar(buf);
1012 }
1013
Gustavo Sverzut Barbierid0915462011-12-10 13:04:43 -02001014 if (node->values.len > 0) {
Lucas De Marchibf89f702011-12-02 18:23:36 -02001015 if (fnmatch(buf_str(buf), subkey, 0) == 0)
1016 index_mm_searchwild_allvalues(node, out);
Gustavo Sverzut Barbieri27fdf632011-12-10 13:28:18 -02001017 else
1018 index_mm_free_node(node);
Lucas De Marchibf89f702011-12-02 18:23:36 -02001019 } else {
1020 index_mm_free_node(node);
1021 }
1022
1023 buf_popchars(buf, pushed);
1024}
1025
1026/* Level 2: descend the tree (until we hit a wildcard) */
1027static void index_mm_searchwild_node(struct index_mm_node *node,
1028 struct buffer *buf,
1029 const char *key, int i,
1030 struct index_value **out)
1031{
1032 struct index_mm_node *child;
1033 int j;
1034 int ch;
1035
1036 while(node) {
1037 for (j = 0; node->prefix[j]; j++) {
1038 ch = node->prefix[j];
1039
1040 if (ch == '*' || ch == '?' || ch == '[') {
1041 index_mm_searchwild_all(node, j, buf,
1042 &key[i+j], out);
1043 return;
1044 }
1045
1046 if (ch != key[i+j]) {
1047 index_mm_free_node(node);
1048 return;
1049 }
1050 }
1051
1052 i += j;
1053
1054 child = index_mm_readchild(node, '*');
1055 if (child) {
1056 buf_pushchar(buf, '*');
1057 index_mm_searchwild_all(child, 0, buf, &key[i], out);
1058 buf_popchar(buf);
1059 }
1060
1061 child = index_mm_readchild(node, '?');
1062 if (child) {
1063 buf_pushchar(buf, '?');
1064 index_mm_searchwild_all(child, 0, buf, &key[i], out);
1065 buf_popchar(buf);
1066 }
1067
1068 child = index_mm_readchild(node, '[');
1069 if (child) {
1070 buf_pushchar(buf, '[');
1071 index_mm_searchwild_all(child, 0, buf, &key[i], out);
1072 buf_popchar(buf);
1073 }
1074
1075 if (key[i] == '\0') {
1076 index_mm_searchwild_allvalues(node, out);
1077
1078 return;
1079 }
1080
1081 child = index_mm_readchild(node, key[i]);
1082 index_mm_free_node(node);
1083 node = child;
1084 i++;
1085 }
1086}
1087
1088/*
1089 * Search the index for a key. The index may contain wildcards.
1090 *
1091 * Returns a list of all the values of matching keys.
1092 */
1093struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key)
1094{
1095 struct index_mm_node *root = index_mm_readroot(idx);
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -02001096 struct buffer buf;
Lucas De Marchibf89f702011-12-02 18:23:36 -02001097 struct index_value *out = NULL;
1098
Gustavo Sverzut Barbieri405f6142011-12-08 14:36:30 -02001099 buf_init(&buf);
1100 index_mm_searchwild_node(root, &buf, key, 0, &out);
1101 buf_release(&buf);
Lucas De Marchibf89f702011-12-02 18:23:36 -02001102 return out;
1103}