2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* env.c - environment variables storage
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Environment support
|
|
|
|
*/
|
|
|
|
|
2007-07-05 16:01:26 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <driver.h>
|
|
|
|
#include <malloc.h>
|
2007-07-05 16:01:49 +00:00
|
|
|
#include <xfuncs.h>
|
2007-07-05 16:01:47 +00:00
|
|
|
#include <errno.h>
|
2007-09-23 21:29:37 +00:00
|
|
|
#include <init.h>
|
2007-11-09 21:09:25 +00:00
|
|
|
#include <environment.h>
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
static struct env_context root = {
|
|
|
|
.local = LIST_HEAD_INIT(root.local),
|
|
|
|
.global = LIST_HEAD_INIT(root.global),
|
|
|
|
};
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
static struct env_context *context = &root;
|
2007-09-23 21:29:37 +00:00
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* Remove a list of environment variables
|
2007-11-05 13:39:07 +00:00
|
|
|
* @param[in] v Variable anchor to remove
|
2007-10-19 12:56:45 +00:00
|
|
|
*/
|
2012-10-14 11:39:10 +00:00
|
|
|
static void free_context(struct env_context *c)
|
2007-09-23 21:29:37 +00:00
|
|
|
{
|
2012-10-14 11:39:10 +00:00
|
|
|
struct variable_d *v, *tmp;
|
2007-09-23 21:29:37 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
list_for_each_entry_safe(v, tmp, &c->local, list) {
|
|
|
|
free(v->name);
|
|
|
|
free(v->data);
|
|
|
|
list_del(&v->list);
|
2007-09-23 21:29:37 +00:00
|
|
|
free(v);
|
|
|
|
}
|
2012-10-14 11:39:10 +00:00
|
|
|
|
|
|
|
list_for_each_entry_safe(v, tmp, &c->global, list) {
|
|
|
|
free(v->name);
|
|
|
|
free(v->data);
|
|
|
|
list_del(&v->list);
|
|
|
|
free(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(c);
|
2007-09-23 21:29:37 +00:00
|
|
|
}
|
|
|
|
|
2007-11-09 21:09:25 +00:00
|
|
|
/** Read back current context */
|
|
|
|
struct env_context *get_current_context(void)
|
|
|
|
{
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(get_current_context);
|
|
|
|
|
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
2012-10-14 11:39:10 +00:00
|
|
|
* Create a new variable context and put it on the stack
|
2007-10-19 12:56:45 +00:00
|
|
|
*/
|
2007-09-23 21:29:37 +00:00
|
|
|
int env_push_context(void)
|
|
|
|
{
|
|
|
|
struct env_context *c = xzalloc(sizeof(struct env_context));
|
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
INIT_LIST_HEAD(&c->local);
|
|
|
|
INIT_LIST_HEAD(&c->global);
|
2007-09-23 21:29:37 +00:00
|
|
|
|
|
|
|
c->parent = context;
|
|
|
|
context = c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
2012-10-14 11:39:10 +00:00
|
|
|
* free current variable context and restore the previous one
|
2007-10-19 12:56:45 +00:00
|
|
|
*/
|
2007-09-23 21:29:37 +00:00
|
|
|
int env_pop_context(void)
|
|
|
|
{
|
|
|
|
struct env_context *c = context;
|
|
|
|
|
|
|
|
if (context->parent) {
|
|
|
|
c = context->parent;
|
2012-10-14 11:39:10 +00:00
|
|
|
free_context(context);
|
2007-09-23 21:29:37 +00:00
|
|
|
context = c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* Return variable's value
|
|
|
|
* @param[in] var Variable of interest
|
|
|
|
* @return Value as text
|
|
|
|
*/
|
2007-11-09 21:09:25 +00:00
|
|
|
char *var_val(struct variable_d *var)
|
2007-07-05 16:01:26 +00:00
|
|
|
{
|
2012-10-14 11:39:10 +00:00
|
|
|
return var->data;
|
2007-07-05 16:01:26 +00:00
|
|
|
}
|
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* Return variable's name
|
|
|
|
* @param[in] var Variable of interest
|
|
|
|
* @return Name as text
|
|
|
|
*/
|
2007-11-09 21:09:25 +00:00
|
|
|
char *var_name(struct variable_d *var)
|
2007-07-05 16:01:26 +00:00
|
|
|
{
|
2012-10-14 11:39:10 +00:00
|
|
|
return var->name;
|
2007-07-05 16:01:26 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
static const char *getenv_raw(struct list_head *l, const char *name)
|
2007-09-23 21:29:37 +00:00
|
|
|
{
|
2012-10-14 11:39:10 +00:00
|
|
|
struct variable_d *v;
|
|
|
|
|
|
|
|
list_for_each_entry(v, l, list) {
|
|
|
|
if (!strcmp(var_name(v), name))
|
|
|
|
return var_val(v);
|
2007-09-23 21:29:37 +00:00
|
|
|
}
|
2012-10-14 11:39:10 +00:00
|
|
|
|
2007-09-23 21:29:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:58 +00:00
|
|
|
const char *getenv (const char *name)
|
2007-07-05 16:01:26 +00:00
|
|
|
{
|
2007-09-23 21:29:37 +00:00
|
|
|
struct env_context *c;
|
|
|
|
const char *val;
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2007-07-05 16:01:49 +00:00
|
|
|
if (strchr(name, '.')) {
|
2010-10-15 07:29:17 +00:00
|
|
|
const char *ret = NULL;
|
2007-07-05 16:01:49 +00:00
|
|
|
char *devstr = strdup(name);
|
|
|
|
char *par = strchr(devstr, '.');
|
|
|
|
struct device_d *dev;
|
|
|
|
*par = 0;
|
2009-06-11 13:33:49 +00:00
|
|
|
dev = get_device_by_name(devstr);
|
2007-07-05 16:01:49 +00:00
|
|
|
if (dev) {
|
|
|
|
par++;
|
|
|
|
ret = dev_get_param(dev, par);
|
|
|
|
}
|
|
|
|
free(devstr);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2007-09-23 21:29:37 +00:00
|
|
|
c = context;
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
val = getenv_raw(&c->local, name);
|
2007-09-23 21:29:37 +00:00
|
|
|
if (val)
|
|
|
|
return val;
|
|
|
|
|
|
|
|
while (c) {
|
2012-10-14 11:39:10 +00:00
|
|
|
val = getenv_raw(&c->global, name);
|
2007-09-23 21:29:37 +00:00
|
|
|
if (val)
|
|
|
|
return val;
|
|
|
|
c = c->parent;
|
2007-07-05 16:01:26 +00:00
|
|
|
}
|
2007-09-23 21:29:37 +00:00
|
|
|
return NULL;
|
2007-07-05 16:01:26 +00:00
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(getenv);
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
static int setenv_raw(struct list_head *l, const char *name, const char *value)
|
2007-07-05 16:01:26 +00:00
|
|
|
{
|
2012-10-14 11:39:10 +00:00
|
|
|
struct variable_d *v;
|
2007-09-13 14:06:02 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
list_for_each_entry(v, l, list) {
|
|
|
|
if (!strcmp(v->name, name)) {
|
2007-09-13 14:06:02 +00:00
|
|
|
if (value) {
|
2012-10-14 11:39:10 +00:00
|
|
|
free(v->data);
|
|
|
|
v->data = xstrdup(value);
|
|
|
|
|
2007-09-13 14:06:02 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2012-10-14 11:39:10 +00:00
|
|
|
list_del(&v->list);
|
|
|
|
free(v->name);
|
|
|
|
free(v->data);
|
|
|
|
free(v);
|
|
|
|
|
2007-09-13 14:06:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-14 11:39:10 +00:00
|
|
|
|
|
|
|
if (value) {
|
|
|
|
v = xzalloc(sizeof(*v));
|
|
|
|
v->name = xstrdup(name);
|
|
|
|
v->data = xstrdup(value);
|
|
|
|
list_add_tail(&v->list, l);
|
|
|
|
}
|
|
|
|
|
2007-09-23 21:29:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-20 15:46:07 +00:00
|
|
|
int setenv(const char *_name, const char *value)
|
2007-09-23 21:29:37 +00:00
|
|
|
{
|
2008-08-20 15:46:07 +00:00
|
|
|
char *name = strdup(_name);
|
2007-09-23 21:29:37 +00:00
|
|
|
char *par;
|
2008-08-20 15:46:07 +00:00
|
|
|
int ret = 0;
|
2012-10-14 11:39:10 +00:00
|
|
|
struct list_head *list;
|
2007-09-23 21:29:37 +00:00
|
|
|
|
2011-05-14 20:33:51 +00:00
|
|
|
if (value && !*value)
|
2007-09-23 21:29:37 +00:00
|
|
|
value = NULL;
|
|
|
|
|
2008-08-20 15:46:07 +00:00
|
|
|
|
2007-09-23 21:29:37 +00:00
|
|
|
if ((par = strchr(name, '.'))) {
|
|
|
|
struct device_d *dev;
|
2008-08-20 15:46:07 +00:00
|
|
|
|
2007-09-23 21:29:37 +00:00
|
|
|
*par++ = 0;
|
2009-06-11 13:33:49 +00:00
|
|
|
dev = get_device_by_name(name);
|
2008-08-20 15:46:07 +00:00
|
|
|
if (dev)
|
|
|
|
ret = dev_set_param(dev, par, value);
|
|
|
|
else
|
|
|
|
ret = -ENODEV;
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = -ret;
|
2008-08-20 15:46:07 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
2007-09-23 21:29:37 +00:00
|
|
|
perror("set parameter");
|
2008-08-20 15:46:07 +00:00
|
|
|
|
|
|
|
goto out;
|
2007-09-23 21:29:37 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
if (getenv_raw(&context->global, name))
|
|
|
|
list = &context->global;
|
2007-09-23 21:29:37 +00:00
|
|
|
else
|
2012-10-14 11:39:10 +00:00
|
|
|
list = &context->local;
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
ret = setenv_raw(list, name, value);
|
2008-08-20 15:46:07 +00:00
|
|
|
out:
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:26 +00:00
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(setenv);
|
2007-07-05 16:01:26 +00:00
|
|
|
|
2007-09-23 21:29:37 +00:00
|
|
|
int export(const char *varname)
|
|
|
|
{
|
2012-10-14 11:39:10 +00:00
|
|
|
const char *val = getenv_raw(&context->local, varname);
|
2007-09-23 21:29:37 +00:00
|
|
|
|
|
|
|
if (val) {
|
2012-10-14 11:39:10 +00:00
|
|
|
setenv_raw(&context->global, varname, val);
|
|
|
|
setenv_raw(&context->local, varname, NULL);
|
2007-09-23 21:29:37 +00:00
|
|
|
}
|
2011-10-06 15:01:19 +00:00
|
|
|
return 0;
|
2007-09-23 21:29:37 +00:00
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(export);
|
2011-11-26 12:53:42 +00:00
|
|
|
|
|
|
|
void export_env_ull(const char *name, unsigned long long val)
|
|
|
|
{
|
2014-04-29 19:16:02 +00:00
|
|
|
char *valstr = asprintf("%llu", val);
|
2011-11-26 12:53:42 +00:00
|
|
|
|
|
|
|
setenv(name, valstr);
|
|
|
|
export(name);
|
|
|
|
|
|
|
|
kfree(valstr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(export_env_ull);
|
|
|
|
|
2013-09-23 10:49:04 +00:00
|
|
|
/*
|
|
|
|
* Like regular getenv, but never returns an empty string.
|
|
|
|
* If the string is empty, NULL is returned instead
|
|
|
|
*/
|
|
|
|
const char *getenv_nonempty(const char *var)
|
2011-11-26 12:53:42 +00:00
|
|
|
{
|
2013-09-23 10:49:04 +00:00
|
|
|
const char *val = getenv(var);
|
2011-11-26 12:53:42 +00:00
|
|
|
|
2013-09-23 10:49:04 +00:00
|
|
|
if (val && *val)
|
|
|
|
return val;
|
2011-11-26 12:53:42 +00:00
|
|
|
|
2013-09-23 10:49:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(getenv_nonempty);
|
|
|
|
|
|
|
|
int getenv_ull(const char *var , unsigned long long *val)
|
|
|
|
{
|
|
|
|
const char *valstr = getenv(var);
|
|
|
|
|
|
|
|
if (!valstr || !*valstr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*val = simple_strtoull(valstr, NULL, 0);
|
|
|
|
|
|
|
|
return 0;
|
2011-11-26 12:53:42 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(getenv_ull);
|
2013-09-23 10:49:04 +00:00
|
|
|
|
|
|
|
int getenv_ul(const char *var , unsigned long *val)
|
|
|
|
{
|
|
|
|
const char *valstr = getenv(var);
|
|
|
|
|
|
|
|
if (!valstr || !*valstr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*val = simple_strtoul(valstr, NULL, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(getenv_ul);
|
|
|
|
|
|
|
|
int getenv_uint(const char *var , unsigned int *val)
|
|
|
|
{
|
|
|
|
const char *valstr = getenv(var);
|
|
|
|
|
|
|
|
if (!valstr || !*valstr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*val = simple_strtoul(valstr, NULL, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(getenv_uint);
|
|
|
|
|
|
|
|
int getenv_bool(const char *var, int *val)
|
|
|
|
{
|
|
|
|
const char *valstr = getenv(var);
|
|
|
|
|
|
|
|
if (!valstr || !*valstr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!*valstr)
|
|
|
|
*val = false;
|
|
|
|
else if (*valstr == '0')
|
|
|
|
*val = false;
|
|
|
|
else
|
|
|
|
*val = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(getenv_bool);
|