~ubuntu-branches/ubuntu/vivid/kmod/vivid

« back to all changes in this revision

Viewing changes to .pc/check_if_exists/libkmod/libkmod-module.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-09-21 16:05:32 UTC
  • mfrom: (4.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20120921160532-bdsxhmpj5og2ru5h
Tags: 9-2ubuntu1
* Sync with Debian unstable; remaining Ubuntu changes:
  - Ubuntu-specific depmod.d and modprobe.d contents.
  - Mark module-init-tools Multi-Arch: foreign.
  - Don't install Debian's extra/aliases.conf file.
  - Install upstart job instead of the sysvinit script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * libkmod - interface to kernel module operations
3
 
 *
4
 
 * Copyright (C) 2011-2012  ProFUSION embedded systems
5
 
 *
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.
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
 
 */
20
 
 
21
 
#include <assert.h>
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <stddef.h>
25
 
#include <stdarg.h>
26
 
#include <unistd.h>
27
 
#include <errno.h>
28
 
#include <string.h>
29
 
#include <ctype.h>
30
 
#include <inttypes.h>
31
 
#include <limits.h>
32
 
#include <dirent.h>
33
 
#include <sys/stat.h>
34
 
#include <sys/types.h>
35
 
#include <sys/mman.h>
36
 
#include <sys/wait.h>
37
 
#include <string.h>
38
 
#include <fnmatch.h>
39
 
 
40
 
#include "libkmod.h"
41
 
#include "libkmod-private.h"
42
 
 
43
 
/**
44
 
 * SECTION:libkmod-module
45
 
 * @short_description: operate on kernel modules
46
 
 */
47
 
 
48
 
/**
49
 
 * kmod_module:
50
 
 *
51
 
 * Opaque object representing a module.
52
 
 */
53
 
struct kmod_module {
54
 
        struct kmod_ctx *ctx;
55
 
        char *hashkey;
56
 
        char *name;
57
 
        char *path;
58
 
        struct kmod_list *dep;
59
 
        char *options;
60
 
        const char *install_commands;   /* owned by kmod_config */
61
 
        const char *remove_commands;    /* owned by kmod_config */
62
 
        char *alias; /* only set if this module was created from an alias */
63
 
        int n_dep;
64
 
        int refcount;
65
 
        struct {
66
 
                bool dep : 1;
67
 
                bool options : 1;
68
 
                bool install_commands : 1;
69
 
                bool remove_commands : 1;
70
 
        } init;
71
 
 
72
 
        /*
73
 
         * private field used by kmod_module_get_probe_list() to detect
74
 
         * dependency loops
75
 
         */
76
 
        bool visited : 1;
77
 
 
78
 
        /*
79
 
         * set by kmod_module_get_probe_list: indicates for probe_insert()
80
 
         * whether the module's command and softdep should be ignored
81
 
         */
82
 
        bool ignorecmd : 1;
83
 
};
84
 
 
85
 
static inline const char *path_join(const char *path, size_t prefixlen,
86
 
                                                        char buf[PATH_MAX])
87
 
{
88
 
        size_t pathlen;
89
 
 
90
 
        if (path[0] == '/')
91
 
                return path;
92
 
 
93
 
        pathlen = strlen(path);
94
 
        if (prefixlen + pathlen + 1 >= PATH_MAX)
95
 
                return NULL;
96
 
 
97
 
        memcpy(buf + prefixlen, path, pathlen + 1);
98
 
        return buf;
99
 
}
100
 
 
101
 
static inline bool module_is_inkernel(struct kmod_module *mod)
102
 
{
103
 
        int state = kmod_module_get_initstate(mod);
104
 
        if (state == KMOD_MODULE_LIVE ||
105
 
                        state == KMOD_MODULE_COMING ||
106
 
                        state == KMOD_MODULE_BUILTIN)
107
 
                return true;
108
 
        else
109
 
                return false;
110
 
}
111
 
 
112
 
int kmod_module_parse_depline(struct kmod_module *mod, char *line)
113
 
{
114
 
        struct kmod_ctx *ctx = mod->ctx;
115
 
        struct kmod_list *list = NULL;
116
 
        const char *dirname;
117
 
        char buf[PATH_MAX];
118
 
        char *p, *saveptr;
119
 
        int err = 0, n = 0;
120
 
        size_t dirnamelen;
121
 
 
122
 
        if (mod->init.dep)
123
 
                return mod->n_dep;
124
 
        assert(mod->dep == NULL);
125
 
        mod->init.dep = true;
126
 
 
127
 
        p = strchr(line, ':');
128
 
        if (p == NULL)
129
 
                return 0;
130
 
 
131
 
        *p = '\0';
132
 
        dirname = kmod_get_dirname(mod->ctx);
133
 
        dirnamelen = strlen(dirname);
134
 
        if (dirnamelen + 2 >= PATH_MAX)
135
 
                return 0;
136
 
 
137
 
        memcpy(buf, dirname, dirnamelen);
138
 
        buf[dirnamelen] = '/';
139
 
        dirnamelen++;
140
 
        buf[dirnamelen] = '\0';
141
 
 
142
 
        if (mod->path == NULL) {
143
 
                const char *str = path_join(line, dirnamelen, buf);
144
 
                if (str == NULL)
145
 
                        return 0;
146
 
                mod->path = strdup(str);
147
 
                if (mod->path == NULL)
148
 
                        return 0;
149
 
        }
150
 
 
151
 
        p++;
152
 
        for (p = strtok_r(p, " \t", &saveptr); p != NULL;
153
 
                                        p = strtok_r(NULL, " \t", &saveptr)) {
154
 
                struct kmod_module *depmod;
155
 
                const char *path;
156
 
 
157
 
                path = path_join(p, dirnamelen, buf);
158
 
                if (path == NULL) {
159
 
                        ERR(ctx, "could not join path '%s' and '%s'.\n",
160
 
                            dirname, p);
161
 
                        goto fail;
162
 
                }
163
 
 
164
 
                err = kmod_module_new_from_path(ctx, path, &depmod);
165
 
                if (err < 0) {
166
 
                        ERR(ctx, "ctx=%p path=%s error=%s\n",
167
 
                                                ctx, path, strerror(-err));
168
 
                        goto fail;
169
 
                }
170
 
 
171
 
                DBG(ctx, "add dep: %s\n", path);
172
 
 
173
 
                list = kmod_list_prepend(list, depmod);
174
 
                n++;
175
 
        }
176
 
 
177
 
        DBG(ctx, "%d dependencies for %s\n", n, mod->name);
178
 
 
179
 
        mod->dep = list;
180
 
        mod->n_dep = n;
181
 
        return n;
182
 
 
183
 
fail:
184
 
        kmod_module_unref_list(list);
185
 
        mod->init.dep = false;
186
 
        return err;
187
 
}
188
 
 
189
 
void kmod_module_set_visited(struct kmod_module *mod, bool visited)
190
 
{
191
 
        mod->visited = visited;
192
 
}
193
 
 
194
 
/*
195
 
 * Memory layout with alias:
196
 
 *
197
 
 * struct kmod_module {
198
 
 *        hashkey -----.
199
 
 *        alias -----. |
200
 
 *        name ----. | |
201
 
 * }               | | |
202
 
 * name <----------' | |
203
 
 * alias <-----------' |
204
 
 * name\alias <--------'
205
 
 *
206
 
 * Memory layout without alias:
207
 
 *
208
 
 * struct kmod_module {
209
 
 *        hashkey ---.
210
 
 *        alias -----|----> NULL
211
 
 *        name ----. |
212
 
 * }               | |
213
 
 * name <----------'-'
214
 
 *
215
 
 * @key is "name\alias" or "name" (in which case alias == NULL)
216
 
 */
217
 
static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
218
 
                                const char *name, size_t namelen,
219
 
                                const char *alias, size_t aliaslen,
220
 
                                struct kmod_module **mod)
221
 
{
222
 
        struct kmod_module *m;
223
 
        size_t keylen;
224
 
 
225
 
        m = kmod_pool_get_module(ctx, key);
226
 
        if (m != NULL) {
227
 
                *mod = kmod_module_ref(m);
228
 
                return 0;
229
 
        }
230
 
 
231
 
        if (alias == NULL)
232
 
                keylen = namelen;
233
 
        else
234
 
                keylen = namelen + aliaslen + 1;
235
 
 
236
 
        m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
237
 
        if (m == NULL) {
238
 
                free(m);
239
 
                return -ENOMEM;
240
 
        }
241
 
 
242
 
        memset(m, 0, sizeof(*m));
243
 
 
244
 
        m->ctx = kmod_ref(ctx);
245
 
        m->name = (char *)m + sizeof(*m);
246
 
        memcpy(m->name, key, keylen + 1);
247
 
        if (alias == NULL) {
248
 
                m->hashkey = m->name;
249
 
                m->alias = NULL;
250
 
        } else {
251
 
                m->name[namelen] = '\0';
252
 
                m->alias = m->name + namelen + 1;
253
 
                m->hashkey = m->name + keylen + 1;
254
 
                memcpy(m->hashkey, key, keylen + 1);
255
 
        }
256
 
 
257
 
        m->refcount = 1;
258
 
        kmod_pool_add_module(ctx, m, m->hashkey);
259
 
        *mod = m;
260
 
 
261
 
        return 0;
262
 
}
263
 
 
264
 
/**
265
 
 * kmod_module_new_from_name:
266
 
 * @ctx: kmod library context
267
 
 * @name: name of the module
268
 
 * @mod: where to save the created struct kmod_module
269
 
 *
270
 
 * Create a new struct kmod_module using the module name. @name can not be an
271
 
 * alias, file name or anything else; it must be a module name. There's no
272
 
 * check if the module exists in the system.
273
 
 *
274
 
 * This function is also used internally by many others that return a new
275
 
 * struct kmod_module or a new list of modules.
276
 
 *
277
 
 * The initial refcount is 1, and needs to be decremented to release the
278
 
 * resources of the kmod_module. Since libkmod keeps track of all
279
 
 * kmod_modules created, they are all released upon @ctx destruction too. Do
280
 
 * not unref @ctx before all the desired operations with the returned
281
 
 * kmod_module are done.
282
 
 *
283
 
 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
284
 
 * module name or if memory allocation failed.
285
 
 */
286
 
KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
287
 
                                                const char *name,
288
 
                                                struct kmod_module **mod)
289
 
{
290
 
        size_t namelen;
291
 
        char name_norm[PATH_MAX];
292
 
 
293
 
        if (ctx == NULL || name == NULL || mod == NULL)
294
 
                return -ENOENT;
295
 
 
296
 
        modname_normalize(name, name_norm, &namelen);
297
 
 
298
 
        return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
299
 
}
300
 
 
301
 
int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
302
 
                                const char *name, struct kmod_module **mod)
303
 
{
304
 
        int err;
305
 
        char key[PATH_MAX];
306
 
        size_t namelen = strlen(name);
307
 
        size_t aliaslen = strlen(alias);
308
 
 
309
 
        if (namelen + aliaslen + 2 > PATH_MAX)
310
 
                return -ENAMETOOLONG;
311
 
 
312
 
        memcpy(key, name, namelen);
313
 
        memcpy(key + namelen + 1, alias, aliaslen + 1);
314
 
        key[namelen] = '\\';
315
 
 
316
 
        err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
317
 
        if (err < 0)
318
 
                return err;
319
 
 
320
 
        return 0;
321
 
}
322
 
 
323
 
