From e9bdecce4d7a245dc54e4dcc1d2f724733f8125d Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Tue, 21 Nov 2023 01:51:48 +0200 Subject: [PATCH] Share TEST_FAIL/TEST_ALLOC_FAIL/GET_FAIL/GET_ALLOC_FAIL handler Move the hostapd and wpa_supplicant control interface handlers into a shared functions instead of duplicated implementation. Signed-off-by: Benjamin Berg --- hostapd/ctrl_iface.c | 77 ++----------------------------------- src/utils/os.h | 16 ++++++-- src/utils/os_unix.c | 56 +++++++++++++++++++++++++-- wpa_supplicant/ctrl_iface.c | 74 ++--------------------------------- 4 files changed, 72 insertions(+), 151 deletions(-) diff --git a/hostapd/ctrl_iface.c b/hostapd/ctrl_iface.c index 2bc54a52f..9d42b60ae 100644 --- a/hostapd/ctrl_iface.c +++ b/hostapd/ctrl_iface.c @@ -2006,74 +2006,6 @@ done: } -static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd) -{ -#ifdef WPA_TRACE_BFD - char *pos; - - wpa_trace_fail_after = atoi(cmd); - pos = os_strchr(cmd, ':'); - if (pos) { - pos++; - os_strlcpy(wpa_trace_fail_func, pos, - sizeof(wpa_trace_fail_func)); - } else { - wpa_trace_fail_after = 0; - } - - return 0; -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - -static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd, - char *buf, size_t buflen) -{ -#ifdef WPA_TRACE_BFD - return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after, - wpa_trace_fail_func); -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - -static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd) -{ -#ifdef WPA_TRACE_BFD - char *pos; - - wpa_trace_test_fail_after = atoi(cmd); - pos = os_strchr(cmd, ':'); - if (pos) { - pos++; - os_strlcpy(wpa_trace_test_fail_func, pos, - sizeof(wpa_trace_test_fail_func)); - } else { - wpa_trace_test_fail_after = 0; - } - - return 0; -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - -static int hostapd_ctrl_get_fail(struct hostapd_data *hapd, - char *buf, size_t buflen) -{ -#ifdef WPA_TRACE_BFD - return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after, - wpa_trace_test_fail_func); -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd) { struct sta_info *sta; @@ -3703,16 +3635,15 @@ static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd, if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0) reply_len = -1; } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) { - if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0) + if (testing_set_fail_pattern(true, buf + 16) < 0) reply_len = -1; } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) { - reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply, - reply_size); + reply_len = testing_get_fail_pattern(true, reply, reply_size); } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) { - if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0) + if (testing_set_fail_pattern(false, buf + 10) < 0) reply_len = -1; } else if (os_strcmp(buf, "GET_FAIL") == 0) { - reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size); + reply_len = testing_get_fail_pattern(false, reply, reply_size); } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) { if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0) reply_len = -1; diff --git a/src/utils/os.h b/src/utils/os.h index 21ba5c3ff..210d74c10 100644 --- a/src/utils/os.h +++ b/src/utils/os.h @@ -669,12 +669,20 @@ int os_exec(const char *program, const char *arg, int wait_completion); #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) #define TEST_FAIL() testing_test_fail() int testing_test_fail(void); -extern char wpa_trace_fail_func[256]; -extern unsigned int wpa_trace_fail_after; -extern char wpa_trace_test_fail_func[256]; -extern unsigned int wpa_trace_test_fail_after; +int testing_set_fail_pattern(bool is_alloc, char *patterns); +int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen); #else #define TEST_FAIL() 0 +static inline int testing_set_fail_pattern(bool is_alloc, char *patterns) +{ + return -1; +} + +static inline int testing_get_fail_pattern(bool is_alloc, char *buf, + size_t buflen) +{ + return -1; +} #endif #endif /* OS_H */ diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c index 258deef9d..6fa366415 100644 --- a/src/utils/os_unix.c +++ b/src/utils/os_unix.c @@ -540,8 +540,8 @@ void * os_memdup(const void *src, size_t len) #ifdef WPA_TRACE #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) -char wpa_trace_fail_func[256] = { 0 }; -unsigned int wpa_trace_fail_after; +static char wpa_trace_fail_func[256] = { 0 }; +static unsigned int wpa_trace_fail_after; static int testing_fail_alloc(void) { @@ -626,8 +626,8 @@ static int testing_fail_alloc(void) } -char wpa_trace_test_fail_func[256] = { 0 }; -unsigned int wpa_trace_test_fail_after; +static char wpa_trace_test_fail_func[256] = { 0 }; +static unsigned int wpa_trace_test_fail_after; int testing_test_fail(void) { @@ -697,6 +697,54 @@ int testing_test_fail(void) return 0; } + +int testing_set_fail_pattern(bool is_alloc, char *patterns) +{ +#ifdef WPA_TRACE_BFD + char *pos; + + if (is_alloc) { + wpa_trace_fail_after = atoi(patterns); + pos = os_strchr(patterns, ':'); + if (pos) { + pos++; + os_strlcpy(wpa_trace_fail_func, pos, + sizeof(wpa_trace_fail_func)); + } else { + wpa_trace_fail_after = 0; + } + } else { + wpa_trace_test_fail_after = atoi(patterns); + pos = os_strchr(patterns, ':'); + if (pos) { + pos++; + os_strlcpy(wpa_trace_test_fail_func, pos, + sizeof(wpa_trace_test_fail_func)); + } else { + wpa_trace_test_fail_after = 0; + } + } + + return 0; +#else /* WPA_TRACE_BFD */ + return -1; +#endif /* WPA_TRACE_BFD */ +} + + +int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen) +{ +#ifdef WPA_TRACE_BFD + if (is_alloc) + return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after, + wpa_trace_fail_func); + return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after, + wpa_trace_test_fail_func); +#else /* WPA_TRACE_BFD */ + return -1; +#endif /* WPA_TRACE_BFD */ +} + #else static inline int testing_fail_alloc(void) diff --git a/wpa_supplicant/ctrl_iface.c b/wpa_supplicant/ctrl_iface.c index a68802e49..b716fa774 100644 --- a/wpa_supplicant/ctrl_iface.c +++ b/wpa_supplicant/ctrl_iface.c @@ -10134,72 +10134,6 @@ done: } -static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd) -{ -#ifdef WPA_TRACE_BFD - char *pos; - - wpa_trace_fail_after = atoi(cmd); - pos = os_strchr(cmd, ':'); - if (pos) { - pos++; - os_strlcpy(wpa_trace_fail_func, pos, - sizeof(wpa_trace_fail_func)); - } else { - wpa_trace_fail_after = 0; - } - return 0; -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - -static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s, - char *buf, size_t buflen) -{ -#ifdef WPA_TRACE_BFD - return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after, - wpa_trace_fail_func); -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - -static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd) -{ -#ifdef WPA_TRACE_BFD - char *pos; - - wpa_trace_test_fail_after = atoi(cmd); - pos = os_strchr(cmd, ':'); - if (pos) { - pos++; - os_strlcpy(wpa_trace_test_fail_func, pos, - sizeof(wpa_trace_test_fail_func)); - } else { - wpa_trace_test_fail_after = 0; - } - return 0; -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - -static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s, - char *buf, size_t buflen) -{ -#ifdef WPA_TRACE_BFD - return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after, - wpa_trace_test_fail_func); -#else /* WPA_TRACE_BFD */ - return -1; -#endif /* WPA_TRACE_BFD */ -} - - static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx) { struct wpa_supplicant *wpa_s = eloop_ctx; @@ -12886,15 +12820,15 @@ char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s, if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0) reply_len = -1; } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) { - if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0) + if (testing_set_fail_pattern(true, buf + 16) < 0) reply_len = -1; } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) { - reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size); + reply_len = testing_get_fail_pattern(true, reply, reply_size); } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) { - if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0) + if (testing_set_fail_pattern(false, buf + 10) < 0) reply_len = -1; } else if (os_strcmp(buf, "GET_FAIL") == 0) { - reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size); + reply_len = testing_get_fail_pattern(false, reply, reply_size); } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) { if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0) reply_len = -1;