plugins: refactor modules-load to read files in lexicographic order

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-05-07 16:29:18 +02:00
1 parent 7af3461e3c
commit eca3098c0e
1 file changed
+38 -23
+38 -23
View File
@@ -67,33 +67,19 @@
#define SERVICE_LINE_NOINDEX \ #define SERVICE_LINE_NOINDEX \
"cgroup.init name:modprobe.%s [2345] /sbin/modprobe %s %s -- Kernel module: %s" "cgroup.init name:modprobe.%s [2345] /sbin/modprobe %s %s -- Kernel module: %s"
static void load(void *arg) static int load_file(const char *file, int index)
{ {
struct dirent *d;
int counter = 1;
FILE *fp;
DIR *dir;
_d("Scanning " MODULES_LOAD_PATH " for config files ...");
dir = opendir(MODULES_LOAD_PATH);
if (!dir)
return;
while ((d = readdir(dir))) {
char module_path[PATH_MAX]; char module_path[PATH_MAX];
int index = counter;
char line[256]; char line[256];
int num = 0;
if (d->d_name[strlen(d->d_name) - 1] == '~') FILE *fp;
continue; /* skip backup files */
strlcpy(module_path, MODULES_LOAD_PATH "/", sizeof(module_path)); strlcpy(module_path, MODULES_LOAD_PATH "/", sizeof(module_path));
strlcat(module_path, d->d_name, sizeof(module_path)); strlcat(module_path, file, sizeof(module_path));
fp = fopen(module_path, "r"); fp = fopen(module_path, "r");
if (!fp) if (!fp)
continue; return 0;
while (fgets(line, sizeof(line), fp)) { while (fgets(line, sizeof(line), fp)) {
char cmd[CMD_SIZE], *mod, *args; char cmd[CMD_SIZE], *mod, *args;
@@ -128,14 +114,43 @@ static void load(void *arg)
snprintf(cmd, sizeof(cmd), SERVICE_LINE_NOINDEX, mod, mod, args, mod); snprintf(cmd, sizeof(cmd), SERVICE_LINE_NOINDEX, mod, mod, args, mod);
else else
snprintf(cmd, sizeof(cmd), SERVICE_LINE, mod, index++, mod, args, mod); snprintf(cmd, sizeof(cmd), SERVICE_LINE, mod, index++, mod, args, mod);
service_register(SVC_TYPE_TASK, cmd, global_rlimit, NULL); service_register(SVC_TYPE_TASK, cmd, global_rlimit, NULL);
counter++; num++;
} }
fclose(fp); fclose(fp);
}
closedir(dir); 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;
_d("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 += load_file(dentry[i]->d_name, index);
free(dentry[i]);
}
free(dentry);
}
} }
static plugin_t plugin = { static plugin_t plugin = {