Skip to content

Commit

Permalink
Configurable ip binary path
Browse files Browse the repository at this point in the history
Different OS have ip binary on different locations

It can be configured at:
  - compile time `IP_BINARY`
  - execution time `--ip-binary`
  • Loading branch information
albfan committed Oct 27, 2021
1 parent 2bd4ddb commit 986f596
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ OPTION(BUILD_ENABLE_DEBUG "Enable Debug" ON )
OPTION(RELY_UDEV "Rely in udev tag to select device" OFF )
OPTION(BUILD_TESTS "Enable TEST" ON )
OPTION(BUILD_ENABLE_CPPCHECK "Enable CPPCheck static analysis" OFF )
SET(IP_BINARY "/bin/ip" CACHE STRING "Path to ip binary")

if(BUILD_ENABLE_DEBUG)
add_definitions(-DBUILD_ENABLE_DEBUG)
Expand Down
2 changes: 2 additions & 0 deletions config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define CONFIG_H

#cmakedefine BUILD_BINDIR "@BUILD_BINDIR@"
#cmakedefine RELY_UDEV @RELY_UDEV@
#cmakedefine IP_BINARY @IP_BINARY@

#cmakedefine PACKAGE_STRING "@PACKAGE_STRING@"

Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ AC_ARG_ENABLE([rely-udev],
AS_HELP_STRING([--enable-rely-udev], [Use tagged device with miraclecast]), AC_DEFINE([RELY_UDEV], [], [Rely on udev to find miraclecast device]))
AC_ARG_ENABLE([log-debug],
AS_HELP_STRING([--disable-log-debug], [Disable log debug]), , AC_DEFINE([BUILD_ENABLE_DEBUG], [], [Enable debug log level]))
AC_ARG_VAR(IP_BINARY, [Path for ip binary])
if test -z "$IP_BINARY"; then
IP_BINARY=/bin/ip
fi
AC_DEFINE_UNQUOTED([IP_BINARY], [$IP_BINARY], [Path for ip binary])

