2007-06-30 21:39:17 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 21:53:29 +02:00
|
|
|
* sched/environ/env_setenv.c
|
2007-06-30 21:39:17 +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 21:39:17 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-06-30 21:39:17 +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 21:39:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2011-04-05 17:50:01 +02:00
|
|
|
#include <stdlib.h>
|
2007-06-30 21:39:17 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-06-30 21:39:17 +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 21:39:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: setenv
|
2007-06-30 21:39:17 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The setenv() function adds the variable name to the environment with the
|
|
|
|
* specified 'value' if the varialbe 'name" does not exist. If the 'name'
|
|
|
|
* does exist in the environment, then its value is changed to 'value' if
|
|
|
|
* 'overwrite' is non-zero; if 'overwrite' is zero, then the value of name
|
|
|
|
* unaltered.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-06-30 21:39:17 +02:00
|
|
|
* name - The name of the variable to change
|
|
|
|
* value - The new value of the variable
|
|
|
|
* overwrite - Replace any existing value if non-zero.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-06-30 21:39:17 +02:00
|
|
|
* Zero on success
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Not called from an interrupt handler
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
int setenv(FAR const char *name, FAR const char *value, int overwrite)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *rtcb;
|
2013-01-26 00:21:27 +01:00
|
|
|
FAR struct task_group_s *group;
|
|
|
|
FAR char *pvar;
|
2022-04-16 13:38:17 +02:00
|
|
|
FAR char **envp;
|
2024-03-27 03:41:15 +01:00
|
|
|
ssize_t envc;
|
|
|
|
ssize_t envpc;
|
2023-02-09 07:59:03 +01:00
|
|
|
ssize_t ret = OK;
|
2013-01-26 00:21:27 +01:00
|
|
|
int varlen;
|
2007-06-30 21:39:17 +02:00
|
|
|
|
|
|
|
/* Verify input parameter */
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if no value is provided, then this is the same as unsetenv (unless
|
|
|
|
* overwrite is false)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!value || *value == '\0')
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* If overwrite is set then this is the same as unsetenv */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
|
|
|
if (overwrite)
|
|
|
|
{
|
|
|
|
return unsetenv(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-08 18:46:31 +01:00
|
|
|
/* Otherwise, it is a request to remove a variable without
|
|
|
|
* altering it?
|
|
|
|
*/
|
2007-06-30 21:39:17 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
/* Get a reference to the thread-private environ in the TCB. */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
|
|
|
sched_lock();
|
2016-02-07 00:44:41 +01:00
|
|
|
rtcb = this_task();
|
2013-01-26 00:21:27 +01:00
|
|
|
group = rtcb->group;
|
|
|
|
DEBUGASSERT(group);
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
/* Check if the variable already exists */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
if (group->tg_envp && (ret = env_findvar(group, name)) >= 0)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
2013-01-26 00:21:27 +01:00
|
|
|
/* It does! Do we have permission to overwrite the existing value? */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
|
|
|
if (!overwrite)
|
|
|
|
{
|
|
|
|
/* No.. then just return success */
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. just remove the name=value pair from the environment. It will
|
|
|
|
* be added again below. Note that we are responsible for reallocating
|
|
|
|
* the environment buffer; this will happen below.
|
|
|
|
*/
|
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
env_removevar(group, ret);
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
/* Check current envirments count */
|
|
|
|
|
|
|
|
DEBUGASSERT(group->tg_envc < SSIZE_MAX);
|
|
|
|
|
2021-02-08 18:46:31 +01:00
|
|
|
/* Get the size of the new name=value string.
|
|
|
|
* The +2 is for the '=' and for null terminator
|
2012-07-14 21:30:31 +02:00
|
|
|
*/
|
2007-06-30 21:39:17 +02:00
|
|
|
|
|
|
|
varlen = strlen(name) + strlen(value) + 2;
|
|
|
|
|
|
|
|
/* Then allocate or reallocate the environment buffer */
|
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
pvar = group_malloc(group, varlen);
|
|
|
|
if (pvar == NULL)
|
|
|
|
{
|
|
|
|
ret = ENOMEM;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2024-03-27 03:41:15 +01:00
|
|
|
envc = group->tg_envc;
|
|
|
|
|
|
|
|
if (group->tg_envp == NULL)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
2024-03-27 03:41:15 +01:00
|
|
|
envpc = SCHED_ENVIRON_RESERVED + 2;
|
|
|
|
|
|
|
|
envp = group_malloc(group, sizeof(*envp) * envpc);
|
2022-04-16 13:38:17 +02:00
|
|
|
if (envp == NULL)
|
|
|
|
{
|
|
|
|
ret = ENOMEM;
|
|
|
|
goto errout_with_var;
|
|
|
|
}
|
2024-03-27 03:41:15 +01:00
|
|
|
|
|
|
|
group->tg_envp = envp;
|
|
|
|
group->tg_envpc = envpc;
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
2024-03-27 03:41:15 +01:00
|
|
|
else if (envc >= group->tg_envpc - 1)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
2024-03-27 03:41:15 +01:00
|
|
|
envpc = envc + SCHED_ENVIRON_RESERVED + 2;
|
|
|
|
|
|
|
|
envp = group_realloc(group, group->tg_envp, sizeof(*envp) * envpc);
|
2022-04-16 13:38:17 +02:00
|
|
|
if (envp == NULL)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
|
|
|
ret = ENOMEM;
|
2022-04-16 13:38:17 +02:00
|
|
|
goto errout_with_var;
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 03:41:15 +01:00
|
|
|
group->tg_envp = envp;
|
|
|
|
group->tg_envpc = envpc;
|
|
|
|
}
|
2022-04-13 09:05:53 +02:00
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
/* Save the new buffer and count */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2024-03-27 03:41:15 +01:00
|
|
|
group->tg_envp[envc++] = pvar;
|
|
|
|
group->tg_envp[envc] = NULL;
|
2023-02-09 07:59:03 +01:00
|
|
|
group->tg_envc = envc;
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
/* Now, put the new name=value string into the environment buffer */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2023-03-05 15:34:33 +01:00
|
|
|
snprintf(pvar, varlen, "%s=%s", name, value);
|
2007-06-30 21:39:17 +02:00
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
errout_with_var:
|
|
|
|
group_free(group, pvar);
|
2007-06-30 21:39:17 +02:00
|
|
|
errout_with_lock:
|
|
|
|
sched_unlock();
|
|
|
|
errout:
|
2014-08-29 01:00:24 +02:00
|
|
|
set_errno(ret);
|
2007-06-30 21:39:17 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_ENVIRON */
|