Commit Graph

22377 Commits

Author SHA1 Message Date
Thomas Haller
2fa7a7c20b shared: make nm_streq() and nm_streq0() inline functions
There is no advantage in having these as macros. Make them
inline functions, compiler should be able to decide that they
are in fact inlinable.

Also, don't call g_strcmp0() for nm_streq0(). It means we first
have to call glib function, only to call a glibc function. No need
for this abstraction.
2019-02-13 16:03:23 +01:00
Thomas Haller
4fab0d09a5 shared: add NM_STR_HAS_SUFFIX()
Contrary to g_str_has_suffix(), it exploits the fact the the suffix length
is known at compile time. No need to call a glib function, to find out what
we already know, to call strcmp().

Instead just calculate the string length and call memcmp().
2019-02-13 16:03:23 +01:00
Thomas Haller
d216e2f305 libnm: fix usage of nm_streq() macro
Yes, C has a preprocessor and nm_streq() currently is a macro.

Still, macros should very much behave like regular functions.
For example, no unexpected side-effects aside what a regular function
would have, evaluating all arguments exactly once, or no side-effects
w.r.t. the order in which arguments are evaluated.

In some cases, we deviate from that for good reasons. For example
NM_IN_SET() may not evaluate all arguments. _LOGD() may not evaluate
any arguments, and NM_UTILS_LOOKUP_STR_DEFINE() is not a function-like
macro at all.

Still, that is not the case here. We avoid to misuse macros to write
code that does not look like C.
2019-02-13 16:03:23 +01:00
Thomas Haller
2c881b8064 wifi-p2p: merge branch 'th/wifi-p2p-wait-supplicant-fix'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/80
2019-02-13 16:01:12 +01:00
Thomas Haller
27169047c5 wifi-p2p: add FIXME comment for handling group_owner in NMDeviceWifiP2P 2019-02-13 16:01:03 +01:00
Thomas Haller
3c989f30d6 wifi-p2p: rework setting pending action waiting for supplicant
Previously, we might have a pending action 'waiting-for-supplicant'
registered, although the device was not waiting:

    <info>  [1549611177.5815] device (wlan0): supplicant interface state: starting -> ready
    <debug> [1549611177.5816] device[0x55d1781ae5d0] (p2p-dev-wlan0): P2P: Releasing WPA supplicant interfaces.
    <debug> [1549611177.5816] device[0x55d1781ae5d0] (p2p-dev-wlan0): P2P: WPA supplicant management interface changed to /fi/w1/wpa_supplicant1/Interfaces/1.
    <trace> [1549611177.5816] device[0x55d1781ae5d0] (p2p-dev-wlan0): remove_pending_action (0): 'waiting-for-supplicant' not pending (expected)
    <debug> [1549611177.5816] device[0x55d1781ae5d0] (p2p-dev-wlan0): constructed (NMDeviceWifiP2P)
    <debug> [1549611177.5816] device[0x55d1781ae5d0] (p2p-dev-wlan0): add_pending_action (1): 'waiting-for-supplicant'

The previous commit already fixed this bug by dropping the constructor
property for NM_DEVICE_WIFI_P2P_MGMT_IFACE.

Still, refactor handling of pending actions to keep track of whether we
have a pending action registered.
2019-02-13 15:54:45 +01:00
Thomas Haller
75741ef5c8 wifi-p2p: drop constructor property NM_DEVICE_WIFI_P2P_MGMT_IFACE
We already have a setter function nm_device_wifi_p2p_set_mgmt_iface()
as we may need to change the mgmt-iface later on. Use that to set the
supplicant interface instead of a constructor property.

