1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <modprobe.h>
#include <dirent.h>
#include "common.h"
static char *aliasdefault = NULL;
static void free_aliases(struct module_alias *aliases) {
if (aliases == NULL)
return;
while (aliases->next) {
struct module_alias *next;
next = aliases->next;
free(aliases->module);
free(aliases);
aliases = next;
}
free(aliases);
}
static void free_options(struct module_options *modoptions) {
if (modoptions == NULL)
return;
while (modoptions->next) {
struct module_options *next;
next = modoptions->next;
free(modoptions->modulename);
free(modoptions->options);
free(modoptions);
modoptions = next;
}
free(modoptions);
}
static void free_commands(struct module_command *commands) {
if (commands == NULL)
return;
while (commands->next) {
struct module_command *next;
next = commands->next;
free(commands->modulename);
free(commands->command);
free(commands);
commands = next;
}
free(commands);
}
static void free_blacklist(struct module_blacklist *blacklist) {
if (blacklist == NULL)
return;
while (blacklist->next) {
struct module_blacklist *next;
next = blacklist->next;
free(blacklist->modulename);
free(blacklist);
blacklist = next;
}
free(blacklist);
}
static void set_default_alias_file(void) {
struct utsname rel_buf;
if (!aliasdefault) {
char *dirname;
char *fallback_aliases = table_name_to_file("fallback-modules.alias");
char *aliasfilename;
struct stat st_alias, st_fallback;
uname(&rel_buf);
asprintf(&dirname, "%s/%s", MODULE_DIR, rel_buf.release);
asprintf(&aliasfilename, "%s/modules.alias", dirname);
free(dirname);
/* fallback on ldetect-lst's modules.alias and prefer it if more recent */
if (stat(aliasfilename, &st_alias) ||
(!stat(fallback_aliases, &st_fallback) && st_fallback.st_mtime > st_alias.st_mtime)) {
free(aliasfilename);
aliasdefault = fallback_aliases;
} else {
aliasdefault = aliasfilename;
}
}
}
char *modalias_resolve_module(const char *modalias) {
struct module_command *commands = NULL;
struct module_options *modoptions = NULL;
struct module_alias *aliases = NULL;
struct module_blacklist *blacklist = NULL;
const char *config = NULL;
if (!aliasdefault)
set_default_alias_file();
/* Returns the resolved alias, options */
read_toplevel_config(config, modalias, 0,
0, &modoptions, &commands, &aliases, &blacklist);
if (!aliases) {
/* We only use canned aliases as last resort. */
char *dkms_file = table_name_to_file("dkms-modules.alias");
char *alias_filelist[] = {
"/lib/module-init-tools/ldetect-lst-modules.alias",
aliasdefault,
dkms_file,
NULL,
};
char **alias_file = alias_filelist;
while (!aliases && *alias_file) {
read_config(*alias_file, modalias, 0,
0, &modoptions, &commands,
&aliases, &blacklist);
aliases = apply_blacklist(aliases, blacklist);
alias_file++;
}
free(dkms_file);
}
free_blacklist(blacklist);
free_commands(commands);
free_options(modoptions);
if (aliases) {
char *result;
// take the last one because we find eg: generic/ata_generic/sata_sil
struct module_alias *it = aliases;
while (it->next)
it = it->next;
result = strdup(it->module);
free_aliases(aliases);
return result;
}
return NULL;
}
void modalias_cleanup(void) {
ifree(aliasdefault);
}
|