about summary refs log tree commit diff
path: root/src/libnm-client-aux-extern
diff options
context:
space:
mode:
authorSebastien Bacher <seb128@ubuntu.com>2021-07-05 20:35:03 +0200
committerSebastien Bacher <seb128@ubuntu.com>2021-07-05 20:35:03 +0200
commit35779c6675728fa6f0fd0a21cefb904408509c23 (patch)
tree553e7239e0ba182b4f9b29b1e7a9d66a595d08bc /src/libnm-client-aux-extern
parentd92aa7f298fe84d4cf686c5ad64b73438e00d377 (diff)
New upstream version 1.32.2
Diffstat (limited to 'src/libnm-client-aux-extern')
-rw-r--r--src/libnm-client-aux-extern/README.md15
-rw-r--r--src/libnm-client-aux-extern/meson.build11
-rw-r--r--src/libnm-client-aux-extern/nm-default-client.h23
-rw-r--r--src/libnm-client-aux-extern/nm-libnm-aux.c137
-rw-r--r--src/libnm-client-aux-extern/nm-libnm-aux.h24
-rw-r--r--src/libnm-client-aux-extern/tests/meson.build23
-rw-r--r--src/libnm-client-aux-extern/tests/test-libnm-client-aux.c250
7 files changed, 483 insertions, 0 deletions
diff --git a/src/libnm-client-aux-extern/README.md b/src/libnm-client-aux-extern/README.md
new file mode 100644
index 00000000..460b269e
--- /dev/null
+++ b/src/libnm-client-aux-extern/README.md
@@ -0,0 +1,15 @@
+nm-libnm-aux is a static library that:
+
+ - uses the public parts of "libnm"
+ - that can also be statically linked into other users of libnm.
+
+Basically, it is a static library with utility functions that extends
+libnm.
+
+That means:
+
+  - you can use it everywhere where you dynamically link with libnm.
+
+Also, since nm-libnm-aux itself only uses public (stable)
+API of libnm, you theoretically can copy the sources into your
+own source tree.
diff --git a/src/libnm-client-aux-extern/meson.build b/src/libnm-client-aux-extern/meson.build
new file mode 100644
index 00000000..a2b46543
--- /dev/null
+++ b/src/libnm-client-aux-extern/meson.build
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+libnm_client_aux_extern = static_library(
+  'nm-client-aux-extern',
+  sources: files(
+    'nm-libnm-aux.c',
+  ),
+  dependencies: [
+    libnm_dep,
+  ],
+)
diff --git a/src/libnm-client-aux-extern/nm-default-client.h b/src/libnm-client-aux-extern/nm-default-client.h
new file mode 100644
index 00000000..a770ae56
--- /dev/null
+++ b/src/libnm-client-aux-extern/nm-default-client.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2015 Red Hat, Inc.
+ */
+
+#ifndef __NM_DEFAULT_CLIENT_H__
+#define __NM_DEFAULT_CLIENT_H__
+
+/*****************************************************************************/
+
+#include "libnm-glib-aux/nm-default-glib-i18n-lib.h"
+
+#undef NETWORKMANAGER_COMPILATION
+#define NETWORKMANAGER_COMPILATION NM_NETWORKMANAGER_COMPILATION_CLIENT
+
+/*****************************************************************************/
+
+#include "nm-version.h"
+#include "NetworkManager.h"
+
+/*****************************************************************************/
+
+#endif /* __NM_DEFAULT_CLIENT_H__ */
diff --git a/src/libnm-client-aux-extern/nm-libnm-aux.c b/src/libnm-client-aux-extern/nm-libnm-aux.c
new file mode 100644
index 00000000..2d17241d
--- /dev/null
+++ b/src/libnm-client-aux-extern/nm-libnm-aux.c
@@ -0,0 +1,137 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "libnm-client-aux-extern/nm-default-client.h"
+
+#include "nm-libnm-aux.h"
+
+/*****************************************************************************/
+
+NMClient *
+nmc_client_new_async_valist(GCancellable *      cancellable,
+                            GAsyncReadyCallback callback,
+                            gpointer            user_data,
+                            const char *        first_property_name,
+                            va_list             ap)
+{
+    NMClient *nmc;
+
+    nmc = NM_CLIENT(g_object_new_valist(NM_TYPE_CLIENT, first_property_name, ap));
+    g_async_initable_init_async(G_ASYNC_INITABLE(nmc),
+                                G_PRIORITY_DEFAULT,
+                                cancellable,
+                                callback,
+                                user_data);
+    return nmc;
+}
+
+NMClient *
+nmc_client_new_async(GCancellable *      cancellable,
+                     GAsyncReadyCallback callback,
+                     gpointer            user_data,
+                     const char *        first_property_name,
+                     ...)
+{
+    NMClient *nmc;
+    va_list   ap;
+
+    va_start(ap, first_property_name);
+    nmc = nmc_client_new_async_valist(cancellable, callback, user_data, first_property_name, ap);
+    va_end(ap);
+    return nmc;
+}
+
+/*****************************************************************************/
+
+typedef struct {
+    GMainLoop *main_loop;
+    NMClient * nmc;
+    GError *   error;
+} ClientCreateData;
+
+static void
+_nmc_client_new_waitsync_cb(GObject *source_object, GAsyncResult *result, gpointer user_data)
+{
+    ClientCreateData *data = user_data;
+
+    g_async_initable_init_finish(G_ASYNC_INITABLE(source_object), result, &data->error);
+    g_main_loop_quit(data->main_loop);
+}
+
+/**
+ * nmc_client_new:
+ * @cancellable: the cancellable to abort the creation.
+ * @out_nmc: (out): (transfer full): if give, transfers a reference
+ *   to the NMClient instance. Note that this never fails to create
+ *   the NMClient GObject, but depending on the return value,
+ *   the instance was successfully initialized or not.
+ * @error: the error if creation fails.
+ * @first_property_name: the name of the first property
+ * @...: the value of the first property, followed optionally by more
+ *  name/value pairs, followed by %NULL
+ *
+ * Returns: %TRUE, if the client was successfully initalized.
+ *
+ * This uses nm_client_new_async() to create a NMClient instance,
+ * but it iterates the current GMainContext until the client is
+ * ready. As such, it waits for the client creation to complete
+ * (like sync nm_client_new()) but it iterates the caller's GMainContext
+ * (unlike sync nm_client_new()). This is often preferable, because
+ * sync nm_client_new() needs to create an additional internal GMainContext
+ * that it can iterate instead. That has a performance overhead that
+ * is often unnecessary.
+ */
+gboolean
+nmc_client_new_waitsync(GCancellable *cancellable,
+                        NMClient **   out_nmc,
+                        GError **     error,
+                        const char *  first_property_name,
+                        ...)
+{
+    gs_unref_object NMClient *nmc = NULL;
+    nm_auto_unref_gmainloop GMainLoop *main_loop =
+        g_main_loop_new(g_main_context_get_thread_default(), FALSE);
+    ClientCreateData data = {
+        .main_loop = main_loop,
+    };
+    va_list ap;
+
+#if NM_MORE_ASSERTS > 10
+    /* The sync initialization of NMClient is generally a bad idea, because it
+     * brings the overhead of an additional GMainContext. Anyway, since our own
+     * code no longer uses that, we hardly test those code paths. But they should
+     * work just the same. Randomly use instead the sync initialization in a debug
+     * build... */
+    if ((g_random_int() % 2) == 0) {
+        gboolean success;
+
+        va_start(ap, first_property_name);
+        nmc = NM_CLIENT(g_object_new_valist(NM_TYPE_CLIENT, first_property_name, ap));
+        va_end(ap);
+
+        /* iterate the context at least once, just so that the behavior from POV of the
+         * caller is roughly the same. */
+        g_main_context_iteration(nm_client_get_main_context(nmc), FALSE);
+
+        success = g_initable_init(G_INITABLE(nmc), cancellable, error);
+        NM_SET_OUT(out_nmc, g_steal_pointer(&nmc));
+        return success;
+    }
+#endif
+
+    va_start(ap, first_property_name);
+    nmc = nmc_client_new_async_valist(cancellable,
+                                      _nmc_client_new_waitsync_cb,
+                                      &data,
+                                      first_property_name,
+                                      ap);
+    va_end(ap);
+
+    g_main_loop_run(main_loop);
+
+    NM_SET_OUT(out_nmc, g_steal_pointer(&nmc));
+    if (data.error) {
+        g_propagate_error(error, data.error);
+        return FALSE;
+    }
+    return TRUE;
+}
diff --git a/src/libnm-client-aux-extern/nm-libnm-aux.h b/src/libnm-client-aux-extern/nm-libnm-aux.h
new file mode 100644
index 00000000..c982eab7
--- /dev/null
+++ b/src/libnm-client-aux-extern/nm-libnm-aux.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#ifndef __NM_LIBNM_AUX_H__
+#define __NM_LIBNM_AUX_H__
+
+NMClient *nmc_client_new_async_valist(GCancellable *      cancellable,
+                                      GAsyncReadyCallback callback,
+                                      gpointer            user_data,
+                                      const char *        first_property_name,
+                                      va_list             ap);
+
+NMClient *nmc_client_new_async(GCancellable *      cancellable,
+                               GAsyncReadyCallback callback,
+                               gpointer            user_data,
+                               const char *        first_property_name,
+                               ...);
+
+gboolean nmc_client_new_waitsync(GCancellable *cancellable,
+                                 NMClient **   out_nmc,
+                                 GError **     error,
+                                 const char *  first_property_name,
+                                 ...);
+
+#endif /* __NM_LIBNM_AUX_H__ */
diff --git a/src/libnm-client-aux-extern/tests/meson.build b/src/libnm-client-aux-extern/tests/meson.build
new file mode 100644
index 00000000..341ead0f
--- /dev/null
+++ b/src/libnm-client-aux-extern/tests/meson.build
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+exe = executable(
+  'test-libnm-client-aux',
+  'test-libnm-client-aux.c',
+  dependencies: [
+    libnm_dep,
+    glib_dep,
+  ],
+  link_with: [
+    libnm_core_aux_extern,
+    libnm_log_null,
+    libnm_glib_aux,
+    libnm_std_aux,
+    libc_siphash,
+  ],
+)
+
+test(
+  'src/libnm-client-aux-extern/tests/test-libnm-client-aux',
+  test_script,
+  args: test_args + [exe.full_path()]
+)
diff --git a/src/libnm-client-aux-extern/tests/test-libnm-client-aux.c b/src/libnm-client-aux-extern/tests/test-libnm-client-aux.c
new file mode 100644
index 00000000..c4db4d6a
--- /dev/null
+++ b/src/libnm-client-aux-extern/tests/test-libnm-client-aux.c
@@ -0,0 +1,250 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2019 Red Hat, Inc.
+ */
+
+#include "libnm-client-aux-extern/nm-default-client.h"
+
+#include "libnm-core-aux-extern/nm-libnm-core-aux.h"
+#include "libnm-core-aux-intern/nm-libnm-core-utils.h"
+
+#include "libnm-glib-aux/nm-test-utils.h"
+
+/*****************************************************************************/
+
+static NMTeamLinkWatcher *
+_team_link_watcher_from_string_impl(const char *str, gsize nextra, const char *const *vextra)
+{
+    NMTeamLinkWatcher *watcher;
+    gs_free char *     str1_free = NULL;
+    gs_free_error GError *error  = NULL;
+    gsize                 i;
+
+    g_assert(str);
+
+    watcher = nm_utils_team_link_watcher_from_string(str, &error);
+    nmtst_assert_success(watcher, error);
+
+    for (i = 0; i < 1 + nextra; i++) {
+        nm_auto_unref_team_link_watcher NMTeamLinkWatcher *watcher1 = NULL;
+        const char *                                       str1;
+
+        if (i == 0) {
+            str1_free = nm_utils_team_link_watcher_to_string(watcher);
+            g_assert(str1_free);
+            str1 = str1_free;
+            g_assert_cmpstr(str, ==, str1);
+        } else
+            str1 = vextra[i - 1];
+
+        watcher1 = nm_utils_team_link_watcher_from_string(str1, &error);
+        nmtst_assert_success(watcher1, error);
+        if (!nm_team_link_watcher_equal(watcher, watcher1)) {
+            gs_free char *ss1 = NULL;
+            gs_free char *ss2 = NULL;
+
+            g_print(">>> watcher differs: \"%s\" vs. \"%s\"",
+                    (ss1 = nm_utils_team_link_watcher_to_string(watcher)),
+                    (ss2 = nm_utils_team_link_watcher_to_string(watcher1)));
+            g_print(">>> ORIG: \"%s\" vs. \"%s\"", str, str1);
+            g_assert_not_reached();
+        }
+        g_assert(nm_team_link_watcher_equal(watcher1, watcher));
+    }
+
+    return watcher;
+}
+#define _team_link_watcher_from_string(str, ...) \
+    _team_link_watcher_from_string_impl((str), NM_NARG(__VA_ARGS__), NM_MAKE_STRV(__VA_ARGS__))
+
+/*****************************************************************************/
+
+static void
+test_team_link_watcher_tofro_string(void)
+{
+    nm_auto_unref_team_link_watcher NMTeamLinkWatcher *w = NULL;
+
+#define _team_link_watcher_cmp(watcher,                                                   \
+                               name,                                                      \
+                               delay_down,                                                \
+                               delay_up,                                                  \
+                               init_wait,                                                 \
+                               interval,                                                  \
+                               missed_max,                                                \
+                               target_host,                                               \
+                               source_host,                                               \
+                               vlanid,                                                    \
+                               arping_flags)                                              \
+    G_STMT_START                                                                          \
+    {                                                                                     \
+        nm_auto_unref_team_link_watcher NMTeamLinkWatcher *_w = g_steal_pointer(watcher); \
+                                                                                          \
+        g_assert_cmpstr((name), ==, nm_team_link_watcher_get_name(_w));                   \
+        g_assert_cmpint((delay_down), ==, nm_team_link_watcher_get_delay_down(_w));       \
+        g_assert_cmpint((delay_up), ==, nm_team_link_watcher_get_delay_up(_w));           \
+        g_assert_cmpint((init_wait), ==, nm_team_link_watcher_get_init_wait(_w));         \
+        g_assert_cmpint((interval), ==, nm_team_link_watcher_get_interval(_w));           \
+        g_assert_cmpint((missed_max), ==, nm_team_link_watcher_get_missed_max(_w));       \
+        g_assert_cmpstr((target_host), ==, nm_team_link_watcher_get_target_host(_w));     \
+        g_assert_cmpstr((source_host), ==, nm_team_link_watcher_get_source_host(_w));     \
+        g_assert_cmpint((vlanid), ==, nm_team_link_watcher_get_vlanid(_w));               \
+        g_assert_cmpint((arping_flags), ==, nm_team_link_watcher_get_flags(_w));          \
+    }                                                                                     \
+    G_STMT_END
+
+    w = _team_link_watcher_from_string("name=ethtool",
+                                       "delay-up=0   name=ethtool",
+                                       "  delay-down=0   name=ethtool   ");
+    _team_link_watcher_cmp(&w,
+                           "ethtool",
+                           0,
+                           0,
+                           -1,
+                           -1,
+                           -1,
+                           NULL,
+                           NULL,
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+
+    w = _team_link_watcher_from_string("name=ethtool delay-up=10",
+                                       "   delay-down=0  delay-up=10   name=ethtool");
+    _team_link_watcher_cmp(&w,
+                           "ethtool",
+                           0,
+                           10,
+                           -1,
+                           -1,
+                           -1,
+                           NULL,
+                           NULL,
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+
+    w = _team_link_watcher_from_string("name=ethtool delay-up=10 delay-down=11",
+                                       "   delay-down=11  delay-up=10   name=ethtool");
+    _team_link_watcher_cmp(&w,
+                           "ethtool",
+                           11,
+                           10,
+                           -1,
+                           -1,
+                           -1,
+                           NULL,
+                           NULL,
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+
+    w = _team_link_watcher_from_string(
+        "name=nsna_ping target-host=xxx",
+        "name=nsna_ping target-host=xxx",
+        "  missed-max=3    target-host=xxx        name=nsna_ping   ");
+    _team_link_watcher_cmp(&w,
+                           "nsna_ping",
+                           -1,
+                           -1,
+                           0,
+                           0,
+                           3,
+                           "xxx",
+                           NULL,
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+
+    w = _team_link_watcher_from_string("name=arp_ping target-host=xxx source-host=yzd",
+                                       "  source-host=yzd target-host=xxx        name=arp_ping   ");
+    _team_link_watcher_cmp(&w,
+                           "arp_ping",
+                           -1,
+                           -1,
+                           0,
+                           0,
+                           3,
+                           "xxx",
+                           "yzd",
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+
+    w = _team_link_watcher_from_string(
+        "name=arp_ping missed-max=0 target-host=xxx vlanid=0 source-host=yzd");
+    _team_link_watcher_cmp(&w,
+                           "arp_ping",
+                           -1,
+                           -1,
+                           0,
+                           0,
+                           0,
+                           "xxx",
+                           "yzd",
+                           0,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+
+    w = _team_link_watcher_from_string(
+        "name=arp_ping target-host=xxx source-host=yzd validate-active=true",
+        "source-host=yzd send-always=false name=arp_ping validate-active=true "
+        "validate-inactive=false target-host=xxx",
+        "  source-host=yzd target-host=xxx   validate-active=true      name=arp_ping   ");
+    _team_link_watcher_cmp(&w,
+                           "arp_ping",
+                           -1,
+                           -1,
+                           0,
+                           0,
+                           3,
+                           "xxx",
+                           "yzd",
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_VALIDATE_ACTIVE);
+
+    w = _team_link_watcher_from_string(
+        "name=arp_ping target-host=xxx source-host=yzd validate-active=true validate-inactive=true "
+        "send-always=true",
+        "source-host=yzd send-always=true name=arp_ping validate-active=true "
+        "validate-inactive=true target-host=xxx",
+        "source-host=yzd send-always=true name=arp_ping validate-active=1 validate-inactive=yes "
+        "target-host=xxx",
+        "  source-host=yzd target-host=xxx   validate-inactive=true send-always=true    "
+        "validate-active=true      name=arp_ping   ");
+    _team_link_watcher_cmp(&w,
+                           "arp_ping",
+                           -1,
+                           -1,
+                           0,
+                           0,
+                           3,
+                           "xxx",
+                           "yzd",
+                           -1,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_VALIDATE_ACTIVE
+                               | NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_VALIDATE_INACTIVE
+                               | NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_SEND_ALWAYS);
+
+    w = _team_link_watcher_from_string(
+        "name=arp_ping missed-max=0 target-host=xxx vlanid=0 source-host=yzd");
+    _team_link_watcher_cmp(&w,
+                           "arp_ping",
+                           -1,
+                           -1,
+                           0,
+                           0,
+                           0,
+                           "xxx",
+                           "yzd",
+                           0,
+                           NM_TEAM_LINK_WATCHER_ARP_PING_FLAG_NONE);
+}
+
+/*****************************************************************************/
+
+NMTST_DEFINE();
+
+int
+main(int argc, char **argv)
+{
+    nmtst_init(&argc, &argv, TRUE);
+
+    g_test_add_func("/libnm-core-aux/test_team_link_watcher_tofro_string",
+                    test_team_link_watcher_tofro_string);
+
+    return g_test_run();
+}