summaryrefslogtreecommitdiff
path: root/shared/nm-glib-aux
Commit message (Collapse)AuthorAgeFilesLines
* shared: add nm_g_slice_free() helperThomas Haller2019-07-251-0/+5
| | | | | | How odd that such a macro does not exist yet. It seems like the majorities of calls to g_slice_free() could be replaced by this.
* core: improve code comment and add assertion to ↵Thomas Haller2019-07-251-7/+9
| | | | nm_utils_monotonic_timestamp_as_boottime()
* config: simplify no-auto-default list handling and sort entriesThomas Haller2019-07-251-0/+2
| | | | | | | | | | | | - 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.
* shared: accept %NULL strings in nm_utils_strv_sort()Thomas Haller2019-07-251-4/+11
| | | | | | | | | | | | 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.
* shared: add nm_strcmp0_p_with_data() helperThomas Haller2019-07-252-0/+10
|
* shared: add nm_strcmp_with_data()Thomas Haller2019-07-252-0/+10
| | | | | | | | | | | | | | | | | | | | 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] https://gitlab.gnome.org/GNOME/glib/blob/0c0cf59858abb3f8464bc55f596f9fbf599ac251/glib/garray.c#L1792
* shared: add nm_utils_clock_gettime_*() utilThomas Haller2019-07-232-0/+39
| | | | | | | | Using clock_gettime() directly is a bit inconvenient. We usually want to combine the fields of struct timespec into one timestamp (for example, in unit nanoseconds). Add a helper function to do that.
* shared: suppress -Werror=stringop-overflow= warning in nm_strndup_a()Thomas Haller2019-07-161-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | nm_strndup_a() uses strncpy() because we want the behavior of clearing out the memory after the first NUL byte. But that can cause a compiler warning: CC src/settings/plugins/keyfile/libNetworkManager_la-nms-keyfile-utils.lo In file included from ../../shared/nm-default.h:279, from ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:20: In function ‘_nm_strndup_a_step’, inlined from ‘nms_keyfile_loaded_uuid_is_filename’ at ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:65:9: ../../shared/nm-glib-aux/nm-macros-internal.h:1661:3: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=] 1661 | strncpy (s, str, len); | ^~~~~~~~~~~~~~~~~~~~~ ../../src/settings/plugins/keyfile/nms-keyfile-utils.c: In function ‘nms_keyfile_loaded_uuid_is_filename’: ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:48:8: note: length computed here 48 | len = strlen (filename); | ^~~~~~~~~~~~~~~~~ It's true that the len argument of _nm_strndup_a_step() depends on the string length of the source string. But in this case it's safe, because we checked that the destination buffer is exactly the right size too. By that reasoning we should use memcpy() or strcpy(), but both are unsuitable. That is because we want nm_strndup_a() to behave like strndup(), which means we need to handle cases where the len argument is larger than the string length of the source string. That is, we want always to return a buffer of size len+1, but we want to copy only the characters up to the first NUL byte, and clear out the rest. That's what strncpy() does for us. Silence the warning.
* shared: add NM_CMP_DIRECT_STRCMP() macroThomas Haller2019-07-101-0/+3
|
* shared: optimize nm_utils_error_set() for string literalsThomas Haller2019-07-101-1/+7
| | | | | | | | | | | | | | | | | | | If there is only one argument, we can assume this is a plain string. That is especially the case, because g_set_error() is G_GNUC_PRINTF() and would warn if this would be a format string with missing parameters. This is for convenience. Previously, one was compelled to explicitly choose between nm_utils_error_set_literal() and nm_utils_error_set(). Now, it automatically chooses. Note that there are a few things that won't work, like nm_utils_error_set (error, code, "bogus %u escape"); But that's good. You get a compiler warning (as you used to) and it's clear in this case you really need nm_utils_error_set_literal().
* shared: add nm_pdirect_hash()/nm_pdirect_equal()Thomas Haller2019-07-102-0/+31
| | | | | | | | | This follows a pointer to a pointer and compares them. In a sense it's like nm_pstr_*(), which follow a pointer to a string. However, these functions use direct pointer comparison. The purpose is when you hash a key that has as first field a pointer value (and then compare them by pointer equality).
* shared: gracefully accept %NULL strings for NM_STR_HAS_PREFIX() and ↵Thomas Haller2019-07-041-4/+9
| | | | | | | NM_STR_HAS_SUFFIX() In case it wasn't obvious to a caller, allow %NULL as valid string argument. Just be a bit more forgiving and fault-tolerant.
* shared: add nm_c_list_elem_free_steal() utilThomas Haller2019-07-021-0/+12
|
* shared: add nm_utils_strv_dup() utilThomas Haller2019-06-282-0/+58
|
* shared: add nm_c_list_free_all() macroThomas Haller2019-06-281-0/+10
|
* shared: add nm_clear_error() and patch g_clear_error() to use this inlinable ↵Thomas Haller2019-06-261-0/+24
| | | | variant
* shared/glib: unconditionally redefine g_object_ref()/g_object_ref_sink() as ↵Thomas Haller2019-06-261-2/+10
| | | | typesafe macro
* shared: add nm_utils_file_stat() utilThomas Haller2019-06-262-0/+27
| | | | | A small convenience function to call stat(). The difference is that the function returns an error code.
* shared: add nm_utils_hashtable_same_keys() utilThomas Haller2019-06-262-0/+32
|
* shared: allow nm_c_list_move_*() API also to move from one list to anotherThomas Haller2019-06-261-2/+26
| | | | | | Previously, nm_c_list_move_*() only allowed to move element inside the same list. Relax that, it works just the same list to move the element from one list into a different list.
* shared: fix nm_utils_bin2hexstr_full() for buffers of length zeroThomas Haller2019-06-191-4/+9
|
* shared: add nm_utils_g_slist_strlist_cmp() utilThomas Haller2019-06-172-0/+21
| | | | | | 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.
* shared: add nm_g_variant_ref_sink() utilThomas Haller2019-06-131-0/+8
|
* shared: add nm_c_list_elem_find_first() and minor cleanups of NMCListElem APIThomas Haller2019-06-131-15/+37
|
* shared: add nm_utils_g_slist_find_str() utilThomas Haller2019-06-132-0/+33
|
* all: drop emacs file variables from source filesThomas Haller2019-06-1119-19/+0
| | | | | | | | | | | | | | | | | | | | | | 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.
* shared: use NM_MIN() in NM_CMP_FIELD_MEMCMP_LEN() macroThomas Haller2019-06-111-1/+1
| | | | To avoid evaluating the argument more than once.
* shared: fix _NM_ENSURE_TYPE_CONST() for const pointers with clangThomas Haller2019-05-291-1/+5
| | | | | | | | | | Clang 3.4.2-9.el7 on CentOS7.6 complains about missing generic type match: ../dispatcher/nm-dispatcher.c:243:2: error: controlling expression type 'const Request *const' (aka 'const struct Request *const') not compatible with any generic association type _LOG_R_D (request, "start running ordered scripts..."); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fixes: 17dc6a9da68a ('shared: add _NM_ENSURE_TYPE_CONST()')
* ifcfg-rh: preserve existence of wired settingBeniamino Galvani2019-05-281-0/+4
| | | | | | | | | | | | | Currently the plugin doesn't preserve the existence of a wired setting because the writer saves only variables with non-default values and, especially, the reader always creates the setting. Fix this; now the writer writes HWADDR even if empty when the setting is present; the reader creates the setting when at least one property is found. https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/166 https://bugzilla.redhat.com/show_bug.cgi?id=1703960
* shared: fix return in ↵Thomas Haller2019-05-271-0/+2
| | | | | | nm_value_type_to_variant()/nm_value_type_get_variant_type() Fixes: 75703a242563 ('shared: add accessor functions for NMValueType')
* shared: add _NM_ENSURE_TYPE_CONST()Thomas Haller2019-05-271-0/+2
| | | | | | | | | | | | The sole purpose of this is more type-safe macros. An alternative solution would be to define a function instead of a macro. But if the function is unused (currently!) you get a compiler warning (on some compilers even when marking the function as "static inline", if it's in the source file). A workaround for that would be to mark the function as _nm_unused, or to use a macro instead. _NM_ENSURE_TYPE_CONST() is to aid the macro solution.
* shared: add nm_utils_gvariant_vardict_filter*() helpersThomas Haller2019-05-232-0/+75
| | | | | | | Usually, such an operation does not make much sense. It's also not good performance wise. But for unit testing this becomes very interesting.
* shared: add JSON helper functions for NMValueTypeThomas Haller2019-05-232-0/+52
|
* shared: add accessor functions for NMValueTypeThomas Haller2019-05-231-0/+183
| | | | | | | | | "nm-value-type.h" is a header-only file, as it contains only small accessors that should be inlined. As such, the implementation of these functions is guarded by "#ifdef NM_VALUE_TYPE_DEFINE_FUNCTIONS", so that one can use this header (and NMValueType enum) with less overhead (at compile time).
* shared: add "shared/nm-glib-aux/nm-value-type.h"Thomas Haller2019-05-231-0/+23
| | | | | | | | | | | | | | | | | | | | | Glib has GValue which used for boxing value. Add NMValueType enum, which has a similar purpose, but it's much more limited. - contrary to GValue, the type must be tracked separately from the user-data. That is, the "user-data" is only a pointer of appropriate type, and the knowledge of the actual NMValueType is kept separately. This will be used to have a static list of meta-data that knows the value types, but keeping the values independent of this type information. With GValue this would not be possible. - the use case is much more limited. Just support basic integers, boolean and strings. Nothing fancy. Note that we already do something similar at muliple places. See for example NMVariantAttributeSpec and nm_utils_team_link_watcher_to_string(). These could/should instead use NMValueType.
* shared: add nm_json_aux_gstr_append_*() helperThomas Haller2019-05-232-0/+163
|
* shared: add nm_jansson_json_as_*() helpers to "shared/nm-glib-aux/nm-jansson.h"Thomas Haller2019-05-231-0/+72
|
* shared: add "shared/nm-glib-aux/nm-json-aux.h"Thomas Haller2019-05-232-0/+45
| | | | | This will be a set of JSON related utilities, that are independent of libjansson.
* shared: add NM_GOBJECT_PROPERTIES_DEFINE_NOTIFY() macro as part of ↵Thomas Haller2019-05-231-6/+9
| | | | | | NM_GOBJECT_PROPERTIES_DEFINE() This will be needed independently from NM_GOBJECT_PROPERTIES_DEFINE().
* shared: add nm_utils_strv_make_deep_copied_n() helperThomas Haller2019-05-232-1/+20
|
* shared: add nm_strcmp0()Thomas Haller2019-05-232-3/+32
| | | | | g_strcmp0() is not inlineable, so we first need to call to glib, only to call to libc.
* shared: implement _nm_utils_monotonic_timestamp_initialized() in ↵Thomas Haller2019-05-223-8/+12
| | | | "nm-logging-stub.c"
* shared: fix type shenanigans for data pointer of nm_memdup_maybe_a()Thomas Haller2019-05-161-3/+2
| | | | | | | | | | | | | | | | | | | | | | | The type of the "data" pointer may not be compatible with the type of the "to_free" / output pointer. This is due to constness, and that we are unable in C to remove constness from a type. For example, { const char *const *data = ...; gs_free const char **cpy_to_free = NULL; const char **cpy; cpy = nm_memdup_maybe_a (300, data, NM_PTRARRAY_LEN (data) + 1, &cpy_to_free); } is prefectly valid , but would not have compiled. It shows that "data" is not of type "*(&cpy_to_free)", but rather it might be a non-const pointer of the same type. Fixes: d0e1d0e626d1 ('shared: propagate types in nm_malloc_maybe_a(), nm_malloc0_maybe_a(), nm_memdup_maybe_a()')
* shared: propagate types in nm_malloc_maybe_a(), nm_malloc0_maybe_a(), ↵Thomas Haller2019-05-161-4/+5
| | | | | | | | nm_memdup_maybe_a() The "to_free" pointer should have the suitable type that we are requesting. Use "typeof()" to preserve and propagate the pointer type in the macro.
* shared: use union instead of _nm_alignas() for static hash-seedThomas Haller2019-05-132-6/+12
| | | | | | | | | | We want the the hash-seed array is alined so it can be used both as guint, guint32, and guint64 directly. Don't use _nm_alignas() but instead just add the fields to the union so we get proper alignment. While at at, also let the seed argument to c_siphash_init() be aligned to 64 integers. c_siphash_init() does not require that, but it tries to read the seed as (unaligned) LE 64 bit integers. So, it doesn't hurt.
* shared: add NM_HASH_OBFUSCATE_PTR() macroThomas Haller2019-05-131-2/+12
| | | | | | | | | | | | | | We want to log pointer values to indicate the related parties of a log message. But we should not, because plain pointer values can be used to defeat ASLR. Instead, we have nm_hash_obfuscate_ptr() to managle a pointer and give a distinct (albeit not 100% unique) 64 bit integer for logging. But for the logging messages to be meaning-full, all related parties must use the same static-seed. Add a macro NM_HASH_OBFUSCATE_PTR() that uses a particular seed.
* shared: add nm_dbus_connection_call_start_service_by_name() helperThomas Haller2019-05-121-0/+24
|
* shared: add nm_clear_g_dbus_connection_signal() helperThomas Haller2019-05-121-0/+17
|
* shared: add nm_dbus_connection_signal_subscribe_name_owner_changed() helperThomas Haller2019-05-122-0/+80
| | | | | | | | | ... and nm_dbus_connection_call_get_name_owner(). We are going to use GDBusConnection more instead of GDBusProxy. Hence, these two functions are the standard repertoire and used over and over. Their arguments are complicated enough to warrant a small helper.
* shared: add "shared/nm-glib-aux/nm-dbus-aux.h"Thomas Haller2019-05-122-0/+50
|