2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
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.
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
21 #include <arpa/inet.h> /* htonl */
30 #include "libkmod-private.h"
31 #include "libkmod-index.h"
34 /* index.c: module index file shared functions for modprobe and depmod */
36 #define INDEX_CHILDMAX 128
40 uint32_t magic = INDEX_MAGIC;
41 uint32_t version = INDEX_VERSION;
44 (node_offset & INDEX_NODE_MASK) specifies the file offset of nodes:
46 char[] prefix; // nul terminated
50 uint32_t children[last - first + 1];
55 char[] value; // nul terminated
56 } values[value_count];
58 (node_offset & INDEX_NODE_FLAGS) indicates which fields are present.
59 Empty prefixes are omitted, leaf nodes omit the three child-related fields.
61 This could be optimised further by adding a sparse child format
62 (indicated using a new flag).
65 /* Format of node offsets within index file */
67 INDEX_NODE_FLAGS
= 0xF0000000, /* Flags in high nibble */
68 INDEX_NODE_PREFIX
= 0x80000000,
69 INDEX_NODE_VALUES
= 0x40000000,
70 INDEX_NODE_CHILDS
= 0x20000000,
72 INDEX_NODE_MASK
= 0x0FFFFFFF, /* Offset value */
75 void index_values_free(struct index_value
*values
)
78 struct index_value
*value
= values
;
85 static int add_value(struct index_value
**values
,
86 const char *value
, unsigned len
, unsigned int priority
)
88 struct index_value
*v
;
90 /* find position to insert value */
91 while (*values
&& (*values
)->priority
< priority
)
92 values
= &(*values
)->next
;
94 v
= malloc(sizeof(struct index_value
) + len
+ 1);
98 v
->priority
= priority
;
100 memcpy(v
->value
, value
, len
);
101 v
->value
[len
] = '\0';
107 static void read_error(void)
109 fatal("Module index: unexpected error: %s\n"
110 "Try re-running depmod\n", errno
? strerror(errno
) : "EOF");
113 static int read_char(FILE *in
)
118 ch
= getc_unlocked(in
);
124 static uint32_t read_long(FILE *in
)
129 if (fread(&l
, sizeof(uint32_t), 1, in
) <= 0)
135 * Buffer abstract data type
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.
147 #define BUF_STEP (2048)
148 static bool buf_grow(struct buffer
*buf
, size_t newsize
)
153 if (newsize
% BUF_STEP
== 0)
156 sz
= ((newsize
/ BUF_STEP
) + 1) * BUF_STEP
;
161 tmp
= realloc(buf
->bytes
, sz
);
162 if (sz
> 0 && tmp
== NULL
)
169 static void buf_init(struct buffer
*buf
)
176 static void buf_release(struct buffer
*buf
)
181 /* Destroy buffer and return a copy as a C string */
182 static char *buf_steal(struct buffer
*buf
)
186 bytes
= realloc(buf
->bytes
, buf
->used
+ 1);
191 bytes
[buf
->used
] = '\0';
195 /* Return a C string owned by the buffer
196 (invalidated if the buffer is changed).
198 static const char *buf_str(struct buffer
*buf
)
200 if (!buf_grow(buf
, buf
->used
+ 1))
202 buf
->bytes
[buf
->used
] = '\0';
206 static bool buf_pushchar(struct buffer
*buf
, char ch
)
208 if (!buf_grow(buf
, buf
->used
+ 1))
210 buf
->bytes
[buf
->used
] = ch
;
215 static unsigned buf_pushchars(struct buffer
*buf
, const char *str
)
220 while ((ch
= str
[i
])) {
221 buf_pushchar(buf
, ch
);
228 static unsigned buf_freadchars(struct buffer
*buf
, FILE *in
)
233 while ((ch
= read_char(in
))) {
234 if (!buf_pushchar(buf
, ch
))
242 static void buf_popchar(struct buffer
*buf
)
244 assert(buf
->used
> 0);
248 static void buf_popchars(struct buffer
*buf
, unsigned n
)
250 assert(buf
->used
>= n
);
254 static void buf_clear(struct buffer
*buf
)
260 * Index file searching
262 struct index_node_f
{
264 char *prefix
; /* path compression */
265 struct index_value
*values
;
266 unsigned char first
; /* range of child nodes */
268 uint32_t children
[0];
271 static struct index_node_f
*index_read(FILE *in
, uint32_t offset
)
273 struct index_node_f
*node
;
275 int i
, child_count
= 0;
277 if ((offset
& INDEX_NODE_MASK
) == 0)
280 fseek(in
, offset
& INDEX_NODE_MASK
, SEEK_SET
);
282 if (offset
& INDEX_NODE_PREFIX
) {
285 buf_freadchars(&buf
, in
);
286 prefix
= buf_steal(&buf
);
288 prefix
= NOFAIL(strdup(""));
290 if (offset
& INDEX_NODE_CHILDS
) {
291 char first
= read_char(in
);
292 char last
= read_char(in
);
293 child_count
= last
- first
+ 1;
295 node
= NOFAIL(malloc(sizeof(struct index_node_f
) +
296 sizeof(uint32_t) * child_count
));
301 for (i
= 0; i
< child_count
; i
++)
302 node
->children
[i
] = read_long(in
);
304 node
= NOFAIL(malloc(sizeof(struct index_node_f
)));
305 node
->first
= INDEX_CHILDMAX
;
310 if (offset
& INDEX_NODE_VALUES
) {
314 unsigned int priority
;
316 value_count
= read_long(in
);
319 while (value_count
--) {
320 priority
= read_long(in
);
321 buf_freadchars(&buf
, in
);
322 value
= buf_str(&buf
);
323 add_value(&node
->values
, value
, buf
.used
, priority
);
329 node
->prefix
= prefix
;
334 static void index_close(struct index_node_f
*node
)
337 index_values_free(node
->values
);
343 uint32_t root_offset
;
346 /* Failures are silent; modprobe will fall back to text files */
347 struct index_file
*index_file_open(const char *filename
)
350 uint32_t magic
, version
;
351 struct index_file
*new;
353 file
= fopen(filename
, "re");
358 magic
= read_long(file
);
359 if (magic
!= INDEX_MAGIC
) {
364 version
= read_long(file
);
365 if (version
>> 16 != INDEX_VERSION_MAJOR
) {
370 new = NOFAIL(malloc(sizeof(struct index_file
)));
372 new->root_offset
= read_long(new->file
);
378 void index_file_close(struct index_file
*idx
)
384 static struct index_node_f
*index_readroot(struct index_file
*in
)
386 return index_read(in
->file
, in
->root_offset
);
389 static struct index_node_f
*index_readchild(const struct index_node_f
*parent
,
392 if (parent
->first
<= ch
&& ch
<= parent
->last
) {
393 return index_read(parent
->file
,
394 parent
->children
[ch
- parent
->first
]);
400 static void index_dump_node(struct index_node_f
*node
, struct buffer
*buf
,
403 struct index_value
*v
;
406 pushed
= buf_pushchars(buf
, node
->prefix
);
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);
415 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
416 struct index_node_f
*child
= index_readchild(node
, ch
);
421 buf_pushchar(buf
, ch
);
422 index_dump_node(child
, buf
, fd
);
426 buf_popchars(buf
, pushed
);
430 void index_dump(struct index_file
*in
, int fd
, const char *prefix
)
432 struct index_node_f
*root
;
436 buf_pushchars(&buf
, prefix
);
437 root
= index_readroot(in
);
438 index_dump_node(root
, &buf
, fd
);
442 static char *index_search__node(struct index_node_f
*node
, const char *key
, int i
)
445 struct index_node_f
*child
;
450 for (j
= 0; node
->prefix
[j
]; j
++) {
451 ch
= node
->prefix
[j
];
453 if (ch
!= key
[i
+j
]) {
461 if (key
[i
] == '\0') {
462 value
= node
->values
!= NULL
463 ? strdup(node
->values
[0].value
)
470 child
= index_readchild(node
, key
[i
]);
480 * Search the index for a key
482 * Returns the value of the first match
484 * The recursive functions free their node argument (using index_close).
486 char *index_search(struct index_file
*in
, const char *key
)
488 // FIXME: return value by reference instead of strdup
489 struct index_node_f
*root
;
492 root
= index_readroot(in
);
493 value
= index_search__node(root
, key
, 0);
500 /* Level 4: add all the values from a matching node */
501 static void index_searchwild__allvalues(struct index_node_f
*node
,
502 struct index_value
**out
)
504 struct index_value
*v
;
506 for (v
= node
->values
; v
!= NULL
; v
= v
->next
)
507 add_value(out
, v
->value
, v
->len
, v
->priority
);
513 * Level 3: traverse a sub-keyspace which starts with a wildcard,
514 * looking for matches.
516 static void index_searchwild__all(struct index_node_f
*node
, int j
,
519 struct index_value
**out
)
524 while (node
->prefix
[j
]) {
525 ch
= node
->prefix
[j
];
527 buf_pushchar(buf
, ch
);
532 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
533 struct index_node_f
*child
= index_readchild(node
, ch
);
538 buf_pushchar(buf
, ch
);
539 index_searchwild__all(child
, 0, buf
, subkey
, out
);
544 if (fnmatch(buf_str(buf
), subkey
, 0) == 0)
545 index_searchwild__allvalues(node
, out
);
552 buf_popchars(buf
, pushed
);
555 /* Level 2: descend the tree (until we hit a wildcard) */
556 static void index_searchwild__node(struct index_node_f
*node
,
558 const char *key
, int i
,
559 struct index_value
**out
)
561 struct index_node_f
*child
;
566 for (j
= 0; node
->prefix
[j
]; j
++) {
567 ch
= node
->prefix
[j
];
569 if (ch
== '*' || ch
== '?' || ch
== '[') {
570 index_searchwild__all(node
, j
, buf
,
575 if (ch
!= key
[i
+j
]) {
583 child
= index_readchild(node
, '*');
585 buf_pushchar(buf
, '*');
586 index_searchwild__all(child
, 0, buf
, &key
[i
], out
);
590 child
= index_readchild(node
, '?');
592 buf_pushchar(buf
, '?');
593 index_searchwild__all(child
, 0, buf
, &key
[i
], out
);
597 child
= index_readchild(node
, '[');
599 buf_pushchar(buf
, '[');
600 index_searchwild__all(child
, 0, buf
, &key
[i
], out
);
604 if (key
[i
] == '\0') {
605 index_searchwild__allvalues(node
, out
);
610 child
= index_readchild(node
, key
[i
]);
618 * Search the index for a key. The index may contain wildcards.
620 * Returns a list of all the values of matching keys.
622 struct index_value
*index_searchwild(struct index_file
*in
, const char *key
)
624 struct index_node_f
*root
= index_readroot(in
);
626 struct index_value
*out
= NULL
;
629 index_searchwild__node(root
, &buf
, key
, 0, &out
);
634 #include <sys/mman.h>
635 #include <sys/stat.h>
638 static const char _idx_empty_str
[] = "";
640 /**************************************************************************/
642 * Alternative implementation, using mmap to map all the file to memory when
646 struct kmod_ctx
*ctx
;
648 uint32_t root_offset
;
652 struct index_mm_value
{
653 unsigned int priority
;
658 struct index_mm_value_array
{
659 struct index_mm_value
*values
;
663 struct index_mm_node
{
664 struct index_mm
*idx
;
665 const char *prefix
; /* mmape'd value */
666 struct index_mm_value_array values
;
672 static inline uint32_t read_long_mm(void **p
)
674 uint8_t *addr
= *(uint8_t **)p
;
677 /* addr may be unalined to uint32_t */
678 v
= get_unaligned((uint32_t *) addr
);
680 *p
= addr
+ sizeof(uint32_t);
684 static inline uint8_t read_char_mm(void **p
)
686 uint8_t *addr
= *(uint8_t **)p
;
688 *p
= addr
+ sizeof(uint8_t);
692 static inline char *read_chars_mm(void **p
, unsigned *rlen
)
694 char *addr
= *(char **)p
;
695 size_t len
= *rlen
= strlen(addr
);
700 static struct index_mm_node
*index_mm_read_node(struct index_mm
*idx
,
703 struct index_mm_node
*node
;
705 int i
, child_count
, value_count
, children_padding
;
706 uint32_t children
[INDEX_CHILDMAX
];
709 if ((offset
& INDEX_NODE_MASK
) == 0)
712 p
= (char *)p
+ (offset
& INDEX_NODE_MASK
);
714 if (offset
& INDEX_NODE_PREFIX
) {
716 prefix
= read_chars_mm(&p
, &len
);
718 prefix
= _idx_empty_str
;
720 if (offset
& INDEX_NODE_CHILDS
) {
721 first
= read_char_mm(&p
);
722 last
= read_char_mm(&p
);
723 child_count
= last
- first
+ 1;
724 for (i
= 0; i
< child_count
; i
++)
725 children
[i
] = read_long_mm(&p
);
727 first
= INDEX_CHILDMAX
;
732 children_padding
= (sizeof(struct index_mm_node
) +
733 (sizeof(uint32_t) * child_count
)) % sizeof(void *);
735 if (offset
& INDEX_NODE_VALUES
)
736 value_count
= read_long_mm(&p
);
740 node
= malloc(sizeof(struct index_mm_node
)
741 + sizeof(uint32_t) * child_count
+ children_padding
742 + sizeof(struct index_mm_value
) * value_count
);
747 node
->prefix
= prefix
;
748 if (value_count
== 0)
749 node
->values
.values
= NULL
;
751 node
->values
.values
= (struct index_mm_value
*)
752 ((char *)node
+ sizeof(struct index_mm_node
) +
753 sizeof(uint32_t) * child_count
+ children_padding
);
755 node
->values
.len
= value_count
;
758 memcpy(node
->children
, children
, sizeof(uint32_t) * child_count
);
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
);
769 static void index_mm_free_node(struct index_mm_node
*node
)
774 struct index_mm
*index_mm_open(struct kmod_ctx
*ctx
, const char *filename
,
775 unsigned long long *stamp
)
779 struct index_mm
*idx
;
783 uint32_t root_offset
;
787 DBG(ctx
, "file=%s\n", filename
);
789 idx
= malloc(sizeof(*idx
));
791 ERR(ctx
, "malloc: %m\n");
795 if ((fd
= open(filename
, O_RDONLY
|O_CLOEXEC
)) < 0) {
796 DBG(ctx
, "open(%s, O_RDONLY|O_CLOEXEC): %m\n", filename
);
801 if ((size_t) st
.st_size
< sizeof(hdr
))
804 if ((idx
->mm
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0))
806 ERR(ctx
, "mmap(NULL, %"PRIu64
", PROT_READ, %d, MAP_PRIVATE, 0): %m\n",
812 hdr
.magic
= read_long_mm(&p
);
813 hdr
.version
= read_long_mm(&p
);
814 hdr
.root_offset
= read_long_mm(&p
);
816 if (hdr
.magic
!= INDEX_MAGIC
) {
817 ERR(ctx
, "magic check fail: %x instead of %x\n", hdr
.magic
,
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
);
828 idx
->root_offset
= hdr
.root_offset
;
829 idx
->size
= st
.st_size
;
833 *stamp
= stat_mstamp(&st
);
838 munmap(idx
->mm
, st
.st_size
);
846 void index_mm_close(struct index_mm
*idx
)
848 munmap(idx
->mm
, idx
->size
);
852 static struct index_mm_node
*index_mm_readroot(struct index_mm
*idx
)
854 return index_mm_read_node(idx
, idx
->root_offset
);
857 static struct index_mm_node
*index_mm_readchild(const struct index_mm_node
*parent
,
860 if (parent
->first
<= ch
&& ch
<= parent
->last
) {
861 return index_mm_read_node(parent
->idx
,
862 parent
->children
[ch
- parent
->first
]);
868 static void index_mm_dump_node(struct index_mm_node
*node
, struct buffer
*buf
,
871 struct index_mm_value
*itr
, *itr_end
;
874 pushed
= buf_pushchars(buf
, node
->prefix
);
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);
885 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
886 struct index_mm_node
*child
= index_mm_readchild(node
, ch
);
891 buf_pushchar(buf
, ch
);
892 index_mm_dump_node(child
, buf
, fd
);
896 buf_popchars(buf
, pushed
);
897 index_mm_free_node(node
);
900 void index_mm_dump(struct index_mm
*idx
, int fd
, const char *prefix
)
902 struct index_mm_node
*root
;
906 buf_pushchars(&buf
, prefix
);
907 root
= index_mm_readroot(idx
);
908 index_mm_dump_node(root
, &buf
, fd
);
912 static char *index_mm_search_node(struct index_mm_node
*node
, const char *key
,
916 struct index_mm_node
*child
;
921 for (j
= 0; node
->prefix
[j
]; j
++) {
922 ch
= node
->prefix
[j
];
924 if (ch
!= key
[i
+j
]) {
925 index_mm_free_node(node
);
932 if (key
[i
] == '\0') {
933 value
= node
->values
.len
> 0
934 ? strdup(node
->values
.values
[0].value
)
937 index_mm_free_node(node
);
941 child
= index_mm_readchild(node
, key
[i
]);
942 index_mm_free_node(node
);
951 * Search the index for a key
953 * Returns the value of the first match
955 * The recursive functions free their node argument (using index_close).
957 char *index_mm_search(struct index_mm
*idx
, const char *key
)
959 // FIXME: return value by reference instead of strdup
960 struct index_mm_node
*root
;
963 root
= index_mm_readroot(idx
);
964 value
= index_mm_search_node(root
, key
, 0);
969 /* Level 4: add all the values from a matching node */
970 static void index_mm_searchwild_allvalues(struct index_mm_node
*node
,
971 struct index_value
**out
)
973 struct index_mm_value
*itr
, *itr_end
;
975 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
);
980 index_mm_free_node(node
);
984 * Level 3: traverse a sub-keyspace which starts with a wildcard,
985 * looking for matches.
987 static void index_mm_searchwild_all(struct index_mm_node
*node
, int j
,
990 struct index_value
**out
)
995 while (node
->prefix
[j
]) {
996 ch
= node
->prefix
[j
];
998 buf_pushchar(buf
, ch
);
1003 for (ch
= node
->first
; ch
<= node
->last
; ch
++) {
1004 struct index_mm_node
*child
= index_mm_readchild(node
, ch
);
1009 buf_pushchar(buf
, ch
);
1010 index_mm_searchwild_all(child
, 0, buf
, subkey
, out
);
1014 if (node
->values
.len
> 0) {
1015 if (fnmatch(buf_str(buf
), subkey
, 0) == 0)
1016 index_mm_searchwild_allvalues(node
, out
);
1018 index_mm_free_node(node
);
1020 index_mm_free_node(node
);
1023 buf_popchars(buf
, pushed
);
1026 /* Level 2: descend the tree (until we hit a wildcard) */
1027 static void index_mm_searchwild_node(struct index_mm_node
*node
,
1029 const char *key
, int i
,
1030 struct index_value
**out
)
1032 struct index_mm_node
*child
;
1037 for (j
= 0; node
->prefix
[j
]; j
++) {
1038 ch
= node
->prefix
[j
];
1040 if (ch
== '*' || ch
== '?' || ch
== '[') {
1041 index_mm_searchwild_all(node
, j
, buf
,
1046 if (ch
!= key
[i
+j
]) {
1047 index_mm_free_node(node
);
1054 child
= index_mm_readchild(node
, '*');
1056 buf_pushchar(buf
, '*');
1057 index_mm_searchwild_all(child
, 0, buf
, &key
[i
], out
);
1061 child
= index_mm_readchild(node
, '?');
1063 buf_pushchar(buf
, '?');
1064 index_mm_searchwild_all(child
, 0, buf
, &key
[i
], out
);
1068 child
= index_mm_readchild(node
, '[');
1070 buf_pushchar(buf
, '[');
1071 index_mm_searchwild_all(child
, 0, buf
, &key
[i
], out
);
1075 if (key
[i
] == '\0') {
1076 index_mm_searchwild_allvalues(node
, out
);
1081 child
= index_mm_readchild(node
, key
[i
]);
1082 index_mm_free_node(node
);
1089 * Search the index for a key. The index may contain wildcards.
1091 * Returns a list of all the values of matching keys.
1093 struct index_value
*index_mm_searchwild(struct index_mm
*idx
, const char *key
)
1095 struct index_mm_node
*root
= index_mm_readroot(idx
);
1097 struct index_value
*out
= NULL
;
1100 index_mm_searchwild_node(root
, &buf
, key
, 0, &out
);