nuttx-apps/nshlib/nsh_wait.c
anjiahao c916f163f2 nsh:support wait command to wait task exit.
usage like:
  nsh> sleep 10 &
  [5:100]
  nsh>set pid1=$!
  nsh>sleep 15 &
  [6:100]
  nsh>set pid2=$!
  nsh>wait $pid1 $pid2

'wait' command will block nsh from running until pid1 and pid2 finish.
This is useful for parallel requirements to perform certain tasks

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-02 21:13:54 +08:00

151 lines
4.0 KiB
C

/****************************************************************************
* apps/nshlib/nsh_wait.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/sched.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include "nsh.h"
#include "nsh_console.h"
#if !defined(CONFIG_NSH_DISABLE_WAIT) && defined(CONFIG_SCHED_WAITPID) && \
!defined(CONFIG_DISABLE_PTHREAD)
static const char g_groupid[] = "Group:";
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_wait
*
* Description:
* Handle 'cmd_wait' command from terminal.
* wait pid1 [pid2 [pid3] ..] - wait for a pid to exit.
*
* Input Parameters:
* vtbl - The NSH console.
* argc - Amount of argument strings in command.
* argv - The argument strings.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int cmd_wait(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
FAR char *nextline;
FAR char *line;
char buf[128];
char path[32];
int status = 0;
int ret = OK;
pid_t self;
pid_t tid;
int fd;
int i;
if (argc == 1)
{
return OK;
}
self = getpid();
for (i = 1; i < argc; i++)
{
tid = atoi(argv[i]);
if (tid == 0)
{
continue;
}
snprintf(path, sizeof(path), "/proc/%d/status", tid);
fd = open(path, O_RDONLY);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "wait", NSH_ERRNO);
continue;
}
ret = read(fd, buf, sizeof(buf) - 1);
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "wait", NSH_ERRNO);
close(fd);
continue;
}
close(fd);
nextline = buf;
do
{
line = nextline;
for (nextline++;
*nextline != '\0' && *nextline != '\n';
nextline++);
if (*nextline == '\n')
{
*nextline++ = '\0';
}
else
{
nextline = NULL;
}
if (strncmp(line, g_groupid, sizeof(g_groupid) - 1) == 0)
{
if (atoi(line + sizeof(g_groupid)) == self)
{
ret = pthread_join(tid, (FAR pthread_addr_t *)&status);
}
else
{
ret = waitpid(tid, &status, 0);
}
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed,
argv[0], "wait", NSH_ERRNO);
}
break;
}
}
while (nextline != NULL);
}
return ret < 0 ? ret : status;
}
#endif /* CONFIG_NSH_DISABLE_WAIT */