LCOV - code coverage report
Current view: top level - shared - sysctl-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 55 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <fcntl.h>
       5             : #include <stdio.h>
       6             : #include <string.h>
       7             : #include <unistd.h>
       8             : 
       9             : #include "fd-util.h"
      10             : #include "fileio.h"
      11             : #include "log.h"
      12             : #include "macro.h"
      13             : #include "string-util.h"
      14             : #include "sysctl-util.h"
      15             : 
      16           0 : char *sysctl_normalize(char *s) {
      17             :         char *n;
      18             : 
      19           0 :         n = strpbrk(s, "/.");
      20             :         /* If the first separator is a slash, the path is
      21             :          * assumed to be normalized and slashes remain slashes
      22             :          * and dots remains dots. */
      23           0 :         if (!n || *n == '/')
      24           0 :                 return s;
      25             : 
      26             :         /* Otherwise, dots become slashes and slashes become
      27             :          * dots. Fun. */
      28           0 :         while (n) {
      29           0 :                 if (*n == '.')
      30           0 :                         *n = '/';
      31             :                 else
      32           0 :                         *n = '.';
      33             : 
      34           0 :                 n = strpbrk(n + 1, "/.");
      35             :         }
      36             : 
      37           0 :         return s;
      38             : }
      39             : 
      40           0 : int sysctl_write(const char *property, const char *value) {
      41             :         char *p;
      42           0 :         _cleanup_close_ int fd = -1;
      43             : 
      44           0 :         assert(property);
      45           0 :         assert(value);
      46             : 
      47           0 :         log_debug("Setting '%s' to '%.*s'.", property, (int) strcspn(value, NEWLINE), value);
      48             : 
      49           0 :         p = strjoina("/proc/sys/", property);
      50           0 :         fd = open(p, O_WRONLY|O_CLOEXEC);
      51           0 :         if (fd < 0)
      52           0 :                 return -errno;
      53             : 
      54           0 :         if (!endswith(value, "\n"))
      55           0 :                 value = strjoina(value, "\n");
      56             : 
      57           0 :         if (write(fd, value, strlen(value)) < 0)
      58           0 :                 return -errno;
      59             : 
      60           0 :         return 0;
      61             : }
      62             : 
      63           0 : int sysctl_writef(const char *property, const char *format, ...) {
      64           0 :         _cleanup_free_ char *v = NULL;
      65             :         va_list ap;
      66             :         int r;
      67             : 
      68           0 :         va_start(ap, format);
      69           0 :         r = vasprintf(&v, format, ap);
      70           0 :         va_end(ap);
      71             : 
      72           0 :         if (r < 0)
      73           0 :                 return -ENOMEM;
      74             : 
      75           0 :         return sysctl_write(property, v);
      76             : }
      77             : 
      78           0 : int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value) {
      79             :         const char *p;
      80             : 
      81           0 :         assert(IN_SET(af, AF_INET, AF_INET6));
      82           0 :         assert(property);
      83           0 :         assert(value);
      84             : 
      85           0 :         p = strjoina("/proc/sys/net/ipv", af == AF_INET ? "4" : "6",
      86             :                      ifname ? "/conf/" : "", strempty(ifname),
      87             :                      property[0] == '/' ? "" : "/", property);
      88             : 
      89           0 :         log_debug("Setting '%s' to '%s'", p, value);
      90             : 
      91           0 :         return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER);
      92             : }
      93             : 
      94           0 : int sysctl_read(const char *property, char **content) {
      95             :         char *p;
      96             : 
      97           0 :         assert(property);
      98           0 :         assert(content);
      99             : 
     100           0 :         p = strjoina("/proc/sys/", property);
     101           0 :         return read_full_file(p, content, NULL);
     102             : }
     103             : 
     104           0 : int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret) {
     105           0 :         _cleanup_free_ char *value = NULL;
     106             :         const char *p;
     107             :         int r;
     108             : 
     109           0 :         assert(IN_SET(af, AF_INET, AF_INET6));
     110           0 :         assert(property);
     111             : 
     112           0 :         p = strjoina("/proc/sys/net/ipv", af == AF_INET ? "4" : "6",
     113             :                      ifname ? "/conf/" : "", strempty(ifname),
     114             :                      property[0] == '/' ? "" : "/", property);
     115             : 
     116           0 :         r = read_one_line_file(p, &value);
     117           0 :         if (r < 0)
     118           0 :                 return r;
     119             : 
     120           0 :         if (ret)
     121           0 :                 *ret = TAKE_PTR(value);
     122             : 
     123           0 :         return r;
     124             : }

Generated by: LCOV version 1.14