Commit graph

30336 commits

Author SHA1 Message Date
Gris Ge
7b07b513e1 merge: branch 'nm-1-38'
[nm-1.38] [RFE] dispatch `dns-change` dispatcher event

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1763
2023-10-19 08:52:52 +00:00
Gris Ge
410f73e7e6 dispatch dns-change dispatcher event
Introducing new dispatcher event -- `dns-change` which will be emitted when
DNS configuration changed(even in `dns=none` mode). This is to solve two
use cases:
 * Invoke dispatch script for DNS changes triggered by the global DNS
   API.

 * Do not invoke [OpenShift resolv-prepender][1] for non-DNS changes.

Bug reference: https://issues.redhat.com/browse/RHEL-1671

[1]: https://github.com/openshift/machine-config-operator/blob/master/templates/common/on-prem/files/resolv-prepender.yaml

Signed-off-by: Gris Ge <fge@redhat.com>
(cherry picked from commit a1db61ebc9)
(cherry picked from commit 3cdce71b95)
(cherry picked from commit 9ae535c61a)
(cherry picked from commit e74a349e1e)
2023-10-19 16:15:44 +08:00
Gris Ge
43523d60f3 emit DNS CONFIG_CHANGED signal even dns=none
Instruct the `NMDnsManager` to emit `CONFIG_CHANGED` signal even
`dns=none` or failed to modify `/etc/resolv.conf`.

The `NMPolicy` will only update hostname when DNS is managed.

Signed-off-by: Gris Ge <fge@redhat.com>
(cherry picked from commit a847ba8075)
(cherry picked from commit d10f20fd01)
(cherry picked from commit e0f3a91a95)
(cherry picked from commit cd9ebfd2bb)
2023-10-19 16:15:42 +08:00
Lubomir Rintel
16dc184845 cloud-setup: actually pass the HTTP method in nm_http_client_poll_req()
https://bugzilla.redhat.com/show_bug.cgi?id=2179718

Fixes: 8b7e12c2d6 ('cloud-setup/ec2: start with requesting a IMDSv2 token')
Fixes: cd74d75002 ('cloud-setup: make nm_http_client_req() accept a method argument')
(cherry picked from commit f07da04cd9)
(cherry picked from commit d787c0c59d)
(cherry picked from commit 6abbdaaa64)
2023-09-15 08:40:17 +02:00
Lubomir Rintel
c5a3e739b1 cloud-setup/ec2: start with requesting a IMDSv2 token
The present version of the EC2 metadata API (IMDSv2) requires a header
with a token to be present in all requests. The token is essentially a
cookie that's not actually a cookie that's obtained with a PUT call that
doesn't put anything. Apparently it's too easy to trick someone into
calling a GET method.

EC2 now supports IMDSv2 everywhere with IMDSv1 being optional, so let's
just use IMDSv2 unconditionally. Also, the presence of a token API can
be used to detect the AWS EC2 cloud.

https://bugzilla.redhat.com/show_bug.cgi?id=2151986
(cherry picked from commit 8b7e12c2d6)
(cherry picked from commit 429f36cd81)
(cherry picked from commit e3ac982b32)
2023-09-15 08:40:07 +02:00
Lubomir Rintel
3d94f4fdf9 cloud-setup: make nm_http_client_req() accept a method argument
We'll need to be able to issue PUT calls.

(cherry picked from commit cd74d75002)
(cherry picked from commit eff4372045)
(cherry picked from commit aaf66e9174)
2023-09-15 08:39:48 +02:00
Lubomir Rintel
9ce530fa7a cloud-setup: rename get/Get identifiers to req and Req
We're going to extend those to issue methods other than GET.
Also, "request" would've been too long, "req" looks nicer.

(cherry picked from commit 85ce088616)
(cherry picked from commit 6e8cfbae32)
(cherry picked from commit 20cd11ee49)
2023-09-15 08:36:49 +02:00
Thomas Haller
d14dc95be3 cloud-setup: use nm_strv_dup_packed() in nm_http_client_poll_get()
No need to do a deep clone. The strv array is not ever modified and we
pack it together in one memory allocation.

(cherry picked from commit 599fe234ea)
(cherry picked from commit 3787eacac9)
(cherry picked from commit 89a6ce575d)
2023-09-15 08:29:08 +02:00
Lubomir Rintel
d83537bff5 cloud_setup: unexport nm_http_client_get()
It's not used anywhere.

