From b6acec0fbc2397c60a00e5503c0630aba0496be4 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 3 Sep 2019 16:41:59 +0200 Subject: [PATCH] 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. --- shared/nm-glib-aux/nm-glib.h | 11 +++++++++ shared/nm-utils/tests/test-shared-general.c | 26 +++++++++++++++++++++ 2 files changed, 37 insertions(+) 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 @@ -15,6 +15,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) { @@ -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);