/**
324
 
 * kmod_module_new_from_path:
325
 
 * @ctx: kmod library context
326
 
 * @path: path where to find the given module
327
 
 * @mod: where to save the created struct kmod_module
328
 
 *
329
 
 * Create a new struct kmod_module using the module path. @path must be an
330
 
 * existent file with in the filesystem and must be accessible to libkmod.
331
 
 *
332
 
 * The initial refcount is 1, and needs to be decremented to release the
333
 
 * resources of the kmod_module. Since libkmod keeps track of all
334
 
 * kmod_modules created, they are all released upon @ctx destruction too. Do
335
 
 * not unref @ctx before all the desired operations with the returned
336
 
 * kmod_module are done.
337
 
 *
338
 
 * If @path is relative, it's treated as relative to the current working
339
 
 * directory. Otherwise, give an absolute path.
340
 
 *
341
 
 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
342
 
 * it's not a valid file for a kmod_module or if memory allocation failed.
343
 
 */
344
 
KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
345
 
                                                const char *path,
346
 
                                                struct kmod_module **mod)
347
 
{
348
 
        struct kmod_module *m;
349
 
        int err;
350
 
        struct stat st;
351
 
        char name[PATH_MAX];
352
 
        char *abspath;
353
 
        size_t namelen;
354
 
 
355
 
        if (ctx == NULL || path == NULL || mod == NULL)
356
 
                return -ENOENT;
357
 
 
358
 
        abspath = path_make_absolute_cwd(path);
359
 
        if (abspath == NULL) {
360
 
                DBG(ctx, "no absolute path for %s\n", path);
361
 
                return -ENOMEM;
362
 
        }
363
 
 
364
 
        err = stat(abspath, &st);
365
 
        if (err < 0) {
366
 
                err = -errno;
367
 
                DBG(ctx, "stat %s: %s\n", path, strerror(errno));
368
 
                free(abspath);
369
 
                return err;
370
 
        }
371
 
 
372
 
        if (path_to_modname(path, name, &namelen) == NULL) {
373
 
                DBG(ctx, "could not get modname from path %s\n", path);
374
 
                free(abspath);
375
 
                return -ENOENT;
376
 
        }
377
 
 
378
 
        m = kmod_pool_get_module(ctx, name);
379
 
        if (m != NULL) {
380
 
                if (m->path == NULL)
381
 
                        m->path = abspath;
382
 
                else if (streq(m->path, abspath))
383
 
                        free(abspath);
384
 
                else {
385
 
                        ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
386
 
                                                        name, abspath, m->path);
387
 
                        free(abspath);
388
 
                        return -EEXIST;
389
 
                }
390
 
 
391
 
                *mod = kmod_module_ref(m);
392
 
                return 0;
393
 
        }
394
 
 
395
 
        err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
396
 
        if (err < 0)
397
 
                return err;
398
 
 
399
 
        m->path = abspath;
400
 
        *mod = m;
401
 
 
402
 
        return 0;
403
 
}
404
 
 
405
 
/**
406
 
 * kmod_module_unref:
407
 
 * @mod: kmod module
408
 
 *
409
 
 * Drop a reference of the kmod module. If the refcount reaches zero, its
410
 
 * resources are released.
411
 
 *
412
 
 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
413
 
 * returns the passed @mod with its refcount decremented.
414
 
 */
415
 
KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
416
 
{
417
 
        if (mod == NULL)
418
 
                return NULL;
419
 
 
420
 
        if (--mod->refcount > 0)
421
 
                return mod;
422
 
 
423
 
        DBG(mod->ctx, "kmod_module %p released\n", mod);
424
 
 
425
 
        kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
426
 
        kmod_module_unref_list(mod->dep);
427
 
        kmod_unref(mod->ctx);
428
 
        free(mod->options);
429
 
        free(mod->path);
430
 
        free(mod);
431
 
        return NULL;
432
 
}
433
 
 
434
 
/**
435
 
 * kmod_module_ref:
436
 
 * @mod: kmod module
437
 
 *
438
 
 * Take a reference of the kmod module, incrementing its refcount.
439
 
 *
440
 
 * Returns: the passed @module with its refcount incremented.
441
 
 */
442
 
KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
443
 
{
444
 
        if (mod == NULL)
445
 
                return NULL;
446
 
 
447
 
        mod->refcount++;
448
 
 
449
 
        return mod;
450
 
}
451
 
 
452
 
#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish)     \
453
 
        do {                                                            \
454
 
                if ((_err) < 0)                                         \
455
 
                        goto _label_err;                                \
456
 
                if (*(_list) != NULL)                                   \
457
 
                        goto finish;                                    \
458
 
        } while (0)
459
 
 
460
 
/**
461
 
 * kmod_module_new_from_lookup:
462
 
 * @ctx: kmod library context
463
 
 * @given_alias: alias to look for
464
 
 * @list: an empty list where to save the list of modules matching
465
 
 * @given_alias
466
 
 *
467
 
 * Create a new list of kmod modules using an alias or module name and lookup
468
 
 * libkmod's configuration files and indexes in order to find the module.
469
 
 * Once it's found in one of the places, it stops searching and create the
470
 
 * list of modules that is saved in @list.
471
 
 *
472
 
 * The search order is: 1. aliases in configuration file; 2. module names in
473
 
 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
474
 
 * in modules.alias index.
475
 
 *
476
 
 * The initial refcount is 1, and needs to be decremented to release the
477
 
 * resources of the kmod_module. The returned @list must be released by
478
 
 * calling kmod_module_unref_list(). Since libkmod keeps track of all
479
 
 * kmod_modules created, they are all released upon @ctx destruction too. Do
480
 
 * not unref @ctx before all the desired operations with the returned list are
481
 
 * completed.
482
 
 *
483
 
 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
484
 
 * methods failed, which is basically due to memory allocation fail. If module
485
 
 * is not found, it still returns 0, but @list is an empty list.
486
 
 */
487
 
KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
488
 
                                                const char *given_alias,
489
 
                                                struct kmod_list **list)
490
 
{
491
 
        int err;
492
 
        char alias[PATH_MAX];
493
 
 
494
 
        if (ctx == NULL || given_alias == NULL)
495
 
                return -ENOENT;
496
 
 
497
 
        if (list == NULL || *list != NULL) {
498
 
                ERR(ctx, "An empty list is needed to create lookup\n");
499
 
                return -ENOSYS;
500
 
        }
501
 
 
502
 
        if (alias_normalize(given_alias, alias, NULL) < 0) {
503
 
                DBG(ctx, "invalid alias: %s\n", given_alias);
504
 
                return -EINVAL;
505
 
        }
506
 
 
507
 
        DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
508
 
 
509
 
        /* Aliases from config file override all the others */
510
 
        err = kmod_lookup_alias_from_config(ctx, alias, list);
511
 
        CHECK_ERR_AND_FINISH(err, fail, list, finish);
512
 
 
513
 
        DBG(ctx, "lookup modules.dep %s\n", alias);
514
 
        err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
515
 
        CHECK_ERR_AND_FINISH(err, fail, list, finish);
516
 
 
517
 
        DBG(ctx, "lookup modules.symbols %s\n", alias);
518
 
        err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
519
 
        CHECK_ERR_AND_FINISH(err, fail, list, finish);
520
 
 
521
 
        DBG(ctx, "lookup install and remove commands %s\n", alias);
522
 
        err = kmod_lookup_alias_from_commands(ctx, alias, list);
523
 
        CHECK_ERR_AND_FINISH(err, fail, list, finish);
524
 
 
525
 
        DBG(ctx, "lookup modules.aliases %s\n", alias);
526
 
        err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
527
 
        CHECK_ERR_AND_FINISH(err, fail, list, finish);
528
 
 
529
 
finish:
530
 
        DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
531
 
        return err;
532
 
fail:
533
 
        DBG(ctx, "Failed to lookup %s\n", alias);
534
 
        kmod_module_unref_list(*list);
535
 
        *list = NULL;
536
 
        return err;
537
 
}
538
 
#undef CHECK_ERR_AND_FINISH
539
 
 
540
 
/**
541
 
 * kmod_module_unref_list:
542
 
 * @list: list of kmod modules
543
 
 *
544
 
 * Drop a reference of each kmod module in @list and releases the resources
545
 
 * taken by the list itself.
546
 
 *
547
 
 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
548
 
 * returns the passed @mod with its refcount decremented.
549
 
 */
550
 
KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
551
 
{
552
 
        for (; list != NULL; list = kmod_list_remove(list))
553
 
                kmod_module_unref(list->data);
554
 
 
555
 
        return 0;
556
 
}
557
 
 
558
 
/**
559
 
 * kmod_module_get_filtered_blacklist:
560
 
 * @ctx: kmod library context
561
 
 * @input: list of kmod_module to be filtered with blacklist
562
 
 * @output: where to save the new list
563
 
 *
564
 
 * Given a list @input, this function filter it out with config's blacklist
565
 
 * ans save it in @output.
566
 
 *
567
 
 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
568
 
 * list.
569
 
 */
570
 
KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
571
 
                                                const struct kmod_list *input,
572
 
                                                struct kmod_list **output)
573
 
{
574
 
        const struct kmod_list *li;
575
 
        const struct kmod_list *blacklist;
576
 
 
577
 
        if (ctx == NULL || output == NULL)
578
 
                return -ENOENT;
579
 
 
580
 
        *output = NULL;
581
 
        if (input == NULL)
582
 
                return 0;
583
 
 
584
 
        blacklist = kmod_get_blacklists(ctx);
585
 
        kmod_list_foreach(li, input) {
586
 
                struct kmod_module *mod = li->data;
587
 
                const struct kmod_list *lb;
588
 
                struct kmod_list *node;
589
 
                bool filtered = false;
590
 
 
591
 
                kmod_list_foreach(lb, blacklist) {
592
 
                        const char *name = lb->data;
593
 
 
594
 
                        if (streq(name, mod->name)) {
595
 
                                filtered = true;
596
 
                                break;
597
 
                        }
598
 
                }
599
 
 
600
 
                if (filtered)
601
 
                        continue;
602
 
 
603
 
                node = kmod_list_append(*output, mod);
604
 
                if (node == NULL)
605
 
                        goto fail;
606
 
 
607
 
                *output = node;
608
 
                kmod_module_ref(mod);
609
 
        }
610
 
 
611
 
        return 0;
612
 
 
613
 
fail:
614
 
        kmod_module_unref_list(*output);
615
 
        *output = NULL;
616
 
        return -ENOMEM;
617
 
}
618
 
 
619
 
static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
620
 
{
621
 
        if (!mod->init.dep) {
622
 
                /* lazy init */
623
 
                char *line = kmod_search_moddep(mod->ctx, mod->name);
624
 
 
625
 
                if (line == NULL)
626
 
                        return NULL;
627
 
 
628
 
                kmod_module_parse_depline((struct kmod_module *)mod, line);
629
 
                free(line);
630
 
 
631
 
                if (!mod->init.dep)
632
 
                        return NULL;
633
 
        }
634
 
 
635
 
        return mod->dep;
636
 
}
637
 
 
638
 
/**
639
 
 * kmod_module_get_dependencies:
640
 
 * @mod: kmod module
641
 
 *
642
 
 * Search the modules.dep index to find the dependencies of the given @mod.
643
 
 * The result is cached in @mod, so subsequent calls to this function will
644
 
 * return the already searched list of modules.
645
 
 *
646
 
 * Returns: NULL on failure or if there are any dependencies. Otherwise it
647
 
 * returns a list of kmod modules that can be released by calling
648
 
 * kmod_module_unref_list().
649
 
 */
650
 
KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
651
 
{
652
 
        struct kmod_list *l, *l_new, *list_new = NULL;
653
 
 
654
 
        if (mod == NULL)
655
 
                return NULL;
656
 
 
657
 
        module_get_dependencies_noref(mod);
658
 
 
659
 
        kmod_list_foreach(l, mod->dep) {
660
 
                l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
661
 
                if (l_new == NULL) {
662
 
                        kmod_module_unref(l->data);
663
 
                        goto fail;
664
 
                }
665
 
 
666
 
                list_new = l_new;
667
 
        }
668
 
 
669
 
        return list_new;
670
 
 
671
 
fail:
672
 
        ERR(mod->ctx, "out of memory\n");
673
 
        kmod_module_unref_list(list_new);
674
 
        return NULL;
675
 
}
676
 
 
677
 
