Commit Graph

95 Commits

Author SHA1 Message Date
Thomas Haller
c366c155f1 shared: rename PROP_0 in NM_GOBJECT_PROPERTIES_DEFINE() and skip it in nm_gobject_notify_together()
PROP_0 is how we commonly name this property when we don't use
NM_GOBJECT_PROPERTIES_DEFINE(). Rename it.

Also, allow to skip PROP_0 in nm_gobject_notify_together(), that
is handy to optionally invoke a notification, like

  nm_gobject_notify_together (obj,
                              PROP_SOMETHING,
                              changed ? PROP_OTHER : PROP_0);
2018-09-04 07:38:30 +02:00
Thomas Haller
b232508707 shared: add nm-secret-utils.h helper
We already had nm_free_secret() to clear the secret out
of a NUL terminated string. That works well for secrets
which are strings, it can be used with a cleanup attribute
(nm_auto_free_secret) and as a cleanup function for a
GBytes.

However, it does not work for secrets which are binary.
For those, we must also track the length of the allocated
data and clear it.

Add two new structs NMSecretPtr and NMSecretBuf to help
with that.
2018-09-04 07:38:30 +02:00
Thomas Haller
74815fd8e0 shared: drop unnecessary NM_AUTO_DEFINE_FCN_STRUCT() macro
It serves no purpose, as it just directly calls the function. We don't
need to define this intermediary.
2018-09-04 07:38:30 +02:00
Thomas Haller
f15d82bc91 shared: add nm_auto_unref_bytearray macro
Internally, GByteArray is actually a GArray, so it would be safe to
use "gs_unref_array" macro. However, that is rather ugly, and means
to rely on an internal implementation detail of GByteArray.

Instead, add a cleanup macro for GByteArray.
2018-09-04 07:38:30 +02:00
Thomas Haller
b7c8e3dbfa shared: add NM_DIV_ROUND_UP() helper macro
Inspired by ethtool's DIV_ROUND_UP() and systemd's DIV_ROUND_UP().
2018-08-10 10:38:19 +02:00
Thomas Haller
852abf3d3d all/style: write elvis operator ?: without space
By far most of the time, we write "?:" and not "? :". Adjust
the few places that don't.
2018-08-09 17:06:18 +02:00
Thomas Haller
ccf6bdb0e2 shared: add nm_gobject_notify_together() helper
NM_GOBJECT_PROPERTIES_DEFINE() defines a helper function
_notify() to emit a GObject property changed notification.

Add another helper function to emit multiple notifications
together, and freeze/thaw the notification before.

This is particularly useful, because our D-Bus glue in
"nm-dbus-object.c" hooks into dispatch_properties_changed(),
to emit a combined PropertiesChanged signal for multiple
properties. By carefully freezing/thawing the notifications,
the exported objects can combine changes of multiple properties
in one D-Bus signal.

This helper is here to make that simpler.

Note that the compiler still has no problem to inline _notify()
entirey. So, in a non-debug build, there is little difference in
the generated code. It can even nicely inline calls like

    nm_gobject_notify_together (self, PROP_ADDRESS_DATA,
                                      PROP_ADDRESSES);
2018-08-01 14:26:36 +02:00
Thomas Haller
0d3bb64008 shared: support zero arguments for NM_NARG() macro
It relies on the GCC extension ##__VA_ARGS__, but we
do that on various places already.

Also add a test.
2018-07-24 09:39:09 +02:00
Thomas Haller
196d7c8ca5 shared: use nm_auto() macro to define other nm_auto_* macros 2018-07-18 10:27:39 +02:00
Thomas Haller
d0b3702b37 shared: cleanup nm_auto implementations
- Reuse NM_AUTO_DEFINE*() where possible.
- Consistently name the cleanup functions for "nm_auto_xzy" as
  _nm_auto_xzy().
2018-07-18 10:21:27 +02:00
Thomas Haller
e9d9fc3fa0 shared/gsystem-local-alloc: merge "gsystem-local-alloc.h" into "nm-macros-shared.h"
We only have a certain granularity of how our headers in "shared/nm-utils"
can be used independently.

