2020-07-24 09:07:19 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-12 04:07:32 +00:00
|
|
|
#define _GNU_SOURCE
|
2020-07-24 09:07:19 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-08-12 04:07:32 +00:00
|
|
|
#include <getopt.h>
|
2020-07-24 09:07:19 +00:00
|
|
|
#include "cmd/multicall.h"
|
|
|
|
|
2020-07-24 10:52:15 +00:00
|
|
|
char *argv0;
|
|
|
|
|
2020-08-12 22:56:51 +00:00
|
|
|
#ifdef CONFIG_IFQUERY
|
2020-07-24 09:07:19 +00:00
|
|
|
extern struct if_applet ifquery_applet;
|
2020-08-12 22:56:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_IFUPDOWN
|
2020-07-24 09:07:19 +00:00
|
|
|
extern struct if_applet ifup_applet;
|
|
|
|
extern struct if_applet ifdown_applet;
|
2020-08-12 22:56:51 +00:00
|
|
|
#endif
|
|
|
|
|
2020-08-12 22:38:15 +00:00
|
|
|
#ifdef CONFIG_IFCTRSTAT
|
|
|
|
extern struct if_applet ifctrstat_applet;
|
|
|
|
#endif
|
|
|
|
|
2020-11-11 03:02:07 +00:00
|
|
|
#ifdef CONFIG_IFPARSE
|
|
|
|
extern struct if_applet ifparse_applet;
|
|
|
|
#endif
|
|
|
|
|
2020-07-24 09:23:41 +00:00
|
|
|
struct if_applet ifupdown_applet;
|
2020-08-12 04:37:10 +00:00
|
|
|
const struct if_applet *self_applet = NULL;
|
2020-07-24 09:07:19 +00:00
|
|
|
|
|
|
|
struct if_applet *applet_table[] = {
|
2020-08-12 22:38:15 +00:00
|
|
|
#ifdef CONFIG_IFCTRSTAT
|
|
|
|
&ifctrstat_applet,
|
|
|
|
#endif
|
2020-08-12 22:56:51 +00:00
|
|
|
#ifdef CONFIG_IFUPDOWN
|
2020-07-24 09:07:19 +00:00
|
|
|
&ifdown_applet,
|
2020-08-12 22:56:51 +00:00
|
|
|
#endif
|
2020-11-11 03:02:07 +00:00
|
|
|
#ifdef CONFIG_IFPARSE
|
|
|
|
&ifparse_applet,
|
|
|
|
#endif
|
2020-08-12 22:56:51 +00:00
|
|
|
#ifdef CONFIG_IFQUERY
|
2020-07-24 09:07:19 +00:00
|
|
|
&ifquery_applet,
|
2020-08-12 22:56:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_IFUPDOWN
|
2020-07-24 09:07:19 +00:00
|
|
|
&ifup_applet,
|
2020-08-12 22:56:51 +00:00
|
|
|
#endif
|
2020-07-24 09:23:41 +00:00
|
|
|
&ifupdown_applet,
|
2020-07-24 09:07:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-12 02:44:04 +00:00
|
|
|
void multicall_usage(int status) __attribute__((noreturn));
|
2020-07-24 09:23:41 +00:00
|
|
|
|
2020-11-19 10:17:57 +00:00
|
|
|
struct if_applet ifupdown_applet;
|
|
|
|
|
2020-07-24 09:07:19 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2020-07-24 10:52:15 +00:00
|
|
|
argv0 = basename(argv[0]);
|
2020-08-12 04:07:32 +00:00
|
|
|
const struct if_applet **app;
|
2020-07-24 09:07:19 +00:00
|
|
|
|
2020-09-14 23:18:45 +00:00
|
|
|
lif_config_load(CONFIG_FILE);
|
|
|
|
|
2020-07-24 09:07:19 +00:00
|
|
|
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);
|
2020-08-12 02:44:04 +00:00
|
|
|
multicall_usage(EXIT_FAILURE);
|
2020-07-24 09:07:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 04:37:10 +00:00
|
|
|
self_applet = *app;
|
2020-08-12 04:07:32 +00:00
|
|
|
|
2020-11-19 10:17:57 +00:00
|
|
|
if (self_applet != &ifupdown_applet)
|
|
|
|
process_options(*app, argc, argv);
|
|
|
|
|
|
|
|
return self_applet->main(argc, argv);
|
2020-07-24 09:07:19 +00:00
|
|
|
}
|
2020-07-24 09:23:41 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
multicall_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
2020-08-12 02:44:04 +00:00
|
|
|
multicall_usage(EXIT_FAILURE);
|
2020-07-24 09:23:41 +00:00
|
|
|
|
|
|
|
return main(argc - 1, argv + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-08-12 02:44:04 +00:00
|
|
|
multicall_usage(int status)
|
2020-07-24 09:23:41 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: ifupdown <applet> [options]\n");
|
|
|
|
|
|
|
|
fprintf(stderr, "\nBuilt-in applets:\n\t");
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(applet_table); i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", applet_table[i]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2020-08-12 02:44:04 +00:00
|
|
|
exit(status);
|
2020-07-24 09:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct if_applet ifupdown_applet = {
|
|
|
|
.name = "ifupdown",
|
|
|
|
.main = multicall_main,
|
2020-08-12 04:07:32 +00:00
|
|
|
.groups = { &global_option_group, NULL }
|
2020-07-24 09:23:41 +00:00
|
|
|
};
|