Commit graph

402 commits

Author SHA1 Message Date
Rasmus Thomsen d38b5d92ee
shared/nm-glib-aux: don't use RTLD_DEEPBIND on musl
[thaller@redhat.com: original patch modified]
2020-12-13 17:10:53 +01:00
Thomas Haller 712516f2ac
shared: fix _POSIX_C_SOURCE not being defined for nm-errno.c
This is the case with musl.

Based-on-patch-by: Rasmus Thomsen <oss@cogitri.dev>
2020-12-13 17:10:53 +01:00
Thomas Haller 3490a09a7d
shared: fix race in nm_ref_string_unref()
We cannot drop the reference count to zero while having
no lock. Otherwise, another thread might race doing

  s = nm_ref_string_new("...");
  nm_ref_string_unref(s);

and already successfully delete the instance.

Hitting this race should be rather difficult, especially because
we tend to use NMRefString only from one thread. But still, access
to global variables must be race free.

Fixes: 908fadec96 ('shared: add NMRefString')
2020-12-08 20:07:10 +01:00
Thomas Haller ea3b979a42
shared: add static asserts for size/alignment of NMEtherAddr 2020-11-19 20:12:26 +01:00
Thomas Haller f125d821be
shared: improve NM_ETHER_ADDR_INIT() helper macro
The macro should require exactly 6 parameters (for the 6 bytes
of the address). On the other hand, we also should be able to
use a macro like

  NM_ETHER_ADDR_INIT(NM_BRIDGE_GROUP_ADDRESS_DEF_BIN)

To get that work properly, we need to expand the variadic macro
once.

Also, cast the result to the struct type. With this, it can
not only be used for initialization, but also for assignment
and temporary variables.
2020-11-19 20:10:33 +01:00
Thomas Haller dd9c818947
shared: add NM_UTILS_ERROR_NOT_READY enum value
This is a general error code for being not ready to perform the
operation.
2020-11-17 19:49:03 +01:00
Thomas Haller eaa48caac4
shared: add nm_g_hash_table_ref()/nm_g_hash_table_unref() helpers 2020-11-17 19:49:02 +01:00
Beniamino Galvani dfd2fcde0f shared: add c-list macros to iterate backwards 2020-11-16 16:43:39 +01:00
Thomas Haller 336270edd5
shared/strbuf: add nm_str_buf_get_char() and nm_str_buf_get_str_at_unsafe() helpers 2020-11-09 17:25:25 +01:00
Thomas Haller a3aa3725e5
shared,all: cleanup nm_utils_hashtable_equal*() functions
We have:

- nm_utils_hashtable_cmp(): this does a full cmp of two hash
  tables, with the intent to provide a stable sort order.
  It thus takes a GCompareDataFunc() argument.

- nm_utils_hashtable_cmp_equal(): this is like nm_utils_hashtable_cmp(),
  except that the caller won't get a compare value, only a boolean
  value that indicates equality.
  This was previously called nm_utils_hashtable_equal().

- nm_utils_hashtable_equal(): this takes a GEqualFunc function
  for comparing the values for equality. It takes thus
  a different kind of predicate, but otherwise is similar to
  nm_utils_hashtable_cmp_equal().
  This was previously called nm_utils_hash_table_equal().

Unify the naming of these functions.
2020-11-09 17:25:25 +01:00
Thomas Haller d52c3b3c94
shared: use GEqualFunc instead of NMUtilsHashTableEqualFunc typedef 2020-11-09 17:25:25 +01:00
Thomas Haller f4d472beab
shared: add nm_g_array_unref() helper 2020-11-09 17:25:24 +01:00
Thomas Haller 0cf4250021
shared: add nm_utils_strdict_clone() helper 2020-11-09 17:25:24 +01:00
Thomas Haller 571aeec933
shared: add nm_utils_named_value_clear_with_g_free() helper 2020-11-09 17:25:24 +01:00
Thomas Haller c4d981959e
shared: add nm_utils_strdup_reset_take() helper 2020-11-09 17:25:24 +01:00
Thomas Haller 7c60e984b6
shared: also reimplement g_atomic_pointer_set() macro
It's not strictly necessary, because contrary to g_atomic_pointer_get()
and g_atomic_pointer_compare_and_exchange(), glib's variant for the
setter is mostly fine.

