plugins: refactor modules-load to read files in lexicographic order
#define SERVICE_LINE_NOINDEX \
"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];
- int index = counter;
char line[256];
-
- if (d->d_name[strlen(d->d_name) - 1] == '~')
- continue; /* skip backup files */
+ int num = 0;
+ FILE *fp;
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");
if (!fp)
- continue;
+ return 0;
while (fgets(line, sizeof(line), fp)) {
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);
else
snprintf(cmd, sizeof(cmd), SERVICE_LINE, mod, index++, mod, args, mod);
+
service_register(SVC_TYPE_TASK, cmd, global_rlimit, NULL);
- counter++;
+ num++;
}
-
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;
}
- closedir(dir);
+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 = {
