summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2021-01-20 15:54:44 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-01-21 10:14:06 +0100
commit90cde3b9ff46b8c62d3b5bdb88f5acea4e62bcac (patch)
treec5affad785deb060445f0d5145b23e140f7f369a /common
parentb5d3c4c00dff9211fb271e644ae5e98aa0a0853e (diff)
downloadbarebox-90cde3b9ff46b8c62d3b5bdb88f5acea4e62bcac.tar.gz
barebox-90cde3b9ff46b8c62d3b5bdb88f5acea4e62bcac.tar.xz
startup: Execute init scripts in alphabetical order
Documentation states that init scripts are executed in order and this had been the case before 90df2a955e. This patch replaced the shell loop around /env/init/* with a plain readdir which is not sorted at all. Iterate over the files with glob() which guarantees sorted results. Reported-by: Marcel Hellwig <git@cookiesoft.de> Fixes: 90df2a955e ("defaultenv: Convert init script to C") Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common')
-rw-r--r--common/startup.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/common/startup.c b/common/startup.c
index 1ac36d950c..080feebf05 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -36,6 +36,7 @@
#include <environment.h>
#include <linux/ctype.h>
#include <watchdog.h>
+#include <glob.h>
extern initcall_t __barebox_initcalls_start[], __barebox_early_initcalls_end[],
__barebox_initcalls_end[];
@@ -298,13 +299,12 @@ postcore_initcall(register_autoboot_vars);
static int run_init(void)
{
- DIR *dir;
- struct dirent *d;
- const char *initdir = "/env/init";
const char *bmode;
bool env_bin_init_exists;
enum autoboot_state autoboot;
struct stat s;
+ glob_t g;
+ int i, ret;
setenv("PATH", "/env/bin");
export("PATH");
@@ -326,23 +326,28 @@ static int run_init(void)
}
/* Run scripts in /env/init/ */
- dir = opendir(initdir);
- if (dir) {
- char *scr;
+ ret = glob("/env/init/*", 0, NULL, &g);
+ if (!ret) {
+ for (i = 0; i < g.gl_pathc; i++) {
+ const char *path = g.gl_pathv[i];
+ char *scr;
+
+ ret = stat(path, &s);
+ if (ret)
+ continue;
- while ((d = readdir(dir))) {
- if (*d->d_name == '.')
+ if (!S_ISREG(s.st_mode))
continue;
- pr_debug("Executing '%s/%s'...\n", initdir, d->d_name);
- scr = basprintf("source %s/%s", initdir, d->d_name);
+ pr_debug("Executing '%s'...\n", path);
+ scr = basprintf("source %s", path);
run_command(scr);
free(scr);
}
-
- closedir(dir);
}
+ globfree(&g);
+
/* source matching script in /env/bmode/ */
bmode = reboot_mode_get();
if (bmode) {