diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2016-09-28 19:46:12 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2016-09-29 15:43:05 +0200 |
commit | 58c955f5f23e874e4f8c2a4b389e46c0775e7f07 (patch) | |
tree | 568f004df95780b881d22284d642f93e5673221c /test | |
parent | ae9ede926a1747216b54e22398edde203ec9a03c (diff) |
core: allow building and running without udev
Instead of relying on the udev daemon and GUDev to manage the devices reported
by the kernel, we can now run ModemManager relying solely on the kernel events
reported via the new ReportKernelEvent() API. Therefore, the '--no-auto-scan'
option is implicit for the ModemManager daemon when udev is disabled in the
build.
Additionally, a new custom implementation of the kernel device object is
provided, which uses sysfs to load the properties and attributes required in
each kernel device, instead of using a GUdevDevice.
The udev rule files are kept in place, and a simple custom parser is provided
which preloads all rules in memory once and then applies them to the different
kernel objects reported via ReportKernelEvent(), e.g. to set port type hints.
A simple unit test setup is prepared to validate the udev rules during the
`check' Makefile target.
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.am | 29 | ||||
-rw-r--r-- | test/mmrules.c | 173 |
2 files changed, 202 insertions, 0 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index ae526cbb..cd2b5d56 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -6,12 +6,16 @@ EXTRA_DIST = # lsudev ################################################################################ +if WITH_UDEV + noinst_PROGRAMS += lsudev lsudev_SOURCES = lsudev.c lsudev_CPPFLAGS = $(GUDEV_CFLAGS) lsudev_LDADD = $(GUDEV_LIBS) +endif + ################################################################################ # mmtty ################################################################################ @@ -38,6 +42,31 @@ mmtty_LDADD = \ $(NULL) ################################################################################ +# mmrules +################################################################################ + +noinst_PROGRAMS += mmrules + +mmrules_SOURCES = mmrules.c + +mmrules_CPPFLAGS = \ + $(MM_CFLAGS) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/src \ + -I$(top_srcdir)/src/kerneldevice \ + -I$(top_srcdir)/include \ + -I$(top_builddir)/include \ + -I$(top_srcdir)/libmm-glib \ + -I$(top_srcdir)/libmm-glib/generated \ + -I$(top_builddir)/libmm-glib/generated + $(NULL) + +mmrules_LDADD = \ + $(MM_LIBS) \ + $(top_builddir)/src/libkerneldevice.la \ + $(NULL) + +################################################################################ # mmcli-test-sms ################################################################################ diff --git a/test/mmrules.c b/test/mmrules.c new file mode 100644 index 00000000..f8c4c3e4 --- /dev/null +++ b/test/mmrules.c @@ -0,0 +1,173 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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 2 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: + * + * Copyright (C) 2015 Aleksander Morgado <aleksander@aleksander.es> + */ + +#include "config.h" + +#include <stdio.h> +#include <stdlib.h> +#include <locale.h> +#include <string.h> + +#include <glib.h> +#include <gio/gio.h> + +#include <mm-log.h> +#include <mm-kernel-device-generic-rules.h> + +#define PROGRAM_NAME "mmrules" +#define PROGRAM_VERSION PACKAGE_VERSION + +/* Context */ +static gchar *path; +static gboolean verbose_flag; +static gboolean version_flag; + +static GOptionEntry main_entries[] = { + { "path", 'p', 0, G_OPTION_ARG_FILENAME, &path, + "Specify path to udev rules directory", + "[PATH]" + }, + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag, + "Run action with verbose logs", + NULL + }, + { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag, + "Print version", + NULL + }, + { NULL } +}; + +void +_mm_log (const char *loc, + const char *func, + guint32 level, + const char *fmt, + ...) +{ + va_list args; + gchar *msg; + + if (!verbose_flag) + return; + + va_start (args, fmt); + msg = g_strdup_vprintf (fmt, args); + va_end (args); + g_print ("%s\n", msg); + g_free (msg); +} + +static void +print_version_and_exit (void) +{ + g_print ("\n" + PROGRAM_NAME " " PROGRAM_VERSION "\n" + "Copyright (2015) Aleksander Morgado\n" + "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law.\n" + "\n"); + exit (EXIT_SUCCESS); +} + +static void +print_rule (MMUdevRule *rule) +{ + /* Process conditions */ + if (rule->conditions) { + guint i; + + for (i = 0; i < rule->conditions->len; i++) { + MMUdevRuleMatch *rule_match; + + rule_match = &g_array_index (rule->conditions, MMUdevRuleMatch, i); + switch (rule_match->type) { + case MM_UDEV_RULE_MATCH_TYPE_EQUAL: + g_print (" [condition %u] %s == %s\n", + i, rule_match->parameter, rule_match->value); + break; + case MM_UDEV_RULE_MATCH_TYPE_NOT_EQUAL: + g_print (" [condition %u] %s != %s\n", + i, rule_match->parameter, rule_match->value); + break; + case MM_UDEV_RULE_MATCH_TYPE_UNKNOWN: + g_assert_not_reached (); + } + } + } + + /* Process result */ + switch (rule->result.type) { + case MM_UDEV_RULE_RESULT_TYPE_LABEL: + g_print (" [result] label %s\n", rule->result.content.tag); + break; + case MM_UDEV_RULE_RESULT_TYPE_GOTO_INDEX: + g_print (" [result] jump to rule %u\n", rule->result.content.index); + break; + case MM_UDEV_RULE_RESULT_TYPE_PROPERTY: + g_print (" [result] set property %s = %s\n", + rule->result.content.property.name, rule->result.content.property.value); + break; + case MM_UDEV_RULE_RESULT_TYPE_GOTO_TAG: + case MM_UDEV_RULE_RESULT_TYPE_UNKNOWN: + g_assert_not_reached (); + } +} + +int main (int argc, char **argv) +{ + GOptionContext *context; + GArray *rules; + guint i; + GError *error = NULL; + + setlocale (LC_ALL, ""); + + g_type_init (); + + /* Setup option context, process it and destroy it */ + context = g_option_context_new ("- ModemManager udev rules testing"); + g_option_context_add_main_entries (context, main_entries, NULL); + g_option_context_parse (context, &argc, &argv, NULL); + g_option_context_free (context); + + if (version_flag) + print_version_and_exit (); + + /* No device path given? */ + if (!path) { + g_printerr ("error: no path specified\n"); + exit (EXIT_FAILURE); + } + + /* Load rules from directory */ + rules = mm_kernel_device_generic_rules_load (path, &error); + if (!rules) { + g_printerr ("error: couldn't load rules: %s", error->message); + exit (EXIT_FAILURE); + } + + /* Print loaded rules */ + for (i = 0; i < rules->len; i++) { + g_print ("-----------------------------------------\n"); + g_print ("rule [%u]:\n", i); + print_rule (&g_array_index (rules, MMUdevRule, i)); + } + + g_array_unref (rules); + + return EXIT_SUCCESS; +} |