mirror of
https://github.com/troglobit/finit.git
synced 2026-11-29 21:08:21 +00:00
Adapt ifupdown support to new world, run 'ifup -a' in the background
All Debian based distros are moving towards a /etc/network/interfaces.d/ world. Adding support for that is too complex, so we're reverting back to calling 'ifup -a' or 'ifdown -a'. We still guard this by checking both for 'ifup' and the existence of /etc/network/interfaces (which in the new world only contains a line to source per-interface files). By popular demand, this patch also changes to bring up/down networking in the background. Useful for embedded system bringup and containers. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
fccba7cd4c
commit
5c2ba8ef3a
1 file changed
+10
-66
+10
-66
@@ -377,54 +377,11 @@ done:
|
|||||||
sethostname(*hostname, strlen(*hostname));
|
sethostname(*hostname, strlen(*hostname));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Some systems, e.g. lxc, come with loopback up by default. We can try
|
|
||||||
* all we want to take it down first or force reconfig, it will fail.
|
|
||||||
* This was added to skip lo on such systems to keep the noise down.
|
|
||||||
*/
|
|
||||||
static int skip_loopback(char *ifname)
|
|
||||||
{
|
|
||||||
struct ifreq ifr;
|
|
||||||
int sd, rc = 0;
|
|
||||||
|
|
||||||
if (strcmp(ifname, "lo"))
|
|
||||||
return 0; /* Not loopback */
|
|
||||||
|
|
||||||
if ((sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0)
|
|
||||||
return 0; /* We dunno, don't skip */
|
|
||||||
|
|
||||||
memset(&ifr, 0, sizeof (ifr));
|
|
||||||
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
|
|
||||||
ifr.ifr_addr.sa_family = AF_INET;
|
|
||||||
if (!ioctl(sd, SIOCGIFFLAGS, &ifr)) {
|
|
||||||
if (ifr.ifr_flags & IFF_UP)
|
|
||||||
rc = 1; /* Already up, skip it */
|
|
||||||
}
|
|
||||||
close(sd);
|
|
||||||
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ifup(char *ifname, int updown)
|
|
||||||
{
|
|
||||||
char cmd[80];
|
|
||||||
|
|
||||||
if (updown) {
|
|
||||||
snprintf(cmd, sizeof(cmd), "ifup %s", ifname);
|
|
||||||
run_interactive(cmd, "Bringing up interface %s", ifname);
|
|
||||||
} else {
|
|
||||||
snprintf(cmd, sizeof(cmd), "ifdown -f %s", ifname);
|
|
||||||
run_interactive(cmd, "Taking down interface %s", ifname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Bring up networking, but only if not single-user or rescue mode
|
* Bring up networking, but only if not single-user or rescue mode
|
||||||
*/
|
*/
|
||||||
void networking(int updown)
|
void networking(int updown)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
/* No need to report errors if network is already down */
|
/* No need to report errors if network is already down */
|
||||||
if (!prevlevel && !updown)
|
if (!prevlevel && !updown)
|
||||||
return;
|
return;
|
||||||
@@ -444,32 +401,19 @@ void networking(int updown)
|
|||||||
if (!whichp("ifup"))
|
if (!whichp("ifup"))
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
fp = fopen("/etc/network/interfaces", "r");
|
if (fexist("/etc/network/interfaces")) {
|
||||||
if (fp) {
|
pid_t pid;
|
||||||
char buf[160];
|
|
||||||
|
|
||||||
/* Bring up, or down, all 'auto' interfaces */
|
pid = fork();
|
||||||
while (fgets(buf, sizeof(buf), fp)) {
|
if (pid == 0) {
|
||||||
char *line, *ifname = NULL;
|
if (updown)
|
||||||
|
run("ifup -a");
|
||||||
chomp(buf);
|
else
|
||||||
line = strip_line(buf);
|
run("ifdown -a");
|
||||||
|
_exit(0);
|
||||||
if (!strncmp(line, "auto", 4))
|
|
||||||
ifname = &line[5];
|
|
||||||
if (!strncmp(line, "allow-hotplug", 13))
|
|
||||||
ifname = &line[14];
|
|
||||||
|
|
||||||
if (!ifname)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (skip_loopback(ifname))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
ifup(ifname, updown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
print(pid > 0 ? 0 : 1, "%s networking", updown ? "Starting" : "Stopping");
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
|
|||||||
Reference in new issue
Block a user