env_dup.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /****************************************************************************
  2. * sched/environ/env_dup.c
  3. *
  4. * Copyright (C) 2007, 2009, 2011, 2013, 2017 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. #ifndef CONFIG_DISABLE_ENVIRON
  40. #include <sys/types.h>
  41. #include <sched.h>
  42. #include <string.h>
  43. #include <errno.h>
  44. #include <nuttx/kmalloc.h>
  45. #include "sched/sched.h"
  46. #include "environ/environ.h"
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: env_dup
  52. *
  53. * Description:
  54. * Copy the internal environment structure of a task. This is the action
  55. * that is performed when a new task is created: The new task has a private,
  56. * exact duplicate of the parent task's environment.
  57. *
  58. * Input Parameters:
  59. * group - The child task group to receive the newly allocated copy of the
  60. * parent task groups environment structure.
  61. *
  62. * Returned Value:
  63. * zero on success
  64. *
  65. * Assumptions:
  66. * Not called from an interrupt handler.
  67. *
  68. ****************************************************************************/
  69. int env_dup(FAR struct task_group_s *group)
  70. {
  71. FAR struct tcb_s *ptcb = this_task();
  72. FAR char *envp = NULL;
  73. size_t envlen;
  74. int ret = OK;
  75. DEBUGASSERT(group != NULL && ptcb != NULL && ptcb->group != NULL);
  76. /* Pre-emption must be disabled throughout the following because the
  77. * environment may be shared.
  78. */
  79. sched_lock();
  80. /* Does the parent task have an environment? */
  81. if (ptcb->group != NULL && ptcb->group->tg_envp != NULL)
  82. {
  83. /* Yes.. The parent task has an environment allocation. */
  84. envlen = ptcb->group->tg_envsize;
  85. envp = NULL;
  86. /* A special case is that the parent has an "empty" environment
  87. * allocation, i.e., there is an allocation in place but it
  88. * contains no variable definitions and, hence, envlen == 0.
  89. */
  90. if (envlen > 0)
  91. {
  92. /* There is an environment, duplicate it */
  93. envp = (FAR char *)kumm_malloc(envlen);
  94. if (envp == NULL)
  95. {
  96. /* The parent's environment can not be inherited due to a
  97. * failure in the allocation of the child environment.
  98. */
  99. envlen = 0;
  100. ret = -ENOMEM;
  101. }
  102. else
  103. {
  104. /* Duplicate the parent environment. */
  105. memcpy(envp, ptcb->group->tg_envp, envlen);
  106. }
  107. }
  108. /* Save the size and child environment allocation. */
  109. group->tg_envsize = envlen;
  110. group->tg_envp = envp;
  111. }
  112. sched_unlock();
  113. return ret;
  114. }
  115. #endif /* CONFIG_DISABLE_ENVIRON */