For example, it's not supported to use "nm-macros-internal.h" without
"gsystem-local-alloc.h". Likewise, you cannot use "nm-glib.h" directly,
you always get it together with "nm-macros-internal.h".

This is, we don't support to use certain headers entirely independently,
because usually you anyway want to use them together.

As such, no longer support "gsystem-local-alloc.h", but merge the
remainder into "nm-macros-internal.h". There is really no reason
to support arbitrary flexibility of including individual bits. You
want cleanup-macros? Include "nm-macros-internal.h".

Merge the headers.
2018-07-18 10:21:27 +02:00
Thomas Haller
a38782f378 shared/trivial: rename GS_DEFINE_CLEANUP_FUNCTION_*() macros
To be consistent with our other nm_auto* related functions.
2018-07-18 10:21:27 +02:00
Thomas Haller
904e8b8ca0 shared/gsystem-local-alloc: rename unused gs_* cleanup macros
These cleanup macros are unused by NetworkManager code. Note that
since "shared/nm-utils" is used by applet and VPN plugins, theoretically,
they could be used there. I didn't check that, but breaking API of
"shared/nm-utils" is fine (as long as we catch it with a compilation
error).

Historically, we use libgsystem's gsystem-local-alloc header and their
"gs_*" macros. However, they are not really our style and don't have
a nm-prefix (like the rest of our code). We keep the gs_ names, because
they are wildly used and because we wanted to keep gsystem-local-alloc
in sync with upstream (which is no longer the case).

Our own cleanup macros are always called "nm_auto_*". So, at least
for the unused "gs_*" macros, rename them to "nm_auto_*".

Don't drop them, despite they being unused. The reason is, that we should
make use of cleanup functions more eagerly. Dropping them now -- because
they are momentarily unused -- hampers using them in the future. We
often don't use the cleanup macros at places where I think we should,
so by dropping them, we hamper future use.
2018-07-18 10:21:27 +02:00
Thomas Haller
5513af2e14 shared/trivial: add comment to nm_auto_free
It's not obvious why we have "nm_auto_free" along "gs_free".
Explain it.

Also, define the cleanup function first, then the nm-auto macro.
2018-07-18 10:21:27 +02:00
Thomas Haller
98c71df8a3 shared/gsystem-local-alloc: strongly type gs_* cleanup functions
At various places, use the correct type for the pointer, this
allows the compiler to be more helpful.

For gs_free, gs_unref_object, and nm_auto_free, the pointer type is
of course still 'void *'.

This catches wrong uses like

   gs_strfreev char *wrong1 = NULL;
   gs_strfreev const char **wrong2 = NULL;
   gs_free_error GError **p_error = NULL;
   gs_unref_array GPtrArray *ptr_array = NULL;

Note that long time ago we copied "gsystem-local-alloc.h" header
from libgsystem library. Until now, we didn't apply any local
modification to this file, to keep it in sync with upstream.
However, upstream libgsystem is not maintained anymore, so there
is no reason to stay in sync with upstream.
2018-07-11 16:38:12 +02:00
Thomas Haller
634b2d1ce2 shared: add nm_auto_vfree macro
This really is the same as gs_strfreev / g_strfreev().
However, the difference is, that the former has the notion
of freeing strv arrays (char **), while this in general
frees an array of pointers. Implementation-wise, they are
the same.
2018-07-11 16:16:22 +02:00
Thomas Haller
e1c7a2b5d0 all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.

    $ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
    587
    $ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
    21114

One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during

  g_object_set (obj, PROPERTY, (gint) value, NULL);

However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.

Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).

A simple style guide is instead: don't use these typedefs.

No manual actions, I only ran the bash script:

  FILES=($(git ls-files '*.[hc]'))
  sed -i \
      -e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
      -e 's/\<g\(char\|short\|int\|long\|float\|double\)\>  /\1   /g' \
      -e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
      "${FILES[@]}"
