summaryrefslogtreecommitdiffstats
path: root/modalias.c
blob: 2d171358b25ed97c3de16e5280e730fcf37264a8 (plain)
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
#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 <list.h>
#include <logging.h>
#include <modprobe.h>
#include <dirent.h>
#include "common.h"

static char *aliasdefault = NULL;
static char * version = NULL;

static void get_version(void) {
	if (version != NULL)
		return;
	struct utsname buf;
	uname(&buf);
	version = strdup(buf.release);
}


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);
}

char*dirname;

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_alias *aliases = NULL;
	struct module_blacklist *blacklist = NULL;
       struct modprobe_conf conf = {};
	LIST_HEAD(list);

	if (!aliasdefault)
		set_default_alias_file();

	get_version();

	/* Makes module-init-tools quiet */
	quiet = 1;

	/* Returns the resolved alias, options */
	parse_toplevel_config(NULL, &conf, 0, 0);

	errfn_t error;
        aliases = find_matching_aliases(modalias, "", &conf, dirname, error, mit_dry_run, &list);

	/* only load blacklisted modules with specific request (no alias) */
	apply_blacklist(&aliases, conf.blacklist);

	if (!aliases) {
		/* We only use canned aliases as last resort. */
		char *dkms_file = table_name_to_file("dkms-modules.alias");
		const char *alias_filelist[] = {
			"/lib/module-init-tools/ldetect-lst-modules.alias",
			aliasdefault,
			dkms_file,
			NULL,
		};
		const char **alias_file = alias_filelist;
		while (!aliases && *alias_file) {
			parse_config_file(*alias_file, &conf, 0, 0);
			aliases = find_matching_aliases(modalias, "", &conf, dirname, error, mit_dry_run, &list);
			apply_blacklist(&aliases, blacklist);
			alias_file++;
		}
		free(dkms_file);
	}
	free_blacklist(blacklist);
	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);
}