From 6380f17d1f4d4279be92d80ea24aee00490f32bc Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 21 Jul 2016 16:11:50 +0200 Subject: [PATCH] Prototype support for /etc/netork/interfaces This patch adds prototype support for the Debian/Ubuntu/Busybox network configuration file /etc/netork/interfaces. If this file exists Finit will call `ifup` for each (non-commented-out) interface marked `auto`. With this implementation we block boot until all ifup commands have returned, which is less than ideal. A better approach would be to ifup only loopback and create deferred tasklets to be run in parallel with the rest of the boot. Signed-off-by: Joachim Nilsson --- conf.c | 18 ------------------ finit.c | 41 +++++++++++++++++++++++++++++++++-------- helpers.c | 18 ++++++++++++++++++ helpers.h | 2 ++ 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/conf.c b/conf.c index 3afb3e48..6e13666a 100644 --- a/conf.c +++ b/conf.c @@ -23,7 +23,6 @@ #include "config.h" /* Generated by configure script */ -#include #include #include #include @@ -41,23 +40,6 @@ static int parse_conf(char *file); -static char *strip_line(char *line) -{ - char *ptr; - - /* Trim leading whitespace */ - while (*line && isblank(*line)) - line++; - - /* Strip any comment at end of line */ - ptr = line; - while (*ptr && *ptr != '#') - ptr++; - *ptr = 0; - - return line; -} - void conf_parse_cmdline(void) { FILE *fp; diff --git a/finit.c b/finit.c index 798f5992..9c03b5e3 100644 --- a/finit.c +++ b/finit.c @@ -75,6 +75,8 @@ static void networking(void) { size_t i; glob_t gl; + FILE *fp; + char buf[160]; /* Setup kernel specific settings, e.g. allow broadcast ping, etc. */ glob("/run/sysctl.d/*.conf", 0, NULL, &gl); @@ -86,19 +88,42 @@ static void networking(void) glob("/etc/sysctl.conf", GLOB_APPEND, NULL, &gl); if (gl.gl_pathc > 0) { for (i = 0; i < gl.gl_pathc; i++) { - char cmd[160]; - - snprintf(cmd, sizeof(cmd), "/sbin/sysctl -e -p %s >/dev/null", gl.gl_pathv[i]); - run(cmd); + snprintf(buf, sizeof(buf), "/sbin/sysctl -e -p %s >/dev/null", gl.gl_pathv[i]); + run(buf); } globfree(&gl); } - /* Run user network start script, or fall back to at least loopback */ - if (network) + /* Run user network start script if enabled */ + if (network) { run_interactive(network, "Starting networking: %s", network); - else - ifconfig("lo", "127.0.0.1", "255.0.0.0", 1); + return; + } + + /* Debian/Ubuntu/Busybox interfaces file */ + fp = fopen("/etc/network/interfaces", "r"); + if (fp) { + i = 0; + + /* Bring up all 'auto' interfaces */ + while (fgets(buf, sizeof(buf), fp)) { + chomp(buf); + strip_line(buf); + + if (!strncmp(buf, "auto", 4)) { + memcpy(buf, "ifup", 4); + run_interactive(buf, "Bringing up interface %s", buf[5]); + i++; + } + } + + fclose(fp); + if (i) + return; + } + + /* Fall back to bring up at least loopback */ + ifconfig("lo", "127.0.0.1", "255.0.0.0", 1); } /* Requires /proc to be mounted */ diff --git a/helpers.c b/helpers.c index d2c04a6a..7ba2c837 100644 --- a/helpers.c +++ b/helpers.c @@ -22,6 +22,7 @@ * THE SOFTWARE. */ +#include /* isblank() */ #include #include #include @@ -39,6 +40,23 @@ #include "helpers.h" #include "private.h" +char *strip_line(char *line) +{ + char *ptr; + + /* Trim leading whitespace */ + while (*line && isblank(*line)) + line++; + + /* Strip any comment at end of line */ + ptr = line; + while (*ptr && *ptr != '#') + ptr++; + *ptr = 0; + + return line; +} + static int encode(int lvl) { if (!lvl) return 0; diff --git a/helpers.h b/helpers.h index f864d19e..dc532946 100644 --- a/helpers.h +++ b/helpers.h @@ -61,6 +61,8 @@ do { \ #define _e(fmt, args...) do { fprintf(stderr, "finit:%s:%s() - " fmt "\n", __FILE__, __func__, ##args); } while (0) #define _pe(fmt, args...) do { fprintf(stderr, "finit:%s:%s() - " fmt ". Error %d: %s\n", __FILE__, __func__, ##args, errno, strerror(errno)); } while (0) +char *strip_line (char *line); + void runlevel_set (int pre, int now); char *runlevel_string (int levels);