Commit graph

402 commits

Author SHA1 Message Date
Thomas Haller d51be7e963 shared: use nm_secret_mem_try_realloc_take() in nm_utils_fd_get_contents() 2020-04-03 11:31:12 +02:00
Thomas Haller e9a2a85799 shared: add nm_secret_mem_realloc() helpers 2020-04-03 11:31:12 +02:00
Thomas Haller 5ab04919a2 shared: use G_UNLIKELY() macro for unlikely branch in nm_explicit_bzero() 2020-04-03 11:31:12 +02:00
Thomas Haller b6fdc30a88 shared: cleanup _get_hash_key_init() and better explain the reasoning
- add more code comments

- refactor the code flow in _get_hash_key_init() to follow a simpler
  code path.

- use c_siphash_hash() instead of 3 separate steps.

- Drop "?: static_seed" from nm_hash_static(). It's not useful, because
  the only _get_hash_key() for which _get_hash_key()^static_seed is zero
  is ~static_seed. That means, only one value of all the static seeds
  can result in zero here. At that point, we can just coerce that value
  to 3679500967u directly.
2020-04-03 11:26:49 +02:00
Thomas Haller 573b02f7d7 shared: add nm_pgbytes_hash()/nm_pgbytes_equal()
For hashing of a pointer to a GBytes*.

This is useful if your key is a GBytes array, and the
first field in your to be hashed struct.
2020-04-03 11:26:49 +02:00
Thomas Haller b1503d8a72 shared: add nm_hash_mem() helper 2020-04-03 11:26:49 +02:00
Thomas Haller 962ad7f850 shared: accept empty buffer for nm_hash_update()
There is no need to reject empty buffers. c_siphash_append() handles
them gracefully.
2020-04-03 11:26:49 +02:00
Thomas Haller 7e49f4a199 all: use wrappers for g_ascii_strtoll(), g_ascii_strtoull(), g_ascii_strtod()
Sometimes these function may set errno to unexpected values like EAGAIN.
This causes confusion. Avoid that by using our own wrappers that retry
in that case. For example, in rhbz#1797915 we have failures like:

    errno = 0;
    v = g_ascii_strtoll ("10", 0, &end);
    if (errno != 0)
        g_assert_not_reached ();

as g_ascii_strtoll() would return 10, but also set errno to EAGAIN.

Work around that by using wrapper functions that retry. This certainly
should be fixed in glib (or glibc), but the issues are severe enough to
warrant a workaround.

Note that our workarounds are very defensive. We only retry 2 times, if
we get an unexpected errno value. This is in the hope to recover from
a spurious EAGAIN. It won't recover from other errors.

https://bugzilla.redhat.com/show_bug.cgi?id=1797915
2020-04-01 17:18:37 +02:00
Thomas Haller 3b58c5fef4 shared: add nm_g_ascii_strtoull() to workaround bug 2020-04-01 17:18:01 +02:00
Thomas Haller 35a9f632a8 shared: add nm_g_ascii_strtod() to workaround bug 2020-04-01 17:18:01 +02:00
Thomas Haller f4446e34c6 shared: add nm_g_ascii_strtoll() to workaround bug 2020-04-01 17:18:00 +02:00
Thomas Haller 87b0d4cd7c shared: add assertions to _parse_legacy_addr4() and _nm_utils_ascii_str_to_int64()
Add more assertion for hunting down assertion failure at [1].

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1797915
2020-03-31 13:48:52 +02:00
Thomas Haller 46dd4d0fbf meson: merge branch 'inigomartinez/meson-license'
Add SPDX license headers for meson files.

As far as I can tell, according to RELICENSE.md file, almost everybody
who contributed to the meson files agreed to the LGPL-2.1+ licensing.
This entails the vast majority of code in question.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/397
2020-03-28 12:45:19 +01:00
Thomas Haller 6f9a478b7d tests: replace NMTST_SWAP() by new NM_SWAP() macro
NMTST_SWAP() used memcpy() for copying the value, while NM_SWAP() uses
a temporary variable with typeof(). I think the latter is preferable.

Also, the macro is essentially doing the same thing.
2020-03-26 21:28:56 +01:00
Thomas Haller cec06138f1 shared: add NM_SWAP() macro 2020-03-26 21:28:56 +01:00
Thomas Haller 999c569585 shared: add NM_ASCII_WHITESPACES macro
NM_ASCII_SPACES contains the ASCII characters according to
g_ascii_isspace().

Add NM_ASCII_WHITESPACES which differs from NM_ASCII_SPACES by not
including "\f". In some cases, that character shall be excluded.
For example, this is what systemd uses as "WHITESPACE" define at
various places.

Also, reorder the spaces string so that plain space comes first. It is
expected that ' ' is much more frequently than newlines or tabs. While
the order here shouldn't matter, it seems preferably to order frequent
characters in front.
2020-03-24 20:43:55 +01:00
Thomas Haller 0f15d5aa40 shared: allow nm_dbus_connection_call_finish_variant_cb() with D-Bus methods that have no return value 2020-03-23 09:32:04 +01:00
Thomas Haller d3e5eab734 shared: add nm_g_variant_is_of_type() helper 2020-03-17 08:02:54 +01:00
Thomas Haller 4087024a9b shared: move assertion in _NM_UTILS_STRING_TABLE_LOOKUP_DEFINE()
Move the assertion for valid LIST first. It only checks static data,
and regardless of the entry_cmd, it should be done first.

Fixes: f4d12f7b59 ('shared: add NM_UTILS_STRING_TABLE_LOOKUP_STRUCT_DEFINE() macro for lookup of structs')
2020-03-02 16:20:50 +01:00
Thomas Haller 8b63fd8cc0 shared: fix handling %NULL argument in nm_ref_string_equals_str()
Fixes: 190a8ed425 ('shared: add nm_ref_string_equals_str() helper')
2020-03-02 15:49:41 +01:00
Thomas Haller 294de8487e shared: add NMU_IFACE_OVS_OR_KERNEL for nm_utils_ifname_valid()
Depending on the type, OVS interfaces also have a corresponding netdev
in kernel (e.g. type "internal" does, type "patch" does not).

Such a case is neither NMU_IFACE_OVS nor NMU_IFACE_KERNEL (alone). There should
be a special type to represent those cases.

Add NMU_IFACE_OVS_OR_KERNEL for that.
2020-02-26 17:51:14 +01:00
Thomas Haller 807cddc754 shared: add NMU_IFACE_ANY for nm_utils_ifname_valid()
nm_utils_ifname_valid() is to validate "connection.interface-name"
property. But the exact validation depends on the connection type.

Add "NMU_IFACE_ANY" to validate the name to check whether it would be
valid for any connection type.

This is for completeness and for places where the caller might not know
the connection type.
2020-02-26 17:51:14 +01:00
Thomas Haller d4d9e9e7bb shared: reject reserved names from "connection.interface-name"
"all" and "default" never works.

"bonding_masters" works if you unload the bonding module. Well,
that should not really be called working...

Reject these names.
2020-02-26 17:51:13 +01:00
Thomas Haller ef567805bb shared: reject '%' from nm_utils_ifname_valid() for kernel names
Generally, it's dangerous to reject values that were accepted
previously. This will lead to NetworkManager being unable to load
a profile from disk, which was loadable previously.

On the other hand, kernel would not have treated this setting as
it was intended. So, I would argue that the such a setting was not
working (as intended) anyway.