(cherry picked from commit ce225b2c06)
(cherry picked from commit 23b9514080)
(cherry picked from commit 36d417af60)
2023-09-15 08:27:59 +02:00
Thomas Haller
4b3a58014b libnm/tests: adjust test for glib change in 2.75.0 for assertion warning
See-also: 0ffe86a1f7
(cherry picked from commit 861fc6854c)
(cherry picked from commit c5917b0a50)
(cherry picked from commit 66d1039520)
2023-07-04 20:00:12 +02:00
Thomas Haller
ec140846aa libnm/tests: avoid uninitialized variable warning in _invalid_option_write_handler()
src/libnm-core-impl/tests/test-keyfile.c: In function '_invalid_option_write_handler':
  src/libnm-core-impl/tests/test-keyfile.c:917:9: error: 'message' may be used uninitialized [-Werror=maybe-uninitialized]
    917 |         g_assert(message && strstr(message, "ethtool.bogus"));
        |         ^
  src/libnm-core-impl/tests/test-keyfile.c:905:29: note: 'message' was declared here
    905 |     const char             *message;
        |                             ^
  lto1: all warnings being treated as errors

(cherry picked from commit 8d6349156b)
(cherry picked from commit a71e51e712)
(cherry picked from commit 0510e6f57d)
2023-07-04 19:00:14 +02:00
Thomas Haller
fa4de663bb platform/tests: skip test_netns_bind_to_path() test on failure
Our copr builds start to fail, since the copr builds updated to Fedora
38 ([1]).

  ERROR: src/core/platform/tests/test-link-linux - Bail out! nm:ERROR:src/core/platform/tests/test-link.c:3486:test_netns_bind_to_path: assertion failed (nmtstp_run_command("ip netns exec " P_NETNS_BINDNAME " true") == 0): (65280 == 0)

The cause is not understood, but it seems not worth investigating.
Just skip the test.

[1] https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/KOR3HE2VHHIPDBLDLXTYRMON6JQXCHMW/#J4K5VB5SA6I5P2ZLI65OHNQ6X7SINSHA

(cherry picked from commit 8dfca3d552)
(cherry picked from commit e06432cc7f)
(cherry picked from commit 533310ca09)
2023-07-04 18:17:39 +02:00
Thomas Haller
01134c6fe8 platform/tests: check errno on failure to umount() in test_netns_bind_to_path()
The umount() call failed in a test. Rework the assertion, so
we might see the errno of the problem.

(cherry picked from commit 1a0fa85397)
(cherry picked from commit c66ae93415)
(cherry picked from commit edf23f434e)
2023-07-04 18:17:32 +02:00
Beniamino Galvani
d61196451d dns: fix tracking of best ip config
When a IP configuration has type "best", it is the configuration with
the best (lowest) metric default route for the specific address
family. Therefore, there can be only one best configuration for
address family.

When a new configuration is added as best, make sure it is the only
one for the address family.

This reverts commit 0abc14b3a0.

Fixes: 0abc14b3a0 ('core: remove unused best_ip_config_[46] field in NMDnsManager')

https://lists.freedesktop.org/archives/networkmanager/2023-June/000123.html
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/1331
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1683
(cherry picked from commit 646e041523)
(cherry picked from commit 2ddb4e5942)
(cherry picked from commit 9cb9dfa77b)
2023-06-29 15:24:12 +02:00
Gris Ge
32d2e3c14b setting-connection: Unblock autoconnect upon finish of Reapply
The activation of a connection will clear the block of autoconnect,
we should do the same for reapply.