/**
678
 
 * kmod_module_get_module:
679
 
 * @entry: an entry in a list of kmod modules.
680
 
 *
681
 
 * Get the kmod module of this @entry in the list, increasing its refcount.
682
 
 * After it's used, unref it. Since the refcount is incremented upon return,
683
 
 * you still have to call kmod_module_unref_list() to release the list of kmod
684
 
 * modules.
685
 
 *
686
 
 * Returns: NULL on failure or the kmod_module contained in this list entry
687
 
 * with its refcount incremented.
688
 
 */
689
 
KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
690
 
{
691
 
        if (entry == NULL)
692
 
                return NULL;
693
 
 
694
 
        return kmod_module_ref(entry->data);
695
 
}
696
 
 
697
 
/**
698
 
 * kmod_module_get_name:
699
 
 * @mod: kmod module
700
 
 *
701
 
 * Get the name of this kmod module. Name is always available, independently
702
 
 * if it was created by kmod_module_new_from_name() or another function and
703
 
 * it's always normalized (dashes are replaced with underscores).
704
 
 *
705
 
 * Returns: the name of this kmod module.
706
 
 */
707
 
KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
708
 
{
709
 
        if (mod == NULL)
710
 
                return NULL;
711
 
 
712
 
        return mod->name;
713
 
}
714
 
 
715
 
/**
716
 
 * kmod_module_get_path:
717
 
 * @mod: kmod module
718
 
 *
719
 
 * Get the path of this kmod module. If this kmod module was not created by
720
 
 * path, it can search the modules.dep index in order to find out the module
721
 
 * under context's dirname.
722
 
 *
723
 
 * Returns: the path of this kmod module or NULL if such information is not
724
 
 * available.
725
 
 */
726
 
KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
727
 
{
728
 
        char *line;
729
 
 
730
 
        if (mod == NULL)
731
 
                return NULL;
732
 
 
733
 
        DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
734
 
 
735
 
        if (mod->path != NULL)
736
 
                return mod->path;
737
 
        if (mod->init.dep)
738
 
                return NULL;
739
 
 
740
 
        /* lazy init */
741
 
        line = kmod_search_moddep(mod->ctx, mod->name);
742
 
        if (line == NULL)
743
 
                return NULL;
744
 
 
745
 
        kmod_module_parse_depline((struct kmod_module *) mod, line);
746
 
        free(line);
747
 
 
748
 
        return mod->path;
749
 
}
750
 
 
751
 
 
752
 
extern long delete_module(const char *name, unsigned int flags);
753
 
 
754
 
/**
755
 
 * kmod_module_remove_module:
756
 
 * @mod: kmod module
757
 
 * @flags: flags to pass to Linux kernel when removing the module
758
 
 *
759
 
 * Remove a module from Linux kernel.
760
 
 *
761
 
 * Returns: 0 on success or < 0 on failure.
762
 
 */
763
 
KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
764
 
                                                        unsigned int flags)
765
 
{
766
 
        int err;
767
 
 
768
 
        if (mod == NULL)
769
 
                return -ENOENT;
770
 
 
771
 
        /* Filter out other flags */
772
 
        flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
773
 
 
774
 
        err = delete_module(mod->name, flags);
775
 
        if (err != 0) {
776
 
                err = -errno;
777
 
                ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
778
 
        }
779
 
 
780
 
        return err;
781
 
}
782
 
 
783
 
extern long init_module(const void *mem, unsigned long len, const char *args);
784
 
 
785
 
/**
786
 
 * kmod_module_insert_module:
787
 
 * @mod: kmod module
788
 
 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
789
 
 * behavior of this function.
790
 
 * @options: module's options to pass to Linux Kernel.
791
 
 *
792
 
 * Insert a module in Linux kernel. It opens the file pointed by @mod,
793
 
 * mmap'ing it and passing to kernel.
794
 
 *
795
 
 * Returns: 0 on success or < 0 on failure. If module is already loaded it
796
 
 * returns -EEXIST.
797
 
 */
798
 
KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
799
 
                                                        unsigned int flags,
800
 
                                                        const char *options)
801
 
{
802
 
        int err;
803
 
        const void *mem;
804
 
        off_t size;
805
 
        struct kmod_file *file;
806
 
        struct kmod_elf *elf = NULL;
807
 
        const char *path;
808
 
        const char *args = options ? options : "";
809
 
 
810
 
        if (mod == NULL)
811
 
                return -ENOENT;
812
 
 
813
 
        path = kmod_module_get_path(mod);
814
 
        if (path == NULL) {
815
 
                ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
816
 
                return -ENOSYS;
817
 
        }
818
 
 
819
 
        file = kmod_file_open(mod->ctx, path);
820
 
        if (file == NULL) {
821
 
                err = -errno;
822
 
                return err;
823
 
        }
824
 
 
825
 
        size = kmod_file_get_size(file);
826
 
        mem = kmod_file_get_contents(file);
827
 
 
828
 
        if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
829
 
                elf = kmod_elf_new(mem, size);
830
 
                if (elf == NULL) {
831
 
                        err = -errno;
832
 
                        goto elf_failed;
833
 
                }
834
 
 
835
 
                if (flags & KMOD_INSERT_FORCE_MODVERSION) {
836
 
                        err = kmod_elf_strip_section(elf, "__versions");
837
 
                        if (err < 0)
838
 
                                INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
839
 
                }
840
 
 
841
 
                if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
842
 
                        err = kmod_elf_strip_vermagic(elf);
843
 
                        if (err < 0)
844
 
                                INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
845
 
                }
846
 
 
847
 
                mem = kmod_elf_get_memory(elf);
848
 
        }
849
 
 
850
 
        err = init_module(mem, size, args);
851
 
        if (err < 0) {
852
 
                err = -errno;
853
 
                INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
854
 
        }
855
 
 
856
 
        if (elf != NULL)
857
 
                kmod_elf_unref(elf);
858
 
elf_failed:
859
 
        kmod_file_unref(file);
860
 
 
861
 
        return err;
862
 
}
863
 
 
864
 
static bool module_is_blacklisted(struct kmod_module *mod)
865
 
{
866
 
        struct kmod_ctx *ctx = mod->ctx;
867
 
        const struct kmod_list *bl = kmod_get_blacklists(ctx);
868
 
        const struct kmod_list *l;
869
 
 
870
 
        kmod_list_foreach(l, bl) {
871
 
                const char *modname = kmod_blacklist_get_modname(l);
872
 
 
873
 
                if (streq(modname, mod->name))
874
 
                        return true;
875
 
        }
876
 
 
877
 
        return false;
878
 
}
879
 
 
880
 
static int command_do(struct kmod_module *mod, const char *type,
881
 
                                                        const char *cmd)
882
 
{
883
 
        const char *modname = kmod_module_get_name(mod);
884
 
        int err;
885
 
 
886
 
        DBG(mod->ctx, "%s %s\n", type, cmd);
887
 
 
888
 
        setenv("MODPROBE_MODULE", modname, 1);
889
 
        err = system(cmd);
890
 
        unsetenv("MODPROBE_MODULE");
891
 
 
892
 
        if (err == -1 || WEXITSTATUS(err)) {
893
 
                ERR(mod->ctx, "Error running %s command for %s\n",
894
 
                                                                type, modname);
895
 
                if (err != -1)
896
 
                        err = -WEXITSTATUS(err);
897
 
        }
898
 
 
899
 
        return err;
900
 
}
901
 
 
902
 
struct probe_insert_cb {
903
 
        int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
904
 
        void *data;
905
 
};
906
 
 
907
 
static int module_do_install_commands(struct kmod_module *mod,
908
 
                                        const char *options,
909
 
                                        struct probe_insert_cb *cb)
910
 
{
911
 
        const char *command = kmod_module_get_install_commands(mod);
912
 
        char *p, *cmd;
913
 
        int err;
914
 
        size_t cmdlen, options_len, varlen;
915
 
 
916
 
        assert(command);
917
 
 
918
 
        if (options == NULL)
919
 
                options = "";
920
 
 
921
 
        options_len = strlen(options);
922
 
        cmdlen = strlen(command);
923
 
        varlen = sizeof("$CMDLINE_OPTS") - 1;
924
 
 
925
 
        cmd = memdup(command, cmdlen + 1);
926
 
        if (cmd == NULL)
927
 
                return -ENOMEM;
928
 
 
929
 
        while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
930
 
                size_t prefixlen = p - cmd;
931
 
                size_t suffixlen = cmdlen - prefixlen - varlen;
932
 
                size_t slen = cmdlen - varlen + options_len;
933
 
                char *suffix = p + varlen;
934
 
                char *s = malloc(slen + 1);
935
 
                if (s == NULL) {
936
 
                        free(cmd);
937
 
                        return -ENOMEM;
938
 
                }
939
 
                memcpy(s, cmd, p - cmd);
940
 
                memcpy(s + prefixlen, options, options_len);
941
 
                memcpy(s + prefixlen + options_len, suffix, suffixlen);
942
 
                s[slen] = '\0';
943
 
 
944
 
                free(cmd);
945
 
                cmd = s;
946
 
                cmdlen = slen;
947
 
        }
948
 
 
949
 
        if (cb->run_install != NULL)
950
 
                err = cb->run_install(mod, cmd, cb->data);
951
 
        else
952
 
                err = command_do(mod, "install", cmd);
953
 
 
954
 
        free(cmd);
955
 
 
956
 
        return err;
957
 
}
958
 
 
959
 
static char *module_options_concat(const char *opt, const char *xopt)
960
 
{
961
 
        // TODO: we might need to check if xopt overrides options on opt
962
 
        size_t optlen = opt == NULL ? 0 : strlen(opt);
963
 
        size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
964
 
        char *r;
965
 
 
966
 
        if (optlen == 0 && xoptlen == 0)
967
 
                return NULL;
968
 
 
969
 
        r = malloc(optlen + xoptlen + 2);
970
 
 
971
 
        if (opt != NULL) {
972
 
                memcpy(r, opt, optlen);
973
 
                r[optlen] = ' ';
974
 
                optlen++;
975
 
        }
976
 
 
977
 
        if (xopt != NULL)
978
 
                memcpy(r + optlen, xopt, xoptlen);
979
 
 
980
 
        r[optlen + xoptlen] = '\0';
981
 
 
982
 
        return r;
983
 
}
984
 
 
985
 
static int __kmod_module_get_probe_list(struct kmod_module *mod,
986
 
                                                bool ignorecmd,
987
 
                                                struct kmod_list **list);
988
 
 
989
 
/* re-entrant */
990
 
static int __kmod_module_fill_softdep(struct kmod_module *mod,
991
 
                                                struct kmod_list **list)
992
 
{
993
 
        struct kmod_list *pre = NULL, *post = NULL, *l;
994
 
        int err;
995
 
 
996
 
        err = kmod_module_get_softdeps(mod, &pre, &post);
997
 
        if (err < 0) {
998
 
                ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
999
 
                goto fail;
1000
 
        }
1001
 
 
1002
 
        kmod_list_foreach(l, pre) {
1003
 
                struct kmod_module *m = l->data;
1004
 
                err = __kmod_module_get_probe_list(m, false, list);
1005
 
                if (err < 0)
1006
 
                        goto fail;
1007
 
        }
1008
 
 
1009
 
        l = kmod_list_append(*list, kmod_module_ref(mod));
1010
 
        if (l == NULL) {
1011
 
                kmod_module_unref(mod);
1012
 
                err = -ENOMEM;
1013
 
                goto fail;
1014
 
        }
1015
 
        *list = l;
1016
 
        mod->ignorecmd = (pre != NULL || post != NULL);
1017
 
 
1018
 
        kmod_list_foreach(l, post) {
1019
 
                struct kmod_module *m = l->data;
1020
 
                err = __kmod_module_get_probe_list(m, false, list);
1021
 
                if (err < 0)
1022
 
                        goto fail;
1023
 
        }
1024
 
 
1025
 
fail:
1026
 
        kmod_module_unref_list(pre);
1027
 
        kmod_module_unref_list(post);
1028
 
 
1029
 
        return err;
1030
 
}
1031
 
 
1032
 
