Commit graph

80 commits

Author SHA1 Message Date
Thomas Haller
d422434945
glib-aux: add nm_g_array_{first,last}() helpers
A GArray can commonly used like a stack or a fifo list.
Add convenience accessors to get the first/last element.
2021-09-08 18:33:43 +02:00
Thomas Haller
398f6a8afb
glib-aux: add nm_assert_is_bool() helper 2021-09-08 18:33:42 +02:00
Thomas Haller
1d019308f4
glib-aux: fix compiler error using thread-local for _nm_utils_to_string_buffer
On CentOS 7, gcc.x86_64 0:4.8.5-44.el7 fails compilation:

  In file included from ./src/libnm-glib-aux/nm-default-glib.h:69:0,
                   from ./src/libnm-glib-aux/nm-default-glib-i18n-lib.h:13,
                   from src/libnm-core-aux-extern/nm-libnm-core-aux.c:6:
  ./src/libnm-glib-aux/nm-shared-utils.h:1051:1: error: '__thread' before 'extern'
   _nm_thread_local extern char _nm_utils_to_string_buffer[2096];
   ^

Fixes: fb94903444 ('glib-aux: mark _nm_utils_to_string_buffer at thread-local')
2021-09-06 09:38:23 +02:00
Thomas Haller
4b6e119010
all: pass pointer to "struct NMUtilsIPv6IfaceId" to functions instead of struct
While NMUtilsIPv6IfaceId is only 8 bytes large, it seems unidiomatic to
pass the plain struct around.

With a "const NMUtilsIPv6IfaceId *" argument it is more clear what the
meaning of this is.

Change to use pointers.
2021-08-31 16:49:46 +02:00
Thomas Haller
fb94903444
glib-aux: mark _nm_utils_to_string_buffer at thread-local
We possibly should refactor our code to no use _nm_utils_to_string_buffer,
but instead always provide a suitable (stack allocated?) buffer.

Until that is done, make the buffer thread local so that it avoids
the most obvious problem (of resulting in non-thread-safe code).
2021-08-31 16:40:45 +02:00
Thomas Haller
06713e7645
glib-aux: add nm_g_main_context_iterate_for_msec() helper 2021-08-06 14:31:05 +02:00
Thomas Haller
622a949ecc
glib-aux: add nm_arr_insert_at()/nm_arr_remove_at() helpers
The point is rather special, and the macros themselves are basically
simple wrappers around memmove().

When having a sorted array (for example, a strv array that is searched
using nm_strv_find_binary_search()), then we want to insert/remove
elements at a particular place (via memmove()).

Getting the memmove() arguments is not terribly hard, but hard enough to
add two helper macros for that.
2021-08-05 14:59:10 +02:00
Thomas Haller
68e049119a
glib-aux: add nm_g_main_context_iterate_ready() helper 2021-08-04 08:23:58 +02:00
Thomas Haller
b7c77d51eb
glib-aux: add nm_g_child_watch_source_new() and nm_g_child_watch_add_source() helpers 2021-08-04 08:23:58 +02:00
Thomas Haller
5d08d3a7ef
glib-aux: use GUnixFDSourceFunc for nm_g_unix_fd_source_new() signature 2021-08-04 08:23:58 +02:00
Thomas Haller
593cb57eb6
all: rename nm_utils_strdict_*() to nm_strdict_*() 2021-08-02 09:26:48 +02:00
Thomas Haller
3587cbd827
all: rename nm_utils_strsplit_set*() to nm_strsplit_set*() 2021-08-02 09:26:47 +02:00
Thomas Haller
4ac66a4215
all: rename nm_utils_strdup_reset*() to nm_strdup_reset*() 2021-08-02 09:26:47 +02:00
Thomas Haller
d0ba87a1ad
all: rename nm_utils_strbuf_*() API to nm_strbuf_*()
The "utils" part does not seem useful in the name.

Note that we also have NMStrBuf, which is named nm_str_buf_*().
There is an unfortunate similarity between the two, but it's still
distinct enough (in particular, because one takes an NMStrBuf and
the other not).
2021-08-02 09:26:42 +02:00
Thomas Haller
4c3aac899e
all: unify and rename strv helper API
Naming is important, because the name of a thing should give you a good
idea what it does. Also, to find a thing, it needs a good name in the
first place. But naming is also hard.

