1 |
dmorgan |
1357 |
#define _GNU_SOURCE |
2 |
|
|
#include <sys/types.h> |
3 |
|
|
#include <sys/stat.h> |
4 |
|
|
#include <sys/wait.h> |
5 |
|
|
#include <sys/utsname.h> |
6 |
|
|
#include <fcntl.h> |
7 |
|
|
#include <unistd.h> |
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include <stdlib.h> |
10 |
|
|
#include <string.h> |
11 |
tv |
2638 |
#include <libkmod.h> |
12 |
dmorgan |
1357 |
#include <dirent.h> |
13 |
|
|
#include "common.h" |
14 |
|
|
|
15 |
|
|
static char *aliasdefault = NULL; |
16 |
tv |
2045 |
static char * version = NULL; |
17 |
dmorgan |
1357 |
|
18 |
tv |
2045 |
static void get_version(void) { |
19 |
|
|
if (version != NULL) |
20 |
|
|
return; |
21 |
|
|
struct utsname buf; |
22 |
|
|
uname(&buf); |
23 |
|
|
version = strdup(buf.release); |
24 |
|
|
} |
25 |
|
|
|
26 |
dmorgan |
1357 |
|
27 |
tv |
5820 |
char *dirname, *dkms_file; |
28 |
tv |
2045 |
|
29 |
dmorgan |
1357 |
static void set_default_alias_file(void) { |
30 |
|
|
struct utsname rel_buf; |
31 |
|
|
if (!aliasdefault) { |
32 |
|
|
char *dirname; |
33 |
|
|
char *fallback_aliases = table_name_to_file("fallback-modules.alias"); |
34 |
|
|
char *aliasfilename; |
35 |
|
|
struct stat st_alias, st_fallback; |
36 |
|
|
|
37 |
|
|
uname(&rel_buf); |
38 |
tv |
2638 |
asprintf(&dirname, "%s/%s", "/lib/modules", rel_buf.release); |
39 |
dmorgan |
1357 |
asprintf(&aliasfilename, "%s/modules.alias", dirname); |
40 |
|
|
free(dirname); |
41 |
|
|
|
42 |
|
|
/* fallback on ldetect-lst's modules.alias and prefer it if more recent */ |
43 |
|
|
if (stat(aliasfilename, &st_alias) || |
44 |
|
|
(!stat(fallback_aliases, &st_fallback) && st_fallback.st_mtime > st_alias.st_mtime)) { |
45 |
|
|
free(aliasfilename); |
46 |
|
|
aliasdefault = fallback_aliases; |
47 |
|
|
} else { |
48 |
|
|
aliasdefault = aliasfilename; |
49 |
tv |
2580 |
free(fallback_aliases); |
50 |
dmorgan |
1357 |
} |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
tv |
5820 |
struct kmod_ctx* modalias_init() { |
55 |
tv |
2638 |
struct kmod_ctx *ctx; |
56 |
dmorgan |
1357 |
|
57 |
|
|
if (!aliasdefault) |
58 |
|
|
set_default_alias_file(); |
59 |
|
|
|
60 |
tv |
2045 |
get_version(); |
61 |
tv |
2047 |
|
62 |
tv |
2638 |
/* We only use canned aliases as last resort. */ |
63 |
|
|
dkms_file = table_name_to_file("dkms-modules.alias"); |
64 |
|
|
const char *alias_filelist[] = { |
65 |
tv |
2686 |
"/run/modprobe.d", |
66 |
|
|
"/etc/modprobe.d", |
67 |
|
|
"/lib/modprobe.d", |
68 |
tv |
2638 |
"/lib/module-init-tools/ldetect-lst-modules.alias", |
69 |
|
|
aliasdefault, |
70 |
|
|
dkms_file, |
71 |
|
|
NULL, |
72 |
|
|
}; |
73 |
tv |
2047 |
|
74 |
tv |
2638 |
/* Init libkmod */ |
75 |
|
|
ctx = kmod_new(dirname, alias_filelist); |
76 |
|
|
if (!ctx) { |
77 |
|
|
fputs("Error: kmod_new() failed!\n", stderr); |
78 |
tv |
5820 |
free(dkms_file); |
79 |
|
|
kmod_unref(ctx); |
80 |
|
|
ctx = NULL; |
81 |
tv |
2638 |
} |
82 |
|
|
kmod_load_resources(ctx); |
83 |
tv |
5820 |
return ctx; |
84 |
|
|
} |
85 |
dmorgan |
1357 |
|
86 |
tv |
5820 |
char *modalias_resolve_module(struct kmod_ctx *ctx, const char *modalias) { |
87 |
|
|
struct kmod_list *l, *list = NULL; |
88 |
|
|
char *str = NULL; |
89 |
|
|
int err = kmod_module_new_from_lookup(ctx, modalias, &list); |
90 |
tv |
2638 |
if (err < 0) |
91 |
|
|
goto exit; |
92 |
tv |
2045 |
|
93 |
tv |
2638 |
// No module found... |
94 |
|
|
if (list == NULL) |
95 |
|
|
goto exit; |
96 |
tv |
2045 |
|
97 |
tv |
2638 |
// filter through blacklist |
98 |
|
|
struct kmod_list *filtered = NULL; |
99 |
|
|
err = kmod_module_get_filtered_blacklist(ctx, list, &filtered); |
100 |
|
|
kmod_module_unref_list(list); |
101 |
|
|
if (err <0) |
102 |
|
|
goto exit; |
103 |
|
|
list = filtered; |
104 |
dmorgan |
1357 |
|
105 |
tv |
2638 |
kmod_list_foreach(l, list) { |
106 |
|
|
struct kmod_module *mod = kmod_module_get_module(l); |
107 |
|
|
//if (str) // keep last one |
108 |
|
|
// free(str); |
109 |
|
|
if (!str) // keep first one |
110 |
|
|
str = strdup(kmod_module_get_name(mod)); |
111 |
|
|
kmod_module_unref(mod); |
112 |
|
|
if (err < 0) |
113 |
|
|
break; |
114 |
dmorgan |
1357 |
} |
115 |
|
|
|
116 |
tv |
2638 |
kmod_module_unref_list(list); |
117 |
|
|
|
118 |
|
|
exit: |
119 |
|
|
return str; |
120 |
dmorgan |
1357 |
} |
121 |
|
|
|
122 |
tv |
5820 |
void modalias_cleanup(struct kmod_ctx *ctx) { |
123 |
dmorgan |
1357 |
ifree(aliasdefault); |
124 |
tv |
2685 |
ifree(version); |
125 |
tv |
5820 |
free(dkms_file); |
126 |
|
|
kmod_unref(ctx); |
127 |
dmorgan |
1357 |
} |