Still, reimplement it, because we use typeof() eagerly and can thus add
more static checks than glib.
2020-11-03 15:39:00 +01:00
Thomas Haller 5e57ea37f0
shared: add a compat implementation for g_atomic_pointer_get()
With glib2-2.67.0-1.fc34.x86_64.rpm, clang-11.0.0-2.fc34.x86_64.rpm, we
get a failure for g_atomic_pointer_get():

    ../shared/nm-glib-aux/nm-hash-utils.c:38:9: error: passing 'typeof (*(&global_seed)) *' (aka 'const unsigned char *volatile *') to parameter of type 'const guint8 **' (aka 'const unsigned char **') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
        g = g_atomic_pointer_get(&global_seed);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /usr/include/glib-2.0/glib/gatomic.h:112:38: note: expanded from macro 'g_atomic_pointer_get'
        __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
                                         ^~~~~~~~~~~~~~~~~
    ../shared/nm-glib-aux/nm-hash-utils.c:109:32: error: passing 'typeof (*(&global_seed)) *' (aka 'const unsigned char *volatile *') to parameter of type 'const guint8 **' (aka 'const unsigned char **') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
        return ((*((const guint *) _get_hash_key())) ^ static_seed) ?: 3679500967u;
                                   ^~~~~~~~~~~~~~~
    ../shared/nm-glib-aux/nm-hash-utils.c:84:14: note: expanded from macro '_get_hash_key'
            _g = g_atomic_pointer_get(&global_seed); \
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /usr/include/glib-2.0/glib/gatomic.h:112:38: note: expanded from macro 'g_atomic_pointer_get'
        __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
                                         ^~~~~~~~~~~~~~~~~
    ../shared/nm-glib-aux/nm-hash-utils.c:123:9: error: passing 'typeof (*(&global_seed)) *' (aka 'const unsigned char *volatile *') to parameter of type 'const guint8 **' (aka 'const unsigned char **') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
        g = _get_hash_key();
            ^~~~~~~~~~~~~~~
    ../shared/nm-glib-aux/nm-hash-utils.c:84:14: note: expanded from macro '_get_hash_key'
            _g = g_atomic_pointer_get(&global_seed); \
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /usr/include/glib-2.0/glib/gatomic.h:112:38: note: expanded from macro 'g_atomic_pointer_get'
        __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
                                         ^~~~~~~~~~~~~~~~~
2020-11-03 15:39:00 +01:00
Thomas Haller 2898daa518
shared,all: introduce and use LOGD_IPX()/LOGD_DHCPX() macros
These macros are consistent with NMP_OBJECT_TYPE_IP_ADDRESS()
and NMP_OBJECT_TYPE_IP_ROUTE(), in name and usage.

