aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Kenzior <denkenz@gmail.com>2023-07-17 00:51:47 -0500
committerDenis Kenzior <denkenz@gmail.com>2023-07-17 22:50:20 -0500
commitb54157513b89d3c6ee13ceb4985f95a0b18dad7d (patch)
tree45496703f0a3dcdd50fa114e7310ef81916ed76f
parentae5300261936a43ee78fc75d64317f6a1f789e66 (diff)
sysctl: Add initial implementation
-rw-r--r--Makefile.am6
-rw-r--r--ell/ell.h1
-rw-r--r--ell/sysctl.c112
-rw-r--r--ell/sysctl.h39
4 files changed, 156 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am
index 1d279e2c..738aa75a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -61,7 +61,8 @@ pkginclude_HEADERS = ell/ell.h \
ell/acd.h \
ell/tester.h \
ell/cleanup.h \
- ell/netconfig.h
+ ell/netconfig.h \
+ ell/sysctl.h
lib_LTLIBRARIES = ell/libell.la
@@ -149,7 +150,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
ell/icmp6-private.h \
ell/acd.c \
ell/tester.c \
- ell/netconfig.c
+ ell/netconfig.c \
+ ell/sysctl.c
ell_libell_la_LDFLAGS = -Wl,--no-undefined \
-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
diff --git a/ell/ell.h b/ell/ell.h
index d4489ac8..647f411c 100644
--- a/ell/ell.h
+++ b/ell/ell.h
@@ -66,3 +66,4 @@
#include <ell/acd.h>
#include <ell/tester.h>
#include <ell/netconfig.h>
+#include <ell/sysctl.h>
diff --git a/ell/sysctl.c b/ell/sysctl.c
new file mode 100644
index 00000000..ca9ddff2
--- /dev/null
+++ b/ell/sysctl.c
@@ -0,0 +1,112 @@
+/*
+ *
+ * Embedded Linux library
+ *
+ * Copyright (C) 2022 Intel Corporation. All rights reserved.
+ * Copyright (C) 2023 Cruise LLC. 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
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "sysctl.h"
+#include "useful.h"
+#include "util.h"
+#include "private.h"
+
+static int sysctl_write(const char *file, const void *value, size_t len)
+{
+ int fd;
+ ssize_t r;
+
+ fd = L_TFR(open(file, O_WRONLY));
+ if (unlikely(fd < 0))
+ return -errno;
+
+ r = L_TFR(write(fd, value, len));
+ if (r < 0)
+ r = -errno;
+ else
+ r = 0;
+
+ close(fd);
+ return r;
+}
+
+static int sysctl_read(const char *file, void *dest, size_t len)
+{
+ int fd;
+ ssize_t r;
+
+ fd = L_TFR(open(file, O_RDONLY));
+ if (unlikely(fd < 0))
+ return -errno;
+
+ r = L_TFR(read(fd, dest, len));
+ if (unlikely(r < 0))
+ r = -errno;
+
+ close(fd);
+ return r;
+}
+
+LIB_EXPORT int l_sysctl_get_u32(uint32_t *out_v, const char *format, ...)
+{
+ _auto_(l_free) char *filename = NULL;
+ va_list ap;
+ char valuestr[64];
+ int r;
+
+ va_start(ap, format);
+ filename = l_strdup_vprintf(format, ap);
+ va_end(ap);
+
+ r = sysctl_read(filename, valuestr, sizeof(valuestr) - 1);
+ if (r < 0)
+ return r;
+
+ while (r > 0 && L_IN_SET(valuestr[r - 1], '\n', '\r', '\t', ' '))
+ r--;
+
+ valuestr[r] = '\0';
+
+ return l_safe_atou32(valuestr, out_v);
+}
+
+LIB_EXPORT int l_sysctl_set_u32(uint32_t v, const char *format, ...)
+{
+ _auto_(l_free) char *filename = NULL;
+ va_list ap;
+ char valuestr[64];
+ size_t len;
+
+ va_start(ap, format);
+ filename = l_strdup_vprintf(format, ap);
+ va_end(ap);
+
+ len = snprintf(valuestr, sizeof(valuestr), "%u", v);
+
+ return sysctl_write(filename, valuestr, len);
+}
diff --git a/ell/sysctl.h b/ell/sysctl.h
new file mode 100644
index 00000000..4dd65e87
--- /dev/null
+++ b/ell/sysctl.h
@@ -0,0 +1,39 @@
+/*
+ *
+ * Embedded Linux library
+ *
+ * Copyright (C) 2022 Intel Corporation. All rights reserved.
+ * Copyright (C) 2023 Cruise LLC. 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
+ *
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+int l_sysctl_get_u32(uint32_t *out_v, const char *format, ...)
+ __attribute__((format(printf, 2, 3)));
+int l_sysctl_set_u32(uint32_t v, const char *format, ...)
+ __attribute__((format(printf, 2, 3)));
+
+#ifdef __cplusplus
+}
+#endif