/* re-entrant */
1033
 
static int __kmod_module_get_probe_list(struct kmod_module *mod,
1034
 
                                                bool ignorecmd,
1035
 
                                                struct kmod_list **list)
1036
 
{
1037
 
        struct kmod_list *dep, *l;
1038
 
        int err = 0;
1039
 
 
1040
 
        if (mod->visited) {
1041
 
                DBG(mod->ctx, "Ignore module '%s': already visited\n",
1042
 
                                                                mod->name);
1043
 
                return 0;
1044
 
        }
1045
 
        mod->visited = true;
1046
 
 
1047
 
        dep = kmod_module_get_dependencies(mod);
1048
 
        kmod_list_foreach(l, dep) {
1049
 
                struct kmod_module *m = l->data;
1050
 
                err = __kmod_module_fill_softdep(m, list);
1051
 
                if (err < 0)
1052
 
                        goto finish;
1053
 
        }
1054
 
 
1055
 
        if (ignorecmd) {
1056
 
                l = kmod_list_append(*list, kmod_module_ref(mod));
1057
 
                if (l == NULL) {
1058
 
                        kmod_module_unref(mod);
1059
 
                        err = -ENOMEM;
1060
 
                        goto finish;
1061
 
                }
1062
 
                *list = l;
1063
 
                mod->ignorecmd = true;
1064
 
        } else
1065
 
                err = __kmod_module_fill_softdep(mod, list);
1066
 
 
1067
 
finish:
1068
 
        kmod_module_unref_list(dep);
1069
 
        return err;
1070
 
}
1071
 
 
1072
 
static int kmod_module_get_probe_list(struct kmod_module *mod,
1073
 
                                                bool ignorecmd,
1074
 
                                                struct kmod_list **list)
1075
 
{
1076
 
        int err;
1077
 
 
1078
 
        assert(mod != NULL);
1079
 
        assert(list != NULL && *list == NULL);
1080
 
 
1081
 
        /*
1082
 
         * Make sure we don't get screwed by previous calls to this function
1083
 
         */
1084
 
        kmod_set_modules_visited(mod->ctx, false);
1085
 
 
1086
 
        err = __kmod_module_get_probe_list(mod, ignorecmd, list);
1087
 
        if (err < 0) {
1088
 
                kmod_module_unref_list(*list);
1089
 
                *list = NULL;
1090
 
        }
1091
 
 
1092
 
        return err;
1093
 
}
1094
 
 
1095
 
/**
1096
 
 * kmod_module_probe_insert_module:
1097
 
 * @mod: kmod module
1098
 
 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1099
 
 * behavior of this function.
1100
 
 * @extra_options: module's options to pass to Linux Kernel. It applies only
1101
 
 * to @mod, not to its dependencies.
1102
 
 * @run_install: function to run when @mod is backed by an install command.
1103
 
 * @data: data to give back to @run_install callback
1104
 
 * @print_action: function to call with the action being taken (install or
1105
 
 * insmod). It's useful for tools like modprobe when running with verbose
1106
 
 * output or in dry-run mode.
1107
 
 *
1108
 
 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1109
 
 * install commands and applying blacklist.
1110
 
 *
1111
 
 * If @run_install is NULL, this function will fork and exec by calling
1112
 
 * system(3). Don't pass a NULL argument in @run_install if your binary is
1113
 
 * setuid/setgid (see warning in system(3)). If you need control over the
1114
 
 * execution of an install command, give a callback function instead.
1115
 
 *
1116
 
 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1117
 
 * failure.
1118
 
 */
1119
 
KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1120
 
                        unsigned int flags, const char *extra_options,
1121
 
                        int (*run_install)(struct kmod_module *m,
1122
 
                                                const char *cmd, void *data),
1123
 
                        const void *data,
1124
 
                        void (*print_action)(struct kmod_module *m,
1125
 
                                                bool install,
1126
 
                                                const char *options))
1127
 
{
1128
 
        struct kmod_list *list = NULL, *l;
1129
 
        struct probe_insert_cb cb;
1130
 
        int err;
1131
 
 
1132
 
        if (mod == NULL)
1133
 
                return -ENOENT;
1134
 
 
1135
 
        if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1136
 
                                        && module_is_inkernel(mod)) {
1137
 
                if (flags & KMOD_PROBE_FAIL_ON_LOADED)
1138
 
                        return -EEXIST;
1139
 
                else
1140
 
                        return 0;
1141
 
        }
1142
 
 
1143
 
        err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1144
 
                                        KMOD_PROBE_APPLY_BLACKLIST_ALL);
1145
 
        if (err != 0) {
1146
 
                if (module_is_blacklisted(mod))
1147
 
                        return err;
1148
 
        }
1149
 
 
1150
 
        err = kmod_module_get_probe_list(mod,
1151
 
                                !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
1152
 
        if (err < 0)
1153
 
                return err;
1154
 
 
1155
 
        if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1156
 
                struct kmod_list *filtered = NULL;
1157
 
 
1158
 
                err = kmod_module_get_filtered_blacklist(mod->ctx,
1159
 
                                                        list, &filtered);
1160
 
                if (err < 0)
1161
 
                        return err;
1162
 
 
1163
 
                kmod_module_unref_list(list);
1164
 
                if (filtered == NULL)
1165
 
                        return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1166
 
 
1167
 
                list = filtered;
1168
 
        }
1169
 
 
1170
 
        cb.run_install = run_install;
1171
 
        cb.data = (void *) data;
1172
 
 
1173
 
        kmod_list_foreach(l, list) {
1174
 
                struct kmod_module *m = l->data;
1175
 
                const char *moptions = kmod_module_get_options(m);
1176
 
                const char *cmd = kmod_module_get_install_commands(m);
1177
 
                char *options = module_options_concat(moptions,
1178
 
                                        m == mod ? extra_options : NULL);
1179
 
 
1180
 
                if (cmd != NULL && !m->ignorecmd) {
1181
 
                        if (print_action != NULL)
1182
 
                                print_action(m, true, options ?: "");
1183
 
 
1184
 
                        if (!(flags & KMOD_PROBE_DRY_RUN))
1185
 
                                err = module_do_install_commands(m, options,
1186
 
                                                                        &cb);
1187
 
                } else {
1188
 
                        if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1189
 
                                                && module_is_inkernel(m)) {
1190
 
                                DBG(mod->ctx, "Ignoring module '%s': "
1191
 
                                                "already loaded\n", m->name);
1192
 
                                err = -EEXIST;
1193
 
                                goto finish_module;
1194
 
                        }
1195
 
                        if (print_action != NULL)
1196
 
                                print_action(m, false, options ?: "");
1197
 
 
1198
 
                        if (!(flags & KMOD_PROBE_DRY_RUN))
1199
 
                                err = kmod_module_insert_module(m, flags,
1200
 
                                                                options);
1201
 
                }
1202
 
 
1203
 
finish_module:
1204
 
                free(options);
1205
 
 
1206
 
                /*
1207
 
                 * Treat "already loaded" error. If we were told to stop on
1208
 
                 * already loaded and the module being loaded is not a softdep
1209
 
                 * or dep, bail out. Otherwise, just ignore and continue.
1210
 
                 *
1211
 
                 * We need to check here because of race conditions. We
1212
 
                 * checked first if module was already loaded but it may have
1213
 
                 * been loaded between the check and the moment we try to
1214
 
                 * insert it.
1215
 
                 */
1216
 
                if (err == -EEXIST && m == mod &&
1217
 
                                (flags & KMOD_PROBE_FAIL_ON_LOADED))
1218
 
                        break;
1219
 
 
1220
 
                if (err == -EEXIST)
1221
 
                        err = 0;
1222
 
                else if (err < 0)
1223
 
                        break;
1224
 
        }
1225
 
 
1226
 
        kmod_module_unref_list(list);
1227
 
        return err;
1228
 
}
1229
 
 
1230
 
/**
1231
 
 * kmod_module_get_options:
1232
 
 * @mod: kmod module
1233
 
 *
1234
 
 * Get options of this kmod module. Options come from the configuration file
1235
 
 * and are cached in @mod. The first call to this function will search for
1236
 
 * this module in configuration and subsequent calls return the cached string.
1237
 
 *
1238
 
 * Returns: a string with all the options separated by spaces. This string is
1239
 
 * owned by @mod, do not free it.
1240
 
 */
1241
 
KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1242
 
{
1243
 
        if (mod == NULL)
1244
 
                return NULL;
1245
 
 
1246
 
        if (!mod->init.options) {
1247
 
                /* lazy init */
1248
 
                struct kmod_module *m = (struct kmod_module *)mod;
1249
 
                const struct kmod_list *l, *ctx_options;
1250
 
                char *opts = NULL;
1251
 
                size_t optslen = 0;
1252
 
 
1253
 
                ctx_options = kmod_get_options(mod->ctx);
1254
 
 
1255
 
                kmod_list_foreach(l, ctx_options) {
1256
 
                        const char *modname = kmod_option_get_modname(l);
1257
 
                        const char *str;
1258
 
                        size_t len;
1259
 
                        void *tmp;
1260
 
 
1261
 
                        DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1262
 
                        if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1263
 
                                                streq(modname, mod->alias))))
1264
 
                                continue;
1265
 
 
1266
 
                        DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1267
 
                        str = kmod_option_get_options(l);
1268
 
                        len = strlen(str);
1269
 
                        if (len < 1)
1270
 
                                continue;
1271
 
 
1272
 
                        tmp = realloc(opts, optslen + len + 2);
1273
 
                        if (tmp == NULL) {
1274
 
                                free(opts);
1275
 
                                goto failed;
1276
 
                        }
1277
 
 
1278
 
                        opts = tmp;
1279
 
 
1280
 
                        if (optslen > 0) {
1281
 
                                opts[optslen] = ' ';
1282
 
                                optslen++;
1283
 
                        }
1284
 
 
1285
 
                        memcpy(opts + optslen, str, len);
1286
 
                        optslen += len;
1287
 
                        opts[optslen] = '\0';
1288
 
                }
1289
 
 
1290
 
                m->init.options = true;
1291
 
                m->options = opts;
1292
 
        }
1293
 
 
1294
 
        return mod->options;
1295
 
 
1296
 
failed:
1297
 
        ERR(mod->ctx, "out of memory\n");
1298
 
        return NULL;
1299
 
}
1300
 
 
1301
 
/**
1302
 
 * kmod_module_get_install_commands:
1303
 
 * @mod: kmod module
1304
 
 *
1305
 
 * Get install commands for this kmod module. Install commands come from the
1306
 
 * configuration file and are cached in @mod. The first call to this function
1307
 
 * will search for this module in configuration and subsequent calls return
1308
 
 * the cached string. The install commands are returned as they were in the
1309
 
 * configuration, concatenated by ';'. No other processing is made in this
1310
 
 * string.
1311
 
 *
1312
 
 * Returns: a string with all install commands separated by semicolons. This
1313
 
 * string is owned by @mod, do not free it.
1314
 
 */
1315
 
KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1316
 
