binfmt_copyargv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /****************************************************************************
  2. * binfmt/binfmt_copyargv.c
  3. *
  4. * Copyright (C) 2009, 2013-2015 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <string.h>
  40. #include <debug.h>
  41. #include <errno.h>
  42. #include <nuttx/kmalloc.h>
  43. #include <nuttx/binfmt/binfmt.h>
  44. #include "binfmt.h"
  45. #ifndef CONFIG_BINFMT_DISABLE
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /* This is an artificial limit to detect error conditions where an argv[]
  50. * list is not properly terminated.
  51. */
  52. #define MAX_EXEC_ARGS 256
  53. /****************************************************************************
  54. * Public Function
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: binfmt_copyargv
  58. *
  59. * Description:
  60. * In the kernel build, the argv list will likely lie in the caller's
  61. * address environment and, hence, by inaccessible when we switch to the
  62. * address environment of the new process address environment. So we
  63. * do not have any real option other than to copy the callers argv[] list.
  64. *
  65. * Input Parameters:
  66. * bin - Load structure
  67. * argv - Argument list
  68. *
  69. * Returned Value:
  70. * Zero (OK) on success; a negated erro value on failure.
  71. *
  72. ****************************************************************************/
  73. int binfmt_copyargv(FAR struct binary_s *bin, FAR char * const *argv)
  74. {
  75. #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
  76. FAR char *ptr;
  77. size_t argvsize;
  78. size_t argsize;
  79. int nargs;
  80. int i;
  81. /* Get the number of arguments and the size of the argument list */
  82. bin->argv = (FAR char **)NULL;
  83. bin->argbuffer = (FAR char *)NULL;
  84. if (argv)
  85. {
  86. argsize = 0;
  87. nargs = 0;
  88. for (i = 0; argv[i]; i++)
  89. {
  90. /* Increment the size of the allocation with the size of the next string */
  91. argsize += (strlen(argv[i]) + 1);
  92. nargs++;
  93. /* This is a sanity check to prevent running away with an unterminated
  94. * argv[] list. MAX_EXEC_ARGS should be sufficiently large that this
  95. * never happens in normal usage.
  96. */
  97. if (nargs > MAX_EXEC_ARGS)
  98. {
  99. berr("ERROR: Too many arguments: %lu\n", (unsigned long)argvsize);
  100. return -E2BIG;
  101. }
  102. }
  103. binfo("args=%d argsize=%lu\n", nargs, (unsigned long)argsize);
  104. /* Allocate the argv array and an argument buffer */
  105. if (argsize > 0)
  106. {
  107. argvsize = (nargs + 1) * sizeof(FAR char *);
  108. bin->argbuffer = (FAR char *)kmm_malloc(argvsize + argsize);
  109. if (!bin->argbuffer)
  110. {
  111. berr("ERROR: Failed to allocate the argument buffer\n");
  112. return -ENOMEM;
  113. }
  114. /* Copy the argv list */
  115. bin->argv = (FAR char **)bin->argbuffer;
  116. ptr = bin->argbuffer + argvsize;
  117. for (i = 0; argv[i]; i++)
  118. {
  119. bin->argv[i] = ptr;
  120. argsize = strlen(argv[i]) + 1;
  121. memcpy(ptr, argv[i], argsize);
  122. ptr += argsize;
  123. }
  124. /* Terminate the argv[] list */
  125. bin->argv[i] = (FAR char *)NULL;
  126. }
  127. }
  128. return OK;
  129. #else
  130. /* Just save the caller's argv pointer */
  131. bin->argv = argv;
  132. return OK;
  133. #endif
  134. }
  135. /****************************************************************************
  136. * Name: binfmt_freeargv
  137. *
  138. * Description:
  139. * Release the copied argv[] list.
  140. *
  141. * Input Parameters:
  142. * binp - Load structure
  143. *
  144. * Returned Value:
  145. * None
  146. *
  147. ****************************************************************************/
  148. #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
  149. void binfmt_freeargv(FAR struct binary_s *binp)
  150. {
  151. /* Is there an allocated argument buffer */
  152. if (binp->argbuffer)
  153. {
  154. /* Free the argument buffer */
  155. kmm_free(binp->argbuffer);
  156. }
  157. /* Nullify the allocated argv[] array and the argument buffer pointers */
  158. binp->argbuffer = (FAR char *)NULL;
  159. binp->argv = (FAR char **)NULL;
  160. }
  161. #endif
  162. #endif /* !CONFIG_BINFMT_DISABLE */