File: modules-load.c

package info (click to toggle)
finit 4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,216 kB
  • sloc: ansic: 17,060; sh: 6,281; makefile: 532
file content (235 lines) | stat: -rw-r--r-- 5,997 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Scans /etc/modules-load.d for modules to load.
 *
 * Copyright (c) 2018  Robert Andersson <robert.m.andersson@se.atlascopco.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * This plugin scans files in /etc/modules-load.d/, for each line in the
 * file it assumes the name of a kernel module, with optional arguments.
 * Each module is loaded when entering runlevel 2, 3, 4, or 5, using the
 * modprobe tool.
 *
 * Loading is done by inserting a `task` stanza in Finit, ensuring all
 * modules are loaded in parallel, with other modules and programs.
 * Each `task` stanza is by default given the name:modprobe.module and
 * indexed with `:ID`, starting with 1.
 *
 * Indexing can be disabled per file in /etc/modules-load.d/, anywwhere
 * in a file, by putting the keyword `noindex` on a line of its own.  A
 * noindex command can appear anywhere in the file, and up to the point
 * it is read, tasks will be indexed with an incrementing `:ID`.  When
 * `noindex` is read indexing is disabled and all subseequent tasks will
 * not have any `:ID` at all.
 *
 * Please note, the :ID is there for your benefit, it ensures that tasks
 * in Finit are unique.  If you have two tasks with the same name and ID
 * (or no ID), the last one read replaces any preceding one!
 */

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#ifdef _LIBITE_LITE
# include <libite/lite.h>
#else
# include <lite/lite.h>
#endif

#include "finit.h"
#include "helpers.h"
#include "plugin.h"
#include "service.h"
#include "conf.h"
#include "log.h"

#ifndef MODULES_LOAD_PATH
#define MODULES_LOAD_PATH "/etc/modules-load.d"
#endif
#define MODPROBE_PATH     "/sbin/modprobe"
#define SERVICE_LINE \
	"cgroup.init name:modprobe.%s :%d [%s] %s %s %s --"
#define SERVICE_LINE_NOINDEX \
	"cgroup.init name:modprobe.%s [%s] %s %s %s --"

static int modules_load(const char *file, int index)
{
	char module_path[PATH_MAX];
	char *modprobe_path;
	int num = 0;
	char *line;
	char *lvl;
	FILE *fp;

	strlcpy(module_path, MODULES_LOAD_PATH "/", sizeof(module_path));
	strlcat(module_path, file, sizeof(module_path));

	fp = fopen(module_path, "r");
	if (!fp) {
		warnx("failed opening %s for reading, skipping ...", module_path);
		return 0;
	}

	modprobe_path = strdup(MODPROBE_PATH);
	if (!modprobe_path) {
	fail:
		warnx("failed allocating memory in modules-load plugin.");
		fclose(fp);
		return -1;
	}

	lvl = strdup("S");
	if (!lvl) {
		free(modprobe_path);
		goto fail;
	}

	while ((line = fparseln(fp, NULL, NULL, NULL, 0))) {
		char cmd[CMD_SIZE], *mod, *args, *set;

		/*
		 * fparseln() skips regular UNIX comments only.
		 * This is for modules-load.d(5) compat.
		 */
		if (line[0] == ';')
			goto next;

		/* trim whitespace */
		mod = strip_line(line);
		if (!mod[0])
			goto next;

		/* Finit extension 'set foo bar' */
		if ((set = fgetval(mod, "set", "= \t"))) {
			char *val = mod;

			if (!strcmp(set, "noindex")) {
				index = 0;
				free(set);
				goto next;
			}

			if ((val = fgetval(set, "index", "= \t"))) {
				index = atoi(val);
				free(set);
				free(val);
				goto next;
			}

			if ((val = fgetval(set, "runlevel", "= \t"))) {
				free(lvl);
				lvl = val;
				free(set);
				goto next;
			}

			if ((val = fgetval(set, "modprobe", "= \t"))) {
				if (access(val, X_OK)) {
					warn("%s: cannot use %s", module_path, val);
					free(set);
					free(val);
					free(line);
					goto skip;
				} else {
					free(modprobe_path);
					modprobe_path = val;
				}
				free(set);
				goto next;
			}

			/* unknown 'set' command, ignore. */
			goto next;
		}

		mod = strtok_r(mod, " ", &args);
		if (!mod)
			goto next;

		if (!index)
			snprintf(cmd, sizeof(cmd), SERVICE_LINE_NOINDEX, mod, lvl, modprobe_path, mod, args);
		else
			snprintf(cmd, sizeof(cmd), SERVICE_LINE, mod, index++, lvl, modprobe_path, mod, args);

		dbg("task %s", cmd);
		service_register(SVC_TYPE_TASK, cmd, global_rlimit, NULL);
		num++;
	next:
		free(line);
	}
skip:
	free(modprobe_path);
	free(lvl);
	fclose(fp);

	return num;
}

/* XXX: check here for .conf only? */
static int module_filter(const struct dirent *d)
{
	if (d->d_type == DT_DIR)
		return 0;
	if (d->d_name[0] == '.')
		return 0;	/* skip dot files */
	if (d->d_name[strlen(d->d_name) - 1] == '~')
		return 0;	/* skip backup files */

	return 1;
}

static void load(void *arg)
{
	struct dirent **dentry;
	int index = 1;
	int i, num;

	dbg("Scanning " MODULES_LOAD_PATH " for config files ...");
	num = scandir(MODULES_LOAD_PATH, &dentry, module_filter, alphasort);
	if (num > 0) {
		for (i = 0; i < num; i++) {
			index += modules_load(dentry[i]->d_name, index);
			free(dentry[i]);
		}
		free(dentry);
	}
}

static plugin_t plugin = {
	.name                  = __FILE__,
	.hook[HOOK_SVC_PLUGIN] = { .cb  = load },
};

PLUGIN_INIT(__init)
{
	plugin_register(&plugin);
}

PLUGIN_EXIT(__exit)
{
	plugin_unregister(&plugin);
}

/**
 * Local Variables:
 *  indent-tabs-mode: t
 *  c-file-style: "linux"
 * End:
 */