2018-07-11 12:02:06 +02:00
Thomas Haller
9586d2794a shared: simplify nm_g_object_ref_set()
Also, assign *_pp before unref-ing the old value. Calling
g_object_unref() on the old value, might invoke callbacks
that are out of control of nm_g_object_ref_set(). During
that time, the pointer should already be assigned the new value,
instead of having an intermediate %NULL value. In most cases,
this would of course not matter, but there is no need to let
anyone see an intermediate %NULL value for a moment.

Also, don't use typeof(**_pp), which would not work with opaque
types (like we commonly have).
2018-06-15 09:07:19 +02:00
Thomas Haller
34122c874e shared: add NM_PID_T_INVAL macro for invalid PIDs 2018-05-11 16:51:20 +02:00
Thomas Haller
286db5049e shared: add nm_auto_unref_gsource cleanup macro 2018-05-11 16:51:20 +02:00
Beniamino Galvani
1b5925ce88 all: remove consecutive empty lines
Normalize coding style by removing consecutive empty lines from C
sources and headers.

https://github.com/NetworkManager/NetworkManager/pull/108
2018-04-30 16:24:52 +02:00
Thomas Haller
bc1b15cf05 shared: move cmp functions to nm-shared-utils.c
For one, these functions are not often needed. No need to define them in the
"nm-macros-internal.h" header, which is included everywhere. Move them to
"nm-shared-utils.h", which must be explicitly included.

Also, these functions are usually not called directly, but by passing their
function pointer to a sort function or similar. There is no point in having
defined in the header file.
2018-04-19 09:36:41 +02:00
Thomas Haller
458b422468 shared: add NM_CAST_STRV_*() helper macros 2018-04-18 07:55:15 +02:00
Thomas Haller
f7b2ebc87a shared: fix typecheck in NM_PTRARRAY_LEN()
Previously, NM_PTRARRAY_LEN() would not work if the pointer type is
an opaque type, which is common. For example:

  NMConnection *const*connections = ...;
2018-03-20 15:08:18 +01:00
Thomas Haller
63ca07f492 shared: add nm_clear_pointer() and implement existing nm_clear_*() based on it
Add an alternative to g_clear_pointer(). The differences are:

  - nm_clear_pointer() is more type safe as it does not cast neither the
    pointer nor the destroy function. Commonly, the types should be compatible
    and not requiring a cast. Casting in the macro eliminates some of the
    compilers type checking. For example, while
       g_clear_pointer (&priv->hash_table, g_ptr_array_unref);
    compiles, nm_clear_pointer() would prevent such an invalid use.

  - also, clear the destination pointer *before* invoking the destroy
    function. Destroy might emit signals (like weak-pointer callbacks
    of GArray clear functions). Clear the destination first, so that
    we don't leave a dangling pointer there.

  - return TRUE/FALSE depending on whether there was a pointer to clear.

I tested that redefining g_clear_pointer()/g_clear_object() with our
more typesafe nm_* variants still compiles and indicates no bugs. So
that is good. It's not really expected that turning on more static checks
would yield a large number of bugs, because generally our code is in a good
shape already. We have few such bugs, because we already turn all all warnings
and extra checks that make sense. That however is not an argument for
not introducing (and using) a more resticted implementation.
2018-03-19 15:51:17 +01:00
Thomas Haller
4cbe3eaaca shared: clear destination pointer in nm_clear_*() functions first
It's slightly more correct to first clear the pointer location
before invoking the destroy function. The destroy function might
emit other callbacks, and at a certain point the pointer becomes
dangling. Avoid this danling pointer, by first clearing the
memory, and then destroing the instance.
2018-03-19 15:50:15 +01:00
Thomas Haller
d46de19a9a shared: add nm_steal_int() helper 2018-02-12 13:29:03 +01:00
Thomas Haller
7a956644d4 shared: add NM_UNCONST_PTR() and NM_UNCONST_PPTR()
Add macros that cast away the constness of a pointer, but
ensure that the type of the pointer is as expected.

