2015-11-14 14:29:47 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/binfmt_copyargv.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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
|
2015-11-14 14:29:47 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-14 14:29:47 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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.
|
2015-11-14 14:29:47 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
|
|
|
|
|
|
#include "binfmt.h"
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && !defined(CONFIG_BINFMT_DISABLE)
|
2015-11-14 14:29:47 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2020-01-11 17:10:49 +01:00
|
|
|
|
2015-11-14 14:29:47 +01:00
|
|
|
/* This is an artificial limit to detect error conditions where an argv[]
|
|
|
|
* list is not properly terminated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_EXEC_ARGS 256
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-11 17:10:49 +01:00
|
|
|
* Public Functions
|
2015-11-14 14:29:47 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: binfmt_copyargv
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* In the kernel build, the argv list will likely lie in the caller's
|
2021-03-10 21:13:31 +01:00
|
|
|
* address environment and, hence, be inaccessible when we switch to the
|
2015-11-14 14:29:47 +01:00
|
|
|
* address environment of the new process address environment. So we
|
|
|
|
* do not have any real option other than to copy the callers argv[] list.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-11-14 14:29:47 +01:00
|
|
|
* argv - Argument list
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-05-24 09:06:29 +02:00
|
|
|
* A non-zero copy is returned on success.
|
2015-11-14 14:29:47 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
int binfmt_copyargv(FAR char * const **copy, FAR char * const *argv)
|
2015-11-14 14:29:47 +01:00
|
|
|
{
|
2021-05-24 09:06:29 +02:00
|
|
|
FAR char **argvbuf = NULL;
|
2015-11-14 14:29:47 +01:00
|
|
|
FAR char *ptr;
|
|
|
|
size_t argvsize;
|
2022-10-17 17:24:34 +02:00
|
|
|
size_t argsize = 0;
|
|
|
|
int nargs = 0;
|
2015-11-14 14:29:47 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the number of arguments and the size of the argument list */
|
|
|
|
|
|
|
|
if (argv)
|
|
|
|
{
|
|
|
|
for (i = 0; argv[i]; i++)
|
|
|
|
{
|
2020-04-22 10:24:15 +02:00
|
|
|
/* Increment the size of the allocation with the size of the next
|
|
|
|
* string
|
|
|
|
*/
|
2015-11-14 14:29:47 +01:00
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
argsize += strlen(argv[i]) + 1;
|
2015-11-14 14:29:47 +01:00
|
|
|
nargs++;
|
|
|
|
|
2020-04-22 10:24:15 +02:00
|
|
|
/* This is a sanity check to prevent running away with an
|
|
|
|
* unterminated argv[] list.
|
|
|
|
* MAX_EXEC_ARGS should be sufficiently large that this
|
2015-11-14 14:29:47 +01:00
|
|
|
* never happens in normal usage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nargs > MAX_EXEC_ARGS)
|
|
|
|
{
|
2022-10-17 17:24:34 +02:00
|
|
|
berr("ERROR: Too many arguments: %zu\n", argsize);
|
|
|
|
return -E2BIG;
|
2015-11-14 14:29:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
binfo("args=%d argsize=%zu\n", nargs, argsize);
|
2015-11-14 14:29:47 +01:00
|
|
|
|
|
|
|
/* Allocate the argv array and an argument buffer */
|
|
|
|
|
|
|
|
if (argsize > 0)
|
|
|
|
{
|
2021-05-24 09:06:29 +02:00
|
|
|
argvsize = (nargs + 1) * sizeof(FAR char *);
|
2023-08-28 09:39:47 +02:00
|
|
|
ptr = kmm_malloc(argvsize + argsize);
|
2021-05-24 09:06:29 +02:00
|
|
|
if (!ptr)
|
2015-11-14 14:29:47 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: Failed to allocate the argument buffer\n");
|
2022-10-17 17:24:34 +02:00
|
|
|
return -ENOMEM;
|
2015-11-14 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the argv list */
|
|
|
|
|
2021-05-24 09:06:29 +02:00
|
|
|
argvbuf = (FAR char **)ptr;
|
|
|
|
ptr += argvsize;
|
2015-11-14 14:29:47 +01:00
|
|
|
for (i = 0; argv[i]; i++)
|
|
|
|
{
|
2021-05-24 09:06:29 +02:00
|
|
|
argvbuf[i] = ptr;
|
|
|
|
argsize = strlen(argv[i]) + 1;
|
2015-11-14 14:29:47 +01:00
|
|
|
memcpy(ptr, argv[i], argsize);
|
2021-05-24 09:06:29 +02:00
|
|
|
ptr += argsize;
|
2015-11-14 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the argv[] list */
|
|
|
|
|
2021-05-24 09:06:29 +02:00
|
|
|
argvbuf[i] = NULL;
|
2015-11-14 14:29:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
*copy = argvbuf;
|
|
|
|
return OK;
|
2015-11-14 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: binfmt_freeargv
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the copied argv[] list.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2021-05-24 09:06:29 +02:00
|
|
|
* argv - Argument list
|
2015-11-14 14:29:47 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-24 09:06:29 +02:00
|
|
|
void binfmt_freeargv(FAR char * const *argv)
|
2015-11-14 14:29:47 +01:00
|
|
|
{
|
|
|
|
/* Is there an allocated argument buffer */
|
|
|
|
|
2021-05-24 09:06:29 +02:00
|
|
|
if (argv)
|
2015-11-14 14:29:47 +01:00
|
|
|
{
|
|
|
|
/* Free the argument buffer */
|
|
|
|
|
2021-05-24 09:06:29 +02:00
|
|
|
kmm_free((FAR char **)argv);
|
2015-11-14 14:29:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
#endif
|