summaryrefslogtreecommitdiffstats
path: root/modalias.c
blob: 029617e1e62f71853c738558f0352e9e1d736590 (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 <libkmod.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);
}


char *dirname, *dkms_file;

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", "/lib/modules", 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;
			free(fallback_aliases);
		}
	}
}

struct kmod_ctx* modalias_init() {
        struct kmod_ctx *ctx;

	if (!aliasdefault)
		set_default_alias_file();

	get_version();

	/* We only use canned aliases as last resort. */
	dkms_file = table_name_to_file("dkms-modules.alias");
	const char *alias_filelist[] = {
		"/run/modprobe.d",
		"/etc/modprobe.d",
		"/lib/modprobe.d",
		"/lib/module-init-tools/ldetect-lst-modules.alias",
		aliasdefault,
		dkms_file,
		NULL,
	};

	/* Init libkmod */
	ctx = kmod_new(dirname, alias_filelist);
	if (!ctx) {
		fputs("Error: kmod_new() failed!\n", stderr);
		free(dkms_file);
		kmod_unref(ctx);
		ctx = NULL;
	}
	kmod_load_resources(ctx);
	return ctx;
}

char *modalias_resolve_module(struct kmod_ctx *ctx, const char *modalias) {
	struct kmod_list *l, *list = NULL;
	char *str = NULL;
	int err = kmod_module_new_from_lookup(ctx, modalias, &list);
	if (err < 0)
		goto exit;

	// No module found...
	if (list == NULL)
		goto exit;

	// filter through blacklist
	struct kmod_list *filtered = NULL;
	err = kmod_module_get_filtered_blacklist(ctx, list, &filtered);
	kmod_module_unref_list(list);
	if (err <0)
		goto exit;
	list = filtered;

	kmod_list_foreach(l, list) {
		struct kmod_module *mod = kmod_module_get_module(l);
		//if (str) // keep last one
		//	free(str);
		if (!str) // keep first one
		str = strdup(kmod_module_get_name(mod));
		kmod_module_unref(mod);
		if (err < 0)
			break;
	}

	kmod_module_unref_list(list);

exit:
	return str;
}

void modalias_cleanup(struct kmod_ctx *ctx) {
    ifree(aliasdefault);
    ifree(version);
    free(dkms_file);
    kmod_unref(ctx);
}