2 * Copyright 2022 Collabora, Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <wayland-util.h>
39 #include "os-compatibility.h"
40 #include "process-util.h"
41 #include "string-helpers.h"
43 extern char **environ; /* defined by libc */
46 fdstr_update_str1(struct fdstr *s)
48 snprintf(s->str1, sizeof(s->str1), "%d", s->fds[1]);
52 fdstr_set_fd1(struct fdstr *s, int fd)
60 fdstr_clear_cloexec_fd1(struct fdstr *s)
62 return os_fd_clear_cloexec(s->fds[1]) >= 0;
66 fdstr_close_all(struct fdstr *s)
70 for (i = 0; i < ARRAY_LENGTH(s->fds); i++) {
78 custom_env_init_from_environ(struct custom_env *env)
83 wl_array_init(&env->envp);
84 env->env_finalized = false;
85 wl_array_init(&env->argp);
86 env->arg_finalized = false;
88 for (it = environ; *it; it++) {
89 ep = wl_array_add(&env->envp, sizeof *ep);
97 custom_env_fini(struct custom_env *env)
101 wl_array_for_each(p, &env->envp)
103 wl_array_release(&env->envp);
105 wl_array_for_each(p, &env->argp)
107 wl_array_release(&env->argp);
111 custom_env_get_env_var(struct custom_env *env, const char *name)
114 size_t name_len = strlen(name);
116 wl_array_for_each(ep, &env->envp) {
119 if (strncmp(entry, name, name_len) == 0 &&
120 entry[name_len] == '=') {
129 custom_env_add_arg(struct custom_env *env, const char *arg)
133 assert(!env->arg_finalized);
135 ap = wl_array_add(&env->argp, sizeof *ap);
143 custom_env_set_env_var(struct custom_env *env, const char *name, const char *value)
147 assert(strchr(name, '=') == NULL);
148 assert(!env->env_finalized);
150 ep = custom_env_get_env_var(env, name);
154 ep = wl_array_add(&env->envp, sizeof *ep);
157 str_printf(ep, "%s=%s", name, value);
162 * Add information from a parsed exec string to a custom_env
164 * An 'exec string' is a string in the format:
165 * ENVFOO=bar ENVBAR=baz /path/to/exec --arg anotherarg
167 * This function will parse such a string and add the specified environment
168 * variables (in the format KEY=value) up until it sees a non-environment
169 * string, after which point every entry will be interpreted as a new
172 * Entries are space-separated; there is no support for quoting.
175 custom_env_add_from_exec_string(struct custom_env *env, const char *exec_str)
177 char *dup_path = strdup(exec_str);
178 char *start = dup_path;
182 /* Build the environment array (if any) by handling any number of
183 * equal-separated key=value at the start of the string, split by
184 * spaces; uses "foo=bar baz=quux meh argh" as the example, where
185 * "foo=bar" and "baz=quux" should go into the environment, and
186 * "meh" should be executed with "argh" as its first argument */
188 char *k = NULL, *v = NULL;
191 /* Leaves us with "foo\0bar baz=quux meh argh", with k pointing
192 * to "foo" and v pointing to "bar baz=quux meh argh" */
193 for (p = start; *p && !isspace(*p); p++) {
205 /* Walk to the next space or NUL, filling any trailing spaces
206 * with NUL, to give us "foo\0bar\0\0baz=quux meh argh".
207 * k will point to "foo", v will point to "bar", and
208 * start will point to "baz=quux meh argh". */
209 while (*p && !isspace(*p))
211 while (*p && isspace(*p))
215 custom_env_set_env_var(env, k, v);
218 /* Now build the argv array by splitting on spaces */
223 for (p = start; *p && !isspace(*p); p++)
229 while (*p && isspace(*p))
232 custom_env_add_arg(env, start);
240 custom_env_get_envp(struct custom_env *env)
244 assert(!env->env_finalized);
246 /* add terminating NULL */
247 ep = wl_array_add(&env->envp, sizeof *ep);
251 env->env_finalized = true;
253 return env->envp.data;
257 custom_env_get_argp(struct custom_env *env)
261 assert(!env->arg_finalized);
263 /* add terminating NULL */
264 ap = wl_array_add(&env->argp, sizeof *ap);
268 env->arg_finalized = true;
270 return env->argp.data;