sysfs: introduce sysfs module

Netconfig was the only user of sysfs but now other modules will
also need it.

Adding existing API for IPv6 settings, a IPv4 and IPv6 'supports'
checker, and a setter for IPv4 settings.
This commit is contained in:
James Prestwood 2021-11-03 15:14:59 -07:00 committed by Denis Kenzior
parent 2c7998e8db
commit 6ea58f9fde
3 changed files with 138 additions and 0 deletions

View File

@ -246,6 +246,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \
src/diagnostic.h src/diagnostic.c \
src/ip-pool.h src/ip-pool.c \
src/band.h src/band.c \
src/sysfs.h src/sysfs.c \
$(eap_sources) \
$(builtin_sources)

109
src/sysfs.c Normal file
View File

@ -0,0 +1,109 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2021 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <ell/ell.h>
#include "src/sysfs.h"
static int write_string(const char *file, const char *value)
{
size_t l = strlen(value);
int fd;
int r;
fd = L_TFR(open(file, O_WRONLY));
if (fd < 0)
return -errno;
r = L_TFR(write(fd, value, l));
L_TFR(close(fd));
return r;
}
static bool sysfs_supports_ip_setting(const char *ipv, const char *ifname,
const char *setting)
{
struct stat st;
int err;
L_AUTO_FREE_VAR(char *, file) =
l_strdup_printf("/proc/sys/net/%s/conf/%s/%s", ipv,
ifname, setting);
err = stat(file, &st);
if (!err && S_ISREG(st.st_mode) != 0)
return true;
return false;
}
bool sysfs_supports_ipv6_setting(const char *ifname, const char *setting)
{
return sysfs_supports_ip_setting("ipv6", ifname, setting);
}
int sysfs_write_ipv6_setting(const char *ifname, const char *setting,
const char *value)
{
int r;
L_AUTO_FREE_VAR(char *, file) =
l_strdup_printf("/proc/sys/net/ipv6/conf/%s/%s",
ifname, setting);
r = write_string(file, value);
if (r < 0)
l_error("Unable to write %s to %s", setting, file);
return r;
}
bool sysfs_supports_ipv4_setting(const char *ifname, const char *setting)
{
return sysfs_supports_ip_setting("ipv4", ifname, setting);
}
int sysfs_write_ipv4_setting(const char *ifname, const char *setting,
const char *value)
{
int r;
L_AUTO_FREE_VAR(char *, file) =
l_strdup_printf("/proc/sys/net/ipv4/conf/%s/%s",
ifname, setting);
r = write_string(file, value);
if (r < 0)
l_error("Unable to write %s to %s", setting, file);
return r;
}

28
src/sysfs.h Normal file
View File

@ -0,0 +1,28 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2021 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
bool sysfs_supports_ipv6_setting(const char *ifname, const char *setting);
int sysfs_write_ipv6_setting(const char *ifname, const char *setting,
const char *value);
bool sysfs_supports_ipv4_setting(const char *ifname, const char *setting);
int sysfs_write_ipv4_setting(const char *ifname, const char *setting,
const char *value);