group/tg_info/argv: Make utility function to read argv as string
This creates a generic and safe way to read a process argument vector as string from any context.
This commit is contained in:
parent
a8e5dcbfa9
commit
905cba3ee3
@ -680,7 +680,6 @@ static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
|
||||
size_t buflen, off_t offset)
|
||||
{
|
||||
FAR const char *name;
|
||||
FAR char **argv;
|
||||
size_t remaining;
|
||||
size_t linesize;
|
||||
size_t copysize;
|
||||
@ -710,45 +709,14 @@ static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
|
||||
return totalsize;
|
||||
}
|
||||
|
||||
#ifndef CONFIG_DISABLE_PTHREAD
|
||||
/* Show the pthread argument */
|
||||
/* Show the task / thread argument list (skipping over the name) */
|
||||
|
||||
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
|
||||
{
|
||||
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
|
||||
|
||||
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
|
||||
" %p %p\n",
|
||||
ptcb->cmn.entry.main, ptcb->arg);
|
||||
copysize = procfs_memcpy(procfile->line, linesize, buffer,
|
||||
remaining, &offset);
|
||||
|
||||
totalsize += copysize;
|
||||
buffer += copysize;
|
||||
remaining -= copysize;
|
||||
|
||||
return totalsize;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Show the task argument list (skipping over the name) */
|
||||
|
||||
for (argv = tcb->group->tg_info->argv + 1; *argv; argv++)
|
||||
{
|
||||
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
|
||||
" %s", *argv);
|
||||
copysize = procfs_memcpy(procfile->line, linesize, buffer,
|
||||
remaining, &offset);
|
||||
|
||||
totalsize += copysize;
|
||||
buffer += copysize;
|
||||
remaining -= copysize;
|
||||
|
||||
if (totalsize >= buflen)
|
||||
{
|
||||
return totalsize;
|
||||
}
|
||||
}
|
||||
linesize = group_argvstr(tcb, procfile->line, remaining);
|
||||
copysize = procfs_memcpy(procfile->line, linesize, buffer,
|
||||
remaining, &offset);
|
||||
totalsize += copysize;
|
||||
buffer += copysize;
|
||||
remaining -= copysize;
|
||||
|
||||
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "\n");
|
||||
copysize = procfs_memcpy(procfile->line, linesize, buffer,
|
||||
|
@ -1073,6 +1073,25 @@ FAR struct task_tcb_s *nxtask_setup_vfork(start_t retaddr);
|
||||
pid_t nxtask_start_vfork(FAR struct task_tcb_s *child);
|
||||
void nxtask_abort_vfork(FAR struct task_tcb_s *child, int errcode);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: group_argvstr
|
||||
*
|
||||
* Description:
|
||||
* Safely read the contents of a task's argument vector, into a a safe
|
||||
* buffer. Function skips the process's name.
|
||||
*
|
||||
* Input Parameters:
|
||||
* tcb - tcb of the task.
|
||||
* args - Output buffer for the argument vector.
|
||||
* size - Size of the buffer.
|
||||
*
|
||||
* Returned Value:
|
||||
* The actual string length that was written.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
size_t group_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: group_exitinfo
|
||||
*
|
||||
|
@ -21,6 +21,7 @@
|
||||
CSRCS += group_create.c group_join.c group_leave.c group_find.c
|
||||
CSRCS += group_setupstreams.c group_setupidlefiles.c group_setuptaskfiles.c
|
||||
CSRCS += group_foreachchild.c group_killchildren.c group_signal.c
|
||||
CSRCS += group_argvstr.c
|
||||
|
||||
ifeq ($(CONFIG_SCHED_HAVE_PARENT),y)
|
||||
ifeq ($(CONFIG_SCHED_CHILD_STATUS),y)
|
||||
|
110
sched/group/group_argvstr.c
Normal file
110
sched/group/group_argvstr.c
Normal file
@ -0,0 +1,110 @@
|
||||
/****************************************************************************
|
||||
* sched/group/group_argvstr.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 <sched.h>
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <nuttx/addrenv.h>
|
||||
#include <nuttx/tls.h>
|
||||
|
||||
#include "sched/sched.h"
|
||||
#include "group/group.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: group_argvstr
|
||||
*
|
||||
* Description:
|
||||
* Safely read the contents of a task's argument vector, into a a safe
|
||||
* buffer. Function skips the process's name.
|
||||
*
|
||||
* Input Parameters:
|
||||
* tcb - tcb of the task.
|
||||
* args - Output buffer for the argument vector.
|
||||
* size - Size of the buffer.
|
||||
*
|
||||
* Returned Value:
|
||||
* The actual string length that was written.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
size_t group_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size)
|
||||
{
|
||||
size_t n = 0;
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
bool saved = false;
|
||||
#endif
|
||||
|
||||
/* Perform sanity checks */
|
||||
|
||||
if (!tcb || !tcb->group || !tcb->group->tg_info)
|
||||
{
|
||||
/* Something is very wrong -> get out */
|
||||
|
||||
*args = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
if (tcb->addrenv_own != NULL)
|
||||
{
|
||||
addrenv_select(tcb->addrenv_own);
|
||||
saved = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_DISABLE_PTHREAD
|
||||
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
|
||||
{
|
||||
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
|
||||
|
||||
n += snprintf(args, size, " %p %p", ptcb->cmn.entry.main, ptcb->arg);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
FAR char **argv = tcb->group->tg_info->argv + 1;
|
||||
|
||||
while (*argv != NULL && n < size)
|
||||
{
|
||||
n += snprintf(args + n, size - n, " %s", *argv++);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
if (saved)
|
||||
{
|
||||
addrenv_restore();
|
||||
}
|
||||
#endif
|
||||
|
||||
return n < size - 1 ? n : size - 1;
|
||||
}
|
@ -24,7 +24,6 @@
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <nuttx/addrenv.h>
|
||||
#include <nuttx/arch.h>
|
||||
#include <nuttx/board.h>
|
||||
#include <nuttx/irq.h>
|
||||
@ -43,6 +42,7 @@
|
||||
|
||||
#include "irq/irq.h"
|
||||
#include "sched/sched.h"
|
||||
#include "group/group.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
@ -214,66 +214,6 @@ static void show_stacks(FAR struct tcb_s *rtcb)
|
||||
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: get_argv_str
|
||||
*
|
||||
* Description:
|
||||
* Safely read the contents of a task's argument vector, into a a safe
|
||||
* buffer.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void get_argv_str(FAR struct tcb_s *tcb, FAR char *args, size_t size)
|
||||
{
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
bool saved = false;
|
||||
#endif
|
||||
|
||||
/* Perform sanity checks */
|
||||
|
||||
if (!tcb || !tcb->group || !tcb->group->tg_info)
|
||||
{
|
||||
/* Something is very wrong -> get out */
|
||||
|
||||
*args = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
if (tcb->addrenv_own != NULL)
|
||||
{
|
||||
addrenv_select(tcb->addrenv_own);
|
||||
saved = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_DISABLE_PTHREAD
|
||||
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
|
||||
{
|
||||
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
|
||||
|
||||
snprintf(args, size, " %p %p", ptcb->cmn.entry.main, ptcb->arg);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
FAR char **argv = tcb->group->tg_info->argv + 1;
|
||||
size_t npos = 0;
|
||||
|
||||
while (*argv != NULL && npos < size)
|
||||
{
|
||||
npos += snprintf(args + npos, size - npos, " %s", *argv++);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
if (saved)
|
||||
{
|
||||
addrenv_restore();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dump_task
|
||||
****************************************************************************/
|
||||
@ -313,7 +253,7 @@ static void dump_task(FAR struct tcb_s *tcb, FAR void *arg)
|
||||
|
||||
/* Stringify the argument vector */
|
||||
|
||||
get_argv_str(tcb, args, sizeof(args));
|
||||
group_argvstr(tcb, args, sizeof(args));
|
||||
|
||||
/* Dump interesting properties of this task */
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user