]> git.ipfire.org Git - pakfire.git/commitdiff
os: Implement reading some values from /proc/cpuinfo
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Oct 2023 10:28:47 +0000 (10:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Oct 2023 10:28:47 +0000 (10:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
src/libpakfire/include/pakfire/os.h [new file with mode: 0644]
src/libpakfire/os.c [new file with mode: 0644]
tests/libpakfire/os.c [new file with mode: 0644]

index fc9e8981f4859629ee20a67fba33fe0e807e4fe1..3922618e6d11f1d9e687423d14fc0b745c2b1a20 100644 (file)
@@ -30,6 +30,7 @@
 /tests/libpakfire/key
 /tests/libpakfire/main
 /tests/libpakfire/makefile
+/tests/libpakfire/os
 /tests/libpakfire/package
 /tests/libpakfire/packager
 /tests/libpakfire/parser
index ef94a43a4dac8a07dc7454f6a197bed07c8768df..da456dbcf119cdb8ef1baa4d4395e8ba802947c9 100644 (file)
@@ -236,6 +236,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/mirror.c \
        src/libpakfire/mirrorlist.c \
        src/libpakfire/mount.c \
+       src/libpakfire/os.c \
        src/libpakfire/package.c \
        src/libpakfire/packager.c \
        src/libpakfire/packagelist.c \
@@ -282,6 +283,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/mirror.h \
        src/libpakfire/include/pakfire/mirrorlist.h \
        src/libpakfire/include/pakfire/mount.h \
+       src/libpakfire/include/pakfire/os.h \
        src/libpakfire/include/pakfire/package.h \
        src/libpakfire/include/pakfire/packager.h \
        src/libpakfire/include/pakfire/packagelist.h \
@@ -599,6 +601,7 @@ check_PROGRAMS += \
        tests/libpakfire/jail \
        tests/libpakfire/key \
        tests/libpakfire/makefile \
+       tests/libpakfire/os \
        tests/libpakfire/package \
        tests/libpakfire/packager \
        tests/libpakfire/parser \
@@ -788,6 +791,18 @@ tests_libpakfire_makefile_CFLAGS = \
 tests_libpakfire_makefile_LDADD = \
        $(TESTSUITE_LDADD)
 
+dist_tests_libpakfire_os_SOURCES = \
+       tests/libpakfire/os.c
+
+tests_libpakfire_os_CPPFLAGS = \
+       $(TESTSUITE_CPPFLAGS)
+
+tests_libpakfire_os_CFLAGS = \
+       $(TESTSUITE_CFLAGS)
+
+tests_libpakfire_os_LDADD = \
+       $(TESTSUITE_LDADD)
+
 dist_tests_libpakfire_package_SOURECES = \
        tests/libpakfire/package.c
 
diff --git a/src/libpakfire/include/pakfire/os.h b/src/libpakfire/include/pakfire/os.h
new file mode 100644 (file)
index 0000000..8cf6021
--- /dev/null
@@ -0,0 +1,40 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program 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 General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_OS_H
+#define PAKFIRE_OS_H
+
+#ifdef PAKFIRE_PRIVATE
+
+// CPU info
+
+#define CPU_VENDOR_MAX 16
+#define CPU_MODEL_MAX  256
+
+struct pakfire_cpuinfo {
+       char vendor[CPU_VENDOR_MAX];
+       char model[CPU_MODEL_MAX];
+};
+
+int pakfire_cpuinfo(struct pakfire_cpuinfo* cpuinfo);
+
+#endif /* PAKFIRE_PRIVATE */
+
+#endif /* PAKFIRE_OS_H */
diff --git a/src/libpakfire/os.c b/src/libpakfire/os.c
new file mode 100644 (file)
index 0000000..66d0378
--- /dev/null
@@ -0,0 +1,141 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program 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 General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <pakfire/os.h>
+#include <pakfire/string.h>
+
+typedef int (*pakfire_parse_line)(char* line, size_t length, void* data);
+
+static int pakfire_parse_file(const char* path, pakfire_parse_line parse, void* data) {
+       FILE* f = NULL;
+       char* line = NULL;
+       size_t length = 0;
+       int r = 0;
+
+       ssize_t bytes_read = 0;
+
+       // Open the file
+       f = fopen(path, "r");
+       if (!f)
+               return -errno;
+
+       // Walk through the file line by line
+       for (;;) {
+               bytes_read = getline(&line, &length, f);
+               if (bytes_read < 0)
+                       break;
+
+               r = parse(line, length, data);
+               if (r)
+                       goto ERROR;
+       }
+
+ERROR:
+       if (line)
+               free(line);
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
+static int pakfire_split_line(char* line, size_t length, const char** key, const char** value) {
+       char* k = line;
+       char* v = NULL;
+       char* p = NULL;
+
+       // Strip any trailing whitespace
+       pakfire_string_strip(line);
+
+       if (!*line)
+               return 0;
+
+       // Find the delimiter
+       p = strchr(line, ':');
+       if (!p)
+               return -EINVAL;
+
+       // The value starts somewhere after the delimiter
+       v = p + 1;
+
+       // Replace the delimiter by NULL
+       *p = '\0';
+
+       // Strip the key
+       pakfire_string_strip(k);
+
+       // The key is empty
+       if (!*k)
+               return -EINVAL;
+
+       // Strip the value
+       pakfire_string_strip(v);
+
+       // Return the pointers
+       *key   = k;
+       *value = v;
+
+       return 0;
+}
+
+// CPU Info
+
+static int pakfire_parse_cpuinfo(char* line, size_t length, void* data) {
+       struct pakfire_cpuinfo* cpuinfo = data;
+       int r;
+
+       // Key & Value
+       const char* k = NULL;
+       const char* v = NULL;
+
+       // Split the line
+       r = pakfire_split_line(line, length, &k, &v);
+       if (r)
+               return r;
+
+       // If we didn't get a result we skip this line
+       if (!k || !v)
+               return 0;
+
+       // Vendor
+       if (strcmp(k, "vendor_id") == 0) {
+               r = pakfire_string_set(cpuinfo->vendor, v);
+               if (r)
+                       return r;
+
+       // Model Name
+       } else if (strcmp(k, "model name") == 0) {
+               r = pakfire_string_set(cpuinfo->model, v);
+               if (r)
+                       return r;
+       }
+
+       return 0;
+}
+
+int pakfire_cpuinfo(struct pakfire_cpuinfo* cpuinfo) {
+       return pakfire_parse_file("/proc/cpuinfo", pakfire_parse_cpuinfo, cpuinfo);
+}
diff --git a/tests/libpakfire/os.c b/tests/libpakfire/os.c
new file mode 100644 (file)
index 0000000..940aec5
--- /dev/null
@@ -0,0 +1,45 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program 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 General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pakfire/os.h>
+
+#include "../testsuite.h"
+
+// This test parses /proc/cpuinfo
+static int test_cpuinfo(const struct test* t) {
+       struct pakfire_cpuinfo cpuinfo = {};
+
+       ASSERT_SUCCESS(pakfire_cpuinfo(&cpuinfo));
+
+       // We can only check is something has been read
+       ASSERT(*cpuinfo.vendor);
+       ASSERT(*cpuinfo.model);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+int main(int argc, const char* argv[]) {
+       testsuite_add_test(test_cpuinfo);
+
+       return testsuite_run(argc, argv);
+}