Historically, some strv helper API was named as nm_utils_strv_*(),
and some API had a leading underscore (as it is internal API).

This was all inconsistent. Do some renaming and try to unify things.

We get rid of the leading underscore if this is just a regular
(internal) helper. But not for example from _nm_strv_find_first(),
because that is the implementation of nm_strv_find_first().

  - _nm_utils_strv_cleanup()                 -> nm_strv_cleanup()
  - _nm_utils_strv_cleanup_const()           -> nm_strv_cleanup_const()
  - _nm_utils_strv_cmp_n()                   -> _nm_strv_cmp_n()
  - _nm_utils_strv_dup()                     -> _nm_strv_dup()
  - _nm_utils_strv_dup_packed()              -> _nm_strv_dup_packed()
  - _nm_utils_strv_find_first()              -> _nm_strv_find_first()
  - _nm_utils_strv_sort()                    -> _nm_strv_sort()
  - _nm_utils_strv_to_ptrarray()             -> nm_strv_to_ptrarray()
  - _nm_utils_strv_to_slist()                -> nm_strv_to_gslist()
  - nm_utils_strv_cmp_n()                    -> nm_strv_cmp_n()
  - nm_utils_strv_dup()                      -> nm_strv_dup()
  - nm_utils_strv_dup_packed()               -> nm_strv_dup_packed()
  - nm_utils_strv_dup_shallow_maybe_a()      -> nm_strv_dup_shallow_maybe_a()
  - nm_utils_strv_equal()                    -> nm_strv_equal()
  - nm_utils_strv_find_binary_search()       -> nm_strv_find_binary_search()
  - nm_utils_strv_find_first()               -> nm_strv_find_first()
  - nm_utils_strv_make_deep_copied()         -> nm_strv_make_deep_copied()
  - nm_utils_strv_make_deep_copied_n()       -> nm_strv_make_deep_copied_n()
  - nm_utils_strv_make_deep_copied_nonnull() -> nm_strv_make_deep_copied_nonnull()
  - nm_utils_strv_sort()                     -> nm_strv_sort()

Note that no names are swapped and none of the new names existed
previously. That means, all the new names are really new, which
simplifies to find errors due to this larger refactoring. E.g. if
you backport a patch from after this change to an old branch, you'll
get a compiler error and notice that something is missing.
2021-07-29 10:26:50 +02:00
Thomas Haller
3775f4395a
all: drop unnecessary casts from nm_utils_strv_find_first()
And, where the argument is a GPtrArray, use
nm_strv_ptrarray_find_first() instead.
2021-07-29 09:33:50 +02:00
Thomas Haller
ac36e48d68
glib-aux: accept any strv pointer at nm_utils_strv_find_first() via NM_CAST_STRV_CC() macro
We commonly have strv arrays as (char **), (const char*const*) or
(const char **). We thus need to frequently cast the argument to
nm_utils_strv_find_first().

Explicit casts in C don't make the code more typesafe, because
they silently allow completely wrong casts too. On the other hand,
changing the function argument to (const void *) also allows any
pointer, and not just strv pointers.

NM_CAST_STRV_CC() casts the the pointer to a (const char*const*)
strv pointer. It uses _Generic() to only cast a string array, and
not completely unrelated pointers.

As such, it is more convenient to use, as it requires the user no longer
to cast the strv argument, while still being strict about which types
are accepted.
2021-07-29 09:33:50 +02:00
Thomas Haller
f0ec3d5a56
glib-aux: add _nm_utils_strv_cleanup_const() helper 2021-07-28 23:32:47 +02:00
Thomas Haller
e2c5634ecd
glib-aux: add nm_strv_is_same_unordered() helper 2021-07-28 22:36:13 +02:00
Beniamino Galvani
bace14fe1f core: introduce device 'allowed-connections' property
Configuration can have [device*] and [connection*] settings and both
can include a 'match-device=' key, which is a list of device-specs.

Introduce a new 'allowed-connections' key for [device*] sections,
which specifies a list of connection-specs to indicate which
connections can be activated on the device.

With this, it becomes possible to have a device configuration like:

  [device-enp1s0]
  match-device=interface-name:enp1s0
  allowed-connections=except:origin:nm-initrd-generator

