This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit f6a0da9913290fb03ccf1fc6ebfcf702422d3f38 Author: gaohedong <[email protected]> AuthorDate: Wed Nov 11 15:32:42 2026 +0800 netutils/iperf: stop instances by pid The process-local instance table cannot work across address spaces. Accept a target pid for abort, validate it in the CLI, and propagate iperf_start() failures to the caller. Signed-off-by: gaohedong <[email protected]> Assisted-by: Kiro:claude-opus-4 --- netutils/iperf/iperf.c | 82 ++++++++++----------------------------------- netutils/iperf/iperf.h | 4 +-- netutils/iperf/iperf_main.c | 24 ++++++++----- 3 files changed, 35 insertions(+), 75 deletions(-) diff --git a/netutils/iperf/iperf.c b/netutils/iperf/iperf.c index df29c25bf..4e391d773 100644 --- a/netutils/iperf/iperf.c +++ b/netutils/iperf/iperf.c @@ -83,9 +83,7 @@ struct iperf_ctrl_t { - FAR struct iperf_ctrl_t *flink; struct iperf_cfg_t cfg; - pid_t pid; pthread_t report_thread; pthread_t traffic_thread; sem_t report_wakeup; @@ -113,13 +111,6 @@ typedef CODE int (*iperf_server_func_t)(FAR struct iperf_ctrl_t *ctrl, socklen_t addrlen, FAR struct sockaddr *remote_addr); -/**************************************************************************** - * Private Data - ****************************************************************************/ - -static pthread_mutex_t g_iperf_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER; -static sq_queue_t g_iperf_ctrl_list; - /**************************************************************************** * Private Functions Prototypes ****************************************************************************/ @@ -1324,7 +1315,6 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) memset(&ctrl, 0, sizeof(ctrl)); memcpy(&ctrl.cfg, cfg, sizeof(*cfg)); - ctrl.pid = getpid(); ctrl.buffer_len = iperf_get_buffer_len(&ctrl); ctrl.buffer = (FAR uint8_t *)malloc(ctrl.buffer_len); if (ctrl.buffer == NULL) @@ -1346,8 +1336,8 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) * SIGINT is control-C and SIGTERM is an explicit kill. SIGPIPE belongs * here too: it is raised when writing to a socket whose peer has gone * away, which is an ordinary way for a test to end. SIGUSR1 is the - * private wake-up the handler sends to the traffic thread; it needs a - * disposition that does not terminate the task. + * private wake-up used by iperf_stop(); it needs a disposition that does + * not terminate the task. * * Dispositions belong to the task group, which is shared with whatever * else is running on this console, so the previous ones are put back @@ -1372,33 +1362,13 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) sigaction(SIGPIPE, &act, &oldpipe); sigaction(SIGUSR1, &act, &oldusr1); - /* Add the instance and create its traffic thread while holding the list - * lock, so abort cannot observe a half-created instance. - */ - - pthread_mutex_lock(&g_iperf_ctrl_mutex); - sq_addlast((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list); ret = -pthread_create(&ctrl.traffic_thread, &attr, iperf_task_traffic, &ctrl); - if (ret < 0) - { - sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list); - } - - pthread_mutex_unlock(&g_iperf_ctrl_mutex); pthread_attr_destroy(&attr); if (ret == 0) { pthread_join(ctrl.traffic_thread, NULL); - - /* Unlink only after the join: iperf_stop() treats an empty list as - * "all stopped" and must never see a ctrl whose stack is gone. - */ - - pthread_mutex_lock(&g_iperf_ctrl_mutex); - sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list); - pthread_mutex_unlock(&g_iperf_ctrl_mutex); } else { @@ -1428,51 +1398,35 @@ int iperf_start(FAR struct iperf_cfg_t *cfg) * ****************************************************************************/ -int iperf_stop(void) +int iperf_stop(pid_t pid) { - FAR sq_entry_t *p; int retry; - int count; - /* A kill that lands after the traffic thread checked finish but before - * it blocked is consumed by the handler without waking it, so keep - * re-sending until every instance has unregistered or the retry budget - * runs out. Only interruptible waits can be broken this way. + if (pid <= 0) + { + return -EINVAL; + } + + /* A signal that lands after the traffic thread checked finish but before + * it blocked may not wake the following blocking call. Keep re-sending + * until the task exits or the retry budget runs out. */ - for (retry = 0; ; retry++) + for (retry = 0; retry < IPERF_ABORT_RETRIES; retry++) { - count = 0; - pthread_mutex_lock(&g_iperf_ctrl_mutex); - sq_for_every(&g_iperf_ctrl_list, p) + if (kill(pid, SIGUSR1) < 0) { - FAR struct iperf_ctrl_t *ctrl = (FAR struct iperf_ctrl_t *)p; - - /* ESRCH means the owning task is gone and the instance can no - * longer be running, so do not wait for it. - */ - - if (kill(ctrl->pid, SIGUSR1) == 0 || errno != ESRCH) + if (errno == ESRCH) { - count++; + return 0; } - } - - pthread_mutex_unlock(&g_iperf_ctrl_mutex); - if (count == 0 || retry >= IPERF_ABORT_RETRIES) - { - break; + return -errno; } usleep(IPERF_ABORT_RETRY_MSEC * 1000); } - if (count > 0) - { - printf("iperf: %d instance(s) did not stop\n", count); - return -ETIMEDOUT; - } - - return 0; + printf("iperf: process %d did not stop\n", pid); + return -ETIMEDOUT; } diff --git a/netutils/iperf/iperf.h b/netutils/iperf/iperf.h index 2c6e73158..cdbf9582f 100644 --- a/netutils/iperf/iperf.h +++ b/netutils/iperf/iperf.h @@ -28,7 +28,7 @@ ****************************************************************************/ #include <nuttx/config.h> - +#include <sys/types.h> #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -104,7 +104,7 @@ int iperf_start(FAR struct iperf_cfg_t *cfg); * ****************************************************************************/ -int iperf_stop(void); +int iperf_stop(pid_t pid); #ifdef __cplusplus } diff --git a/netutils/iperf/iperf_main.c b/netutils/iperf/iperf_main.c index ed74f7c93..505f87021 100644 --- a/netutils/iperf/iperf_main.c +++ b/netutils/iperf/iperf_main.c @@ -64,7 +64,7 @@ struct wifi_iperf_s FAR char *devname; int port; int interval; - int abort; + pid_t abort_pid; int time; int len; }; @@ -83,7 +83,8 @@ struct wifi_iperf_s static void iperf_showusage(FAR const char *progname) { - printf("USAGE: %s [-sua] [-c <ip|cpu>] [-p <port>] [-i <interval>] " + printf("USAGE: %s [-su] [-a <pid>] [-c <ip|cpu>] [-p <port>] " + "[-i <interval>] " "[-t <time>] [--local <path>] [--rpmsg <name>] " "[--vsock <host/guest>] [--devname <dev>]" #ifdef IPERF_HAVE_NETPOLL @@ -109,7 +110,7 @@ static void iperf_showusage(FAR const char *progname) " secs)\n" " -l, --len=<length> length of buffer in bytes to read or write" " (default: TCP 8K, UDP TX 1472, UDP RX 16K)\n" - " -a, --abort abort running iperf\n", + " -a, --abort=<pid> abort the running iperf process\n", IPERF_DEFAULT_TIME); } @@ -188,7 +189,7 @@ int main(int argc, FAR char *argv[]) {"interval", required_argument, 0, 'i'}, {"time", required_argument, 0, 't'}, {"len", required_argument, 0, 'l'}, - {"abort", no_argument, 0, 'a'}, + {"abort", required_argument, 0, 'a'}, {0, 0, 0, 0} }; @@ -205,7 +206,7 @@ int main(int argc, FAR char *argv[]) cfg.sport = IPERF_DEFAULT_PORT; cfg.dport = IPERF_DEFAULT_PORT; - while ((opt = getopt_long(argc, argv, "suac:B:p:i:t:l:", + while ((opt = getopt_long(argc, argv, "sua:c:B:p:i:t:l:", loptions, NULL)) != -1) { switch (opt) @@ -217,7 +218,12 @@ int main(int argc, FAR char *argv[]) cfg.flag |= IPERF_FLAG_UDP; break; case 'a': - iperf.abort = 1; + iperf.abort_pid = atoi(optarg); + if (iperf.abort_pid <= 0) + { + printf("ERROR: invalid pid: %s\n", optarg); + return EXIT_FAILURE; + } break; case 'c': cfg.flag |= IPERF_FLAG_CLIENT; @@ -264,11 +270,11 @@ int main(int argc, FAR char *argv[]) } } - if (iperf.abort != 0) + if (iperf.abort_pid != 0) { - int ret = iperf_stop(); + int ret = iperf_stop(iperf.abort_pid); - printf("ERROR: abort: %d\n", iperf.abort); + printf("Abort iperf process: %d\n", iperf.abort_pid); if (ret < 0) { printf("ERROR: abort incomplete: %d\n", -ret);
