summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-09-03 16:41:59 +0200
committerThomas Haller <thaller@redhat.com>2019-09-26 08:18:58 +0200
commitb6acec0fbc2397c60a00e5503c0630aba0496be4 (patch)
tree1c20230802d16af7e538a792a61135ce106556c9
parente28460819c0fb1022a153ab8336841f4aae913dd (diff)
shared: add compat macro for G_PID_FORMAT
Having G_PID_FORMAT macro is useful, but it's only available in recent glib versions. Add a compat implementation and a test that our assumptions hold.
-rw-r--r--shared/nm-glib-aux/nm-glib.h11
-rw-r--r--shared/nm-utils/tests/test-shared-general.c26
2 files changed, 37 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-glib.h b/shared/nm-glib-aux/nm-glib.h
index 9b5f1627bc..98b778f995 100644
--- a/shared/nm-glib-aux/nm-glib.h
+++ b/shared/nm-glib-aux/nm-glib.h
@@ -556,6 +556,17 @@ _nm_g_value_unset (GValue *value)
#define g_value_unset _nm_g_value_unset
#endif
+/* G_PID_FORMAT was added only in 2.53.5. Define it ourself.
+ *
+ * If this was about "pid_t", we would check SIZEOF_PID_T, and set
+ * PRIi32/PRIi16, like systemd does. But it's actually about
+ * GPid, which glib typedefs as an "int".
+ *
+ * There is a test_gpid() that check that GPid is really a typedef
+ * for int. */
+#undef G_PID_FORMAT
+#define G_PID_FORMAT "i"
+
/*****************************************************************************/
#endif /* __NM_GLIB_H__ */
diff --git a/shared/nm-utils/tests/test-shared-general.c b/shared/nm-utils/tests/test-shared-general.c
index 51459cb235..032b79420c 100644
--- a/shared/nm-utils/tests/test-shared-general.c
+++ b/shared/nm-utils/tests/test-shared-general.c
@@ -16,6 +16,31 @@
/*****************************************************************************/
static void
+test_gpid (void)
+{
+ const int *int_ptr;
+ GPid pid = 42;
+
+ /* We redefine G_PID_FORMAT, because it's only available since glib 2.53.5.
+ *
+ * Also, this is the format for GPid, which for glib is always a typedef
+ * for "int". Add a check for that here.
+ *
+ * G_PID_FORMAT is not about pid_t, which might be a smaller int, and which we would
+ * check with SIZEOF_PID_T. */
+ G_STATIC_ASSERT (sizeof (GPid) == sizeof (int));
+
+ g_assert_cmpstr (""G_PID_FORMAT, ==, "i");
+
+ /* check that it's really "int". We will get a compiler warning, if that's not
+ * the case. */
+ int_ptr = &pid;
+ g_assert_cmpint (*int_ptr, ==, 42);
+}
+
+/*****************************************************************************/
+
+static void
test_monotonic_timestamp (void)
{
g_assert (nm_utils_get_monotonic_timestamp_s () > 0);
@@ -502,6 +527,7 @@ int main (int argc, char **argv)
{
nmtst_init (&argc, &argv, TRUE);
+ g_test_add_func ("/general/test_gpid", test_gpid);
g_test_add_func ("/general/test_monotonic_timestamp", test_monotonic_timestamp);
g_test_add_func ("/general/test_nmhash", test_nmhash);
g_test_add_func ("/general/test_nm_make_strv", test_make_strv);