so that NM in the real root ignores connections created by the
nm-initrd-generator, and starts activating a persistent
connection. This requires also setting 'keep-configuration=no' to not
generate an assumed connection.
2021-07-27 17:43:45 +02:00
Thomas Haller
a99ac7ccd8
glib-aux: add nm_g_idle_add()
g_idle_add() is discouraged, and the checkpatch.pl script warns
about it.

Sometimes there is a legitimate use of it, when you want to always
schedule an idle action (without intent to cancel or track it). That
makes more sense for g_idle_add() than it does for g_timeout_add(),
because a timeout really should be tracked and cancelled if necessary.

Add a wrapper to rename the legitimate uses. This way, we can avoid the
checkpatch.pl warnings, and can grep for the remaining illegitimate uses.
2021-07-26 15:30:04 +02:00
Thomas Haller
abf5d01583
glib-aux: add NM_G_PARAM_SPEC_GET_DEFAULT_INT() helper 2021-07-23 17:02:01 +02:00
Thomas Haller
09daf5dd92
glib-aux: add nm_g_unix_signal_add_source() helper 2021-07-19 09:25:42 +02:00
Thomas Haller
4b21056fde
glib-aux: also backslash escape ASCII DEL character in nm_utils_buf_utf8safe_escape() 2021-07-19 08:59:34 +02:00
Thomas Haller
5b6005d06e
glib-aux: add nm_ascii_is_ctrl() helper (and similar)
These functions have overlap with g_ascii_is*() functions.

However g_ascii_is*() (and the is* functions from <ctype.h>) are
always confusing to me, in the sense that it's not clearly stated
which characters qualify for a certain category. And review is not
easy either, because they are implemented via a table lookup.

E.g. were you aware that 127 is considered g_ascii_iscntrl()? Probably
you were, but it's not clear to see that anywhere.

The main point of our own functions is to have is easier to see how
characters get categorized, by using comparison instead of table lookup.

Also, several existing code did in fact not use the g_ascii_is*()
macros, possibly because of the (perceived) difficulty to understand
their exact meaning. As a consequence, several checks got wrong.

For example, (ch < ' ') is not a valid check for testing whether
the character is a ASCII control character, for two reasons:

 - if char is a signed type (as likely it is), then this also evaluates
   to TRUE for all non-ASCII, UTF-8 characters that are greater than
   127.

 - it does not consider DEL character (127) a control character.
2021-07-19 08:59:33 +02:00
Thomas Haller
4e109bacab
clang-format: use "IndentPPDirectives:None" instead of "BeforeHash"
Subjectively, I think this looks better.
2021-07-09 08:49:06 +02:00
Thomas Haller
0c4a65929c
glib-aux: use nm_utils_gettid() instead of gettid()
I missed that we already have a gettid() wrapper. Drop the duplicated
again and use nm_utils_gettid().

Fixes: e874c5bf6b ('random: Provide missing gettid() declaration')
2021-07-08 08:31:47 +02:00
Lukasz Majewski
e874c5bf6b
random: Provide missing gettid() declaration
Provide missing declaration of gettid() function necessary for entropy
generation for hash table in the _rand_init_seed() function in the
src/libnm-glib-aux/nm-random-utils.c.

Fixes: 3649efe2b5 ('glib-aux: put more effort into seeding GRand fallback for nm_utils_random_bytes()')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/754

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/918
2021-07-08 08:16:27 +02:00
Thomas Haller
bec4a40437
glib-aux: add nm_utils_thread_local_register_destroy() helper
_nm_thread_local is very neat, but when we allocate resources
we need to make sure that they are destroyed when the thread
exits.

We can use pthread_setspecific() for that, but using it is cumbersome.
Add a helper function to make that simpler.

Also, the number of possible pthread_key_t keys is limited. With this
way, we only need one key in total.
2021-07-05 14:51:27 +02:00
Thomas Haller
05aa751957
core,glib-aux: move nm_hostname_manager_validate_hostname() to shared-utils
This function is badly named, because it has no NMHostnameManager self
argument. It's just a simple function that entirely operates on a string
argument.

Move it away from "nm-hostname-manager.h" to "libnm-glib-aux/nm-shared-utils.h".