That makes the object creation simpler, because nothing noteworthy
happens, until the very last statement in constructed() to add the
pending action.
2019-02-13 15:52:13 +01:00
Thomas Haller
5c7a9f65b0 wifi-p2p: don't use g_signal_connect_object()
We already explicitly take care of the lifetime of mgmt_iface and
disconnect all signal handlers. No need to register an additional
weak-reference.
2019-02-13 15:52:06 +01:00
scootergrisen
e390cc0529 po: update Danish (da) translation
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/82
2019-02-13 08:29:42 +01:00
Thomas Haller
128099151d shared: fix nm_errno_from_native() for negative input
Fixes: 67130e6706
2019-02-12 09:13:29 +01:00
Thomas Haller
5d9a2d9168 all: merge branch 'th/errno'
https://github.com/NetworkManager/NetworkManager/pull/292
2019-02-12 08:50:39 +01:00
Thomas Haller
d83d5f1da2 shared: use nm_strerror_native_r() in lower layers
Subsequent calls to nm_strerror_native() overwrite the previous
buffer. That is potentially dangerious. At least functions in
shared/nm-utils (which are lower-layer utilities) should not do
that and instead use a stack-local buffer. That is because these
functions should not make assumptions about the way they are called.

On the other end, nmcli passing the return-value of nm_strerror_native()
to g_print() is clearly OK because the higher layers are in control of
when the call nm_strerror_native() -- by relying that lower layers don't
interfere.
2019-02-12 08:50:28 +01:00
Thomas Haller
2b630bc22e systemd: define strerror() in sd-adapt header to nm_strerror_native()
Systemd uses strerror() extensively. Patch the function to use the thread-safe
nm_strerror_native().
2019-02-12 08:50:28 +01:00
Thomas Haller
9beed4f661 all: replace strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
a4fb6ddfca all: replace g_strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
737ab51472 all: include "nm-utils/nm-errno.h" via "nm-default.h" 2019-02-12 08:50:28 +01:00
Thomas Haller
e1ca3bf7ed shared: add nm_strerror_native() to replace strerror() and g_strerror()
We have various options for strerror(), with ups and downsides:

- strerror()

    - returns pointer that is overwritten on next call. It's convenient
      to use, but dangerous.

    - not thread-safe.

    - not guaranteed to be UTF-8.

- strerror_r()

    - takes input buffer and is less convenient to use. At least, we
      are in control of when the buffer gets overwritten.

    - there is a Posix/XSI and a glibc variant, making it sligthly
      inconvenient to used. This could be solved by a wrapper we implement.

    - thread-safe.

    - not guaranteed to be UTF-8.

- g_strerror()

    - convenient and safe to use. Also the buffer is never released for the
      remainder of the program.

    - passing untrusted error numbers to g_strerror() can result in a
      denial of service, as the internal buffer grows until out-of-memory.

    - thread-safe.

    - guaranteed to be UTF-8 (depending on locale).

Add our own wrapper nm_strerror_native(). It is:

    - convenient to use (returning a buffer that does not require
      management).

    - slightly dangerous as the buffer gets overwritten on the next call
      (like strerror()).

    - thread-safe.

    - guaranteed to be UTF-8 (depending on locale).

    - doesn't keep an unlimited cache of strings, unlike g_strerror().

You can't have it all. g_strerror() is leaking all generated error messages.
I think that is unacceptable, because it would mean we need to
keep track where our error numbers come from (and trust libraries we
use to only set a restricted set of known error numbers).
2019-02-12 08:50:28 +01:00
Thomas Haller
4d9918aac2 all: assert that native errno numbers are positive
Use the NM_ERRNO_NATIVE() macro that asserts that these errno numbers are
indeed positive. Using the macro also serves as a documentation of what
the meaning of these numbers is.

That is often not obvious, whether we have an nm_errno(), an nm_errno_native()
(from <errno.h>), or another error number (e.g. WaitForNlResponseResult). This
situation already improved by merging netlink error codes (nle),
NMPlatformError enum and <errno.h> as nm_errno(). But we still must
always be careful about not to mix error codes from different
domains or transform them appropriately (like nm_errno_from_native()).
2019-02-12 08:50:28 +01:00
Thomas Haller
67130e6706 shared: cleanup separation and transition between errno and nmerr numbers
The native error numbers (from <errno.h>) and our nmerr extention on top
of them are almost the same. But there are peculiarities.

Both errno and nmerr must be positive values. That is because some API
(systemd) like to return negative error codes. So, a positive errno and
its negative counter part indicate the same error. We need normalization
functions that make an error number positive (these are nm_errno() and
nm_errno_native()).

