convert to multicall binary

This commit is contained in:
Ariadne Conill 2020-07-24 03:07:19 -06:00
parent b9f1ae8254
commit 1a9fd7a0cc
5 changed files with 139 additions and 27 deletions

View file

@ -5,7 +5,7 @@ PACKAGE_BUGREPORT := https://github.com/kaniini/ifupdown-ng/issues/new
INTERFACES_FILE := /etc/network/interfaces INTERFACES_FILE := /etc/network/interfaces
STATE_FILE := /run/ifstate 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}\" 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_OBJ = ${LIBIFUPDOWN_SRC:.c=.o}
LIBIFUPDOWN_LIB = libifupdown.a LIBIFUPDOWN_LIB = libifupdown.a
MULTICALL_SRC = cmd/multicall.c
CMDS = ifquery ifup ifdown MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o}
MULTICALL = ifupdown
LIBS = ${LIBIFUPDOWN_LIB}
all: libifupdown.a ${CMDS}
IFQUERY_SRC = cmd/ifquery.c IFQUERY_SRC = cmd/ifquery.c
IFQUERY_OBJ = ${IFQUERY_SRC:.c=.o} IFQUERY_OBJ = ${IFQUERY_SRC:.c=.o}
ifquery: ${LIBS} ${IFQUERY_OBJ}
${CC} -o $@ ${IFQUERY_OBJ} ${LIBS}
IFUPDOWN_SRC = cmd/ifupdown.c IFUPDOWN_SRC = cmd/ifupdown.c
IFUPDOWN_OBJ = ${IFUPDOWN_SRC:.c=.o} IFUPDOWN_OBJ = ${IFUPDOWN_SRC:.c=.o}
ifup: ${LIBS} ${IFUPDOWN_OBJ}
${CC} -o $@ ${IFUPDOWN_OBJ} ${LIBS}
ifdown: ifup CMD_OBJ = ${MULTICALL_OBJ} ${IFQUERY_OBJ} ${IFUPDOWN_OBJ}
ln -s ifup $@
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} libifupdown.a: ${LIBIFUPDOWN_OBJ}
${AR} -rcs $@ ${LIBIFUPDOWN_OBJ} ${AR} -rcs $@ ${LIBIFUPDOWN_OBJ}
clean: clean:
rm -f ${LIBIFUPDOWN_OBJ} ${IFQUERY_OBJ} ${IFUPDOWN_OBJ} rm -f ${LIBIFUPDOWN_OBJ} ${CMD_OBJ}
rm -f ${CMDS} rm -f ${CMDS} ${MULTICALL}
check: libifupdown.a ${CMDS} check: libifupdown.a ${CMDS}
kyua test kyua test
install: all install: all
install -D -m755 ./ifquery ${DESTDIR}/sbin/ifquery install -D -m755 ${MULTICALL} ${DESTDIR}/sbin/${MULTICALL}
install -D -m755 ./ifup ${DESTDIR}/sbin/ifup for i in ${CMDS}; do \
ln -s /sbin/ifup ${DESTDIR}/sbin/ifdown ln -s /sbin/${MULTICALL} ${DESTDIR}/sbin/$$i; \
done

View file

@ -19,6 +19,7 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "libifupdown/libifupdown.h" #include "libifupdown/libifupdown.h"
#include "cmd/multicall.h"
void void
print_interface(struct lif_interface *iface) print_interface(struct lif_interface *iface)
@ -61,7 +62,7 @@ print_interface(struct lif_interface *iface)
} }
void void
usage() ifquery_usage(void)
{ {
fprintf(stderr, "usage: ifquery [options] <interfaces>\n"); fprintf(stderr, "usage: ifquery [options] <interfaces>\n");
fprintf(stderr, " ifquery [options] --list\n"); fprintf(stderr, " ifquery [options] --list\n");
@ -138,7 +139,7 @@ list_state(struct lif_dict *state, struct match_options *opts)
} }
int int
main(int argc, char *argv[]) ifquery_main(int argc, char *argv[])
{ {
struct lif_dict state = {}; struct lif_dict state = {};
struct lif_dict collection = {}; struct lif_dict collection = {};
@ -168,7 +169,7 @@ main(int argc, char *argv[])
switch (c) { switch (c) {
case 'h': case 'h':
usage(); ifquery_usage();
break; break;
case 'V': case 'V':
lif_common_version(); lif_common_version();
@ -214,7 +215,7 @@ main(int argc, char *argv[])
/* --list --state is not allowed */ /* --list --state is not allowed */
if (listing && listing_stat) if (listing && listing_stat)
usage(); ifquery_usage();
if (listing) if (listing)
{ {
@ -228,7 +229,7 @@ main(int argc, char *argv[])
} }
if (optind >= argc) if (optind >= argc)
usage(); ifquery_usage();
int idx = optind; int idx = optind;
for (; idx < argc; idx++) for (; idx < argc; idx++)
@ -254,3 +255,9 @@ main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
struct if_applet ifquery_applet = {
.name = "ifquery",
.main = ifquery_main,
.usage = ifquery_usage
};

View file

@ -19,6 +19,7 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "libifupdown/libifupdown.h" #include "libifupdown/libifupdown.h"
#include "cmd/multicall.h"
struct match_options { struct match_options {
bool is_auto; bool is_auto;
@ -31,7 +32,7 @@ static bool up;
static struct lif_execute_opts exec_opts = {}; static struct lif_execute_opts exec_opts = {};
void void
usage() ifupdown_usage(void)
{ {
fprintf(stderr, "usage: %s [options] <interfaces>\n", argv0); fprintf(stderr, "usage: %s [options] <interfaces>\n", argv0);
@ -106,7 +107,7 @@ change_auto_interfaces(struct lif_dict *collection, struct lif_dict *state, stru
} }
int int
main(int argc, char *argv[]) ifupdown_main(int argc, char *argv[])
{ {
argv0 = basename(argv[0]); argv0 = basename(argv[0]);
up = !is_ifdown(); up = !is_ifdown();
@ -137,7 +138,7 @@ main(int argc, char *argv[])
switch (c) { switch (c) {
case 'h': case 'h':
usage(); ifupdown_usage();
break; break;
case 'V': case 'V':
lif_common_version(); lif_common_version();
@ -187,7 +188,7 @@ main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
else if (optind >= argc) else if (optind >= argc)
usage(); ifupdown_usage();
int idx = optind; int idx = optind;
for (; idx < argc; idx++) for (; idx < argc; idx++)
@ -231,3 +232,15 @@ main(int argc, char *argv[])
return EXIT_SUCCESS; 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
};

60
cmd/multicall.c Normal file
View file

@ -0,0 +1,60 @@
/*
* cmd/multicall.c
* Purpose: multi-call binary frontend
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* 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 <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

27
cmd/multicall.h Normal file
View file

@ -0,0 +1,27 @@
/*
* cmd/multicall.h
* Purpose: structures for multicall frontend
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* 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