Unfortunately, there is no way (AFAIK) to remove the constness of
a variable, without explicitly passing @type to the macro.
2018-02-12 13:29:03 +01:00
Lubomir Rintel
fc4552d391 shared/utils: don't warn of unknown warning disables with clang
When pushing a warning disable with clang, always disable
-Wunknown-warning-option first -- it might be that clang wouldn't warn
of what we're trying to disable because it doesn't recognize it in the
first place. That is entierely okay.

With clang-5.0.0:

    CC       libnm/tests/libnm_tests_test_secret_agent-test-secret-agent.o
  In file included from libnm/tests/test-secret-agent.c:29:
  In file included from ./shared/nm-test-libnm-utils.h:23:
  ./shared/nm-utils/nm-test-utils.h:432:3: error: unknown warning group '-Wunused-but-set-variable', ignored [-Werror,-Wunknown-warning-option]
                  NM_PRAGMA_WARNING_DISABLE("-Wunused-but-set-variable")
                  ^
  ./shared/nm-utils/nm-macros-internal.h:223:9: note: expanded from macro 'NM_PRAGMA_WARNING_DISABLE'
          _Pragma(_NM_PRAGMA_WARNING_DO(warning))
          ^
  <scratch space>:204:25: note: expanded from here
   GCC diagnostic ignored "-Wunused-but-set-variable"
                          ^
  1 error generated.
2018-01-24 09:53:38 +01:00
Thomas Haller
901aa0315b shared: add nm_cmp_int2ptr_p_with_data() helper
A cmp() implementation, for sorting an array with pointers, where each
pointer is an inteter according to GPOINTER_TO_INT().

That cames for example handy, if you have a GHashTable with keys
GINT_TO_POINTER(). Then you get the list of keys via
g_hash_table_get_keys_as_array() and want to sort them.
2018-01-09 14:24:54 +01:00
Thomas Haller
2fb7479e82 macros: add _nm_fallthrough macro
Systemd introduced a _fallthrough_ macro in
https://github.com/systemd/systemd/pull/7389.

There might still be some issue with it, but as
I am going to re-import the latest systemd code,
we get them too.

We need it, because "shared/nm-utils/siphash24.c"
will use it too, and that source file does not include
the other systemd macros. So, we will need to re-define
it.
2017-12-13 10:41:01 +01:00
Thomas Haller
7ca601d529 shared: propagate type for g_object_ref()
See related bug https://bugzilla.gnome.org/show_bug.cgi?id=790697
2017-12-06 10:34:28 +01:00
Thomas Haller
011a609bd4 shared/trivial: fix type on comment 2017-12-05 19:57:25 +01:00
Thomas Haller
48960ba8da shared: add nm_construct_name_a() macro 2017-11-23 14:44:24 +01:00
Thomas Haller
97f8d21674 shared: add nm_auto_free_secret macro 2017-11-20 11:37:24 +01:00
Thomas Haller
f4780f85ae shared: always call close() from nm_close() wrapper
The nm_close() wrapper should behave exactly the same as calling
close() directly. This is well known, documented behavior.

The only addition on top of that, should be the nm_assert() to catch
double-closing.

