/[soft]/ldetect/trunk/modalias.c
ViewVC logotype

Contents of /ldetect/trunk/modalias.c

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.28