Signed-off-by: Gris Ge <fge@redhat.com>
(cherry picked from commit 0486efd358)
(cherry picked from commit 18ce5f43bd)
(cherry picked from commit 2695396939)
2023-06-27 19:59:53 +08:00
Thomas Haller
b2b49df992
core: fix enum argument in prototype of nm_utils_kill_process_sync(), etc.
This avoids a new compiler warning with gcc 13.0.0-0.9.fc38:

  ../src/core/nm-core-utils.c:482:1: error: conflicting types for 'nm_utils_kill_child_async' due to enum/integer mismatch; have 'void(pid_t,  int,  NMLogDomain,  const char *, guint32,  void (*)(pid_t,  gboolean,  int,  void *), void *)' {aka 'void(int,  int,  NMLogDomain,  const char *, unsigned int,  void (*)(int,  int,  int,  void *), void *)'} [-Werror=enum-int-mismatch]
    482 | nm_utils_kill_child_async(pid_t                   pid,
        | ^~~~~~~~~~~~~~~~~~~~~~~~~
  In file included from ../src/core/nm-core-utils.c:9:

Fixes: 067202b34e ('core: use explict NMLogDomain enum instead of int')
(cherry picked from commit a60476b27f)
(cherry picked from commit 18047f09d4)
2023-05-25 23:25:25 +02:00
Thomas Haller
309cc0e2f2
curl: use CURLOPT_PROTOCOLS_STR instead of deprecated CURLOPT_PROTOCOLS
CURLOPT_PROTOCOLS [0] was deprecated in libcurl 7.85.0 with
CURLOPT_PROTOCOLS_STR [1] as a replacement.

Well, technically it was only deprecated in 7.87.0, and retroactively
marked as deprecated since 7.85.0 [2]. But CURLOPT_PROTOCOLS_STR exists
since 7.85.0, so that's what we want to use.

This causes compiler warnings and build errors:

  ../src/core/nm-connectivity.c: In function 'do_curl_request':
  ../src/core/nm-connectivity.c:770:5: error: 'CURLOPT_PROTOCOLS' is deprecated: since 7.85.0. Use CURLOPT_PROTOCOLS_STR [-Werror=deprecated-declarations]
    770 |     curl_easy_setopt(ehandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
        |     ^~~~~~~~~~~~~~~~
  In file included from ../src/core/nm-connectivity.c:13:
  /usr/include/curl/curl.h:1749:3: note: declared here
   1749 |   CURLOPTDEPRECATED(CURLOPT_PROTOCOLS, CURLOPTTYPE_LONG, 181,
        |   ^~~~~~~~~~~~~~~~~

This patch is largely taken from systemd patch [2].

Based-on-patch-by: Frantisek Sumsal <frantisek@sumsal.cz>

[0] https://curl.se/libcurl/c/CURLOPT_PROTOCOLS.html
[1] https://curl.se/libcurl/c/CURLOPT_PROTOCOLS_STR.html
[2] 6967571bf2
[3] e61a4c0b7c

Fixes: 7a1734926a ('connectivity,cloud-setup: restrict curl protocols to HTTP and HTTPS')
(cherry picked from commit dabfea2fc2)
(cherry picked from commit 7122ef4007)
2023-05-25 23:23:43 +02:00
Beniamino Galvani
f61519167d ipv6ll: don't regenerate the address when it's removed externally
Currently if the IPv6 link-local address is removed after it passed
DAD, NetworkManager tries to generate a new link-local address. If
this fails, which is always the case for EUI64, ipv6ll is considered
as failed and the connection can go down (depending on may-fail).

This is particularly bad for virtual interfaces because if somebody
removes the link-local address, the activation can fail and destroy
the interface, breaking all services that require it. Also, it's a
change in behavior introduced in 1.36.0.

It seems that a better approach here is to re-add the address that was
removed externally.

[bgalvani@redhat.com: since the branch is missing commit 7ca95cee15
('platform: always reconfigure IP routes even if removed externally'),
we need to set flag NM_L3CFG_CONFIG_FLAGS_FORCE_ONCE when committing
the address, otherwise it's not re-added]

Fixes: aa070fb821 ('core: add NML3IPv6LL helper')
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1622
(cherry picked from commit 53ba9f4701)
(cherry picked from commit 2976e4c3b7)
(cherry picked from commit 4a13b5f522)
2023-05-23 22:01:01 +02:00
Beniamino Galvani
15236fcba6 core: fix l3cd comparison
NM_CMP_SELF(a, b) returns immediately if the objects are the same.

Fixes: cb29244552 ('core: support compare flags in nm_l3_config_data_cmp_full()')
Fixes-test: @dracut_NM_iSCSI_ibft_table

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1583
(cherry picked from commit 0a02995175)
(cherry picked from commit 5d95c20787)
(cherry picked from commit 1395171326)
2023-05-19 11:21:47 -04:00
Beniamino Galvani
cae8903abb device: stop activation when queueing the unmanaged state
When the unmanaged state is queued, we must ensure that the current
activation doesn't overwrite the queue stated with a new one. This can
happen for example if a dispatcher script or a firewall call
terminate, or if the next activation stage is dispatched.

Fixes-test: @preserve_master_and_ip_settings
https://bugzilla.redhat.com/show_bug.cgi?id=2178269
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1599
(cherry picked from commit 89a8f51235)
(cherry picked from commit b2ed01efc9)
(cherry picked from commit 5f9547dcc1)
2023-04-11 09:59:59 +02:00
Beniamino Galvani
115634d3b1 device: skip DNS resolution for tentative IPv6 addresses
A tentative IPv6 address can still fail DAD, so don't use it to
resolve the hostname via DNS. Furthermore, tentative addresses can't
be used to contact the nameserver and so the resolution will fail if
there is no other valid IPv6 address. Wait that the address becomes
non-tentative.

(cherry picked from commit 4138be6a5a)
(cherry picked from commit 0ebd753819)
(cherry picked from commit 26d5ad4680)
2023-03-01 11:49:41 +01:00
Beniamino Galvani
54c1d321ac macsec: fix tracking of parent ifindex
For MACsec interfaces, kernel announces the parent ifindex in the
generic IFLA_LINK netlink attribute, which we save in
NMPlatformLink.parent. There is no need to have a dedicate member in
NMPlatformLnkMacsec.

The dedicate member was never set and during a restart of
NetworkManager the parent of the MACsec device could be unset leading
to a failed assertion:

  act_stage2_config: assertion 'parent' failed

Fixes: 85103656e9 ('platform: add support for macsec links')

https://bugzilla.redhat.com/show_bug.cgi?id=2122564
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1481
(cherry picked from commit cf11884a85)
(cherry picked from commit fe42644fcd)
2023-03-01 11:47:43 +01:00
Beniamino Galvani
69e66102ce core: wait for carrier before resolving hostname via DNS
If there is no carrier on a device, don't try to resolve the hostname
on it. Instead, subscribe to carrier change notifications and retry
again once carrier goes up.

https://bugzilla.redhat.com/show_bug.cgi?id=2118817
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1402
(cherry picked from commit e3cf5083fb)
(cherry picked from commit 1673e3f051)
2022-12-16 16:57:15 +01:00
Beniamino Galvani
96f93e3a5b ovs: wait that links disappear during initial cleanup
At startup, we remove from ovsdb any existing interface created by NM
and later an interface with the same name might be readded. This can
cause race conditions. Consider this series of events:

1. at startup NM removes the entry from ovsdb;
2. ovsdb reports success;
3. NM inserts an interface with the same name again;
4. ovs-vswitch monitors ovsdb changes, and gets events for removal and
   insertion. Depending on how those events are split in different
   batches, it might decide:
   4a. to delete the link and add it back, or
   4b. to keep the existing link because the delete and insertion
       cancel out each other.

When NM sees the link staying in platform, it doesn't know if it's
because of 4b or because 4a will happen eventually.

To avoid this ambiguity, after ovsdb reports the successful deletion
NM should also wait that the link disappears from platform.

Unfortunately, this means that ovsdb gets a dependency to the platform
code.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1386
(cherry picked from commit 4f60fe293c)
(cherry picked from commit f702be2992)
2022-12-16 16:29:33 +01:00
Fernando Fernandez Mancera
4ae14b0bed veth: drop iface peer check during create_and_realize()
When fetching the parent device, if the system is slow, NetworkManager
can hit a race condition where the property is still NULL. In that case,
NetworkManager should create the veth link.

Checking that the peer device exists, it is type NM_DEVICE_TYPE_VETH and
it have a parent device is enough to know that we can skip the link
creation.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1399

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

Fixes: 4655b7c308 ('veth: fix veth activation on booting')
(cherry picked from commit 07e0ab48d1)
(cherry picked from commit 65d31a11f8)
2022-11-16 17:26:56 +01:00
Fernando Fernandez Mancera
36bb9a569a veth: fix veth activation on booting
When creating one profile for each veth during activation the creation
of the veth could fail. When the link for the first profile is created
the link for the peer is generated in kernel. Therefore when trying to
activate the second profile it will fail because the link already
exists. NetworkManager must check if the link already exists and
corresponds to the same veth, if so, it should skip the link creation.

https://bugzilla.redhat.com/show_bug.cgi?id=2036023
https://bugzilla.redhat.com/show_bug.cgi?id=2105956
(cherry picked from commit 4655b7c308)
2022-11-16 17:26:09 +01:00
Thomas Haller
61cfdae2e6
release: bump version to 1.38.7 (development) 2022-10-19 17:31:21 +02:00
Thomas Haller
93aaa3f0df
release: bump version to 1.38.6 2022-10-19 17:31:21 +02:00
Thomas Haller
a694e8f65d
NEWS: update 2022-10-19 14:54:35 +02:00
Beniamino Galvani
9def416b7b Revert "device: restart DHCP when the MAC changes"
The commit causes problems with bridges. When a new port is attached
the MAC of the bridge possibly changes and if we restart DHCP the
bridge will get a different IP address.

Revert the change until a better solution to the original problem is
found.

This reverts commit 905adabdba.

https://bugzilla.redhat.com/show_bug.cgi?id=2124443
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1401
(cherry picked from commit 9819738607)
(cherry picked from commit f9db4f4604)
2022-10-19 14:02:52 +02:00
Beniamino Galvani
53eba18d60 dns: merge branch 'bg/dns-sort'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1427

(cherry picked from commit e03b8fa447)

(cherry picked from commit 925ea8b24c)
2022-10-14 10:48:42 +02:00
Beniamino Galvani
6ac62a746f dns: sort the ip-data list when a new element is added
In nm_dns_manager_set_ip_config() we try to avoid calling update_dns()
unless something changes, because updating DNS is expensive and can
trigger other actions such as a new hostname resolution.

When we add a new ip_data, even if the new element is equivalent to
the old one that was removed, we need to sort the list again.

Fixes: ce0a36d20f ('dns: better track l3cd changes')
https://bugzilla.redhat.com/show_bug.cgi?id=2098574
(cherry picked from commit 3cc7801779)
(cherry picked from commit db4c55c8d3)
2022-10-14 10:48:41 +02:00
Beniamino Galvani
ad8a6a59af dns: add comment explaining the purpose of any_removed
(cherry picked from commit 4d1ecd8d6d)
(cherry picked from commit 01b4040a7a)
2022-10-14 10:48:41 +02:00
Beniamino Galvani
c36bc32d83 dhcp: decline IPv6 lease if all adresses fail DAD
Currently we accept the DHCPv6 just after addresses are configured on
kernel, without waiting DAD result. Instead, wait that DAD completes
and decline the lease if all addresses are detected as duplicate.

Note that when an address has non-infinite lifetime and fails DAD,
kernel removes it automatically. With iproute2 we see something like:

602: testX6    inet6 2620:🔢5678/128 scope global tentative dynamic noprefixroute
       valid_lft 7500sec preferred_lft 7200sec
Deleted 602: testX6    inet6 2620:🔢5678/128 scope global dadfailed tentative dynamic noprefixroute
       valid_lft 7500sec preferred_lft 7200sec

Since the address gets removed from the platform cache, at the moment
we don't have a way to check the flags of the removal
message. Therefore, we assume that any address that goes away in
tentative state was detected as duplicate.

https://bugzilla.redhat.com/show_bug.cgi?id=2096386
(cherry picked from commit a7eb77260a)
(cherry picked from commit b671c36189)

Conflicts:
	src/core/dhcp/nm-dhcp-client.c
	[bgalvani: changed 'priv->l3cd_curr' to 'priv->l3cd' because
         nm-1-38 is missing 240ec7f891 ('dhcp: implement ACD
         (address collision detection) for DHCPv4')]
2022-10-12 11:06:39 +02:00
Thomas Haller
659ee74d8d
doc: fix bind-activation description for AddAndActivateConnection2 D-Bus API
Fixes: eb883e34a5 ('core: Add option to AddAndActivateConnection2 to bind the lifetime')
(cherry picked from commit 88f5e7518a)
(cherry picked from commit afe53b902f)
2022-10-05 10:49:05 +02:00
Thomas Haller
fff62917b7
core: merge branch 'th/mlag-bonding-slb' (part 2)
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1385

(cherry picked from commit c5beec90a8)

(cherry picked from commit 278d32c7e7)
2022-09-29 16:04:19 +02:00
Thomas Haller
798d8ee832
firewall: introduce helper function for add/flush/delete nft table command
(cherry picked from commit e796a67d6c)
(cherry picked from commit 56a503c879)
2022-09-29 16:04:18 +02:00
Thomas Haller
403c6de957
firewall: fail from nm_firewall_nft_call() on non-zero exit code
(cherry picked from commit 02feefb1df)
(cherry picked from commit d7bc25b3bd)
2022-09-29 16:04:17 +02:00
Thomas Haller
aebfb3461e
device: allow resetting the devip state via nm_device_devip_set_state()
There is no reason to disallow resetting the state.

(cherry picked from commit 607a9544cb)
(cherry picked from commit 6af0233a21)
2022-09-29 16:04:17 +02:00
Thomas Haller
9b982c4ea3
glib-aux: add NM_UTILS_ERROR_COMMAND_FAILED error code
(cherry picked from commit a5f125f8cb)
(cherry picked from commit f4126a12cd)
2022-09-29 16:04:16 +02:00
Thomas Haller
f3f99e03f5
glib-aux: add nm_utils_get_process_exit_status_desc_buf() helper
(cherry picked from commit 95e6ebec66)
(cherry picked from commit 431c219ad1)
2022-09-29 16:04:16 +02:00
Thomas Haller
51b9f0ad4c
glib-aux: fix nm_str_buf_finalize() for cloning buffer
NMStrBuf can also contains NUL characters. We thus cannot use g_strndup(),
which uses strncpy() and truncates at the first NUL.

Fixes: 13d25f9d0b ('glib-aux: add support for starting with stack-allocated buffer in NMStrBuf')
(cherry picked from commit 520411623d)
(cherry picked from commit 7a3de841b8)
2022-09-29 15:49:11 +02:00
Thomas Haller
fed6e78d05
glib-aux: fix spurious semicolon after NM_STR_BUF_INIT() macros
It's wrong, and it breaks certain uses.

Fixes: 13d25f9d0b ('glib-aux: add support for starting with stack-allocated buffer in NMStrBuf')
(cherry picked from commit c5ec4ebd77)
(cherry picked from commit 7b487e6951)
2022-09-29 15:49:11 +02:00
Thomas Haller
6c2c3fdfcc
glib-aux: add nm_str_buf_append_printfv()
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1321
(cherry picked from commit d5b31a05e6)
2022-09-29 15:49:10 +02:00
Thomas Haller
c744607c60
glib-aux: merge branch 'th/str-buf-stack-allocated'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1203

(cherry picked from commit c6e41b2df3)
2022-09-29 15:49:09 +02:00
Thomas Haller
de6da97e9d
glib-aux: avoid #if in "nm-str-buf.h"
NM_MORE_ASSERT is a compile time constant. The compiler can optimize
it away just fine.

(cherry picked from commit 560feecb4c)
2022-09-29 15:49:09 +02:00
Thomas Haller
fc71b2b1f7
glib-aux/trivial: add code comment to nm_str_buf_get_str_unsafe()
(cherry picked from commit 24dab91a66)
2022-09-29 15:49:08 +02:00
Thomas Haller
90255a8aa8
glib-aux: drop nm_str_buf_init() for NM_STR_BUF_INIT()
NM_STR_BUF_INIT() and nm_str_buf_init() were pretty much redundant. Drop one of
them.

Usually our pattern is that we don't have functions that return structs.
But NM_STR_BUF_INIT() returns a struct, because it's convenient to use
with

  nm_auto_str_buf NMStrBuf strbuf = NM_STR_BUF_INIT(...);

So use that variant instead.

(cherry picked from commit 532f3e34a8)
2022-09-29 15:49:08 +02:00
Thomas Haller
51393413b4
glib-aux: add support for starting with stack-allocated buffer in NMStrBuf
Allow to initialize NMStrBuf with an externally allocated array.
Usually a stack buffer. If the NMStrBuf grows beyond the size of
that initial buffer, then it would switch using malloc.

The idea is to support the common case where the result is small enough
to fit on the stack.

I always wanted to do such optimization because the main purpose of
NMStrBuf is to put it on the stack and ad-hoc construct a string.
I just figured, it would complicate the implementation and add
a runtime overhead. But turns out, it doesn't really.
The biggest question is how NMStrBuf should behave with a pre-allocated
buffer? Turns out, most choices can be made in a rather obvious way.
The only non-obvious thing is that nm_str_buf_finalize() would malloc()
a buffer, but that too seems consistent and what a user would probably
expect. As such, this doesn't seem to add unexpected semantics to the API.

(cherry picked from commit 13d25f9d0b)
2022-09-29 15:49:08 +02:00
Thomas Haller
327113098b
std-aux: add NM_UTILS_GET_NEXT_REALLOC_SIZE_488 define
(cherry picked from commit 2c5bacd416)
2022-09-29 15:48:35 +02:00