{
1317
 
        if (mod == NULL)
1318
 
                return NULL;
1319
 
 
1320
 
        if (!mod->init.install_commands) {
1321
 
                /* lazy init */
1322
 
                struct kmod_module *m = (struct kmod_module *)mod;
1323
 
                const struct kmod_list *l, *ctx_install_commands;
1324
 
 
1325
 
                ctx_install_commands = kmod_get_install_commands(mod->ctx);
1326
 
 
1327
 
                kmod_list_foreach(l, ctx_install_commands) {
1328
 
                        const char *modname = kmod_command_get_modname(l);
1329
 
 
1330
 
                        if (fnmatch(modname, mod->name, 0) != 0)
1331
 
                                continue;
1332
 
 
1333
 
                        m->install_commands = kmod_command_get_command(l);
1334
 
 
1335
 
                        /*
1336
 
                         * find only the first command, as modprobe from
1337
 
                         * module-init-tools does
1338
 
                         */
1339
 
                        break;
1340
 
                }
1341
 
 
1342
 
                m->init.install_commands = true;
1343
 
        }
1344
 
 
1345
 
        return mod->install_commands;
1346
 
}
1347
 
 
1348
 
void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1349
 
{
1350
 
        mod->init.install_commands = true;
1351
 
        mod->install_commands = cmd;
1352
 
}
1353
 
 
1354
 
static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1355
 
{
1356
 
        struct kmod_list *ret = NULL;
1357
 
        unsigned i;
1358
 
 
1359
 
        for (i = 0; i < count; i++) {
1360
 
                const char *depname = array[i];
1361
 
                struct kmod_list *lst = NULL;
1362
 
                int err;
1363
 
 
1364
 
                err = kmod_module_new_from_lookup(ctx, depname, &lst);
1365
 
                if (err < 0) {
1366
 
                        ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1367
 
                        continue;
1368
 
                } else if (lst != NULL)
1369
 
                        ret = kmod_list_append_list(ret, lst);
1370
 
        }
1371
 
        return ret;
1372
 
}
1373
 
 
1374
 
/**
1375
 
 * kmod_module_get_softdeps:
1376
 
 * @mod: kmod module
1377
 
 * @pre: where to save the list of preceding soft dependencies.
1378
 
 * @post: where to save the list of post soft dependencies.
1379
 
 *
1380
 
 * Get soft dependencies for this kmod module. Soft dependencies come
1381
 
 * from configuration file and are not cached in @mod because it may include
1382
 
 * dependency cycles that would make we leak kmod_module. Any call
1383
 
 * to this function will search for this module in configuration, allocate a
1384
 
 * list and return the result.
1385
 
 *
1386
 
 * Both @pre and @post are newly created list of kmod_module and
1387
 
 * should be unreferenced with kmod_module_unref_list().
1388
 
 *
1389
 
 * Returns: 0 on success or < 0 otherwise.
1390
 
 */
1391
 
KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1392
 
                                                struct kmod_list **pre,
1393
 
                                                struct kmod_list **post)
1394
 
{
1395
 
        const struct kmod_list *l, *ctx_softdeps;
1396
 
 
1397
 
        if (mod == NULL || pre == NULL || post == NULL)
1398
 
                return -ENOENT;
1399
 
 
1400
 
        assert(*pre == NULL);
1401
 
        assert(*post == NULL);
1402
 
 
1403
 
        ctx_softdeps = kmod_get_softdeps(mod->ctx);
1404
 
 
1405
 
        kmod_list_foreach(l, ctx_softdeps) {
1406
 
                const char *modname = kmod_softdep_get_name(l);
1407
 
                const char * const *array;
1408
 
                unsigned count;
1409
 
 
1410
 
                if (fnmatch(modname, mod->name, 0) != 0)
1411
 
                        continue;
1412
 
 
1413
 
                array = kmod_softdep_get_pre(l, &count);
1414
 
                *pre = lookup_softdep(mod->ctx, array, count);
1415
 
                array = kmod_softdep_get_post(l, &count);
1416
 
                *post = lookup_softdep(mod->ctx, array, count);
1417
 
 
1418
 
                /*
1419
 
                 * find only the first command, as modprobe from
1420
 
                 * module-init-tools does
1421
 
                 */
1422
 
                break;
1423
 
        }
1424
 
 
1425
 
        return 0;
1426
 
}
1427
 
 
1428
 
/**
1429
 
 * kmod_module_get_remove_commands:
1430
 
 * @mod: kmod module
1431
 
 *
1432
 
 * Get remove commands for this kmod module. Remove commands come from the
1433
 
 * configuration file and are cached in @mod. The first call to this function
1434
 
 * will search for this module in configuration and subsequent calls return
1435
 
 * the cached string. The remove commands are returned as they were in the
1436
 
 * configuration, concatenated by ';'. No other processing is made in this
1437
 
 * string.
1438
 
 *
1439
 
 * Returns: a string with all remove commands separated by semicolons. This
1440
 
 * string is owned by @mod, do not free it.
1441
 
 */
1442
 
KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1443
 
{
1444
 
        if (mod == NULL)
1445
 
                return NULL;
1446
 
 
1447
 
        if (!mod->init.remove_commands) {
1448
 
                /* lazy init */
1449
 
                struct kmod_module *m = (struct kmod_module *)mod;
1450
 
                const struct kmod_list *l, *ctx_remove_commands;
1451
 
 
1452
 
                ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1453
 
 
1454
 
                kmod_list_foreach(l, ctx_remove_commands) {
1455
 
                        const char *modname = kmod_command_get_modname(l);
1456
 
 
1457
 
                        if (fnmatch(modname, mod->name, 0) != 0)
1458
 
                                continue;
1459
 
 
1460
 
                        m->remove_commands = kmod_command_get_command(l);
1461
 
 
1462
 
                        /*
1463
 
                         * find only the first command, as modprobe from
1464
 
                         * module-init-tools does
1465
 
                         */
1466
 
                        break;
1467
 
                }
1468
 
 
1469
 
                m->init.remove_commands = true;
1470
 
        }
1471
 
 
1472
 
        return mod->remove_commands;
1473
 
}
1474
 
 
1475
 
void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1476
 
{
1477
 
        mod->init.remove_commands = true;
1478
 
        mod->remove_commands = cmd;
1479
 
}
1480
 
 
1481
 
/**
1482
 
 * SECTION:libkmod-loaded
1483
 
 * @short_description: currently loaded modules
1484
 
 *
1485
 
 * Information about currently loaded modules, as reported by Linux kernel.
1486
 
 * These information are not cached by libkmod and are always read from /sys
1487
 
 * and /proc/modules.
1488
 
 */
1489
 
 
1490
 
/**
1491
 
 * kmod_module_new_from_loaded:
1492
 
 * @ctx: kmod library context
1493
 
 * @list: where to save the list of loaded modules
1494
 
 *
1495
 
 * Create a new list of kmod modules with all modules currently loaded in
1496
 
 * kernel. It uses /proc/modules to get the names of loaded modules and to
1497
 
 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1498
 
 * They are put are put in @list in no particular order.
1499
 
 *
1500
 
 * The initial refcount is 1, and needs to be decremented to release the
1501
 
 * resources of the kmod_module. The returned @list must be released by
1502
 
 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1503
 
 * kmod_modules created, they are all released upon @ctx destruction too. Do
1504
 
 * not unref @ctx before all the desired operations with the returned list are
1505
 
 * completed.
1506
 
 *
1507
 
 * Returns: 0 on success or < 0 on error.
1508
 
 */
1509
 
KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1510
 
                                                struct kmod_list **list)
1511
 
{
1512
 
        struct kmod_list *l = NULL;
1513
 
        FILE *fp;
1514
 
        char line[4096];
1515
 
 
1516
 
        if (ctx == NULL || list == NULL)
1517
 
                return -ENOENT;
1518
 
 
1519
 
        fp = fopen("/proc/modules", "re");
1520
 
        if (fp == NULL) {
1521
 
                int err = -errno;
1522
 
                ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1523
 
                return err;
1524
 
        }
1525
 
 
1526
 
        while (fgets(line, sizeof(line), fp)) {
1527
 
                struct kmod_module *m;
1528
 
                struct kmod_list *node;
1529
 
                int err;
1530
 
                char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1531
 
 
1532
 
                err = kmod_module_new_from_name(ctx, name, &m);
1533
 
                if (err < 0) {
1534
 
                        ERR(ctx, "could not get module from name '%s': %s\n",
1535
 
                                name, strerror(-err));
1536
 
                        continue;
1537
 
                }
1538
 
 
1539
 
                node = kmod_list_append(l, m);
1540
 
                if (node)
1541
 
                        l = node;
1542
 
                else {
1543
 
                        ERR(ctx, "out of memory\n");
1544
 
                        kmod_module_unref(m);
1545
 
                }
1546
 
        }
1547
 
 
1548
 
        fclose(fp);
1549
 
        *list = l;
1550
 
 
1551
 
        return 0;
1552
 
}
1553
 
 
1554
 
/**
1555
 
 * kmod_module_initstate_str:
1556
 
 * @state: the state as returned by kmod_module_get_initstate()
1557
 
 *
1558
 
 * Translate a initstate to a string.
1559
 
 *
1560
 
 * Returns: the string associated to the @state. This string is statically
1561
 
 * allocated, do not free it.
1562
 
 */
1563
 
KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1564
 
{
1565
 
        switch (state) {
1566
 
        case KMOD_MODULE_BUILTIN:
1567
 
                return "builtin";
1568
 
        case KMOD_MODULE_LIVE:
1569
 
                return "live";
1570
 
        case KMOD_MODULE_COMING:
1571
 
                return "coming";
1572
 
        case KMOD_MODULE_GOING:
1573
 
                return "going";
1574
 
        default:
1575
 
                return NULL;
1576
 
        }
1577
 
}
1578
 
 
1579
 
/**
1580
 
 * kmod_module_get_initstate:
1581
 
 * @mod: kmod module
1582
 
 *
1583
 
 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1584
 
 * /sys filesystem.
1585
 
 *
1586
 
 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1587
 
 */
1588
 
KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1589
 
{
1590
 
        char path[PATH_MAX], buf[32];
1591
 
        int fd, err, pathlen;
1592
 
 
1593
 
        if (mod == NULL)
1594
 
                return -ENOENT;
1595
 
 
1596
 
        pathlen = snprintf(path, sizeof(path),
1597
 
                                "/sys/module/%s/initstate", mod->name);
1598
 
        fd = open(path, O_RDONLY|O_CLOEXEC);
1599
 
        if (fd < 0) {
1600
 
                err = -errno;
1601
 
 
1602
 
                DBG(mod->ctx, "could not open '%s': %s\n",
1603
 
                        path, strerror(-err));
1604
 
 
1605
 
                if (pathlen > (int)sizeof("/initstate") - 1) {
1606
 
                        struct stat st;
1607
 
                        path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1608
 
                        if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1609
 
                                return KMOD_MODULE_BUILTIN;
1610
 
                }
1611
 
 
1612
 
                DBG(mod->ctx, "could not open '%s': %s\n",
1613
 
                        path, strerror(-err));
1614
 
                return err;
1615
 
        }
1616
 
 
1617
 
        err = read_str_safe(fd, buf, sizeof(buf));
1618
 
        close(fd);
1619
 
        if (err < 0) {
1620
 
                ERR(mod->ctx, "could not read from '%s': %s\n",
1621
 
                        path, strerror(-err));
1622
 
                return err;
1623
 
        }
1624
 
 
1625
 
        if (streq(buf, "live\n"))
1626
 
                return KMOD_MODULE_LIVE;
1627
 
        else if (streq(buf, "coming\n"))
1628
 
                return KMOD_MODULE_COMING;
1629
 
        else if (streq(buf, "going\n"))
1630
 
                return KMOD_MODULE_GOING;
1631
 
 
1632
 
        ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1633
 
        return -EINVAL;
1634
 
}
1635
 
 
1636
 
/**
1637
 
 * kmod_module_get_size:
1638
 
 * @mod: kmod module
1639
 
 *
1640
 
 * Get the size of this kmod module as returned by Linux kernel. It reads the
1641
 
 * file /proc/modules to search for this module and get its size.
1642
 
 *
1643
 
 * Returns: the size of this kmod module.
1644
 
 */
1645
 
KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1646
 
{
1647
 
        // FIXME TODO: this should be available from /sys/module/foo
1648
 
        FILE *fp;
1649
 
        char line[4096];
1650
 
        int lineno = 0;
1651
 
        long size = -ENOENT;
1652
 
 
1653
 
        if (mod == NULL)
1654
 
                return -ENOENT;
1655
 
 
1656
 
        fp = fopen("/proc/modules", "re");
1657
 
        if (fp == NULL) {
1658
 
                int err = -errno;
1659
 
                ERR(mod->ctx,
1660
 
                    "could not open /proc/modules: %s\n", strerror(errno));
1661
 
                return err;
1662
 
        }
1663
 
 
1664
 
        while (fgets(line, sizeof(line), fp)) {
1665
 
                char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1666
 
                long value;
1667
 
 
1668
 
                lineno++;
1669
 
                if (tok == NULL || !streq(tok, mod->name))
1670
 
                        continue;
1671
 
 
1672
 
                tok = strtok_r(NULL, " \t", &saveptr);
1673
 
                if (tok == NULL) {
1674
 
                        ERR(mod->ctx,
1675
 
                        "invalid line format at /proc/modules:%d\n", lineno);
1676
 
                        break;
1677
 
                }
1678
 
 
1679
 
                value = strtol(tok, &endptr, 10);
1680
 
                if (endptr == tok || *endptr != '\0') {
1681
 
                        ERR(mod->ctx,
1682
 
                        "invalid line format at /proc/modules:%d\n", lineno);
1683
 
                        break;
1684
 
                }
1685
 
 
1686
 
                size = value;
1687
 
                break;
1688
 
        }
1689
 
        fclose(fp);
1690
 
        return size;
1691
 
}
1692
 
 
1693
 
/**
1694
 
 * kmod_module_get_refcnt:
1695
 
 * @mod: kmod module
1696
 
 *
1697
 
 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1698
 
 * /sys filesystem.
1699
 
 *
1700
 
 * Returns: 0 on success or < 0 on failure.
1701
 
 */
1702
 
KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1703
 
{
1704
 
        char path[PATH_MAX];
1705
 
        long refcnt;
1706
 
        int fd, err;
1707
 
 
1708
 
        if (mod == NULL)
1709
 
                return -ENOENT;
1710
 
 
1711
 
        snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
1712
 
        fd = open(path, O_RDONLY|O_CLOEXEC);
1713
 
        if (fd < 0) {
1714
 
                err = -errno;
1715
 
                ERR(mod->ctx, "could not open '%s': %s\n",
1716
 
                        path, strerror(errno));
1717
 
                return err;
1718
 
        }
1719
 
 
1720
 
        err = read_str_long(fd, &refcnt, 10);
1721
 
        close(fd);
1722
 
        if (err < 0) {
1723
 
                ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1724
 
                        path, strerror(-err));
1725
 
                return err;
1726
 
        }
1727
 
 
1728
 
        return (int)refcnt;
1729
 
}
1730
 
 
1731
 
/**
1732
 
 * kmod_module_get_holders:
1733
 
 * @mod: kmod module
1734
 
 *
1735
 
 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1736
 
 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1737
 
 *
1738
 
 * Returns: a new list of kmod modules on success or NULL on failure.
1739
 
 */
1740
 
KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1741
 
{
1742
 
        char dname[PATH_MAX];
1743
 
        struct kmod_list *list = NULL;
1744
 
        DIR *d;
1745
 
 
1746
 
        if (mod == NULL)
1747
 
                return NULL;
1748
 
 
1749
 
        snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1750
 
 
1751
 
        d = opendir(dname);
1752
 
        if (d == NULL) {
1753
 
                ERR(mod->ctx, "could not open '%s': %s\n",
1754
 
                                                dname, strerror(errno));
1755
 
                return NULL;
1756
 
        }
1757
 
 
1758
 
        for (;;) {
1759
 
                struct dirent de, *entp;
1760
 
                struct kmod_module *holder;
1761
 
                struct kmod_list *l;
1762
 
                int err;
1763
 
 
1764
 
                err = readdir_r(d, &de, &entp);
1765
 
                if (err != 0) {
1766
 
                        ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1767
 
                                                mod->name, strerror(-err));
1768
 
                        goto fail;
1769
 
                }
1770
 
 
1771
 
                if (entp == NULL)
1772
 
                        break;
1773
 
 
1774
 
                if (de.d_name[0] == '.') {
1775
 
                        if (de.d_name[1] == '\0' ||
1776
 
                            (de.d_name[1] == '.' && de.d_name[2] == '\0'))
1777
 
                                continue;
1778
 
                }
1779
 
 
1780
 
                err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
1781
 
                if (err < 0) {
1782
 
                        ERR(mod->ctx, "could not create module for '%s': %s\n",
1783
 
                                de.d_name, strerror(-err));
1784
 
                        goto fail;
1785
 
                }
1786
 
 
1787
 
                l = kmod_list_append(list, holder);
1788
 
                if (l != NULL) {
1789
 
                        list = l;
1790
 
                } else {
1791
 
                        ERR(mod->ctx, "out of memory\n");
1792
 
                        kmod_module_unref(holder);
1793
 
                        goto fail;
1794
 
                }
1795
 
        }
1796
 
 
1797
 
        closedir(d);
1798
 
        return list;
1799
 
 
1800
 
fail:
1801
 
        closedir(d);
1802
 
        kmod_module_unref_list(list);
1803
 
        return NULL;
1804
 
}
1805
 
 
1806
 
struct kmod_module_section {
1807
 
        unsigned long address;
1808
 
        char name[];
1809
 
};
1810
 
 
1811
 
static void kmod_module_section_free(struct kmod_module_section *section)
1812
 
{
1813
 
        free(section);
1814
 
}
1815
 
 
1816
 
/**
1817
 
 * kmod_module_get_sections:
1818
 
 * @mod: kmod module
1819
 
 *
1820
 
 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1821
 
 * structure contained in this list is internal to libkmod and their fields
1822
 
 * can be obtained by calling kmod_module_section_get_name() and
1823
 
 * kmod_module_section_get_address().
1824
 
 *
1825
 
 * After use, free the @list by calling kmod_module_section_free_list().
1826
 
 *
1827
 
 * Returns: a new list of kmod module sections on success or NULL on failure.
1828
 
 */
1829
 
KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1830
 
{
1831
 
        char dname[PATH_MAX];
1832
 
        struct kmod_list *list = NULL;
1833
 
        DIR *d;
1834
 
        int dfd;
1835
 
 
1836
 
        if (mod == NULL)
1837
 
                return NULL;
1838
 
 
1839
 
        snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1840
 
 
1841
 
        d = opendir(dname);
1842
 
        if (d == NULL) {
1843
 
                ERR(mod->ctx, "could not open '%s': %s\n",
1844
 
                        dname, strerror(errno));
1845
 
                return NULL;
1846
 
        }
1847
 
 
1848
 
        dfd = dirfd(d);
1849
 
 
1850
 
        for (;;) {
1851
 
                struct dirent de, *entp;
1852
 
                struct kmod_module_section *section;
1853
 
                struct kmod_list *l;
1854
 
                unsigned long address;
1855
 
                size_t namesz;
1856
 
                int fd, err;
1857
 
 
1858
 
                err = readdir_r(d, &de, &entp);
1859
 
                if (err != 0) {
1860
 
                        ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1861
 
                                                mod->name, strerror(-err));
1862
 
                        goto fail;
1863
 
                }
1864
 
 
1865
 
                if (de.d_name[0] == '.') {
1866
 
                        if (de.d_name[1] == '\0' ||
1867
 
                            (de.d_name[1] == '.' && de.d_name[2] == '\0'))
1868
 
                                continue;
1869
 
                }
1870
 
 
1871
 
                fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
1872
 
                if (fd < 0) {
1873
 
                        ERR(mod->ctx, "could not open '%s/%s': %m\n",
1874
 
                                                        dname, de.d_name);
1875
 
                        goto fail;
1876
 
                }
1877
 
 
1878
 
                err = read_str_ulong(fd, &address, 16);
1879
 
                close(fd);
1880
 
                if (err < 0) {
1881
 
                        ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1882
 
                                                        dname, de.d_name);
1883
 
                        goto fail;
1884
 
                }
1885
 
 
1886
 
                namesz = strlen(de.d_name) + 1;
1887
 
                section = malloc(sizeof(*section) + namesz);
1888
 
 
1889
 
                if (section == NULL) {
1890
 
                        ERR(mod->ctx, "out of memory\n");
1891
 
                        goto fail;
1892
 
                }
1893
 
 
1894
 
                section->address = address;
1895
 
                memcpy(section->name, de.d_name, namesz);
1896
 
 
1897
 
                l = kmod_list_append(list, section);
1898
 
                if (l != NULL) {
1899
 
                        list = l;
1900
 
                } else {
1901
 
                        ERR(mod->ctx, "out of memory\n");
1902
 
                        free(section);
1903
 
                        goto fail;
1904
 
                }
1905
 
        }
1906
 
 
1907
 
        closedir(d);
1908
 
        return list;
1909
 
 
1910
 
fail:
1911
 
        closedir(d);
1912
 
        kmod_module_unref_list(list);
1913
 
        return NULL;
1914
 
}
1915
 
 
1916
 
/**
1917
 
 * kmod_module_section_get_module_name:
1918
 
 * @entry: a list entry representing a kmod module section
1919
 
 *
1920
 
 * Get the name of a kmod module section.
1921
 
 *
1922
 
 * After use, free the @list by calling kmod_module_section_free_list().
1923
 
 *
1924
 
 * Returns: the name of this kmod module section on success or NULL on
1925
 
 * failure. The string is owned by the section, do not free it.
1926
 
 */
1927
 
KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1928
 
{
1929
 
        struct kmod_module_section *section;
1930
 
 
1931
 
        if (entry == NULL)
1932
 
                return NULL;
1933
 
 
1934
 
        section = entry->data;
1935
 
        return section->name;
1936
 
}
1937
 
 
1938
 
/**
1939
 
 * kmod_module_section_get_address:
1940
 
 * @entry: a list entry representing a kmod module section
1941
 
 *
1942
 
 * Get the address of a kmod module section.
1943
 
 *
1944
 
 * After use, free the @list by calling kmod_module_section_free_list().
1945
 
 *
1946
 
 * Returns: the address of this kmod module section on success or ULONG_MAX
1947
 
 * on failure.
1948
 
 */
1949
 
KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1950
 
{
1951
 
        struct kmod_module_section *section;
1952
 
 
1953
 
        if (entry == NULL)
1954
 
                return (unsigned long)-1;
1955
 
 
1956
 
        section = entry->data;
1957
 
        return section->address;
1958
 
}
1959
 
 
1960
 
/**
1961
 
 * kmod_module_section_free_list:
1962
 
 * @list: kmod module section list
1963
 
 *
1964
 
 * Release the resources taken by @list
1965
 
 */
1966
 
KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1967
 
{
1968
 
        while (list) {
1969
 
                kmod_module_section_free(list->data);
1970
 
                list = kmod_list_remove(list);
1971
 
        }
1972
 
}
1973
 
 
1974
 
struct kmod_module_info {
1975
 
        char *key;
1976
 
        char value[];
1977
 
};
1978
 
 
1979
 
static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1980
 
{
1981
 
        struct kmod_module_info *info;
1982
 
 
1983
 
        info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1984
 
        if (info == NULL)
1985
 
                return NULL;
1986
 
 
1987
 
        info->key = (char *)info + sizeof(struct kmod_module_info)
1988
 
                + valuelen + 1;
1989
 
        memcpy(info->key, key, keylen);
1990
 
        info->key[keylen] = '\0';
1991
 
        memcpy(info->value, value, valuelen);
1992
 
        info->value[valuelen] = '\0';
1993
 
        return info;
1994
 
}
1995
 
 
1996
 
