Commit Graph

31 Commits

Author SHA1 Message Date
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
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
ec868916c8 shared: move nm_utils_ip._address_clear_host_address() helpers to shared 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
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
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
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
e688e70b37 shared: add nm_utils_hash_values_to_array() helper 2019-09-22 16:05:50 +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
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
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
b4fe51b5fa shared: add nm_utils_strv_dup() util 2019-06-28 16:48:17 +02:00
Thomas Haller
ec707f56c1 shared: add nm_utils_hashtable_same_keys() util 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
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
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
dadd38c484 shared: add nm_utils_strv_make_deep_copied_n() helper 2019-05-23 18:09:49 +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
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