2016-01-29 18:49:55 +01:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/envlist.h"
|
2009-01-30 20:59:17 +01:00
|
|
|
|
|
|
|
struct envlist_entry {
|
2023-03-15 04:26:49 +01:00
|
|
|
const char *ev_var; /* actual env value */
|
|
|
|
QLIST_ENTRY(envlist_entry) ev_link;
|
2009-01-30 20:59:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct envlist {
|
2023-03-15 04:26:49 +01:00
|
|
|
QLIST_HEAD(, envlist_entry) el_entries; /* actual entries */
|
|
|
|
size_t el_count; /* number of entries */
|
2009-01-30 20:59:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int envlist_parse(envlist_t *envlist,
|
|
|
|
const char *env, int (*)(envlist_t *, const char *));
|
|
|
|
|
|
|
|
/*
|
2017-03-20 18:38:28 +01:00
|
|
|
* Allocates new envlist and returns pointer to it.
|
2009-01-30 20:59:17 +01:00
|
|
|
*/
|
|
|
|
envlist_t *
|
|
|
|
envlist_create(void)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
envlist_t *envlist;
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
envlist = g_malloc(sizeof(*envlist));
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
QLIST_INIT(&envlist->el_entries);
|
|
|
|
envlist->el_count = 0;
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
return (envlist);
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Releases given envlist and its entries.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
envlist_free(envlist_t *envlist)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
struct envlist_entry *entry;
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
assert(envlist != NULL);
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
while (envlist->el_entries.lh_first != NULL) {
|
|
|
|
entry = envlist->el_entries.lh_first;
|
|
|
|
QLIST_REMOVE(entry, ev_link);
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
g_free((char *)entry->ev_var);
|
|
|
|
g_free(entry);
|
|
|
|
}
|
|
|
|
g_free(envlist);
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses comma separated list of set/modify environment
|
|
|
|
* variable entries and updates given enlist accordingly.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
* envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
|
|
|
|
*
|
|
|
|
* inserts/sets environment variables HOME and SHELL.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, errno otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
envlist_parse_set(envlist_t *envlist, const char *env)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
return (envlist_parse(envlist, env, &envlist_setenv));
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses comma separated list of unset environment variable
|
|
|
|
* entries and removes given variables from given envlist.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, errno otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
envlist_parse_unset(envlist_t *envlist, const char *env)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
return (envlist_parse(envlist, env, &envlist_unsetenv));
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses comma separated list of set, modify or unset entries
|
|
|
|
* and calls given callback for each entry.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success, errno otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
envlist_parse(envlist_t *envlist, const char *env,
|
|
|
|
int (*callback)(envlist_t *, const char *))
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
char *tmpenv, *envvar;
|
|
|
|
char *envsave = NULL;
|
2015-02-06 18:59:53 +01:00
|
|
|
int ret = 0;
|
|
|
|
assert(callback != NULL);
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
if ((envlist == NULL) || (env == NULL))
|
|
|
|
return (EINVAL);
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
tmpenv = g_strdup(env);
|
2015-02-06 18:59:53 +01:00
|
|
|
envsave = tmpenv;
|
|
|
|
|
|
|
|
do {
|
|
|
|
envvar = strchr(tmpenv, ',');
|
|
|
|
if (envvar != NULL) {
|
|
|
|
*envvar = '\0';
|
|
|
|
}
|
|
|
|
if ((*callback)(envlist, tmpenv) != 0) {
|
|
|
|
ret = errno;
|
|
|
|
break;
|
2023-03-15 04:26:49 +01:00
|
|
|
}
|
2015-02-06 18:59:53 +01:00
|
|
|
tmpenv = envvar + 1;
|
|
|
|
} while (envvar != NULL);
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2017-03-20 18:38:28 +01:00
|
|
|
g_free(envsave);
|
2015-02-06 18:59:53 +01:00
|
|
|
return ret;
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets environment value to envlist in similar manner
|
|
|
|
* than putenv(3).
|
|
|
|
*
|
|
|
|
* Returns 0 in success, errno otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
envlist_setenv(envlist_t *envlist, const char *env)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
struct envlist_entry *entry = NULL;
|
|
|
|
const char *eq_sign;
|
|
|
|
size_t envname_len;
|
|
|
|
|
|
|
|
if ((envlist == NULL) || (env == NULL))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* find out first equals sign in given env */
|
|
|
|
if ((eq_sign = strchr(env, '=')) == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
envname_len = eq_sign - env + 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there already exists variable with given name
|
|
|
|
* we remove and release it before allocating a whole
|
|
|
|
* new entry.
|
|
|
|
*/
|
|
|
|
for (entry = envlist->el_entries.lh_first; entry != NULL;
|
|
|
|
entry = entry->ev_link.le_next) {
|
|
|
|
if (strncmp(entry->ev_var, env, envname_len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry != NULL) {
|
|
|
|
QLIST_REMOVE(entry, ev_link);
|
|
|
|
g_free((char *)entry->ev_var);
|
|
|
|
g_free(entry);
|
|
|
|
} else {
|
|
|
|
envlist->el_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = g_malloc(sizeof(*entry));
|
|
|
|
entry->ev_var = g_strdup(env);
|
|
|
|
QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
|
|
|
|
|
|
|
|
return (0);
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes given env value from envlist in similar manner
|
|
|
|
* than unsetenv(3). Returns 0 in success, errno otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
envlist_unsetenv(envlist_t *envlist, const char *env)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
struct envlist_entry *entry;
|
|
|
|
size_t envname_len;
|
|
|
|
|
|
|
|
if ((envlist == NULL) || (env == NULL))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* env is not allowed to contain '=' */
|
|
|
|
if (strchr(env, '=') != NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out the requested entry and remove
|
|
|
|
* it from the list.
|
|
|
|
*/
|
|
|
|
envname_len = strlen(env);
|
|
|
|
for (entry = envlist->el_entries.lh_first; entry != NULL;
|
|
|
|
entry = entry->ev_link.le_next) {
|
|
|
|
if (strncmp(entry->ev_var, env, envname_len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry != NULL) {
|
|
|
|
QLIST_REMOVE(entry, ev_link);
|
|
|
|
g_free((char *)entry->ev_var);
|
|
|
|
g_free(entry);
|
|
|
|
|
|
|
|
envlist->el_count--;
|
|
|
|
}
|
|
|
|
return (0);
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns given envlist as array of strings (in same form that
|
|
|
|
* global variable environ is). Caller must free returned memory
|
2017-03-20 18:38:28 +01:00
|
|
|
* by calling g_free for each element and the array.
|
|
|
|
* Returned array and given envlist are not related (no common
|
|
|
|
* references).
|
2009-01-30 20:59:17 +01:00
|
|
|
*
|
|
|
|
* If caller provides count pointer, number of items in array is
|
2017-03-20 18:38:28 +01:00
|
|
|
* stored there.
|
2009-01-30 20:59:17 +01:00
|
|
|
*/
|
|
|
|
char **
|
|
|
|
envlist_to_environ(const envlist_t *envlist, size_t *count)
|
|
|
|
{
|
2023-03-15 04:26:49 +01:00
|
|
|
struct envlist_entry *entry;
|
|
|
|
char **env, **penv;
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
penv = env = g_new(char *, envlist->el_count + 1);
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
for (entry = envlist->el_entries.lh_first; entry != NULL;
|
|
|
|
entry = entry->ev_link.le_next) {
|
|
|
|
*(penv++) = g_strdup(entry->ev_var);
|
|
|
|
}
|
|
|
|
*penv = NULL; /* NULL terminate the list */
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
if (count != NULL)
|
|
|
|
*count = envlist->el_count;
|
2009-01-30 20:59:17 +01:00
|
|
|
|
2023-03-15 04:26:49 +01:00
|
|
|
return (env);
|
2009-01-30 20:59:17 +01:00
|
|
|
}
|