Prevously, when passing a negative file descriptor, we wouldn't properly
set errno. Also, the call would not show up in strace, which it should
(at least, if libc's close actually makes the syscall).

I would argue, that passing a negative file descriptor is a bug already
and we should never do that. Maybe we should even assert non-negative
fds. I don't do that now, because I am not sufficiently confident.
Anyway, the change should have not practical effect, because we
shouldn't actually pass negative fds already.
2017-11-14 15:09:26 +01:00
Thomas Haller
557d83bf2d build: detect compiler features _Generic() and __auto_type in configure script
There is still a fallback detection in "shared/nm-utils/nm-macros-internal.h",
so that VPN-plugins and applet don't need to bother about adding these
configure checks.
2017-11-13 11:35:44 +01:00
Thomas Haller
bdfdabea51 shared: propagate constness in _NM_GET_PRIVATE_PTR()
The _NM_GET_PRIVATE() macro already preserved and propagated
the constness of @self to the resulting private pointer.

_NM_GET_PRIVATE_PTR() didn't do that. Extend the macro,
to make that possible.
2017-11-13 11:35:44 +01:00
Thomas Haller
03efc9e2c9 shared: fix detection of _Generic() support 2017-11-13 11:35:44 +01:00
Thomas Haller
b339a2742a shared: make NM_CONSTCAST() macro variadic
We need to pass more alias-types. Instead of having numbered
versions, use variadic number of macro arguments.

Also, fix build failure with old compiler:

  In file included from src/nm-ip6-config.c:24:
  ./src/nm-ip6-config.h:44:29: error: controlling expression type 'typeof (ipconf_iter->current->obj)' (aka 'const void *const') not compatible with any generic association type
                  *out_address = has_next ? NMP_OBJECT_CAST_IP6_ADDRESS (ipconf_iter->current->obj) : NULL;
                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fixes: b1810d7a68
2017-11-09 14:15:34 +01:00
Thomas Haller
b1810d7a68 shared: rework _NM_GET_PRIVATE() to use _Generic()
_NM_GET_PRIVATE() used typeof() to propagate constness of the @self
pointer. However, that means, it could only be used with a self pointer
of the exact type. That means, you explicitly had to cast from (GObject *)
or from (void *).
The requirement is cumbersome, and often led us to either create @self
pointer we didn't need:

    NMDeviceVlan *self = NM_DEVICE_VLAN (device);
    NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);

or casting:

    NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE ((NMDevice *) device);

In both cases we forcefully cast the source variable, loosing help from
the compiler to detect a bug.

For "nm-linux-platform.c", instead we commonly have a pointer of type
NMPlatform. Hence, we always forcefully cast the type via _NM_GET_PRIVATE_VOID().

Rework the macro to use _Generic(). If compiler supports _Generic(), then we
will get all compile time checks as desired. If the compiler doesn't support
_Generic(), it will still work. You don't get the compile-time checking of course,
but you'd notice that something is wrong once you build with a suitable
compiler.
2017-11-09 10:57:13 +01:00
Thomas Haller
de65d3da91 shared: fix nm_steal_fd() for handling zero file descriptor
Zero is a valid file descriptor.

Fixes: 956b3e8bd5
2017-10-30 11:44:37 +01:00
Thomas Haller
956b3e8bd5 shared: add nm_steal_fd() helper function 2017-10-19 15:49:58 +02:00
Beniamino Galvani
1bc1809e11 shared: introduce nm_close()
nm_close() is like close(), but throws an assertion if the input fd is
>=0 and invalid. Passing an invalid (i.e. already closed) fd to
close() is a programming error with potentially catastrophic effects,
as another thread may reuse the closed fd.
2017-10-19 15:49:50 +02:00
Thomas Haller
acbab884ee shared: add _NM_ENSURE_TYPE() macro 2017-10-17 20:02:59 +02:00
Thomas Haller
bdb8e65434 shared: add _nm_align() and _nm_alignof() macros 2017-10-17 20:02:59 +02:00
Thomas Haller
4a2798434e core: introduce NM_HASH_INIT() to initialize hash seed
Introduce a NM_HASH_INIT() function. It makes the places
where we initialize a hash with a certain seed visually clear.

Also, move them from "shared/nm-utils/nm-shared-utils.h" to
"shared/nm-utils/nm-macros-internal.h". We might want to
have NM_HASH_INIT() non-inline (hence, define it in the
source file).
2017-10-13 12:47:55 +02:00
Thomas Haller
373684fdc0 shared: add _nm_thread_local macro
Copied and adjusted from systemd.
2017-10-13 12:47:55 +02:00
Thomas Haller
6a3005ea72 shared: add nm_strstrip_avoid_copy() 2017-10-09 22:05:35 +02:00
Thomas Haller
569b443233 shared: add nm-glib.h compat implementation for g_variant_new_printf() 2017-09-18 20:14:09 +02:00