static void kmod_module_info_free(struct kmod_module_info *info)
1997
 
{
1998
 
        free(info);
1999
 
}
2000
 
 
2001
 
/**
2002
 
 * kmod_module_get_info:
2003
 
 * @mod: kmod module
2004
 
 * @list: where to return list of module information. Use
2005
 
 *        kmod_module_info_get_key() and
2006
 
 *        kmod_module_info_get_value(). Release this list with
2007
 
 *        kmod_module_info_free_list()
2008
 
 *
2009
 
 * Get a list of entries in ELF section ".modinfo", these contain
2010
 
 * alias, license, depends, vermagic and other keys with respective
2011
 
 * values.
2012
 
 *
2013
 
 * After use, free the @list by calling kmod_module_info_free_list().
2014
 
 *
2015
 
 * Returns: 0 on success or < 0 otherwise.
2016
 
 */
2017
 
KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2018
 
{
2019
 
        struct kmod_file *file;
2020
 
        struct kmod_elf *elf;
2021
 
        const char *path;
2022
 
        const void *mem;
2023
 
        char **strings;
2024
 
        size_t size;
2025
 
        int i, count, ret = 0;
2026
 
 
2027
 
        if (mod == NULL || list == NULL)
2028
 
                return -ENOENT;
2029
 
 
2030
 
        assert(*list == NULL);
2031
 
 
2032
 
        path = kmod_module_get_path(mod);
2033
 
        if (path == NULL)
2034
 
                return -ENOENT;
2035
 
 
2036
 
        file = kmod_file_open(mod->ctx, path);
2037
 
        if (file == NULL)
2038
 
                return -errno;
2039
 
 
2040
 
        size = kmod_file_get_size(file);
2041
 
        mem = kmod_file_get_contents(file);
2042
 
 
2043
 
        elf = kmod_elf_new(mem, size);
2044
 
        if (elf == NULL) {
2045
 
                ret = -errno;
2046
 
                goto elf_open_error;
2047
 
        }
2048
 
 
2049
 
        count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2050
 
        if (count < 0) {
2051
 
                ret = count;
2052
 
                goto get_strings_error;
2053
 
        }
2054
 
 
2055
 
        for (i = 0; i < count; i++) {
2056
 
                struct kmod_module_info *info;
2057
 
                struct kmod_list *n;
2058
 
                const char *key, *value;
2059
 
                size_t keylen, valuelen;
2060
 
 
2061
 
                key = strings[i];
2062
 
                value = strchr(key, '=');
2063
 
                if (value == NULL) {
2064
 
                        keylen = strlen(key);
2065
 
                        valuelen = 0;
2066
 
                } else {
2067
 
                        keylen = value - key;
2068
 
                        value++;
2069
 
                        valuelen = strlen(value);
2070
 
                }
2071
 
 
2072
 
                info = kmod_module_info_new(key, keylen, value, valuelen);
2073
 
                if (info == NULL) {
2074
 
                        ret = -errno;
2075
 
                        kmod_module_info_free_list(*list);
2076
 
                        *list = NULL;
2077
 
                        goto list_error;
2078
 
                }
2079
 
 
2080
 
                n = kmod_list_append(*list, info);
2081
 
                if (n != NULL)
2082
 
                        *list = n;
2083
 
                else {
2084
 
                        kmod_module_info_free(info);
2085
 
                        kmod_module_info_free_list(*list);
2086
 
                        *list = NULL;
2087
 
                        ret = -ENOMEM;
2088
 
                        goto list_error;
2089
 
                }
2090
 
        }
2091
 
        ret = count;
2092
 
 
2093
 
list_error:
2094
 
        free(strings);
2095
 
get_strings_error:
2096
 
        kmod_elf_unref(elf);
2097
 
elf_open_error:
2098
 
        kmod_file_unref(file);
2099
 
 
2100
 
        return ret;
2101
 
}
2102
 
 
2103
 
/**
2104
 
 * kmod_module_info_get_key:
2105
 
 * @entry: a list entry representing a kmod module info
2106
 
 *
2107
 
 * Get the key of a kmod module info.
2108
 
 *
2109
 
 * Returns: the key of this kmod module info on success or NULL on
2110
 
 * failure. The string is owned by the info, do not free it.
2111
 
 */
2112
 
KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2113
 
{
2114
 
        struct kmod_module_info *info;
2115
 
 
2116
 
        if (entry == NULL)
2117
 
                return NULL;
2118
 
 
2119
 
        info = entry->data;
2120
 
        return info->key;
2121
 
}
2122
 
 
2123
 
/**
2124
 
 * kmod_module_info_get_value:
2125
 
 * @entry: a list entry representing a kmod module info
2126
 
 *
2127
 
 * Get the value of a kmod module info.
2128
 
 *
2129
 
 * Returns: the value of this kmod module info on success or NULL on
2130
 
 * failure. The string is owned by the info, do not free it.
2131
 
 */
2132
 
KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2133
 
{
2134
 
        struct kmod_module_info *info;
2135
 
 
2136
 
        if (entry == NULL)
2137
 
                return NULL;
2138
 
 
2139
 
        info = entry->data;
2140
 
        return info->value;
2141
 
}
2142
 
 
2143
 
/**
2144
 
 * kmod_module_info_free_list:
2145
 
 * @list: kmod module info list
2146
 
 *
2147
 
 * Release the resources taken by @list
2148
 
 */
2149
 
KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2150
 
{
2151
 
        while (list) {
2152
 
                kmod_module_info_free(list->data);
2153
 
                list = kmod_list_remove(list);
2154
 
        }
2155
 
}
2156
 
 
2157
 
struct kmod_module_version {
2158
 
        uint64_t crc;
2159
 
        char symbol[];
2160
 
};
2161
 
 
2162
 
static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2163
 
{
2164
 
        struct kmod_module_version *mv;
2165
 
        size_t symbollen = strlen(symbol) + 1;
2166
 
 
2167
 
        mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2168
 
        if (mv == NULL)
2169
 
                return NULL;
2170
 
 
2171
 
        mv->crc = crc;
2172
 
        memcpy(mv->symbol, symbol, symbollen);
2173
 
        return mv;
2174
 
}
2175
 
 
2176
 
static void kmod_module_version_free(struct kmod_module_version *version)
2177
 
{
2178
 
        free(version);
2179
 
}
2180
 
 
2181
 
/**
2182
 
 * kmod_module_get_versions:
2183
 
 * @mod: kmod module
2184
 
 * @list: where to return list of module versions. Use
2185
 
 *        kmod_module_version_get_symbol() and
2186
 
 *        kmod_module_version_get_crc(). Release this list with
2187
 
 *        kmod_module_versions_free_list()
2188
 
 *
2189
 
 * Get a list of entries in ELF section "__versions".
2190
 
 *
2191
 
 * After use, free the @list by calling kmod_module_versions_free_list().
2192
 
 *
2193
 
 * Returns: 0 on success or < 0 otherwise.
2194
 
 */
2195
 
KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2196
 
{
2197
 
        struct kmod_file *file;
2198
 
        struct kmod_elf *elf;
2199
 
        const char *path;
2200
 
        const void *mem;
2201
 
        struct kmod_modversion *versions;
2202
 
        size_t size;
2203
 
        int i, count, ret = 0;
2204
 
 
2205
 
        if (mod == NULL || list == NULL)
2206
 
                return -ENOENT;
2207
 
 
2208
 
        assert(*list == NULL);
2209
 
 
2210
 
        path = kmod_module_get_path(mod);
2211
 
        if (path == NULL)
2212
 
                return -ENOENT;
2213
 
 
2214
 
        file = kmod_file_open(mod->ctx, path);
2215
 
        if (file == NULL)
2216
 
                return -errno;
2217
 
 
2218
 
        size = kmod_file_get_size(file);
2219
 
        mem = kmod_file_get_contents(file);
2220
 
 
2221
 
        elf = kmod_elf_new(mem, size);
2222
 
        if (elf == NULL) {
2223
 
                ret = -errno;
2224
 
                goto elf_open_error;
2225
 
        }
2226
 
 
2227
 
        count = kmod_elf_get_modversions(elf, &versions);
2228
 
        if (count < 0) {
2229
 
                ret = count;
2230
 
                goto get_strings_error;
2231
 
        }
2232
 
 
2233
 
        for (i = 0; i < count; i++) {
2234
 
                struct kmod_module_version *mv;
2235
 
                struct kmod_list *n;
2236
 
 
2237
 
                mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2238
 
                if (mv == NULL) {
2239
 
                        ret = -errno;
2240
 
                        kmod_module_versions_free_list(*list);
2241
 
                        *list = NULL;
2242
 
                        goto list_error;
2243
 
                }
2244
 
 
2245
 
                n = kmod_list_append(*list, mv);
2246
 
                if (n != NULL)
2247
 
                        *list = n;
2248
 
                else {
2249
 
                        kmod_module_version_free(mv);
2250
 
                        kmod_module_versions_free_list(*list);
2251
 
                        *list = NULL;
2252
 
                        ret = -ENOMEM;
2253
 
                        goto list_error;
2254
 
                }
2255
 
        }
2256
 
        ret = count;
2257
 
 
2258
 
list_error:
2259
 
        free(versions);
2260
 
get_strings_error:
2261
 
        kmod_elf_unref(elf);
2262
 
elf_open_error:
2263
 
        kmod_file_unref(file);
2264
 
 
2265
 
        return ret;
2266
 
}
2267
 
 
2268
 
/**
2269
 
 * kmod_module_versions_get_symbol:
2270
 
 * @entry: a list entry representing a kmod module versions
2271
 
 *
2272
 
 * Get the symbol of a kmod module versions.
2273
 
 *
2274
 
 * Returns: the symbol of this kmod module versions on success or NULL
2275
 
 * on failure. The string is owned by the versions, do not free it.
2276
 
 */
2277
 
KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2278
 
{
2279
 
        struct kmod_module_version *version;
2280
 
 
2281
 
        if (entry == NULL)
2282
 
                return NULL;
2283
 
 
2284
 
        version = entry->data;
2285
 
        return version->symbol;
2286
 
}
2287
 
 
2288
 
/**
2289
 
 * kmod_module_version_get_crc:
2290
 
 * @entry: a list entry representing a kmod module version
2291
 
 *
2292
 
 * Get the crc of a kmod module version.
2293
 
 *
2294
 
 * Returns: the crc of this kmod module version on success or NULL on
2295
 
 * failure. The string is owned by the version, do not free it.
2296
 
 */
2297
 
KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2298
 
{
2299
 
        struct kmod_module_version *version;
2300
 
 
2301
 
        if (entry == NULL)
2302
 
                return 0;
2303
 
 
2304
 
        version = entry->data;
2305
 
        return version->crc;
2306
 
}
2307
 
 
2308
 
/**
2309
 
 * kmod_module_versions_free_list:
2310
 
 * @list: kmod module versions list
2311
 
 *
2312
 
 * Release the resources taken by @list
2313
 
 */
2314
 
KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2315
 
{
2316
 
        while (list) {
2317
 
                kmod_module_version_free(list->data);
2318
 
                list = kmod_list_remove(list);
2319
 
        }
2320
 
}
2321
 
 
2322
 
struct kmod_module_symbol {
2323
 
        uint64_t crc;
2324
 
        char symbol[];
2325
 
};
2326
 
 
2327
 
static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2328
 
{
2329
 
        struct kmod_module_symbol *mv;
2330
 
        size_t symbollen = strlen(symbol) + 1;
2331
 
 
2332
 
        mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2333
 
        if (mv == NULL)
2334
 
                return NULL;
2335
 
 
2336
 
        mv->crc = crc;
2337
 
        memcpy(mv->symbol, symbol, symbollen);
2338
 
        return mv;
2339
 
}
2340
 
 
2341
 
static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2342
 