Hostname handling is complicated enough. Simple string validation
functions should not obscure the view on the complicated parts.
2021-06-28 14:32:05 +02:00
Thomas Haller
34f48a33f3
glib-aux: add nm_g_unix_fd_add_source() helper 2021-06-28 13:31:35 +02:00
Thomas Haller
5e448f2339
glib-aux/trivial: add code comment to nm_g_source_destroy_and_unref() about g_source_destroy()
I think this is non-obvious API, and should be pointed out. As we don't
really have a good place for this comment, the place is a bit unmotivated.
Still, add a comment.
2021-06-28 13:31:32 +02:00
Thomas Haller
3706118ab1
glib-aux/trivial: fix spelling error in code comment 2021-06-28 13:31:32 +02:00
Thomas Haller
87229b22ae
glib-aux: add NM_G_PARAM_SPEC_GET_DEFAULT_STRING() helper 2021-06-23 12:13:38 +02:00
Thomas Haller
e2defd0115
glib-aux: add nm_g_variant_singleton_s_empty() helper 2021-06-23 12:13:38 +02:00
Thomas Haller
edb31252cc
glib-aux: add nm_g_variant_singleton_b() helper 2021-06-23 12:13:27 +02:00
Thomas Haller
c6559b04c2
std-aux/glib-aux: minor cleanup of nm_ip_addr_*()/nm_utils_addr_family_*()
- let nm_ip_addr_is_null() work for unaligned addresses.

- drop redundant nm_assert_addr_family().

- move code around.
2021-06-22 09:47:44 +02:00
Thomas Haller
92daaff7d1
glib-aux: add NM_STRV_EMPTY_CC() helper macro 2021-06-17 17:48:15 +02:00
Thomas Haller
f51d3862f9
glib-aux: add nm_utils_ptrarray_is_sorted() helper 2021-06-17 17:48:12 +02:00
Beniamino Galvani
517852dccd libnm-glib-aux: add nm_utils_get_process_exit_status_desc() 2021-06-11 21:43:11 +02:00
Thomas Haller
5ccc5e10b9 shared: add nm_ip_addr_init() helper 2021-06-11 21:43:11 +02:00
Thomas Haller
17be7ea72e
glib-aux: mark result of nm_g_slice_free_fcn() as warn_unused_result
It's tempting to mix up

    nm_g_slice_free(arg)

with

    nm_g_slice_free_fcn(arg)

Use __attribute((__warn_unused_result__)) to catch such bugs.
2021-06-10 11:08:58 +02:00
Thomas Haller
5abf61f53a
glib-aux: fix formatting for _nm_g_slice_free_fcn_define() by adding semicolon 2021-06-10 11:08:57 +02:00
Thomas Haller
f98d47f77d
glib-aux: implement varidic macros NM_UTILS_{LOOKUP,ENUM2STR}_DEFINE() with NM_VA_ARGS_JOIN()
The previous implementation of these macros simply relied on the
__VA_ARGS__ to be expended and joined with ','. That make that work
inside the switch statement, the macros expanded to

   switch (val) {
       (void) 0, (void) 0;
   case 0x1:
       s = " ""value" "";
       break;
       (void) 0, (void) 0;
   };

Those NOP statements cause lgtm.com to complain "Dead code due to goto
or break statement".

Implement these macros differently using NM_VA_ARGS_JOIN().
2021-06-08 08:24:11 +02:00
Thomas Haller
8c6be1909f
glib-aux: add nm_strvarray_get_strv_non_empty_dup() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
851267b6e7
glib-aux: add nm_strvarray_find_first() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
b0acbe504f
glib-aux: add nm_strvarray_get_idx() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
a266bc15b2
glib-aux: add nm_strv_has_duplicate() helper 2021-06-04 09:29:21 +02:00
Thomas Haller
9a0f3f3e09
glib-aux: add nm_utils_strv_dup_shallow_maybe_a() helper 2021-06-04 09:29:12 +02:00
Thomas Haller
ecd5d07b3a
glib-aux: move forward declaration of inet_ntop() out of function scope
lgtm.com doesn't like this:

  Query pack:com.lgtm/cpp-queries
  Query ID:cpp/function-in-block

  Functions should always be declared at file scope. It is confusing
  to declare a function at block scope, and the visibility of the function
  is not what would be expected.
2021-06-01 17:32:33 +02:00