sysfs.c (2553B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2021 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <sys/stat.h> 28 #include <errno.h> 29 #include <fcntl.h> 30 #include <unistd.h> 31 32 #include <ell/ell.h> 33 34 #include "src/sysfs.h" 35 36 static int write_string(const char *file, const char *value) 37 { 38 size_t l = strlen(value); 39 int fd; 40 int r; 41 42 fd = L_TFR(open(file, O_WRONLY)); 43 if (fd < 0) 44 return -errno; 45 46 r = L_TFR(write(fd, value, l)); 47 L_TFR(close(fd)); 48 49 return r; 50 } 51 52 static bool sysfs_supports_ip_setting(const char *ipv, const char *ifname, 53 const char *setting) 54 { 55 struct stat st; 56 int err; 57 L_AUTO_FREE_VAR(char *, file) = 58 l_strdup_printf("/proc/sys/net/%s/conf/%s/%s", ipv, 59 ifname, setting); 60 61 err = stat(file, &st); 62 63 if (!err && S_ISREG(st.st_mode) != 0) 64 return true; 65 66 return false; 67 } 68 69 bool sysfs_supports_ipv6_setting(const char *ifname, const char *setting) 70 { 71 return sysfs_supports_ip_setting("ipv6", ifname, setting); 72 } 73 74 int sysfs_write_ipv6_setting(const char *ifname, const char *setting, 75 const char *value) 76 { 77 int r; 78 79 L_AUTO_FREE_VAR(char *, file) = 80 l_strdup_printf("/proc/sys/net/ipv6/conf/%s/%s", 81 ifname, setting); 82 83 r = write_string(file, value); 84 if (r < 0) 85 l_error("Unable to write %s to %s", setting, file); 86 87 return r; 88 } 89 90 bool sysfs_supports_ipv4_setting(const char *ifname, const char *setting) 91 { 92 return sysfs_supports_ip_setting("ipv4", ifname, setting); 93 } 94 95 int sysfs_write_ipv4_setting(const char *ifname, const char *setting, 96 const char *value) 97 { 98 int r; 99 100 L_AUTO_FREE_VAR(char *, file) = 101 l_strdup_printf("/proc/sys/net/ipv4/conf/%s/%s", 102 ifname, setting); 103 104 r = write_string(file, value); 105 if (r < 0) 106 l_error("Unable to write %s to %s", setting, file); 107 108 return r; 109 }