diff --git a/Makefile b/Makefile index 134595d..e6dc073 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PACKAGE_BUGREPORT := https://github.com/kaniini/ifupdown-ng/issues/new INTERFACES_FILE := /etc/network/interfaces STATE_FILE := /run/ifstate -CFLAGS = -ggdb3 -O2 -Wall -Wextra +CFLAGS = -ggdb3 -Os -Wall -Wextra CPPFLAGS = -I. -DINTERFACES_FILE=\"${INTERFACES_FILE}\" -DSTATE_FILE=\"${STATE_FILE}\" -DPACKAGE_NAME=\"${PACKAGE_NAME}\" -DPACKAGE_VERSION=\"${PACKAGE_VERSION}\" -DPACKAGE_BUGREPORT=\"${PACKAGE_BUGREPORT}\" @@ -24,37 +24,42 @@ LIBIFUPDOWN_SRC = \ LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_LIB = libifupdown.a - -CMDS = ifquery ifup ifdown - -LIBS = ${LIBIFUPDOWN_LIB} - -all: libifupdown.a ${CMDS} +MULTICALL_SRC = cmd/multicall.c +MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o} +MULTICALL = ifupdown IFQUERY_SRC = cmd/ifquery.c IFQUERY_OBJ = ${IFQUERY_SRC:.c=.o} -ifquery: ${LIBS} ${IFQUERY_OBJ} - ${CC} -o $@ ${IFQUERY_OBJ} ${LIBS} IFUPDOWN_SRC = cmd/ifupdown.c IFUPDOWN_OBJ = ${IFUPDOWN_SRC:.c=.o} -ifup: ${LIBS} ${IFUPDOWN_OBJ} - ${CC} -o $@ ${IFUPDOWN_OBJ} ${LIBS} -ifdown: ifup - ln -s ifup $@ +CMD_OBJ = ${MULTICALL_OBJ} ${IFQUERY_OBJ} ${IFUPDOWN_OBJ} + +CMDS = ifup ifdown ifquery + +LIBS = ${LIBIFUPDOWN_LIB} + +all: libifupdown.a ${MULTICALL} ${CMDS} + +${CMDS}: ${MULTICALL} + ln -s ifupdown $@ + +${MULTICALL}: ${LIBS} ${CMD_OBJ} + ${CC} -o $@ ${CMD_OBJ} ${LIBS} libifupdown.a: ${LIBIFUPDOWN_OBJ} ${AR} -rcs $@ ${LIBIFUPDOWN_OBJ} clean: - rm -f ${LIBIFUPDOWN_OBJ} ${IFQUERY_OBJ} ${IFUPDOWN_OBJ} - rm -f ${CMDS} + rm -f ${LIBIFUPDOWN_OBJ} ${CMD_OBJ} + rm -f ${CMDS} ${MULTICALL} check: libifupdown.a ${CMDS} kyua test install: all - install -D -m755 ./ifquery ${DESTDIR}/sbin/ifquery - install -D -m755 ./ifup ${DESTDIR}/sbin/ifup - ln -s /sbin/ifup ${DESTDIR}/sbin/ifdown + install -D -m755 ${MULTICALL} ${DESTDIR}/sbin/${MULTICALL} + for i in ${CMDS}; do \ + ln -s /sbin/${MULTICALL} ${DESTDIR}/sbin/$$i; \ + done diff --git a/cmd/ifquery.c b/cmd/ifquery.c index 6ec858f..a6ad06e 100644 --- a/cmd/ifquery.c +++ b/cmd/ifquery.c @@ -19,6 +19,7 @@ #include #include #include "libifupdown/libifupdown.h" +#include "cmd/multicall.h" void print_interface(struct lif_interface *iface) @@ -61,7 +62,7 @@ print_interface(struct lif_interface *iface) } void -usage() +ifquery_usage(void) { fprintf(stderr, "usage: ifquery [options] \n"); fprintf(stderr, " ifquery [options] --list\n"); @@ -138,7 +139,7 @@ list_state(struct lif_dict *state, struct match_options *opts) } int -main(int argc, char *argv[]) +ifquery_main(int argc, char *argv[]) { struct lif_dict state = {}; struct lif_dict collection = {}; @@ -168,7 +169,7 @@ main(int argc, char *argv[]) switch (c) { case 'h': - usage(); + ifquery_usage(); break; case 'V': lif_common_version(); @@ -214,7 +215,7 @@ main(int argc, char *argv[]) /* --list --state is not allowed */ if (listing && listing_stat) - usage(); + ifquery_usage(); if (listing) { @@ -228,7 +229,7 @@ main(int argc, char *argv[]) } if (optind >= argc) - usage(); + ifquery_usage(); int idx = optind; for (; idx < argc; idx++) @@ -254,3 +255,9 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } + +struct if_applet ifquery_applet = { + .name = "ifquery", + .main = ifquery_main, + .usage = ifquery_usage +}; diff --git a/cmd/ifupdown.c b/cmd/ifupdown.c index 0b3c8ad..24bd70b 100644 --- a/cmd/ifupdown.c +++ b/cmd/ifupdown.c @@ -19,6 +19,7 @@ #include #include #include "libifupdown/libifupdown.h" +#include "cmd/multicall.h" struct match_options { bool is_auto; @@ -31,7 +32,7 @@ static bool up; static struct lif_execute_opts exec_opts = {}; void -usage() +ifupdown_usage(void) { fprintf(stderr, "usage: %s [options] \n", argv0); @@ -106,7 +107,7 @@ change_auto_interfaces(struct lif_dict *collection, struct lif_dict *state, stru } int -main(int argc, char *argv[]) +ifupdown_main(int argc, char *argv[]) { argv0 = basename(argv[0]); up = !is_ifdown(); @@ -137,7 +138,7 @@ main(int argc, char *argv[]) switch (c) { case 'h': - usage(); + ifupdown_usage(); break; case 'V': lif_common_version(); @@ -187,7 +188,7 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } else if (optind >= argc) - usage(); + ifupdown_usage(); int idx = optind; for (; idx < argc; idx++) @@ -231,3 +232,15 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } + +struct if_applet ifup_applet = { + .name = "ifup", + .main = ifupdown_main, + .usage = ifupdown_usage +}; + +struct if_applet ifdown_applet = { + .name = "ifdown", + .main = ifupdown_main, + .usage = ifupdown_usage +}; diff --git a/cmd/multicall.c b/cmd/multicall.c new file mode 100644 index 0000000..94102e0 --- /dev/null +++ b/cmd/multicall.c @@ -0,0 +1,60 @@ +/* + * cmd/multicall.c + * Purpose: multi-call binary frontend + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include +#include +#include +#include +#include "cmd/multicall.h" + +extern struct if_applet ifquery_applet; +extern struct if_applet ifup_applet; +extern struct if_applet ifdown_applet; + +struct if_applet *applet_table[] = { + &ifdown_applet, + &ifquery_applet, + &ifup_applet, +}; + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x)) + +int +applet_cmp(const void *a, const void *b) +{ + const char *key = a; + const struct if_applet *applet = *(void **)b; + + return strcmp(key, applet->name); +} + +int +main(int argc, char *argv[]) +{ + char *argv0 = basename(argv[0]); + struct if_applet **app; + + app = bsearch(argv0, applet_table, + ARRAY_SIZE(applet_table), sizeof(*applet_table), + applet_cmp); + + if (app == NULL) + { + fprintf(stderr, "%s: applet not found\n", argv0); + return EXIT_FAILURE; + } + + return (*app)->main(argc, argv); +} diff --git a/cmd/multicall.h b/cmd/multicall.h new file mode 100644 index 0000000..ea720d2 --- /dev/null +++ b/cmd/multicall.h @@ -0,0 +1,27 @@ +/* + * cmd/multicall.h + * Purpose: structures for multicall frontend + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef IFUPDOWN_CMD_MULTICALL_H__GUARD +#define IFUPDOWN_CMD_MULTICALL_H__GUARD + +#include "libifupdown/libifupdown.h" + +struct if_applet { + const char *name; + int (*const main)(int argc, char *argv[]); + void (*const usage)(void); +}; + +#endif