This means, G_MININT needs special treatment, because it cannot be
represented as a positive integer. Also, zero needs special
treatment, because we want to encode an error, and zero already encodes
no-error. Take care of these special cases.

On top of that, nmerr reserves a range within native error numbers for
NetworkManager specific failure codes. So we need to transition from native
numbers to nmerr numbers via nm_errno_from_native().

Take better care of some special cases and clean them up.

Also add NM_ERRNO_NATIVE() macro. While nm_errno_native() coerces a
value in the suitable range, NM_ERRNO_NATIVE() asserts that the number
is already positive (and returns it as-is). It's use is only for
asserting and implicitly documenting the requirements we have on the
number passed to it.
2019-02-12 08:50:28 +01:00
Thomas Haller
89d3c5242b shared: fix nm_errno_from_native() for negative values
We first need to map negative values to their positive form,
and then do the check for the reserved range.

Fixes: 18732c3493
2019-02-12 08:50:28 +01:00
Thomas Haller
047998f80a all: cache errno in local variable before using it 2019-02-12 08:50:28 +01:00
Thomas Haller
b7bb744973 libnm,core: use _nm_utils_ascii_str_to_uint64() instead of strtol()
Using strtol() correctly proves to be hard.

Usually, we want to also check that the end pointer is points to the end
of the string. Othewise, we silently accept trailing garbage.
2019-02-12 08:50:28 +01:00
Thomas Haller
a3370af3a8 all: drop unnecessary includes of <errno.h> and <string.h>
"nm-macros-interal.h" already includes <errno.h> and <string.h>.
No need to include it everywhere else too.
2019-02-12 08:50:28 +01:00
Thomas Haller
32a847a61f systemd/trivial: adjust naming of include guard define 2019-02-12 08:50:28 +01:00
Thomas Haller
65884733ec all: minor coding style fixes (space before parentheses) 2019-02-11 15:22:57 +01:00
Beniamino Galvani
3a0f7114fe platform: limit the maximum size of sysctl cache
When the logging level is DEBUG or TRACE, we keep all the sysctl
values we read in a cache to log how they change. Currently there is
no limit on the size of this cache and it can take a large amount of
memory.

Implement a LRU cache where the oldest entries are deleted to make
space for new ones.

https://github.com/NetworkManager/NetworkManager/pull/294
2019-02-10 10:38:54 +01:00
Thomas Haller
fb1baa636b build,ci: merge branch 'th/missing-braces-warning'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/81
2019-02-09 08:53:06 +01:00
Thomas Haller
668dc1cd02 core: use NM_CMP_*() macro in route_compare()
nm_ip_route_get_prefix() and plen are guint type, hence the following
is not correct:

    plen = nm_ip_route_get_prefix (route1);
    r = plen - nm_ip_route_get_prefix (route2);
    if (r)
         return r > 0 ? 1 : -1;

Use the macro, it gets subtle cases like this right.