#
# Mandatory dependencies
#
Expand Down Expand Up @@ -166,6 +172,7 @@ AC_MSG_NOTICE([Build configuration:
libdir: $libdir
includedir: $includedir
sysconfdir: $sysconfdir
ip-binary: $IP_BINARY
Miscellaneous Options:
building tests: $have_check
Expand All @@ -183,6 +190,7 @@ AC_MSG_NOTICE([Build configuration:
libdir: $libdir
includedir: $includedir
sysconfdir: $sysconfdir
ip-binary: $IP_BINARY
Miscellaneous Options:
building tests: $have_check
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ if get_option('rely-udev')
add_project_arguments('-DRELY_UDEV', language: 'c')
endif

add_project_arguments('-DIP_BINARY='+get_option('ip-binary'), language: 'c')

glib2 = dependency('glib-2.0')
udev = dependency('libudev')
libsystemd = dependency('libsystemd')
Expand Down
4 changes: 4 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ option('build-tests',
type: 'boolean',
value: true,
description: 'Enable TEST')
option('ip-binary',
type: 'string',
value: '/bin/ip',
description: 'Path for ip binary')
7 changes: 5 additions & 2 deletions src/dhcp/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@
#include "shl_log.h"
#include "config.h"

#define XSTR(x) STR(x)
#define STR(x) #x

static const char *arg_netdev;
static const char *arg_ip_binary = "/bin/ip";
static const char *arg_ip_binary = XSTR(IP_BINARY);
static bool arg_server;
static char arg_local[INET_ADDRSTRLEN];
static char arg_gateway[INET_ADDRSTRLEN];
Expand Down Expand Up @@ -749,7 +752,7 @@ static int help(void)
" --log-time Prefix log-messages with timestamp\n"
"\n"
" --netdev <dev> Network device to run on\n"
" --ip-binary <path> Path to 'ip' binary [default: /bin/ip]\n"
" --ip-binary <path> Path to 'ip' binary [default: "XSTR(IP_BINARY)"]\n"
" --comm-fd <int> Comm-socket FD passed through execve()\n"
"\n"
"Server Options:\n"
Expand Down
17 changes: 17 additions & 0 deletions src/wifi/wifid-link.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void link_free(struct link *l)
free(l->friendly_name);
free(l->ifname);
free(l->config_methods);
free(l->ip_binary);
free(l);
}

Expand Down Expand Up @@ -164,6 +165,22 @@ int link_set_config_methods(struct link *l, char *config_methods)
return 0;
}

int link_set_ip_binary(struct link *l, const char *ip_binary)
{
char *ipb;

if (!ip_binary)
return log_EINVAL();

ipb = strdup(ip_binary);
if (!ipb)
return log_ENOMEM();

free(l->ip_binary);
l->ip_binary = ipb;
return 0;
}

bool link_get_managed(struct link *l)
{
return l->managed;
Expand Down
8 changes: 8 additions & 0 deletions src/wifi/wifid-supplicant.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ static int supplicant_group_spawn_dhcp_server(struct supplicant_group *g,
argv[i++] = g->ifname;
argv[i++] = "--comm-fd";
argv[i++] = commfd;
if (g->s->l->ip_binary) {
argv[i++] = "--ip-binary";
argv[i++] = g->s->l->ip_binary;
}
argv[i] = NULL;

if (execvpe(argv[0], argv, environ)< 0) {
Expand Down Expand Up @@ -458,6 +462,10 @@ static int supplicant_group_spawn_dhcp_client(struct supplicant_group *g)
argv[i++] = g->ifname;
argv[i++] = "--comm-fd";
argv[i++] = commfd;
if (g->s->l->ip_binary) {
argv[i++] = "--ip-binary";
argv[i++] = g->s->l->ip_binary;
}
argv[i] = NULL;

if (execvpe(argv[0], argv, environ) < 0) {
Expand Down
12 changes: 12 additions & 0 deletions src/wifi/wifid.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@
#include "wifid.h"
#include "config.h"

#define XSTR(x) STR(x)
#define STR(x) #x
const char *interface_name = NULL;
const char *config_methods = NULL;
unsigned int arg_wpa_loglevel = LOG_NOTICE;
bool arg_wpa_syslog = false;
bool use_dev = false;
bool lazy_managed = false;
const char *arg_ip_binary = NULL;


/*
* Manager Handling
Expand Down Expand Up @@ -111,6 +115,8 @@ static void manager_add_udev_link(struct manager *m,

if(use_dev)
link_use_dev(l);
if(arg_ip_binary)
link_set_ip_binary(l, arg_ip_binary);

#ifdef RELY_UDEV
bool managed = udev_device_has_tag(d, "miracle") && !lazy_managed;
Expand Down Expand Up @@ -484,6 +490,7 @@ static int help(void)
" --wpa-syslog wpa_supplicant use syslog\n"
" --use-dev enable workaround for 'no ifname' issue\n"
" --lazy-managed manage interface only when user decide to do\n"
" --ip-binary <path> path to 'ip' binary [default: "XSTR(IP_BINARY)"]\n"
, program_invocation_short_name);
/*
* 80-char barrier:
Expand All @@ -504,6 +511,7 @@ static int parse_argv(int argc, char *argv[])
ARG_USE_DEV,
ARG_CONFIG_METHODS,
ARG_LAZY_MANAGED,
ARG_IP_BINARY,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
Expand All @@ -517,6 +525,7 @@ static int parse_argv(int argc, char *argv[])
{ "use-dev", no_argument, NULL, ARG_USE_DEV },
{ "config-methods", required_argument, NULL, ARG_CONFIG_METHODS },
{ "lazy-managed", no_argument, NULL, ARG_LAZY_MANAGED },
{ "ip-binary", required_argument, NULL, ARG_IP_BINARY },
{}
};
int c;
Expand Down Expand Up @@ -552,6 +561,9 @@ static int parse_argv(int argc, char *argv[])
case ARG_WPA_SYSLOG:
arg_wpa_syslog = true;
break;
case ARG_IP_BINARY:
arg_ip_binary = optarg;
break;
case '?':
return -EINVAL;
}
Expand Down
3 changes: 3 additions & 0 deletions src/wifi/wifid.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ struct link {
char *friendly_name;
char *wfd_subelements;
char *config_methods;
char *ip_binary;

size_t peer_cnt;
struct shl_htable peers;
Expand Down Expand Up @@ -159,6 +160,8 @@ void link_free(struct link *l);
void link_use_dev(struct link *l);
bool link_is_using_dev(struct link *l);

int link_set_ip_binary(struct link *l, const char *ip_binary);

int link_set_managed(struct link *l, bool set);
bool link_get_managed(struct link *l);
int link_renamed(struct link *l, const char *ifname);
Expand Down

0 comments on commit 986f596

Please sign in to comment.