From f11bb3d93d3557847018871cacf918ea24664ddf Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 25 May 2018 15:38:21 +0200 Subject: shared: minor cleanup of nm_utils_get_start_time_for_pid() --- libnm-core/tests/test-general.c | 18 ++++++++++++++++++ shared/nm-utils/nm-shared-utils.c | 11 ++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 0f04aee99b..c853754da0 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -6873,6 +6873,22 @@ test_nm_set_out (void) /*****************************************************************************/ +static void +test_get_start_time_for_pid (void) +{ + guint64 x_start_time; + char x_state; + pid_t x_ppid; + + x_start_time = nm_utils_get_start_time_for_pid (getpid (), &x_state, &x_ppid); + + g_assert (x_start_time > 0); + g_assert (x_ppid == getppid ()); + g_assert (!NM_IN_SET (x_state, '\0', ' ')); +} + +/*****************************************************************************/ + NMTST_DEFINE (); int main (int argc, char **argv) @@ -7026,6 +7042,8 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/route_attributes/parse", test_route_attributes_parse); g_test_add_func ("/core/general/route_attributes/format", test_route_attributes_format); + g_test_add_func ("/core/general/get_start_time_for_pid", test_get_start_time_for_pid); + return g_test_run (); } diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 96e5da350b..d0019c1174 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -1371,8 +1371,7 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid) char filename[256]; gs_free gchar *contents = NULL; size_t length; - gs_strfreev gchar **tokens = NULL; - guint num_tokens; + gs_free const char **tokens = NULL; gchar *p; char state = ' '; gint64 ppid = 0; @@ -1392,7 +1391,7 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid) * processes trying to fool us */ p = strrchr (contents, ')'); - if (p == NULL) + if (!p) goto fail; p += 2; /* skip ') ' */ if (p - contents >= (int) length) @@ -1400,11 +1399,9 @@ nm_utils_get_start_time_for_pid (pid_t pid, char *out_state, pid_t *out_ppid) state = p[0]; - tokens = g_strsplit (p, " ", 0); + tokens = nm_utils_strsplit_set (p, " "); - num_tokens = g_strv_length (tokens); - - if (num_tokens < 20) + if (NM_PTRARRAY_LEN (tokens) < 20) goto fail; if (out_ppid) { -- cgit v1.2.1