2007-06-30 19:05:44 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 21:53:29 +02:00
|
|
|
* sched/environ/env_dup.c
|
2007-06-30 19:05:44 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +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
|
2007-06-30 19:05:44 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-06-30 19:05:44 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +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.
|
2007-06-30 19:05:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <sys/types.h>
|
2007-06-30 19:05:44 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-06-30 19:05:44 +02:00
|
|
|
#include <errno.h>
|
2011-04-03 22:41:49 +02:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 21:53:29 +02:00
|
|
|
#include "environ/environ.h"
|
2007-06-30 19:05:44 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: env_dup
|
2007-06-30 19:05:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Copy the internal environment structure of a task. This is the action
|
2021-02-08 18:46:31 +01:00
|
|
|
* that is performed when a new task is created: The new task has a
|
|
|
|
* private, exact duplicate of the parent task's environment.
|
2007-06-30 19:05:44 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2022-04-04 12:11:06 +02:00
|
|
|
* group - The child task group to receive the newly allocated copy of
|
|
|
|
* the parent task groups environment structure.
|
|
|
|
* envcp - Pointer to the environment strings to copy.
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-06-30 19:05:44 +02:00
|
|
|
* zero on success
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Not called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
|
2007-06-30 19:05:44 +02:00
|
|
|
{
|
2022-04-16 13:38:17 +02:00
|
|
|
FAR char **envp = NULL;
|
|
|
|
size_t envc = 0;
|
2023-02-08 04:29:09 +01:00
|
|
|
size_t size;
|
2007-06-30 19:05:44 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
DEBUGASSERT(group != NULL);
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* Is there an environment ? */
|
2007-06-30 19:05:44 +02:00
|
|
|
|
2024-05-17 00:11:52 +02:00
|
|
|
if (envcp != NULL && group->tg_envp == NULL)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
2024-03-05 03:39:27 +01:00
|
|
|
/* Pre-emption must be disabled throughout the following because the
|
|
|
|
* environment may be shared.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* Count the strings */
|
2007-06-30 19:05:44 +02:00
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
while (envcp[envc] != NULL)
|
2022-04-16 13:38:17 +02:00
|
|
|
{
|
|
|
|
envc++;
|
|
|
|
}
|
2017-09-19 03:02:54 +02:00
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
group->tg_envc = envc;
|
2024-03-27 03:41:15 +01:00
|
|
|
group->tg_envpc = (envc + SCHED_ENVIRON_RESERVED + 1);
|
2023-02-09 07:59:03 +01:00
|
|
|
|
2017-09-19 03:02:54 +02:00
|
|
|
/* A special case is that the parent has an "empty" environment
|
|
|
|
* allocation, i.e., there is an allocation in place but it
|
2022-04-16 13:38:17 +02:00
|
|
|
* contains no variable definitions and, hence, envc == 0.
|
2017-09-19 03:02:54 +02:00
|
|
|
*/
|
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
if (envc > 0)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
2017-09-19 03:02:54 +02:00
|
|
|
/* There is an environment, duplicate it */
|
|
|
|
|
2024-03-27 03:41:15 +01:00
|
|
|
envp = group_malloc(group, sizeof(*envp) * group->tg_envpc);
|
2017-09-19 03:02:54 +02:00
|
|
|
if (envp == NULL)
|
|
|
|
{
|
|
|
|
/* The parent's environment can not be inherited due to a
|
|
|
|
* failure in the allocation of the child environment.
|
|
|
|
*/
|
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
ret = -ENOMEM;
|
2017-09-19 03:02:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-16 13:38:17 +02:00
|
|
|
envp[envc] = NULL;
|
|
|
|
|
2019-06-30 18:35:10 +02:00
|
|
|
/* Duplicate the parent environment. */
|
2017-09-19 03:02:54 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
while (envc-- > 0)
|
|
|
|
{
|
2023-02-08 04:29:09 +01:00
|
|
|
size = strlen(envcp[envc]) + 1;
|
|
|
|
envp[envc] = group_malloc(group, size);
|
2022-04-16 13:38:17 +02:00
|
|
|
if (envp[envc] == NULL)
|
|
|
|
{
|
|
|
|
while (envp[++envc] != NULL)
|
|
|
|
{
|
|
|
|
group_free(group, envp[envc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
group_free(group, envp);
|
2023-10-12 10:06:20 +02:00
|
|
|
envp = NULL;
|
2022-04-16 13:38:17 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-08 04:29:09 +01:00
|
|
|
strlcpy(envp[envc], envcp[envc], size);
|
2022-04-16 13:38:17 +02:00
|
|
|
}
|
2017-09-19 03:02:54 +02:00
|
|
|
}
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
2017-09-19 03:02:54 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
/* Save the child environment allocation. */
|
2017-09-19 03:02:54 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
group->tg_envp = envp;
|
2024-03-05 03:39:27 +01:00
|
|
|
|
|
|
|
sched_unlock();
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
2007-06-30 19:05:44 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_ENVIRON */
|