Fixes: b32bb36c61
2019-02-09 07:14:32 +01:00
Thomas Haller
167142c38e build/meson: build with c_std=gnu11
For autotools, we already updated to C11 (gnu11) with commit
066357aa47 ("build: bump C standard
to (gcc's) C11").
2019-02-09 07:14:32 +01:00
Thomas Haller
557271fa6a gitlab-ci: rename build-jobs in gitlab-ci
It just looks nicer.
2019-02-09 07:14:32 +01:00
Thomas Haller
53aac0233c build/ci: enable --werror for meson builds
This enables -Werror for meson builds on gitlab-ci and semaphore.
Not on Travis, the compiler there is too old, giving too many bogus
warnings.

This reverts commit 928d68d04a ("m4:
disable -Wmissing-braces for newer clang").
2019-02-08 20:14:50 +01:00
Thomas Haller
ff5944ce03 contrib/rpm: enable warnings and fatal-warnings with meson build 2019-02-08 20:14:50 +01:00
Thomas Haller
c537e5fd25 build: re-enable "-Wmissing-braces" warning
We should always get the nesting in struct initializers right.
Everyhing else is error-prone, and the warning is good.

Enable it.
2019-02-08 20:14:50 +01:00
Thomas Haller
c236dc161d device: avoid "-Wmissing-braces" warning for initializing "struct in6_addr"
The right way is IN6_ADDR_INIT_ANY.

While at it, don't initialize multiple variables in the same line.

    ../src/devices/nm-device-ip-tunnel.c:153:29: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]
            struct in6_addr local6 = { 0 }, remote6 = { 0 };
                                       ^
                                       {}
2019-02-08 20:14:50 +01:00
Thomas Haller
3458c02acb platform/tests: avoid "-Wmissing-braces" warning in "test-route.c" 2019-02-08 20:14:50 +01:00
Thomas Haller
395174f659 shared: avoid "-Wmissing-braces" warning initalizing NMIPAddr
NMIPAddr contains an unnamed union. We have to either explicitly
initialize one field, or omit it.

    ../shared/nm-utils/nm-shared-utils.c:38:36: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]
    const NMIPAddr nm_ip_addr_zero = { 0 };
                                       ^
                                       {}
2019-02-08 20:14:50 +01:00
Thomas Haller
814bcf5575 libnm: avoid "-Wmissing-braces" warning for intializing NMUuid 2019-02-08 20:14:50 +01:00
Thomas Haller
fcb7001302 cli: avoid "-Wmissing-braces" warning for INT_VALUE_INFOS() and ENUM_VALUE_INFOS()
Also fix indentations and enforce that each block is terminated by a
trailing comma.
2019-02-08 20:14:50 +01:00
Thomas Haller
a44d276f55 libnm: avoid "-Wmissing-braces" warning for test-link.c
../src/platform/tests/test-link.c: In function ‘_test_wireguard_change’:
    ../src/platform/tests/test-link.c:861:16: warning: missing braces around initializer [-Wmissing-braces]
         endpoint = (NMSockAddrUnion) {
                    ^
    ../src/platform/tests/test-link.c:864:21:
           .sin_addr   = nmtst_inet4_from_string (nm_sprintf_buf (s_addr, "192.168.7.%d", i)),
                         {                                                                   }
    ../src/platform/tests/test-link.c:861:16: warning: missing braces around initializer [-Wmissing-braces]
         endpoint = (NMSockAddrUnion) {
                    ^
    ../src/platform/tests/test-link.c:864:21:
           .sin_addr   = nmtst_inet4_from_string (nm_sprintf_buf (s_addr, "192.168.7.%d", i)),
                         {
2019-02-08 20:14:50 +01:00
Thomas Haller
ce8b053674 libnm: avoid "-Wmissing-braces" warning for teams _prop_to_keys array
[1/384] Compiling C object 'libnm-core/2b1af02@@nm-core@sta/nm-setting-team.c.o'.
    ../libnm-core/nm-setting-team.c:649:77: warning: missing braces around initializer [-Wmissing-braces]
     static const _NMUtilsTeamPropertyKeys _prop_to_keys[_PROPERTY_ENUMS_LAST] = {
                                                                                 ^
      [PROP_CONFIG] =                      { NULL, NULL, NULL, 0 },
                                                               { }

And use designated initializers.
2019-02-08 20:14:50 +01:00
Thomas Haller
4f931a1920 tests: avoid "-Wmissing-braces" warning in test_nm_utils_dhcp_client_id_systemd_node_specific()
[1/2] Compiling C object 'src/tests/a4ccf2d@@test-general@exe/test-general.c.o'.
    ../src/tests/test-general.c: In function ‘test_nm_utils_dhcp_client_id_systemd_node_specific’:
    ../src/tests/test-general.c:2056:16: warning: missing braces around initializer [-Wmissing-braces]
      } d_array[] = {
                    ^
    ../src/tests/test-general.c:2058:20:
        .machine_id = { 0xcb, 0xc2, 0x2e, 0x47, 0x41, 0x8e, 0x40, 0x2a, 0xa7, 0xb3, 0x0d, 0xea, 0x92, 0x83, 0x94, 0xef },
                        {
2019-02-08 20:14:50 +01:00
Thomas Haller
6eaf52a509 wifi/iwd: avoid "-Wstrict-aliasing" warning in nm_device_iwd_set_dbus_object()
The cast is bogus and leads to a compiler warning:

    [424/583] Compiling C object src/devices/wifi/914a32e@@nm-device-plugin-wifi@sha/nm-device-iwd.c.o.
    In file included from ../shared/nm-default.h:293,
                     from ../src/devices/wifi/nm-device-iwd.c:21:
    ../src/devices/wifi/nm-device-iwd.c: In function ‘nm_device_iwd_set_dbus_object’:
    ../src/devices/wifi/nm-device-iwd.c:2404:28: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
      if (!nm_g_object_ref_set ((GObject **) &priv->dbus_obj, (GObject *) object))
    ../shared/nm-utils/nm-macros-internal.h:1048:13: note: in definition of macro ‘nm_g_object_ref_set’
       typeof (*(pp)) *const _pp = (pp); \
                 ^~
2019-02-08 20:14:50 +01:00
Thomas Haller
2510f60e92 cli: avoid "-Wduplicate-decl-specifier" warning in nmcli's resolve_color_alias()
[1/2] Compiling C object 'clients/cli/2641089@@nmcli@exe/nmcli.c.o'.
    ../clients/cli/nmcli.c: In function ‘resolve_color_alias’:
    ../clients/cli/nmcli.c:507:4: warning: duplicate ‘const’ declaration specifier [-Wduplicate-decl-specifier]
      } const aliases[] = {
        ^~~~~
2019-02-08 20:14:50 +01:00
Thomas Haller
2fe9ade10d tests: avoid "-Wduplicate-decl-specifier" warning in test_duplicate_decl_specifier()
The test should check the behavior with "const typeof(a)" in a macro,
where "a" itself is const. For that we don't need a double const
declaration of v2.

Also, that fixes an actual compiler warning:

    ../src/tests/test-general.c: In function ‘test_duplicate_decl_specifier’:
    ../src/tests/test-general.c:1669:8: warning: duplicate ‘const’ declaration specifier [-Wduplicate-decl-specifier]
      const const int v2 = 3;
            ^~~~~
2019-02-08 20:14:50 +01:00
Thomas Haller
7a8a4a5fa3 clients: avoid "-Wduplicate-decl-specifier" warning in array declarions in "nm-vpn-helpers.c"
[1/5] Compiling C object 'clients/common/913ef36@@nmc-base@sta/nm-vpn-helpers.c.o'.
    ../clients/common/nm-vpn-helpers.c: In function ‘nm_vpn_get_secret_names’:
    ../clients/common/nm-vpn-helpers.c:118:31: warning: duplicate ‘const’ declaration specifier [-Wduplicate-decl-specifier]
      static const VpnPasswordName const generic_vpn_secrets[] = {
                                   ^~~~~
2019-02-08 20:14:50 +01:00
Thomas Haller
9c3a59341f shared: fix static array declaration for _by_name in "nm-ethtool-utils.c"
[1/73] Compiling C object 'libnm-core/2b1af02@@nm-core@sta/.._shared_nm-ethtool-utils.c.o'.
    ../shared/nm-ethtool-utils.c:93:14: warning: duplicate 'const' declaration specifier [-Wduplicate-decl-specifier]
     const guint8 const _by_name[_NM_ETHTOOL_ID_NUM] = {
                  ^~~~~
2019-02-08 20:14:50 +01:00
Thomas Haller
7b18bd1fa8 build/meson: disable "-Wgnu-variable-sized-type-not-at-end warning"
It's not useful for us.

    In file included from ../src/systemd/src/libsystemd/sd-event/sd-event.c:14:
    ../src/systemd/src/libsystemd/sd-event/event-source.h:195:36: error: field 'buffer' with variable sized type 'union inotify_event_buffer' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
            union inotify_event_buffer buffer;
                                       ^
2019-02-08 20:14:50 +01:00
Thomas Haller
dc4f6f6656 gitlab-ci: fix moving docs-html
Seems .git directory is somehow shared/cached between CI runs.
Use /tmp instead.
2019-02-08 20:14:19 +01:00
Thomas Haller
aacb3c4f5b connectivity: merge branch 'th/connectivity-rp-filter' 2019-02-08 17:28:58 +01:00
Thomas Haller
983b430075 device: print warning when rp_filter is set to strict with connectivity checking 2019-02-08 16:34:18 +01:00