We can only hope that users don't configure arbitrary interface names.
It generally isn't a good idea to do, so "breaking" such things is less
of a concern.
2020-02-26 17:51:13 +01:00
Thomas Haller 9f2014ac64 shared: fix suffix for NM_GOBJECT_PROPERTIES_DEFINE_BASE*() for multiple definitions
To really use multiple NM_GOBJECT_PROPERTIES_DEFINE_BASE*() defines in
the same source file, several fixes to the suffix handling are
necessary. This fixes commit f13c7e3bbd ('shared: extend
NM_GOBJECT_PROPERTIES_DEFINE*() macros to append suffix to defined
names') to really work.

Fixes: f13c7e3bbd ('shared: extend NM_GOBJECT_PROPERTIES_DEFINE*() macros to append suffix to defined names')
2020-02-24 13:13:34 +01:00
Beniamino Galvani cf6940665d shared: add more debug messages to nm_utils_parse_inaddr_bin_full()
Log the reason for the assertion failure to debug the crash in [1].

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1797915
2020-02-21 16:54:53 +01:00
Thomas Haller f13c7e3bbd shared: extend NM_GOBJECT_PROPERTIES_DEFINE*() macros to append suffix to defined names
This way, we will be able to use the macro multiple times in the same
source file by using different suffixes.
2020-02-21 15:59:44 +01:00
Thomas Haller cba938f3ee shared: add nm_utils_named_values_from_str_dict_full() to allow different sort order (or none) 2020-02-19 16:24:55 +01:00
Thomas Haller f4d12f7b59 shared: add NM_UTILS_STRING_TABLE_LOOKUP_STRUCT_DEFINE() macro for lookup of structs 2020-02-19 15:35:07 +01:00
Thomas Haller 14461e7217 shared: add NM_MORE_ASSERT_ONCE() macro 2020-02-19 15:35:07 +01:00
Thomas Haller eb74d5f65f shared/trivial: add code comment to nm_utils_ifname_valid_kernel() 2020-02-18 13:23:50 +01:00
Thomas Haller 82fb8b00b4 shared: check for valid UTF-8 in nm_utils_ifname_valid()
The interface name might come from the command line or from a filename
(like during nm_vpn_wireguard_import()). It's not clear that this
is valid UTF-8. Asserting against that requires the caller to ensure
that the encoding is valid. That is cumbersome, especially since we anyway
check. Just report a regular error.
2020-02-18 13:23:50 +01:00
Antonio Cardace ed5a647ad1 nm-shared-utils: relax ovs ifname check to accept any (non-space) ASCII printable char
quoting 'man ovs-vswitchd.conf.db':
"The name must be alphanumeric and must not contain forward or backward
slashes."

OVS actually accepts a wider range of chars (all printable UTF-8 chars),
NetworkManager restricts this to ASCII char as it's a safer option for
now since OVS is not well documented on this matter.

https://bugzilla.redhat.com/show_bug.cgi?id=1788432

Fixes: e7d72a14f6 ('libnm-core: use different ifname validation function for OVS bridges, ports and interfaces')
2020-02-18 13:11:21 +01:00
Thomas Haller 037507f790 shared: fix returning out_len from nm_utils_hash_values_to_array()
The only affected caller is nm_modem_manager_get_modems(), which
is used by NMDeviceBt for DUN connections. This is rather bad.

Fixes: 4154d9618c ('bluetooth: refactor BlueZ handling and let NMBluezManager cache ObjectManager data')
Fixes: e688e70b37 ('shared: add nm_utils_hash_values_to_array() helper')
2020-02-17 17:54:10 +01:00
Antonio Cardace 550f538564 nm-shared-utils: add nm_utils_ifname_valid*() to shared utils
Move the body of nm_utils_is_valid_iface_name() to
nm_utils_ifname_valid_kernel() so that it's shared between NM and
clients.
2020-02-17 15:27:35 +01:00
Thomas Haller eae862e385 shared: add LOGD_DHCP_from_addr_family() helper 2020-02-17 14:45:09 +01:00
Iñigo Martínez 648155e4a1 license: Add license using SPDX identifiers to meson build files
License is missing in meson build files. This has been added using
SPDX identifiers and licensed under LGPL-2.1+.
2020-02-17 13:16:57 +01:00
Thomas Haller fee50e17b2 license: relicense "shared/nm-glib-aux/nm-jansson.h" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/nm-glib-aux/nm-jansson.h shared/nm-utils/nm-jansson.h
       7    Thomas Haller
       1    Lubomir Rintel
       1    Beniamino Galvani
       1    Francesco Giudici

All contributors agreed to the relicensing according to "RELICENSE.md".
2020-02-16 15:41:40 +01:00
Thomas Haller e70ce4042c license: relicense "shared/nm-glib-aux/nm-glib.h" under LGPL-2.1+
$ git shortlog -n -s a3e75f3294 -- shared/nm-glib-aux/nm-glib.h shared/nm-utils/nm-glib.h shared/nm-glib.h include/nm-glib.h include/nm-glib-compat.h
      35    Thomas Haller
      12    Dan Winship
       5    Dan Williams
       3    Lubomir Rintel
       2    Beniamino Galvani
       1    Jan Kantert
       1    Thomas Bechtold

The last two contributions by Thomas and Jan are no longer present in any
form, because they were for an older version of glib which is no longer
supported. All other contributors agree to the licences change according
to "RELICENSE.md" file.
2020-02-16 15:41:36 +01:00
Thomas Haller d36df06c24 shared: don't require callback argument to nm_dbus_connection_call_set()
It's fine to call propery-set without caring about the result.
2020-02-14 09:48:37 +01:00
Thomas Haller 829f8e8314 shared: drop compat implementation for G_DEFINE_QUARK()
For one, we by now require glib >= 2.34.0, so this is not used.

Also, I think G_DEFINE_QUARK() is rather ugly because it constructs
the defined function name (so you cannot grep for it). We should use
NM_CACHED_QUARK_FCN() instead.
2020-02-13 17:17:07 +01:00
Thomas Haller cd31437024 shared: drop _STATIC variant of macros that define functions
Several macros are used to define function. They had a "_STATIC" variant,
to define the function as static.

I think those macros should not try to abstract entirely what they do.
They should not accept the function scope as argument (or have two
variants per scope). This also because it might make sense to add
additional __attribute__(()) to the function. That only works, if
the macro does not pretend to *not* define a plain function.

Instead, embrace what the function does and let the users place the
function scope as they see fit.

This also follows what is already done with

    static NM_CACHED_QUARK_FCN ("autoconnect-root", autoconnect_root_quark)
2020-02-13 17:17:07 +01:00
Thomas Haller 17d9b852c8 shared: explicitly implement binary search in NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*()
When looking at nm_utils_array_find_binary_search(), we see that binary
search really isn't complicated. In nm_utils_array_find_binary_search()
it looks complicated, because that is our general purpose function which
accepts arbitrary lists, uses an opaque compare function, accepts a user
data argument, and returns the insertion position.

This is unnecessary for the narrow purpose in NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*().
When we inline the binary search, it can be simplified, and the remaining
parts is simple enough to prefer this duplication of binary search over
using our general purpose function.

Also, this gives the compiler more chance for optimization. For
example, we can use "unsigned" as index type instead of gssize, because
we know (at compile time), that this type will always be large enough
for our LIST. Also, we can directly call strcmp().

The result is that the macro's implementation is also fast in the best
case (where the needle is found with only one strcmp()) and in the cases
where there is a small number of items to search.
It thus alleviates concerns that using the macro might be slower than
an optimized implementation.

The binary size of the defined function increases slightly (from 112
bytes to 192 bytes, on x86_64, GCC, -O2). But according to my tests it
is slightly and measurably faster.
2020-02-13 14:49:45 +01:00
Thomas Haller 760551e3fc shared/tests: add test for NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*() 2020-02-13 14:20:26 +01:00
Thomas Haller 487141d4a8 shared: add entry_cmd argument to NM_UTILS_STRING_TABLE_LOOKUP_DEFINE*() macro
This extra argument allows to tweak whether to assert for the input argument name.
2020-02-13 10:46:34 +01:00
Thomas Haller 190a8ed425 shared: add nm_ref_string_equals_str() helper 2020-02-10 19:11:50 +01:00
Thomas Haller 333e6f66fb shared: rename local variable in _NM_GET_PRIVATE_PTR() macro
The _NM_GET_PRIVATE_PTR() macro is possibly used from other macros. And
"_self" is a pretty good name to use. Don't let the lower layer macro
use this name.
2020-02-10 19:11:50 +01:00
Thomas Haller 8c23586a77 shared: drop nm_utils_dbus_normalize_object_path() in favor of nm_dbus_path_not_empty()
They do the same thing. Unify and drop one.
2020-02-10 19:11:50 +01:00
Thomas Haller c7e6573eb4 shared: add nm_g_variant_lookup() and nm_g_variant_lookup_value() helpers
It's often convenient to accept %NULL as dictionary argument.
2020-02-10 19:11:50 +01:00
Thomas Haller 455cec9986 shared: add nm_utils_strdup_reset() helper 2020-02-10 19:11:50 +01:00
Thomas Haller 45a832ac50 shared: add NM_FLAGS_ASSIGN_MASK() helper 2020-02-10 19:11:50 +01:00
Thomas Haller 5528086ec5 shared: add nm_dbus_connection_call_set() util 2020-02-10 19:11:50 +01:00
Thomas Haller 5ce7ea38ff shared: add NM_PRINT_FMT_QUOTE_REF_STRING() macro 2020-02-10 19:11:50 +01:00
Thomas Haller f4fa003434 shared: add NM_UTILS_STRING_TABLE_LOOKUP_DEFINE() macro to simplify defining string lookup functions 2020-02-10 19:11:50 +01:00
Thomas Haller 9153e7912f shared: add nm_utils_get_monotonic_timestamp_msec_cached() helper 2020-02-10 19:11:50 +01:00
Thomas Haller 8b63b229dd shared/trivial: rename time related functions to use "nsec"/"msec" abbreviation instead of "ns"/"ms"
The "ns" abbreviation doesn't look too nice. We mostly use "nsec" at other
places. Rename.
2020-02-10 19:11:50 +01:00
Thomas Haller 53f6858a27 all: add nm_utils_error_is_cancelled() and nm_utils_error_is_cancelled_or_disposing()
Most callers would pass FALSE to nm_utils_error_is_cancelled(). That's
not very useful. Split the two functions and have nm_utils_error_is_cancelled()
and nm_utils_error_is_cancelled_is_disposing().
2020-02-10 19:11:50 +01:00
Thomas Haller 90c5d1d99c shared: add debugging statement for assertion in nm_utils_parse_inaddr_bin_full()
There are crashes where this assertion fails, but it's not clear
how that could happen (because the input text seems a usual IPv4 address).

Try to collect some more information about what failed. It's only
enabled with NM_MORE_ASSERTS anyway.

Related: https://bugzilla.redhat.com/show_bug.cgi?id=1797915
2020-02-10 16:44:17 +01:00
Beniamino Galvani 7f801685b2 shared: redefine G_SOURCE_FUNC
G_SOURCE_FUNC has attribute GLIB_AVAILABLE_MACRO_IN_2_58, which means
that the compiler will emit a warning when GLIB_VERSION_MAX_ALLOWED <
GLIB_VERSION_2_58. We currently define GLIB_VERSION_MAX_ALLOWED as
GLIB_VERSION_2_40. Redefine the macro to fix the following build error
when using glib >= 2.63.5 (the version in which the attribute was
added):

  CC       shared/nm-glib-aux/libnm_glib_aux_la-nm-shared-utils.lo
 shared/nm-glib-aux/nm-shared-utils.c: In function ‘nm_g_unix_fd_source_new’:
 shared/nm-glib-aux/nm-shared-utils.c:3679:13: error: Not available before  [-Werror]
  3679 |  g_source_set_callback (source, G_SOURCE_FUNC (source_func), user_data, destroy_notify);

Fixes: 9c5741ccd2 ('shared/nm-glib: add compat implementation for G_SOURCE_FUNC()')
2020-02-05 14:31:46 +01:00
Thomas Haller 228519fe79 shared: add nm_ip_addr_cmp()/nm_ip_addr_equal() helpers 2020-01-28 11:43:22 +01:00
Thomas Haller b9c5c07c4d shared: add NM_IP_ADDR_ZERO macro for initializing NMIPAddr to zero 2020-01-28 11:17:41 +01:00
Thomas Haller cdfbbe651b shared: add nm_utils_ipaddr_is_valid()/nm_utils_ipaddr_is_normalized() helpers 2020-01-28 11:17:41 +01:00
Thomas Haller 0f9664f417 shared: use static array indices in function parameter declarations of _nm_utils_inet[46]_ntop()
This should give the compiler more possibilities to warn about wrong
use of the API.

In practice, my current compiler wouldn't flag any issues. However,
some compilers (or compile options) might.
2020-01-28 11:17:41 +01:00
Thomas Haller c0bd6752b9 shared: move nm_utils_inet* helpers from libnm-core to shared 2020-01-28 11:17:41 +01:00
Thomas Haller 6e7e18c86f shared: add nm_g_main_context_is_thread_default() util 2020-01-28 10:54:14 +01:00
Thomas Haller 24b50625bd shared: add NM_UTILS_USEC_PER_SEC macro 2020-01-28 10:54:14 +01:00
Thomas Haller 0cb0ee0482 shared: add _NM_MACRO_COMMA_IF_ARGS() helper macro 2020-01-28 10:42:08 +01:00
Thomas Haller 9b8dcc2665 shared: cleanup NM_NARG() and related macros
- add new helper macros NM_MACRO_CALL(), _NM_MACRO_SELECT_ARG_64(),
  NM_NARG_MAX1(), NM_NARG_MAX2().

- cleanup implementation of NM_NARG() and _NM_UTILS_MACRO_REST() macros
  by reusing helper macros.
2020-01-28 10:42:08 +01:00
Thomas Haller a2fd2ab55d shared: remove nm_dbus_connection_signal_subscribe_object_manager() helper
It seems to complicate things more than helping. Drop it. What we still have
is a wrapper around plain g_dbus_connection_signal_subscribe(). That one is
trivial and helpful. The previous wrapper seems to add more complexity.
2020-01-16 12:42:41 +01:00
Thomas Haller 1afabd5ffd shared: add nm_source_func_unref_gobject() helper 2020-01-16 12:42:41 +01:00
Thomas Haller 421256073b shared: add nm_g_unix_fd_source_new() helper 2020-01-13 15:46:04 +01:00
Thomas Haller d63cd26e60 shared: improve nm_free_secret() to clear entire memory buffer
The purpose is to clear the entire available buffer, not only
up to the first '\0'. This is done, because otherwise we might
leak sensitive data that happens to be after the first '\0',
or we might give away the length of the secrets.

Of course, those are very (very) minor concerns. But avoiding them is
easy enough.
2020-01-09 17:45:01 +01:00
Thomas Haller cb7cfc3f12 shared: don't allow NULL arguments with g_hash_table_steal_extended() compat implementation
We cannot know the key/value free functions, hence, our compat implementation
cannot free the values if they are not requested. The "solution" is to require
the caller to fetch all values, always.
2019-12-31 02:13:45 +01:00
Antonio Cardace c0f1a657c3 shared: add io-util to read data from a fd into a GString 2019-12-24 10:13:51 +01:00
Thomas Haller 27d51c3250 shared/glib: add compat implementation for g_hash_table_steal_extended() 2019-12-21 12:30:37 +01:00
Beniamino Galvani 0de4fd6ebc shared: add missing va_end() to _nm_dbus_error_is()
Found by covscan:

   NetworkManager-1.22.0/shared/nm-glib-aux/nm-dbus-aux.c:361:
   missing_va_end: va_end was not called for "ap".

Fixes: ce36494c0a ('shared: add nm_dbus_error_is() helper')
2019-12-18 09:54:08 +01:00
Thomas Haller 1b00fd2fd2 shared: implement _LOGx() macros using log levels that are themself defines
"nm-glib-aux/nm-logging-fwd.h" provides macros like _LOGD() to be reused
by various parts which implement logging (by defining _NMLOG() accordingly).

libnm also has logging, however it uses different logging levels
aside LOGD_DEBUG.

Instead, implement _LOGD() using a define _LOGL_DEBUG, so that libnm can
redefine thos _LOGL_DEBUG defines and use the _LOGD() macro.
2019-12-16 18:37:47 +01:00
Thomas Haller ce36494c0a shared: add nm_dbus_error_is() helper 2019-12-16 18:16:12 +01:00
Thomas Haller bd9b253540 all: rename time related function to spell out nsec/usec/msec/sec
The abbreviations "ns" and "ms" seem not very clear to me. Spell them
out to nsec/msec. Also, in parts we already used the longer abbreviations,
so it wasn't consistent.
2019-12-13 16:54:40 +01:00
Thomas Haller f5b0713651 shared/glib: reimplement g_atomic_pointer_compare_and_exchange() macro
With glib 2.63.2 and clang 9.0.0 (Fedora 32) we get compile errors:

  ../clients/cloud-setup/nmcs-provider-ec2.c:51:8: error: incompatible pointer types passing 'typeof ((((void *)0))) *' (aka 'void **') to parameter of type 'const char **' [-Werror,-Wincompatible-pointer-types]
                  if (!g_atomic_pointer_compare_and_exchange (&base_cached, NULL, base))
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/include/glib-2.0/glib/gatomic.h:192:44: note: expanded from macro 'g_atomic_pointer_compare_and_exchange'
      __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
                                             ^~~~~~~~~~~~~~
  ../src/devices/bluetooth/nm-bluez-manager.c:2836:2: error: incompatible pointer types passing 'typeof ((((void *)0))) *' (aka 'void **') to parameter of type 'const NMBtVTableNetworkServer **' (aka 'const struct _NMBtVTableNetworkServer **') [-Werror,-Wincompatible-pointer-types]
          g_atomic_pointer_compare_and_exchange (&nm_bt_vtable_network_server, NULL, &priv->vtable_network_server);
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/include/glib-2.0/glib/gatomic.h:192:44: note: expanded from macro 'g_atomic_pointer_compare_and_exchange'
      __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
                                             ^~~~~~~~~~~~~~
  ../src/devices/bluetooth/nm-bluez-manager.c:2853:2: error: passing 'typeof ((&priv->vtable_network_server)) *' (aka 'struct _NMBtVTableNetworkServer **') to parameter of type 'const NMBtVTableNetworkServer **' (aka 'const struct _NMBtVTableNetworkServer **') discards qualifiers in nested pointer types [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
          g_atomic_pointer_compare_and_exchange (&nm_bt_vtable_network_server, &priv->vtable_network_server, NULL);
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/include/glib-2.0/glib/gatomic.h:192:44: note: expanded from macro 'g_atomic_pointer_compare_and_exchange'
      __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
                                             ^~~~~~~~~~~~~~
  ../src/devices/nm-device.c:8857:8: error: incompatible pointer types passing 'typeof ((((void *)0))) *' (aka 'void **') to parameter of type 'GBytes **' (aka 'struct _GBytes **') [-Werror,-Wincompatible-pointer-types]
                  if (!g_atomic_pointer_compare_and_exchange (&global_duid, NULL, p)) {
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/include/glib-2.0/glib/gatomic.h:192:44: note: expanded from macro 'g_atomic_pointer_compare_and_exchange'
      __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
                                             ^~~~~~~~~~~~~~

The issue happens because glib passes the "atomic" argument to

  __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)

without cast, and clang 9 seems to be picky about const pointers.
Add our own version of the macro that does better casts while also having
better compile time checks for valid arguments.
2019-12-13 13:46:37 +01:00
Thomas Haller 44c5331e29 shared: move "shared/nm-utils/tests/test-shared-general" to "shared/nm-glib-aux/tests"
"shared/nm-utils" got long renamed and split into separate parts. The remaining
tests are really to test nm-std-aux and nm-glib-aux (no libnm dependencies). Move
the tests to the appropriate place.
2019-12-10 09:17:17 +01:00
Thomas Haller 8fbf67d138 shared: add nm_utils_parse_inaddr_bin_full() to support legacy IPv4 formats as inet_aton()
inet_aton() also supports IPv4 addresses in octal (with a leading '0')
or where not all 4 digits of the address are present.

Add nm_utils_parse_inaddr_bin_full() to optionally fallback to
parse the address with inet_aton().

Note taht inet_aton() also supports all crazy formats, including
ignoring trailing garbage after a whitespace. We don't want to accept
that in general.

Note that even in legacy format we:

  - accept everything that inet_pton() would accept

  - additionally, we also accept some forms which inet_aton() would
    accept, but not all.

That means, the legacy format that we accept is a superset of
inet_pton() and a subset of inet_aton(). Which is desirable.
2019-12-05 12:36:13 +01:00
Thomas Haller 06a976358b shared: add nm_utils_addr_family_from_size() helper 2019-12-05 12:36:13 +01:00
Thomas Haller 2b6f5a305c shared: add nm_utils_error_new() and nm_utils_error_new_cancelled() helper 2019-11-28 19:20:33 +01:00
Thomas Haller 40012e2aa8 shared: move log level info from core to "nm-logging-base.h"
We have our NM specific logging and log levels. Maybe we should
not have that, and instead only rely on syslog (like systemd)
or glog(). Anyway, currently we have one way and it makes sense
that this is also used outside from "src".

Move the helper function to parse log levels from string to
"nm-logging-base.h" so that we can use the same logging levels
outside of core.

This moves code that is currently GPL2+ licensed to
LGPL2.1+. However as far as I see, this code was entirely written
by Red Hat employees who would not object with this change. Also,
it's as obvious and trivial as it gets.
2019-11-28 19:20:33 +01:00
Thomas Haller 32d3a3f7ef shared: cleanup include guard for nm-logging-fwd.h 2019-11-28 19:20:33 +01:00
Thomas Haller 41d81e6893 shared/logging: add "nm-logging-base.h"
We have "nm-logging-fwd.h", which (as the name implies) is header-only.
Add instead a "nm-logging-base.c", which also contains implementation for
logging functions that are not only useful under "src/nm-logging.c"
2019-11-28 19:20:33 +01:00
Thomas Haller 4fad8c7c64 shared: add nm_utils_g_main_context_create_integrate_source() for integrating a GMainContext in another
We will rework NMClient entirely. Then, the synchronous initialization will also use
the asynchronous code paths. The difference will be that with synchronous initialization,
all D-Bus interaction will be done with an internal GMainContext as current thread default,
and that internal context will run until initialization completes.

Note that even after initialization completes, it cannot be swapped back to the user's
(outer) GMainContext. That is because contexts are essentially the queue for our
D-Bus events, and we cannot swap from one queue to the other in a race
free manner (or a full resync). In other words, the two contexts are not in sync,
so after using the internal context NMClient needs to stick to that (at least, until
the name owner gets lost, which gives an opportunity to resync and switch back to the
user's main context).

We thus need to hook the internal (inner) GMainContext with the user's (outer) context,
so when the user iterates the outer context, events on the inner context get dispatched.

Add nm_utils_g_main_context_create_integrate_source() to create such a GSource for
integrating two contexts.

Note that the use-case here is limited: the integrated, inner main context must
not be explicitly iterated except from being dispatched by the integrating
source. Otherwise, you'd get recursive runs, possible deadlocks and general
ugliness. NMClient must show restrain how to use the inner context while it is
integrated.
2019-11-25 12:58:33 +01:00
Thomas Haller 45c9f3c39b shared: make NM_STRUCT_OFFSET_ENSURE_TYPE() work with arrays
Some compilers don't convert arrays as _Generic() type selectors to
their pointer type. That means, for those compilers the generic type
would be an array and not a pointer. Work around that by adding zero
to the pointer/array argument.

Also, I cannot get this to work with "clang-3.4.2-9.el7". Disable it
for that compiler. The value of the generic check is anyway that it only
needs to work with some compiler combinations. That will trigger a
compilation failure and we can fix the implementation also for compilers
that don't support the macro.

See-also: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1930.htm
2019-11-25 12:58:33 +01:00
Thomas Haller b8b8ef7600 shared: mark _notify() function as "_nm_unused"
There are two macros: NM_GOBJECT_PROPERTIES_DEFINE_BASE() and
NM_GOBJECT_PROPERTIES_DEFINE(). The former just defines the
property enums and the obj_properties array. The latter also
defines the functions _notify() and _nm_gobject_notify_together_impl().

That means, depending on whether you actually use _notify(), you have
to choose one of the macros. I think that is unnecessarily cumbersome.
Let's mark the function as _nm_unused so that the compiler doesn't
complain about the unused function. I don't think it's a problem
to use NM_GOBJECT_PROPERTIES_DEFINE() even if you don't actually use
_notify().
2019-11-25 12:58:33 +01:00
Thomas Haller 58811e019d shared: add nm_auto_unref_gmaincontext macro 2019-11-25 12:58:33 +01:00
Thomas Haller bc9baf0fd5 shared: add nm_pint_hash()/nm_pint_equals() utils 2019-11-25 12:58:33 +01:00
Thomas Haller 033c2b82c2 core: move _LOG*() macros to "shared/nm-glib-aux/nm-logging-fwd.h"
We preferably should use our convenience macros like _LOGD().
Since those macros expand to _NMLOG() (which needs to be defined
separately), we can move it to "nm-logging-fwd.h" and reuse.
2019-11-22 15:32:52 +01:00
Thomas Haller ec868916c8 shared: move nm_utils_ip._address_clear_host_address() helpers to shared 2019-11-22 15:32:52 +01:00
Thomas Haller 2ef5014f98 shared: add nm_clear_g_source_inst()
glib really likes the numeric source IDs. That is, g_idle_add(), g_timeout_add(),
etc. return those IDs, that can then be destroyed with g_remove_source() (or
nm_clear_g_source()).

I think these numeric IDs are really not great.

- API like g_idle_add() and g_remove_source() only works with the g_main_context_get_default()
  instance. That means, you cannot use this API for any other contexts. If you'd insist on using
  numeric IDs, you'd need to call g_main_context_find_source_by_id() on the right context
  first (but you'd also have to track the context alongside the ID).
- g_remove_source() requires first a call to g_main_context_find_source_by_id(). This involves
  taking a mutex and doing an extra hash lookup.

Instead, it often seems preferable to use the GSource instance directly. It works
with any context, it can be referenced and unreferenced, and it can be destroyed, and
avoids the overhead of g_main_context_find_source_by_id().

The only downside really is that keeping a GSource pointer takes one pointer size, while
the guint source ID is usually only 4 bytes.

Anyway, I think we should deal more with GSource instances directly. Hence, add this
convenience macro, that works like nm_clear_g_source().
2019-11-22 15:32:52 +01:00
Thomas Haller c40ff42ae6 shared: add nm_g_*_source_new() and nm_g_source_attach() helpers
Small utilities to make is more convenient to create and attach GSource
instances.
2019-11-22 15:32:52 +01:00
Thomas Haller 9c5741ccd2 shared/nm-glib: add compat implementation for G_SOURCE_FUNC()
G_SOURCE_FUNC() was only added in glib 2.58.
2019-11-22 15:32:52 +01:00
Beniamino Galvani 97a8785148 nettools: enable logging 2019-11-22 10:24:49 +01:00
Thomas Haller 42026f9fb3 shared: add nm_utils_monotonic_timestamp_from_boottime() util
We sometimes have a CLOCK_BOOTTIME and need to convert it
to NetworkManager's monotonic timestamps.
2019-11-20 14:54:39 +01:00
Thomas Haller afa54fdfd5 shared: add nm_g_main_context_push_thread_default*() and nm_auto helper 2019-11-07 11:34:36 +01:00
Thomas Haller 3b95905ae3 shared: add nm_auto_destroy_and_unref_gsource macro 2019-11-07 11:34:36 +01:00
Thomas Haller 01335ecec6 shared: assert in nm_ref_string_unref() for valid NMRefString instance 2019-11-07 11:34:36 +01:00
Thomas Haller 15fe8ad851 shared: add nm_dbus_path_not_empty() helper 2019-11-07 11:34:36 +01:00
Thomas Haller 9b2d5742c1 shared: add nm_g_set_error_take*() util 2019-11-07 11:34:36 +01:00
Thomas Haller d21439eaa0 shared: add nm_ppdirect_hash()/nm_ppdirect_equal() helpers
Useful for hashing pointers to a pointer to a pointer, and
compare the resulting pointer directly. This will be actually
used.
2019-11-07 11:34:36 +01:00
Thomas Haller 86352ceaf8 shared: add NM_STRUCT_OFFSET_ENSURE_TYPE() helper 2019-11-07 11:34:36 +01:00
Thomas Haller 1c4acc89f1 shared: add NM_IS_REF_STRING() helper 2019-10-27 14:30:51 +01:00
Thomas Haller a75dccad78 shared: add @deep_copied argument to nm_utils_strv_dup() 2019-10-27 14:30:51 +01:00
Thomas Haller 69de5ee4e9 shared: move nm_utils_parse_debug_string() from core to shared 2019-10-18 22:09:18 +02:00
Thomas Haller 308beb85fe shared: add nm_c_list_for_each_entry_prev() helper macro
It's non-trivial to get this right. Add the macro for iterating the list
in reverse.
2019-10-18 22:09:18 +02:00
Thomas Haller c02710bb0f shared: add nm_g_source_destroy_and_unref() helper 2019-10-18 22:09:18 +02:00
Thomas Haller 03633fef42 shared: add nm_dbus_connection_call_finish_*_cb() helpers 2019-10-16 08:56:00 +02:00
Thomas Haller 9059b49002 shared: add nm_g_task_new() and nm_g_task_is_valid() helper
Note that we should always set the source-tag of our GTask.
This allows us to better assert that the user uses the right
_finish() method for the task.

The plain g_task_new() does not have a souce-tag argument. Hence, we would
always need to explicitly call g_task_set_source_tag().

Likewise, to check the source tag, we would always need to write

  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
  g_return_val_if_fail (g_async_result_is_tagged (result, tag), FALSE);

Actually, g_async_result_is_tagged() uses the GAsyncResultIface to
call iface->is_tagged(). This has unnecessary overhead, so we should
just call g_task_get_source_tag() directly.

Add helper functions for that.
2019-10-16 08:56:00 +02:00
Thomas Haller 90487ef509 shared: allow "NMRefString" typedef to be forward declared by naming the struct 2019-10-16 08:56:00 +02:00
Thomas Haller 5ed917a8c1 shared: avoid extra asserts in production code for NMRefString
These asserts were always intended as for extra debugging mode.
Don't enable them in production code.

Fixes: 908fadec96 ('shared: add NMRefString')
2019-10-16 08:53:33 +02:00
Thomas Haller 61ccdf1710 shared: fix crash in nm_ref_string_new_len()
Obvious bug, apparently untested so far :)
No worry, this code will get in use soon.

Fixes: 908fadec96 ('shared: add NMRefString')
2019-10-16 08:37:02 +02:00
Thomas Haller 3b69f02164 all: unify format of our Copyright source code comments
```bash

readarray -d '' FILES < <(
  git ls-files -z \
    ':(exclude)po' \
    ':(exclude)shared/c-rbtree' \
    ':(exclude)shared/c-list' \
    ':(exclude)shared/c-siphash' \
    ':(exclude)shared/c-stdaux' \
    ':(exclude)shared/n-acd' \
    ':(exclude)shared/n-dhcp4' \
    ':(exclude)src/systemd/src' \
    ':(exclude)shared/systemd/src' \
    ':(exclude)m4' \
    ':(exclude)COPYING*'
  )

sed \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[-–] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C1pyright#\5 - \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[,] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C2pyright#\5, \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C3pyright#\5#\7/' \
  -e 's/^Copyright \(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/C4pyright#\1#\3/' \
  -i \
  "${FILES[@]}"

echo ">>> untouched Copyright lines"
git grep Copyright "${FILES[@]}"

echo ">>> Copyright lines with unusual extra"
git grep '\<C[0-9]pyright#' "${FILES[@]}" | grep -i reserved

sed \
  -e 's/\<C[0-9]pyright#\([^#]*\)#\(.*\)$/Copyright (C) \1 \2/' \
  -i \
  "${FILES[@]}"

```

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/298
2019-10-02 17:03:52 +02:00
Thomas Haller abff46cacf all: manually drop code comments with file description 2019-10-01 07:50:52 +02:00
Thomas Haller df6714102c shared: add NM_G_VARIANT_TYPE() macro
Like G_VARIANT_TYPE(), but this one can be used to initialize a static variable.
2019-09-30 08:23:19 +02:00
Thomas Haller efa51ba9a2 shared: add nm_utils_g_slist_strlist_join() util 2019-09-26 19:33:05 +02:00
Thomas Haller a780b04837 dns/dnsmasq: refactor tracking of dnsmasq process
Several points.

- We spawn the dnsmasq process directly. That has several downsides:

  - The lifetime of the process is tied to NetworkManager's. When
    stopping NetworkManager, we usually also stop dnsmasq. Or we keep
    the process running, but later the process is no longer a child process
    of NetworkManager and we can only kill it using the pidfile.

  - We don't do special sandboxing of the dnsmasq process.

- Note that we want to ensure that only one dnsmasq process is running
at any time. We should track that in a singletone. Note that NMDnsDnsmasq
is not a singleton. While there is only one instance active at any time,
the DNS plugin can be swapped (e.g. during SIGHUP). Hence, don't track the
process per-NMDnsDnsmasq instance, but in a global variable "gl_pid".

- Usually, when NetworkManager quits, it also stops the dnsmasq process.
Previously, we would always try to terminate the process based on the
pidfile. That is wrong. Most of the time, NetworkManager spawned the
process itself, as a child process. Hence, the PID is known and NetworkManager
will get a signal when dnsmasq exits. The only moment when NetworkManager should
use the pidfile, is the first time when checking to kill the previous instance.
That is: only once at the beginning, to kill instances that were
intentionally or unintentionally (crash) left running earlier.
This is now done by _gl_pid_kill_external().

- Previously, before starting a new dnsmasq instance we would kill a
possibly already running one, and block while waiting for the process to
disappear. We should never block. Especially, since we afterwards start
the process also in non-blocking way, there is no reason to kill the
existing process in a blocking way. For the most part, starting dnsmasq
is already asynchronous and so should be the killing of the dnsmasq
process.

- Drop GDBusProxy and only use GDBusConnection. It fully suffices.

- When we kill a dnsmasq instance, we actually don't have to wait at
all. That can happen fully in background. The only pecularity is that
when we restart a new instance before the previous instance is killed,
then we must wait for the previous process to terminate first. Also, if
we are about to exit while killing the dnsmasq instance, we must register
nm_shutdown_wait_obj_*() to wait until the process is fully gone.
2019-09-26 08:18:58 +02:00
Thomas Haller b6acec0fbc shared: add compat macro for G_PID_FORMAT
Having G_PID_FORMAT macro is useful, but it's only available in
recent glib versions. Add a compat implementation and a test that
our assumptions hold.
2019-09-26 08:18:58 +02:00
Thomas Haller 3214841765 shared/trivial: rename _nm_g_slice_free_fcn1() macro to nm_g_slice_free_fcn1()
The leading underscore has the notion that this would be a private function.
It really isn't, and it would be fine for the user to call it directly.

Just like we have g_slice_free() and g_slice_free1().
2019-09-23 13:44:36 +02:00
Thomas Haller d048050c2d shared: add more nm_dbus_connection_call_*() and nm_dbus_connection_signal_subscribe_*() helpers 2019-09-22 16:05:50 +02:00
Thomas Haller e688e70b37 shared: add nm_utils_hash_values_to_array() helper 2019-09-22 16:05:50 +02:00
Thomas Haller 2667a46874 shared: add nm_auto_remove_source cleanup macro 2019-09-22 16:05:50 +02:00
Thomas Haller 6a9ab7bb30 shared: add nm_auto_unref_io_channel cleanup macro 2019-09-22 16:05:50 +02:00
Thomas Haller b911cc17d8 shared: support nm_g_slice_free_fcn() for sizes of 32 bytes 2019-09-22 16:05:50 +02:00
Thomas Haller 908fadec96 shared: add NMRefString
I'd like to refactor libnm's caching. Note that cached D-Bus objects
have repeated strings all over the place. For example every object will
have a set of D-Bus interfaces (strings) and properties (strings) and an
object path (which is referenced by other objects). We can save a lot of
redundant strings by deduplicating/interning them. Also, by interning
them, we can compare them using pointer equality.

Add a NMRefString implementation for this.

Maybe an alternative name would be NMInternedString or NMDedupString, because
this string gets always interned. There is no way to create a NMRefString
that is not interned. Still, NMRefString name sounds better. It is ref-counted
after all.

Notes:

 - glib has GQuark and g_intern_string(). However, such strings cannot
   be unrefered and are leaked indefinitely. It is thus unsuited for
   anything but a fixed set of well-known strings.

 - glib 2.58 adds GRefString, but we cannot use that because we
   currently still use glib 2.40.
   There are some differences:

     - GRefString is just a typedef to char. That means, the glib API
       exposes GRefString like regular character strings.
       NMRefString intentionally does that not. This makes it slightly
       less convenient to pass it to API that expects "const char *".
       But it makes it clear to the reader, that an instance is in fact
       a NMRefString, which means it indicates that the string is
       interned and can be referenced without additional copy.

     - GRefString can be optionally interned. That means you can
       only use pointer equality for comparing values if you know
       that the GRefString was created with g_ref_string_new_intern().
       So, GRefString looks like a "const char *" pointer and even if
       you know it's a GRefString, you might not know whether it is
       interned. NMRefString is always interned, and you can always
       compare it using pointer equality.

  - In the past I already proposed a different implementation for a
    ref-string. That made different choices. For example NMRefString
    then was a typedef to "const char *", it did not support interning
    but deduplication (without a global cache), ref/unref was not
    thread safe (but then there was no global cache so that two threads
    could still use the API independently).

The point is, there are various choices to make. GRefString, the
previous NMRefString implementation and the one here, all have pros and
cons. I think for the purpose where I intend NMRefString (dedup and
efficient comparison), it is a preferable implementation.

Ah, and of course NMRefString is an immutable string, which is a nice
property.
2019-09-21 14:58:26 +02:00
Thomas Haller dd33b3a14e shared: add nm_auto_unlock_g_mutex and NM_G_MUTEX_LOCKED() helper macros 2019-09-21 14:52:31 +02:00
Lubomir Rintel 24028a2246 all: SPDX header conversion
$ find * -type f |xargs perl contrib/scripts/spdx.pl
  $ git rm contrib/scripts/spdx.pl
2019-09-10 11:19:56 +02:00
Thomas Haller a63f9aad25 shared/hash: implement nm_hash_obfuscate_ptr() as inline function instead of macro
There is really no reason for this to be a macro. Our hash-related
helpers (like nm_hash_update_val()) are macros because they do some
shenigans to accept arguments of different (compile-time) types. But
the arguments for nm_hash_obfuscate_ptr() are well known and expected
of a certain form.

Note that with "-O2" some quick testing shows that the compiler no
longer inlines the function. But I guess that's fine, probably the
compiler knows best anyway.
2019-08-23 11:48:58 +02:00
Thomas Haller a7d8fe0ea5 shared: allow negative timestamps for nm_utils_monotonic_timestamp_as_boottime() 2019-08-21 11:18:39 +02:00
Thomas Haller c1e40a4f39 shared: use nm_auto_unref_gtypeclass in _nm_utils_enum_from_str_full() 2019-08-16 08:16:02 +02:00
Thomas Haller b216abb012 shared,all: return boolean success from nm_utils_file_get_contents()
... and nm_utils_fd_get_contents() and nm_utils_file_set_contents().

Don't mix negative errno return value with a GError output. Instead,
return a boolean result indicating success or failure.

Also, optionally

  - output GError

  - set out_errsv to the positive errno (or 0 on success)

Obviously, the return value and the output arguments (contents, length,
out_errsv, error) must all agree in their success/failure result.
That means, you may check any of the return value, out_errsv, error, and
contents to reliably detect failure or success.

Also note that out_errsv gives the positive(!) errno. But you probably
shouldn't care about the distinction and use nm_errno_native() either
way to normalize the value.
2019-08-08 11:59:59 +02:00
Thomas Haller 1bad35061f shared: let nm_utils_file_set_contents() return a errno error code
nm_utils_file_set_contents() is a re-implementation of g_file_set_contents(),
as such it returned merely a boolean success value.

It's sometimes interesting to get the native error code. Let the function
deviate from glib's original g_file_set_contents() and return the error code
(as negative value) instead.

This requires all callers to change. Also, it's potentially a dangerous
change, as this is easy to miss.

Note that nm_utils_file_get_contents() also returns an errno, and
already deviates from g_file_get_contents() in the same way. This patch
resolves at least the inconsistency with nm_utils_file_get_contents().
2019-08-08 10:53:03 +02:00
Thomas Haller dda3289206 shared: add nm_c_list_elem_find_first() helper macro
- add nm_c_list_elem_find_first() macro that takes a predicate
  and returns the first match.

  This macro has a non-function-like behavior, which we often try to
  avoid because macros should behave like functions. In this case it's
  however convenient, so let's do it.
  Also, despite being non-function-like, it should be pretty hard to
  use wrongly.

- rename nm_c_list_elem_find_first() to nm_c_list_elem_find_first_ptr().
2019-08-08 10:07:15 +02:00
Thomas Haller bedafc661a shared: refactor nm_utils_g_slist_strlist_cmp() to avoid dead-code warning from Coverity
Coverity sees that "return 0" cannot be reached. Refactor the code,
to avoid the warning.

(cherry picked from commit 8fb954b81d)
2019-08-02 11:10:51 +02:00
Thomas Haller 14debab83a shared: try avoid coverity warning in _nm_utils_user_data_unpack()
Coverity says

  CID 202453 (#1 of 1): Wrong sizeof argument (SIZEOF_MISMATCH)suspicious_sizeof:
  Passing argument user_data of type gconstpointer and argument (gsize)nargs * 8UL /* sizeof (gconstpointer) */ to function g_slice_free1 is suspicious.

Let's pass instead the "data" pointer. It's the same, but maybe that
avoids the warning.

(cherry picked from commit d76df4c139)
2019-08-02 11:10:51 +02:00
Thomas Haller 709570b8e9 shared: explicitly ignore return value of g_utf8_validate()
Coverity doesn't like us ignoring the return value, although
we really only care about the "p" output pointer.

Try casting the result to (void), maybe that silences Coverity.

(cherry picked from commit e6fa3ce2df)
2019-08-02 11:10:51 +02:00
Thomas Haller f804bc7007 shared: fix non-serious bug with bogus condition in assertion in nm_key_file_db_ref()
(cherry picked from commit 9a229241f9)
2019-08-01 17:25:37 +02:00
Thomas Haller f3c2c07e37 shared: add NM_HASH_SEED_16() macro
(cherry picked from commit 72e0b522ff)
2019-07-31 10:44:37 +02:00
Thomas Haller dcdbe98406 shared: add nm_g_slice_free() helper
How odd that such a macro does not exist yet. It seems like
the majorities of calls to g_slice_free() could be replaced
by this.
2019-07-25 15:26:49 +02:00
Thomas Haller 9dfc3ff5af core: improve code comment and add assertion to nm_utils_monotonic_timestamp_as_boottime() 2019-07-25 15:10:43 +02:00
Thomas Haller b424f75479 config: simplify no-auto-default list handling and sort entries
- don't let no_auto_default_from_file() do any preprocessing of
  the lines that it reads. It merely splits the lines at '\n'
  and utf8safe-unescapes them.
  This was previously duplicated also by NMConfigData's property
  setter. We don't need to do it twice.

- sort the lines. This makes the entire handling O(n*ln(n)) instead
  of O(n^2). Also, sorting effectively normalizes the content, and
  it's desirable to have one true representation of what we write.
2019-07-25 10:52:47 +02:00
Thomas Haller 1440a3c149 shared: accept %NULL strings in nm_utils_strv_sort()
In particular when calling nm_utils_strv_sort() with a positive length
argument, then this is not a %NULL terminated strv arrary. That may mean
that it makes sense for the input array to contain %NULL strings.

Use a strcmp() function that accepts %NULL too.

While this is not used at the moment, I think nm_utils_strv_sort()
should accept %NULL strings beause otherwise it's a possibly unexpected
restriction of its API. The function should handle sensible input gracefully.
2019-07-25 10:44:22 +02:00
Thomas Haller 29a7bffecf shared: add nm_strcmp0_p_with_data() helper 2019-07-25 10:43:44 +02:00
Thomas Haller a78ba1c33a shared: add nm_strcmp_with_data()
It is like strcmp(), but has a signature suitable for GCompareDataFunc.

This is necessary with nm_utils_ptrarray_find_binary_search()
to search a sorted strv array.

The fault is here really C, which doesn't allow inline static functions.
So, you need all kinds of slightly different flavors for the same
callbacks (with or without user-data).

Note that glib2 internally just casts strcmp() to GCompareDataFunc ([1]),
relying on the fact how arguments are passed to the function and
ignoring the additional user-data argument. But I find that really
ugly and probably not permissible in general C. Dunno whether POSIX
would guarantee for this to work. I'd rather not do such function
pointer casts.

[1] 0c0cf59858/glib/garray.c (L1792)
2019-07-25 10:42:06 +02:00
Thomas Haller 3c0161a385 shared: add nm_utils_clock_gettime_*() util
Using clock_gettime() directly is a bit inconvenient. We usually
want to combine the fields of struct timespec into one timestamp
(for example, in unit nanoseconds).

Add a helper function to do that.
2019-07-23 12:19:22 +02:00
Thomas Haller d5ad315f11 shared: suppress -Werror=stringop-overflow= warning in nm_strndup_a()
nm_strndup_a() uses strncpy() because we want the behavior of clearing out
the memory after the first NUL byte. But that can cause a compiler warning:

    CC       src/settings/plugins/keyfile/libNetworkManager_la-nms-keyfile-utils.lo
  In file included from ../../shared/nm-default.h:279,
                   from ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:20:
  In function ‘_nm_strndup_a_step’,
      inlined from ‘nms_keyfile_loaded_uuid_is_filename’ at ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:65:9:
  ../../shared/nm-glib-aux/nm-macros-internal.h:1661:3: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
   1661 |   strncpy (s, str, len);
        |   ^~~~~~~~~~~~~~~~~~~~~
  ../../src/settings/plugins/keyfile/nms-keyfile-utils.c: In function ‘nms_keyfile_loaded_uuid_is_filename’:
  ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:48:8: note: length computed here
     48 |  len = strlen (filename);
        |        ^~~~~~~~~~~~~~~~~

It's true that the len argument of _nm_strndup_a_step() depends on the
string length of the source string. But in this case it's safe, because
we checked that the destination buffer is exactly the right size too.
By that reasoning we should use memcpy() or strcpy(), but both are
unsuitable. That is because we want nm_strndup_a() to behave like
strndup(), which means we need to handle cases where the len argument
is larger than the string length of the source string. That is, we want
always to return a buffer of size len+1, but we want to copy only the
characters up to the first NUL byte, and clear out the rest. That's what
strncpy() does for us.

Silence the warning.
2019-07-16 10:48:38 +02:00
Thomas Haller a9b15bde3c shared: add NM_CMP_DIRECT_STRCMP() macro 2019-07-10 12:43:06 +02:00
Thomas Haller 6d30021fee shared: optimize nm_utils_error_set() for string literals
If there is only one argument, we can assume this is a plain string.

That is especially the case, because g_set_error() is G_GNUC_PRINTF()
and would warn if this would be a format string with missing parameters.

This is for convenience. Previously, one was compelled to explicitly
choose between nm_utils_error_set_literal() and nm_utils_error_set().
Now, it automatically chooses.

Note that there are a few things that won't work, like

  nm_utils_error_set (error, code, "bogus %u escape");

But that's good. You get a compiler warning (as you used to)
and it's clear in this case you really need
nm_utils_error_set_literal().
2019-07-10 12:43:06 +02:00
Thomas Haller 7f75a1b5f5 shared: add nm_pdirect_hash()/nm_pdirect_equal()
This follows a pointer to a pointer and compares them. In a sense
it's like nm_pstr_*(), which follow a pointer to a string. However,
these functions use direct pointer comparison.

The purpose is when you hash a key that has as first field a pointer
value (and then compare them by pointer equality).
2019-07-10 12:43:06 +02:00
Thomas Haller fe52a9467b shared: gracefully accept %NULL strings for NM_STR_HAS_PREFIX() and NM_STR_HAS_SUFFIX()
In case it wasn't obvious to a caller, allow %NULL as valid string
argument. Just be a bit more forgiving and fault-tolerant.
2019-07-04 17:11:24 +02:00
Thomas Haller 7c84227c93 shared: add nm_c_list_elem_free_steal() util 2019-07-02 17:52:53 +02:00
Thomas Haller b4fe51b5fa shared: add nm_utils_strv_dup() util 2019-06-28 16:48:17 +02:00
Thomas Haller 1d2df314cc shared: add nm_c_list_free_all() macro 2019-06-28 16:45:06 +02:00
Thomas Haller b9587008fc shared: add nm_clear_error() and patch g_clear_error() to use this inlinable variant 2019-06-26 09:53:54 +02:00
Thomas Haller 03b8eb124e shared/glib: unconditionally redefine g_object_ref()/g_object_ref_sink() as typesafe macro 2019-06-26 09:53:54 +02:00
Thomas Haller 02ac5693d3 shared: add nm_utils_file_stat() util
A small convenience function to call stat(). The difference is that the
function returns an error code.
2019-06-26 09:53:54 +02:00
Thomas Haller ec707f56c1 shared: add nm_utils_hashtable_same_keys() util 2019-06-26 09:53:54 +02:00
Thomas Haller fcaf7994f2 shared: allow nm_c_list_move_*() API also to move from one list to another
Previously, nm_c_list_move_*() only allowed to move element inside the
same list. Relax that, it works just the same list to move the element
from one list into a different list.
2019-06-26 09:53:54 +02:00
Thomas Haller 637c785f4e shared: fix nm_utils_bin2hexstr_full() for buffers of length zero 2019-06-19 15:30:55 +02:00
Thomas Haller 1cc4a8b6a9 shared: add nm_utils_g_slist_strlist_cmp() util
Usually we avoid GSList, because I think it's not a great data type.
Anyway, our match-specs are just a GSList of strings, so we need some
API to handle them.
2019-06-17 12:12:02 +02:00
Thomas Haller b1f5e971f3 shared: add nm_g_variant_ref_sink() util 2019-06-13 16:10:53 +02:00
Thomas Haller 5b721ba90d shared: add nm_c_list_elem_find_first() and minor cleanups of NMCListElem API 2019-06-13 16:10:53 +02:00
Thomas Haller 8b2d115f9d shared: add nm_utils_g_slist_find_str() util 2019-06-13 16:10:53 +02:00
Thomas Haller c0e075c902 all: drop emacs file variables from source files
We no longer add these. If you use Emacs, configure it yourself.

Also, due to our "smart-tab" usage the editor anyway does a subpar
job handling our tabs. However, on the upside every user can choose
whatever tab-width he/she prefers. If "smart-tabs" are used properly
(like we do), every tab-width will work.

No manual changes, just ran commands:

    F=($(git grep -l -e '-\*-'))
    sed '1 { /\/\* *-\*-  *[mM]ode.*\*\/$/d }'     -i "${F[@]}"
    sed '1,4 { /^\(#\|--\|dnl\) *-\*- [mM]ode/d }' -i "${F[@]}"

Check remaining lines with:

    git grep -e '-\*-'

The ultimate purpose of this is to cleanup our files and eventually use
SPDX license identifiers. For that, first get rid of the boilerplate lines.
2019-06-11 10:04:00 +02:00
Thomas Haller 7440c0c564 shared: use NM_MIN() in NM_CMP_FIELD_MEMCMP_LEN() macro
To avoid evaluating the argument more than once.
2019-06-11 08:25:10 +02:00
Thomas Haller 3d42b2f1fa shared: fix _NM_ENSURE_TYPE_CONST() for const pointers with clang
Clang 3.4.2-9.el7 on CentOS7.6 complains about missing generic type match:

    ../dispatcher/nm-dispatcher.c:243:2: error: controlling expression type 'const Request *const' (aka 'const struct Request *const') not compatible with any generic association type
            _LOG_R_D (request, "start running ordered scripts...");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fixes: 17dc6a9da6 ('shared: add _NM_ENSURE_TYPE_CONST()')
2019-05-29 09:42:40 +02:00
Beniamino Galvani d6a51ced40 ifcfg-rh: preserve existence of wired setting
Currently the plugin doesn't preserve the existence of a wired setting
because the writer saves only variables with non-default values and,
especially, the reader always creates the setting.

Fix this; now the writer writes HWADDR even if empty when the setting
is present; the reader creates the setting when at least one property
is found.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/166
https://bugzilla.redhat.com/show_bug.cgi?id=1703960
2019-05-28 09:53:00 +02:00
Thomas Haller e1b824b871 shared: fix return in nm_value_type_to_variant()/nm_value_type_get_variant_type()
Fixes: 75703a2425 ('shared: add accessor functions for NMValueType')
2019-05-27 13:27:13 +02:00
Thomas Haller 17dc6a9da6 shared: add _NM_ENSURE_TYPE_CONST()
The sole purpose of this is more type-safe macros.

An alternative solution would be to define a function instead of a
macro. But if the function is unused (currently!) you get a compiler
warning (on some compilers even when marking the function as "static
inline", if it's in the source file). A workaround for that would be
to mark the function as _nm_unused, or to use a macro instead.

_NM_ENSURE_TYPE_CONST() is to aid the macro solution.
2019-05-27 12:01:09 +02:00
Thomas Haller 5d3b033072 shared: add nm_utils_gvariant_vardict_filter*() helpers
Usually, such an operation does not make much sense. It's also not good
performance wise.

But for unit testing this becomes very interesting.
2019-05-23 18:09:49 +02:00
Thomas Haller 848a80598e shared: add JSON helper functions for NMValueType 2019-05-23 18:09:49 +02:00
Thomas Haller 75703a2425 shared: add accessor functions for NMValueType
"nm-value-type.h" is a header-only file, as it contains only small
accessors that should be inlined.

As such, the implementation of these functions is guarded by "#ifdef
NM_VALUE_TYPE_DEFINE_FUNCTIONS", so that one can use this header (and
NMValueType enum) with less overhead (at compile time).
2019-05-23 18:09:49 +02:00
Thomas Haller e64fdeeaf6 shared: add "shared/nm-glib-aux/nm-value-type.h"
Glib has GValue which used for boxing value.

Add NMValueType enum, which has a similar purpose, but it's much more
limited.

- contrary to GValue, the type must be tracked separately from the
  user-data. That is, the "user-data" is only a pointer of appropriate
  type, and the knowledge of the actual NMValueType is kept separately.
  This will be used to have a static list of meta-data that knows the
  value types, but keeping the values independent of this type
  information. With GValue this would not be possible.

- the use case is much more limited. Just support basic integers,
  boolean and strings. Nothing fancy.

Note that we already do something similar at muliple places. See for
example NMVariantAttributeSpec and nm_utils_team_link_watcher_to_string().
These could/should instead use NMValueType.
2019-05-23 18:09:49 +02:00
Thomas Haller efccc2a53c shared: add nm_json_aux_gstr_append_*() helper 2019-05-23 18:09:49 +02:00
Thomas Haller 50dbcda015 shared: add nm_jansson_json_as_*() helpers to "shared/nm-glib-aux/nm-jansson.h" 2019-05-23 18:09:49 +02:00
Thomas Haller f84e623732 shared: add "shared/nm-glib-aux/nm-json-aux.h"
This will be a set of JSON related utilities, that are independent of
libjansson.
2019-05-23 18:09:49 +02:00
Thomas Haller f5482013ac shared: add NM_GOBJECT_PROPERTIES_DEFINE_NOTIFY() macro as part of NM_GOBJECT_PROPERTIES_DEFINE()
This will be needed independently from NM_GOBJECT_PROPERTIES_DEFINE().
2019-05-23 18:09:49 +02:00
Thomas Haller dadd38c484 shared: add nm_utils_strv_make_deep_copied_n() helper 2019-05-23 18:09:49 +02:00
Thomas Haller 79ef1abcca shared: add nm_strcmp0()
g_strcmp0() is not inlineable, so we first need to call to glib, only to
call to libc.
2019-05-23 18:09:49 +02:00
Thomas Haller 2946d07085 shared: implement _nm_utils_monotonic_timestamp_initialized() in "nm-logging-stub.c" 2019-05-22 20:04:08 +02:00
Thomas Haller fbddd27e73 shared: fix type shenanigans for data pointer of nm_memdup_maybe_a()
The type of the "data" pointer may not be compatible with the type of
the "to_free" / output pointer. This is due to constness, and that we
are unable in C to remove constness from a type.

For example,

    {
        const char *const *data = ...;
        gs_free const char **cpy_to_free = NULL;
        const char **cpy;

        cpy = nm_memdup_maybe_a (300, data, NM_PTRARRAY_LEN (data) + 1, &cpy_to_free);
    }

is prefectly valid , but would not have compiled.

It shows that "data" is not of type "*(&cpy_to_free)", but rather
it might be a non-const pointer of the same type.

Fixes: d0e1d0e626 ('shared: propagate types in nm_malloc_maybe_a(), nm_malloc0_maybe_a(), nm_memdup_maybe_a()')
2019-05-16 11:42:16 +02:00
Thomas Haller d0e1d0e626 shared: propagate types in nm_malloc_maybe_a(), nm_malloc0_maybe_a(), nm_memdup_maybe_a()
The "to_free" pointer should have the suitable type that we are
requesting. Use "typeof()" to preserve and propagate the pointer
type in the macro.
2019-05-16 08:06:46 +02:00
Thomas Haller 3712e7a89f shared: use union instead of _nm_alignas() for static hash-seed
We want the the hash-seed array is alined so it can be used both as
guint, guint32, and guint64 directly. Don't use _nm_alignas() but
instead just add the fields to the union so we get proper alignment.

While at at, also let the seed argument to c_siphash_init() be aligned
to 64 integers. c_siphash_init() does not require that, but it tries to
read the seed as (unaligned) LE 64 bit integers. So, it doesn't hurt.
2019-05-13 09:25:05 +02:00
Thomas Haller 78999f9b61 shared: add NM_HASH_OBFUSCATE_PTR() macro
We want to log pointer values to indicate the related parties of a
log message. But we should not, because plain pointer values can be
used to defeat ASLR.

Instead, we have nm_hash_obfuscate_ptr() to managle a pointer and give
a distinct (albeit not 100% unique) 64 bit integer for logging.

But for the logging messages to be meaning-full, all related parties
must use the same static-seed.

Add a macro NM_HASH_OBFUSCATE_PTR() that uses a particular seed.
2019-05-13 09:25:05 +02:00
Thomas Haller 654faa4d38 shared: add nm_dbus_connection_call_start_service_by_name() helper 2019-05-12 09:56:36 +02:00
Thomas Haller f7fff62067 shared: add nm_clear_g_dbus_connection_signal() helper 2019-05-12 09:56:36 +02:00
Thomas Haller 8ffa75685e shared: add nm_dbus_connection_signal_subscribe_name_owner_changed() helper
... and nm_dbus_connection_call_get_name_owner().

We are going to use GDBusConnection more instead of GDBusProxy. Hence,
these two functions are the standard repertoire and used over and over.

Their arguments are complicated enough to warrant a small helper.
2019-05-12 09:56:36 +02:00
Thomas Haller 655e6bb1e3 shared: add "shared/nm-glib-aux/nm-dbus-aux.h" 2019-05-12 09:56:36 +02:00
Beniamino Galvani 4735d6764a all: fix typos (milli seconds -> milliseconds) 2019-05-08 13:35:59 +02:00
Thomas Haller 9d2623cceb shared: use nm_str_skip_leading_spaces() in _nm_utils_ascii_str_to_int64() 2019-05-07 20:58:17 +02:00
Thomas Haller b0693863c1 shared: add NMKeyFileDB API
It will be used for "/var/lib/NetworkManager/seen-bssids" and
"/var/lib/NetworkManager/timestamps" which currently is implemented
in NMSettingConnection.
2019-05-07 16:41:21 +02:00
Thomas Haller 8c2fda7ca0 shared: add "shared/nm-glib-aux/nm-keyfile-aux.h" 2019-05-07 16:41:21 +02:00
Thomas Haller 8decdf2225 shared: add nm_log_level_from_syslog() helper to convert from syslog levels 2019-05-07 16:41:21 +02:00
Thomas Haller 945620624a shared: add nm_malloc_maybe_a(), nm_malloc0_maybe_a() and nm_memdup_maybe_a() utils 2019-05-07 09:38:44 +02:00
Thomas Haller 4853be52af shared: add nm_g_variant_take_ref() util 2019-05-01 13:46:32 +02:00
Thomas Haller 2f9e55ee52 shared,libnm-core: use nm_utils_named_value_list_sort() 2019-04-25 08:53:51 +02:00
Thomas Haller 6d472dacb4 shared: add nm_utils_named_value_list_*() utils
nm_utils_named_value_list_find(), nm_utils_named_value_list_sort(),
and nm_utils_named_value_list_is_sorted().
2019-04-25 08:53:51 +02:00
Thomas Haller d984b2ce4a shared: move most of "shared/nm-utils" to "shared/nm-glib-aux"
From the files under "shared/nm-utils" we build an internal library
that provides glib-based helper utilities.

Move the files of that basic library to a new subdirectory
"shared/nm-glib-aux" and rename the helper library "libnm-core-base.la"
to "libnm-glib-aux.la".

Reasons:

 - the name "utils" is overused in our code-base. Everything's an
   "utils". Give this thing a more distinct name.

 - there were additional files under "shared/nm-utils", which are not
   part of this internal library "libnm-utils-base.la". All the files
   that are part of this library should be together in the same
   directory, but files that are not, should not be there.

 - the new name should better convey what this library is and what is isn't:
   it's a set of utilities and helper functions that extend glib with
   funcitonality that we commonly need.

There are still some files left under "shared/nm-utils". They have less
a unifying propose to be in their own directory, so I leave them there
for now. But at least they are separate from "shared/nm-glib-aux",
which has a very clear purpose.

(cherry picked from commit 80db06f768)
2019-04-18 19:57:27 +02:00