Replace the previous functions that had inconsistent and a verbose
naming.
2020-10-30 12:38:31 +01:00
Thomas Haller a7e1fa48fd
shared: add nm_g_variant_new_ay_inaddr() helper 2020-10-30 11:58:44 +01:00
Thomas Haller c3fe895ebc
trivial: whitespace fixes 2020-10-23 17:11:53 +02:00
Thomas Haller 6e44842dc6
shared: introduce opaque type NMUtilsUserData for nm_utils_user_data_pack()
This makes the usage slightly more type safe.
2020-10-22 17:38:08 +02:00
Thomas Haller 8dc3f07d34
shared: fix NM_CAST_STRV_CC() for "char **const" pointers
clang-3.4.2-9.el7.x86_64 otherwise fails:

    ../src/devices/wifi/nm-wifi-p2p-peer.c:410:44: error: controlling expression type 'const char **const' not compatible with any generic association type
        if (!nm_utils_strv_equal(priv->groups, peer_info->groups)) {
                                               ^~~~~~~~~~~~~~~~~
    ../shared/nm-glib-aux/nm-shared-utils.h:1689:78: note: expanded from macro 'nm_utils_strv_equal'
    #define nm_utils_strv_equal(strv1, strv2) (nm_utils_strv_cmp_n((strv1), -1, (strv2), -1) == 0)
                                                                                 ^
    ../shared/nm-glib-aux/nm-shared-utils.h:1687:74: note: expanded from macro 'nm_utils_strv_cmp_n'
        _nm_utils_strv_cmp_n(NM_CAST_STRV_CC(strv1), (len1), NM_CAST_STRV_CC(strv2), (len2))
                                                                             ^
    ../shared/nm-glib-aux/nm-macros-internal.h:706:21: note: expanded from macro 'NM_CAST_STRV_CC'
            (_Generic ((value), \
                        ^
2020-10-19 21:25:11 +02:00
Thomas Haller a9ffb5b73e
shared: enforce trailing semicolon after NM_UTILS_FLAGS2STR_DEFINE(),NM_UTILS_ENUM2STR_DEFINE() 2020-10-19 16:43:47 +02:00
Thomas Haller 8b25e4ecfe
shared: add nm_g_ptr_array_ref() and similar helpers 2020-10-13 13:39:01 +02:00
Thomas Haller 28bbe2a798
shared,platform: take pointer to NMEtherAddr in NM_ETHER_ADDR_FORMAT_VAL() macro
It seems nicer to require a pointer than the plain struct.
2020-10-13 12:56:25 +02:00
Thomas Haller ef9510e30c
all: remove unneessary cases from nm_utils_strv_equal()/nm_utils_strv_cmp_n() 2020-10-12 22:21:11 +02:00
Thomas Haller 251ba8ea44
shared: automatically cast strv argument for nm_utils_strv_equal()/nm_utils_strv_cmp_n()
It's cumbersome if we always need to cast our arguments for
the strv helper functions. Depending on the situation, we often
have a "char **" or a "const char *const*" argument.

Use NM_CAST_STRV_CC() macros instead. This macro uses C11's _Generic()
and casts types that are presumed to be safe. This tends to be less
typing and more type-safe, because you don't need an explicit C cast
(which would overrule any warning that the compiler might have for you).
2020-10-12 22:20:01 +02:00
Thomas Haller cbcfc58794
all/trivial: rename _nm_utils_strv_equal()/_nm_utils_strv_cmp_n() to have no leading underscore
The underscore somehow indicated that these would be an internal
function. Which they are in the sense that they are in "shared/nm-glib-aux/".
But they part of our internal helper functions, and in our code base
their use is no discouraged or "private.

Also, next I'll replace the function call with a macro, so, I will
have a need for the underscore name.

Rename.
2020-10-12 22:18:26 +02:00
Thomas Haller 3cab307fa7
shared: enforce trailing semicolon after NM_UTILS_STRING_TABLE_LOOKUP_DEFINE() 2020-10-08 17:01:26 +02:00
Thomas Haller fb2d11f1f9
all: use _NM_DUMMY_STRUCT_FOR_TRAILING_SEMICOLON define for trailing semicolon 2020-10-08 17:00:22 +02:00
Thomas Haller d456d083f1
all: add trailing semicolon after NM_UTILS_STRING_TABLE_LOOKUP_DEFINE() 2020-10-08 16:54:44 +02:00
Thomas Haller 5b941666e9
shared: add NM_HASH_OBFUSCATE_PTR_STR() helper macro
Contrary to NM_HASH_OBFUSCATE_PTR(), this allows to special case a %NULL
pointer. Also, it quotes the obfuscated value in square brackets.
2020-10-08 11:55:36 +02:00
Thomas Haller 978145f8ba
shared: return NULL from nm_malloc_maybe_a() when asking for zero bytes
The documentation of g_alloca()/alloca() isn't clear about what
happens when asking for zero bytes. Make it clear, by always returning
NULL.

Also, add a static assertion that @alloca_maxlen is a well-defined
positive integer.
2020-10-02 11:51:33 +02:00
Thomas Haller f7f979cda6
shared: add nm_ether_addr_to_string() helper 2020-10-01 17:35:14 +02:00
Thomas Haller c36eedf4d8
shared: add ether_addr field of type NMEtherAddr to NMIPAddr union 2020-10-01 17:35:13 +02:00
Thomas Haller d9ca728005
shared: add nm_ether_addr_cmp()/nm_ether_addr_equal() helpers 2020-09-30 09:49:20 +02:00
Thomas Haller f37dd5d394
shared: move NM_CMP*() macros to "nm-std-aux.h"
I would need these macros earlier in "nm-glib-aux/nm-shared-utils.h",
so it would be sufficient to just move them.

However, when I already move them, move them to "nm-std-aux/nm-std-aux.h"
because they don't need a glib dependency.
2020-09-30 09:48:26 +02:00
Thomas Haller 88071abb43
all: unify comment style for SPDX-License-Identifier tag
Our coding style recommends C style comments (/* */) instead of C++
(//). Also, systemd (which we partly fork) uses C style comments for
the SPDX-License-Identifier.

Unify the style.

  $ sed -i '1 s#// SPDX-License-Identifier: \([^ ]\+\)$#/* SPDX-License-Identifier: \1 */#' -- $(git ls-files -- '*.[hc]' '*.[hc]pp')
2020-09-29 16:50:53 +02:00
Thomas Haller 8841d529e1
format: manually replace remaining tabs with spaces and reformat 2020-09-29 09:12:27 +02:00
Thomas Haller 740b092fda
format: replace tabs for indentation in code comments
sed -i \
     -e 's/^'$'\t'' \*/     */g' \
     -e 's/^'$'\t\t'' \*/         */g' \
     -e 's/^'$'\t\t\t'' \*/             */g' \
     -e 's/^'$'\t\t\t\t'' \*/                 */g' \
     -e 's/^'$'\t\t\t\t\t'' \*/                     */g' \
     -e 's/^'$'\t\t\t\t\t\t'' \*/                         */g' \
     -e 's/^'$'\t\t\t\t\t\t\t'' \*/                             */g' \
     $(git ls-files -- '*.[hc]')
2020-09-28 16:07:52 +02:00
Antonio Cardace 328fb90f3e
all: reformat all with new clang-format style
Run:

    ./contrib/scripts/nm-code-format.sh -i
    ./contrib/scripts/nm-code-format.sh -i

Yes, it needs to run twice because the first run doesn't yet produce the
final result.

Signed-off-by: Antonio Cardace <acardace@redhat.com>
2020-09-28 16:07:51 +02:00
Thomas Haller 39b52d0fd0
shared: add nm_utils_uid_to_name()/nm_utils_name_to_uid() helpers
These are inspired by systemd.

We should replace our calls to getpwuid() and getpwnam() with
their thread safe variants.

We run possibly multiple threads (e.g. helper threads from GDBus and
GResolver). It's hard to be sure that they don't also access the
functions.
2020-09-25 21:03:27 +02:00
Thomas Haller 3aca45ed55
shared,libnm: avoid mixing signed and unsigned integers in netmask_to_prefix()/prefix_to_netmask() 2020-09-24 11:07:36 +02:00
Thomas Haller 1b79b33206
shared: add NM_PRINT_FMT_QUOTED2() helper macro 2020-09-24 09:43:53 +02:00
Thomas Haller dea59122e7
shared: move nm_utils_hwaddr_ntoa_buf() to shared/ as _nm_utils_hwaddr_ntoa()
The name is better as it mirrors nm_utils_hwaddr_aton(). Also, move
it to shared/ so it can be reused (and inlined).
2020-09-23 13:57:37 +02:00
Thomas Haller 74c03da086
shared: move _nm_utils_hwaddr_aton() to shared/
_nm_utils_hwaddr_aton() is only a wrapper around nm_utils_hexstr2bin_full().
But it abstracts the "right" parameters for what we consider a valid MAC
address and what not. As such, this function is useful.

Move it to "shared/" and replace the dupicate macro hwaddr_aton() with
it.
2020-09-23 13:57:04 +02:00
Thomas Haller dc3a477884
all/style: remove duplicate semicolon (";;") in sources 2020-09-23 12:43:47 +02:00
Thomas Haller b8811d97a4
all: require a semicolon after NM_CACHED_QUARK_FCN() 2020-09-23 10:55:17 +02:00
Thomas Haller e8dd19bb01
shared: extend nm_utils_hexstr2bin_full() to require hexdigits in pairs
nm_utils_hexstr2bin_full() is our general hexstr to binary parsing
method. It uses (either mandatory or optional) delimiters. Before,
if delimiters are in use, it would accept individual hexdigits.
E.g. "a:b" would be accepted as "0a:0b:.

Add an argument that prevents accepting such single digits.
2020-09-22 17:40:41 +02:00
Thomas Haller 479de883b3
shared: add m_g_variant_singleton_u_0()
We anyway cache our variants for the properties of NMDBusObject instances.
If such a variant is well known to be always the same, there is no need
to allocate a new instance every time. In particular, because GVariant
is an immutable and ref counted type.

Add a singleton getter for a "u" variant with numeric value 0.
2020-09-18 15:26:37 +02:00