{
2343
 
        free(symbol);
2344
 
}
2345
 
 
2346
 
/**
2347
 
 * kmod_module_get_symbols:
2348
 
 * @mod: kmod module
2349
 
 * @list: where to return list of module symbols. Use
2350
 
 *        kmod_module_symbol_get_symbol() and
2351
 
 *        kmod_module_symbol_get_crc(). Release this list with
2352
 
 *        kmod_module_symbols_free_list()
2353
 
 *
2354
 
 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2355
 
 *
2356
 
 * After use, free the @list by calling kmod_module_symbols_free_list().
2357
 
 *
2358
 
 * Returns: 0 on success or < 0 otherwise.
2359
 
 */
2360
 
KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2361
 
{
2362
 
        struct kmod_file *file;
2363
 
        struct kmod_elf *elf;
2364
 
        const char *path;
2365
 
        const void *mem;
2366
 
        struct kmod_modversion *symbols;
2367
 
        size_t size;
2368
 
        int i, count, ret = 0;
2369
 
 
2370
 
        if (mod == NULL || list == NULL)
2371
 
                return -ENOENT;
2372
 
 
2373
 
        assert(*list == NULL);
2374
 
 
2375
 
        path = kmod_module_get_path(mod);
2376
 
        if (path == NULL)
2377
 
                return -ENOENT;
2378
 
 
2379
 
        file = kmod_file_open(mod->ctx, path);
2380
 
        if (file == NULL)
2381
 
                return -errno;
2382
 
 
2383
 
        size = kmod_file_get_size(file);
2384
 
        mem = kmod_file_get_contents(file);
2385
 
 
2386
 
        elf = kmod_elf_new(mem, size);
2387
 
        if (elf == NULL) {
2388
 
                ret = -errno;
2389
 
                goto elf_open_error;
2390
 
        }
2391
 
 
2392
 
        count = kmod_elf_get_symbols(elf, &symbols);
2393
 
        if (count < 0) {
2394
 
                ret = count;
2395
 
                goto get_strings_error;
2396
 
        }
2397
 
 
2398
 
        for (i = 0; i < count; i++) {
2399
 
                struct kmod_module_symbol *mv;
2400
 
                struct kmod_list *n;
2401
 
 
2402
 
                mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2403
 
                if (mv == NULL) {
2404
 
                        ret = -errno;
2405
 
                        kmod_module_symbols_free_list(*list);
2406
 
                        *list = NULL;
2407
 
                        goto list_error;
2408
 
                }
2409
 
 
2410
 
                n = kmod_list_append(*list, mv);
2411
 
                if (n != NULL)
2412
 
                        *list = n;
2413
 
                else {
2414
 
                        kmod_module_symbol_free(mv);
2415
 
                        kmod_module_symbols_free_list(*list);
2416
 
                        *list = NULL;
2417
 
                        ret = -ENOMEM;
2418
 
                        goto list_error;
2419
 
                }
2420
 
        }
2421
 
        ret = count;
2422
 
 
2423
 
list_error:
2424
 
        free(symbols);
2425
 
get_strings_error:
2426
 
        kmod_elf_unref(elf);
2427
 
elf_open_error:
2428
 
        kmod_file_unref(file);
2429
 
 
2430
 
        return ret;
2431
 
}
2432
 
 
2433
 
/**
2434
 
 * kmod_module_symbol_get_symbol:
2435
 
 * @entry: a list entry representing a kmod module symbols
2436
 
 *
2437
 
 * Get the symbol of a kmod module symbols.
2438
 
 *
2439
 
 * Returns: the symbol of this kmod module symbols on success or NULL
2440
 
 * on failure. The string is owned by the symbols, do not free it.
2441
 
 */
2442
 
KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2443
 
{
2444
 
        struct kmod_module_symbol *symbol;
2445
 
 
2446
 
        if (entry == NULL)
2447
 
                return NULL;
2448
 
 
2449
 
        symbol = entry->data;
2450
 
        return symbol->symbol;
2451
 
}
2452
 
 
2453
 
/**
2454
 
 * kmod_module_symbol_get_crc:
2455
 
 * @entry: a list entry representing a kmod module symbol
2456
 
 *
2457
 
 * Get the crc of a kmod module symbol.
2458
 
 *
2459
 
 * Returns: the crc of this kmod module symbol on success or NULL on
2460
 
 * failure. The string is owned by the symbol, do not free it.
2461
 
 */
2462
 
KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2463
 
{
2464
 
        struct kmod_module_symbol *symbol;
2465
 
 
2466
 
        if (entry == NULL)
2467
 
                return 0;
2468
 
 
2469
 
        symbol = entry->data;
2470
 
        return symbol->crc;
2471
 
}
2472
 
 
2473
 
/**
2474
 
 * kmod_module_symbols_free_list:
2475
 
 * @list: kmod module symbols list
2476
 
 *
2477
 
 * Release the resources taken by @list
2478
 
 */
2479
 
KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2480
 
{
2481
 
        while (list) {
2482
 
                kmod_module_symbol_free(list->data);
2483
 
                list = kmod_list_remove(list);
2484
 
        }
2485
 
}
2486
 
 
2487
 
struct kmod_module_dependency_symbol {
2488
 
        uint64_t crc;
2489
 
        uint8_t bind;
2490
 
        char symbol[];
2491
 
};
2492
 
 
2493
 
static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2494
 
{
2495
 
        struct kmod_module_dependency_symbol *mv;
2496
 
        size_t symbollen = strlen(symbol) + 1;
2497
 
 
2498
 
        mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2499
 
        if (mv == NULL)
2500
 
                return NULL;
2501
 
 
2502
 
        mv->crc = crc;
2503
 
        mv->bind = bind;
2504
 
        memcpy(mv->symbol, symbol, symbollen);
2505
 
        return mv;
2506
 
}
2507
 
 
2508
 
static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2509
 
{
2510
 
        free(dependency_symbol);
2511
 
}
2512
 
 
2513
 
/**
2514
 
 * kmod_module_get_dependency_symbols:
2515
 
 * @mod: kmod module
2516
 
 * @list: where to return list of module dependency_symbols. Use
2517
 
 *        kmod_module_dependency_symbol_get_symbol() and
2518
 
 *        kmod_module_dependency_symbol_get_crc(). Release this list with
2519
 
 *        kmod_module_dependency_symbols_free_list()
2520
 
 *
2521
 
 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2522
 
 *
2523
 
 * After use, free the @list by calling
2524
 
 * kmod_module_dependency_symbols_free_list().
2525
 
 *
2526
 
 * Returns: 0 on success or < 0 otherwise.
2527
 
 */
2528
 
KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2529
 
{
2530
 
        struct kmod_file *file;
2531
 
        struct kmod_elf *elf;
2532
 
        const char *path;
2533
 
        const void *mem;
2534
 
        struct kmod_modversion *symbols;
2535
 
        size_t size;
2536
 
        int i, count, ret = 0;
2537
 
 
2538
 
        if (mod == NULL || list == NULL)
2539
 
                return -ENOENT;
2540
 
 
2541
 
        assert(*list == NULL);
2542
 
 
2543
 
        path = kmod_module_get_path(mod);
2544
 
        if (path == NULL)
2545
 
                return -ENOENT;
2546
 
 
2547
 
        file = kmod_file_open(mod->ctx, path);
2548
 
        if (file == NULL)
2549
 
                return -errno;
2550
 
 
2551
 
        size = kmod_file_get_size(file);
2552
 
        mem = kmod_file_get_contents(file);
2553
 
 
2554
 
        elf = kmod_elf_new(mem, size);
2555
 
        if (elf == NULL) {
2556
 
                ret = -errno;
2557
 
                goto elf_open_error;
2558
 
        }
2559
 
 
2560
 
        count = kmod_elf_get_dependency_symbols(elf, &symbols);
2561
 
        if (count < 0) {
2562
 
                ret = count;
2563
 
                goto get_strings_error;
2564
 
        }
2565
 
 
2566
 
        for (i = 0; i < count; i++) {
2567
 
                struct kmod_module_dependency_symbol *mv;
2568
 
                struct kmod_list *n;
2569
 
 
2570
 
                mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2571
 
                                                        symbols[i].bind,
2572
 
                                                        symbols[i].symbol);
2573
 
                if (mv == NULL) {
2574
 
                        ret = -errno;
2575
 
                        kmod_module_dependency_symbols_free_list(*list);
2576
 
                        *list = NULL;
2577
 
                        goto list_error;
2578
 
                }
2579
 
 
2580
 
                n = kmod_list_append(*list, mv);
2581
 
                if (n != NULL)
2582
 
                        *list = n;
2583
 
                else {
2584
 
                        kmod_module_dependency_symbol_free(mv);
2585
 
                        kmod_module_dependency_symbols_free_list(*list);
2586
 
                        *list = NULL;
2587
 
                        ret = -ENOMEM;
2588
 
                        goto list_error;
2589
 
                }
2590
 
        }
2591
 
        ret = count;
2592
 
 
2593
 
list_error:
2594
 
        free(symbols);
2595
 
get_strings_error:
2596
 
        kmod_elf_unref(elf);
2597
 
elf_open_error:
2598
 
        kmod_file_unref(file);
2599
 
 
2600
 
        return ret;
2601
 
}
2602
 
 
2603
 
/**
2604
 
 * kmod_module_dependency_symbol_get_symbol:
2605
 
 * @entry: a list entry representing a kmod module dependency_symbols
2606
 
 *
2607
 
 * Get the dependency symbol of a kmod module
2608
 
 *
2609
 
 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2610
 
 * on failure. The string is owned by the dependency_symbols, do not free it.
2611
 
 */
2612
 
KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2613
 
{
2614
 
        struct kmod_module_dependency_symbol *dependency_symbol;
2615
 
 
2616
 
        if (entry == NULL)
2617
 
                return NULL;
2618
 
 
2619
 
        dependency_symbol = entry->data;
2620
 
        return dependency_symbol->symbol;
2621
 
}
2622
 
 
2623
 
/**
2624
 
 * kmod_module_dependency_symbol_get_crc:
2625
 
 * @entry: a list entry representing a kmod module dependency_symbol
2626
 
 *
2627
 
 * Get the crc of a kmod module dependency_symbol.
2628
 
 *
2629
 
 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2630
 
 * failure. The string is owned by the dependency_symbol, do not free it.
2631
 
 */
2632
 
KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2633
 
{
2634
 
        struct kmod_module_dependency_symbol *dependency_symbol;
2635
 
 
2636
 
        if (entry == NULL)
2637
 
                return 0;
2638
 
 
2639
 
        dependency_symbol = entry->data;
2640
 
        return dependency_symbol->crc;
2641
 
}
2642
 
 
2643
 
/**
2644
 
 * kmod_module_dependency_symbol_get_bind:
2645
 
 * @entry: a list entry representing a kmod module dependency_symbol
2646
 
 *
2647
 
 * Get the bind type of a kmod module dependency_symbol.
2648
 
 *
2649
 
 * Returns: the bind of this kmod module dependency_symbol on success
2650
 
 * or < 0 on failure.
2651
 
 */
2652
 
KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2653
 
{
2654
 
        struct kmod_module_dependency_symbol *dependency_symbol;
2655
 
 
2656
 
        if (entry == NULL)
2657
 
                return 0;
2658
 
 
2659
 
        dependency_symbol = entry->data;
2660
 
        return dependency_symbol->bind;
2661
 
}
2662
 
 
2663
 
/**
2664
 
 * kmod_module_dependency_symbols_free_list:
2665
 
 * @list: kmod module dependency_symbols list
2666
 
 *
2667
 
 * Release the resources taken by @list
2668
 
 */
2669
 
KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2670
 
{
2671
 
        while (list) {
2672
 
                kmod_module_dependency_symbol_free(list->data);
2673
 
                list = kmod_list_remove(list);
2674
 
        }
2675
 
}